My system verification makes use of C function for some processing calculation. The C function needs to pass in a pointer of integer array, where the result will be calculated. Can you show me an example how it could be done? I want to print out the result calculated by my_lib_cal(). There's not much literature about how to implement pass by reference.
Here's how to C function looks like:
Code:
void my_lib_cal(int * out, int size)
Here's how I import the fnction intp my SV environment and execute them.
Code:
import "DPI-C" function void my_lib_cal(int out[9:0], int size);
initial begin
int result[9:0];
int cycle=24;
int (int i=0; i<cycle; i++) begin
my_lib_cal(result,10);
for (int j=0; j<10; j++) begin
$display("%d",result[j]);
end
end
end
Obviously you never tried to compile this code. If you use ModelSim/Questa, always use the -dpiheader switch to create the C prototypes needed for your C code.
Code:
module top;
import "DPI-C" function void my_lib_cal(output int out[9:0], input int size);
int result[9:0];
int cycle=24;
initial begin
for (int i=0; i<cycle; i++) begin
my_lib_cal(result,10);
for (int j=0; j<10; j++) begin
$display("%d",result[j]);
end
end
end
endmodule
the vlog -dpiheader switch will produce
Code:
DPI_LINK_DECL DPI_DLLESPEC
void
my_lib_cal(
int* out,
int size);
The C side thinks it is has an array of ints, but the SV side uses copy value semantics. Use an output or inout argument on the SV side.