H6.1 UART Frame Codec

EasyFixIcarus · browser samples / server checksReview the topic

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

SignalWidthRole
data_byte8input of each check
parity_mode2input of each check
received11input of each check
Full specification
The function (keep this name and these types)
function automatic logic [79:0] frame_codec(input logic [7:0] data_byte,
                                            input logic [1:0] parity_mode,
                                            input logic [10:0] received);
PartWhat the line carries
IdleThe line is high (1) between frames.
Start bitOne low (0) bit begins a frame.
Data bitsEight data bits follow, least significant bit (LSB) first.
Parity bitparity_mode 0 sends no parity bit. Mode 1 is even parity and mode 2 is odd parity, over the eight data bits.
Stop bitOne 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 resultHolds
79:24Always 0.
23stop_error: the received stop bit is low.
22parity_error: a parity mode is on and the received parity bit is wrong for the received data.
21start_error: received bit 0, the start bit, is high.
20:19Always 0.
18:11decoded: received bits 8:1, the data, LSB first.
10:0encoded: the frame for data_byte. Bit 0 is the first bit sent.
Frame bitWithout parity (mode 0)With parity (mode 1 or 2)
0Start (0)Start (0)
8:1Data, bit 0 of the byte in frame bit 1Data, bit 0 of the byte in frame bit 1
9Stop (1)Parity
10Not part of the frame: encoded 0, received ignoredStop (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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Good Frame Without Parity

Input
data_byte = 0x55, parity_mode = 0, received = 11'b010_1010_1010
Expected 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.

Run samples executes all 3 public checks. Check all tests runs 336 checks on the server.

Icarus · browser samples / server checks