Project: How Do You Build a UART in Verilog?
Count clocks. A UART sends each byte as a frame: a low start bit, eight data bits least significant first, and a high stop bit, each lasting one bit period. The transmitter is a small state machine that holds each bit on the line for CLKS_PER_BIT clocks. The receiver waits for the falling edge of a start bit, moves to the middle of it, and then samples once per bit period, so it reads every bit at its most stable point.
What you'll learn
- The 8N1 frame and how to work out the clock divider
- The transmitter state machine
- The receiver: synchronizer, start-bit check and mid-bit sampling
- A loopback testbench that checks every byte and a framing error
- How much clock mismatch a UART tolerates
Conventions and assumptions
A 50 MHz system clock and 115200 baud, 8 data bits, no parity, 1 stop bit (8N1). Both modules were simulated in Icarus Verilog 12 and linted with Verilator 5.036 with -Wall, with no warnings. Pin assignments for a real FPGA board depend on the board, so they are left to its constraint file.
Step 1: the frame and the divider
| Quantity | Value | How |
|---|---|---|
| Clocks per bit | 434 | 50,000,000 / 115,200 = 434.03, rounded |
| Actual baud rate | 115,207 | 50,000,000 / 434 |
| Baud error | +0.0064% | Far inside the tolerance |
| Frame length | 10 bits | 1 start + 8 data + 1 stop |
| Time per byte | 86.8 µs | 10 × 434 clocks × 20 ns |
Step 2: the transmitter
Four states: IDLE holds the line high; START drives it low for one bit period; DATA sends bit 0 to bit 7, each for one bit period; STOP drives it high for one more. A counter counts to CLKS_PER_BIT − 1 in each bit, and busy tells the user when a new byte can be started.
// UART transmitter: 8 data bits, no parity, 1 stop bit (8N1), LSB first.
// CLKS_PER_BIT = clock frequency / baud rate, e.g. 50 MHz / 115200 = 434.
module uart_tx #(parameter CLKS_PER_BIT = 434) (
input wire clk,
input wire rst,
input wire start, // pulse for one clock with data valid
input wire [7:0] data,
output reg tx, // idles high
output wire busy
);
localparam IDLE = 2'd0, START = 2'd1, DATA = 2'd2, STOP = 2'd3;
reg [1:0] state;
reg [15:0] count; // clocks spent in the current bit
reg [2:0] bit_idx;
reg [7:0] shifter;
assign busy = (state != IDLE);
always @(posedge clk) begin
if (rst) begin
state <= IDLE; tx <= 1'b1; count <= 0; bit_idx <= 0; shifter <= 0;
end else begin
case (state)
IDLE: begin
tx <= 1'b1;
count <= 0;
if (start) begin shifter <= data; state <= START; end
end
START: begin
tx <= 1'b0; // start bit
if (count == CLKS_PER_BIT - 1) begin count <= 0; bit_idx <= 0; state <= DATA; end
else count <= count + 1'b1;
end
DATA: begin
tx <= shifter[bit_idx]; // LSB first
if (count == CLKS_PER_BIT - 1) begin
count <= 0;
if (bit_idx == 3'd7) state <= STOP;
else bit_idx <= bit_idx + 1'b1;
end else count <= count + 1'b1;
end
STOP: begin
tx <= 1'b1; // stop bit
if (count == CLKS_PER_BIT - 1) begin count <= 0; state <= IDLE; end
else count <= count + 1'b1;
end
endcase
end
end
endmodule
Step 3: the receiver
The rx line comes from outside the FPGA, asynchronous to clk, so it goes through two flip-flops first to keep metastability out of the state machine. On a falling edge the receiver counts half a bit period and checks the line is still low; if it isn't, that was a glitch, not a start bit. From the middle of the start bit, every further CLKS_PER_BIT clocks lands in the middle of the next bit. A low stop bit sets frame_err instead of valid.
// UART receiver, 8N1. The input passes through two flip-flops first (it is
// asynchronous to clk), then each bit is sampled in its middle.
module uart_rx #(parameter CLKS_PER_BIT = 434) (
input wire clk,
input wire rst,
input wire rx,
output reg [7:0] data,
output reg valid, // one clock when data is new
output reg frame_err // stop bit was 0
);
localparam IDLE = 2'd0, START = 2'd1, DATA = 2'd2, STOP = 2'd3;
reg rx_meta, rx_sync;
reg [1:0] state;
reg [15:0] count;
reg [2:0] bit_idx;
always @(posedge clk) begin // synchronizer
rx_meta <= rx;
rx_sync <= rx_meta;
end
always @(posedge clk) begin
if (rst) begin
state <= IDLE; count <= 0; bit_idx <= 0; data <= 0; valid <= 1'b0; frame_err <= 1'b0;
end else begin
valid <= 1'b0;
case (state)
IDLE: begin
count <= 0;
if (rx_sync == 1'b0) state <= START; // falling edge: maybe a start bit
end
START: begin
if (count == (CLKS_PER_BIT - 1) / 2) begin
count <= 0;
if (rx_sync == 1'b0) begin bit_idx <= 0; state <= DATA; end // still low mid-bit: real start
else state <= IDLE; // a glitch
end else count <= count + 1'b1;
end
DATA: begin
if (count == CLKS_PER_BIT - 1) begin // middle of the next bit
count <= 0;
data[bit_idx] <= rx_sync;
if (bit_idx == 3'd7) state <= STOP;
else bit_idx <= bit_idx + 1'b1;
end else count <= count + 1'b1;
end
STOP: begin
if (count == CLKS_PER_BIT - 1) begin
count <= 0;
frame_err <= (rx_sync == 1'b0);
valid <= (rx_sync == 1'b1);
state <= IDLE;
end else count <= count + 1'b1;
end
endcase
end
end
endmodule
Step 4: a loopback testbench
The testbench connects tx straight to rx, sends 0x55, 0x00, 0xFF, 0xA5 and 100 random bytes, and checks each received byte against a list of what was sent. Then it holds the line low through one byte's stop bit to check the receiver reports a framing error and doesn't accept the byte.
always @(posedge clk) if (valid) begin
if (dout !== expect_q[got]) begin errors = errors + 1; $display("MISMATCH byte %0d: got %h, expected %h", got, dout, expect_q[got]); end
got = got + 1;
end
task send(input [7:0] b);
begin
@(negedge clk); din = b; start = 1; expect_q[sent] = b; sent = sent + 1;
@(negedge clk); start = 0;
wait (!busy);
end
endtask
What the simulation shows
sent 104 bytes, received 104, 0 mismatches
framing error flagged: 1, extra bytes accepted: 0
one byte takes 4340 clocks = 86.80 us
PASS
How much clock error is allowed?
The receiver resynchronizes on every start bit, so error only builds up within one frame. Its last sample, in the stop bit, comes 9.5 bit periods after the start edge. That sample stays inside the right bit as long as the accumulated drift is under half a bit: 0.5 / 9.5 = 5.26% in total between the two ends. Real designs aim well below that, since edges aren't perfectly sharp; the 0.0064% here is negligible.
Common mistakes
- Sending the data most significant bit first. UART is least significant bit first.
- Sampling at the start of each bit instead of the middle, which fails with the smallest clock error.
- Feeding rx straight into the state machine without a synchronizer.
- Treating every falling edge as a start bit, so line noise produces garbage bytes.
- Forgetting that idle is high, so a line held at 0 by reset looks like a stream of start bits.
Check your understanding
- Q1. What is CLKS_PER_BIT for 9600 baud from a 12 MHz clock? Answer: 1250, since 12,000,000 / 9,600 = 1,250 exactly.
- Q2. What does the receiver do if the line is high again at the middle of the start bit? Answer: treats it as a glitch and returns to IDLE.
- Q3. Why is the UART self-synchronizing between bytes? Answer: every frame begins with a start bit edge, so timing error resets each byte.
Related
Sources
Last verified
Simulated in Icarus Verilog 12 and linted with Verilator 5.036 on 14 November 2026; the output above is copied from that run, and the divider and timing figures are computed in the site's tests (lib/projectNumbers.test.js). The full files are in the site's repository under scripts/projects/uart.