Project 2, Part 4: The Loopback Test
The previous articles designed the baud generator, transmitter, and receiver modules. This article integrates those submodules into a top-level UART controller. We verify operation using an automated loopback testbench that routes transmitted serial data directly into the receiver.
Top-Level UART Architecture
The top-level controller connects the transmitter, receiver, 1x baud generator, and 16x oversampling generator:
module uart_top #(
parameter CLK_FREQ = 50_000_000,
parameter BAUD_RATE = 115200
) (
input logic clk, rst_n,
input logic [7:0] tx_data_in,
input logic tx_start,
output logic tx_busy,
output logic [7:0] rx_data_out,
output logic rx_valid,
output logic rx_framing_error,
output logic serial_line // Shared serial net for loopback testing
);
logic baud_tick, tick_16x;
baud_tick_gen #(.CLK_FREQ(CLK_FREQ), .BAUD_RATE(BAUD_RATE)) u_tx_baud (.clk(clk), .rst_n(rst_n), .tick(baud_tick));
baud_tick_gen_16x #(.CLK_FREQ(CLK_FREQ), .BAUD_RATE(BAUD_RATE)) u_rx_baud (.clk(clk), .rst_n(rst_n), .tick_16x(tick_16x));
uart_tx u_tx (
.clk(clk), .rst_n(rst_n), .baud_tick(baud_tick),
.tx_data_in(tx_data_in), .tx_start(tx_start),
.tx_busy(tx_busy), .tx_line(serial_line)
);
uart_rx u_rx (
.clk(clk), .rst_n(rst_n), .tick_16x(tick_16x),
.rx_line(serial_line),
.rx_data_out(rx_data_out), .rx_valid(rx_valid), .framing_error(rx_framing_error)
);
endmodule
Net serial_line provides the physical loopback path.
The receiver samples the exact serial waveforms produced by the transmitter.
Directed Loopback Testbench
The testbench initiates transmissions and asserts checks when the receiver signals valid data:
module tb_uart_loopback;
logic clk = 0, rst_n;
logic [7:0] tx_data_in;
logic tx_start, tx_busy;
logic [7:0] rx_data_out;
logic rx_valid, rx_framing_error;
logic serial_line;
uart_top #(.CLK_FREQ(50_000_000), .BAUD_RATE(115200)) dut (
.clk(clk), .rst_n(rst_n), .tx_data_in(tx_data_in), .tx_start(tx_start),
.tx_busy(tx_busy), .rx_data_out(rx_data_out), .rx_valid(rx_valid),
.rx_framing_error(rx_framing_error), .serial_line(serial_line)
);
always #10 clk = ~clk; // 50 MHz clock period (20 ns)
task automatic send_and_check(bit [7:0] byte_to_send);
tx_data_in = byte_to_send;
tx_start = 1'b1;
wait (tx_busy);
tx_start = 1'b0;
@(posedge rx_valid); // Wait for receiver completion strobe
@(posedge clk);
if (rx_data_out == byte_to_send && !rx_framing_error) begin
$display("PASS: Transmitted %h, Received %h", byte_to_send, rx_data_out);
end else begin
$display("FAIL: Transmitted %h, Received %h, framing_error=%b",
byte_to_send, rx_data_out, rx_framing_error);
end
endtask
initial begin
rst_n = 0;
tx_start = 0;
tx_data_in = 0;
repeat (3) @(posedge clk);
rst_n = 1;
send_and_check(8'h55); // Alternating bit pattern (01010101)
send_and_check(8'h00); // All zeros
send_and_check(8'hFF); // All ones
$finish;
end
endmodule
The testbench evaluates three boundary patterns:
Pattern 8'h55 forces maximum pin transitions.
Patterns 8'h00 and 8'hFF verify line stability during static bit intervals.
Constrained-Random Verification Extension
Extend coverage by transmitting randomized data streams:
class uart_byte_txn;
rand bit [7:0] data;
endclass
initial begin
uart_byte_txn t = new();
// Execute after directed tests
repeat (50) begin
if (!t.randomize()) $fatal(1, "Randomization error");
send_and_check(t.data);
end
end
Transmitting 50 randomized bytes tests arbitrary bit combinations efficiently. Physical microcontrollers implement identical loopback paths for self-test routines during factory manufacturing.
Practice Exercise
Misconfigure the receiver baud rate parameter by 5 percent higher than the transmitter.
Predict whether rx_framing_error will assert during transmission.
Solution
If the receiver operates 5 percent faster than the transmitter, its sample clock advances continuously.
Across a 10-bit frame (start bit, 8 data bits, stop bit), a 5 percent error accumulates to half a bit period ($0.5,T_{\text{bit}}$).
By the time the receiver samples the stop bit, the sample point drifts into the adjacent bit boundary.
Consequently, the receiver samples the falling edge before the stop bit arrives and asserts rx_framing_error.
Loopback testbenches verify serial transceiver correctness by routing transmit pins to receive inputs. Combining directed boundary patterns with randomized data sequences provides comprehensive verification coverage. This completes Project 2. Project 3 implements a synchronous Serial Peripheral Interface (SPI) master controller.