目录
1 verilator介绍
verilator详细内容可以查看官方手册Overview — Verilator 5.003 documentation
1.1 简介
Verilator是一种开源的Verilog/SystemVerilog仿真器,可用于编译代码以及代码在线检查,Verilator能够读取Verilog或者SystemVerilog文件,并进行lint checks(基于lint工具的语法检测),并最终将其转换成C++的源文件.cpp和.h。
Verilator不直接将Verilog HDL转换为C++或者SystemC,反之Verilator将代码编译成更快的优化过的并且支持多线程的模型,该模型被依次包装在(wrapped)在C++/SystemC模型中。这样就生成一个编译的Verilog模型,其功能和Verilog是一致的,但效率由于基于C++即使是单线程模型也可以10倍快于SystemC,100倍快于基于解释Verilog的仿真器,并且通过多线程可以进一步加速。
1.2 安装
# 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
1.3 hello,world
安装好verilator后可以在文件目录下找到官方提供的example。以make_hello_c为例
top.v文件
module top;
initial begin
$display("Hello World!");
$finish;
end
endmodule
sim_main.cpp文件
#include <verilated.h>// verilator官方库
#include "Vtop.h"//top.v会被封装为头文件供c++调用
int main(int argc, char** argv, char** env) {
if (false && argc && argv && env) {}
Vtop* top = new Vtop;// 构建verilator模型,可以通过类型调用top中的参数
while (!Verilated::gotFinish()) {// 开始仿真直到$finish
top->eval();// Evaluate model
}
top->final();//结束仿真
delete top;// 清除模型
return 0;// Return good completion status
}
Makefile文件
ifeq ($(VERILATOR_ROOT),)
VERILATOR = verilator
else
export VERILATOR_ROOT
VERILATOR = $(VERILATOR_ROOT)/bin/verilator
endif
default:
@echo "-- Verilator hello-world simple example"
@echo "-- VERILATE & BUILD --------"
$(VERILATOR) -Wall -cc --exe --build -j top.v sim_main.cpp
@echo "-- RUN ---------------------"
obj_dir/Vtop
@echo "-- DONE --------------------"
@echo "Note: Once this example is understood, see examples/make_tracing_c."
@echo "Note: Also see the EXAMPLE section in the verilator manpage/document."
Makefile用于文件构建,主要的语句只有
$(VERILATOR) -cc --exe --build -j top.v sim_main.cpp
-Wall
:让verilator
执行强类型警告--cc<