Ibex RISC-V 固件开源项目与开发环境指南

前言

Ibex 是由 lowRISC 维护的开源 32 位 RISC-V CPU 核,采用 SystemVerilog 编写,具有高度可配置性,广泛应用于嵌入式控制领域。本文整理了基于 Ibex 核开发固件的相关开源项目与编译开发环境信息,为固件开发者提供参考。

一、Ibex 核心特性

Ibex 是一款生产级开源 RISC-V 处理器核,主要特性包括:

特性 说明
架构 32 位 RISC-V (RV32I/E)
ISA 扩展 M(乘除法)、C(压缩指令)、B(位操作)
流水线 2 级或 3 级流水线可选
调试支持 RISC-V Debug Spec
内存保护 PMP (Physical Memory Protection)
应用场景 嵌入式控制、IoT、OpenTitan 项目

二、开源固件项目

2.1 Simple System(官方示例)

仓库地址: lowRISC/ibex - examples/simple_system

Simple System 是 Ibex 官方提供的最小系统示例,集成在 Ibex 仓库中,专门用于仿真验证。

系统组件

  • Ibex Core
  • 单端口存储(指令与数据共用)
  • UART 外设(ASCII 输出)
  • 定时器外设(支持中断)
  • 仿真终止控制外设

软件示例

  • hello_test - Hello World 示例
  • benchmarks - 性能基准测试(Coremark 等)
1
2
3
4
5
6
# 编译 Hello World 示例
make -C examples/sw/simple_system/hello_test

# 运行仿真
./build/lowrisc_ibex_ibex_simple_system_0/sim-verilator/Vibex_simple_system \
-t --meminit=ram,examples/sw/simple_system/hello_test/hello_test.elf

2.2 Ibex Demo System

仓库地址: lowrisc/ibex-demo-system

Ibex Demo System 是功能更完整的 SoC 参考设计,支持 FPGA 部署和真实硬件调试。

系统组件

外设 功能说明
UART 串口通信
GPIO 通用输入输出
PWM 脉宽调制
Timer 定时器
SPI 串行外设接口
Debug Module RISC-V 调试规范接口

支持的 FPGA 开发板

  • Digilent Arty A7-35T(主力支持)
  • Digilent Arty A7-50T
  • Digilent Nexys A7-100T
  • NewAE Sonata
  • RealDigital Blackboard / Boolean
  • NewAE CW305-A100 / CW312T-XC7A35

软件架构

1
2
3
4
sw/
├── demo/ # 演示应用程序(C 语言)
├── common/ # 公共驱动与链接脚本
└── rust/ # Rust 语言支持

2.3 Ibex Demo System Labs

仓库地址: lowRISC/ibex-demo-system-labs

官方提供的配套实验教程,适合学习 Ibex 系统开发和 FPGA 实践。

实验列表

Lab 内容 技能点
Lab 1 FPGA 编程与外设交互 硬件部署、UART 通信
Lab 2 Verilator 仿真与波形调试 GTKWave、硬件修改
Lab 3 SPI LCD 显示屏驱动 SPI 协议、外设驱动
Lab 4 自定义指令扩展 RISC-V ISA 扩展

2.4 Rust 固件支持

Ibex Demo System 提供了实验性的 Rust 软件栈:

1
2
3
4
5
6
# 位置: sw/rust/

# 示例应用
demo/
├── hello_world/ # Hello World
└── led/ # LED 控制

Rust 开发者可使用 Cargo 构建工具链:

1
2
cargo build --bin led
cargo run --bin hello_world

三、编译开发环境

3.1 RISC-V GNU Toolchain

推荐使用riscv-collab/riscv-gnu-toolchain

Ibex 采用 rv32imc 架构(32 位,含 M、C 扩展),需要专用的交叉编译工具链。

预编译版本下载

从 GitHub releases 下载适合 Ubuntu 24.04 的工具链:

文件名 架构 C 库
riscv32-elf-ubuntu-24.04-gcc.tar.xz RV32 newlib
riscv32-glibc-ubuntu-24.04-gcc.tar.xz RV32 glibc

工具链安装

1
2
3
4
5
6
7
8
9
# 解压到 /opt 目录
sudo tar -xf riscv32-elf-ubuntu-24.04-gcc.tar.xz -C /opt

# 配置环境变量
export PATH=/opt/riscv32/bin:$PATH
export RISCV=/opt/riscv32

# 验证安装
riscv32-unknown-elf-gcc --version

从源码编译

1
2
3
4
5
6
7
8
git clone --recursive https://github.com/riscv-collab/riscv-gnu-toolchain
cd riscv-gnu-toolchain

# 配置为 RV32IMC 架构
./configure --prefix=$RISCV --with-arch=rv32imc --with-abi=ilp32

# 编译(首次需要约 6-8 GB 磁盘空间)
make -j$(nproc)

3.2 FuseSoC 构建系统

地址: FuseSoC

FuseSoC 是 FPGA/ASIC 设计的包管理和构建工具,Ibex 项目广泛使用。

FuseSoC 核心概念

  • core 文件:描述 IP 核的 RTL 文件、参数、依赖
  • cores-root:核心库搜索路径
  • run:执行构建/仿真

Ibex 命名配置

Ibex 提供预定义的配置方案,通过 util/ibex_config.py 脚本管理:

配置名 特性 适用场景
small RV32IMC, 2级流水线 资源受限
opentitan RV32IMC + 安全特性 OpenTitan 项目
maxperf RV32IMC, 3级流水线 最高性能

构建示例

1
2
3
4
5
6
7
8
9
# 使用 small 配置构建仿真
fusesoc --cores-root=. run --target=sim --setup --build \
lowrisc:ibex:ibex_simple_system \
$(./util/ibex_config.py small fusesoc_opts)

# 使用 maxperf-pmp-bmfull 配置
fusesoc --cores-root=. run --target=sim --setup --build \
lowrisc:ibex:ibex_simple_system \
$(./util/ibex_config.py maxperf-pmp-bmfull fusesoc_opts)

3.3 Verilator 仿真器

地址: verilator/verilator

Verilator 是开源的 SystemVerilog 仿真器,性能优异,适合 Ibex RTL 仿真。

安装

1
2
3
4
5
6
7
8
9
10
11
# Ubuntu/Debian
sudo apt-get install verilator

# 或从源码编译(获取最新版本)
git clone https://github.com/verilator/verilator
cd verilator
git checkout v5.022
autoconf
./configure
make -j$(nproc)
sudo make install

3.4 QEMU 模拟器

QEMU 原生支持 Ibex CPU 模型,可用于快速固件验证:

1
2
3
4
5
# 运行 Ibex 固件(RV32IMC)
qemu-system-riscv32 \
-nographic \
-machine lowrisc-ibex \
-kernel firmware.elf

Ibex CPU 模型特性

  • MISA: RV32IMCU
  • Priv Spec: 1.12
  • 支持 PMP
  • 不支持 MMU(Nommu)

3.5 OpenOCD + GDB 调试

OpenOCD 支持 RISC-V Debug Spec,可用于片上调试:

1
2
3
4
5
6
7
# 连接 OpenOCD
openocd -f interface/ftdi/digilent-hs2.cfg \
-f target/riscv.cfg

# GDB 连接调试
riscv32-unknown-elf-gdb firmware.elf
(gdb) target remote localhost:3333

四、开发流程示例

4.1 仿真流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
┌─────────────────────────────────────────────────────────────┐
│ Ibex 固件开发流程 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 编写固件 (C/汇编) │
│ └── examples/sw/simple_system/hello_test/main.c │
│ │
│ 2. 编译固件 │
│ └── make -C examples/sw/simple_system/hello_test │
│ │
│ 3. 构建仿真器 │
│ └── fusesoc --cores-root=. run --target=sim ... │
│ │
│ 4. 运行仿真 │
│ └── ./build/.../Vibex_simple_system -t --meminit=... │
│ │
│ 5. 调试(如需要) │
│ └── GDB + OpenOCD 或 GTKWave 查看波形 │
│ │
└─────────────────────────────────────────────────────────────┘

4.2 FPGA 部署流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 构建 FPGA bitstream
fusesoc --cores-root=. run --target=synth --build lowrisc:ibex:demo_system

# 2. 编程 FPGA
fusesoc --cores-root=. run --target=synth --run lowrisc:ibex:demo_system

# 或使用 OpenFPGALoader(更快)
openFPGALoader -b arty_a7-35t build/lowrisc_ibex_demo_system_0/fpga/...

# 3. 加载固件
util/load_demo_system.sh sw/demo/hello_world/hello_world.elf

# 4. 调试
openocd &
riscv32-unknown-elf-gdb sw/demo/hello_world/hello_world.elf
(gdb) target remote localhost:3333

五、固件编译参数

Ibex 固件编译使用 freestanding 模式,需要注意以下 GCC 选项:

参数 说明
-nostdlib 不链接标准库
-nostartfiles 不使用标准启动文件
-ffreestanding freestanding 编译环境
-march=rv32imc 目标 ISA
-mabi=ilp32 32 位整数 ABI

典型编译命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
riscv32-unknown-elf-gcc \
-nostartfiles \
-nostdlib \
-ffreestanding \
-march=rv32imc \
-mabi=ilp32 \
-I../common/include \
-c main.c -o main.o

riscv32-unknown-elf-gcc \
-T ../common/link.ld \
-nostartfiles \
-nostdlib \
-ffreestanding \
-march=rv32imc \
-mabi=ilp32 \
main.o -o firmware.elf

六、参考资料

资源 链接
Ibex 官方仓库 https://github.com/lowRISC/ibex
Ibex Demo System https://github.com/lowrisc/ibex-demo-system
Ibex Demo System Labs https://github.com/lowRISC/ibex-demo-system-labs
Ibex 官方文档 https://ibex-core.readthedocs.io
RISC-V GNU Toolchain https://github.com/riscv-collab/riscv-gnu-toolchain
FuseSoC https://github.com/olofk/fusesoc
Verilator https://verilator.github.io
OpenTitan 项目 https://github.com/lowRISC/opentitan
PULP RISC-V Debug https://github.com/pulp-platform/riscv-dbg

七、总结

本文整理了基于 Ibex RISC-V 核开发固件的主要资源:

  1. Simple System:适合快速验证固件功能的轻量级仿真环境
  2. Ibex Demo System:完整的 SoC 参考设计,支持 FPGA 部署
  3. Demo System Labs:配套学习教程,适合入门实践
  4. Rust 支持:实验性的 Rust 嵌入式开发栈

开发环境主要依赖 RISC-V GNU Toolchain + FuseSoC + Verilator,三者配合可完成从固件开发到 RTL 仿真的完整流程。如需硬件调试,可使用 OpenOCD + GDB 通过 JTAG 接口进行片上调试。


本文整理于 2026 年 8 月,如链接失效请访问对应仓库获取最新信息。