I am trying to import a C function into a System verilog test bench. The code for the C function is as shown below. I want to pass files as arguments. The function basically reads from one file and writes to another.
int readmem(int z, FILE *file1, FILE *file2) {
char data;
int x;
int i;
for(i = 0; i<z;i ++) {
data = fgetc(file1);
x = data;
fputc(x,file2);
}
return 0;
}
Kindly advise me on how I could call this function in a System verilog test bench.
You cannot pass file descriptors between SystemVerilog and C via the DPI, so I don't think it's possible to import the function directly as-is.
If all you really need to do is get the functionality in SystemVerilog, it will be easier to just port it to SystemVerilog rather than trying to import it via the DPI.
Something like this should work (not tested!):
Then from somewhere else:
The reason
data
is declared as 9 bits is to capture an EOF if the end of file is reached. Your original function could run past the end offile1
since you are not checking for EOF.