Adding and using a new instruction

To add a new instruction to RISCV, we must add it to our compiler (riscv-gnu-toolchain for now) and whatever simulator/emulator we're using (spike for now).

First we follow the steps from Spike's setup page:

  1. Describe the instruction's functional behavior in the package riscv-isa-sim, in riscv-isa-sim/riscv/insns/.h.
    Examine other instructions in that directory as a starting point.
  2. Add the opcode and opcode mask to riscv-opcodes/opcodes. You can pick any open opcode, and there are several formats to choose from. Pick the type that fits your needs (source/destination registers, use of an immediate, etc.). riscv-opcodes/opcodes shows how two instructions, tagenforce and settag were inserted.
  3. Run make install in riscv-opcodes.
  4. This should have installed the opcode/masks necessary for your new instructions to all the proper header files.
    At the very least, check that a match and mask for your instruction have been generated and placed in
    riscv-isa-sim/riscv/encoding.hriscv-gnu-toolchain/binutils/include/opcode/riscv-opc.h, and riscv-gnu-toolchain/gcc/gcc/config/riscv/riscv-opc.h.
    If your instruction interacts with the spike proxy kernel, also check riscv-pk/pk/encoding.h.
  5. Finally, add structs for this instruction in the riscv_builtin_opcodes array in riscv-gnu-toolchain/binutils/opcodes/riscv-opc.h.
    The easiest way to do this is to find an existing instruction with the same instruction type as yours, and copy the formatting, replacing MATCH's and MASK's appropriately. As far as the strange letters signifying arguments go, this is currently how I understand them:
    d = uses rd
    s = uses rs1
    t = uses rs2
    j = uses immediate12 - i-type instructions (also ‘o’?)
    u = uses immediate20 - u-type instructions (shifted down)
    a = uses immediate20 - uj-type instructions
    p = uses immediate12 - sb-type instructions
    q = uses immediate12 - s-type instructions
    d = uses immediate20 - u-type instructions (unshifted)
    z = uses zero register (always loads 0, stores do nothing)

Now, we need to add support for decoding this instruction to Spike:
6. In riscv-isa-sim/spike_main/disasm.cc, add a DEFINE_$TYPE call for your instruction, replacing $ with the appropriate instruction format (this should match the instruction format you used in riscv-opcodes/opcodes).
For uj-type instructions, you'll have to add the instruction manually, as there is no DEFINE macro available.
7. Give it a try! Try compiling a small program and running it in spike with your new instruction
(declared via __asm__ or __asm__ volatile).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_19582165

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值