Fix a two-byte UART receive buffer that drops a byte when a read frees a slot on the same edge.
Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Verify UART receive-queue overrun and break recovery before the next valid frame.
Software read a full UART receive buffer exactly as a new byte arrived. The read worked, but the new byte vanished and the overrun bit was set. A slot was free, so nothing should have been lost.
The module uart_buffer holds up to two received bytes, oldest first. Software takes bytes with pop. Sticky status bits record parity, framing, break and overrun errors until clear_status clears them.
A trusted receiver feeds the buffer. The tests fill the buffer, pop and clear at chosen edges, and send bad frames and breaks. They compare the receiver and buffer outputs on every edge. Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
parity | 2 | input of each check |
reset_count | 3 | input of each check |
Full specification
module uart_buffer(input logic clk, reset, put, input logic [7:0] received,
input logic parity_error, frame_error, break_event, pop, clear_status,
output logic [1:0] used, output logic [3:0] status,
output logic output_valid, output logic [7:0] output_byte);
// your buffer
endmodule
| Buffer rule | What a correct buffer does |
|---|---|
| Inputs | put, received and the three error inputs are the receiver outputs. The buffer acts on them one edge after the receiver pulses them. |
| Pop | A pop with at least one byte stored returns the oldest byte on output_byte with output_valid = 1. A pop on an empty buffer does nothing. |
| Pop first | The pop happens before the put on the same edge. So a full buffer can take a new byte on the edge that a pop frees a slot. |
| No bypass | A pop and a put on an empty buffer return nothing. The new byte is stored. |
| Overrun | A put to a full buffer, with no pop on that edge, drops the new byte and sets the overrun bit. |
| Status | status is sticky. Bit 0 is parity, bit 1 is framing, bit 2 is break and bit 3 is overrun. |
| Clear | clear_status clears the old bits first. Events on the same edge then set their bits, so a new event wins. |
| Output | output_valid is 1 only after an edge with a successful pop. output_byte keeps the last popped byte. |
| Reset | Reset empties the buffer and clears used, status, output_valid and output_byte. |
The receiver is correct. It never passes a bad frame or a break to the buffer as data. It follows these rules.
| Receiver rule | What a correct receiver does |
|---|---|
| Samples | serial_in is sampled on every rising edge. A nominal bit lasts 16 samples. |
| Arming | Reset leaves the receiver disarmed. A high sample arms it. A line that is already low cannot start a frame. |
| Start | While armed, count low samples in a row. A high sample sets the count back to 0. The eighth low sample in a row accepts the start. |
| Origin | Call the edge of that eighth low sample Q. The receiver captures parity_mode at Q. |
| Data | Data bit k is sampled at edge Q + 16 × (k + 1), for k = 0 to 7. That is near the middle of each bit. |
| Parity and stop | With parity, the parity bit is sampled at Q + 144 and the stop bit at Q + 160. Without parity, the stop bit is sampled at Q + 144. |
| Good frame | If parity and stop are both good, valid pulses for one edge at the stop sample. data_byte takes the new byte on that edge. |
| Bad frame | Otherwise parity_error and/or frame_error pulse for one edge. data_byte keeps the last good byte. |
| Rearm | If the stop sample was high, the receiver is armed again at once. If it was low, it waits for a high sample first. |
| Break | A separate counter counts low samples in a row. break_event pulses once on the 176th low. The counter stops there and restarts after any high sample. |
| Reset | reset is synchronous and active high. It wins over everything, cancels a frame in progress, disarms, and clears data_byte to 0. |
Each test lists the low ranges of serial_in and any reset edges. It also lists the edges where pop and clear_status are 1.
Edges count from 0. Edge 0 is the first edge after the starting reset. After each edge the tests record the four receiver flags, a colon, data_byte in hex and a semicolon. Then they record used, status as one hex digit, output_valid, a colon, output_byte in hex and a semicolon. 768 edges give an 11520-character string.
The result gives the length of your string, the length of the expected string, and match. match = yes only when the two strings are identical.
Examples
Example 1
Two Bytes Wait for Software- Input
parity_mode = 0; receive 0xa5 and 0x81; pop at edges 600, 601 and 602
- Output
actual_length = 11520, expected_length = 11520, match = yes
The receiver gives 0xa5 at edge 154, and the buffer stores it at edge 155. 0x81 is stored at edge 331, so used = 2. Pops at edges 600 and 601 return 0xa5 and then 0x81. The pop at edge 602 finds the buffer empty and does nothing.
Example 2
A Parity Error Sets a Sticky Bit- Input
parity_mode = 1; 0x33 with a flipped parity bit, then 0x77; clear_status at edge 178
- Output
actual_length = 11520, expected_length = 11520, match = yes
The receiver pulses parity_error at edge 169. At edge 170 status becomes 1, and nothing is stored. clear_status at edge 178 clears it. 0x77 is stored at edge 350 and popped at edge 600.
Example 3
A Third Byte Overruns- Input
parity_mode = 0; receive 0x11, 0x82 and 0xf3; no pop until edge 600
- Output
actual_length = 11520, expected_length = 11520, match = yes
The receiver gives 0xf3 at edge 477. At edge 478 two bytes are stored, so it is dropped, and status becomes 8, the overrun bit. Pops at edges 600 and 601 return 0x11 and 0x82.
Example 4
A Pop Frees the Slot Just in Time- Input
parity_mode = 0; receive 0x11, 0x82 and 0xf3; pop and clear_status at edge 478
- Output
actual_length = 11520, expected_length = 11520, match = yes
At edge 478 the pop returns 0x11 first. That frees a slot, so 0xf3 is stored, used stays 2 and status stays 0. Pops at edges 700 and 701 return 0x82 and 0xf3.
Constraints and Contract
- Submit uart_buffer only. The receiver is part of the tests.
- All inputs are 0 or 1, never x or z. Keep the module name and the ports.
Hints
Hint 1
Find a test where a pop and a new byte arrive on the same edge while two bytes are stored. What should used be after that edge?
Hint 2
Do the pop first, then decide the put from the count after the pop. The count from before the edge is too old.
Hint 3
Look at the put branch. It checks used, the register value from before the edge, instead of n, the count after the pop.
What the grader checks: A trusted receiver drives the buffer with clean frames, bad frames, breaks and resets in all parity modes. Pops and clears come at fixed, periodic and same-edge times. Every receiver and buffer output is compared on all 768 edges.
26 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 7 steps and the final capstone