# Start here: the 65C816 as a core author needs it The SNES's CPU is a WDC 65C816 core inside Ricoh's **5A22** (marked S-CPU, S-CPU A, S-CPU B). The 5A22 adds DMA/HDMA, an 8x8 multiplier and 16/8 divider, H/V IRQ timers, joypad auto-read, an I/O port and a memory-speed selector around the core, and drives the core's clock so that each cycle lasts 6, 8 or 12 master clocks (fullsnes "CPU Clock Cycles"; SNESdev wiki *Timing* rev 1467). This repository builds the 65C816 alone. The board (`snes-python`, a sibling checkout) owns the 5A22's extras and everything else ([below](#what-the-core-does-not-own)). 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`. "DS" is WDC's W65C816S datasheet (`docs/w65c816s.pdf`, revision dated **March 13, 2024**, printed page numbers). Each 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). Tier words used below: **hw-verified** (a self-checking ROM whose author states it passes on a real SNES), **manufacturer** (WDC's datasheet: the designer's description, with known misprints), **research** (fullsnes, SNESdev wiki, 6502.org tutorials), **emulator** (SingleStepTests via Clock Signal, bsnes, ares, Mesen2, MAME). Full definitions in validation.md. ## Registers (DS §2, Table 2-1, pp. 6-9) | Register | Width | Notes | | --- | --- | --- | | A (C) | 16 | "C" names all 16 bits; with M = 1 only the low byte (A) is affected by 8-bit operations and the high byte (B) is preserved and reachable by `XBA` (manufacturer DS §2.4; emulator: every SST `.n` file with M = 1 keeps B) | | X, Y | 16 | with X = 1 the high bytes are **forced to zero** (DS §2.7; setting X by `SEP #$10` or entering emulation clears XH/YH; SST agrees) | | S | 16 | in emulation mode SH is forced to $01 (DS §2.11, Table 7-1) | | D | 16 | direct page base; D.l ≠ 0 costs a cycle on every direct-page access (DS note 2) | | DBR (B) | 8 | data bank for absolute and indirect data accesses | | PBR (K) | 8 | program bank; PC never carries into it (DS §2.9, §7.3) | | PC | 16 | | | P | 8 | N V M X D I Z C in native mode; in emulation mode bit 5 is 1 and bit 4 is B, as on the 6502 (DS §2.8) | | E | 1 | emulation flag, swapped with C by `XCE`, not in P (DS §2.17) | Reset (DS §2.25, p. 15, manufacturer): D = $0000, DBR = PBR = $00, SH = $01, XH = YH = $00, E = 1, M = X = 1, D flag = 0, I = 1; A, X.l, Y.l, S.l, N V Z C not initialised. bsnes and SST leave them at whatever they held. ## Emulation and native mode `E` = 1 after reset. In emulation mode M and X read as 1 and cannot be cleared (`REP #$30` does nothing to them), S is in page one, the vectors are the 6502's ($FFFA NMI, $FFFC RES, $FFFE IRQ/BRK, plus $FFF4 COP and $FFF8 ABORT), interrupts push 3 bytes and BRK is distinguished by B in the pushed P. `XCE` with C = 0 enters native mode, where M and X select 8- or 16-bit accumulator/memory and index registers, S is 16 bits, interrupts push PBR too (4 bytes) and use their own vectors ($FFE4 COP, $FFE6 BRK, $FFE8 ABORT, $FFEA NMI, $FFEE IRQ) (DS Tables 5-2, 5-3, p. 30). Emulation mode is **not an NMOS 6502** and not quite a 65C02 (DS Table 7-1, "Caveats", p. 49, manufacturer; 6502.org *65C816 Opcodes*, research): - decimal ADC/SBC give valid N and Z (the NMOS part does not), and unlike the 65C02 take **no** extra cycle (6502.org *Decimal Mode* §3.2.3); - D is cleared by every interrupt and BRK (NMOS: unchanged); - `JMP ($xxFF)` reads $xxFF and $xx00+$100 correctly (no NMOS page bug), and `JMP (a)` takes 5 cycles; - dummy cycles are "IO" cycles with VDA = VPA = 0 (DS Table 5-7), not reads of a real address; the indexed page-cross cycle drives an invalid address (DS Table 7-1 "Indexed across page boundary": "extra read of invalid address"); - a taken branch across a page costs one more cycle only in emulation mode (DS note 6), as on the 6502; - read-modify-write: in emulation mode the modify cycle has RWB low, a second write of the old value as on NMOS (DS note 17); in native mode it is an IO cycle (contested: [undocumented-behavior.md](undocumented-behavior.md) C3); - there are no undefined opcodes: all 256 do something (`$42` is `WDM`, a 2-byte no-op). The wrapping rules (when emulation mode keeps an access in page zero/page one and when it does not) are the largest single source of differences between implementations; [undocumented-behavior.md](undocumented-behavior.md) U1-U4 states them with sources. ## Instruction set 92 mnemonics on 256 opcodes, 24 addressing modes (DS §1.1, Table 5-4 p. 31, Table 5-6 p. 35). The opcode map, transcribed from Table 5-4 and checked against all 512 SingleStepTests files, is the `ROWS` table in `scripts/datasheet_timing.py`; generate the core's dispatch table from it or from 6502-python's (the brief decides). Table 5-4's printed cycle counts assume E = 1 for BRK/COP, native for RTI, M = X = 1, branches not taken; at least one is misprinted ($87 `STA [d]` shows "2,2", Table 5-7 row 15 and the corpus give 6 cycles). New on the 65C816 relative to the 65C02: `BRL COP JML JSL MVN MVP PEA PEI PER PHB PHD PHK PLB PLD REP RTL SEP STP TCD TCS TDC TSC TXY TYX WAI WDM XBA XCE`, the long, stack-relative, `[d]` and `(a,x)`-JSR modes. The 65C02's Rockwell bit instructions (`RMB SMB BBR BBS`) do not exist on it: their opcodes are the 65C816's long and stack-relative modes. ## Addressing and the 24-bit bus Every cycle drives a 24-bit address: A0-A15 on the address pins and A16-A23 multiplexed onto the data bus during PHI2 low (DS §2.16, Figure 5-1). Table 5-7 writes each cycle's address as `BANK,expression`: | Mode | Data address | | --- | --- | | `a` (absolute), `(d)`, `(d,x)`, `(d),y`, `(d,s),y`, `a,x`, `a,y` | DBR,(16-bit address [+ index]); the index **carries into the bank** (DS §7.3: "indexing from page ZZFFXX may result in ZZ+1,00YY") | | `al`, `al,x`, `[d]`, `[d],y` | the 24-bit operand or pointer [+ index], carrying through 24 bits | | `d`, `d,x`, `d,y`, `d,s` | bank 0, D + offset [+ index] wrapping at 16 bits (DS §7.2), with the emulation-mode exceptions of U1/U2 | | stack | bank 0, S | | `JMP (a)`, `JML [a]` | pointer read from **bank 0** (Table 5-7 rows 3a, 3b) | | `JMP (a,x)`, `JSR (a,x)` | pointer read from **PBR** (rows 2a, 2b) | | program | PBR,PC; PC wraps at 16 bits, never into PBR | | `MVN/MVP` | source SBA,X, destination DBA,Y; DBR becomes the destination bank | The full per-cycle list is [timing.md](timing.md). ## The bus as the 5A22 sees it The 65C816 marks every cycle with **VDA** (valid data address), **VPA** (valid program address), **VPB** (vector pull, low on the two vector reads), **MLB** (memory lock, low on read-modify-write cycles) and **RWB** (read/write) (DS §2.19-2.28). They combine into the cycle kinds a board needs (DS §2.26 table): | VDA | VPA | Kind | Example | | --- | --- | --- | --- | | 1 | 1 | opcode fetch | cycle 1 of every instruction | | 0 | 1 | program read (operand) | `LDA #$12` cycle 2; `JMP (a,x)` pointer per the datasheet (but see C1) | | 1 | 0 | data read or write | operands, stack, vectors (with VPB) | | 0 | 0 | internal operation (IO) | implied cycle 2, D.l penalty, index carry, RTS's last cycle; the address is driven but "may be invalid" (DS §7.5) | The 5A22 turns each cycle into 6, 8 or 12 master clocks: IO cycles are 6; accesses are 6 (I/O at $2000-$3FFF and $4200-$5FFF in banks $00-$3F/$80-$BF, and banks $80-$FF ROM when MEMSEL bit 0 is set), 8 (WRAM, slow ROM, $0000- $1FFF, $6000-$7FFF), or 12 ($4000-$41FF, the manual joypad ports) (research: fullsnes "Memory Map", SNESdev *Timing*; emulator: MAME `g65816.cpp` `bus_5A22_cycle_burst` (BSD-3), the same table). So the board must see, **for every cycle, its 24-bit address and whether it is an access (VDA or VPA) or IO** before it can advance time. This is why the core makes a callback for internal cycles too, unlike 6502-python. The board also keeps the open-bus latch (MDR), which IO cycles and reads of $4000-$43FF do not update (HYPOTHESIS: read from bsnes's GPL source before the 2026-11-14 rule; for review, 2026-11-24). On the SNES the 65C816 is clocked so that the address is presented at the start of the cycle and data latched near its end (2/4/8 clocks before and 4 after the data transfer: HYPOTHESIS, read from bsnes's GPL source before the 2026-11-14 rule; for review, 2026-11-24); that sub-cycle placement is the board's concern. The core needs only to make each access at the right point in the instruction, in order. ## Interrupts - **NMI** is edge-triggered, **IRQ** a level, both sampled "on the edge of every opcode"; ares (ISC) samples them at the start of an instruction's last cycle (bsnes too: HYPOTHESIS, read from its GPL source before the 2026-11-14 rule; for review, 2026-11-24); 6502-python already polls "at the end of the second-to-last cycle" (its `docs/start-here.md`), which is the same instant. Emulator tier; the 6502 rule has visual6502 behind it, the 65C816's has only emulator authors' hardware timing work. - Entry (DS Table 5-7 row 22a): 1 cycle at PBR,PC with the opcode fetch discarded, 1 IO, push PBR (native only), PCH, PCL, P, then the two vector reads with VPB low: 8 cycles native, 7 emulation. I set, D cleared, PBR = 0. In emulation mode the pushed P has B = 0 for IRQ/NMI and B = 1 for BRK (DS note 11 and Table 7-1; SST `00.e`: P|$30 pushed in all 10,000 cases). - **BRK, COP** (row 22j): the signature byte is read as a program byte (VPA) and PC skips it; otherwise as an interrupt. **ABORT** finishes the current instruction's bus cycles without changing registers, then enters through $FFE8/$FFF8 with the aborted instruction's address pushed (DS §2.13, §7.4). The SNES does not use ABORT (fullsnes "CPU Vectors": $FFE8 and $FFF8 "not used in SNES"), so it is low priority. - **RESET** (row 22a with note 10): RWB stays high on the three push cycles, which become reads of the stack; S decrements by 3. - **WAI** (row 19d, DS §2.24): 3 cycles, then the clock stops with RDY pulled low until NMI, IRQ, ABORT or RESET; if I = 1 an IRQ wakes it and execution continues with the next instruction, no vector taken. **STP** (row 19c) stops until reset. SST records both as 3 cycles followed by a `[null, null, "--------"]` row (timing.md). - bsnes turns the IO cycle of a 2-cycle implied instruction into a read of PC when an IRQ is about to be taken, which changes its length on the SNES (U9; HYPOTHESIS, read from its GPL source before the 2026-11-14 rule; for review, 2026-11-24). ## MVN / MVP Each byte is one 7-cycle pass: opcode fetch, the two bank operands (VPA), read SBA,X, write DBA,Y, two IO cycles at DBA,Y (Table 5-7 rows 9a/9b); then X, Y and C step (MVN +1, MVP -1) and, unless C wrapped to $FFFF, PC is moved back to the opcode so the next pass refetches it. Interrupts are taken between passes and return to the opcode (DS row 9 diagram; the poll on the last IO cycle is HYPOTHESIS, read from bsnes's GPL source before the 2026-11-14 rule; for review, 2026-11-24). X and Y step in 8 bits when X = 1 (SST, timing.md). DBR ends as the destination bank. In SST each `44`/ `54` case is a whole move (at most 1,000 bytes, 100 cases per file, README). ## Decimal mode ADC and SBC in BCD on 8 or 16 bits. N and Z are valid (they reflect the result), C is the decimal carry; V and the results for invalid BCD digits are undocumented and specified by 6502.org *Decimal Mode* Appendix A (research) and exercised by SST and gilyon's cputest (U7). No extra cycle. ## What the core does not own The 5A22's DMA and HDMA (they halt the CPU between cycles, aligned to the 8-clock DMA counter, and add overhead: board), the multiplier and divider ($4202-$4206, results after 8/16 CPU cycles: board), H/V IRQ timers and NMI generation ($4200, $4207-$420A, RDNMI/TIMEUP), joypad auto-read, WRIO/RDIO, MEMSEL and every memory speed, the 40-clock DRAM refresh per scanline, the open-bus MDR, the B-bus, the S-SMP and its boot ROM, cartridges and mappers. The core must **expose** what the board needs to do each of them exactly: a per-cycle callback carrying the 24-bit address and the cycle kind (fetch/program/data/IO, read/write, vector, lock), interrupt inputs sampled at the documented point, a way for the board to stall the CPU between cycles (the callback simply takes longer), and the WAI/STP state.