csystem-verilogsystem-verilog-dpi

SystemVerilog DPI-C pointers


I have a question about the DPI connection between SystemVerilog and C. Specifically, I have a C function that looks like:

unsigned short C_FUN(unsigned char* data)

and what I want to pass to it is a bit[7:0] my_darray[];

Which is the best way to do it? Thanks in advance.


Solution

  • A mixed packed/unpacked dynamic array is handled as an svOpenArrayHandle in the C layer of the DPI. You're going to have to create a wrapper function that converts from the SystemVerilog type to your type:

    #include "svdpi.h"
    
    unsigned short c_fun_wrapper(const svOpenArrayHandle a) {
      unsigned char* data;
      // convert 'a' to your type
      // ...
    
      // call your function
      return c_fun(data);
    }
    

    For more info on how to convert from have a look at the IEEE 1800-2012 standard, section 35 and annex H.

    What you basically have are some nice functions that you can use to operate on the array (defined in the svdpi.h file):

    int svLength(const svOpenArrayHandle h, int d);
    void *svGetArrElemPtr1(const svOpenArrayHandle, int indx1);
    

    You can use these functions to loop over all elements and fill an array of char that will be pointed to by data.