Skip to content

Latest commit

 

History

History
72 lines (65 loc) · 2.17 KB

nim_svdpi.org

File metadata and controls

72 lines (65 loc) · 2.17 KB

Step 1: Nim to libdpi.so

nim c --out:libdpi.so --app:lib libdpi.nim

Step 2: Compile SystemVerilog as usual

xrun -sv -64bit tb.sv

Files

tb.sv

program top;

  import "DPI-C" hello=task hello();

  initial begin
    hello();
  end

endprogram

libdpi.nim

proc hello() {.exportc, dynlib.} =
  echo "Hello from Nim!"

Datatype Translation

SystemVerilogCNim
bytecharbyte or char or uint8
intintcint or int32
longintlong longclonglong or int64
shortintshort intcshort or int16
realdoublecdouble or float64
shortrealfloatcfloat or float32
chandlevoid*pointer or ptr T
stringchar*cstring

Imports

Tasks

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

Context

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.

Exports

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