DPI-C error from C to SV

I’m trying to call a systemverilog function from C. I’ve called the c-function from systemverilog and the response is going to be a call back from the C to the systemverilog. But I’m having some scoping issues. I put the:

export “DPI-c” context function export_task;

and the task declaration in the top level env class…ie

export “DPI-C” function export_task();

task export_task();
$display(“SV: hello from sv”);
endtask

class my_env extends uvm_env

endclass

** Error (suppressible): (vsim-3756) The DPI exported task ‘export_task’ must be called from a context imported tf. A call from a non-context imported tf was detected. The caller’s scope is test.

The nearest DPI import tf up the call chain is at line 196 of file …/src/env/myfile.sv

Time: 178716800 ps Iteration: 1 Region: /test/iMonitor::run_phase

** Fatal: (vsim-3757) The DPI exported task ‘export_task’ must be called from a context imported task. A call from a context imported function was detected. The caller’s scope is test.

The nearest DPI import tf up the call chain is at line 196 of file …/src/env/myfile.sv

I’m not sure what this error means, I’ve tried putting this export and task in various places, but I keep getting scoping errors. Where should I put the task and the export declaration?

In reply to mhibarger:

You haven’t shown you DPI-C import statement. To call a task from C needs to be called from an imported task, not a function. Remember, in SystemVerilog, tasks can call functions or tasks, but functions can only call functions.

If your export_task is really meant to be a non-time-consuming routine, then declare it a void function. Your import statement will still need to be imported with
import “DPI-C” context function void …;

Thanks Dave! I think that fixed it. Although I think the error message was cryptic.

In reply to mike_h:

I’m currently getting this warning message:

** Warning: (vsim-3770) Failed to find user specified function ‘c_encrypt_function’ in DPI precompiled library search list "/opt/tools/mentor/questasim/10.4_1/questasim/uvm-1.1d/linux_x86_64/uvm_dpi.so ".

followed by this:

** Fatal: (vsim-160) …/src/models/sim_models/encryption.sv(26): Null foreign function pointer encountered when calling ‘c_encrypt_function’

Time: 19353600 ps Iteration: 12 Process: /uvm_pkg::uvm_task_phase::execute/#FORK#137(#ublk#215181159#137)_7fefc98aeb8 File: …/src/models/sim_models/encryption.sv

Fatal error in Module test at …/src/models/sim_models/encryption.sv line 26

I’ve compiled the C and see the .o files in the ./work/_dpi directory. I’ve got this working in another bench, but the compilation and makefiles are a little different, but I don’t see any difference in the work dirs, it seems like the .o files are not being found by the SystemVerilog files and not being linked in correctly. Any help is appreciated. Thanks.