Automatic Pass and Fail
Printing simulation numbers requires manual inspection. Manual inspection is impractical for large verification test suites containing thousands of test vectors. An automated self-checking testbench compares every output against a reference calculation in software. It prints an immediate status summary indicating overall pass or failure metrics.
Tracking Errors and Test Counts
Declare dedicated variables to track execution progress:
integer error_count;
integer test_count;
Each verification evaluation increments test_count. If the observed output differs from the calculated reference, the testbench increments error_count and displays diagnostic data:
initial begin
error_count = 0;
test_count = 0;
// Apply stimulus
expected = compute_expected(inputs);
test_count = test_count + 1;
if (actual_output !== expected) begin
$display("FAIL [test %0d]: input=%0d expected=%0d got=%0d",
test_count, input_val, expected, actual_output);
error_count = error_count + 1;
end
// Report final verdict
if (error_count == 0)
$display("PASSED: all %0d tests.", test_count);
else
$display("FAILED: %0d of %0d tests.", error_count, test_count);
$finish;
end
Tracking test_count guarantees that loops execute. If a loop terminates prematurely, the test count reveals the discrepancy.
Always use the case-inequality operator (!==) rather than standard inequality (!=). If an output produces an unknown logic level (x), standard inequality evaluates to x, which conditional statements treat as false. The case-inequality operator compares all four logic states (0, 1, x, z) deterministically.
Exhaustive 512-Vector Self-Checking Adder Testbench
A 4-bit adder with carry-in accepts nine total input bits ($2^9 = 512$ combinations). The following testbench verifies all combinations automatically:
`timescale 1ns / 1ps
module tb_adder_4bit_selfcheck();
reg [3:0] a_tb, b_tb;
reg cin_tb;
wire [3:0] sum_tb;
wire cout_tb;
integer error_count, test_count;
integer i, j, k;
reg [4:0] expected_full; // Stores {cout, sum}
adder_4bit dut (
.a (a_tb),
.b (b_tb),
.cin (cin_tb),
.sum (sum_tb),
.cout(cout_tb)
);
initial begin
$dumpfile("tb_adder_4bit_selfcheck.vcd");
$dumpvars(0, tb_adder_4bit_selfcheck);
error_count = 0;
test_count = 0;
for (k = 0; k < 2; k = k + 1) begin // Carry-in loop
for (i = 0; i < 16; i = i + 1) begin // Operand a loop
for (j = 0; j < 16; j = j + 1) begin // Operand b loop
a_tb = i[3:0];
b_tb = j[3:0];
cin_tb = k[0];
#10;
expected_full = i + j + k; // Golden reference calculation
test_count = test_count + 1;
if ({cout_tb, sum_tb} !== expected_full) begin
$display("FAIL [test %0d]: %0d + %0d + %0d = %0d (expected %0d)",
test_count, i, j, k, {cout_tb, sum_tb}, expected_full);
error_count = error_count + 1;
end
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 the simulation using Icarus Verilog:
iverilog -o sim_adder_sc tb_adder_4bit_selfcheck.v adder_4bit.v
vvp sim_adder_sc
For a correct adder design, the console prints:
PASSED: all 512 tests.
If a defect exists, the console prints the failing input vectors, expected outputs, and measured outputs.
Modular Verification Using Check Tasks
Encapsulate output comparison logic inside a reusable task:
`timescale 1ns / 1ps
module tb_adder_4bit_task();
reg [3:0] a_tb, b_tb;
reg cin_tb;
wire [3:0] sum_tb;
wire cout_tb;
integer error_count, test_count;
integer i, j, k;
reg [4:0] expected_full;
adder_4bit dut (
.a (a_tb),
.b (b_tb),
.cin (cin_tb),
.sum (sum_tb),
.cout(cout_tb)
);
task check_output;
input [3:0] a_in, b_in;
input cin_in;
input [4:0] expected;
begin
test_count = test_count + 1;
if ({cout_tb, sum_tb} !== expected) begin
$display("FAIL [test %0d]: %0d + %0d + cin=%0d", test_count, a_in, b_in, cin_in);
$display(" got {cout,sum}=%05b expected %05b", {cout_tb, sum_tb}, expected);
error_count = error_count + 1;
end
end
endtask
initial begin
$dumpfile("tb_adder_4bit_task.vcd");
$dumpvars(0, tb_adder_4bit_task);
error_count = 0;
test_count = 0;
for (k = 0; k < 2; k = k + 1) begin
for (i = 0; i < 16; i = i + 1) begin
for (j = 0; j < 16; j = j + 1) begin
a_tb = i[3:0];
b_tb = j[3:0];
cin_tb = k[0];
#10;
expected_full = i + j + k;
check_output(i[3:0], j[3:0], k[0], expected_full);
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
Moving the check logic into a task clarifies stimulus generation.
Continuous Integration and Automation Exit Codes
In automated build scripts, configure the testbench to return a non-zero exit code upon failure:
if (error_count != 0)
$fatal(1, "FAILED with %0d errors.", error_count);
The $fatal system task terminates execution and returns a failure status code to continuous integration runners.
Self-checking testbenches integrate seamlessly into automated verification flows. Section 6.2 discusses simulation-only system tasks, file operations, and timing controls.