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

Reference Models

Robust self-checking testbenches avoid hardcoding expected numerical outputs. Instead, they incorporate two concurrent implementations of the design specification: the synthesizable RTL under test and an untimed behavioral reference model. The testbench applies identical input vectors to both blocks and compares their outputs (Figure 1).

Reference model beside the design
Figure 1: The same inputs into the design and the model; the checker compares the outputs

Characteristics of Golden Reference Models

A reference model computes the mathematically correct response for any input vector. It operates under different constraints than synthesizable RTL:

  • It does not require physical hardware synthesis.
  • It can use abstract constructs: 64-bit integer arithmetic, floating-point real numbers, and unbounded loops.
  • It focuses exclusively on algorithmic correctness by inspection rather than gate-level execution speed.

For example, an RTL designer implements an optimized Wallace-tree multiplier to achieve timing closure. The reference model computes a * b using standard software integer multiplication.

Combinational Reference Model: 8-Bit Multiplier

The following testbench verifies an 8-bit multiplier against an automatic function:

`timescale 1ns / 1ps

module tb_multiplier_8bit();
    reg  [7:0]  a, b;
    wire [15:0] product;
    integer     error_count, test_count;
    integer     i, j;
    reg  [15:0] expected;

    multiplier_8bit dut (
        .a      (a),
        .b      (b),
        .product(product)
    );

    // Behavioral reference function
    function automatic [15:0] ref_multiply;
        input [7:0] fa, fb;
        begin
            ref_multiply = fa * fb;
        end
    endfunction

    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;
                expected   = ref_multiply(a, b);
                test_count = test_count + 1;

                assert (product === expected)
                else begin
                    $error("[test %0d] a=%0d b=%0d expected=%0d actual=%0d",
                           test_count, i, j, expected, product);
                    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 testbench verifies 65,536 combinations automatically without manual expected-value tables.

Algorithmic Reference Model: CRC-8 Engine

When an RTL block uses parallel table lookups, the reference model can use the foundational bit-serial mathematical definition:

function automatic [7:0] ref_crc8_byte;
    input [7:0] current_crc, data;
    integer     k;
    reg  [7:0]  crc;
    begin
        crc = current_crc ^ data;
        for (k = 0; k < 8; k = k + 1)
            crc = crc[7] ? ((crc << 1) ^ 8'h07) : (crc << 1);
        ref_crc8_byte = crc;
    end
endfunction

The following testbench verifies an 8-byte message stream:

`timescale 1ns / 1ps

module tb_crc8();
    reg        clk, rst_n;
    reg  [7:0] data_byte;
    reg        data_valid;
    wire [7:0] crc_out;

    integer error_count, test_count, byte_idx;
    reg [7:0] test_message [0:7];
    reg [7:0] ref_result;

    crc8_engine dut (
        .clk       (clk),
        .rst_n     (rst_n),
        .data_byte (data_byte),
        .data_valid(data_valid),
        .crc_out   (crc_out)
    );

    initial clk = 0;
    always #5 clk = ~clk;

    function automatic [7:0] ref_crc8_byte;
        input [7:0] current_crc, data;
        integer     k;
        reg  [7:0]  crc;
        begin
            crc = current_crc ^ data;
            for (k = 0; k < 8; k = k + 1)
                crc = crc[7] ? ((crc << 1) ^ 8'h07) : (crc << 1);
            ref_crc8_byte = crc;
        end
    endfunction

    initial begin
        error_count = 0;
        test_count  = 0;

        rst_n = 0; data_byte = 8'h00; data_valid = 0;
        repeat (4) @(posedge clk);
        #2; rst_n = 1;

        test_message[0] = 8'h31; test_message[1] = 8'h32;
        test_message[2] = 8'h33; test_message[3] = 8'h34;
        test_message[4] = 8'h35; test_message[5] = 8'h36;
        test_message[6] = 8'h37; test_message[7] = 8'h38;

        // Compute golden CRC over message
        ref_result = 8'h00;
        for (byte_idx = 0; byte_idx < 8; byte_idx = byte_idx + 1)
            ref_result = ref_crc8_byte(ref_result, test_message[byte_idx]);

        // Stream bytes to RTL DUT
        for (byte_idx = 0; byte_idx < 8; byte_idx = byte_idx + 1) begin
            @(posedge clk); #2;
            data_byte  = test_message[byte_idx];
            data_valid = 1;
        end
        @(posedge clk); #2;
        data_valid = 0;
        @(posedge clk); #2;

        test_count = test_count + 1;
        assert (crc_out === ref_result)
        else begin
            $error("CRC mismatch: expected=0x%0h actual=0x%0h", ref_result, crc_out);
            error_count = error_count + 1;
        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

Stateful Reference Model: Saturating Counter

When verifying sequential logic such as counters, FIFOs, and state machines, the reference model must retain internal state. Use tasks to advance model state synchronously with testbench clock cycles:

`timescale 1ns / 1ps

module tb_saturating_counter();
    reg        clk, rst_n, inc, dec;
    wire [3:0] count;

    integer error_count, test_count, i;
    integer ref_count;                     // Reference model internal state

    sat_counter_4bit dut (
        .clk  (clk),
        .rst_n(rst_n),
        .inc  (inc),
        .dec  (dec),
        .count(count)
    );

    // Reference model update task
    task ref_tick;
        input t_inc, t_dec;
        begin
            if (t_inc && !t_dec && ref_count < 15)
                ref_count = ref_count + 1;
            else if (t_dec && !t_inc && ref_count > 0)
                ref_count = ref_count - 1;
        end
    endtask

    initial clk = 0;
    always #5 clk = ~clk;

    initial begin
        error_count = 0;
        test_count  = 0;
        ref_count   = 0;

        rst_n = 0; inc = 0; dec = 0;
        repeat (3) @(posedge clk);
        #2; rst_n = 1;

        // Verify up-counting and saturation at 15
        for (i = 0; i < 20; i = i + 1) begin
            @(posedge clk); #2;
            inc = 1; dec = 0;
            ref_tick(1, 0);
            @(posedge clk); #2;
            test_count = test_count + 1;
            assert (count === ref_count[3:0])
            else begin
                $error("[inc %0d] expected=%0d actual=%0d", i, ref_count, count);
                error_count = error_count + 1;
            end
        end

        // Verify down-counting and clamping at 0
        inc = 0;
        for (i = 0; i < 20; i = i + 1) begin
            @(posedge clk); #2;
            dec = 1;
            ref_tick(0, 1);
            @(posedge clk); #2;
            test_count = test_count + 1;
            assert (count === ref_count[3:0])
            else begin
                $error("[dec %0d] expected=%0d actual=%0d", i, ref_count, count);
                error_count = error_count + 1;
            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

In industrial verification flows, reference models are often implemented in C++ or Python and connected via the SystemVerilog Direct Programming Interface (DPI).

The next article examines output comparison operators and explains how to prevent uninitialized x values from bypassing verification checks.

← $error, $fatal and Assertions The Comparison →