600行C编写RISC-V内核
https://github.com/mnurzia/rv用这个代码来学习RISC-V内核非常不错
https://www.armbbs.cn/static/image/filetype/zip.gif rv-main.zip (13.29 KB)
支持的指令:
https://img.anfulai.cn/dz/attachment/forum/202306/15/165231vl0l1f0lb3b3l7p5.png
#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));
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)) = data;
return RV_OK;
}
rv_u32 program = {
/* _start: */
0x02A88893, /* add a7, a7, 42 */
0x00000073/* ecall */
};
int main(void) {
rv_u8 mem;
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: %u\n", cpu.pc, cpu.r);
return 0;
}
页:
[1]