今天在 GitHub 上看到一個(gè) C 語(yǔ)言項(xiàng)目,用大約 600 行代碼實(shí)現(xiàn)了一個(gè) RISC-V CPU 核,甚為感嘆,分享一下。不管是學(xué)習(xí) C,還是學(xué)習(xí) RISC-V,這個(gè)項(xiàng)目都有非常高的學(xué)習(xí)價(jià)值,開源萬(wàn)歲!
rv
用 ANSI C 編寫的 RISC-V CPU 內(nèi)核。
特征:
- RV32IMC 用戶級(jí)實(shí)現(xiàn)
- 通過(guò) riscv 測(cè)試中所有支持的測(cè)試
- ~600 行代碼
- 不使用任何大于 32 位的整數(shù)類型,即使對(duì)于乘法也是如此
- 簡(jiǎn)單 API(兩個(gè)函數(shù),加上您提供的兩個(gè)內(nèi)存回調(diào)函數(shù))
- 無(wú)內(nèi)存分配
應(yīng)用程序接口
/* Memory access callbacks: data is input/output, return RV_BAD on fault, 0 otherwise */
typedef rv_res (*rv_store_cb)(void *user, rv_u32 addr, rv_u8 data);
typedef rv_res (*rv_load_cb)(void *user, rv_u32 addr, rv_u8 *data);
/* Initialize CPU. */
void rv_init(rv *cpu, void *user, rv_load_cb load_cb, rv_store_cb store_cb);
/* Single-step CPU. Returns 0 on success, one of RV_E* on exception. */
rv_u32 rv_step(rv *cpu);
用法
#include < stdio.h >
#include < string.h >
#include "rv.h"
rv_res load_cb(void *user, rv_u32 addr, rv_u8 *data) {
if (addr - 0x80000000 > 0x10000) /* Reset vector is 0x80000000 */
return RV_BAD;
*data = ((rv_u8 *)(user))[addr - 0x80000000];
return RV_OK;
}
rv_res store_cb(void *user, rv_u32 addr, rv_u8 data) {
if (addr - 0x80000000 > 0x10000)
return RV_BAD;
((rv_u8 *)(user))[addr - 0x80000000] = data;
return RV_OK;
}
rv_u32 program[2] = {
/* _start: */
0x02A88893, /* add a7, a7, 42 */
0x00000073 /* ecall */
};
int main(void) {
rv_u8 mem[0x10000];
rv cpu;
rv_init(&cpu, (void *)mem, &load_cb, &store_cb);
memcpy((void *)mem, (void *)program, sizeof(program));
while (rv_step(&cpu) != RV_EECALL) {
}
printf("Environment call @ %08X: %un", cpu.pc, cpu.r[17]);
return 0;
}
為rv
編譯程序
使用 riscv-gnu-toolchain工具鏈和 rv 鏈接腳本 。
建議使用gcc
命令行:
riscv64-unknown-elf-gcc example.S -nostdlib -nostartfiles -Tlink.ld -march=rv32imc -mabi=ilp32 -o example.o -e _start -g -no-pie
然后用 obj 工具將0x80000000
起始的二進(jìn)制代碼生成能被rv
加載的二進(jìn)制文件:
riscv64-unknown-elf-objcopy -g -O binary example.o example.bin
支持的指令列表
參見 支持指令列表。
-
內(nèi)核
+關(guān)注
關(guān)注
3文章
1382瀏覽量
40422 -
cpu
+關(guān)注
關(guān)注
68文章
10902瀏覽量
213013 -
ANSI
+關(guān)注
關(guān)注
0文章
28瀏覽量
20555 -
C代碼
+關(guān)注
關(guān)注
1文章
89瀏覽量
14356 -
RISC-V
+關(guān)注
關(guān)注
45文章
2322瀏覽量
46592
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
64位RISC-V CPU發(fā)展現(xiàn)狀和未來(lái)前景
瑞薩電子推出64位RISC-V CPU內(nèi)核RZ/Five通用MPU,開創(chuàng)RISC-V技術(shù)先河
![瑞薩電子推出64位<b class='flag-5'>RISC-V</b> <b class='flag-5'>CPU</b><b class='flag-5'>內(nèi)核</b>RZ/Five通用MPU,開創(chuàng)<b class='flag-5'>RISC-V</b>技術(shù)先河](https://file.elecfans.com/web2/M00/33/40/poYBAGIdtXiAKLJ-AA74O_RvuIA526.png)
瑞薩電子推出采用自研RISC-V CPU內(nèi)核的通用32位MCU
為什么選擇RISC-V?
RISC-V MCU開發(fā) (一):集成開發(fā)環(huán)境
如何實(shí)現(xiàn)一個(gè)RISC-V內(nèi)核架構(gòu)的芯片移植工作
RISC-V基礎(chǔ)知識(shí):模塊化開放式的ISA CISC和RISC代碼區(qū)別
RISC-V中國(guó)峰會(huì):RISC-V產(chǎn)業(yè)及生態(tài)發(fā)展
![<b class='flag-5'>RISC-V</b>中國(guó)峰會(huì):<b class='flag-5'>RISC-V</b>產(chǎn)業(yè)及生態(tài)發(fā)展](https://file.elecfans.com/web2/M00/02/93/poYBAGDRXViAUPHpAAFeVg2-3fU038.png)
第一屆RISC-V中國(guó)峰會(huì)看點(diǎn) risc-v開發(fā)要怎么優(yōu)化risc-v指令集架構(gòu)代碼密度
![第一屆<b class='flag-5'>RISC-V</b>中國(guó)峰會(huì)看點(diǎn) <b class='flag-5'>risc-v</b>開發(fā)要怎么優(yōu)化<b class='flag-5'>risc-v</b>指令集架構(gòu)<b class='flag-5'>代碼</b>密度](https://file.elecfans.com/web2/M00/02/DC/poYBAGDTEB6AIsGcABJPSIG4sfw082.png)
RISC-V MCU開發(fā) (六):代碼下載
![<b class='flag-5'>RISC-V</b> MCU開發(fā) (六):<b class='flag-5'>代碼</b>下載](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
RISC-V CPU調(diào)試機(jī)制的設(shè)計(jì)原理
TenstorrentInc首席CPU構(gòu)架師 練維漢:助力數(shù)字化升級(jí)的RISC-V AI 高性能CPU
![TenstorrentInc首席<b class='flag-5'>CPU</b>構(gòu)架師 練維漢:助力數(shù)字化升級(jí)的<b class='flag-5'>RISC-V</b> AI 高性能<b class='flag-5'>CPU</b>](https://file1.elecfans.com/web2/M00/A0/42/wKgZomTsHk2AaPQXAAgfg0W4suc814.png)
評(píng)論