2、验证环境搭建:Linux环境配置、RISC-V工具链安装、仿真工具(Verilator/Icarus)安装、版本管理(Git)基础
说实话,做RISC-V验证,第一步不是写代码,而是把环境搭好。我见过太多人一上来就急着跑仿真,结果被工具链折腾得死去活来。嗯,咱们今天就把这事一次性说清楚。
2.1 Linux环境配置
我个人习惯用Ubuntu 20.04 LTS,稳定,社区支持好。你问为什么不用CentOS?说白了,RISC-V工具链对Ubuntu的兼容性更好,踩坑少。
装好系统后,第一件事就是更新包管理器:
sudo apt update
sudo apt upgrade -y
然后安装基础开发工具。我建议你一次性装全,省得后面缺这缺那:
sudo apt install -y build-essential git curl wget
sudo apt install -y autoconf automake libtool
sudo apt install -y flex bison texinfo
sudo apt install -y python3 python3-pip
sudo apt install -y libboost-all-dev
2.2 RISC-V工具链安装
RISC-V工具链是验证工作的核心。说白了,没有它,你写的RISC-V程序就是一堆废纸。
安装方式有两种:预编译二进制和源码编译。我建议新手用预编译的,省时间。
2.2.1 预编译工具链(推荐)
从SiFive官网下载预编译的RISC-V GNU工具链:
wget https://static.dev.sifive.com/dev-tools/riscv64-unknown-elf-gcc-2020.12.8-x86_64-linux-ubuntu14.tar.gz
tar -xzf riscv64-unknown-elf-gcc-2020.12.8-x86_64-linux-ubuntu14.tar.gz
sudo mv riscv64-unknown-elf-gcc-2020.12.8-x86_64-linux-ubuntu14 /opt/riscv
然后配置环境变量:
echo 'export PATH=$PATH:/opt/riscv/bin' >> ~/.bashrc
source ~/.bashrc
验证安装:
riscv64-unknown-elf-gcc --version
2.2.2 源码编译(进阶)
我曾经为了调试一个浮点指令的bug,不得不自己编译工具链。过程确实繁琐,但能让你对工具链有更深的理解。
git clone https://github.com/riscv-collab/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git submodule update --init --recursive
./configure --prefix=/opt/riscv --enable-multilib
make -j$(nproc)
2.3 仿真工具安装
仿真工具是验证工作的眼睛。我主要用两个:Verilator和Icarus Verilog。
2.3.1 Verilator安装
Verilator是高性能的Verilog仿真器,能把Verilog代码编译成C++。说白了,它跑得飞快,适合做大规模验证。
# 安装依赖
sudo apt install -y perl python3 make
sudo apt install -y g++ libfl-dev
# 下载源码
git clone https://github.com/verilator/verilator
cd verilator
git checkout v4.228
# 编译安装
autoconf
./configure
make -j$(nproc)
sudo make install
验证安装:
verilator --version
2.3.2 Icarus Verilog安装
Icarus Verilog(简称iverilog)是轻量级的仿真器,适合做小规模验证和快速原型。
sudo apt install -y iverilog
验证安装:
iverilog -V
简单测试一下:
// test.v
module test;
initial begin
$display("Hello, RISC-V!");
$finish;
end
endmodule
// 编译运行
iverilog -o test test.v
vvp test
2.4 版本管理(Git)基础
做验证,版本管理是保命符。我曾经有一次误删了整个验证环境,还好有Git,不然就真的凉了。
2.4.1 Git配置
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor vim
2.4.2 基本操作
我建议你养成这样的习惯:每天开始工作前先pull,下班前commit并push。
# 克隆仓库
git clone git@github.com:your-org/riscv-verification.git
# 创建分支
git checkout -b feature/add-csr-test
# 查看状态
git status
# 添加文件
git add test_cases/csr_test.sv
# 提交
git commit -m "Add CSR test cases"
# 推送
git push origin feature/add-csr-test
2.4.3 避坑指南
常用的分支策略:
- master:稳定版本,只合入经过review的代码
- develop:日常开发分支
- feature/*:新功能分支
- bugfix/*:bug修复分支
2.5 环境验证
装完所有工具后,我建议你跑一个完整的验证流程,确保环境没问题。
# 1. 编译RISC-V测试程序
riscv64-unknown-elf-gcc -O2 -o test.elf test.c
# 2. 反汇编查看
riscv64-unknown-elf-objdump -d test.elf
# 3. 用Verilator仿真
verilator --cc --exe --build testbench.cpp test.sv
# 4. 用Icarus仿真
iverilog -o testbench testbench.sv test.sv
vvp testbench
- riscv64-unknown-elf-gcc 能编译C程序
- verilator 能编译Verilog代码
- iverilog 能运行仿真
- git 能正常clone和push
2.6 知识体系总览
下面这张图是我自己整理的验证环境搭建知识体系,你照着这个框架来,基本不会漏东西:
环境搭建这事,说白了就是一次配置,长期受益。我建议你把这篇文档保存下来,以后换机器或者重装系统时,照着来一遍就行。
公众号:蓝海资料掘金营,微信deep3321