前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于QuestaSIM的SystemVerilog DPI使用流程(step by step)

基于QuestaSIM的SystemVerilog DPI使用流程(step by step)

作者头像
AsicWonder
发布2020-08-10 14:57:38
1.2K0
发布2020-08-10 14:57:38
举报

本文旨在通过一个小设计展示SystemVerilog Direct Programming Interface (DPI)的使用。这个小设计模拟了一个交通信号灯,我们将在GUI中查看代表交通信号灯信号的波形并观察Verilog函数C语言函数调用如何改变交通信号灯的颜色。

设计和Questa SIM仿真脚本存放在工具安装目录:

<install_dir>/examples/tutorials/systemverilog/dpi_basic

在执行上图中的脚本之前需要完成三件事:

1、创建一个文件夹,将上述文件复制到这个文件夹

2、设置QUESTA_HOME环境变量(也许你在当初安装的时候已经设置过了)

3、安装gcc-4.2.1-mingw32vc9编译器到Questa SIM的安装目录中

1 module test ();
 
3 typedef enum {RED, GREEN, YELLOW} traffic_signal;
 
5 traffic_signal light;
 
7 function void sv_GreenLight ();
8 begin
9    light = GREEN;
10 end
11 endfunction
 
13 function void sv_YellowLight ();
14 begin
15 light = YELLOW;
16 end
17 endfunction
 
19 function void sv_RedLight ();
20 begin
21 light = RED;
22 end
23 endfunction
 
25 task sv_WaitForRed ();
26 begin
27 #10;
28 end
29 endtask
 
31 export "DPI-C" function sv_YellowLight;
32 export "DPI-C" function sv_RedLight;
33 export "DPI-C" task sv_WaitForRed;
 
35 import "DPI-C" context task c_CarWaiting ();
 
37 initial
38 begin
39   #10 sv_GreenLight;
40   #10 c_CarWaiting;
41   #10 sv_GreenLight;
42 end
 
44 endmodule

先看一下上述的测试文件test.sv

line1line44给测试平台一个名字test,在其中执行各种仿真活动

line3line5定义一个枚举变量light,这是后续操作的对象

line7~line11、line13~line17和line19~line23分别定义了SystemVerilog function,前缀sv_指明这是SystemVerilog语言编写的function

line25~line29定义了SystemVerilog task,前缀sv_指明这是SystemVerilog语言编写的task。这个task内包含了延时,所以不能用function实现。

line31~line33是SystemVerilog DPI的关键,使用关键字“export”使指定的function或task对于C语言可见,并且其名称必须放在特殊的name space中。

line35"import"声明用于将C世界的代码导入到Verilog世界中

#include "dpi_types.h"
int c_CarWaiting()
{
    printf("There's a car waiting on theother side. \n");
       printf("Initiatechange sequence ...\n");
       sv_YellowLight();
       sv_WaitForRed();
       sv_RedLight();
       return 0;
}

在这里我们可以发现SystemVerilog DPI是以SystemVerilog为中心的当你希望让SystemVerilog中的function或task对C语言可见时,需要将其export到C语言世界。同样,如果你想让SystemVerilog代码看到并访问C语言世界中的某些内容,则需要将其import到SystemVerilog

在Windows平台下,我们之间双击这个windows.bat文件

vlib work
vlog test.sv -dpiheader dpi_types.h foreign.c
vopt +acc test -o opt_test
vsim -i opt_test -do "add wave light; view source"

然后执行

VSIM 3> run
# There's a car waiting on the other side.
# Initiate change sequence ...
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数字芯片实验室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档