Friday, August 7, 2020

TYPEDEF Class in System Verilog

Hi Everyone !!!

    Today We are going to see the concepts of typedef class in System Verilog.

    A typedef used to provide a forward declaration of the class. In some use case, the class needs to be instantiated/used before the class declaration. For this kind of situation, typedef  is used.

Syntax :

typedef class class_name;

Typedef Examples:

Without typedef

 In the below example. There are two classes class1 and class2. class2 is instantiated class1 and class1 is instantiated class2. Both the classes need the handle of each other. As execution will happen in sequential order. So it leads to compilation error.

//class-1
class class1;
  class2 c;    //using class2 handle before declaring it.
endclass
 
//class-2
class class2;
  class1 c;
endclass
  
module  tb;
  initial begin
    class1 c1;
    class2 c2;
    $display("Inside typedef_class");
  end
endmodule
OUTPUT:
Error-[SE] Syntax error
Following verilog source has syntax error :
token 'class2' should be a valid type. Please declare it virtual if it
is an Interface.
"test1.sv", 6: token is ';'
class2 c;

With typedef

 When compiler sees typedef class it will know the definition of the class will found on later. The compilation error of the above example can be avoided by using typedef


typedef class class2; //Indication to the compiler class2 might be used before declaration
//class-1
class class1;
  class2 c;    // compiler knows declaration may come later.
endclass
 
//class-2
class class2;
  class1 c;
endclass
  
module tb;
  initial begin
    class1 c1;
    class2 c2;
    $display("Inside typedef_class");
  end
endmodule
OUTPUT:
Inside typedef_class
Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM  

"Doing Nothing for others is the undoing of ourselves".
                                                                                             - Horace Mann

                                          



No comments:

Post a Comment

`define Macro usage in System Verilog

 Hi Everyone !!! In this blog we are going to see the usage of `define macro in System Verilog. A text macro substitution facility has been ...