Class in System Verilog
Class in System Verilog
Class in System Verilog
1. Class:- A class is a template for objects. A class defines object properties including a valid range of values, and a default value. A class also
describes object behavior.
3. Point to be note:-
a. "function new ()" is called the constructor and is automatically called upon object creation.
b. "this" keyword is used to refer to the current class. Normally used within a class to refer to its own properties/methods.
c. "display ()" is a function, and rightly so, because displaying values does not consume simulation time.
4. Methods:- We know that a class can define both attributes and behaviors. Again attributes are defined by variables and behaviors are represented
by methods. In other words, methods define the abilities of an object.
5. Abstract/virtual class :- If u create an abtract class using "virtual " keyword then u cannot able to create an object of that create. it is used in
when u dont want others to create an obj of that class and use in tb.
bit enable;
endclass
6. Constructor:-it is used to create a new object of particular class data type. Constructor cannot be virtual or static.
a. explicitly defined.
7. Super keyword:- “super” keyword is used in oder to call the method /propertyof baseclass in the derived class.
// Define a simple class and initialize the class member "data" in new() function
class baseClass;
bit [15:0] data;
function new ();
data = 16'hface;
endfunction
endclass
// Define a child class extended from the above class with more members
// The constructor new() function accepts a value as argument, and by default is 2
class subClass extends baseClass;
bit [3:0] id;
bit [2:0] mode = 3;
function new (int val = 2);
// The new() function in child class calls the new function in
// the base class using the "super" keyword
super.new ();
// Assign the value obtained through the argument to the class member
id = val;
endfunction
endclass
module tb;
initial begin
// Create two handles for the child class
subClass sc1, sc2;
// Instantiate the child class and display member variable values
sc1 = new ();
$display ("data=0x%0h id=%0d mode=%0d", sc1.data, sc1.id, sc1.mode);
// Pass a value as argument to the new function in this case and print
sc2 = new (4);
$display ("data=0x%0h id=%0d mode=%0d", sc2.data, sc2.id, sc2.mode);
end
endmodule
Error based code where there is no derived class and super keyword is used
class Packet;
int addr;
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
class extPacket; // "extends" keyword missing -> not a child class
function new ();
super.new ();
endfunction
endclass
module tb;
Packet p;
extPacket ep;
initial begin
ep = new();
p = new();
p.display();
end
endmodule
class Packet;
int addr;
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
class extPacket extends Packet;
function display();
super.display(); // Call base class display method
$display ("[Child] addr=0x%0h", addr);
endfunction
function new ();
super.new ();
endfunction
endclass
module tb;
Packet p;
extPacket ep;
initial begin
ep = new();
p = new();
ep.display(); //first base class display then derioved class display will execute
end
endmodule
8. typedef class:-
class ABC;
DEF def; // Error: DEF has not been declared yet
endclass
class DEF;
ABC abc;
endclass
. typedef XYZ;
module top;
XYZ #(8'h3f, real) xyz0; // positional parameter override
XYZ #(.ADDR(8'h60), .T(real)) xyz1; // named parameter override
endmodule
class XYZ #(parameter ADDR = 8'h00, type T = int);
endclass
9. INHERITANCE:-inheritance is a concept in oops that allow us to extend the class into another class and allow to access to all the
properties and methods of the original parent class from the handle odf a new class object.
class Packet;
int addr;
function new (int addr);
this.addr = addr;
endfunction
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
// A subclass called 'ExtPacket' is derived from the base class 'Packet' using
// 'extends' keyword which makes 'EthPacket' a child of the parent class 'Packet'
// The child class inherits all variables and methods from the parent class
class ExtPacket extends Packet;
// This is a new variable only available in child class
int data;
function new (int addr, data);
super.new (addr); // Calls 'new' method of parent class
this.data = data;
endfunction
function display ();
$display ("[Child] addr=0x%0h data=0x%0h", addr, data);
endfunction
endclass
module tb;
Packet bc; // bc stands for BaseClass
ExtPacket sc; // sc stands for SubClass
initial begin
bc = new (32'hface_cafe);
bc.display ();
sc = new (32'hfeed_feed, 32'h1234_5678);
sc.display ();
end
endmodule
10. Polymorphism:- Polymorphism means many forms. Polymorphism in SystemVerilog provides an ability to an object to take on many
forms.
Method handle of super-class can be made to refer to the subclass method, this allows polymorphism or different forms of the same method.
How, many forms of a method can be made by referring to the subclass method?
will see with an example,
// base class
class base_class;
virtual function void display();
endfunction
endclass
// extended class 1
class ext_class_1 extends base_class;
function void display();
endfunction
endclass
// extended class 2
class ext_class_2 extends base_class;
function void display();
endfunction
endclass
// extended class 3
class ext_class_3 extends base_class;
function void display();
$display("Inside extended class 3");
endfunction
endclass
// module
module class_polymorphism;
initial begin
ext_class_1 ec_1 = new();
ext_class_2 ec_2 = new();
ext_class_3 ec_3 = new();
base_class b_c[3];
//assigning extended class to base class
b_c[0] = ec_1;
b_c[1] = ec_2;
b_c[2] = ec_3;
b_c[0].display();
b_c[1].display();
b_c[2].display();
end
endmodule
Simulator Output
virtual function function_name;
//Function definition
endfunction
virtual task task_name;
//task definition
endtask
About Virtual Method
In a virtual method, If the base_class handle is referring to the extended class, then the extended class method handle will get assigned to
the base class handle.
base_class b_c;
extended_class e_c;
b_c = e_c;
On calling b_c.display()
if display() method in base_class is virtual, then extended class display method will get called
if display() method in base_class is non-virtual, then base class display method will get called
class base_class; class base_class;
function void display; virtual function void display;
$display("Inside base_class"); $display("Inside base_class");
endfunction endfunction
endclass endclass
class extended_class extends base_class; class extended_class extends base_class;
function void display; function void display;
$display("Inside extended class"); $display("Inside extended class");
endfunction endfunction
endclass endclass
module virtual_class; module virtual_class;
initial begin initial begin
base_class b_c; base_class b_c;
extended_class e_c; extended_class e_c;
e_c = new(); e_c = new();
b_c = e_c; b_c = e_c;
b_c.display(); b_c.display();
end end
endmodule endmodule
12. Static property:- Class members can be created with the keyword static. class members with the keyword static are called as static class
members. the class can have static properties and static methods (functions and tasks). a single copy of static variables is shared across
multiple instances
a. Static Properties
The class can have multiple instances, each instance of the class will be having its own copy of variables.
Sometimes only one version of a variable is required to be shared by all instances. These class properties are created using the
keyword static. Static<datatyoe><varname>
b. Static methods
a static method can access only static properties of the class and access to the non-static properties is illegal and lead to a
compilation error.
Static methods cannot be virtual. Static task/function <name>
Note:
Static class properties and methods can be used without creating an object of that type
class packet; class packet;
//class properties
//class properties byte packet_id;
byte packet_id;
//static property to keep track of number
//static property to keep track of number of pkt's of pkt's created
created static byte no_of_pkts_created;
static byte no_of_pkts_created;
//constructor
//constructor function new();
function new(); //incrementing pkt count on creating an
//incrementing pkt count on creating an object object
no_of_pkts_created++; no_of_pkts_created++;
packet_id = no_of_pkts_created; packet_id = no_of_pkts_created;
endfunction endfunction
//method to display class prperties
function void display(); //method to display class prperties
$display("--------------------------------------"); function void display();
$display("\t packet_id = %0d",packet_id); $display("\t packet_id =
$display("--------------------------------------"); %0d",packet_id);
endfunction endfunction
endclass endclass
module static_properties; module static_properties;
packet pkt[3]; packet pkt[3];
initial begin initial begin
foreach(pkt[i]) begin foreach(pkt[i]) begin
pkt[i] = new(); pkt[i] = new();
packet_id = 1
3 packed cretaed
packet_id = 2
packet_id = 3
a. shallow copy:content of pkt is copied to pkt2 when pkt is used along with new() constructor for new object.
Packet pkt, pkt2;
pkt = new;
pkt2 = new pkt;
class Header;
int id;
function new (int id);
this.id = id;
endfunction
function showId();
$display ("id=0x%0d", id);
endfunction
endclass
class Packet;
int addr;
int data;
Header hdr;
module tb;
Packet p1, p2;
initial begin
// Create a new pkt object called p1
p1 = new (32'hface_cafe, 32'h1234_5678, 26);
p1.display ("p1");
// Print p2 and see that hdr.id points to the hdr in p1, while
// addr and data remain unchanged.
p2.display ("p2");
end
endmodule
Packet p1 = new;
Packet p2 = new;
p2.copy (p1);
class Packet;
...
function copy (Packet p);
this.addr = p.addr;
this.data = p.data;
this.hdr.id = p.hdr.id;
endfunction
...
endclass
module tb;
Packet p1, p2;
initial begin
p1 = new (32'hface_cafe, 32'h1234_5678, 32'h1a);
p1.display ("p1");
Simulation Log
ncsim> run
[p1] addr=0xfacecafe data=0x12345678 id=26
[p2] addr=0xfacecafe data=0x12345678 id=26