RISC-V 提供的开源工程里面提供了一个模拟器,解除了学习者、移植者对专用硬件的依赖。本文是对搭建RISC-V开源模拟器环境的简介。针对搭建过程中可能遇到的问题,提供解决建议。
1. 工具链
为了将源代码生成能在RISC-V上运行的程序,我们首先要生成工具链。
RISC-V 工具链官方资源: GitHub - riscv-collab/riscv-gnu-toolchain: GNU toolchain for RISC-V, including GCCGNU toolchain for RISC-V, including GCC. Contribute to riscv-collab/riscv-gnu-toolchain development by creating an account on GitHub.https://github.com/riscv-collab/riscv-gnu-toolchain
RISC-V 工具链国内镜像:
mirrors / riscv / riscv-gnu-toolchain · GitCodehttps://gitcode.net/mirrors/riscv/riscv-gnu-toolchain
提示: 工具链工程源文件接近7G,clone过程中容易发生失败。建议通过网盘下载,下载链接:https://pan.baidu.com/s/1KTckAhgQTcqStdgAK8yJ8g 提取码:p8my
1.1 预先准备
构建工具链依赖若干基础组件,在Ubuntu 上采用以下命令完成组件的安装。
$ sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
提示: 安装组件的过程可能会遇到依赖关系不满足的问题。遇到这种情况,建议每次安装一个组件的方式,找到依赖关系不满足组件。然后利用aptitude进行的处理。
针对上述 texinfo 安装失败的例子,aptitude的使用方法如下:
$ sudo apt-get install aptitude
$ sudo aptitude install texinfo
$ sudo apt-get install texinfo
1.2 构建工具链
在模拟器中,我们将使用到的 riscv64-unknown-elf-gcc 和 riscv64-unknown-linux-gnu-gcc. 各自对用的构建命令如下:
riscv64-unknown-elf-gcc
./configure --prefix=/opt/riscv
make
riscv64-unknown-linux-gnu-gcc
./configure --prefix=/opt/riscv --enable-multilib
make linux
提示: 工具链构建时间非常长,大约2小时。
1.3 配置工具链
将工具链的bin(我们这里是/opt/riscv/bin)加进$PATH,就可以正常使用了。
2. RISC-V 模拟器
riscv-isa-sim 是 RISC-V 的模拟器,它的构建依赖于其他riscv-tools。riscv-tools中也包含了riscv-isa-sim, 所以通过对riscv-tools的构建来完成对模拟器的构建。
RISC-V tools 官方资源
https://github.com/riscv-software-src/riscv-toolshttps://github.com/riscv-software-src/riscv-tools
RISC-V tools 国内镜像
mirrors / riscv / riscv-tools · GitCodehttps://gitcode.net/mirrors/riscv/riscv-tools
按照以下命令完成构建
# git submodule update --init --recursive
# export RISCV=/opt/riscv/
# ./build.sh
提示: /opt/riscv 是我们这个案例中的riscv-tools安装地址。
提示: 构建需要使用管理员账号。
3.测试
按照惯例,我们还是写一个Hello world来跑第一个程序。
$echo -e '#include \n int main(void) { printf("Helloworld!\\n"); return 0; }' > hello.c
build后,在spike(riscv-isa-sim)中运行,恭喜!