Classes, Inheritance, and Polymorphism
Model test transactions, understand handles, and extend behavior without copying an entire environment.
Chargement…
Model test transactions, understand handles, and extend behavior without copying an entire environment.
A class describes a verification object: a transaction, driver, or model. It groups data and methods without representing ordinary synthesizable RTL.
class bus_transaction;
rand bit write;
rand bit [15:0] address;
rand bit [31:0] data;
function new(bit [15:0] address = '0);
this.address = address;
endfunction
virtual function void display();
$display("write=%0b address=%04h data=%08h",
write, address, data);
endfunction
endclassThe declaration defines a type. new creates the object:
bus_transaction tr;
tr = new(16'h0100);
tr.display();tr is a handle. Assigning it to another handle makes both refer to the same object; it does not duplicate fields.
The constructor initializes the object. Keep randomization in the generator and timed activity in a run() task so construction does not hide those actions.
class error_transaction extends bus_transaction;
bit inject_parity_error;
function new();
super.new();
inject_parity_error = 1'b1;
endfunction
virtual function void display();
super.display();
$display("inject_parity_error=%0b", inject_parity_error);
endfunction
endclassA derived class should remain a genuine form of its base class. Here it adds error injection. Avoid deep inheritance solely to share a few lines.
bus_transaction base_handle;
error_transaction error_tr = new();
base_handle = error_tr;
base_handle.display();The base handle refers to the derived object. Because display is virtual, the call selects error_transaction's implementation. Without virtual, the handle type would select the method.
new original creates a separate object but shares internal handles: it is a shallow copy. A deep copy recreates each mutable subobject.
For bus_transaction's scalar fields, add this method inside the class:
virtual function bus_transaction copy();
bus_transaction result = new();
result.write = write;
result.address = address;
result.data = data;
return result;
endfunctionA derived class must adapt copying to preserve its extra fields and dynamic type. Copying a handle field is not enough; copy the subobject. Keep copy, compare, pack, and unpack consistent about their fields.
A mailbox passes handles. Rerandomizing an object already sent would change a transaction while the driver processes it:
task run();
bus_transaction blueprint = new();
repeat (100) begin
if (!blueprint.randomize())
$fatal(1, "Randomization failed");
gen_to_drv.put(blueprint.copy());
end
endtaskThe blueprint preserves history, including randc history, while each copy remains independent. Creating a new object each iteration also avoids sharing but restarts that history.
A base handle can receive a derived object. The reverse direction requires a checked cast:
error_transaction err;
if (!$cast(err, base_handle))
$fatal(1, "The transaction is not an error_transaction");Do not hide an essential $cast inside an assertion that can be disabled. Release unnecessary references and watch retained objects during long simulations.
📝 Test your knowledge - Chapter quiz