Module 6: Verilog for Testbench & Simulation · 6.4 Writing Self-Checking Tests

Why Checking Has to Be Automatic

Sections 6.1 through 6.3 established testbench structural infrastructure. This section focuses on automated output response checking. Consider an input space for a 16-bit adder: two 16-bit inputs produce over four billion combinations. Manual waveform inspection cannot verify input spaces of this magnitude. Automated self-checking testbenches execute thousands of checks rapidly and report clear status verdicts.

The Combinatorial Input Space

The input combination space grows exponentially with signal bit width:

Total Input Width Input Combinations Manual Verification Time (10 checks/second)
8 bits 256 26 seconds
12 bits 4,096 7 minutes
16 bits 65,536 Under 2 hours
24 bits 16,777,216 19 days
32 bits 4,294,967,296 13 years

An automated simulator verifies 65,536 combinations in seconds. Automated execution eliminates human fatigue and omissions. When complete exhaustive verification is impossible, automated random and directed test sequences still provide superior coverage compared to manual inspection.

Preventing Regression Defects

A regression occurs when an engineering modification to fix one component inadvertently breaks another functional component. For example, modifying an arithmetic shifter can accidentally disrupt shared carry-chain logic in an ALU.

Self-checking test suites enable developers to re-execute entire verification suites after every code modification. Regression testing detects unintended defects during continuous integration builds before committing changes.

Relative Cost of Defect Resolution

The cost to correct a hardware defect escalates by an order of magnitude across each design phase:

Phase of Defect Discovery Relative Resolution Cost
Initial RTL simulation 1x (Minutes of developer time)
Post-synthesis gate-level netlist 10x (Hours of synthesis and constraint rework)
Physical FPGA prototype validation 100x (Days of laboratory instrumentation)
Tapeout and ASIC silicon fabrication 1000x+ (Months of delay and fabrication mask re-spins)

Thorough automated simulation discovers defects when correction requires simple source code edits.

Comparison: Visual Inspection Versus Automated Verification

Manual Printing Testbench

The following testbench relies on manual console inspection:

`timescale 1ns / 1ps

module tb_adder_manual();
    reg  [7:0] a, b;
    wire [8:0] sum;

    adder_8bit dut (.a(a), .b(b), .sum(sum));

    initial begin
        a = 8'd10;  b = 8'd20;  #10;
        $display("10 + 20 = %0d", sum);
        a = 8'd200; b = 8'd100; #10;
        $display("200 + 100 = %0d", sum);
        a = 8'd255; b = 8'd1;   #10;
        $display("255 + 1 = %0d", sum);
        $finish;
    end
endmodule

The engineer must manually read and calculate each printed result. This approach introduces observation errors and validates only a tiny fraction of the input space.

Automated Self-Checking Testbench

The following testbench verifies all 65,536 input combinations automatically:

`timescale 1ns / 1ps

module tb_adder_selfcheck();
    reg  [7:0] a, b;
    wire [8:0] sum;
    integer    error_count, test_count;
    integer    i, j;

    adder_8bit dut (.a(a), .b(b), .sum(sum));

    initial begin
        error_count = 0;
        test_count  = 0;

        for (i = 0; i < 256; i = i + 1) begin
            for (j = 0; j < 256; j = j + 1) begin
                a = i[7:0];
                b = j[7:0];
                #10;
                test_count = test_count + 1;

                if (sum !== (i + j)) begin
                    $error("Mismatch: a=%0d b=%0d expected=%0d got=%0d", i, j, i + j, sum);
                    error_count = error_count + 1;
                end
            end
        end

        if (error_count == 0)
            $display("PASSED: all %0d tests.", test_count);
        else
            $display("FAILED: %0d of %0d tests.", error_count, test_count);

        $finish;
    end
endmodule

The simulator checks every input pair in fractions of a second. If a defect occurs, the console reports the specific mismatched vectors alongside expected and measured values.

Automated checking replaces subjective human review with deterministic, repeatable verification scripts. The next article examines immediate assertions and standardized error reporting tasks.

← Two Clocks $error, $fatal and Assertions →