module real_test ();
// map the C function name to the Verilog function name
import "DPI-C" context get_real_c = function real get_real_v ();
import "DPI-C" context put_real_c = function void put_real_v (real x);
real pi;
initial
begin
$display("real_test");
pi = get_real_v(); // get a real number from the C code
$display("pi = %f", pi); // display it
put_real_v(pi); // send the real number back to C code
end
endmodule
#include "svdpi.h"
// since we are using math functions, include the math library
#include <math.h>
// can use io_printf() instead of printf() to put into ncsim.log file
double get_real_c() { // imported SV function
// exercise DPI function - get calling scope
printf("Function Calling HDL scope is %s \n", svGetNameFromScope(svGetScope() ) );
return 3.14159;
}
void put_real_c(double n) { // imported SV function
// exercise DPI function - get calling scope
printf("Function Calling HDL scope is %s \n", svGetNameFromScope(svGetScope() ) );
printf("pi + 5.0 = %f\n", n+5.0);
}
gcc -fPIC -g -shared -o libdpi.so real_dpi.c -I/`ncroot`/tools/include
irun -64 real_test.sv
or
ncvlog real_test.v -sv
ncelab -dpiheader dpi.h worklib.real_test:module
ncsim worklib.real_test:module -sv_lib libdpi.so -sv_root .