当前位置: 首页 > 工具软件 > Verilator > 使用案例 >

EDA开源仿真工具verilator入门7:使用verilator仿真玄铁

陈飞语
2023-12-01

之前的相关章节对verilator的相关功能和基本数据结构进行了介绍,本节将介绍如何使用verilator编译和仿真一个大一点的工程,将以阿里的玄铁为例来介绍,如何使用verilator对玄铁进行仿真。这里使用ubuntu20系统,且默认已经安装了verilator,如果还没有成功安装,可以参考安装与测试

首先,我们从git上拷贝玄铁代码到本地:

git clone https://github.com/kknet/openc910.git

玄铁已经提供了verilator配置相关的基本文件,我们只需要在此基础上搭建好配置文件,就可以完成编译和仿真,进入文件夹./openc90/smart_run文件夹,创建文件夹work并进入:

cd ./openc90/smart_run/
mkdir work
cd work

在使用verilator编译之前,需要配置需要编译的所有相关verilog文件以及仿真.cpp文件,对于verilog文件其对应的.fl文件为/openc910/smart_run/logical/filelists/sim_verilator.fl,其主要内容为:

-f ../logical/filelists/ip.fl
-f ../logical/filelists/smart.fl
-f ../logical/filelists/tb_verilator.fl

这里为了避免找不到,建议改为全路径,例如:

-f /home/s/shenzhou/xuantie_test/openc910/smart_run/logical/filelists/ip.fl
-f /home/s/shenzhou/xuantie_test/openc910/smart_run/logical/filelists/smart.fl
-f /home/s/shenzhou/xuantie_test/openc910/smart_run/logical/filelists/tb_verilator.fl

可以看到涉及3个相关的文件列表,ip.fl玄铁设计中使用的ip核相关文件,smart.fl是主框架相关的代码,tb_verilator.fl为测试代码。注意这3个文件里面也可能涉及路径配置找不到的问题,这里建议全部改为全路径

仿真的.cpp文件为/openc910/smart_run/logical/filelists/tb/sim_main1.cpp

注意,默认仿真的主代码sim_main1.cpp是没有什么打印信息的,这里添加打印信息来观察仿真的cycle数:

top->eval();
if ( (i % 1000) == 0) {
    std::cout << i << " cycles have run" << std::endl;
}
i++;

好了到这里我们已经配置了所有的相关文件,现在可以使用verilator进行编译了。

输入编译指令,对玄铁进行编译:

verilator --timescale 1ns/100fs -Os -x-assign 0 --threads 4 -Wno-fatal  --cc --exe --top-module top -f ../logical/filelists/sim_verilator.fl ../logical/tb/sim_main1.cpp

这里开了4个线程进行仿真,如果编译成功,当前目录会生成对应的obj_dir文件夹,里面会有完整的仿真相关的C++工程文件。将对应的Makefile文件(这里是仿真工程编译的Makefile)拷贝到当前work目录:

cp ../logical/tb/Makefile_obj .

执行编译指令:

make -C obj_dir -f ../Makefile_obj -j8

这里会根据Makefile编译刚才生成的C++文件,生成最终的可执行仿真程序,如果成功,obj_dir/ 文件夹下会生成Vtop,可执行仿真程序,运行该仿真程序:

./obj_dir/Vtop

打印效果如下:

-Info: /home/s/shenzhou/xuantie_test/openc910/smart_run/logical/tb/tb_verilator.v:371: $dumpvar ignored, as Verilated without --trace
	********* Init Program *********
	********* Wipe memory to 0 *********
	********* Read program *********
%Warning: inst.pat:0: $readmem file not found
%Warning: data.pat:0: $readmem file not found
	********* Load program to memory *********
0 cycles have run
1000 cycles have run
2000 cycles have run
3000 cycles have run
4000 cycles have run
5000 cycles have run
6000 cycles have run
7000 cycles have run
8000 cycles have run

先讲到这里,小伙伴们可以试一试,可以明显感受到verilator的仿真速度是明显要快很多的,基于verilator工具可以明显加快RISC相关设计开发的迭代速度。

 类似资料: