SystemVerilog External Methods
Table of Contents
If the definition of the method written outside the body of the class then the method is called an external method.
- external function. The definition of the function written outside the class body is referred to as an external function
- external task. The definition of the task written outside the class body is referred to as an external task
- to do this, need to declare the method (Function/Task) with an externkeyword in the class body along with
- any qualifiers (local, protected or virtual)
- full argument list
- The extern qualifier indicates that the body of the method (its implementation) is to be found outside the class declaration
- Before the method name, the class name should be specified with a class resolution operator to specify to which class the method corresponds to.
Note:
- Number of arguments, arguments name and argument type should match between method declaration and method definition
External function example
In the example below,
The function display is declared inside the class with the extern keyword, and the definition of the function is written outside the class.
//class with extern function class packet; bit [31:0] addr; bit [31:0] data; //function declaration - extern indicates out-of-body declaration extern virtual function void display(); endclass //function implementation outside class body function void packet::display(); $display("Addr = %0d Data = %0d",addr,data); endfunction module extern_method; initial begin packet p; p = new(); p.addr = 10; p.data = 20; p.display(); end endmodule
Simulator Output
Addr = 10 Data = 20
External task example
In the example below,
The task display is declared inside the class with the extern keyword, and the definition of the task is written outside the class.
//class with extern function class packet; bit [31:0] addr; bit [31:0] data; //task declaration - extern indicates out-of-body declaration extern virtual task display(); endclass //task implementation outside class body task packet::display(); $display("Addr = %0d Data = %0d",addr,data); endtask module extern_method; initial begin packet p; p = new(); p.addr = 10; p.data = 20; p.display(); end endmodule
Simulator Output
Addr = 10 Data = 20
External function with arguments
In the below examples,
an external function is declared and defined with arguments.
arguments name mismatch
Change in argument name between method declaration and method definition will lead to a compilation error.
class packet; //function declaration - extern indicates out-of-body declaration extern virtual function void display(bit [31:0] addr, data ); endclass //function implementation outside class body function void packet::display(bit [31:0] addr_t, data_t); $display("Addr = %0d Data = %0d",addr_t,data_t); endfunction module extern_method; initial begin packet p; p = new(); p.display(20,30); end endmodule
Simulator Output
Error-[ECMDSMPD] Mismatched method definition testbench.sv, 11 External class method definition should match prototype declaration. Argument names do not match. The signature of function 'packet::display' should match the corresponding prototype declaration at: "testbench.sv", 7.
arguments name match
class packet; //function declaration - extern indicates out-of-body declaration extern virtual function void display(bit [31:0] addr, data ); endclass //function implementation outside class body function void packet::display(bit [31:0] addr, data); $display("Addr = %0d Data = %0d",addr,data); endfunction module extern_method; initial begin packet p; p = new(); p.display(20,30); end endmodule
Simulator Output
Addr = 20 Data = 30