nim c --out:libdpi.so --app:lib libdpi.nim
xrun -sv -64bit tb.sv
program top;
import "DPI-C" hello=task hello();
initial begin
hello();
end
endprogram
proc hello() {.exportc, dynlib.} =
echo "Hello from Nim!"
SystemVerilog | C | Nim |
---|---|---|
byte | char | byte or char or uint8 |
int | int | cint or int32 |
longint | long long | clonglong or int64 |
shortint | short int | cshort or int16 |
real | double | cdouble or float64 |
shortreal | float | cfloat or float32 |
chandle | void* | pointer or ptr T |
string | char* | cstring |
- A Nim proc mapped to a SV task needs to have an
cint
return type, and it should return a value of 0 (default) or 1.Failure to do so will give this error:
xmsim: *F,INVDIS: The import task import_task returned a value other than 1 or 0.
If an imported function/task is calling an exported function/task,
that import
declaration must contain the context
keyword.
Example:
export "DPI-C" function export_func;
import "DPI-C" context function void import_func_calling_export_func();
// ..
If that context
keyword is left you, you get an error like:
xmsim: *E,NONCONI: The C identifier "export_func" representing an export task/function cannot be executed from a non-context import task/function.
If a function or task name foo is exported from SystemVerilog to a C function (Nim proc), it needs to be declared in the Nim code as:
proc foo() {.importc.} # Assuming that foo has no arguments and has void return type