Can i use systemverilog task or function to be called in main() in c program

i have RTL code of open source microprocessor written in systemVerilog , i want it to load data from file and send it over UART for example . but i can not do that in C code running on the microcontroller ( memory overflows )

so i was thinking that maybe DPI can help me by calling an exported .sv task in c main() that would load data from file and put it in Microcontroller memory.

so my question is :

  • is this can be done ? to call .sv task in main() of compiled C code with c compiler ,becuase all i see is exported tasks are used in c code in functions that would be later imported , and run everything using vsim , so i think it all exists for the sake of simulation by vsim not like what i want

can you please tell me is that possible or not ?
thank you

In reply to waleedOsama:

In your c program include svdpi.h header file.Prefix the extern keyword in front of all methods being exported to C.

funcs.c should look like:
include <stdio.h>
include “svdpi.h”
extern int sayHello();
void something() {
printf(“something\n”);
sayHello();
}

You have to use the below format in sv file----

export “DPI” function write; // Note – not a function prototype
// This SystemVerilog function could be called from C
function void write(int address, int data);
// Call C function
slave_write(address, data); // Arguments passed by copy
endfunction

Now you can use the sv task in your c program.
Example:

module data_ex2 ();
export "DPI-C" function Double;
import "DPI-C" context task doit(input int val);

function void Double(input int num_in, output int num_out);
  num_out = 2 * num_in;

endfunction

initial
begin
  doit(2);
  doit(5);
end
endmodule

In reply to Subhra Bera:

thanks for your reply

i was trying what you said ,but i see you made function calls in initial block , is this example intended to be ran using vsim ? if yes then this is not what i meant

i will make my question more clear

here is the .sv file
////////////////////////////////////////////////////////

module data_ex2 ();
export "DPI-C" function Double;
 
function void Double();
  $display("hello from sv");
 
endfunction
endmodule
////////////////////////////////////////////////////////

and now i want to make .c file , and run this task there , i generated the dpiheader.h file
and here is .c file
////////////////////////////////////////////////////////

#include <stdio.h>
#include "dpiheader.h"

extern void Double();

int main (){
	
Double();
return 0 ;
}

////////////////////////////////////////////////////////

and now i will compile it , i want to run the .c file and i am expecting it will print “hello from sv”
////////////////////////////////////////////////////////
gcc main.c
////////////////////////////////////////////////////////
and the output of this that linker says , no refrence for Double()


so that is exactly what i want , is this valid or DPI can not do that ?

thank you

In reply to waleedOsama:

The DPI specification is written with the intention of the SystemVerilog scheduling kernel being the main(). That means your SystemVerilog code must call an imported C routine first, then that C code can call an exported SystemVerilog routine. It is set up that way so that multiple SystemVerilog processes can call imported tasks, and those tasks can call exported tasks that block.

To get your main() application to start first requires tool specific support and you need to contact your tool vendor directly.

In reply to dave_59:

thank you mr. Dave