PLI (Programming Language Interface) 是 Verilog 仿真器提供的接口,允许用户通过 C/C++ 代码扩展仿真器功能。VCS 支持多种 PLI 接口标准。让我详细介绍各个版本和使用方法。
PLI 1.0 (TF/ACC Routines)
PLI 1.0 包含两套例程:
TF (Task/Function) Routines : 用于创建系统任务和函数
ACC (Access) Routines : 用于访问设计数据结构
TF Routine 示例
C代码 (hello_tf.c):
c Copy code
#include "veriuser.h"
// 实际执行的函数
int hello_calltf() {
io_printf("Hello from PLI 1.0 TF!\n");
return 0;
}
// 注册函数
s_tfcell veriusertfs[] = {
{usertask, 0, 0, 0, hello_calltf, 0, "$hello"},
{0} // 终止符
};
Verilog代码 (test.v):
verilog Copy code
module test;
initial begin
$hello;
#10 $finish;
end
endmodule
ACC Routine 示例
C代码 (display_nets.c):
c Copy code
#include "acc_user.h"
int display_nets_calltf() {
handle module_handle, net_handle;
acc_initialize();
// 获取顶层模块
module_handle = acc_handle_tfarg(1);
// 遍历所有wire
net_handle = acc_handle_by_name("wire", module_handle);
while (net_handle) {
io_printf("Net: %s\n", acc_fetch_name(net_handle));
net_handle = acc_next_net(module_handle, net_handle);
}
acc_close();
return 0;
}
PLI 2.0 (VPI)
VPI (Verilog Procedural Interface) 是 PLI 2.0 的标准接口,提供了更统一和强大的功能。
VPI 示例
C代码 (monitor_vpi.c):
c Copy code
#include "vpi_user.h"
// 值变化回调函数
PLI_INT32 value_change_callback(p_cb_data cb_data) {
vpiHandle signal = (vpiHandle)cb_data->user_data;
s_vpi_value value;
value.format = vpiIntVal;
vpi_get_value(signal, &value);
vpi_printf("Signal %s changed to %d at time %d\n",
vpi_get_str(vpiName, signal),
value.value.integer,
(int)cb_data->time->low);
return 0;
}
// 系统任务实现
PLI_INT32 monitor_calltf(PLI_BYTE8 *user_data) {
vpiHandle systf_handle, arg_iterator, signal_handle;
s_vpi_time time;
s_vpi_value value;
s_cb_data cb_data;
// 获取参数
systf_handle = vpi_handle(vpiSysTfCall, NULL);
arg_iterator = vpi_iterate(vpiArgument, systf_handle);
signal_handle = vpi_scan(arg_iterator);
// 设置值变化回调
time.type = vpiSimTime;
cb_data.reason = cbValueChange;
cb_data.cb_rtn = value_change_callback;
cb_data.obj = signal_handle;
cb_data.time = &time;
cb_data.value = &value;
cb_data.user_data = (PLI_BYTE8*)signal_handle;
vpi_register_cb(&cb_data);
return 0;
}
// 注册函数
void monitor_register() {
s_vpi_systf_data tf_data;
tf_data.type = vpiSysTask;
tf_data.tfname = "$monitor_signal";
tf_data.calltf = monitor_calltf;
tf_data.compiletf = NULL;
tf_data.sizetf = NULL;
tf_data.user_data = NULL;
vpi_register_systf(&tf_data);
}
// VPI启动函数
void (*vlog_startup_routines[])() = {
monitor_register,
NULL
};
Verilog测试代码:
verilog Copy code
module test;
reg [7:0] data;
initial begin
$monitor_signal(data);
#10 data = 8'h55;
#10 data = 8'hAA;
#10 data = 8'h00;
#10 $finish;
end
endmodule
DPI (Direct Programming Interface)
DPI 是 SystemVerilog 标准的一部分,提供了更简单的 C/C++ 接口。
DPI 示例
C代码 (dpi_functions.c):
c Copy code
#include "svdpi.h"
// 导出的C函数
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
// 可以从SV调用的C函数
void print_message(const char* msg) {
printf("DPI Message: %s\n", msg);
}
// 导入的SV函数(在C中调用)
extern void sv_display(int value);
void process_data(int data) {
int result = data * 2;
sv_display(result); // 调用SV函数
}
SystemVerilog代码:
systemverilog Copy code
module test;
// 导入C函数
import "DPI-C" function int factorial(input int n);
import "DPI-C" function void print_message(input string msg);
import "DPI-C" function void process_data(input int data);
// 导出SV函数供C调用
export "DPI-C" function sv_display;
function void sv_display(input int value);
$display("SV Display: value = %d", value);
endfunction
initial begin
int result;
result = factorial(5);
$display("5! = %d", result);
print_message("Hello from DPI!");
process_data(42);
#10 $finish;
end
endmodule
VCS 编译和运行
使用 -P 选项编译 PLI 1.0/2.0
bash Copy code
# 编译 PLI 1.0 TF/ACC
vcs -P pli_tab.tab test.v -CFLAGS "-I$VCS_HOME/include" \
hello_tf.c display_nets.c -o simv
# pli_tab.tab 文件内容
$hello call=hello_calltf
$display_nets call=display_nets_calltf acc+=rw:*
使用 VPI
bash Copy code
# 编译 VPI
vcs test.v monitor_vpi.c -CFLAGS "-I$VCS_HOME/include" \
-LDFLAGS "-shared" -o simv
使用 DPI
bash Copy code
# 编译 DPI
vcs -sverilog test.sv dpi_functions.c -o simv
# 或者先编译成共享库
gcc -fPIC -shared -o dpi_lib.so dpi_functions.c \
-I$VCS_HOME/include
vcs -sverilog test.sv -sv_lib dpi_lib -o simv
高级用法示例
混合使用多种接口
Makefile:
makefile Copy code
VCS = vcs
CFLAGS = -CFLAGS "-I$(VCS_HOME)/include -fPIC"
LDFLAGS = -LDFLAGS "-shared"
all: simv
simv: test.sv pli_funcs.c vpi_funcs.c dpi_funcs.c
$(VCS) -sverilog test.sv \
-P pli.tab \
pli_funcs.c \
vpi_funcs.c \
dpi_funcs.c \
$(CFLAGS) $(LDFLAGS) \
-o simv
run: simv
./simv
clean:
rm -rf simv* csrc *.daidir
调试 PLI 代码
bash Copy code
# 使用 GDB 调试
vcs -sverilog test.sv pli_code.c -CFLAGS "-g -O0" -o simv
gdb ./simv
# 在 VCS 中启用 PLI 调试信息
./simv +vpi_verbose +acc_verbose
这些例子展示了从 PLI 1.0 到 DPI 的演进,以及如何在 VCS 中使用各种接口。DPI 因其简单性和高效性,在现代设计中更受欢迎,但了解所有接口对于维护遗留代码和理解仿真器内部机制都很重要。