EE5530 - Lecture15 - Virtual Interfaces
EE5530 - Lecture15 - Virtual Interfaces
Advanced OOP
Memory Read
1. Explain IPC?
2. What is an event?
3. What is a semaphore?
class Agent;
mailbox gen2agt, agt2drv;
Transaction tr;
function new(mailbox gen2agt, agt2drv);
this.gen2agt = gen2agt;
this.agt2drv = agt2drv;
endfunction
task run();
forever begin
gen2agt.get(tr); // Get transaction from upstream block
... // Do some processing
agt2drv.put(tr); // Send it to downstream block
end
endtask
task wrap_up(); // Empty for now
endtask
endclass
Advanced OOP and Virtual Interfaces
Inheritance and Composition
Inheritance: (is-a relationship)
• Allows users to extend existing classes, making minor modifications. Extending
the “Automobile” class example, users might create subclasses for “sedan”,
“truck”, “van”, etc. The “van” class might also have a “minivan” subclass. Etc. In
these cases, the subclass IS-A superclass. i.e. a “sedan” is a “Automobile”.
• When using inheritance, the sub-class “inherits” all the parents public/protected
data properties and methods. It is allowed to override them, or use them as-is.
Child class has access to class properties and class methods of its base class. Thus, inheritance grants
re-usability.
Along with existing class properties and methods, a derived class can also add new properties and
methods based on the requirement.
A child class can modify its base class properties and methods without disturbing the base class.
Multilevel inheritance is also possible in SystemVerilog. A derived class can also further extended,
this is multilevel inheritance.
Inheritance
A Child_trans is an extended class from its parent_trans (base class). A child_class can access its base
class properties (data variable) and methods (disp_p function)
class parent_trans;
bit [31:0] data; module class_example;
initial begin
function void disp_p(); child_trans c_tr;
$display("Value of data = %0h", data); c_tr = new();
endfunction c_tr.data = 5; // child class is updating property of its
endclass base class
c_tr.id = 1;
class child_trans extends parent_trans;
int id; c_tr.disp_p(); // child class is accessing method of its
base class
function void disp_c(); c_tr.disp_c();
$display("Value of id = %0h", id); end
endfunction endmodule
endclass
Multilevel Inheritance
Multilevel Inheritance Example
class parent_trans;
bit [31:0] data_p;
cB_tr.disp_p();
cB_tr.disp_c1();
cB_tr.disp_cA();
cB_tr.disp_cB();
end
endmodule
Overriding base class members
class parent_trans;
bit [31:0] data = 100; module class_example;
int id = 1; initial begin
child_trans c_tr;
function void display(); c_tr = new();
$display("Base: Value of data = %0d and id = %0d", data, id); c_tr.display();
endfunction end
endclass endmodule
class parent_trans;
bit [31:0] data; module class_example;
initial begin
function void display(); child_trans c_tr;
$display("Base: Value of data = %0h", data); c_tr = new();
endfunction
endclass c_tr.data = 5;
c_tr.display();
class child_trans extends parent_trans; end
bit [31:0] data; endmodule
class parent_trans;
bit [31:0] data; module class_example;
initial begin
function new(bit [31:0] data); child_trans c_tr;
this.data = data; 00
c_tr = new(5, 7);
$display("Base: Value of data = %0h", data);
end
endfunction
endclass endmodule
endclass
Virtual functions and tasks in SystemVerilog
A virtual method is a virtual function or task from the base class which can be overridden by a method of its child
class having the same signature (same method name and arguments). In simple words, When a child class handle is
assigned to its base class. On calling a method using a base class handle, the base class method will be executed. On
declaring a method as a virtual method, a base class handle can call the method of its child class.
//abstract class
virtual class packet;
bit [31:0] addr;
virtual_class, "p = new();"
Endclass
Instantiation of the object 'p' can not be done because its type
'packet' is
module virtual_class;
an abstract base class.
initial begin
Perhaps there is a derived class that should be used.
packet p;
p = new();
end
endmodule
Abstract Class Example
//abstract class
virtual class packet;
bit [31:0] addr;
endclass
module virtual_class;
initial begin
extended_packet p;
p = new();
p.addr = 10;
p.display();
end
endmodule
Pure Virtual Methods
A virtual method inside an abstract class can be declared with a pure keyword. Such methods only require a
prototype to be specified within the abstract class and the implementation is left to the subclasses
Access Control
By default all the members and methods of a class are accessible from anywhere using the object handle,
sometimes this could corrupt the class members values, which should not be touched at all.
Access control rules that restrict the members of a class from being used outside the class, this is
achieved by prefixing the class members with the keywords, local and protected
function display();
$display("tmp_addr = %0d",tmp_addr); Addr = 15
endfunction
endclass
// module
module encapsulation;
initial begin
parent_class p_c = new(5);
p_c.display();
end
endmodule
Protected class members
class parent_class;
protected bit [31:0] tmp_addr; // module
module encapsulation;
function new(bit [31:0] r_addr); initial begin
tmp_addr = r_addr + 10; parent_class p_c = new(5);
endfunction child_class c_c = new(10);
To make data members visible only to the class, use the local
keyword.
class myPacket extends BasePacket;
local int x;
24
Virtual Interfaces
1. An interface represents signals that are used to connect design modules or
testbench to the DUT and commonly known as a physical interface.
2. The design and physical interface are static in nature. Hence, they can not be
used dynamically.
3. In modern testbench, randomized class objects are used and connect to the
design dynamically.
4. Hence, to bridge the gap between the static world of modules and the dynamic
world of objects, a virtual interface is used as a pointer or handle for an actual
interface
Virtual Interfaces
1.The virtual interface must be pointed to the actual or physical interface. This is
also known as virtual interface initialization.
3.The virtual interfaces can be passed to the functions and tasks as an argument.
initial begin
virtual mult_if vif;
drive drv = new();
vif = inf;
repeat(3) begin
assert(drv.randomize());
@(posedge vif.clk);
vif.a = drv.a;
vif.b = drv.b;
#2 vif.en = 1;
#5 inf.en = 0;
wait(vif.ack);
$display("%0t: a=%d b=%d, out=%d", $time, vif.a,vif.b,vif.out);
#20;
end
#10;
$finish;
end