$error, $fatal and Assertions
Not all verification failures have identical consequences. A single mismatched output vector should be logged without terminating simulation, allowing engineers to identify patterns across subsequent vectors. In contrast, an illegal state machine lockup or uninitialized bus signal justifies immediate termination. Verilog severity tasks provide these distinct reporting capabilities.
The Four Severity Task Levels
Verilog and SystemVerilog define four severity reporting levels:
$info: Prints informational progress messages without recording errors.$warning: Flags unexpected or suspicious conditions without interrupting test execution.$error: Records a verification failure, increments internal counters, and continues execution.$fatal(level, message): Prints a fatal error message and terminates simulation with a non-zero exit status code. Parameterlevel(0 to 2) sets diagnostic output verbosity.
$info("Initiating bus passthrough verification");
if (data_out !== 8'h00)
$warning("Output non-zero during idle transaction: %0h", data_out);
if (data_out !== data_in)
$error("Data mismatch: in=%0h out=%0h", data_in, data_out);
if (data_out === 8'hxx)
$fatal(1, "Output contains unknown state (x); halting execution");
Use $error for mismatched data vectors so that regression runs collect complete failure counts. Use $fatal when internal state corruption renders subsequent testing invalid.
Immediate Assertions
An immediate assertion (assert) evaluates an expression when reached in procedural code. If the expression evaluates to false or unknown (x), the simulator executes the associated else block:
// Procedural if condition
if (dut_sum !== expected_sum)
$error("Sum mismatch: expected=%0h got=%0h", expected_sum, dut_sum);
// Immediate assertion syntax
assert (dut_sum === expected_sum)
else $error("Sum mismatch: expected=%0h got=%0h", expected_sum, dut_sum);
Always include the else clause to specify custom severity levels and formatted diagnostic messages.
Structuring Diagnostic Error Messages
An actionable verification error message must include three core fields:
- Unique test identifier or loop index.
- Stimulus inputs applied to the DUT.
- Expected golden value versus measured DUT value.
// Defective error message: Lacks diagnostic context
ERROR: output mismatch
// Informative error message: Enables direct reproduction
ERROR: [test 47] a=0xFE b=0x03 expected=0x101 actual=0x001
The following multiplier testbench demonstrates assertion formatting:
`timescale 1ns / 1ps
module tb_multiplier_8bit();
reg [7:0] a, b;
wire [15:0] product;
integer error_count, test_id;
integer i, j;
reg [15:0] expected;
multiplier_8bit dut (.a(a), .b(b), .product(product));
initial begin
error_count = 0;
test_id = 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 = i * j;
test_id = test_id + 1;
assert (product === expected)
else begin
$error("[test %0d] a=%0d b=%0d expected=%0d actual=%0d",
test_id, i, j, expected, product);
error_count = error_count + 1;
end
end
end
if (error_count == 0)
$display("PASSED: all %0d tests.", test_id);
else
$display("FAILED: %0d of %0d tests.", error_count, test_id);
$finish;
end
endmodule
Because simulators do not export internal assertion counts automatically, track error_count explicitly.
Integrated Check Task Example
The following testbench verifies an 8-bit arithmetic logic unit (ALU) using a check task:
`timescale 1ns / 1ps
module tb_alu_8bit();
reg [7:0] a, b;
reg [2:0] op;
wire [7:0] result;
wire overflow;
integer error_count, test_count;
alu_8bit dut (
.a (a),
.b (b),
.op (op),
.result (result),
.overflow(overflow)
);
task run_check;
input [7:0] t_a, t_b;
input [2:0] t_op;
input [7:0] t_expected;
input t_exp_ovf;
input [31:0] t_id;
begin
a = t_a;
b = t_b;
op = t_op;
#10;
test_count = test_count + 1;
if (result === 8'hxx || overflow === 1'bx)
$fatal(1, "[test %0d] Output is x: Undriven net or corrupt state", t_id);
if (overflow !== t_exp_ovf)
$warning("[test %0d] Overflow flag got=%b expected=%b", t_id, overflow, t_exp_ovf);
assert (result === t_expected)
else begin
$error("[test %0d] op=%0b a=%0h b=%0h expected=%0h got=%0h",
t_id, t_op, t_a, t_b, t_expected, result);
error_count = error_count + 1;
end
end
endtask
initial begin
error_count = 0;
test_count = 0;
$info("ALU verification initiated");
// Directed test vectors: Addition
run_check(8'd10, 8'd20, 3'b000, 8'd30, 1'b0, 1);
run_check(8'd200, 8'd100, 3'b000, 8'd44, 1'b1, 2); // Overflow condition
run_check(8'd0, 8'd0, 3'b000, 8'd0, 1'b0, 3);
run_check(8'd255, 8'd255, 3'b000, 8'd254, 1'b1, 4);
// Bitwise AND
run_check(8'hAA, 8'h55, 3'b001, 8'h00, 1'b0, 5);
run_check(8'hFF, 8'hF0, 3'b001, 8'hF0, 1'b0, 6);
// Bitwise XOR
run_check(8'hAA, 8'hAA, 3'b010, 8'h00, 1'b0, 7);
run_check(8'h00, 8'hFF, 3'b010, 8'hFF, 1'b0, 8);
$info("%0d tests executed", test_count);
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 next article examines golden reference models for generating expected values automatically in simulation.