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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 <NULL>. Then there is no need to use the sv methods.
@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.
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 They're described in the LRM (linked in the answer, available for free to download).
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.