
Import Methods
Methods implemented in C and given import declarations in SystemVerilog can be called from SystemVerilog, such methods are referred to as imported methods.Imported tasks or functions are similar to SystemVerilog tasks or functions.
Imported tasks or functions can have zero or more formal input, output, and inout arguments.
Imported tasks always return an int result as part of the DPI-C disable protocol and, thus, are declared in foreign code as int functions. We will discusses about the DPI-C disable protocol in following sections.
Imported functions can return a result or be defined as void functions.
The syntax import method:
import {"DPI" | "DPI-C"} [context | pure] [c_identifier =] [function|task] [function_identifier|task_identifier] ([tf_port_list]);
Steps To Write Import Metyhods
In SV Code
Step1 : Import the C function
import "DPI-C" string_sv2c=task string_sv2c();
Step2 : Invoke the Importted C function
initial
begin
string_sv2c();
end
In C code:
Step3: Define the Imported function
void string_sv2c(){
printf(" C: Hellow from C ");
}
Full Example:
CODE: SV_file
program main;
string str;
import "DPI-C" string_sv2c=task string_sv2c();
initial
begin
string_sv2c();
end
endprogram
CODE: C_file
#include "svdpi.h"
void string_sv2c(){
printf(" C: Hellow from C ");
}
RESULTS
C: Hellow from C
EXAMPLE: optional default arguments
Standard C Functions
Users can also call the standared C functions.
EXAMPLE:
import "DPI" function chandle malloc(int size);
import "DPI" function void free(chandle ptr);