Вы находитесь на странице: 1из 18

lass 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.

2. Object:- An object is a member or an "instance" of a class

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.

d. "function new ()" has default values to the arguments.

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.

virtual class Base;

bit [7:0] data;

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.

// Define a class called "Packet" with a 32-bit variable to store address


// Initialize "addr" to 32'hfade_cafe in the new function, also called constructor
class Packet;
bit [31:0] addr;
 
function new ();
addr = 32'hfade_cafe;
endfunction
endclass
 
module tb;
 
// Create a class handle called "pkt" and instantiate the class object
initial begin
// The class's constructor new() fn is called when the object is instantiated
Packet pkt = new;
 
// Display the class variable - Because constructor was called during
// instantiation, this variable is expected to have 32'hfade_cafe;
$display ("addr=0x%0h", pkt.addr);
end
endmodule
 
b. implicitly defined: if a class don’t have function new() , so it will be create automatically.

// Define a class called "Packet" with a 32-bit variable to store address


// Initialize "addr" to 32'hfade_cafe in the new function, also called constructor
class Packet;
bit [31:0] addr;
 
function new ();
addr = 32'hfade_cafe;
endfunction
endclass
 
module tb;
 
// Create a class handle called "pkt" and instantiate the class object
initial begin
// The class's constructor new() fn is called when the object is instantiated
Packet pkt = new;
 
// Display the class variable - Because constructor was called during
// instantiation, this variable is expected to have 32'hfade_cafe;
$display ("addr=0x%0h", pkt.addr);
end
endmodule
 

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

display method is called by “display” method in derived class

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

type class DEF;


class ABC;
DEF def;
endclass
 
class DEF;
ABC abc;
endclass
typedef can be used on a class with a parameterized port list

. 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();

    $display("Inside base class");

  endfunction

endclass

 
// extended class 1

class ext_class_1 extends base_class;

  function void display();

    $display("Inside extended class 1");

  endfunction

endclass

// extended class 2

class ext_class_2 extends base_class;

  function void display();

    $display("Inside extended class 2");

  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 

    //declare and create extended class

    ext_class_1 ec_1 = new();

    ext_class_2 ec_2 = new();

    ext_class_3 ec_3 = new();

     

    //base class handle

    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;

     

    //accessing extended class methods using base class handle

    b_c[0].display();

    b_c[1].display();

    b_c[2].display();

  end

endmodule

 Simulator Output  

Inside extended class 1


Inside extended class 2
Inside extended class 3
11. Virtual method:-Method with virtual keyword is virtual method.
a. Virtual function:- function with virtual keyword is virtual function.

virtual function function_name;
   
  //Function definition
endfunction

b. Virtual task:- task with virtual keyword is virtual task.

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.

In the below explanation, extended_class is an extended class of base_class.

base_class     b_c;

extended_class e_c;

assigning e_c to b_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

Output:- Inside base class


output:- Inside extend class

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

13. System Verilog copy object:-

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;

function new (int addr, int data, int id);


hdr = new (id);
this.addr = addr;
this.data = data;
endfunction
function display (string name);
$display ("[%s] addr=0x%0h data=0x%0h id=%0d", name, addr, data, hdr.id);
endfunction
endclass

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");

// Shallow copy p1 into p2; p2 is a new object with contents in p1


p2 = new p1;
p2.display ("p2");

// Now let's change the addr and id in p1


p1.addr = 32'habcd_ef12;
p1.data = 32'h5a5a_5a5a;
p1.hdr.id = 17;
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

output [p1] addr=0xfacecafe data=0x12345678 id=26


[p2] addr=0xfacecafe data=0x12345678 id=26

[p1] addr=0xabcdef12 data=0x5a5a5a5a id=17


[p2] addr=0xfacecafe data=0x12345678 id=17
b. deep copy:- SystemVerilog deep copy copies all the class members and its nested class members. unlike in shallow copy, only nested
class handles will be copied. In shallow copy, Objects will not be copied, only their handles will be copied. to perform a full or deep copy, the
custom method needs to be added.

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");

p2 = new (1,2,3); // give some values


p2.copy (p1);
p2.display ("p2");

// Now let's change the addr and id in p1


p1.addr = 32'habcd_ef12;
p1.data = 32'h5a5a_5a5a;
p1.hdr.id = 32'h11;
p1.display ("p1");

// Now let's print p2 - you'll see the changes made to hdr id


// but not addr
p2.display ("p2");
end
endmodule
Note that we have invoked the custom  copy()  function here instead of the shallow copy method, and hence Header object contents
are expected to be copied into p2 as well.

Simulation Log
ncsim> run
[p1] addr=0xfacecafe data=0x12345678 id=26
[p2] addr=0xfacecafe data=0x12345678 id=26

[p1] addr=0xabcdef12 data=0x5a5a5a5a id=17


[p2] addr=0xfacecafe data=0x12345678 id=26
ncsim: *W,RNQUIE: Simulation is complete.

Вам также может понравиться