百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rv

> 编程语言
开源

32 位 RISC-V CPU,约 800 行 C89 代码

628 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

32 位 RISC-V CPU,约 800 行 C89 代码

rv

RISC-V CPU core written in ANSI C.

Features:

  • RV32IMAC_Zicsr implementation with M-mode and S-mode
  • Boots RISCV32 Linux
  • Passes all supported tests in riscv-tests
  • ~800 lines of code
  • Doesn't use any integer types larger than 32 bits, even for multiplication
  • Simple API (two required functions, plus one memory callback function that you provide)
  • No memory allocations

API

c
/* Memory access callback: data is input/output, return RV_BAD on fault. */
typedef rv_res (*rv_bus_cb)(void *user, rv_u32 addr, rv_u8 *data, rv_u32 is_store, rv_u32 width);

/* Initialize CPU. You can call this again on `cpu` to reset it. */
void rv_init(rv *cpu, void *user, rv_bus_cb bus_cb);

/* Single-step CPU. Returns RV_E* on exception. */
rv_u32 rv_step(rv *cpu);

Usage

…

See tools/example/example.c.

Running Linux

This repository contains a machine emulator that can use rv to boot Linux. See tools/linux/README.md.

Targeting rv

Use riscv-gnu-toolchain with tools/link.ld.

Suggested GCC commandline:

riscv64-unknown-elf-gcc example.S -nostdlib -nostartfiles -Tlink.ld -march=rv32imac -mabi=ilp32 -o example.o -e _start -g -no-pie

To dump a binary starting at 0x80000000 that can be directly loaded by rv as in the above example:

riscv64-unknown-elf-objcopy -g -O binary example.o example.bin

Instruction List

Click an instruction to see its implementation in rv.c.

  • add addi amoadd.w amoand.w amomax.w amomaxu.w amomin.w amominu.w
  • amoor.w amoswap.w amoxor.w and andi auipc beq bge
  • bgtu blt bltu bne c.add c.addi c.addi16spc.and
  • c.andi c.beqz c.bnez c.ebreak c.j c.jal c.jalr c.jr
  • c.li c.lui c.lw c.lwsp c.mv c.or c.slli c.srai
  • c.srli c.sub c.sw c.swsp c.xor csrrc csrrci csrrs
  • csrrsi csrrw csrrwi div divu ebreak ecall fence
  • fence.i jal jalr lb lbu lh lhu lr.w
  • lui lw mret mul mulh mulhsu mulhu or
  • ori rem remu sb sc.w sfence.vmash sll
  • slli slt slti sltiu sltu sra srai sret
  • srl srli sub sw wfi xor xori

FAQ

Spaghetti code!

  • rv was written in a way that takes maximal advantage of RISCV's instruction orthogonality.
  • rv also tries to strike a good balance between conciseness and readability.
  • Of course, being able to read this code at all requires intimate prior knowledge of the ISA encoding.

No switch statements!

  • C only allows constant expressions in switch statements. In addition to an abundance of break statements using these would result in more bloated code in the author's opinion. As it turns out, you are actually free to reimplement this code with switch statements. See LICENSE.txt.

Not useful!

  • Ok

Slow!

  • Ok

Caveats

  • Written in C89.
  • Not actually written in C89, since it uses external names longer than 6 characters.
  • Doesn't use any integer types larger than 32 bits, even for multiplication, because it's written in C89.
  • Assumes width of integer types in a way that's not completely compliant with C89/99. Fix for this is coming soon, I'm working on a watertight `` for C89.
  • Written in C89.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言