Tuesday, July 21, 2020

What is extern in system verilog and its purpose?

Hi Everyone !!!,
Today I am going to share the ideas about extern and its use case.

In System verilog, extern indicates that the body of the methods(task/function/constraint implementaion ) is to be found on outside of the class.
By using scope resolution operator also we can access the methods in outside of the class. Here the difference is if any method declared as extern it must be implemented in outside of the class other wise it will throw the compilation error. 

Coding Examples:
    
                    class methods;
                         rand int data1;
                         rand int data2;
                         constraint protocol1;  //**** implicit form--> it throws an warning message if we didn't implemnt the constraint in outside of the class****//
                         extern constraint protocol2; //*** explicit form--> we must implement the function otherwise it leads to compilation error
                  endclass
//*** Before the method name class name should  be specified with scope resolution operator to specify which class the method corresponds to ***//
                  constraint methods::protocol1{data1 inside{-4,5,7};} // **if we didn't implement we will get the warning message as protocol1 is not defined***//
                  constraint methods::protocol2{data2 >=0;} //  ** if we didn't implement we will get the compilation error**//
                
                   module TB;
                         methods mth;
                         initial begin
                           mth=new();
                            repeat(5) begin
                             mth.randomize();
                           end
                         end
                   endmodule


Example 2:

              class methods;
                  bit[2:0] data;
                  bit[2:0] addr;
//Here the function is declared as extern. From this we can understand the computation method implementation contains outside of the class//
                  extern function void computation();
              endclass
              // Implentation of the function in done outside of the class
                   function void methods::computation();
                      $display(" %0d %0d",data,addr);
                   endfunction
          
                   module extern_method;
                          methods m;
                          initial begin
                             m=new();
                              m.addr=5;
                              m.data=4;
                              m.computation();
                          end
                  endmodule

Note:
    local/protected/virtual qualifiers are allowed in extern methods. For example in above program we can declare extern vitrual function void computation
    The number of arguements,arguments name and its type should  be matched with definition and implementation  of methods.
         

Hope this code 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

     "Share your knowledge. It is the way to achieve immortality"
                                                                                                  -Dalai Lama



3 comments:

  1. In the second example, while function call, Computation was wrongly named as computaion.

    ReplyDelete
    Replies
    1. Hari Updated. Thanks for notifying!!! Keep supporting!!!

      Delete

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