The Comparison
A reference model calculates expected circuit outputs. The comparison logic evaluates if the device under test (DUT) produces matching data. An incorrect comparison operator can cause a broken design to pass verification. It can also trigger false failures. Both failure modes degrade verification quality.
Equality Operators: == Versus ===
Use the == operator in synthesizable RTL code.
Always use the === operator in simulation testbenches.
Standard four-state logic causes == to return 1'bx when either operand contains an unknown (x) or high-impedance (z) bit.
In a Verilog conditional statement (if), the simulator interprets 1'bx as false:
actual = 8'hxx; // an uninitialised register
expected = 8'h42;
if (actual != expected) // Evaluates to x, treated as false
$error("mismatch"); // Does not execute; the test passes incorrectly
The case equality operator === checks both logic values and unknown states.
The comparison x === x evaluates to true (1'b1).
The comparison x === 0 evaluates to false (1'b0).
if (actual !== expected) // Evaluates to 1'b1
$error("mismatch"); // Executes correctly
Uninitialized registers, floating signals, and missing reset connections generate x states.
A testbench that uses != will silently ignore these failure conditions.
If automated simulations pass but physical hardware fails, check the comparison operators first.
Writing a Reusable Check Task
A dedicated check task standardizes error reporting across test suites:
task automatic check_result;
input [7:0] test_id;
input [15:0] expected;
input [15:0] actual;
inout integer error_count;
begin
if (actual !== expected) begin
$error("[test %0d] expected=0x%0h actual=0x%0h", test_id, expected, actual);
error_count = error_count + 1;
end
end
endtask
The inout port allows the task to increment the caller’s failure counter.
The automatic keyword allocates separate local variables for every task invocation.
This storage isolation prevents variable corruption when multiple threads call the task concurrently inside a fork ... join block.
The diagnostic message prints the test identifier, the expected value, and the received value.
Exhaustive Verification for 8-Bit Inputs
Circuits with small input spaces permit exhaustive testing. Two 8-bit inputs require $256 \times 256 = 65,536$ total test iterations:
`timescale 1ns / 1ps
module tb_multiplier_exhaustive();
reg [7:0] a, b;
wire [15:0] product;
integer error_count, test_count, i, j;
reg [15:0] expected;
multiplier_8bit dut (.a(a), .b(b), .product(product));
function automatic [15:0] ref_multiply;
input [7:0] fa, fb;
begin ref_multiply = fa * fb; end
endfunction
task automatic check_result;
input [7:0] t_a, t_b;
input [15:0] t_expected, t_actual;
inout integer errs;
begin
if (t_actual !== t_expected) begin
$error("[a=%0d b=%0d] expected=%0d actual=%0d", t_a, t_b, t_expected, t_actual);
errs = errs + 1;
end
end
endtask
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;
check_result(a, b, expected, product, error_count);
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
This testbench is fully self-contained. It reports whether the multiplier works correctly across all input patterns. If an error occurs, the output message identifies the failing input values. Engineers can then inspect signal waveforms to locate the root cause.
Structured Verification for 32-Bit Inputs
Exhaustive verification of two 32-bit inputs requires $2^{64}$ combinations. This scale exceeds practical simulation limits. Verification suites use structured corner-case values instead. Selected values test edge conditions: zero, all ones, single bit sets, maximum positive numbers, alternating bit patterns, and power-of-two boundaries:
`timescale 1ns / 1ps
module tb_alu_32bit_structured();
reg [31:0] a, b;
reg [2:0] op;
wire [31:0] result;
integer error_count, test_count, k;
alu_32bit dut (.a(a), .b(b), .op(op), .result(result));
function automatic [31:0] ref_alu;
input [31:0] fa, fb;
input [2:0] fop;
begin
case (fop)
3'b000: ref_alu = fa + fb;
3'b001: ref_alu = fa & fb;
3'b010: ref_alu = fa ^ fb;
default: ref_alu = 32'hx;
endcase
end
endfunction
task automatic check_op;
input [31:0] t_a, t_b;
input [2:0] t_op;
inout integer errs;
reg [31:0] t_expected;
begin
a = t_a; b = t_b; op = t_op;
#10;
t_expected = ref_alu(t_a, t_b, t_op);
test_count = test_count + 1;
if (result !== t_expected) begin
$error("[op=%0b a=0x%0h b=0x%0h] expected=0x%0h actual=0x%0h", t_op, t_a, t_b, t_expected, result);
errs = errs + 1;
end
end
endtask
reg [31:0] corner [0:11];
initial begin
error_count = 0;
test_count = 0;
corner[0] = 32'h0000_0000;
corner[1] = 32'hFFFF_FFFF;
corner[2] = 32'h8000_0000;
corner[3] = 32'h7FFF_FFFF;
corner[4] = 32'h0000_0001;
corner[5] = 32'hFFFF_FFFE;
corner[6] = 32'h0000_FFFF;
corner[7] = 32'hFFFF_0000;
corner[8] = 32'h5555_5555;
corner[9] = 32'hAAAA_AAAA;
corner[10] = 32'h0000_0100;
corner[11] = 32'h0001_0000;
for (k = 0; k < 12; k = k + 1) begin
check_op(corner[k], corner[k], 3'b000, error_count);
check_op(corner[k], 32'h0, 3'b000, error_count);
check_op(corner[k], 32'hFFFF_FFFF, 3'b000, error_count);
check_op(corner[k], 32'hFFFF_FFFF, 3'b001, error_count);
check_op(corner[k], 32'h0, 3'b001, error_count);
check_op(corner[k], corner[k], 3'b010, error_count);
check_op(corner[k], ~corner[k], 3'b010, error_count);
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
Testing corner values against algebraic identities and numerical extremes uncovers critical bugs efficiently. This structured pattern detects edge-case failures more reliably than pseudo-random vectors alone.
Summary Verdict and Exit Codes
At simulation completion, print clear status summaries:
if (error_count == 0)
$display("PASSED: all %0d tests.", test_count);
else
$display("FAILED: %0d of %0d tests.", error_count, test_count);
$finish;
Automated scripts can parse the first word of the log to determine test status.
Continuous integration dashboards track test metrics across revisions.
When build systems require non-zero process exit codes on failure, call $fatal(1, "...") when error_count is greater than zero.
This guarantees that simulators such as Icarus Verilog return a non-zero exit code to the shell.