DPI import C function pass by reference to int array

Status
Not open for further replies.

marco5polo

Newbie level 1
Joined
Jul 17, 2014
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
9
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
 
Last edited by a moderator:

So, did you mean your code doesn't work? The array in c side can be definedas svOpenArrayHandle, and RTL side should be defined as inout
 

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.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…