# The instruction set (MIPS III as the VR4300 implements it) Sources: UM chapter 3 (summary), chapter 16 (one page per instruction, with pseudo-code), Figure 16-1 "VR4300 Opcode Bit Encoding" (UM p.544-545), chapter 17 (FPU, see fpu.md); R4400 UM appendix A. Tier 2 (DOC) unless a line says otherwise. ## Decode (UM Figure 16-1, read 2026-11-25) Primary opcode, bits 31..26 (rows are bits 31..29, columns bits 28..26): | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | 0 | SPECIAL | REGIMM | J | JAL | BEQ | BNE | BLEZ | BGTZ | | 1 | ADDI | ADDIU | SLTI | SLTIU | ANDI | ORI | XORI | LUI | | 2 | COP0 | COP1 | COP2 | * | BEQL | BNEL | BLEZL | BGTZL | | 3 | DADDI e | DADDIU e | LDL e | LDR e | * | * | * | * | | 4 | LB | LH | LWL | LW | LBU | LHU | LWR | LWU e | | 5 | SB | SH | SWL | SW | SDL e | SDR e | SWR | CACHE | | 6 | LL | LWC1 | LWC2 | * | LLD e | LDC1 | LDC2 | LD e | | 7 | SC | SWC1 | SWC2 | * | SCD e | SDC1 | SDC2 | SD e | SPECIAL (bits 5..0): SLL, -, SRL, SRA, SLLV, -, SRLV, SRAV / JR, JALR, -, -, SYSCALL, BREAK, -, SYNC / MFHI, MTHI, MFLO, MTLO, DSLLV, -, DSRLV, DSRAV / MULT, MULTU, DIV, DIVU, DMULT, DMULTU, DDIV, DDIVU / ADD, ADDU, SUB, SUBU, AND, OR, XOR, NOR / -, -, SLT, SLTU, DADD, DADDU, DSUB, DSUBU / TGE, TGEU, TLT, TLTU, TEQ, -, TNE, - / DSLL, -, DSRL, DSRA, DSLL32, -, DSRL32, DSRA32. REGIMM (bits 20..16): BLTZ, BGEZ, BLTZL, BGEZL / TGEI, TGEIU, TLTI, TLTIU, TEQI, -, TNEI / BLTZAL, BGEZAL, BLTZALL, BGEZALL. COPz `rs` (bits 25..21): MF, DMF, CF, -, MT, DMT, CT, - / BC / CO (bit 25 set: a coprocessor operation). COPz BC `rt`: BCF, BCT, BCFL, BCTL. CP0 function (bits 5..0, with CO set): TLBR 0x01, TLBWI 0x02, TLBWR 0x06, TLBP 0x08, ERET 0x18. The figure marks the other CP0 functions in two ways, and the difference matters to a decoder: - **phi**: "invalid but do not cause reserved instruction exceptions" (most of the table); - **xi** at function 0x10: "cause a reserved instruction exception on only VR4300". Key: `*` and gamma raise the reserved instruction exception; `e` (64-bit operations) are valid in 64-bit mode and in 32-bit **kernel** mode, and raise the reserved instruction exception in 32-bit user or supervisor mode; CACHE is marked "valid only for VR4000 processors with CP0 enabled". The decode table is the first thing the build generates from data (as sh2-python's `_decode.py` is generated from its manual's table A.50), so the generator's input is this figure transcribed once, by hand, with the page cited, and the JIT proposal's "semantics in a restricted form" can start here (handoff-brief, Open decision 5). ## Families and their corners - **Arithmetic** ADD/ADDI/SUB/DADD/DADDI/DSUB trap on signed overflow (Integer Overflow exception, register unchanged); the U forms do not. 32-bit results are sign-extended to 64 bits. - **Shifts** SLL/SRL/SRA/SLLV/SRLV/SRAV operate on 32 bits and sign-extend; the 64-bit D-forms and the +32 forms. **SRA/SRAV deviate on the N64** (start-here.md, item 1; n64brew: VR4300 > 32-bit Shift Right Arithmetic Bug; tier 3 OBSERVED). - **Multiply and divide** write HI/LO and stall the pipeline: MULT/MULTU 5, DIV/DIVU 37, DMULT/DMULTU 8, DDIV/DDIVU 69 PCycles (UM Table 3-12, p.76). Divide by zero leaves an architecturally undefined HI/LO: the values real units produce are a test-ROM question (n64-systemtest and krom's DIV tests; U3). MULT/DIV with non-sign-extended inputs: start-here.md item 2. The R4000 HI/LO hazard (a MFHI/MFLO within two instructions of a following MULT/DIV) is documented for the R4000; whether the VR4300 interlocks it fully is to be read from UM chapter 16's notes and checked (U4). - **Loads and stores**: LB/LBU/LH/LHU/LW/LWU/LD and stores; the unaligned pairs LWL/LWR/LDL/LDR/SWL/SWR/SDL/SDR (byte-lane merges described per endianness in UM chapter 16); address error on misaligned natural loads; LL/SC/LLD/SCD with the LLbit (cleared by ERET; n64-systemtest tests them). The N64 runs **big-endian** (the BE pin; MAME's device is "VR4300 (big)"). - **Branches**: one delay slot for every jump and branch (UM 4.2); the "likely" forms (BEQL, BNEL, BLEZL, BGTZL, BLTZL, BGEZL, BLTZALL, BGEZALL, BC1FL, BC1TL) **nullify** the delay slot when not taken. The link forms write r31 even when not taken (BLTZAL...). An exception in a delay slot sets Cause.BD and EPC to the branch. - **Traps** TGE...TNE and the immediate forms; SYSCALL; BREAK (code field ignored by the CPU). - **SYNC** "is executed as a NOP on the VR4300" (UM p.524). - **CACHE** op: the 5-bit op field selects the cache (I, D) and the operation (index invalidate, index load tag, index store tag, create dirty exclusive, hit invalidate, hit writeback invalidate, fill, hit writeback); UM chapter 16 "CACHE" lists which exist on the VR4300 (it has no secondary cache) (see timing.md, caches). - **COP2 / LWC2 / SWC2 / LDC2 / SDC2**: the VR4300 has no CP2. With Status.CU2 clear they raise Coprocessor Unusable; with it set, what they do is a question (U5), which n64-systemtest's COP2 tests answer (tier 3: its author runs it on real units). - **Reserved instruction** and **coprocessor unusable** exceptions as in cp0-tlb.md. ## Readability contract for the handlers (from z80-python, sh2-python) One instruction family per module; one handler per decode row with the mnemonic and the manual's page in the docstring; the hardware reason as a comment on the line that encodes it; evidence tags (DOC, TEST ROM, OBSERVED, HYPOTHESIS) on every behaviour the manual does not settle. The deviations above are written as the hardware does it, with the manual's wording cited as the reason the line looks surprising.