# Start here: the SPC700 as a core author needs it The SNES's sound unit is built around Sony's **S-SMP** (the die the SNES schematics call S-SMP; fullsnes "SNES APU SPC700 CPU Overview"). Inside it is an **SPC700** CPU core: an 8-bit processor whose registers and many addressing modes recall the 6502, with its own opcode map, 16-bit `YA` operations, `MUL`/`DIV`, bit instructions on a 13-bit address, and a selectable direct page. The same instruction set appears in Sony's CXP8nnnn microcontrollers (fullsnes); this repository builds the core as the S-SMP uses it. The rest of the die (IPL ROM, timers, I/O ports, TEST/CONTROL, DSP ports) belongs to the board, snes-python ("What the core does not own", below, and [the brief](handoff-brief.md)). Paths: `src/X` is `reference/src/X`, `docs/X` is `reference/docs/X`, `roms/X` is `reference/roms/X`, all filled by `scripts/fetch_references.py`. Every source's tier is in [validation.md](validation.md); cycle detail is in [timing.md](timing.md), quirks and contradictions in [undocumented-behavior.md](undocumented-behavior.md) (items U1.., C1..). Sony's own SPC700 manual is not public; everything here rests on the community documents (anomie, fullsnes), byuu's model as published in ares (ISC), SingleStepTests (MIT), and the hardware-run test ROMs. GPL and LGPL emulators (bsnes/higan, Mesen2, snes_spc in libgme, MAME's S-SMP and S-DSP devices) are closed boxes only: run, never read (provenance.md, Wave 2). ## Registers | Register | Width | Notes | | --- | --- | --- | | A | 8 | accumulator; the low half of YA | | X | 8 | index; `(X)`, `(X)+`, `d+X`, `[d+X]`, `!a+X`, `[!a+X]` | | Y | 8 | index; the high half of YA; `DBNZ Y` | | YA | 16 | **not a register of its own**: Y:A paired, for `MOVW`, `ADDW`, `SUBW`, `CMPW`, `MUL`, `DIV` (fullsnes "Registers"; ares `r.ya` is a union over A and Y) | | SP | 8 | stack pointer; the stack is always page 1, `$0100 + SP`, grows down, wraps within the page (anomie "OPCODES") | | PC | 16 | wraps $FFFF to $0000 (anomie) | | PSW | 8 | `N V P B H I Z C`, bits 7..0 | PSW bits (fullsnes "Flags (PSW)"; anomie): | Bit | Flag | Meaning | | --- | --- | --- | | 7 | N | sign of the result | | 6 | V | signed overflow (ADC, SBC, ADDW, SUBW); DIV's quotient overflow (U5) | | 5 | **P** | **direct page select**: 0 = `$00xx`, 1 = `$01xx` for every direct-page operand (`d`, `d+X`, `d+Y`, `(X)`, `(Y)`, `[d]`, `[d+X]`, `d.b`); `SETP`/`CLRP`; also gates writes to `$F0` (U9) | | 4 | B | set by BRK; there is no separate pushed-only B as on the 6502: PSW holds a real B bit (fullsnes "set BRK opcode") | | 3 | H | half carry: bit 3→4 for 8-bit ADC/SBC, bit 11→12 for ADDW/SUBW (fullsnes "16bit ALU Operations"); CLRV clears it; DIV sets it oddly (U5) | | 2 | I | interrupt enable; **no function in the SNES APU** (fullsnes "Wait/Delay/Control" note) | | 1 | Z | result zero | | 0 | C | carry; "no borrow" after SBC/CMP | All eight bits are real flip-flops: `POP PSW` and `RET1` load all eight, `PUSH PSW` and BRK store all eight (SingleStepTests spc700 `8e.json`, `0d.json`; ares `P = pull()`). ## Instruction set - **256 opcodes, all defined**: no holes, no prefix bytes. Two of them, `SLEEP` ($EF) and `STOP` ($FF), halt the CPU for good on the SNES (below). - Operands are stored little-endian and, for two-operand forms, **source before destination** ("OP A, B" is stored as "OP B A"); the exceptions are `BBC`, `BBS`, `CBNE` and `DBNZ d`, which store the direct-page byte first and the branch offset second (anomie "OPCODES"). - Columns of the opcode map follow a scheme: low nibble $1 is `TCALL n` (n = high nibble), $2 is `SET1`/`CLR1 d.b`, $3 is `BBS`/`BBC d.b,r`, and the ALU block (`OR AND EOR CMP ADC SBC` at $0x, $2x, $4x, $6x, $8x, $Ax) repeats its twelve operand forms in columns $4-$9 of each row pair (fullsnes "8bit ALU Operations"). - Machine-readable sources: `tests/anomie_opcodes.json` (made by `scripts/anomie_opcodes.py` from anomie's table: mnemonic, operands, bytes, cycles, flags) and SingleStepTests spc700 (`src/spc700/v1/*.json`). Generate the dispatch table from the first, check it against the second, hand-write the handlers (the m6800-python and sm83-python practice). - Addressing modes (fullsnes "Addressing Modes"; anomie): | Mode | Syntax | Effective address | | --- | --- | --- | | immediate | `#i` | the operand byte | | direct | `d` | `P*$100 + d` | | direct indexed | `d+X`, `d+Y` | `P*$100 + ((d + X) & $FF)`: wraps **within** the direct page | | indirect | `(X)`, `(Y)` | `P*$100 + X` (or Y) | | indirect, post-increment | `(X)+` | `P*$100 + X`, then X += 1 (8-bit) | | direct, direct | `dd, ds` | two direct-page operands | | direct, immediate | `d, #i` | direct page, with an immediate source | | absolute | `!a` | 16-bit | | absolute indexed | `!a+X`, `!a+Y` | `(a + X) & $FFFF`: no page-wrap quirk | | indexed indirect | `[d+X]` | word at `P*$100 + ((d + X) & $FF)` (high byte from `(d+X+1) & $FF`, still in the page) | | indirect indexed | `[d]+Y` | word at `P*$100 + d`, plus Y | | absolute indexed indirect | `[!a+X]` | `JMP` only: word at `a + X` | | direct bit | `d.b` | bit b (from the opcode) of a direct-page byte | | absolute bit | `m.b` | 16-bit operand: low 13 bits an address in $0000-$1FFF, top 3 bits the bit number | | relative | `r` | signed 8-bit offset from the address after the instruction | | table | `TCALL n` | word at `$FFDE - 2n` | | upper page | `PCALL u` | `$FF00 + u` | The direct-page word reads (`MOVW`, `ADDW`, `SUBW`, `CMPW`, `INCW`, `DECW`, `[d]`, `[d+X]`) take the high byte from `(d + 1) & $FF` within the same page (ares `load(address + 1)` with an 8-bit `address`; SingleStepTests cases with d = $FF). ## Flags per instruction anomie's table (`tests/anomie_opcodes.json`, `flags` column) and fullsnes agree on which flags each instruction changes. The rules a core needs beyond "N and Z from the result" (ares `algorithms.cpp`, checked by SingleStepTests on every opcode; tier in [validation.md](validation.md)): - `ADC`: C = carry out of bit 7, H = carry out of bit 3, V = signed overflow. `SBC x, y` is `ADC x, ~y` (C = no borrow, H = no half-borrow). - `CMP`: N Z C only, C = no borrow; V and H untouched. - `ADDW`/`SUBW`: computed as two 8-bit ADC/SBC (low then high, C cleared first for ADDW, set first for SUBW), so H is the carry from bit 11 and V the overflow of the high byte; **Z is set from the whole 16-bit result** (ares `algorithmADW`). - `CMPW`: N Z C from the 16-bit subtraction; V, H untouched. - `INCW`/`DECW`: N Z from the 16-bit result; C untouched. - `MOVW YA, d`: N Z from 16 bits. `MOVW d, YA`: no flags. - `MUL`: N Z from **Y only** (the high byte); `DIV`: U5. - `MOV` into a register sets N Z, except `MOV SP, X` (no flags); `MOV` into memory sets none. - `TSET1`/`TCLR1`: N Z as for `CMP A, (a)`, taken **before** the write. - `AND1`/`OR1`/`EOR1`/`MOV1 C, m.b`/`NOTC`/`SETC`/`CLRC`: C only. `CLRV` clears V and H. - `POP PSW`, `RET1`: all eight. `BRK`: B ← 1, I ← 0. - `DAA`/`DAS`: U4. `XCN`: N Z from the swapped A. ## Clocks and the bus cycle The S-SMP is clocked from the APU module's 24.576 MHz crystal through the S-DSP: 24.576 MHz ÷ 12 = 2.048 MHz into the S-SMP, and one SPC700 bus cycle is two of those, **1.024 MHz** (ares `sfc/smp/timing.cpp`: "DSP clock (~24576khz) / 12 (~2048khz) is fed into the SMP", a normal cycle costs 2 of those clocks; fullsnes counts opcode cycles at this rate; MAME's `totalcycles` counts the 2.048 MHz clock, 2 per cycle, measured in [mame-oracle.md](mame-oracle.md)). Every count in this repository's documents is in SPC700 cycles unless it says otherwise. The TEST register (`$F0`) can stretch cycles: bits 4-5 add wait states to RAM accesses, bits 6-7 to I/O and IPL ROM accesses (0/1/4/9 waits, i.e. 1/2/5/10 cycles per access; fullsnes "00F0h - TEST"). Which wait class an internal cycle takes is itself a contested question (C3). No commercial software is known to change `$F0` from `$0A` (anomie: "print lots of warnings ... if any value other than $0A is written"). The **board** owns all of this; the core's part is to make one callback per cycle with enough information for the board to price it ([the brief](handoff-brief.md) "The bus contract"). ## The bus as the S-SMP sees it Every cycle is one of three things (SingleStepTests spc700 `cycles` lists; ares `read`/`write`/`idle`; anomie's cycle doc): | Kind | SST | Example | Note | | --- | --- | --- | --- | | read | `read` with data | operand fetch, `(d)`, stack pull, vector | | | **dummy read** | `read`, data `null` | 2nd cycle of every one-byte implied opcode (`NOP`, `INC A`, `MOV A,X`, `PUSH`, `RET`, ...): a read of PC (the byte after the opcode), result discarded | anomie writes "`??` IO" for these; fullsnes counts them as internal cycles (C2) | | write | `write` | store, push, RMW write-back | | | internal | `wait`, address `null` | `d+X` index add, branch taken (2 cycles), `MUL`'s 7, `DIV`'s 10 | the corpus gives no address (C1) | **Read-before-write.** Most stores to memory are RMW-shaped: they read the destination before writing it. `MOV d, #i`, `MOV d, A`, `MOV !a, A`, `MOV (X), A`, `MOV [d]+Y, A` and the rest read first; **`MOV dd, ds` (FA), `MOV (X)+, A` (AF) do not, and `MOVW d, YA` (DA) reads the low byte only** (anomie "OPCODES"; fullsnes "Memory Store"; measured on all SingleStepTests cases: [validation.md](validation.md)). On the S-SMP this matters: a read of `$FD`-`$FF` clears that timer's 4-bit counter, so `MOV $FD, #0` loses a pending timer tick (anomie: "MOV $ff, #$00 will read from $ff at some point, and therefore will reset T2OUT. OTOH, MOV $ff, $00 won't"). The dummy reads of PC matter the same way when code runs from `$00F0`-`$00FF` (blargg's `1-test_exec_from_io`, U10). ## Control flow - **CALL !a** (8 cycles), **PCALL u** (6: `$FF00 + u`), **TCALL n** (8: vector at `$FFDE - 2n`, so TCALL 0 uses $FFDE and TCALL 15 $FFC0, inside the IPL ROM area). All push **the address of the next instruction** (unlike the 6502's return-1), high byte first (fullsnes "Note: Calls are pushing the exact retadr"). - **BRK** (8): pushes PC (next instruction), then PSW, sets B, clears I, jumps through `$FFDE`/`$FFDF` (the same vector as TCALL 0). **RET1** ($7F, called RETI elsewhere) pops PSW then PC. - **RET** (5), **JMP !a** (3), **JMP [!a+X]** (6). - Branches: 2 cycles not taken, 4 taken (the two extra cycles are internal); `BBS/BBC d.b, r` and `CBNE d, r` 5/7; `CBNE d+X, r` 6/8; `DBNZ d, r` 5/7 (an RMW: decrements memory and writes it back even when not branching); `DBNZ Y, r` 4/6. No branch flag effects (fullsnes "Conditional Jumps"). - No page-crossing penalties anywhere (SingleStepTests: every non-branch opcode has one fixed cycle count across all 1,000 random cases). ## No interrupts on the SNES The SPC700 instruction set has `EI`, `DI`, `BRK`, `RET1`, `SLEEP` and a PSW I flag, but the S-SMP in the SNES has **no interrupt sources** (fullsnes: "The SNES APU doesn't have any interrupt sources, so SLEEP/STOP will hang the CPU forever, and DI/EI have no effect (other than changing the I-flag in PSW)"; fullsnes "Audio-related Registers": "there are no additional communication methods (like IRQs)"). MAME's generic `spc700` device has IRQ/NMI inputs (`execute_set_input`); its S-SMP never drives them. So the core needs no interrupt input; `EI`/`DI` only change I; `BRK`/`RET1` are ordinary instructions. See the brief's open decision on whether to keep a hook anyway. ## SLEEP and STOP `SLEEP` ($EF) waits for an interrupt that never comes; `STOP` ($FF) stops the clock. On the SNES both hang the S-SMP until reset (fullsnes, above). Their bus activity while halted is not documented by any hardware source: ares loops "dummy read PC, internal cycle" forever (`instructionWait`, `instructionStop`). SingleStepTests records 7 cycles (`RDIDIDI`), an artefact of its generator (`SPC_GEN_WAIT_HOW_LONG = 6`: three dummy-read/internal pairs after the fetch), not a hardware fact (C5). The core should enter a `stopped` state and make one cycle per `step()` from then on ([the brief](handoff-brief.md)). ## Reset and power-on On reset the S-SMP jumps through `$FFFE` with the IPL ROM mapped (fullsnes "/RESET: PC=[FFFEh] ..00.0.." meaning P = 0, B = 0, I = 0; the IPL ROM's last two bytes are $FFC0). The IPL then sets SP = $EF and clears $0000-$00EF; the documented register state on entry to uploaded code is A = X = Y = 0, PSW = $02, SP = $EF (anomie "IPL BOOT ROM"). Register values before the IPL runs are not settled by any hardware source (ares: SP = $EF, PSW = $02); the IPL overwrites SP and the flags it uses, so the difference is invisible to software (U12). The bus activity of the reset sequence itself is undocumented. The core provides plain register attributes and a `reset()` that reads the vector through the bus; the board supplies the IPL ROM behind `$FFC0`-`$FFFF`. ## What the core does not own Everything in `$00F0`-`$00FF` and the IPL ROM overlay is the S-SMP's, not the SPC700's: TEST (`$F0`: timer enable/disable, RAM write enable, "crash" bit, wait states), CONTROL (`$F1`: timer enables, port clears, IPL ROM mapping), DSPADDR/DSPDATA (`$F2`/`$F3`: the S-DSP's register file, which lives in snes-python's in-board DSP module), CPUIO0-3 (`$F4`-`$F7`: the S-CPU mailboxes), AUXIO4/5 (`$F8`/`$F9`, RAM-like on the SNES), the three timers (`$FA`-`$FF`), the 64 KiB RAM (shared with the S-DSP, which also reads BRR samples and the echo buffer from it), and the cycle clock with its wait states. [The brief](handoff-brief.md) "Core boundary" recommends where each goes. The core must **expose** enough for the board to do each exactly: one callback per cycle in chip order (dummy reads and internal cycles included), the P flag readable inside a write callback (U9), a stopped state, and enough about each internal cycle for the board to choose its wait class (C3).