Fix a 16-times oversampling UART receiver whose bit timer is one tick short, so its sample points drift.
Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Recover receive data under the specified oversampling and clock-mismatch bounds.
A UART receiver returned wrong bytes, and the high bits were the worst. Each sample point came one edge earlier than the one before.
The module uart_rx samples serial_in 16 times per bit. It waits for eight low samples in a row to accept a start bit. It then samples each data bit near its middle and checks parity and the stop bit. It also reports a break, a line held low for a long time.
The tests feed it 768 samples per run. Some transmitters use bits of 15 or 17 samples instead of 16. The expected bytes come from the correct sample points, so a sample one edge early can change a byte. 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. |
Each test lists the low ranges of serial_in, both ends included. All other samples are high.
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
Two Bytes Without Parity- Input
parity_mode = 0; serial_in low at 3-18, 35-50, 67-98, 115-130, 179-194 and 211-306
- Output
actual_length = 6144, expected_length = 6144, match = yes
The first frame starts low at sample 3, so the eighth low is edge 10. The stop bit is sampled at edge 10 + 144 = 154, where valid pulses with 0xa5. The second frame gives valid with 0x81 at edge 330.
Example 2
Even Parity- Input
parity_mode = 1; serial_in low at 2-49, 114-161, 187-202, 219-234, 251-266, 283-298 and 315-346
- Output
actual_length = 6144, expected_length = 6144, match = yes
With parity, the stop bit is sampled 160 edges after the start. valid pulses with 0x3c at edge 169 and with 0x55 at edge 354.
Example 3
Odd Parity with 0x00 and 0xff- Input
parity_mode = 2; serial_in low at 4-147 and 185-200
- Output
actual_length = 6144, expected_length = 6144, match = yes
The first frame is the start, eight 0s and an odd parity bit of 1. valid pulses with 0x00 at edge 171, then with 0xff at edge 352.
Example 4
A Slow Transmitter- Input
parity_mode = 0; bits of 17 samples carry 0x96, then a 16-sample frame carries 0x21
- Output
actual_length = 6144, expected_length = 6144, match = yes
The sample points fall one sample further behind in each bit. The sample for bit 7 lands in bit 6, so 0x96 arrives as 0x16. The stop sample is still high, so valid pulses at edge 154. The expected result follows these real sample points.
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 valid pulse in a failing test. Is it on time? Count the edges between two data samples.
Hint 2
Each bit lasts 16 samples, so two data samples must be 16 edges apart. If they are 15 apart, each sample comes one edge earlier than the one before.
Hint 3
Look at every place that loads timer. It is loaded with 15. It must be 16, at the start and after each sample.
What the grader checks: The tests cover all parity modes with bits of 15, 16 and 17 samples, a glitch, a parity error and a break. Every output is compared on all 768 edges. Samples are clean 0s and 1s; no synchronizer, majority vote or analog timing is modeled.
15 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