Fix a UART receiver that invents frames after a bad stop bit or a break.
Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Verify receiver recovery after glitches, parity, stop-bit and break faults.
After a line break, a UART receiver reported extra framing errors and lost the next real byte. The line was still low after the bad frame, and the receiver started a new frame right away.
The module uart_rx samples serial_in 16 times per bit and waits for eight low samples in a row to accept a start. It reports good bytes with valid. It reports bad frames with parity_error or frame_error, and a long low line with break_event.
A low stop bit means the line may still be low. The receiver must see a high sample before it accepts another start. The tests send bad parity, bad stop bits, glitches, breaks and resets mid-frame, each followed by a good frame. 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_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);
// your receiver
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.
| 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. |
A break therefore gives one frame_error, then one break_event, then nothing until the line goes high again.
Each test lists the low ranges of serial_in, both ends included. All other samples are high. Some tests also list reset edges.
The tests reset the module once at the beginning. Edges count from 0. Edge 0 is the first edge after the starting reset. After each edge they record valid, parity_error, frame_error and break_event as four digits. Then come a colon, data_byte in hex and a semicolon. 768 edges give a 6144-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
A Parity Error, Then a Good Byte- Input
parity_mode = 1; 0x33 with a flipped parity bit, then 0x77
- Output
actual_length = 6144, expected_length = 6144, match = yes
The first frame has a wrong parity bit, so parity_error pulses at edge 169. data_byte stays 0x00. The stop bit was high, so the receiver rearms at once. The next frame gives valid with 0x77 at edge 349.
Example 2
A Seven-Sample Glitch- Input
parity_mode = 0; serial_in low at 1-7, 10-25 and 42-137
- Output
actual_length = 6144, expected_length = 6144, match = yes
Seven lows are not a start, and the high at sample 8 clears the run. The frame that starts low at sample 10 gives valid with 0x81 at edge 161.
Example 3
A Break, Then Recovery- Input
parity_mode = 0; serial_in low at 2-211, 228-259, 276-339 and 356-371
- Output
actual_length = 6144, expected_length = 6144, match = yes
The start is accepted at edge 9, and the stop sample at edge 153 is low, so frame_error pulses. break_event pulses at edge 177, the 176th low. The receiver then waits for the high at sample 212. The next frame gives valid with 0x42 at edge 379.
Example 4
A Bad Stop Bit While the Line Stays Low- Input
parity_mode = 0; 0xa5 with a low stop bit, then 24 high samples, then 0x69
- Output
actual_length = 6144, expected_length = 6144, match = yes
frame_error pulses at edge 154. The line is still low for eight more samples, but the receiver is not armed. It rearms on the next high and gives valid with 0x69 at edge 338.
Constraints and Contract
- serial_in is already synchronized to clk. All inputs are 0 or 1, never x or z.
- Keep the module name and the ports.
Hints
Hint 1
Find the first frame_error in a failing test. What does the line do in the samples right after it?
Hint 2
Trace a frame with a low stop bit. If the receiver is armed at once, eight more lows start a frame that nobody sent.
Hint 3
Look at what the end of a frame does to armed. It should copy the stop sample, not always set armed to 1.
What the grader checks: The tests cover bad parity, bad stop bits, a glitch, a break and resets at three points. They use all parity modes and add clean frames. A fast transmitter sends frames back to back, so a start bit follows the stop sample at once. Every output is compared on all 768 edges. Samples are clean 0s and 1s; no synchronizer or analog timing is modeled.
29 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