Whether DPI - C functions can be used in the environment where the top is in C

I have created a simple example for dpi functions. Below is the code:

CODE: C_file.c

#include "stdio.h" 
#include "svdpi.h" 
//int main()
//{
extern void export_func(void); 
void import_func() 
{ 
export_func(); 
} 
//}

CODE: SV_file.sv

module sv_file; 
export "DPI-C" function export_func; 
import "DPI-C" context function void import_func(); 
function void export_func(); 
$display("SV: Hello from SV "); 
endfunction 
initial 
begin 
import_func(); 
end 
endmodule

I am running the code with the command,
irun –sv sv_file.sv c_file.c

The dpi functions are working fine as expected.

But this code is without main() function. When I try running the code with main function, the sv code is not able to find the import_func definition in C.

  1. So I wanted to know whether dpi functions can be used in an environment where we have only C testcases instead without sv testcases (ie) C is in the top of the environment and execution starts and ends in C.
  2. And, I am not able to run the code when I am trying to use only the export function (ie) calling the C-defined function in SV. Is it possible to use only the export function without import?

Your help will be very useful for me.

Thanks,
Vimala

The SystemVerilog DPI requires that C code calling SV code must have originated from SV code that called the C code. This is because the SystemVerilog simulation must establish a context for the C code that includes timing and scope information that does not exist in C code. Calling C code from SV code establishes that context in the C code.

There are still a number of ways to control a test from C code. See the following links for more information:

Easy Steps Towards Virtual Prototyping using the SystemVerilog DPI

C Based Stimulus

In reply to dave_59:

Thank you so much for your inputs. It was very helpful for me.