Find and fix the one bug in a UART made of a transmitter, a receiver and a receive buffer.
Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Qualify bidirectional UART traffic, buffering, overrun and reset-mid-frame behavior.
A UART recovered after a reset in most tests. But when reset came in the middle of a received frame, a framing error appeared later.
The design has three modules. uart_tx sends frames, uart_rx receives frames from 16 samples per bit, and uart_buffer holds up to two received bytes. One of them has one bug.
The tests drive both directions at once. The transmitter sends four bytes at fixed times. The receiver gets its own samples, with bad frames, breaks and resets. Software pops and clears the buffer at chosen edges. Every output is compared 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_tx(input logic clk, reset, start, input logic [7:0] data_byte,
input logic [1:0] parity_mode, input logic [7:0] divisor,
output logic tx, busy, done);
// ...
endmodule
module uart_rx(input logic clk, reset, serial_in, input logic [1:0] parity_mode,
output logic valid, parity_error, frame_error, break_event,
output logic [7:0] data_byte);
// ...
endmodule
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);
// ...
endmodule
| Part | What the line carries |
|---|---|
| Idle | The line is high (1) between frames. |
| Start bit | One low (0) bit begins a frame. |
| Data bits | Eight data bits follow, least significant bit (LSB) first. |
| Parity bit | parity_mode 0 sends no parity bit. Mode 1 is even parity and mode 2 is odd parity, over the eight data bits. |
| Stop bit | One high bit ends the frame. A frame is 10 bits without parity and 11 bits with it. |
Even parity makes the count of 1s in the data and parity bits even. Odd parity makes that count odd.
| Transmitter rule | Meaning |
|---|---|
| Start | A start on an edge where busy = 0 begins a frame and captures the byte, parity mode and divisor. A start while busy is ignored. |
| Bit time | Each frame bit, the stop bit included, stays on tx for divisor edges. |
| Done | On the edge after the full stop bit, tx = 1, busy = 0 and done = 1 for that edge only. |
| Reset | Reset ends any frame at once with tx = 1, busy = 0 and no done pulse. |
| Traffic | Starts come at edges 0, 240, 480 and 720 with bytes 0xa5, 0x83, 0x3c and 0x66. divisor is 2 + parity_mode. |
| 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. |
| 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 transmitter and the receiver share clk, reset and parity_mode. tx is not looped back; the receiver gets its own samples.
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 receiver outputs, the buffer outputs, and then tx, busy and done as three digits. 768 edges give a 13824-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
Both Directions, No Errors- Input
parity_mode = 0; receive 0xa5 and 0x81; pop at edges 600, 601 and 602
- Output
actual_length = 13824, expected_length = 13824, match = yes
The transmitter sends four frames with two edges per bit. Each pulses done 20 edges after its start: at edges 20, 260, 500 and 740. The receiver gives 0xa5 at edge 154 and 0x81 at edge 330. The pops at edges 600 and 601 return them in order.
Example 2
A Parity Error on the Receive Side- Input
parity_mode = 1; receive 0x33 with a flipped parity bit, then 0x77; clear_status at edge 178
- Output
actual_length = 13824, expected_length = 13824, match = yes
The receiver pulses parity_error at edge 169, and status bit 0 is set at edge 170. clear_status at edge 178 clears it. 0x77 arrives at edge 349 and is popped at edge 600. Meanwhile the transmitter sends 11-bit frames with three edges per bit; the first ends with done at edge 33.
Example 3
Reset in the Middle of Both Directions- Input
parity_mode = 0; serial_in low at 2-145 and 182-197; reset at edge 10; pop every 19 edges
- Output
actual_length = 13824, expected_length = 13824, match = yes
At edge 10 the transmitter is sending 0xa5 and the receiver is inside a frame. Reset cancels both, with no done and no error. The receiver waits for the high at sample 146. It then gives 0xff at edge 333, and the pop at edge 342 returns it. The later transmit frames run normally.
Constraints and Contract
- Submit uart_tx, uart_rx and uart_buffer together.
- serial_in is already synchronized to clk. All inputs are 0 or 1, never x or z.
Hints
Hint 1
Find the first edge where your string differs. Its position inside the record for that edge tells you which module is wrong.
Hint 2
The failing tests all reset while traffic is on the line. Check what each module does when reset arrives in the middle of its work.
Hint 3
Look at the reset branch of uart_rx. It must cancel a frame in progress, so it cannot depend on receiving.
What the grader checks: The tests run the three modules together in all parity modes. They use clean frames, bad frames, breaks, full buffers and resets at three points. Every output of every module is compared on all 768 edges. This is digital checking of the stated format, not analog or clock-domain-crossing signoff.
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