RTL Design / Module 10: Verification for RTL Designers
Lesson 10.1

Your first self-checking testbench

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.

Core idea

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.

1

Testbench anatomy

Self-checking testbench block diagram

Stimulus and checker live in the testbench module. The DUT is the only synthesizable block.

BlockResponsibilityTypical code
StimulusGenerate clock, assert reset, drive transactionsinitial, always #5 clk=~clk, task drive()
DUTImplement the design specacc_8bit dut (...) instance
CheckerCompare output vs golden model; count errorsif (sum !== exp) errors++
ReporterPrint final PASS/FAIL summary$display("=== ALL TESTS PASSED ===")
Reset→Drive inputs→DUT responds→Check !==→Report
2

Clock and reset — the foundation

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_reset.v — reusable TB boilerplate
// 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
Trap

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.

3

Simple DUT — 8-bit accumulator

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.v
// 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
4

Self-check pattern — if / !==

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.

check_task.v — reusable checker
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
⚖️

!== vs !=

!== fails if any bit is X/Z and the other side is 0/1. Catches uninitialized outputs early.

📊

Error counter

Never $finish on first failure — run all tests, then report total errors. One run surfaces every bug.

🏷️

Named tests

Pass a test_name string to every check. When something fails at 2 AM, the label tells you which scenario broke.

5

Full self-checking testbench

acc_8bit_tb.v — complete worked example
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
▶ Run in Verilog simulator
6

What "self-checking" buys you

  • Regression-ready: re-run after every RTL edit; PASS/FAIL is unambiguous.
  • Reference model lives in TB: your golden behavior is explicit and reviewable.
  • Scales up: same pattern works for FIFOs, FSMs, and pipelined blocks — just add tasks.
  • CI-friendly: simulators exit with error count; scripts grep for 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.

7

Verification checklist

  • Clock generator with known period; reset held ≥2 cycles
  • DUT instantiated with named port connections
  • Checker uses !== (four-state compare)
  • Error counter increments on mismatch; simulation continues
  • Final $display reports PASS or FAIL with error count
  • Each test case has a descriptive name string
  • Reference model in TB matches spec (not copied from DUT logic)

🎯 What to remember

  • Every TB = stimulus + DUT + checker. The checker is your safety net.
  • Clock/reset first, then transactions. Never stimulus during reset.
  • Use !== and an error counter — one run, all failures reported.
  • Keep a reference model in the TB; compare every cycle you care about.
Advertisement