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

Directed Tests

Exhaustive verification proves the correctness of circuits with small input widths. Circuits with more than 16 input bits require targeted test vector selection. Directed testing applies selected stimuli to target high-risk logic conditions. These tests target edge cases, boundary conditions, and common design defects. Directed tests execute quickly and identify critical architectural failures immediately.

Standard Test Categories

Verification engineers test common boundary conditions before running long simulations:

  • Zero Values: Identity paths frequently use distinct hardware multiplexers. An adder that returns an incorrect value for $0 + 0$ contains a localized design defect.
  • Maximum Values: Carry chains, arithmetic overflow logic, and saturation circuits operate at numerical boundaries, such as 255 + 1 or 32'hFFFF_FFFF.
  • Powers of Two: Stimulus with a single bit set targets individual stages of shifters, multiplexers, and decoder trees.
  • Off-By-One Boundaries: Values around counter limits, FIFO thresholds, and modulo boundaries expose comparator errors, such as testing values 98, 99, 100, and 101.
  • Sign Bit Boundaries: Values such as 8'h7F and 8'h80 test sign extension, two’s-complement arithmetic, and signed comparison circuits.
  • Consecutive Identical Inputs: Sequential circuits, pipelines, and FIFOs can fail when processing identical data on consecutive clock cycles.

Implementing Named Test Cases

Generic numerical error reports require manual tracing. Named test cases describe the exact functional failure immediately:

`timescale 1ns / 1ps

module tb_adder_8bit_directed();
    reg  [7:0] a, b;
    wire [8:0] sum;
    integer    error_count, test_count, k;

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

    localparam NUM_DIRECTED = 14;

    reg [8*24-1:0] test_names    [0:NUM_DIRECTED-1];   // 24 characters
    reg [7:0]      test_a        [0:NUM_DIRECTED-1];
    reg [7:0]      test_b        [0:NUM_DIRECTED-1];
    reg [8:0]      test_expected [0:NUM_DIRECTED-1];

    task run_directed_tests;
        integer idx;
        begin
            for (idx = 0; idx < NUM_DIRECTED; idx = idx + 1) begin
                a = test_a[idx];
                b = test_b[idx];
                #10;
                test_count = test_count + 1;
                if (sum !== test_expected[idx]) begin
                    $error("[%0s] a=%0d b=%0d expected=%0d actual=%0d",
                           test_names[idx], test_a[idx], test_b[idx], test_expected[idx], sum);
                    error_count = error_count + 1;
                end
            end
        end
    endtask

    integer   i, j;
    reg [8:0] expected_sweep;

    initial begin
        error_count = 0;
        test_count  = 0;

        // Zero boundary
        test_names[0]  = "ZERO_PLUS_ZERO";         test_a[0]  = 8'd0;   test_b[0]  = 8'd0;   test_expected[0]  = 9'd0;
        test_names[1]  = "ZERO_PLUS_MAX";          test_a[1]  = 8'd0;   test_b[1]  = 8'd255; test_expected[1]  = 9'd255;
        test_names[2]  = "MAX_PLUS_ZERO";          test_a[2]  = 8'd255; test_b[2]  = 8'd0;   test_expected[2]  = 9'd255;
        // Maximum boundary and overflow
        test_names[3]  = "OVERFLOW_MAX_PLUS_ONE";  test_a[3]  = 8'd255; test_b[3]  = 8'd1;   test_expected[3]  = 9'd256;
        test_names[4]  = "OVERFLOW_MAX_PLUS_MAX";  test_a[4]  = 8'd255; test_b[4]  = 8'd255; test_expected[4]  = 9'd510;
        // Powers of two
        test_names[5]  = "POW2_128_PLUS_128";      test_a[5]  = 8'd128; test_b[5]  = 8'd128; test_expected[5]  = 9'd256;
        test_names[6]  = "POW2_64_PLUS_64";        test_a[6]  = 8'd64;  test_b[6]  = 8'd64;  test_expected[6]  = 9'd128;
        test_names[7]  = "POW2_1_PLUS_1";          test_a[7]  = 8'd1;   test_b[7]  = 8'd1;   test_expected[7]  = 9'd2;
        // Off-by-one boundary
        test_names[8]  = "OFFBYONE_127_PLUS_1";    test_a[8]  = 8'd127; test_b[8]  = 8'd1;   test_expected[8]  = 9'd128;
        test_names[9]  = "OFFBYONE_128_PLUS_255";  test_a[9]  = 8'd128; test_b[9]  = 8'd255; test_expected[9]  = 9'd383;
        // Sign bit boundaries
        test_names[10] = "SIGNED_MIN_PLUS_ZERO";   test_a[10] = 8'h80;  test_b[10] = 8'h00;  test_expected[10] = 9'h080;
        test_names[11] = "SIGNED_MAX_PLUS_ONE";    test_a[11] = 8'h7F;  test_b[11] = 8'h01;  test_expected[11] = 9'h080;
        // Alternating bit patterns
        test_names[12] = "ALTERNATING_AA_55";      test_a[12] = 8'hAA;  test_b[12] = 8'h55;  test_expected[12] = 9'hFF;
        test_names[13] = "ALTERNATING_55_AA";      test_a[13] = 8'h55;  test_b[13] = 8'hAA;  test_expected[13] = 9'hFF;

        $info("Running %0d directed tests", NUM_DIRECTED);
        run_directed_tests();
        $info("Directed tests completed: %0d failures", error_count);

        $info("Running full input sweep");
        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_sweep = i + j;
                test_count     = test_count + 1;
                if (sum !== expected_sweep) begin
                    $error("[SWEEP a=%0d b=%0d] expected=%0d actual=%0d", i, j, expected_sweep, 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

Execute directed tests before running extensive sweeps. If basic identity operations fail, simulation terminates quickly without executing thousands of redundant iterations. Named tests clarify failure modes immediately.

Loading Test Vectors From Files

When test vectors scale to thousands of patterns, store values in external data files. Load vector files into memory using $readmemh or $readmemb. Software reference models can generate these vector files automatically. When design specifications change, re-run the generator script without modifying the Verilog testbench architecture.

Verification Tiers

Production verification environments organize simulations into three distinct tiers:

  1. Directed Unit Tests: Handcrafted test suites targeting boundary conditions. They run in seconds and provide immediate feedback during active development.
  2. Exhaustive or Constrained-Random Sweeps: Broad input suites executing thousands or millions of cycles. They validate complex interactions across multiple minutes.
  3. System-Level Regressions: Full chip workloads running complete software stacks. They run for multiple hours to confirm system integration stability.
← The Comparison VCD Files →