Lesson 1: Which SystemVerilog Features Does a Testbench Need?
A testbench needs the parts of SystemVerilog that describe behavior rather than hardware: classes to bundle a test's inputs and results into one transaction object, functions that compute the expected answer (a reference model), dynamic containers such as queues, tasks that drive the design over several clocks, and assertions that report a mismatch. With those you can write a test that checks its own results instead of one you have to read waveforms for.
What you'll learn
- The design used throughout the course
- Classes, constructors and a reference model function
- Queues and tasks for driving the design
- Immediate assertions as checkers
- A real gotcha: static variables in module functions
Conventions and assumptions
Every lesson tests the same small design: a registered 8-bit ALU (add, sub, and, or, xor) whose 9-bit result appears one clock after in_valid. All code was compiled and run in AMD Vivado xsim 2025.2, and the output shown is copied from those runs. The complete files are in the site's repository under scripts/verification.
The design under test
// The design under test for the verification course: a registered 8-bit
// ALU. Inputs are sampled when in_valid is high; the result appears one
// clock later with out_valid.
module alu (
input logic clk,
input logic rst_n,
input logic in_valid,
input logic [2:0] op, // 0 add, 1 sub, 2 and, 3 or, 4 xor
input logic [7:0] a, b,
output logic out_valid,
output logic [8:0] y
);
always_ff @(posedge clk) begin
if (!rst_n) begin
out_valid <= 1'b0;
y <= '0;
end else begin
out_valid <= in_valid;
if (in_valid) begin
case (op)
3'd0: y <= a + b;
3'd1: y <= a - b;
3'd2: y <= {1'b0, a & b};
3'd3: y <= {1'b0, a | b};
3'd4: y <= {1'b0, a ^ b};
default: y <= '0;
endcase
end
end
end
endmodule
A transaction class with its own reference model
The class holds one test's inputs and the output it produced. expected() is the reference model: an independent, simple description of what the ALU should do. Keeping it in the class means every part of the testbench that has a transaction can check it.
class alu_txn;
bit [2:0] op;
bit [7:0] a, b;
bit [8:0] y; // filled in from the DUT
function new(bit [2:0] op, bit [7:0] a, bit [7:0] b);
this.op = op; this.a = a; this.b = b;
endfunction
function bit [8:0] expected();
case (op)
0: return a + b;
1: return a - b;
2: return a & b;
3: return a | b;
4: return a ^ b;
default: return 0;
endcase
endfunction
endclass
Driving and checking
A queue holds the directed tests. The run task drives one transaction on the falling edge, waits a clock for the registered result, and checks it with an immediate assertion. assert ... else lets you count failures and print a useful message.
alu_txn tests[$]; // a queue of directed tests
int errors = 0;
function automatic void add_test(bit [2:0] op, bit [7:0] a, bit [7:0] b);
alu_txn t = new(op, a, b);
tests.push_back(t);
endfunction
task automatic run(alu_txn t);
@(negedge clk); in_valid = 1; op = t.op; a = t.a; b = t.b;
@(negedge clk); in_valid = 0;
t.y = y;
assert (t.y == t.expected())
else begin errors++; $error("op %0d: %0d, %0d gave %0d, expected %0d", t.op, t.a, t.b, t.y, t.expected()); end
endtask
What the run prints
op 0 a 200 b 100 -> y 300
op 1 a 5 b 10 -> y 507
op 2 a 240 b 60 -> y 48
op 3 a 240 b 15 -> y 255
op 4 a 255 b 15 -> y 240
5 tests, 0 errors
5 − 10 gives 507 because the result is 9 bits: −5 wraps to 512 − 5 = 507. 200 + 100 = 300 needs the ninth bit, which is why the output is 9 bits wide.
The gotcha: static by default
The first version of add_test was declared function void, not function automatic void, and every test came out as 0 + 0. Functions and tasks in a module are static by default, so the line alu_txn t = new(op, a, b) is a static variable initializer: it runs once, at time zero, with the arguments still 0. Declaring the function automatic makes t a fresh variable on each call. Class methods are automatic already; module and program functions are not.
Exercises
- E1. Add a test for 0 − 1. What should y be?
- E2. Add a sixth operation code, 5, to the test list. What does the ALU return, and what should the reference model say?
- E3. Rewrite run so it takes the queue and runs every test, returning the error count.
Solutions
- E1. 511: 0 − 1 is −1, which is 512 − 1 in 9 bits. add_test(1, 0, 1) and the assertion passes.
- E2. The ALU's default branch returns 0, and so does expected(). Testing an illegal code is still worth doing: it checks the design does something defined.
- E3. task automatic run_all(ref alu_txn q[$], output int errs); errs = 0; foreach (q[i]) begin run(q[i]); if (q[i].y != q[i].expected()) errs++; end endtask
Common mistakes
- Calling new inside a static function and getting the same object, or zeros, every time.
- Comparing with == when the DUT might output x: use !== in checks so an unknown counts as a mismatch.
- Sampling the result on the same edge the inputs change, which races. Drive on the falling edge, check after the rising edge.
- Writing the reference model by copying the RTL, so both share the same bug.
Check your understanding
- Q1. Why should the reference model not be a copy of the RTL? Answer: a copied bug passes every check. The model should be written independently, ideally from the specification.
- Q2. What does assert (cond) else ... do when cond is x? Answer: it fails, since an immediate assertion passes only when the expression is true.
- Q3. Why does add_test need to be automatic? Answer: its local variable's initializer would otherwise run once, at time zero.
The course
Related
Last verified
Run in Vivado xsim 2025.2 on 14 November 2026; outputs copied from that run. Language rules checked against IEEE 1800-2023.