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
** 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?
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 …;
** 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’
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.
The DPI exported function ‘encryption_inputs_1’ must be called from a context imported tf. A call from a non-context imported tf was detected. The caller’s scope is ciphertest_sv.
The nearest DPI import tf up the call chain is at line 34 of file …/code/10_7_aes_gcm/verilog/file.sv . this is my sv code: module ciphertest_sv;
import "DPI-C" function void sv_print_encryption_inputs(
input string key, input string iv, input string p, input string a
);
import "DPI-C" function void sv_print_encryption_outputs(
input string c, input string tag
);
import "DPI-C" function void sv_print_decryption_inputs(
input string key, input string iv, input string c, input string tag, input string a
);
import "DPI-C" function void sv_print_decryption_output(
input string p
);
import "DPI-C" function void sv_print_final_result(
input string msg
);
// Import the C function for running the ciphertest
import "DPI-C" function int ciphertest(input byte key[], input byte iv[], input byte p[], input byte a[], input byte c[],
input int np, input int na, input byte nt, output string r);
// Import the C function for running the test
import "DPI-C" function int run_test();
// Task to call the run_test function in C
task run_full_test;
int result;
result = run_test();
if (result == 0) begin
$display("Full test passed.");
end else begin
$display("Full test failed with error code: %0d", result);
end
endtask
initial begin
// Run the full test task
run_full_test();
end
// Define the DPI-C functions to print the received data
export "DPI-C" function encryption_inputs_1;
export "DPI-C" function encryption_outputs_1;
export "DPI-C" function decryption_inputs_1;
export "DPI-C" function decryption_outputs_1;
export "DPI-C" function final_result_1;
function void encryption_inputs_1(
input string key, input string iv, input string p, input string a
);
//sv_print_encryption_inputs(
// input string key, input string iv, input string p, input string a