# The two instruction sets, opcode by opcode Both processors run 16-bit instructions: opcode bits 15-10, **reg1** (the source, or a 5-bit immediate) bits 9-5, **reg2** (the destination, or a condition code) bits 4-0 (TRM p.44, p.100). Assembler order is `OP source,destination`. 58 opcodes are shared; six slots differ. Sources and their tier: **TRM** = *Technical Reference Manual, Tom & Jerry, Revision 8* (manufacturer; page numbers are the PDF's), **ucode** = the GPU microcode ROM decoded by `scripts/mcode_table.py` from Flare's `mcodegen.c` (netlist tier: the words the silicon was built with, read, not simulated), **ARITH** = `NET/tom/ARITH.NET` (netlist, read), **MiSTer** = the reconstructed DSP words (HDL tier, [netlists.md](netlists.md)), **MAME** = 0.285 `src/devices/cpu/jaguar/jaguar.cpp` (emulator). `python -I scripts/opcode_tables.py` puts six encodings side by side (TRM, MiSTer's ROM, rmac, vasm, jrisc_tools, MAME): **they agree on all 64 GPU and 64 DSP slots except DSP 62** (C6). ## Flags: the rule from the netlist `ARITH.NET` l.248-270 (the flag latches): when an instruction's microword has its flag-load bit set, **Z and N are always loaded from the result** (Z = result is zero, N = result bit 31) and **C is always loaded**: from the adder's carry/borrow (`alu_co`) when the result selector is even (ALU 0, saturator 2, mantissa 4, pack 6), from the barrel shifter's carry (`brl_carry`) when it is odd (shifter 1, multiplier 3, normaliser 5, unpack/mirror 7) (`Cfisel0/1`, l.260-262). The adder runs on every instruction, whatever the ALU function selects as its output: `ALU32` (l.296-381) computes `sum`, `and`, `or`, `xor` in parallel and a 4-way mux picks one, while `alu_co` always comes from `sum`. Consequences: - Where the TRM says **"C - not defined"** after AND, OR, XOR, NOT, BSET, BCLR (and BTST), the netlist gives a definite value: the carry out of *(destination + source operand)*, where the source operand is the register, `~0` for NOT, `1< operand | | --- | --- | --- | | 1-32 | ADDQ, ADDQT, SUBQ, SUBQT, SHRQ, SHARQ, (DSP) ADDQMOD, SUBQMOD | field 0 means 32 | | 32-n | SHLQ | the field holds 32-n; the shifter sees a negative count (left) | | 0-31 | MOVEQ, BTST, BSET, BCLR, RORQ | raw field (TRM says RORQ takes 1-32: field 0 rotates by 0, which equals 32 for the result) | | signed -16..15 | CMPQ, JR | two's complement 5-bit; JR's is in words from the next instruction | | n x 4, n 1-32 | LOAD/STORE (R14+n), (R15+n) | long-word offset, field 0 means 32 | | condition | JUMP, JR (reg2) | see Conditions | ## Conditions (JUMP, JR; reg2 field) TRM p.40-41, p.48: bit 0 "Z clear", bit 1 "Z set", bit 2 "selected flag clear", bit 3 "selected flag set", bit 4 selects N (set) or C (clear) for bits 2-3; every set condition must hold (AND). So `00000` is always, `00011` and `01100` never, `11111` never (TRM lists it as "Jump never"). The TRM's table names 16 useful codes; the others are "jump always or jump never, and are reserved". MAME precomputes `condition_table` for all 32 codes x 8 flag states with the same rule. ## The opcode map Columns: **No.** opcode; **GPU / DSP** mnemonic; **Operation** (TRM's words, shortened); **TRM flags** Z N C as the manual states them (`-` unaffected, `?` "not defined"); **ucode** the flag-load and C source from the microword (`ZN Ca` = Z, N, C from adder; `ZN Cs` = C from shifter; `-` no flag load); **W** the cycle at which TRM says the destination is written (1 = the execute tick). Notes give contradictions and edge cases. | No. | GPU | DSP | Operation | TRM flags | ucode | W | Notes | | --- | --- | --- | --- | --- | --- | --- | --- | | 0 | ADD Rs,Rd | same | Rd = Rd + Rs | Z N C=carry | ZN Ca | 3 | | | 1 | ADDC Rs,Rd | same | Rd = Rd + Rs + C | Z N C | ZN Ca | 3 | waits for flags | | 2 | ADDQ n,Rd | same | Rd = Rd + n (1-32) | Z N C | ZN Ca | 3 | | | 3 | ADDQT n,Rd | same | as ADDQ, flags kept | - | - | 3 | | | 4 | SUB Rs,Rd | same | Rd = Rd - Rs | Z N C=borrow | ZN Ca | 3 | C is a borrow (ARITH inverts `alu_cout` when subtracting) | | 5 | SUBC Rs,Rd | same | Rd = Rd - Rs - C | Z N C | ZN Ca | 3 | | | 6 | SUBQ n,Rd | same | Rd = Rd - n (1-32) | Z N C | ZN Ca | 3 | | | 7 | SUBQT n,Rd | same | as SUBQ, flags kept | - | - | 3 | the interrupt entry's first instruction | | 8 | NEG Rd | same | Rd = 0 - Rd | Z N C=borrow | ZN Ca | 3 | `80000000` "cannot be negated" (stays, N set) | | 9 | AND Rs,Rd | same | Rd = Rd & Rs | Z N C? | ZN Ca | 3 | C4 | | 10 | OR Rs,Rd | same | Rd = Rd \| Rs | Z N C? | ZN Ca | 3 | C4 | | 11 | XOR Rs,Rd | same | Rd = Rd ^ Rs | Z N C? | ZN Ca | 3 | C4 | | 12 | NOT Rd | same | Rd = Rd ^ FFFFFFFF | Z N C? | ZN Ca | 3 | C4 | | 13 | BTST n,Rd | same | test bit n | Z=bit clear, N? C? | ZN Ca | (3) | no write (ucode `wr=-`); TRM's "Cycle 3 write" is the flags. N = 1 only for n = 31 with the bit set | | 14 | BSET n,Rd | same | set bit n | Z N C? | ZN Ca | 3 | | | 15 | BCLR n,Rd | same | clear bit n | Z N C? | ZN Ca | 3 | | | 16 | MULT Rs,Rd | same | Rd = Rd[15:0] x Rs[15:0] unsigned | Z N C? | ZN Cs | 3 | | | 17 | IMULT Rs,Rd | same | signed 16 x 16 -> 32 | Z N C? | ZN Cs | 3 | | | 18 | IMULTN Rs,Rd | same | as IMULT into the result register only | Z N C? | ZN Cs | - | starts a MAC group; atomic with the next instruction | | 19 | RESMAC Rd | same | Rd = accumulator (low 32) | - | - | 3 | DSP: top 8 bits readable at `F1A120` | | 20 | IMACN Rs,Rd | same | accumulate signed product, no write | - | - | - | multiply now, add one tick later (ucode) | | 21 | DIV Rs,Rd | same | Rd = Rd / Rs unsigned (or 16.16) | - | - | 18 | background unit; remainder register; see Divide | | 22 | ABS Rd | same | Rd = \|Rd\| | Z, N cleared, C=was negative | ZN Ca | 3 | ALU function 7 (subtract when the operand is negative); `80000000` stays, N set | | 23 | SH Rs,Rd | same | shift by signed Rs: + right, - left, zero in | Z N C=bit 0 (right) / bit 31 (left) | ZN Cs | 3 | count saturated to -32..+31; beyond gives 0 (`ARITH.NET` l.421-425) | | 24 | SHLQ n,Rd | same | shift left n (1-32) | Z N C=bit 31 | ZN Cs | 3 | encoded 32-n | | 25 | SHRQ n,Rd | same | shift right n (1-32), zero in | Z N C=bit 0 | ZN Cs | 3 | | | 26 | SHA Rs,Rd | same | as SH, sign in on right shift | Z N C | ZN Cs | 3 | out of range: 0, or all ones for a negative right shift | | 27 | SHARQ n,Rd | same | arithmetic right n (1-32) | Z N C=bit 0 | ZN Cs | 3 | | | 28 | ROR Rs,Rd | same | rotate right by Rs[4:0] | Z N C=bit 31 | ZN Cs | 3 | rotates are not saturated | | 29 | RORQ n,Rd | same | rotate right n | Z N C=bit 31 | ZN Cs | 3 | see Immediate operands | | 30 | CMP Rs,Rd | same | flags of Rd - Rs | Z N C=borrow | ZN Ca | (3) | no write | | 31 | CMPQ n,Rd | same | flags of Rd - n (-16..15) | Z N C=borrow | ZN Ca | (3) | no write | | 32 | SAT8 Rd | SUBQMOD n,Rd | GPU: clamp signed to 0..255. DSP: Rd - n under the modulo mask | GPU: Z, N cleared, C?. DSP: Z N C=borrow before masking | GPU ZN Ca; DSP (MiSTer) ZN Ca | 3 | DSP word reconstructed | | 33 | SAT16 Rd | SAT16S Rd | GPU: clamp to 0..65535. DSP: clamp signed to -32768..32767 | Z, N cleared, C? | ZN Ca (both) | 3 | C10 for SAT16S's N | | 34 | MOVE Rs,Rd | same | Rd = Rs | - | - | 2 | write-only destination (bug 13) | | 35 | MOVEQ n,Rd | same | Rd = n (0-31) | - | - | 2 | write-only | | 36 | MOVETA Rs,Rd | same | other bank's Rd = Rs | - | - | 2 | | | 37 | MOVEFA Rs,Rd | same | Rd = other bank's Rs | - | - | 2 | | | 38 | MOVEI #imm,Rd | same | Rd = next two words, low word first | - | - | 3 | three words; atomic; low word first regardless of BIG_INSTR (TRM p.60) | | 39 | LOADB (Rs),Rd | same | zero-extended byte | - | - | n | local space: a 32-bit read (TRM p.49) | | 40 | LOADW (Rs),Rd | same | zero-extended word | - | - | n | local space: a 32-bit read | | 41 | LOAD (Rs),Rd | same | long | - | - | 3-4 local | | | 42 | LOADP (Rs),Rd | SAT32S Rd | GPU: phrase, high long into HIDATA. DSP: clamp {acc[39:32], Rd} to signed 32 | GPU -; DSP Z N C? | GPU -; DSP (MiSTer) ZN Ca | n / 3 | LOADP in local space is a 32-bit read | | 43 | LOAD (R14+n),Rd | same | long at R14 + 4n | - | - | 5-6 local | two-tick address overhead | | 44 | LOAD (R15+n),Rd | same | long at R15 + 4n | - | - | 5-6 local | | | 45 | STOREB Rd,(Rs) | same | byte | - | - | - | local: a 32-bit write | | 46 | STOREW Rd,(Rs) | same | word | - | - | - | local: a 32-bit write | | 47 | STORE Rd,(Rs) | same | long | - | - | - | assembler order is `STORE data,(address)`: reg1 is the address, reg2 the data | | 48 | STOREP Rd,(Rs) | MIRROR Rd | GPU: phrase, high long from HIDATA. DSP: bit-reverse Rd | GPU -; DSP Z N C? | GPU -; DSP (MiSTer) ZN Cs | - / 3 | | | 49 | STORE Rd,(R14+n) | same | long at R14 + 4n | - | - | - | data not scoreboarded (bug 2) | | 50 | STORE Rd,(R15+n) | same | long at R15 + 4n | - | - | - | bug 2 | | 51 | MOVE PC,Rd | same | Rd = address of this instruction | - | - | 2 | PC minus the prefetch queue size (`PREFETCH.NET` l.309-312, `SUBSIZE` l.374-385); illegal after a jump | | 52 | JUMP cc,(Rs) | same | PC = Rs if cc; next instruction always executes | - | - | - | reg2 = condition | | 53 | JR cc,n | same | PC = next + 2n if cc; delay slot | - | - | - | target computed one tick later than JUMP's | | 54 | MMULT Rs,Rd | same | systolic dot product, see below | Z N C=carry (TRM) | - (the forced IMULTN loads them) | - | interrupts locked out; C11 | | 55 | MTOI Rs,Rd | same | mantissa + sign of an IEEE float to an integer, bit 23 sign-extended | Z N C? | ZN Ca | 3 | write-only destination. MAME computes `(Rs >> 8 signed) & FF800000 \| Rs & 007FFFFF` and sets no flags | | 56 | NORMI Rs,Rd | same | normalisation integer of an unsigned value | Z N C? | ZN Cs | 3 | write-only destination. MAME (`normi_rn_rn`) gives 0 for 0, else the signed number of right shifts that brings the leading 1 to bit 22 (negative = left). The archive's `soft/tstnormi.c` has the same loop, but its function name is Virtual Jaguar's style, so it is a later test harness, not Flare's: U12 | | 57 | NOP | same | nothing | - | - | - | occupies stage 1 only | | 58 | LOAD (R14+Rs),Rd | same | long at R14 + Rs | - | - | 5-6 local | | | 59 | LOAD (R15+Rs),Rd | same | long at R15 + Rs | - | - | 5-6 local | | | 60 | STORE Rd,(R14+Rs) | same | long at R14 + Rs | - | - | - | TRM p.56 names the destination field as the offset ("sum of R14 and the destination register"); MAME and rmac put the offset register in reg1 and the data in reg2, as for every store: C12 | | 61 | STORE Rd,(R15+Rs) | same | long at R15 + Rs | - | - | - | C12 | | 62 | SAT24 Rd | (undefined) | GPU: clamp to 0..16777215 | Z, N cleared, C? | ZN Ca | 3 | DSP: TRM silent; MiSTer NOP; MAME "illegal"; rmac assembles SAT24 for the DSP (C6) | | 63 | PACK Rd / UNPACK Rd | ADDQMOD n,Rd | GPU: reg1 = 0 PACK, 1 UNPACK. DSP: Rd + n under the modulo mask | GPU: "unaffected"; DSP Z N C=carry | GPU **ZN Ca** (C3); DSP (MiSTer) ZN Ca | 3 | the netlist decodes reg1 bit 0 only | ### Notes on single instructions - **PACK / UNPACK** (TRM p.43, p.53, p.58; p.132): UNPACK maps bits 12-15 to 22-25, 8-11 to 13-16, 0-7 to 0-7, all else 0; PACK the reverse, upper 16 bits 0. There are five spare bits above each field so up to 32 unpacked pixels can be summed before a shift and a PACK. - **ADDQMOD / SUBQMOD** (TRM p.99, p.101, p.108, p.111): result bits where the modulo register (`F1A118`, write-only) is 1 keep the old value; where it is 0 they take the sum. C is from the adder before masking. MAME: `res = (Rd + n) & ~mod | Rd & mod`. - **SAT32S** (TRM p.99, p.106): the 40-bit value is the accumulator's top 8 bits above the 32-bit **register operand**, so it is meant to follow `RESMAC` directly. MAME uses the accumulator's top bits and the register, matching that text. - **Width of loads and stores in local space**: TRM p.36-37, p.49-50: "only 32-bit transfers are possible within the GPU local address space"; LOADB/LOADW/LOADP and STOREB/STOREW/STOREP there do a 32-bit access. What a LOADB of a local address returns (the whole long, or the addressed byte) is U6. - **Alignment**: LOAD/STORE "must be long-word aligned", LOADW word aligned, LOADP phrase aligned (TRM). The hardware's behaviour when they are not is U6; MAME names games that do it (fball95, kasumi, sensible, pdrive, protect, atarikrt, barkley, bretth, chekflag) and special-cases Tom and Jerry register space only (`READLONG`/`WRITELONG` comments). - **Addresses** are 24 bits (TRM p.29: "a 24-bit address"); the CoJag trace shows `movei $fff02100,r21` used as the flags register address ([mame-oracle.md](mame-oracle.md)), so bits 24-31 are ignored (emulator observation; consistent with a 24-bit bus). ## Divide TRM p.42, p.61, p.111; `DIVIDE.NET`: unsigned non-restoring division, two quotient bits per tick (two cascaded stages), 16 ticks, running beside the pipeline; the quotient is written at cycle 18 and flags are untouched. The remainder register (`F0211C` / `F1A11C` read) holds the remainder if it is positive, else the remainder minus the divisor. Writing the same address sets **DIV_OFFSET**: the dividend is shifted left 16 before dividing, giving a 16.16 quotient of 16.16 operands. Both `DIVIDE.NET` copies take DIV_OFFSET from **data bit 0** (`Div_offsett := FDSYNCR (div_offsett, gpu_din{0}, divwr, ...`, Tom l.77, Jerry l.75); the TRM's DSP page says bit 1 (C2). Divide by zero is not described anywhere (U10). Consecutive divides fail unless the first quotient is read in between (bug 25, [pipeline-and-bugs.md](pipeline-and-bugs.md)). ## Multiply-accumulate and MMULT - **MAC group** (TRM p.39, p.41): `IMULTN a,b` puts the signed 16x16 product in the result register; each `IMACN` multiplies, and the product is added one tick later (ucode: "accumulate one tick later"); `RESMAC Rd` writes the sum. Anything else between them is illegal; IMULTN and IMACN are atomic with the next instruction, so no interrupt can split a group. **GPU accumulator: 32 bits. DSP: 40 bits** (TRM p.99), the top 8 readable sign-extended at `F1A120` and used by SAT32S. - **MMULT Rs,Rd** (TRM p.42, p.59, p.109): the hardware forces `IMULTN`, then `IMACN` x (width - 1), then `RESMAC Rd` into the stream. One matrix is in registers, **always the secondary bank whatever REGPAGE says**, packed two 16-bit elements per register starting at Rs; the other is in local RAM at MTXADDR (bits 2-11 of `F02108`/`F1A108`; the rest hard-wired, so `F03xxx` on the GPU and `F1Bxxx`, the low 4 KB, on the DSP: Jerry bug 6), stepping one long (MADDW = 0) or one matrix width (MADDW = 1) per element. MWIDTH 3-15 (`F02104` bits 0-3). Interrupts are locked out until it completes. Which half of each packed register goes first, the sign of the elements, and what MWIDTH 0-2 do are not stated (U11). MAME's `mmult_rn_rn` notes the games that use MADDW = 0 (BIOS logo, superx3d, ironsol2 DSP, hstrike) and none for MADDW = 1. ## Registers outside the file (memory-mapped, 32-bit, local space) | GPU | DSP | Name | Access | Meaning (TRM p.58-61, p.108-111) | | --- | --- | --- | --- | --- | | `F02100` | `F1A100` | FLAGS | R/W | 0 Z, 1 C, 2 N, 3 IMASK (write 0 clears, 1 ignored), 4-8 interrupt enables 0-4, 9-13 latch clears (write 1; read 0), 14 REGPAGE, 15 DMAEN; DSP 16 enable 5, 17 clear 5 | | `F02104` | `F1A104` | MTXC | W | 0-3 MWIDTH, 4 MADDW | | `F02108` | `F1A108` | MTXA | W | 2-11 MTXADDR | | `F0210C` | `F1A10C` | END | W | 0 BIG_IO, 1 BIG_PIX (GPU only), 2 BIG_INSTR | | `F02110` | `F1A110` | PC | R/W | write only while GO = 0; reads the executing instruction's address | | `F02114` | `F1A114` | CTRL | R/W | 0 GO, 1 CPUINT (pulse out), 2 interrupt 0 to self, 3 SINGLE_STEP (reads SINGLE_STOP), 4 SINGLE_GO, 6-10 latches 0-4 (read), 11 BUS_HOG, 12-15 version (2); DSP 16 latch 5 | | `F02118` | - | HIDATA | R/W | high long of LOADP/STOREP; changed by **any** external load (bug 16, bugs sheet 8) | | - | `F1A118` | MOD | W | modulo mask | | `F0211C` | `F1A11C` | REMAIN / DIVCTRL | R / W | remainder; DIV_OFFSET (bit 0 by the netlist, C2) | | - | `F1A120` | MACHI | R | accumulator bits 39-32, sign-extended | "All GPU registers are 32-bit, and will require all 32 bits to be written" (TRM p.58); other masters see them as 16-bit pairs, low address first, or as 32-bit write-only at `+$8000` (TRM p.31, p.43). A write to FLAGS and use of the flags by the next instruction does not work: TRM p.59 says one instruction must intervene, the 1995 bugs sheet item 11 says two NOPs (C1).