[SV]SystemVerilog Static Class Members

                      SystemVerilog Static Class Members

       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.

 

一、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.

(1)Syntax

static <data_type> <property_name>;


二、Static methods are the same as static properties,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

     Note:
     Static class properties and methods can be used without creating an object of that type.

(1)Syntax

static task/function <method_name>;

(2)Static properties example

       In the below example,The class has the variable packet_id, which is the unique ID assigned to the packet;Static variable no_of_pkts_created, no_of_pkts_created will get incremented on every object creation.no_of_pkts_created is assigned to packet_id.

class packet;
   
  //class properties
  byte packet_id;
     
  //static property to keep track of number of pkt's created
  static byte no_of_pkts_created;
   
  //constructor
  function new();
    //incrementing pkt count on creating an object
    no_of_pkts_created++;
    packet_id = no_of_pkts_created;
  endfunction
   
  //method to display class prperties
  function void display();
    $display("--------------------------------------");
    $display("\t packet_id  = %0d",packet_id);
    $display("--------------------------------------");
  endfunction
endclass
 
module static_properties;
  packet pkt[3];
 
  initial begin
    foreach(pkt[i]) begin
      pkt[i] = new();
      pkt[i].display();
    end
  end 
endmodule

 Simulator Output 

————————————–
packet_id  = 1
————————————–
————————————–
packet_id  = 2
————————————–
————————————–
packet_id  = 3
————————————–

(3)Static method example

       Below example shows the declaration of a static function.

class packet;
   
  //class properties
  byte packet_id;
     
  //static property to keep track of number of pkt's created
  static byte no_of_pkts_created;
   
  //constructor
  function new();
    //incrementing pkt count on creating an object
    no_of_pkts_created++;
    packet_id = no_of_pkts_created;
  endfunction
   
  //method to display class prperties
  function void display();
    $display("--------------------------------------");
    $display("\t packet_id  = %0d",packet_id);
    $display("--------------------------------------");
  endfunction
endclass
 
module static_properties;
  packet pkt[3];
 
  initial begin
    foreach(pkt[i]) begin
      pkt[i] = new();
      pkt[i].display();
    end
  end 
endmodule

Simulator Output 

————————————–
3 packets created.
————————————–

(4)STATIC METHOD TRYING TO ACCESS A NON-STATIC VARIABLE

       In the below example,The static function tries to access the non-static variable of the class, which leads to a compilation error.

class packet;
  byte packet_id;
   
  //static property to keep track of number of pkt's created
  static byte no_of_pkts_created;
   
  //constructor
  function new();
    //incrementing pkt count on creating an object
    no_of_pkts_created++;
  endfunction
     
  //Static method to display class prperties
  static function void display_packets_created();
    $display("--------------------------------------");
    $display("\t Packet Id is %0d",packet_id);
    $display("\t %0d packets created.",no_of_pkts_created);
    $display("--------------------------------------");
  endfunction
endclass
 
module static_properties;
  packet pkt[3];
 
  initial begin
    foreach(pkt[i]) begin
      pkt[i] = new();
    end
    pkt[0].display_packets_created();
  end 
endmodule

Simulator Output 

Error-[SV-AMC] Non-static member access
testbench.sv, 19
$unit, “packet_id”
Illegal access of non-static member ‘packet_id’ from static method
‘packet::display_packets_created’.

(5)ACCESSING STATIC CLASS PROPERTIES WITHOUT CREATING AN OBJECT

       In the below example,Static class variable (no_of_pkts_created) and Static class function (display_packets_created) is accessed with class handle which is not constructed (new() is not done).

class packet;
     
  //static property to keep track of number of pkt's created
  static byte no_of_pkts_created;
   
  //constructor
  function new();
    //incrementing pkt count on creating an object
    no_of_pkts_created++;
  endfunction
     
  //Static method to display class prperties
  static function void display_packets_created();
    $display("--------------------------------------");
    $display("\t %0d packets created.",no_of_pkts_created);
    $display("--------------------------------------");
  endfunction
endclass
 
module static_properties;
  packet pkt[3];
  packet p;
 
  initial begin
    foreach(pkt[i]) begin
      pkt[i] = new();
    end
     
    //Accesing static Variable with class handle p
    $display("--------------------------------------");
    $display("\t %0d packets created.",p.no_of_pkts_created);
    $display("--------------------------------------");   
     
    //Accesing static Method with class handle p
    p.display_packets_created();
  end 
endmodule

Simulator Output 

————————————–
3 packets created.
————————————–
————————————–
3 packets created.
————————————–

发布了185 篇原创文章 · 获赞 118 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105156415
今日推荐