riscv-isa-sim的使用

本文详细介绍RISC-V指令集仿真器riscv-isa-sim的使用方法,包括C程序仿真过程、新指令仿真及添加流程,以及交互式debug模式的使用技巧。同时提供了在仿真过程中记录详细信息的方法,并分享了部分学习资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3.3 riscv-isa-sim的使用

主要资料来源:
https://github.com/riscv/riscv-isa-sim/tree/d48f107dba6a96fb827cb47fdf290261feadeb35

riscv-isa-sim是一个RISC-V指令的仿真器,riscv-isa-sim的正常运行需要依赖riscv-pk和riscv-fesvr工具。前面有对这些工具的说明。riscv-isa-sim编译后主要用到的工具是spike。其实spike不单单可以仿真RISC-V指令的,还能仿真Linux系统。我主要工作的方面是硬件,因此对于软件的使用没有过多的深入,大家可以在优快云中查找更多软件方面的使用方法,最后我会贴一些我找到的资料。

仿真C程序过程:
Compiling and Running a Simple C Program
Install spike (see Build Steps), riscv-gnu-toolchain, and riscv-pk.
Write a short C program and name it hello.c. Then, compile it into a RISC-V ELF binary named hello:

$ riscv64-unknown-elf-gcc -o hello hello.c

Now you can simulate the program atop the proxy kernel:

$ spike pk hello

仿真新指令过程(官方说法):
Simulating a New Instruction
Adding an instruction to the simulator requires two steps:
Describe the instruction’s functional behavior in the file riscv/insns/<new_instruction_name>.h. Examine other instructions in that directory as a starting point.
Add the opcode and opcode mask to riscv/opcodes.h. Alternatively, add it to the riscv-opcodes package, and it will do so for you:
$ cd ../riscv-opcodes $ vi opcodes // add a line for the new instruction $ make install
Rebuild the simulator.

添加新指令:
https://blog.youkuaiyun.com/qq_33096883/article/details/83476656

交互的debug模式:
Interactive Debug Mode
To invoke interactive debug mode, launch spike with -d:

$ spike -d pk hello

: reg 0 a0					//读core0 a0的值
: mem 0 2020					//读core0 地址2020的值
: until pc 0 2020           			//(stop when pc=2020)					
: until mem 2020 50a990     			//(stop when mem[2020]=50a990)
: while mem 2020 50a990				//当地址2020的值等于50a990
: r						//继续运行
: q						//退出

在spike的仿真过程中,大家可以对/riscv-tools/risc-isa-sim/riscv目录下的process.cc文件做一定的修改,第313行,添加以下的语句:

FILE *fp;
fp = fopen("./spike.log","a");	
fprintf(fp, "core %3d: 0x%016" PRIx64 " (0x%08" PRIx64 ") %s\n",
        id, state.pc, bits, disassembler->disassemble(insn).c_str());
fclose(fp);

这样就可以将spike和pk仿真过程中的信息一一记录下来,方便debug。不过每次重新仿真需要将旧的spike.log删除,不然会一直在后面增加打印,无法准确定位本次仿真的信息。

利用spike进行Linux系统的仿真比较复杂,而且不是我的主要研究方向,所以我没有花费过多的时间,没有完成一次完整的仿真,如果大家有兴趣可以通过以下的链接进行学习。

仿真Linux系统:
https://blog.youkuaiyun.com/l919898756/article/details/80987440
https://blog.youkuaiyun.com/l919898756/article/details/80987456
https://github.com/riscv/riscv-tools/tree/2faac3398af956cea9f1cbdc64b0575f807fc6a8#linuxman

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值