SystemVerilog includes DPI (Direct Programming Interface) which lets your SystemVerilog call C functions and can even let your C call SystemVerilog tasks/functions. Check out IEEE std 1800-2009 Section 35 and Annex H & I. There are limitations with data types so check out Annex H.7.4 that for basic SV/C type mapping.
To call C functions in SystemVerilog, simply import it into the desired scope (e.g. module or package)
import "DPI-C" context function C_function_name(/* args */);
To call SystemVerilog from C requires one extra step.
In SV :
export "DPI-C" function SV_function_name; /*no args */
In C :
extern return_type SV_function_name( /* args */);
Depending on your simulator you may need to compile the C code first and reference the object file, or just include the source file in your file list. You make need to add options to your simulator to, so check the manual.
Here are some resources that can help you get started:
Revision:
Use a translate wrapper since FILE does does not translate across DPI. C's const char*
maps to SystemVerilog's string
.
C:
#include <stdlib.h>
#include <stdio.h>
// include for DPI
#include "svdpi.h"
// wrapper
int C2SV_readmem(int z, const char *filename1, const char *filename2) {
FILE *file1;
FILE *file2;
int rtn;
file1 = fopen(filename1, "r");
file2 = fopen(filename2, "w");
if (file1 == NULL) {
printf("failed to open '%s' for read\n", filename1);
return 1;
}
if (file2 == NULL) {
printf("failed to open '%s' for write\n", filename2);
return 1;
}
return readmem(z, file1, file2); // call original readmem function
}
/* ... */
SystemVerilog:
module test;
import "DPI-C" context function int C2SV_readmem(input int z, input string filename1, input string filename2);
int value;
initial begin
value = C2SV_readmem( 25, "FileIn.txt", "FileOut.txt");
end
endmodule