System Verilog OOP
System Verilog OOP
class class_name;
//Declare class properties
endclass: Transaction
initial begin
MemTrans MyMemTrans;
MyMemTrans = new();
end
// or
initial begin
MemTrans MyMemTrans = new();
end Verification using System Verilog 13
Custom Constructor
• For every class SystemVerilog creates a default new()
• Memory space is allocated
• All variables initialized to their default value
• Use a custom constructor to override this behavior
class Transaction;
logic [31:0] addr, crc, data[8];
function new();
addr = 3;
data = '{default:5};
endfunction
endclass
class Transaction;
logic [31:0] addr, crc, data[8];
function new(logic [31:0] a=3, d=5);
addr = a;
data = '{default:d};
endfunction
endclass
Transaction tr1 = new(10);
Transaction tr2 = new(, 6);
Transaction tr3 = new(10, 6);
Transaction tr4 = new(.a(10), .d(6));
Verification using System Verilog 15
Custom Constructor Exercise
Using your MemTrans class create a custom constructor so that
data_in and address are both initialized to 0.
function new;
data_in = 8’h0;
address = 4’h0;
endfunction
endclass
initial begin
MemTrans mt1, mt2;
mt1 = new(.address_init(2));
mt2 = new(.data_init(3), .address_init(4));
end Verification using System Verilog 19
Object Deallocation
t2 t1
t1 = new();
Object mem t1 Object mem t1_new
t2 t1 = null
t1 = null; Object mem t1 Object B
Transaction t;
t = new();
Usage:
t.addr = 32’h42;
t.display();
Verification using System Verilog 21
Using Objects Exercise
Using the previous MemTrans exercise assign the address of the
first object to 4’hF after construction
Use the print function to print out the values of data_in and
address for the 2 objects.
initial begin
MemTrans mt1, mt2;
mt1 = new(, .address_init(2));
mt2 = new(3, 4);
mt1.address = 4’hF;
mt1.print;
$display("-------------------");
mt2.print;
class PCI_Tran;
bit [31:0] addr, data;
extern function void display();
endclass
Specify the class that the function is defined for
Transaction::dec_count();
$display("Transaction.count = %0d", Transaction::count);
# Transaction.count = 1
Verification using System Verilog 31
Static Methods Exercise
Using the previous MemTrans exercise create a static method
called print_last_address that prints out the value of static
variable last_address
After allocating objects of class MemTrans, call the method
print_last_address to print out the value of
last_address
function new();
stats=new();
endfunction
task create_packet();
// Fill packet with data
stats.start();
...... // Transmit packet
stats.stop();
endtask
endclass
class Statistics;
endclass
typedef class Statistics;
class Transaction;
Statistics stats;
endclass
class Statistics;
endclass
Verification using System Verilog 38
Passing objects/handles to methods
• What happens when an object is passed to a method?
• The handle to the object is passed, not the object.
• A copy of the handle is made
object of class
gen trans
Transaction
src
id=0 startT=42
stats
dst.stats.StartT = 96;
end id=1
dst stats startT=96
Verification using System Verilog 42
Inheritance
• What if there is a class that does almost everything you need
and missing only one small feature?
– Inheritance – build upon existing classes.
• Ability of a class to inherit properties and methods from
another class(superclass) is called Inheritance.
• If class A inherits from class B, then B is called super
(Parent)class of A and A is called subclass(Child) of B.
• Objects of a subclass can be used where objects of the
corresponding superclass are expected.
• A subclass inherits all the members from its superclass(Except
the custom constructor), but subclass can invoke super class
constructor using “super” keyword.
module inheritence;
initial begin
child_class c = new();
c.addr = 10;
c.data = 20;
$display(“Addr= %0d Data = %0d",c.addr,c.data);
end
endmodule
Verification using System Verilog 44
Inheritance - Example
class Packet;
int addr;
int data;
endclass
initial begin
bc = new (32'hface_cafe);
bc.display ();