Verilator 环境搭建

声明

这篇博客仅用于本人日后参考,如果 ysyx 的同学找到了这篇博客,请勿直接使用该博客作为安装步骤。

Verilator 的安装

这里通过 Github 方式安装 Verilator.

cd ysyx-workbench/
bash init.sh npc
source ~/.bashrc

# 依赖
sudo apt-get install git help2man 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
cd verilator
git checkout v5.008

unset VERILATOR_ROOT
autoconf
./configure
make -j `nproc`
sudo make install

echo $VERILATOR_ROOT # 应为空
export VERILATOR_ROOT=/home/ubuntu/verilator
export PATH=$VERILATOR_ROOT/bin:$PATH

测试(https://verilator.org/guide/latest/example_cc.html):

mkdir test_our
cd test_our
cat >our.v <<'EOF'
  module our;
     initial begin $display("Hello World"); $finish; end
  endmodule
EOF

cat >sim_main.cpp <<'EOF'
  #include "Vour.h"
  #include "verilated.h"
  int main(int argc, char** argv) {
      VerilatedContext* contextp = new VerilatedContext;
      contextp->commandArgs(argc, argv);
      Vour* top = new Vour{contextp};
      while (!contextp->gotFinish()) { top->eval(); }
      delete top;
      delete contextp;
      return 0;
  }
EOF

verilator --cc --exe --build -j 0 -Wall sim_main.cpp our.v
obj_dir/Vour

应得到如下结果:

Hello World
- our.v:2: Verilog $finish

System C 的安装

参考链接:Github.

wget https://github.com/accellera-official/systemc/archive/refs/tags/3.0.1.tar.gz
tar -xvzf systemc-3.0.1.tar.gz
cd systemc-3.0.1
mkdir objdir
cd objdir
export CXX=g++
export CXXFLAGS=-std=c++17 # 较新版本需要大于17的c++
../configure
make install
echo "# SystemC Install path" >> ~/.bashrc
echo "export SYSTEMC_HOME=$HOME/systemc-3.0.1" >> ~/.bashrc # 根据安装路径更改
echo "export LD_LIBRARY_PATH=$SYSTEMC_HOME/lib-linux64" >> ~/.bashrc 
source ~/.bashrc

测试(https://verilator.org/guide/latest/example_sc.html):

mkdir test_our_sc
cd test_our_sc

cat >our.v <<'EOF'
  module our (clk);
     input clk;  // Clock is required to get initial activation
     always @(posedge clk)
        begin $display("Hello World"); $finish; end
  endmodule
EOF

cat >sc_main.cpp <<'EOF'
  #include "Vour.h"
  using namespace sc_core;
  int sc_main(int argc, char** argv) {
      Verilated::commandArgs(argc, argv);
      sc_clock clk{"clk", 10, SC_NS, 0.5, 3, SC_NS, true};
      Vour* top = new Vour{"top"};
      top->clk(clk);
      while (!Verilated::gotFinish()) { sc_start(1, SC_NS); }
      delete top;
      return 0;
  }
EOF

verilator --sc --exe -Wall sc_main.cpp our.v

执行到这里,遇到第一个报错:

%Error: Need $SYSTEMC_INCLUDE in environment or when Verilator configured,
and need $SYSTEMC_LIBDIR in environment or when Verilator configured
Probably System-C isn't installed, see http://www.systemc.org
        ... See the manual at https://verilator.org/verilator_doc.html?v=5.036 for more assistance.

显然通过先前的安装方法并没有将对应的环境变量正确地设置,手动补足后重新运行:

export SYSTEMC_INCLUDE=$SYSTEMC_HOME/include
export SYSTEMC_LIBDIR=$SYSTEMC_HOME/lib-linux64/
verilator --sc --exe -Wall sc_main.cpp our.v
make -j -C obj_dir -f Vour.mk Vour

这里会遇到第二个报错,检查可以发现依然是因为我们安装了较新版本的 System C 导致的,因此需要手动指定 C++ 标准。经查阅, verilator 指令中的 -CFLAGS <c-flags> 参数可以指定 GCC 的编译选项,因此我们修改上面的指令并重新执行得到:

verilator --sc --exe -Wall -CFLAGS -std=c++17 sc_main.cpp our.v
make -j -C obj_dir -f Vour.mk Vour -lCXXFLAGSstd=c++17
obj_dir/Vour

应得到以下结果:

SystemC 3.0.1-Accellera --- May  5 2025 20:29:56
        Copyright (c) 1996-2024 by all Contributors,
        ALL RIGHTS RESERVED
Hello World
- our.v:4: Verilog $finish

至此便通过了安装测试。

dark
sans