一、官网介绍
1.官网:
2.介绍:
- Verilator:最快的 Verilog/SystemVerilog 模拟器
- 执行 lint 代码质量检查
- 接受可合成的 Verilog 或 SystemVerilog
- 编译成多线程 C + + 或 SystemC
- 创建 XML 用于自己的工具的前端
3.What Verilator Does
- 使用类似于 GCC 或 Synopsys 的 VCS 的参数调用 Verilator。它通过读取指定的 Verilog 或 SystemVerilog 代码,执行 lint 检查,并有选择地插入断言检查和覆盖率分析点来“ Verilog”指定的 Verilog 或 SystemVerilog 代码。它输出单线程或多线程.cpp 和.h 文件,“ Verilated”代码。
- 用户写一个小小的 c + +/SystemC 包装文件,它实例化用户顶级模块的“ Verilated”模型。这些 c + +/SystemC 文件然后由 c + + 编译器(gcc/clang/MSVC + +)编译。生成的可执行文件执行设计仿真。Verilator 还支持将其生成的库链接到其他模拟器(可选加密)。
- 如果你期待 NC-Verilog、 VCS 或另一个商业 Verilog 模拟器的全功能替代品,或者如果你正在寻找一个行为 Verilog 模拟器,比如一个快速类项目(我们推荐 Icarus Verilog) ,那么 veriator 可能不是最佳选择然而,如果您正在寻找将 SystemVerilog 迁移到 c + + 或 SystemC 的路径,或者您的团队仅仅编写一点 c + + 代码就已经很舒服了,Verilator 就是您的工具。
4.查阅资料
在线手册:Verilator User’s Guide — Verilator 4.223 documentation
5.工作流程
通常情况下,Verilator的工作流程如下所示:
- 首先Verilator将读取特定的HDL文件并检查其代码,同时还可以选择支持检查覆盖率和debug波形的生成。然后将源文件编译成源代码级的多线程(source level multithreaded) C++或SystemC模型。其输出的模型会以.cpp和.h文件存在。这个阶段的过程叫做"to Varilate",输出的文件叫做"Verilated Model"。
- 为了能够完成仿真,Verilator需要一个用户自行编写的C++ wrapper,这个wrapper与传统的Verilog Testbench 功能类似,主要是为了连接顶层模块,并给予相应的激励。
- 在C++ 编译器的工作下,所有的之前生成的文件(C++ wrapper以及Verilated Model)以及库文件(Verilator提供的runtime library或者SystemC库文件)会被一同合并成一个可执行文件。
- 执行生成的可执行文件,就可以开始实际的仿真,此时成为"simulation runtime"
- 最后如果在编译阶段将特定的编译选项打开,可执行文件也会生成波形和覆盖率等信息。
二、安装verilator
安装步骤在官网链接
操作系统:Ubuntu 21.04
# Prerequisites:
#sudo apt-get install git perl python3 make autoconf g++ flex bison ccache
#sudo apt-get install libgoogle-perftools-dev numactl perl-doc
#sudo apt-get install libfl2 # Ubuntu only (ignore if gives error)
#sudo apt-get install libfl-dev # Ubuntu only (ignore if gives error)
#sudo apt-get install zlibc zlib1g zlib1g-dev # Ubuntu only (ignore if gives error)
git clone https://github.com/verilator/verilator # Only first time
# Every time you need to build:
unsetenv VERILATOR_ROOT # For csh; ignore error if on bash
unset VERILATOR_ROOT # For bash
cd verilator
git pull # Make sure git repository is up-to-date
git tag # See what versions exist
#git checkout master # Use development branch (e.g. recent bug fixes)
#git checkout stable # Use most recent stable release
#git checkout v{version} # Switch to specified release version
autoconf # Create ./configure script
./configure # Configure and create Makefile
make -j `nproc` # Build Verilator itself (if error, try just 'make')
sudo make install
最后键入:verilator --version
出现下图,说明搭建成功。
三、运行官方demo
demo链接:C++ example
按照手册做就好。改动一个地方
cd verilator
git pull # Make sure we're up-to-date
git tag # See what versions exist
#git checkout master # Use development branch (e.g. recent bug fix)
#git checkout stable # Use most recent release
git checkout v4.210 # Switch to specified release version
这里有一个:cat和<<的配合使用
官方给的demo:
our.v
module our;
initial begin
$display("Hello World");
$finish;
end
endmodule
sim_main.cpp---------testbench
#include "Vour.h"
#include "verilated.h"
int main(int argc, char** argv, char** env)
{
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
Vour* top = new Vour{contextp};
while (!contextp->gotFinish())
{
top->eval();
}
delete top;
delete contextp;
return 0;
}
执行:
verilator -Wall --cc --exe --build sim_main.cpp our.v
当出现这个,说明官方demo跑通了。
四、gtkwave使用
1.安装gtkwave
sudo apt-get install gtkwave
#gtkwave --version
如果出现下面报错:缺少canberra-gtk-module,apt安装即可
2.示例
以双控开关为例:
top.v
module top(
input a,
input b,
output f
);
assign f = a ^ b;
endmodule
top_main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Vtop.h" // create `top.v`,so use `Vtop.h`
#include "verilated.h"
#include "verilated_vcd_c.h" //可选,如果要导出vcd则需要加上
int main(int argc, char** argv, char** env) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
Vtop* top = new Vtop{contextp};
VerilatedVcdC* tfp = new VerilatedVcdC; //初始化VCD对象指针
contextp->traceEverOn(true); //打开追踪功能
top->trace(tfp, 0); //
tfp->open("wave.vcd"); //设置输出的文件wave.vcd
while (!contextp->gotFinish()) {
int a = rand() & 1;
int b = rand() & 1;
top->a = a;
top->b = b;
top->eval();
printf("a = %d, b = %d, f = %d\n", a, b, top->f);
tfp->dump(contextp->time()); //dump wave
contextp->timeInc(1); //推动仿真时间
assert(top->f == a ^ b);
}
delete top;
tfp->close();
delete contextp;
return 0;
}
verilator -Wall top.v top_main.cpp --cc --trace --exe --build
#增加了--trace 是为了显示波形的
./obj_dir/Vtop //必须执行这个,才会出现.vcd文件
gtkwave wave.vcd //如果报错缺少canberra-gtk-module,apt安装即可