1

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.

Greg
  • 18,111
  • 5
  • 46
  • 68
arandomuser
  • 521
  • 1
  • 7
  • 22

1 Answers1

1

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.

Tudor Timi
  • 7,453
  • 1
  • 24
  • 53
  • It would be more efficient if you just used `byte unsigned ` on the SV side and `char` on the C side. The encode the length in the data or use a special terminating char like . Then there is no need to use the sv methods. – dave_59 Nov 23 '14 at 00:00
  • @Alessandro Just as a hint for StackOverflow, when writing a comment, you can address it using the `@` charachter. In your case, you can write `@dave_59` or just start writing `@dave` and a tooltip will pop up to select the full name. This way, the addressee gets an inbox message when you post a comment. – Tudor Timi Nov 26 '14 at 07:17
  • I have been looking for documentation on all these functions (svLength, svGetArrElemPtr1, etc) and others I can't find it. I just keep seeing forums threads mentioning them by passing but nothing "offical" or "complete" let's say. Are they part of the SystemVerilog standard? are they defined by the vendor? Can somebody help finding some documentation about them? – viterbi Jan 03 '20 at 10:15
  • @viterbi They're described in the LRM (linked in the answer, available for free to download). – Tudor Timi Jan 03 '20 at 12:07
  • thanks @TudorTimi I found it. I noticed that in my simulation writing an open array allocated in SV from my C code will not work unless I have written something to the array before in SV before calling the C function... I allocate the array with data = new[size]; and then call the C method "get_data(data)". In C, I get the pointer to the array with "p = (unsigned int*)svGetArrayPtr(data_handle);" and then write it with a mempcy(p, source, svSize(data_handle)*4);. The same code works fine if I have written something to the array first... So I am pretty sure the code is correct. – viterbi Jan 03 '20 at 17:02
  • FYI `svLength` was changed to `svSize` in the first IEEE 1800-2005 LRM. This was to correspond with `$size`. There was never a $length. – dave_59 May 14 '20 at 16:28