VPI (PLI 2.0)
hello_vpi.c:
#include<stdio.h>
#include <vpi_user.h>
void hello(){
printf("HELLO");
}
void register_hello()
{
s_vpi_systf_data data;
data.type = vpiSysTask; //vpiSysFunc;
// data.sysfunctype = vpiSysFuncInt; // return type if type is Func
data.tfname = "$hello";
data.calltf = hello;
data.compiletf = 0;
data.sizetf = 0;
data.user_data = 0;
vpi_register_systf(&data);
}
命令:
gcc hello_vpi.c -fPIC -shared -o hello_vpi.so
ncverilog test.v +access+r -loadvpi ./hello_vpi.so:register_hello
PLI 1.0 要求在s_tfcell veriusertfs[] 中定义verilog 中使用的所有C 方法。 Ncverilog 需要一个额外的引导方法,它返回一个类型p_tf_cell,这个方法定义了一个静态s_tfcell veriusertfs[]。这是一个例子:
veriuser_nc.c:
#include "veriuser.h"
#include "acc_user.h"
extern void hello();
#define user_task 1
#define user_function 2
p_tfcell my_boot() {
s_tfcell veriusertfs[] = {
/* {user_function/usertask, (int)data, pli_checkp, pli_sizep, pli, ?, verilog_name, ? } */
{usertask, 0, 0, 0, hello, 0, "$hello", 1},
{0} // last entry must be 0
};
return(veriusertfs);
}
命令:
gcc hello.c veriuser_nc.c -fPIC -shared -o hello_pli.so
ncverilog test.v +access+r -loadpli1 ./hello_pli.so:my_boot
VCS 需要一个选项卡文件而不是引导方法。此处略作介绍:http://www.asic-world.com/verilog/pli2.html#Linking_With_Simulator
另一种方法是使用 SystemVerilog DPI,它没有与 PLI/VPI 相同的包装/翻译层。
将#include "svdpi.h" 添加到hello.c 的头部。我只是更改共享对象名称以识别库类型(不是必需的)。 gcc hello.c -fPIC -shared -o hello_dpi.so
verilog 代码:test.sv
module test();
import "DPI-C" pure function void hello();
// DPI methods are treated as verilog task/function after import, including scope access
initial begin
hello(); // dpi doesn't have a $
#10 $finish;
end
endmodule
verilog 命令:
ncverilog -sv test.sv +access+r -sv_lib hello_dpi.so
有关 DPI 的文档位于 IEEE std 1800-2012 § 35。直接编程接口。 PLI/VPI 包含在第 36 节中,但不包括模拟器特定要求。有关 DPI 的更多说明和教程,请查看 http://en.wikipedia.org/wiki/SystemVerilog_DPI 和 http://www.doulos.com/knowhow/sysverilog/tutorial/dpi/