It's my first time to ask questions on stackoverflow and I have done some searching about this question. And if there is something I missed, please let me know.
I want to ask how do hardware recognize RVC instructions.
For example, I fetch a 32-bit instruction and want to decide whether the first 16-bit is a RVC instruction. My question is about LUI instruction. LUI begins with 20-bit immediate. Since immediate can be any number, may the first 16-bit immediate be a RVC instruction?
How can I know the first 16-bit is a RVC instruction or just a part of imm/func/.. or something?
I fetch a 32-bit instruction and want to decide whether the first 16-bit is a RCV instruction. My question is about LUI instruction. LUI begins with 20-bit immediate. Since immediate can be any number, may the first 16-bit immediate be a RVC instruction?
You're looking at the encoding from most significant bits down to least, but for RISC V, that is backwards. From an instruction set perspective, LUI
"begins" from the least significant bit and ends with the most significant bit. Bit numbering on RISC V has 0 for the LSB and 31 for the MSB (when 32 bits).
The RISC V instruction set is defined from a little endian perspective, so the lowest numbered byte in the instruction encodes the size of the instruction with a fairly simple scheme. Generally the first (lowest order) two bits specify either a 32-bit or 16-bit instruction, but other sizes like 48 and 64 and larger, are possible, and use additional bits.
In two bits, we can encode 4 different values: one is used for 32-bit instructions, two are used for 16-bit instructions (they're short so they need more encodings), and one for neither, which is used for the 48 bit or larger encodings (which then use more bits to indicate the actual length).