How to pass an unpacked array from SV to C in DPI export task

I want to implement a AHB write/read task in SV. It will be called from C.
So I’m using a DPI and export task.
Since data can be of any length,it has to be an array.I should be able to pass data for a write command from C to SV.
We cannot pass open arrays in export tasks,so I want to pass an array of lenght 16 i.e.,data[16] from C to SV.
How can I pass an unpacked array of type int from C to SV.
My .sv file has this:


export "DPI" task incr_write;
task incr_write(int addr,int unsigned data[16],int size,int len,output resp);
..........
endtask


My .c file has this

extern void incr_write(int,int ???,int,int,int*);

what should I be using in place of ??? in the above line

Thanks

The C header should be

int incr_write(
    int addr,
    const unsigned int* data,
    int size,
    int len,
    svLogic* resp);

The array of int’s will be passed in like in C; you get a pointer to the first element. It would be up to your C code to know there is only 16 elements.

A couple of notes about your task declaration:

  • You should be using “DPI-C” as “DPI” has been deprecated. There will eventually be -C++, -SC, -VHDL, etc.
  • An exported task has an int return value in C that is normally 0. An imported task should also return an int. See 35.9 Disabling DPI tasks and functions
  • You last argument is a single bit, not an int. I strongly recommend not relying on implicit types in SystemVerilog.

One last comment about passing open arrays to export tasks. If the potential array size is large, you can skip passing the array in the exported tasks, and instead retrieve the entire array or individual elements through an imported function.

import "DPI" context function void get_open_array(inout int data[]);
export "DPI-C" task incr_write;
task incr_write(int addr,int size,int len,output bit resp);
   int data[];
   data = new[size];
   get_open_array(data);
   ...
endtask

Can I ask, what about the read case, where the exported dpi must return an array?

How do I write the task? So far, I’ve tried something like the following example, but I C doesn’t see the value. Could be a scoping issue.

export "DPI-C" task incr_read;
task incr_read(input int addr, output int data[8], ...);

    data[0] = 0;
    data[1] = 1;
    data[2] = 2;