Overview
Opcode injection is used in an instruction stream generator to turn a fully randomized instruction word into a more useful test instruction. The technique injects a random valid instruction opcode while leaving the remaining instruction fields randomized. This creates a valid instruction form without fully constraining operands, immediates, or other fields.
The cited RISC-V verification work presents opcode injection as the first modification to a baseline generator that otherwise fully randomizes generated instructions. The modification is described as simple, generic, and effective, and as important for ensuring that many legal instructions are considered during testing.
Motivation
A fully random 32-bit word is most likely to be an illegal instruction because the state space of illegal encodings is significantly larger than the state space of legal instructions. Opcode injection addresses this by forcing the opcode portion of the randomized word to correspond to a valid instruction while keeping the rest of the encoding random.
Operation
The technique starts with a fully randomized 32-bit instruction word. A random valid opcode is then injected into the opcode field. The example in the evidence injects the ADDI opcode into a randomized word, producing a randomized ADDI instruction. The register and immediate fields remain randomized after the opcode is injected.
In the described generator algorithm, instruction generation proceeds by first creating a random 32-bit instruction word. With high probability, the generator injects a random valid opcode into that word. The cited pseudocode uses a 98% probability for opcode injection.
Relationship to field mutation
Opcode injection may be followed by field mutation. The cited approach applies random field mutation with a smaller probability after opcode injection. These mutation rules are based on the RISC-V instruction format and include inserting special immediate values such as MIN, -1, 0, 1, and MAX, changing register fields such as mutating RD to zero, making RD equal to RS1 or RS2, making RS1 match RS2, and changing the CSR selector field to a supported CSR.
In the example, after the ADDI opcode is injected, a mutation rule changes RD to match RS1, while the fields remain randomized subject to that equality constraint.
Use in verification
The technique appears in the context of a cross-level co-simulation setup for processor verification. The instruction stream generator is designed to generate an endless and unrestricted instruction stream, and opcode injection guides that random generation toward legal and interesting instruction cases. The cited work applies the overall approach to verification of a pipelined 32-bit industrial RISC-V TGF series core implemented in SpinalHDL.