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:
- Describe the instruction's functional behavior in the package
riscv-isa-sim
, inriscv-isa-sim/riscv/insns/.h
.
Examine other instructions in that directory as a starting point. - 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
andsettag
were inserted. - Run
make install
inriscv-opcodes
. - 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 inriscv-isa-sim/riscv/encoding.h
,riscv-gnu-toolchain/binutils/include/opcode/riscv-opc.h
, andriscv-gnu-toolchain/gcc/gcc/config/riscv/riscv-opc.h
.
If your instruction interacts with thespike
proxy kernel, also checkriscv-pk/pk/encoding.h
. - Finally, add structs for this instruction in the
riscv_builtin_opcodes
array inriscv-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, replacingMATCH
's andMASK
's appropriately. As far as the strange letters signifying arguments go, this is currently how I understand them:d
= usesrd
s
= usesrs1
t
= usesrs2
j
= usesimmediate12
-i-type
instructions (also ‘o’?)u
= usesimmediate20
-u-type
instructions (shifted down)a
= usesimmediate20
-uj-type
instructionsp
= usesimmediate12
-sb-type
instructionsq
= usesimmediate12
-s-type
instructionsd
= usesimmediate20
-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
).
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?