Query on DPI open Array of String

Dear Member,

I want to use DPI to connect system verilog and C.So I used open array of string in argument.

In System Verilog:
Ex. import “DPI-C” function void ABC(input string str);

So What should be in C program to connect this function? and How can I access the elements of Open Array in this example.

If you are using Questa, you should always use the vlog -dpiheader switch to generate a header file

vlog -dpiheader dpiheader.h hello.sv hello.c

and then in your DPI C file

#include "dpiheader.h"

This ensures that your SV task/function prototypes match your C routine prototypes. You will get a compile time error if they don’t instead of a run time segmentation fault or some other weird behavior.

The header your example creates is

void ABC(
    const svOpenArrayHandle str);

The you would use

void *svGetArrElemPtr1(const svOpenArrayHandle, int indx1);

To access each element of the array

In reply to dave_59:

Thank You Dave_59 for Quick Reply.

Finally successfully connect in DPI.Here is the Code :

SV:

import "DPI-C" function void fib_oa(input string data[]);
module automatic test;
  string d[20];
  int i;
  initial begin
//  foreach(d[i])
  d[0]="AA";
  d[1]="BB";
  d[2]="CC";
  fib_oa(d);
end
endmodule

C:

void fib_oa(const svOpenArrayHandle a) 
   {
        char *str1,*str2,*str3;
        {
               str1=*(char **)svGetArrElemPtr1(a,i);
               str2=*(char **)svGetArrElemPtr1(a,i+1);
               str3=*(char **)svGetArrElemPtr1(a,i+2);
               printf("string str1=%s\n str2=%s \n Str3=%s\n",str1,str2,str3);
        }
   }

In reply to kansagaratushar:

Good. FYI, there is no need for the foreach statement in the code that you showed. You never iterate over i. You just make 20 assignments to d[0]. And there is no need to declare the foreach iterator variable i even if you do wind up using it.