Adding a custom instruction in the RVI subset
Custom opcodes
In the existing instruction encoding, there are some opcodes available for custom instructions as shown in Figure 1.
Custom instructions in the RISC-V ISA
Figure 2 is a reminder base instructions format that can be found in the documentation.
Chapter 26 of the RISC-V specification [risd] includes several details about RISC-V extensions.
According to [risd], the base ISA is defined in a 30-bits space (bits 31-2 of the instruction).
A prefix refer to the bits to the right of an encoding space. In this case, the prefix of the standard base ISA is only 2-bits wide and usually equal to
11
(that is the reason why there is noinst[1:0]
in Figure 1).
Example of the M extension
Here is the instructions set of the M extension:
This extension is composed of 8 instructions in the R format (4 for multiplication, 4 for division). The opcode 0110011
is also used for some instructions of RVI :
Add and sub between registers:
add
,sub
“set if less than”:
slt
,sltu
Boolean arithmetics:
xor
,or
,and
Shift-related operations:
sll
,srl
,sra
RVM differs from RVI mainly due to different funct7
values. It is also interesting to see that neither RVI nor RVM uses custom
opcodes shown in Figure 1 (conclusion should be the same for other ratified extensions): it confirms that it can be used for custom instructions.
Example of the cryptography extension
The study is based on the latest release available at the time of writing (Scalar Cryptograph v1.0.0-rc6
, chapter 5 may be interesting for constant-time related projects).
There are several extensions in this specification:
Zbkb
for bit manipulationsZbkc
for carry-less multiplyZbkx
for crossbar permutationZknd
for AES decryptionZkne
for AES encryptionZknh
for hash functionsZksed
for SM4 block cipherZksh
for SM3 hash functionZkr
for entropy-related operations
A subset of instructions is shown in Figure 4.
In this case, two opcodes are used (0110011
and 0010011
):
The first is similar to the M extension.
The second one is used for others RVI instructions such as
addi
,xori
,slti
and so on.
As for the M extension, it mainly relies on funct7
and funct3
to create new instructions.