Example of passing a static array of bytes from SystemVerilog to C via DPI

In reply to dnguyen82us:

Here is some code to get started. Refer to the LRM for all the DPI related functions available for array information. Also, most simulators will generate DPI headers for C/C++ code, so you can ensure that your function prototypes match.

sv_monitor.sv


import "DPI-C" function void dpi_monitor(byte data[]);

module sv_monitor();
  byte packet_data[2048];

  initial begin
      dpi_monitor(packet_data);
  end
endmodule

header.h

#ifndef INCLUDED_HEADER
#define INCLUDED_HEADER

#ifdef __cplusplus
#define DPI_LINK_DECL  extern "C" 
#else
#define DPI_LINK_DECL 
#endif

#include "svdpi.h"

DPI_LINK_DECL DPI_DLLESPEC
void
dpi_monitor(
    const svOpenArrayHandle data);

#endif

dpi.c

#include <stdio.h>
#include "header.h"

void dpi_monitor(const svOpenArrayHandle data) {
    int size;
    int dimensions;

    dimensions = svDimensions(data);

    printf("Data array has %d dimensions\n", dimensions);

    size = svSize(data, 1);
    
    printf("Data array has %d elements\n", size);
}