新ちゃん 发表于 2020-8-18 23:20:43

riscv-isa-sim的使用

本帖最后由 新ちゃん 于 2020-8-20 22:35 编辑

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系统。我主要工作的方面是硬件,因此对于软件的使用没有过多的深入,大家可以在CSDN中查找更多软件方面的使用方法,最后我会贴一些我找到的资料。
仿真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.
添加新指令:Rocket之添加指令
交互的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=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.csdn.net/l919898756/article/details/80987440
https://blog.csdn.net/l919898756/article/details/80987456
https://github.com/riscv/riscv-tools/tree/2faac3398af956cea9f1cbdc64b0575f807fc6a8#linuxman

本篇完,感谢关注:RISC-V单片机中文网
页: [1]
查看完整版本: riscv-isa-sim的使用