gpt4 book ai didi

c - 如何使用 verilog PLI 通过 ncverilog 编译器与 c 通信

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:53 24 4
gpt4 key购买 nike

我想用verilog来交流c。我发现 Verilog PLI 可以解决我的问题。我看这个网站学习http://www.asic-world.com/verilog/pli1.html#How_it_Works .但是我什至无法使用 printf 函数。我将 ncverilog 用于 verilog 编译器。我所做的是打击。我无法为此成功编译。它说找不到函数。谁能告诉我如何解决我的问题。谢谢 =)

C代码:hello.c

#include<stdio.h>
void hello(){
printf("HELLO");
}

创建库:

gcc hello.c -fPIC -shared -o hello.so

verilog代码:test.v

module test();
initial begin
$hello;
#10 $finish;
end
endmodule

verilog命令:

ncverilog test.v +access+r -v hello.so

最佳答案

VPI (PLI 2.0)

你好_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 需要一个额外的 bootstrap 方法,它返回一个类型 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 需要选项卡文件而不是 Bootstrap 方法。这在此处略有介绍: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. 直接编程接口(interface)。 PLI/VPI 包含在§ 36 中,但不包含模拟器特定要求。有关 DPI 的更多说明和教程,请查看 http://en.wikipedia.org/wiki/SystemVerilog_DPIhttp://www.doulos.com/knowhow/sysverilog/tutorial/dpi/

关于c - 如何使用 verilog PLI 通过 ncverilog 编译器与 c 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20044001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com