For input and inout arguments, the temporary variable is initialized with the value of the actual argument with the appropriate coercion. For output or inout arguments, the value of the temporary variable is assigned to the actual argument with the appropriate conversion. Arguments specified in SystemVerilog as input must not be modified by the foreign language code. The initial values of formal arguments specified in SystemVerilog as output are undetermined and implementation dependent.
Pass By Ref
For arguments passed by reference, a reference (a pointer) to the actual data object is passed. In the case of packed data, a reference to a canonical data object is passed. The actual argument is usually allocated by a caller. The caller can also pass a reference to an object already allocated somewhere else, for example, its own formal argument passed by reference. If an argument of type T is passed by reference, the formal argument shall be of type T*. Packed arrays are passed using a pointer to the appropriate canonical type definition, either svLogicVecVal* or svBitVecVal*.
Pass By Value
Only small values of formal input arguments are passed by value. Function results are also directly passed by value. The user needs to provide the C type equivalent to the SystemVerilog type of a formal argument if an argument is passed by value.
Passing String
The layout of SystemVerilog string objects is implementation dependent. However, when a string value is passed from SystemVerilog to C, implementations shall ensure that all characters in the string are laid out in memory per C string conventions, including a trailing null character present at the end of the C string.
initial begin str = " HELLO: This string is created in SystemVerilog \n" ;
string_sv2c(str);
end
endprogram
CODE: C_file.c #include "svdpi.h"
int string_sv2c(const char* str){
printf(" C: %s",str);
return 0;
}
RESULTS
C: HELLO: This string is created in SystemVerilog
Example: Passing String From C To Sv
From the Data type mapping table, a SystemVerilog "String" is mapped to "const char*" in C. In the Following example, string "HELLO: This string is created in C" is assigned to a string and passed as return value to function import "string_c2sv" and this import function is called in SystemVerilog.
CODE: SV_file.v program main;
string str;
import "DPI-C" contextfunctionstring string_c2sv();
initial begin str = string_c2sv();
$display(" SV: %s ",str);
end
endprogram
CODE: C_file.c #include "svdpi.h"
const char* string_c2sv(void) {
char* str;
str = " HELLO: This string is created in C ";
return str;
}