Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Testbenches

Self-Checking Testbenches

Build self-checking Verilog testbenches with reference models, check tasks, error counters and pass/fail summaries instead of reading waveforms by eye.

A testbench that only prints values forces you to be the checker: you read 512 lines and decide whether they look right. That does not scale and it fails silently the day you stop looking. A self-checking testbench compares the DUT's outputs with expected values automatically, counts mismatches and ends with a clear PASS or FAIL. This is the single biggest step from "toy" to "professional" verification.

This lesson shows the pieces: an expected-value source (a reference model or golden model), a check task, error counters, and a final summary. The stand-ins in C use the same structure so you can run it.

The Verilog is read-only (no simulator on this site). The C programs are labelled stand-ins: a deliberately buggy "DUT" function and a checker that catches the bug.

Where do expected values come from?

There are three common sources, from simplest to most powerful:

  1. Hand-written constants: check(a, b, 5'd16). Fine for directed tests, tedious for many cases.
  2. A reference model written as behavioural code, often a plain + or * operator written in testbench style, or a function.
  3. A file of vectors produced by another program (a Python or C model), loaded with $readmem (see clocks, memories and files).

A reference model should be as different from the DUT as possible. If the DUT is a gate-level ripple-carry adder, the model is the + operator; if you copy the DUT's logic into the model you copy its bugs.

A check task

A task is a reusable procedure that may contain delays and timing controls (unlike a function). It is the natural home of a comparison:

VERILOG
integer errors = 0;
integer checks = 0;

task check_sum(input [3:0] a, b, input cin);
  reg [4:0] expected;
  begin
    expected = a + b + cin;
    #1;                                   // let the DUT settle
    checks = checks + 1;
    if ({cout, sum} !== expected) begin
      errors = errors + 1;
      $display("FAIL t=%0t a=%0d b=%0d cin=%0d: got %0d, expected %0d",
               $time, a, b, cin, {cout, sum}, expected);
    end
  end
endtask

Four details matter:

  • !== instead of !=. The case-inequality operators compare x and z as values. With !=, a comparison involving x gives x (treated as false), so a DUT that outputs x would pass the check. With !== it fails, as it should.
  • The #1 lets combinational outputs settle after the inputs changed.
  • Print the inputs, the actual and the expected value in one line: it is all you need to debug.
  • Keep counters (checks, errors) so the end of the simulation can summarise.

The final verdict

VERILOG
initial begin : stimulus
  integer i;
  for (i = 0; i < 512; i = i + 1) begin
    {cin, a, b} = i[8:0];
    check_sum(a, b, cin);
  end
  if (errors == 0) $display("PASS: %0d checks", checks);
  else             $display("FAIL: %0d of %0d checks failed", errors, checks);
  $finish;
end

A very common mistake is printing nothing at the end. A test that "ran without output" is indistinguishable from one that never ran; always print an explicit PASS or FAIL line, because regression scripts search for it.

flowchart LR
  G["Stimulus generator"] --> D["DUT"]
  G --> M["Reference model"]
  D --> C{"Compare<br/>(!==)"}
  M --> C
  C -->|mismatch| E["errors++ and print"]
  C -->|match| P["checks++"]

A self-checking testbench feeds the same inputs to the DUT and a reference model, then compares.

Stand-in: catching a bug

The first program has a DUT with a typical hardware bug: the carry out is computed from a + b and forgets the carry in. The checker compares against a straightforward model and reports every mismatch. Run it and notice that only some input combinations fail, which is why exhaustive checking is valuable.

C Stand-in: exhaustive checker finds a buggy carry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Only the 16 inputs where a + b is exactly 15 and cin is 1 fail (so "16 of 512" is the verdict), and the program prints just the first three, a good habit: the first failure is usually the interesting one, and thousands of lines hide it. Real testbenches often stop after a maximum error count with if (errors > 10) $finish;.

Why x matters: the !== story

Hardware has four-valued logic: 0, 1, x (unknown) and z (high impedance). A DUT that was never reset has x in its registers. The second stand-in models the comparison difference: a tri-state value is stored as a value plus an "unknown" flag, and the two comparisons differ exactly as != and !== do.

C Stand-in: != versus !== with an unknown bit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

That is the whole argument for !== in checkers: the if (x) branch is not taken, so the error report never runs and the testbench says PASS on a broken design.

Good checker habits

  • Check outputs at the right time. After combinational delay, or at the clock edge plus a small offset for registered designs.
  • Make checks independent of stimulus. Stimulus code should not know the expected result; give that job to the model.
  • Don't check while in reset unless the reset behaviour itself is what you are checking.
  • Self-test the testbench. Introduce a deliberate bug (as in the stand-in) and confirm the testbench fails. A checker that has never failed has not been shown to work.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is !== preferred over != in a checker?

    With !=, a comparison involving x yields x, which if treats as false, so the error branch is skipped and a broken design passes.
  2. 2. What is the main job of a reference model?

    The model computes what the output should be, so the testbench can compare it with the DUT output automatically.
  3. 3. A test finishes with no PASS or FAIL line. What is the real problem?

    Regression scripts need an explicit verdict line. Silence can mean success, a hang, or a mis-wired test.
  4. 4. How can you show that your checker works?

    A checker that has never been seen to fail is untrusted. Mutating the DUT proves it can catch errors.

Summary

  • Self-checking testbenches compare DUT outputs against a reference model and report a final PASS or FAIL.
  • Put comparisons in a task, count checks and errors, and print the failing inputs with actual and expected values.
  • Use !== so x and z outputs fail the check.
  • Prove the checker works by injecting a bug.

Next: Clocks, Memories and Files