# The integer instruction set, exceptions and reset Sources: ARCH (NEC U10082EJ1V0UM00), HW (U10661EJ5V0UM00), STS (Sacred Tech Scroll, CPU chapter). Keys as in [start-here.md](start-here.md). Facts only; nothing is copied. ## Formats (DOC: ARCH 5.1; STS "Instruction Formats") | Format | Size | Fields | Used by | | --- | --- | --- | --- | | I | 16 | opcode(6) reg2(5) reg1(5) | register-register ALU, JMP, LDSR/STSR use II | | II | 16 | opcode(6) reg2(5) imm5 | immediate ALU, SETF, TRAP, RETI, HALT, LDSR, STSR, the bit-string group (opcode 011111, sub-op in imm5) | | III | 16 | 100 cond(4) disp9 | Bcond (NOP is the "never" condition) | | IV | 32 | opcode(6) disp26 | JR, JAL | | V | 32 | opcode(6) reg2 reg1 imm16 | MOVEA, ADDI, ORI, ANDI, XORI, MOVHI | | VI | 32 | opcode(6) reg2 reg1 disp16 | LD.x, ST.x, IN.x, OUT.x, CAXI | | VII | 32 | opcode 111110, reg2 reg1, sub-op(6) in the second halfword | the FPU group; on the NVC also XB, XH, REV, MPYHW | The opcode map (DOC: ARCH appendix C) leaves five 6-bit opcodes free (010110, 011011, 011110, 110010, 110110; read from the table's columns, to re-check against the scan's layout in rung 0), four bit-string sub-op rows and most of the format VII sub-ops. The NVC fills 010110 (CLI) and 011110 (SEI) and sub-ops 001000-001010 and 001100 (DOC: STS "Nintendo"). What the stock µPD70732 does with those codes: an **invalid instruction code** exception (FF90h, DOC: ARCH table 6-1) is the documented behaviour for undefined codes; whether every free slot traps on each part is U7. ## Instruction groups (DOC: ARCH 5.2-5.3, appendix B) - **Data movement:** MOV (reg and imm5), MOVEA (add a sign-extended imm16), MOVHI (add imm16 << 16). - **Integer ALU:** ADD, ADDI, SUB, CMP, MUL, MULU, DIV, DIVU (with r30 as the high word or remainder), AND/ANDI, OR/ORI, XOR/XORI, NOT, SHL, SHR, SAR (register or imm5 count), SETF (condition to 0/1). ANDI/ORI/XORI zero-extend imm16; ADDI/MOVEA sign-extend it (DOC: ARCH 5.3; to be confirmed instruction by instruction from the pages, rung 0). - **Branches:** Bcond with 16 conditions (BV, BC/BL, BZ/BE, BNH, BN, BR, BLT, BLE and their complements, NOP), JR, JAL (link in r31), JMP [reg1]. **No delay slots:** the pipeline is refilled on a taken branch (DOC: PGN; timing: taken Bcond 3 clocks, not taken 1, ARCH table 5-11). - **Memory:** LD.B/H/W (sign-extending), ST.B/H/W, IN.B/H/W (zero-extending, DOC: ARCH 5.3, to confirm), OUT.B/H/W; disp16 is signed. **Unaligned addresses are rounded down** (the low bits are masked, no exception; DOC: ARCH 3.2, quoted in PGN "Data alignment"; STS "Data Types"). - **CAXI** compare-and-exchange interlocked: a locked read-modify-write bus sequence (22 clocks 32-bit bus, 26 on a 16-bit bus; DOC: ARCH table 5-11). - **Control:** LDSR, STSR, TRAP imm5, RETI, HALT (waits for an interrupt), and on the NVC CLI/SEI (12 clocks each; DOC: STS "Nintendo"). ### Flags (PSW bits 0-3, DOC: ARCH 2.2.4) Z, S, OV, CY. Bit-for-bit flag rules per instruction come from ARCH 5.3's per-instruction pages (the "Flags" box on each). Corners to settle in rung 0 against the pages and Pizza Gauntlet: shift by 0 (CY cleared?), SAR/SHR by register count masked to 5 bits, DIV overflow (80000000h / -1), DIVU by zero (exception FF80h, current PC). ## PSW (DOC: ARCH 2.2.4) Bits: Z(0) S(1) OV(2) CY(3), FPR(4) FUD(5) FOV(6) FZD(7) FIV(8) FRO(9), ID(12) AE(13) EP(14) NP(15), I3-I0 (16-19, the interrupt level mask). Bits 10, 11 and 20-31 read zero (the EIPSW/FEPSW notes say they are fixed to 0). A change by LDSR "becomes valid immediately after execution of the LDSR instruction has been completed" (ARCH 2.2.4). ## Exceptions and interrupts (DOC: ARCH ch. 6, table 6-1; HW ch. 4) | Source | Code | Handler | Restore PC | | --- | --- | --- | --- | | Reset | FFF0 | FFFFFFF0 | (EIPC/FEPC undefined) | | NMI | FFD0 | FFFFFFD0 | next PC | | Duplexed exception (an exception while EP = 1) | first code low, second high in ECR | FFFFFFD0 | current PC | | Address trap (PC = ADTRE, PSW.AE) | FFC0 | FFFFFFC0 | current PC | | TRAP 10h-1Fh | FFBn | FFFFFFB0 | next PC | | TRAP 00h-0Fh | FFAn | FFFFFFA0 | next PC | | Invalid instruction code | FF90 | FFFFFF90 | current PC | | Zero division | FF80 | FFFFFF80 | current PC | | FIV, FZD, FOV, FRO | FF70, FF68, FF64, FF60 | **FFFFFF60** (one handler for the FPU) | current PC | | (FUD FF62, FPR FF61: never raised on the V810 family) | | | | | Maskable interrupt level n (0-15) | FEn0 | FFFFFEn0 | next PC | - Interrupts take precedence over exceptions (ARCH ch. 6 intro). A maskable request is accepted when its level >= the PSW's I field and NP, EP and ID are all 0 (ARCH 6.2; the exact comparison is the first thing rung 0 checks against the page). The level is given by four pins INTV0-INTV3 with INT (HW 2.3); the board drives them. - **Interruptible long instructions:** DIV/DIVU, every floating-point instruction and the bit-string instructions are aborted by an interrupt, and the restore PC is then the current PC, so they restart (table 6-2, note 3). The bit-string registers hold the resume state (ARCH 2.1, r26-r30), so a bit string continues where it stopped; DIV and the FPU restart from scratch (HYPOTHESIS for DIV/FPU: "aborted" read as "no state kept"; U8). - EIPC/EIPSW receive PC and PSW; with EP = 1 or for NMI, FEPC/FEPSW instead (ARCH 2.2.1-2). - The NVC's own codes used by the Virtual Boy (game pad FE00, timer FE10, game pak FE20, link FE30, VIP FE40) are maskable levels 0-4 (DOC: STS "Exception Handlers"). ## Reset (DOC: ARCH ch. 9, table 9-1) PC = FFFFFFF0h, PSW = 00008000h (NP set: no interrupt accepted until software clears it), ECR = 0000FFF0h, r0 = 0, everything else undefined. The board must give r1-r31 a defined value for determinism: zero, marked as a modelling choice (HYPOTHESIS: not the hardware's value), as other family cores do. STS "System Reset" lists what the Virtual Boy's own reset leaves (WRAM undefined, the VSU apparently zeroed). ## Address trap and debug (DOC: ARCH 2.2.8, ch. 8) ADTRE holds a PC value; with PSW.AE set, fetching that address raises FFC0h. The debug-support chapter is short (ARCH ch. 8): no on-chip breakpoint unit beyond this.