Fix a UART frame codec that gets odd parity wrong in both directions.
Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Encode and decode the configured UART frame fields.
A UART monitor reported a parity error on every good frame from an odd-parity device. Its own encoder also sent the wrong parity bit. Even-parity links and links without parity worked.
The function frame_codec does two jobs. It encodes data_byte into the bits of one frame. It also decodes a received frame and reports which parts are wrong: the start, the parity or the stop bit.
The tests call it with all three parity modes, many bytes, and frames with one broken field. They compare every output bit. Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
data_byte | 8 | input of each check |
parity_mode | 2 | input of each check |
received | 11 | input of each check |
Full specification
function automatic logic [79:0] frame_codec(input logic [7:0] data_byte,
input logic [1:0] parity_mode,
input logic [10:0] received);
| 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.
| Bits of the result | Holds |
|---|---|
| 79:24 | Always 0. |
| 23 | stop_error: the received stop bit is low. |
| 22 | parity_error: a parity mode is on and the received parity bit is wrong for the received data. |
| 21 | start_error: received bit 0, the start bit, is high. |
| 20:19 | Always 0. |
| 18:11 | decoded: received bits 8:1, the data, LSB first. |
| 10:0 | encoded: the frame for data_byte. Bit 0 is the first bit sent. |
| Frame bit | Without parity (mode 0) | With parity (mode 1 or 2) |
|---|---|---|
| 0 | Start (0) | Start (0) |
| 8:1 | Data, bit 0 of the byte in frame bit 1 | Data, bit 0 of the byte in frame bit 1 |
| 9 | Stop (1) | Parity |
| 10 | Not part of the frame: encoded 0, received ignored | Stop (1) |
The function always decodes the received data, even when the frame has errors. A caller checks the error bits before it uses the byte.
Examples
Example 1
A Good Frame Without Parity- Input
data_byte = 0x55, parity_mode = 0, received = 11'b010_1010_1010
- Output
stop_error = no, parity_error = no, start_error = no, decoded = 0x55, encoded = 'b01010101010
The frame is start 0, the data 0x55 LSB first, then stop 1 in bit 9. The received frame is the same, so no error is set. Bit 10 is 0 because the frame has only 10 bits.
Example 2
One Flipped Parity Bit- Input
data_byte = 0xa5, parity_mode = 1 (even), received = the good frame with bit 9 flipped
- Output
stop_error = no, parity_error = yes, start_error = no, decoded = 0xa5, encoded = 'b10101001010
0xa5 has four 1s, so even parity sends 0 in bit 9. The received frame has 1 there. Only parity_error is set, and the data still decodes as 0xa5.
Example 3
Every Field Broken- Input
data_byte = 0x00, parity_mode = 2 (odd), received = 11'b000_0000_0001
- Output
stop_error = yes, parity_error = yes, start_error = yes, decoded = 0x0, encoded = 'b11000000000
The start bit is high, the stop bit is low, and odd parity for 0x00 needs a 1. All three errors are set. The encoded frame has parity 1 in bit 9 and stop 1 in bit 10.
Constraints and Contract
- parity_mode is 0, 1 or 2. data_byte and received can take any value.
- Keep the function name, the argument types and the 80-bit return type.
Hints
Hint 1
Encode one byte with each parity mode by hand. Count the 1s in the data, then pick the parity bit for even and for odd parity.
Hint 2
Try data_byte = 0 in mode 2. The data has no 1s, so odd parity needs a parity bit of 1. What does the function send and expect?
Hint 3
Look at parity_value and the parity check. Both use the XOR of the data. For odd parity that XOR must be inverted when parity_mode == 2.
What the grader checks: The tests cover all three parity modes, every seventh byte value, and frames with a broken start, parity or stop bit. They check the frame layout and the error bits, not timing on a real line.
336 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