UART Transmitter/Receiver
Build and verify an 8-N-1 UART link. Learn how serial bits become a frame, how baud timing affects sampling, and how a self-checking testbench finds parity and framing errors.
What you will build
You will verify two RTL blocks:
uart_txaccepts a byte and serializes it ontotx.uart_rxsamplesrx, reconstructs a byte, and reports completion or errors.
The project uses the common 8-N-1 format: eight data bits, no parity bit, and one stop bit. The least-significant data bit is transmitted first. The testbench sends known bytes, checks the received byte, and deliberately creates invalid timing and framing conditions.
Step 1: Understand the UART frame
A UART line is idle high. A frame begins with a low start bit, followed by data bits, then a high stop bit.
idle start data bits, least-significant first stop
1 0 d0 d1 d2 d3 d4 d5 d6 d7 1
| |
+------------- one complete 8-N-1 frame ------+
| Part | Level | Duration | Purpose |
|---|---|---|---|
| Idle | 1 | until a transfer begins | marks the line as inactive |
| Start bit | 0 | 1 baud period | lets the receiver identify a frame |
| Data bits | 0 or 1 | 8 baud periods | transmits the byte, LSB first |
| Stop bit | 1 | 1 baud period | confirms the frame ended correctly |
For a byte 8'hA5, binary is 1010_0101, but the line sends 1, 0, 1, 0, 0, 1, 0, 1 because transmission starts with bit zero.
Step 2: Choose timing parameters
For a simulation-friendly example, use a 10 MHz clock and a baud rate of 1 MHz. That gives ten system-clock cycles per UART bit.
localparam int CLOCK_HZ = 10_000_000;
localparam int BAUD_RATE = 1_000_000;
localparam int CLKS_PER_BIT = CLOCK_HZ / BAUD_RATE; // 10
A transmitter changes the line only at bit boundaries. A receiver should not sample exactly at the edge; it should detect the start edge and sample near the middle of each bit.
CLKS_PER_BIT, the error accumulates across the frame. A small mismatch may pass short frames and fail longer ones.Step 3: Create the transmitter
The transmitter uses a shift register and a bit counter. When start is asserted while idle, it loads the frame and begins shifting one bit per baud period.
module uart_tx #(
parameter int CLKS_PER_BIT = 10
)(
input logic clk,
input logic rst_n,
input logic start,
input logic [7:0] data_in,
output logic tx,
output logic busy,
output logic done
);
logic [9:0] frame;
logic [3:0] bit_index;
integer baud_count;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
frame <= '1;
bit_index <= 0;
baud_count <= 0;
tx <= 1'b1;
busy <= 1'b0;
done <= 1'b0;
end else begin
done <= 1'b0;
if (!busy && start) begin
frame <= {1'b1, data_in, 1'b0};
tx <= 1'b0;
busy <= 1'b1;
bit_index <= 0;
baud_count <= 0;
end else if (busy) begin
if (baud_count == CLKS_PER_BIT - 1) begin
baud_count <= 0;
if (bit_index == 9) begin
busy <= 1'b0;
done <= 1'b1;
tx <= 1'b1;
end else begin
bit_index <= bit_index + 1'b1;
tx <= frame[bit_index + 1'b1];
end
end else begin
baud_count <= baud_count + 1;
end
end
end
end
endmodule
What to notice
- The idle output is high.
- The frame is ordered as stop, data, start so shifting selects start first and data follows LSB first.
busyprevents a new byte from replacing an active frame.doneis a one-cycle pulse at the end of the stop bit.- Reset returns the line to the UART idle state.
Step 4: Create the receiver
The receiver waits for a falling edge, waits half a bit to reach the center of the start bit, then samples the center of each data bit.
module uart_rx #(
parameter int CLKS_PER_BIT = 10
)(
input logic clk,
input logic rst_n,
input logic rx,
output logic [7:0] data_out,
output logic valid,
output logic framing_error
);
typedef enum logic [1:0] {IDLE, START, DATA, STOP} state_t;
state_t state;
integer baud_count;
logic [2:0] bit_index;
logic [7:0] data_shift;
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
state <= IDLE;
baud_count <= 0;
bit_index <= 0;
data_shift <= 0;
data_out <= 0;
valid <= 1'b0;
framing_error <= 1'b0;
end else begin
valid <= 1'b0;
framing_error <= 1'b0;
case (state)
IDLE: begin
if (!rx) begin
state <= START;
baud_count <= 0;
end
end
START: begin
if (baud_count == (CLKS_PER_BIT / 2) - 1) begin
baud_count <= 0;
state <= rx ? IDLE : DATA;
end else baud_count <= baud_count + 1;
end
DATA: begin
if (baud_count == CLKS_PER_BIT - 1) begin
baud_count <= 0;
data_shift[bit_index] <= rx;
if (bit_index == 7) state <= STOP;
else bit_index <= bit_index + 1'b1;
end else baud_count <= baud_count + 1;
end
STOP: begin
if (baud_count == CLKS_PER_BIT - 1) begin
baud_count <= 0;
if (rx) begin
data_out <= data_shift;
valid <= 1'b1;
end else framing_error <= 1'b1;
state <= IDLE;
end else baud_count <= baud_count + 1;
end
endcase
end
end
endmodule
The receiver does not declare a byte valid until the stop bit is sampled high. If the stop bit is low, it raises framing_error and discards the frame.
Complete working code: self-checking loopback testbench
Loopback connects the transmitter output to the receiver input. This is useful because one test can verify serialization, sampling, reconstruction, and the valid handshake.
module tb_uart;
localparam int CLKS_PER_BIT = 10;
logic clk = 0;
logic rst_n = 0;
logic start;
logic [7:0] tx_data;
logic tx_line;
logic tx_busy;
logic tx_done;
logic [7:0] rx_data;
logic rx_valid;
logic framing_error;
always #5 clk = ~clk;
uart_tx #(.CLKS_PER_BIT(CLKS_PER_BIT)) tx_i (
.clk(clk), .rst_n(rst_n), .start(start),
.data_in(tx_data), .tx(tx_line),
.busy(tx_busy), .done(tx_done)
);
uart_rx #(.CLKS_PER_BIT(CLKS_PER_BIT)) rx_i (
.clk(clk), .rst_n(rst_n), .rx(tx_line),
.data_out(rx_data), .valid(rx_valid),
.framing_error(framing_error)
);
task automatic send_and_check(input logic [7:0] expected);
@(posedge clk);
wait (!tx_busy);
tx_data <= expected;
start <= 1'b1;
@(posedge clk);
start <= 1'b0;
wait (rx_valid || framing_error);
if (framing_error)
$error("Unexpected framing error for 0x%02h", expected);
else if (rx_data !== expected)
$error("UART mismatch: expected=0x%02h actual=0x%02h", expected, rx_data);
else
$display("UART PASS: transmitted and received 0x%02h", expected);
endtask
initial begin
start = 1'b0;
tx_data = 8'h00;
repeat (3) @(posedge clk);
rst_n = 1'b1;
send_and_check(8'h55);
send_and_check(8'h00);
send_and_check(8'hFF);
$display("UART loopback test complete");
$finish;
end
endmodule
The task waits for the transmitter to become idle, applies one byte, waits for a receiver event, and checks the result. It does not assume that the byte arrived merely because the transmitter pulsed done.
Step 6: Add error injection
Good verification does not test only clean frames. For a framing-error test, drive the receiver line low during the stop-bit sample.
task automatic send_bad_stop_bit(input logic [7:0] value);
// Drive start and eight data bits at the agreed baud period.
rx_line = 1'b0;
#(CLKS_PER_BIT * 10);
for (int bit_number = 0; bit_number < 8; bit_number++) begin
rx_line = value[bit_number];
#(CLKS_PER_BIT * 10);
end
// A valid stop bit is high. Keep it low to inject the error.
rx_line = 1'b0;
#(CLKS_PER_BIT * 10);
if (!framing_error)
$error("Expected a framing error for a low stop bit");
else
$display("UART PASS: framing error detected");
rx_line = 1'b1;
endtask
| Injected condition | Expected result |
|---|---|
| Wrong data bit | valid may assert with a different byte; scoreboard reports mismatch. |
| Low stop bit | framing_error asserts and valid remains low. |
| False short start pulse | receiver returns to idle without producing a byte. |
| Reset during frame | receiver discards partial data and returns to idle. |
Step 7: Expected output and waveform
UART PASS: transmitted and received 0x55
UART PASS: transmitted and received 0x00
UART PASS: transmitted and received 0xff
UART PASS: framing error detected
UART loopback test complete
In the waveform, check that the line is idle high, begins with a low start bit, sends bit zero first, holds each bit for the expected number of clock cycles, and ends with a high stop bit. Also check that rx_valid is a pulse and that framing_error is not asserted for a valid frame.
Step 8: UVM architecture extension
Once the directed loopback test works, move the protocol work into a UVM agent. The sequence creates byte transactions; the driver serializes them; the monitor reconstructs frames; and the scoreboard compares received transactions with expected bytes.
class uart_item extends uvm_sequence_item;
rand bit [7:0] data;
bit parity_error;
bit framing_error;
`uvm_object_utils(uart_item)
function new(string name = "uart_item");
super.new(name);
endfunction
endclass
class uart_scoreboard extends uvm_scoreboard;
`uvm_component_utils(uart_scoreboard)
uvm_tlm_analysis_fifo #(uart_item) expected_fifo;
uvm_tlm_analysis_fifo #(uart_item) actual_fifo;
task run_phase(uvm_phase phase);
uart_item expected;
uart_item actual;
forever begin
expected_fifo.get(expected);
actual_fifo.get(actual);
if (actual.data !== expected.data)
`uvm_error("UART_SB", "Received byte does not match expected byte")
if (actual.framing_error || actual.parity_error)
`uvm_error("UART_SB", "Unexpected UART error on clean transaction")
end
endtask
endclass
Keep the byte-level transaction separate from pin timing. That lets the same sequence drive a real UART agent, a loopback model, or a different baud-rate configuration.
Project checklist
- Implement or review the 8-N-1 frame format and timing constants.
- Run clean loopback transfers for
8'h55,8'h00,8'hFF, and an alternating pattern. - Verify reset while idle and reset during an active frame.
- Check transmitter busy/done behavior and receiver valid/error exclusivity.
- Inject a low stop bit and confirm framing error detection.
- Change the baud divider by one count and observe the resulting timing failure.
- Capture console output and waveform evidence.
- Refactor the working test into a UVM item, sequence, driver, monitor, and scoreboard.
What you learned
- UART verification is a timing and framing problem, not only a byte comparison.
- The receiver should sample near the center of each bit for timing tolerance.
- Busy, valid, done, and error signals define useful transaction boundaries.
- Error injection proves that the environment detects prohibited behavior.
- A directed loopback test is a useful first step before building a reusable UVM agent.