A self-checking testbench drives your DUT, compares outputs to a golden model, and prints PASS/FAIL — no eyeballing waveforms required. This is the minimum verification skill every RTL designer needs before handing off a block.
Every testbench has three jobs: stimulus (drive inputs), DUT (the design), and checker (compare actual vs expected). If the checker finds a mismatch, increment an error counter and keep going — then report the final score.
Stimulus and checker live in the testbench module. The DUT is the only synthesizable block.
| Block | Responsibility | Typical code |
|---|---|---|
| Stimulus | Generate clock, assert reset, drive transactions | initial, always #5 clk=~clk, task drive() |
| DUT | Implement the design spec | acc_8bit dut (...) instance |
| Checker | Compare output vs golden model; count errors | if (sum !== exp) errors++ |
| Reporter | Print final PASS/FAIL summary | $display("=== ALL TESTS PASSED ===") |
Every synchronous testbench starts with a clock generator and an active-low reset sequence. Hold reset long enough for the DUT to reach a known state — typically 2–5 clock cycles after rst_n deasserts.
// Clock: 10 ns period (100 MHz) — adjust for your project
reg clk;
initial clk = 0;
always #5 clk = ~clk;
// Reset: hold low, release after a few cycles
reg rst_n;
initial begin
rst_n = 0;
repeat (3) @(posedge clk); // 3 cycles in reset
rst_n = 1;
$display("[%0t] Reset released", $time);
end
Driving stimulus before reset completes produces false failures. Always wait for rst_n == 1 (or a #(posedge clk) after release) before your first real transaction.
We'll verify a tiny accumulator: on each cycle where en is high, add din to the running sum. Clear on reset. Simple enough to hold the whole DUT in your head.
// acc_8bit — running sum with enable
module acc_8bit (
input wire clk,
input wire rst_n,
input wire en,
input wire [7:0] din,
output wire [7:0] sum
);
reg [7:0] acc_r;
assign sum = acc_r;
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
acc_r <= 8'h00;
else if (en)
acc_r <= acc_r + din;
end
endmodule
The checker computes what the output should be (a reference model in the TB) and compares with !==. Use four-state comparison — != can hide X mismatches.
integer errors;
integer tests_run;
// Reference model: what the accumulator SHOULD hold
function [7:0] ref_model(input [7:0] prev, input [7:0] din, input en);
ref_model = en ? (prev + din) : prev;
endfunction
task check_sum(input [7:0] expected; input string test_name);
begin
tests_run = tests_run + 1;
@(posedge clk); // sample after DUT updates
if (sum !== expected) begin
$display("FAIL [%s]: got %0d (0x%02h) expected %0d (0x%02h) at t=%0t",
test_name, sum, sum, expected, expected, $time);
errors = errors + 1;
end else begin
$display("PASS [%s]: sum = %0d", test_name, sum);
end
end
endtask
!== fails if any bit is X/Z and the other side is 0/1. Catches uninitialized outputs early.
Never $finish on first failure — run all tests, then report total errors. One run surfaces every bug.
Pass a test_name string to every check. When something fails at 2 AM, the label tells you which scenario broke.
module acc_8bit_tb;
reg clk, rst_n, en;
reg [7:0] din;
wire [7:0] sum;
acc_8bit dut (.clk(clk), .rst_n(rst_n), .en(en), .din(din), .sum(sum));
initial clk = 0;
always #5 clk = ~clk;
integer errors, tests_run;
reg [7:0] ref_acc;
task drive(input bit en_in; input [7:0] data);
begin
@(posedge clk);
en = en_in;
din = data;
end
endtask
task check(input [7:0] exp; input string name);
begin
tests_run = tests_run + 1;
@(posedge clk);
#1; // settle time for nonblocking updates
if (sum !== exp) begin
$display("FAIL [%s]: got %0d expected %0d", name, sum, exp);
errors = errors + 1;
end else
$display("PASS [%s]: sum = %0d", name, sum);
end
endtask
initial begin
errors = 0; tests_run = 0;
ref_acc = 8'd0;
rst_n = 0; en = 0; din = 0;
repeat (3) @(posedge clk);
rst_n = 1;
@(posedge clk);
check(8'd0, "after_reset");
// Test 1: single add
drive(1, 8'd10);
ref_acc = ref_acc + 8'd10;
check(ref_acc, "add_10");
// Test 2: accumulate
drive(1, 8'd5);
ref_acc = ref_acc + 8'd5;
check(ref_acc, "add_5");
// Test 3: disabled — sum should hold
drive(0, 8'd99);
check(ref_acc, "en_low_hold");
// Test 4: overflow wrap (8-bit)
drive(1, 8'd250);
ref_acc = ref_acc + 8'd250; // wraps at 255
check(ref_acc, "overflow_wrap");
// Final report
$display("-----------------------------------");
$display("Tests run: %0d Errors: %0d", tests_run, errors);
if (errors == 0)
$display("=== ALL TESTS PASSED ===");
else
$display("=== %0d TEST(S) FAILED ===", errors);
$finish;
end
endmodule
FAILED.Designer mindset: if you can't write a 20-line self-checking TB for your block, you don't fully understand the spec yet. Write the checker before you debug waveforms.
!== (four-state compare)$display reports PASS or FAIL with error count!== and an error counter — one run, all failures reported.