Classes and Objects for Verification
RTL hardware design describes static physical structures like registers and gates. In contrast, verification testbenches model dynamic stimulus streams, transaction packets, and behavioral scoreboards. SystemVerilog provides object-oriented classes to model verification testbenches cleanly. Classes execute strictly in simulation and are not intended for hardware synthesis.
Limitations of Module-Based Testbenches
Classic Verilog testbenches rely on static reg arrays, tasks, and procedural blocks.
As verification environments expand, managing individual variables across complex protocols becomes difficult.
Every transaction format requires dedicated parallel arrays and manual tracking logic.
Object-oriented classes solve this scaling issue by encapsulating data fields and methods into unified objects.
Defining a Transaction Class
A class defines the template for a data transaction:
class fifo_transaction;
rand bit [7:0] data; // 'rand' enables constrained randomization
bit wr_en;
function void display();
$display("Transaction: data=%h wr_en=%b", data, wr_en);
endfunction
endclass
This class encapsulates the data payload, control flags, and reporting methods into one structure. It contains no synthesis clocks or physical port definitions. It exists purely to structure verification data in simulation memory.
Creating and Using Dynamic Objects
A class variable represents an object handle (pointer).
Construct the actual object dynamically using the new() constructor:
module tb_fifo_oop;
fifo_transaction txn; // Declare handle reference
initial begin
txn = new(); // Allocate dynamic object instance
txn.data = 8'hAB;
txn.wr_en = 1'b1;
txn.display(); // Output: Transaction: data=ab wr_en=1
end
endmodule
In hardware Verilog, wires and registers occupy permanent storage throughout simulation.
In SystemVerilog verification, handles can reference different objects dynamically or hold null pointers.
This dynamic memory allocation allows testbenches to generate millions of distinct transactions seamlessly.
Managing Multiple Objects
Testbenches can allocate collections of transactions using standard arrays:
fifo_transaction txn_array[10];
initial begin
for (int i = 0; i < 10; i++) begin
txn_array[i] = new();
txn_array[i].data = i * 8'h11;
txn_array[i].wr_en = 1'b1;
end
foreach (txn_array[i])
txn_array[i].display();
end
Each array element points to an independent object in simulation memory. This approach isolates transactions cleanly without requiring parallel global arrays.
Passing Objects to Testbench Tasks
Because objects encapsulate transaction data, driver tasks require only a single handle argument:
task automatic drive_transaction(fifo_transaction t);
wdata = t.data;
wr_en = t.wr_en;
@(posedge clk);
wr_en = 1'b0;
endtask
initial begin
fifo_transaction t = new();
t.data = 8'h42;
t.wr_en = 1'b1;
drive_transaction(t);
end
Passing object handles simplifies task interfaces and prevents parameter mismatches. This transaction-oriented architecture forms the foundation of modern Universal Verification Methodology (UVM) environments.
Practice Exercise
Define a class named alu_transaction.
Include two 8-bit operands, a 3-bit opcode, and an 8-bit expected result field.
Create an object, assign test values, and display the transaction content.
Solution
class alu_transaction;
bit [7:0] operand_a;
bit [7:0] operand_b;
bit [2:0] opcode;
bit [7:0] expected_result;
function void display();
$display("ALU txn: a=%h b=%h op=%b expected=%h",
operand_a, operand_b, opcode, expected_result);
endfunction
endclass
module tb_alu_class;
alu_transaction t;
initial begin
t = new();
t.operand_a = 8'h0A;
t.operand_b = 8'h05;
t.opcode = 3'b000; // Addition operation
t.expected_result = 8'h0F;
t.display();
end
endmodule
The alu_transaction class bundles all input stimuli and reference outputs into a single object.
This prepares the data structure for constrained random generation.
SystemVerilog classes provide object-oriented abstractions for verification testbenches.
Engineers allocate transaction objects dynamically using handles and the new() constructor.
Encapsulating transaction fields simplifies driver interfaces and scales testbench architectures.
The next article explains how to generate automated random stimulus with rand and randc.