My objective is to pass an array from C to SV and print the array contents in SV, I have tried the following C program of converting some text file(data_file.txt) (given full source in the link below) to an array and trying to read back the array by using DPI calls in SystemVeilog(SV), in "C" I have passed the array values to my function(mydisplay) which is inside the main func.(please correct me if I am wrong here) also it seems the array values are not read back to the SV environment as I would expect what could be the reason, is there an efficient way to get back the array in SV?
c code:
void mydisplay(svOpenArrayHandle h) {
int *a;
a =(int*)svGetArrayPtr(h);
for( i=0;i<idx;i++) {
io_printf("C: values[%2zu]=0x%02x\n",i,values[i]);
a[i] = values[i];
}
}
sv code:
program automatic top;
int a[32000];
import "DPI-C" function void mydisplay(inout int h[]);
initial begin
mydisplay(a);
foreach(a[i]) $display("SV after DPI: a[%0d]=%0d",i,a[i]);
end
endprogram
source at EDAplayground
After some trials I've finally found the solution and able to pass the processed text data from C to SV by a small tweak, I just imported a function and called the exported function using already available context method in the SV-DPI as per LRM, also only used the user defined function and removed the main from native "C" lang., this way I was able to read back the values in the array values from C to SV, provided below the code samples, updated code at EDAPlayground
C code:
#include <stdio.h>
#include <stdlib.h> /* for strtol */
#include <string.h> /* for strchr */
#include <limits.h> /* for INT_MIN/INT_MAX */
#include <errno.h> /* for errno */
extern int dV(int r);
#define MAXL 50000
unsigned long xstrtoul (char *p, char **ep, int base);
int mydisplay()
{
int v[15];
int sd;
int d;
FILE *fp = fopen ("data_file.txt", "r");
char line[MAXL] = {0};
unsigned values[MAXL] = {0};
int base = 16;
size_t i, idx = 0;
if (!fp) { /* validate file open */
fprintf (stderr, "error: file open failen '%s'.\n", fp);
return 1;
}
/* read each line in file (up to MAXL chars per-line) */
while (fgets (line, MAXL, fp)) {
char *p = line;
char *ep = p;
char digits[3] = {0};
errno = 0;
/* convert each string of digits into number */
while (errno == 0) {
/* skip any non-digit characters */
if (!(p = strchr (p, 'x'))) break;
strncpy (digits, ++p, 2);
digits[2] = 0; /* nul-terminate */
/* convert string to number */
values[idx++] = (unsigned)xstrtoul (digits, &ep, base);
if (errno || idx == MAXL) { /* check for error */
fprintf (stderr, "warning: MAXL values reached.\n");
break;
}
p += 2;
}
}
if (fp != stdin) fclose (fp);
/* print results */
for (i = 0; i < idx; i++)
{
printf ("C values[%2zu] : 0x%02x\t", i, values[i]);
v[d]=values[i];
sd = dV(v[d]);
}
return(sd);
}
/** string to unsigned long with error checking */
unsigned long xstrtoul (char *p, char **ep, int base)
{
errno = 0;
unsigned long tmp = strtoul (p, ep, base);
/* Check for various possible errors */
if ((errno == ERANGE && (tmp == ULONG_MAX)) ||
(errno != 0 && tmp == 0)) {
perror ("strtoul");
exit (EXIT_FAILURE);
}
if (*ep == p) {
fprintf (stderr, "No digits were found\n");
exit (EXIT_FAILURE);
}
return tmp;
}
SV code:
program automatic top();
int res;
import "DPI" context mydisplay= function int mD();
export "DPI" dV =function mydisplay;
function int mydisplay(input int xyz);
$display ("SV after DPI: %0d\n",xyz);
return -1;
endfunction
initial begin
#5 res=mD();
$display("Finished reading values...\n");
end
endprogram