H6.2 Timed UART Transmitter

MediumFixIcarus · browser samples / server checksReview the topic

Fix a clocked UART transmitter that sends its data bits in the wrong order.

Hardware context: UART transmitter/receiver with a specified baud and sampling contract. Check transmitted bit durations and the configured inter-frame idle requirement.

A UART link passed its first bring-up test with bytes like 0x00, 0xff and 0xa5. Then a host received 0xcc when the board sent 0x33. The bit timing was right, but the bits came in the wrong order.

The module uart_tx sends one frame per start request. Each bit stays on tx for divisor clock edges. busy is high while a frame is on the line. done pulses for one edge when the frame is over.

The tests request frames at edges 0 and 80 and an extra start at edge 3, which must be ignored. Some tests reset the transmitter in the middle of a frame. They record tx, busy and done after every edge for 160 edges. Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
baud_divisor3input of each check
parity2input of each check
cancel_tick8input of each check
Full specification
The ports (keep them)
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);
  // your transmitter
endmodule
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.

Transmitter ruleMeaning
StartA start on an edge where busy = 0 begins a frame. Right after that edge, tx = 0 (the start bit) and busy = 1.
CaptureThat edge also captures data_byte, parity_mode and divisor. Later changes on those inputs do not affect the frame.
BusyA start while busy = 1 is ignored.
Bit timeEach frame bit stays on tx for exactly divisor edges, the stop bit included.
DoneOn the edge after the full stop bit, tx = 1, busy = 0 and done = 1. done is 1 for that one edge only.
Resetreset is synchronous and active high, and it wins over start. It ends any frame at once: tx = 1, busy = 0, and no done pulse.

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 three digits: tx, busy and done. The 160 edges give a 480-digit 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 Frames Without Parity, Two Edges per Bit
Input
divisor = 2, parity_mode = 0, first byte 0xa5, second byte 0x81, no reset
Output
actual_length = 480, expected_length = 480, match = yes

After edges 0 and 1, tx = 0 for the start bit. The data bits of 0xa5 follow LSB first, two edges each. The stop bit is high after edges 18 and 19, and done = 1 after edge 20. The start at edge 3 is ignored. The second frame starts at edge 80 and ends with done after edge 100.

Example 2

Even Parity, Four Edges per Bit
Input
divisor = 4, parity_mode = 1, first byte 0x00, second byte 0xff, no reset
Output
actual_length = 480, expected_length = 480, match = yes

For 0x00, tx stays low for 40 edges: the start, eight data bits and a parity bit of 0. The stop bit fills edges 40 to 43, and done = 1 after edge 44. For 0xff, the parity bit is 0 again, after edges 116 to 119.

Example 3

Reset in the Middle of a Frame
Input
divisor = 3, parity_mode = 2, first byte 0x5a, second byte 0x33, reset at edge 13
Output
actual_length = 480, expected_length = 480, match = yes

Reset at edge 13 cuts the first frame. tx goes high and busy drops, with no done pulse. The frame for 0x33 starts at edge 80 and runs to the end. Its 11 bits take 33 edges, so done = 1 after edge 113.

Example 4

One Edge per Bit Shows the Order
Input
divisor = 1, parity_mode = 0, first byte 0xce, second byte 0xd5, no reset
Output
actual_length = 480, expected_length = 480, match = yes

0xce is 1100 1110 in binary. LSB first, tx shows 0, 1, 1, 1, 0, 0, 1, 1 after edges 1 to 8. The stop bit follows after edge 9, and done = 1 after edge 10.

Constraints and Contract

  • divisor is 1 to 4. Inputs change while the clock is low. All inputs are 0 or 1, never x or z.
  • Keep the module name and the ports.

Hints

Hint 1

Write out the frame for 0x33 by hand, bit by bit. Then compare it with the bits the module puts on tx.

Hint 2

Bytes like 0x00, 0xff, 0xa5 and 0x5a read the same from either end. A bit-order bug hides behind them.

Hint 3

Look at how the start branch loads held[8:1]. A UART sends bit 0 of the byte first, so data_byte goes in unchanged.

What the grader checks: The tests run every divisor from 1 to 4 with every parity mode, random bytes, and resets at several points. They compare tx, busy and done on every edge. They check bit timing in clock edges, not electrical timing or baud-rate tolerance.

39 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

Two Frames Without Parity, Two Edges per Bit

Input
divisor = 2, parity_mode = 0, first byte 0xa5, second byte 0x81, no reset
Expected output
actual_length = 480, expected_length = 480, match = yes

After edges 0 and 1, tx = 0 for the start bit. The data bits of 0xa5 follow LSB first, two edges each. The stop bit is high after edges 18 and 19, and done = 1 after edge 20. The start at edge 3 is ignored. The second frame starts at edge 80 and ends with done after edge 100.

Run samples executes all 4 public checks. Check all tests runs 39 checks on the server.

Icarus · browser samples / server checks