# Start here: the Super Nintendo as a board author needs it The machine in one paragraph: the **S-CPU** (Ricoh 5A22) is a WDC 65C816 core plus the parts the board owns: the address decoder that sets each bus cycle's length (6, 8 or 12 master clocks), eight DMA/HDMA channels, the 8x8 multiplier and 16/8 divider, the H/V IRQ and NMI timer, the joypad auto-read and the I/O port. Two picture chips, **S-PPU1** (5C77) and **S-PPU2** (5C78), share 64 KiB of VRAM, hold OAM and CGRAM, and are reached from the CPU over the 8-bit **B bus** ($2100-$21FF). The 128 KiB of work RAM is **S-WRAM**, on both buses (the A bus directly, the B bus through $2180-$2183, so DMA can copy cartridge to WRAM). The sound side is a separate little computer: the **S-SMP** (an SPC700 core with a 64-byte IPL ROM, three timers and four mailbox ports to the CPU at $2140-$2143) and the **S-DSP**, which owns the 64 KiB of audio RAM (ARAM) and produces the 32 kHz stereo output; it runs from its own 24.576 MHz resonator, not the master clock. The cartridge sees the whole A bus (24-bit address, /ROMSEL, /WRAMSEL) and part of the B bus, and may carry a coprocessor. Paths: `fullsnes` is Martin Korth's `reference/docs/fullsnes.txt` (the live URL, fetched 2026-10-21, SHA-256 in `scripts/pinned_files.json`); `anomie timing/regs/memmap` are `reference/docs/anomie_*.txt` ($Revision 1157-1160, 2007-2008); `wiki/X` is `reference/snesdev-wiki/X.wiki` (SNESdev wiki raw pages, fetched 2026-10-21); `ares/` (ISC, readable with its notice) and `mame/` (BSD-3 files readable after a per-file header check) are pinned trees under `reference/src/`; `bsnes/`, `Mesen2/`, `snes9x/` are pinned there too, but only to be **built and run as closed boxes**, never opened (docs/restart.md). The tier of each source is in [validation.md](validation.md). Tags used throughout (the family's evidence tags, methodology.md, map onto these: [hw] = MEASURED or TEST ROM, [doc] = DOC, [emu] = OBSERVED): | Tag | Meaning | | --- | --- | | **[hw]** | hardware-captured: a real console produced it (a photo, a capture, a test ROM's verdict on a named unit) | | **[hc]** | hardware-corrected: a model or document checked against real units by a named person | | **[doc]** | research document (fullsnes, anomie, SNESdev wiki) whose claim rests on experiments we cannot see; a strong detector, not a judge | | **[emu]** | emulator-derived: a closed-box run (bsnes, Mesen2, MAME, snes9x), or the source of ares (ISC) or of MAME's BSD-3 files, named with file and commit: a detector only | | **[measured]** | run here, on the date given | ## The machines (NTSC first) | Machine | S-CPU | PPU1 / PPU2 | Master clock | Phase | | --- | --- | --- | --- | --- | | **SNS-001 / SHVC-001, 5A22-02 (CPU version 2): the reference** | 5A22-02 | 5C77-01 / 5C78-03 | 21.477272 MHz | **1** | | early units with 5A22-01 (CPU version 1) | 5A22-01 | 5C77-01 / 5C78-01..03 | same | 1, as a field | | PAL SNES | 5A22-02 (PAL strap) | 5C77 / 5C78 PAL | 21.281370 MHz | later, as data | | SNS-101 / SHVC-101 ("1CHIP", "jr.") | S-CPU and both PPUs in one die ("1CHIP-01/02/03") | same | NTSC | later, as a field | Sources: fullsnes "4210h - RDNMI" (bits 3-0: CPU version, "version 2 exists") and "213Eh/213Fh" (PPU1 version 1 only, PPU2 versions 1-3) [doc]; ares 9408cb4 (ISC) `sfc/cpu/cpu.cpp` `power()` and `sfc/cpu/timing.cpp` `scanline()` (CPU version 1 and 2 differ in where the DRAM refresh and HDMA setup fall within the 8-clock DMA divider) [emu]; undisbeliever's `scpu-a-dma-bug-*` probes (a DMA failure on "S-CPU-A" units) [probe, result not captured: see validation.md]. Revision is a field of the machine table from milestone 1, because DMA, HDMA and refresh timing differ by CPU version and the mid-frame brightness glitches differ by PPU2 version and 1CHIP. Fields the machine table needs, each with its source in a comment: - `region` (ntsc, pal): master clock, 262 or 312 lines, `$213F` bit 4, the short/long line rule; - `cpu_version` (1, 2): `$4210` bits 3-0, refresh and HDMA setup position, the DMA/HDMA collision bug of version 1; - `ppu1_version`, `ppu2_version` (1; 1-3): `$213E`/`$213F` low nibble, PPU2-revision picture differences (C5); - `one_chip` (bool): 1CHIP consoles differ in mid-frame INIDISP glitches and video output (undisbeliever `inidisp_*` probes); - power-on fill of WRAM, VRAM, OAM, CGRAM, ARAM (not zero on hardware; fullsnes "SNES Memory Map" notes random contents) and the S-SMP's state. ## Clocks | | NTSC | PAL | | --- | --- | --- | | Master clock | 21.477272 MHz (6 x colour subcarrier; 945/44 MHz) | 21.281370 MHz (17.734475 MHz x 6/5) | | CPU bus cycle | 6, 8 or 12 master clocks | same | | Dot | 4 master clocks, except dots 323 and 327 at 6 (C1) | same | | Line | 1364 master clocks, 340 dots; **1360** on line 240 of odd fields with interlace off | 1364; **1368** on line 311 of odd fields with interlace on | | Lines per frame | 262 (263 on even fields with interlace on) | 312 (313) | | Master clocks per frame | 357,368 / 357,364 | 425,568 / ... | | Frame rate | 60.0988 Hz | 50.007 Hz | | S-DSP / S-SMP | 24.576 MHz resonator; S-SMP 1.024 MHz (/24), DSP sample 32 kHz (/768) | same | `wiki/Timing` "Master Clock", "Short and Long Scanlines" [doc]; anomie timing l.55-66 ("1364 master cycles, except in non-interlace mode scanline $f0 of every other frame ... only 1360 cycles") [doc]; ares 9408cb4 (ISC) `sfc/ppu/counter/inline.hpp` [emu]. **Measured 2026-10-21**: MAME 0.285's 5A22 trace of Super Mario World (USA) over 120 frames gives a mean of 357,368 master clocks per frame between frame starts, i.e. MAME does not model the 1360-clock line (the mean would be 357,366) [measured, emu]. The resonator is not a crystal: the wiki says consoles run ~0.25 % fast and drift ~0.04 % warm (`wiki/Timing` "Audio"; nesdev forum t=24610) [doc]. So the **CPU-to-APU clock ratio is a machine parameter**, not a constant: the board default is the nominal 24.576 MHz, and a field allows a measured console's value. Games that communicate with the S-SMP by polling are insensitive to it; games that race it are the classic emulation failures (`wiki/Tricky-to-emulate games`). **Use one timeline in master clocks.** The S-CPU, the PPU dot counter, DMA, the refresh pause, the IRQ timer and the joypad auto-read all count master clocks. The S-SMP/S-DSP side is a second clock domain; the board converts with an exact rational accumulator (master x 24.576/21.477272), never a float, and synchronises the two sides at every `$2140-$2143` access from either side (timing.md "Two clock domains"). ## The CPU side ### Bus cycle length (the board decides, per cycle) | Address (bank : offset) | Cycle | What | | --- | --- | --- | | `$00-3F,$80-BF : $0000-$1FFF` | 8 | WRAM low 8 KiB mirror | | `$00-3F,$80-BF : $2000-$3FFF` | 6 | B bus ($2100-$21FF) and expansion | | `$00-3F,$80-BF : $4000-$41FF` | **12** | old-style joypad ports ($4016/$4017) | | `$00-3F,$80-BF : $4200-$5FFF` | 6 | CPU registers, DMA | | `$00-3F,$80-BF : $6000-$7FFF` | 8 | expansion / cartridge SRAM | | `$00-3F : $8000-$FFFF`, `$40-7F : any` | 8 | cartridge ROM (and `$7E-7F` WRAM) | | `$80-BF : $8000-$FFFF`, `$C0-FF : any` | **6 if MEMSEL ($420D) bit 0, else 8** | "FastROM" | | internal operation (no access) | 6 | | fullsnes "CPU Clock Cycles" [doc]; `wiki/Timing` "CPU" [doc]; ares 9408cb4 (ISC) `sfc/cpu/memory.cpp` `wait()` implements the same table, and reads the data 4 master clocks before the end of the cycle while writes land at the end [emu]. **Measured 2026-10-21**: MAME 0.285's trace of Super Mario World's reset code charges `SEI` 14 master clocks (8 opcode fetch from SlowROM + 6 internal) and `STZ $4200` 30 (3 x 8 + 6), so its `totalcycles` is in master clocks and follows the table [measured, emu]. ### Memory map (A bus) | Range | Device | | --- | --- | | `$00-3F,$80-BF : $0000-$1FFF` | WRAM `$7E:0000-1FFF` | | `$00-3F,$80-BF : $2100-$213F` | PPU1/PPU2 registers (B bus) | | `$00-3F,$80-BF : $2140-$217F` | APU ports `$2140-$2143`, mirrored every 4 | | `$00-3F,$80-BF : $2180-$2183` | WRAM data port and address (WMDATA, WMADD) | | `$00-3F,$80-BF : $4016-$4017` | joypad serial ports (JOYSER0/1) | | `$00-3F,$80-BF : $4200-$421F` | CPU registers (NMITIMEN, WRIO, mul/div, H/VTIME, MDMAEN, HDMAEN, MEMSEL, RDNMI, TIMEUP, HVBJOY, RDIO, results, JOY1-4) | | `$00-3F,$80-BF : $4300-$437F` | DMA channel registers (8 x 16, `$43xC-$43xE` unused, `$43xF` mirrors `$43xB`) | | `$7E-$7F : any` | WRAM, 128 KiB | | everything else | the cartridge (and expansion port), per its map (cartridge.md) | fullsnes "SNES Memory Map", "SNES I/O Map" [doc]; anomie memmap [doc]; `wiki/Memory map` [doc]. Unmapped reads return **open bus**, the CPU's MDR (the last value on the data bus), except that reads of `$4000-$43FF` are internal to the S-CPU and do not update the MDR (ares 9408cb4 (ISC) `sfc/cpu/memory.cpp` `read()`; fullsnes "Unused region (open bus)") [doc, emu]. The B bus has its own open-bus values: PPU1 and PPU2 each keep their own latch of the last value read from them (below). ### CPU registers the board owns - **NMITIMEN `$4200`**: NMI enable, V/H IRQ enable, auto-joypad enable. - **WRIO `$4201` / RDIO `$4213`**: the I/O port; bit 7 of WRIO is the PPU's H/V counter latch line (a 1-to-0 transition latches, "1 dot later than" `$2137`, anomie timing l.21) and the light-gun pin. - **Mul/div `$4202-$4206`, results `$4214-$4217`**: hardware multiplies over 8 CPU cycles and divides over 16, one step per cycle, so reading early returns a partial result (ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `aluEdge()` implements the shift-add/shift-subtract steps) [emu; fullsnes "wait 8 clk cycles" / "16 clk cycles" [doc]; undisbeliever `wrmpyb-in-flight` probes the rewrite-while-busy case [probe]]. Division by zero: quotient `$FFFF`, remainder = dividend (fullsnes l.2823) [doc]; **MAME 0.285 returns remainder `$000C`** (`mame/src/devices/cpu/g65816/g65816.cpp` `wrdvdd_w`) and computes both results instantly: a detector weakness. - **HTIME/VTIME `$4207-$420A`, TIMEUP `$4211`**: the IRQ timer (timing.md). - **MDMAEN `$420B`, HDMAEN `$420C`**, the channel registers `$43x0-$43xB`. - **MEMSEL `$420D`**: FastROM. - **RDNMI `$4210`** (bit 7 NMI flag, cleared by the read; bits 3-0 CPU version), **HVBJOY `$4212`** (VBlank, HBlank, auto-joypad busy), **JOY1-4 `$4218-$421F`**. ### DMA and HDMA (in the board) Eight channels. General-purpose DMA starts on the write to `$420B`, after the current CPU cycle, aligned to the 8-clock DMA divider; it moves one byte per 8 master clocks between an A-bus address and a B-bus register, in one of eight transfer patterns; the CPU is halted (between its bus cycles, never inside one) until it ends. HDMA runs one table step per visible line for each enabled channel, at H~278 dots, with an initialisation at V=0 and a per-line overhead; HDMA preempts a running DMA. All of this is in timing.md "DMA" with the cycle counts and their sources. **The board owns DMA, not the core**: the core only has to let the board take the bus between two of its cycles. ### WRAM and WMADD `$2180` WMDATA reads or writes WRAM at the 17-bit address in `$2181-$2183` and increments it; it is a B-bus register, so a DMA from cartridge ROM (A bus) to `$2180` (B bus) copies ROM into WRAM. "WRAM-to-WRAM DMA isn't possible (neither in A-Bus to B-Bus direction, nor vice-versa) ... the WRAM chip is unable to process both at once" (fullsnes l.408-410) [doc]; what the transfer then writes is not stated there (open item U3). `$2180` reads have no prefetch (fullsnes l.397-400). WRAM is DRAM: the refresh pause is its cost. ### Controllers Two ports, each a clock line, a latch line (`$4016` bit 0 write, shared) and two data lines (`$4016`/`$4017` bits 0-1). The standard pad is a 16-bit shift register (B, Y, Select, Start, Up, Down, Left, Right, A, X, L, R, 4 ID bits), 1 after the 16th bit. With auto-read enabled, the S-CPU latches and clocks both ports at the start of VBlank into JOY1-JOY4 over 4224 master clocks, during which `$4212` bit 0 is set (anomie timing "Auto Joypad Read", start "between dots 32.5 and 95.5" of the first VBlank line, every 256 clocks phase-aligned) [doc]; ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `joypadEdge()` clocks it every 128 master clocks [emu]. undisbeliever's `auto-joypad/` probes (14 ROMs, source only) and rainwarrior's `ctrltest` are the timing probes. Mouse, Multitap, Super Scope, Justifier are later peripherals behind the same port protocol (`wiki/Mouse`, `wiki/Multitap`, `wiki/Super Scope`). ## The PPU (S-PPU1 + S-PPU2) ### Programming model, briefly | Register | What | | --- | --- | | `$2100` INIDISP | force blank, brightness | | `$2101` OBSEL | sprite sizes and name base | | `$2102-$2104` OAMADD/OAMDATA | OAM (544 bytes: 512 low table + 32 high table) | | `$2105` BGMODE | mode 0-7, BG3 priority, tile sizes | | `$2106` MOSAIC | | | `$2107-$210C` | BG map and tile bases | | `$210D-$2114` | BG scroll (write-twice latches; mode 7 shares `$210D/$210E`) | | `$2115-$2119` | VRAM increment mode / address / data (word-addressed, address remapping) | | `$211A-$2120` | mode 7 settings and matrix (write-twice latch) | | `$2121-$2122` | CGRAM address / data (256 x 15-bit colours) | | `$2123-$212B` | window positions and logic | | `$212C-$212F` | main/sub screen designation, window masks | | `$2130-$2132` | colour math control, fixed colour | | `$2133` SETINI | interlace, overscan (239 lines), pseudo-hires, OBJ interlace, mode 7 EXTBG | | `$2134-$2136` | mode 7 multiply result (signed 16 x 8) | | `$2137` | latch H/V counters | | `$2138-$213D` | OAM, VRAM, CGRAM read ports, H/V counter reads | | `$213E-$213F` | STAT77/STAT78: time/range over, versions, field, NTSC/PAL, latch flag | `wiki/PPU registers`, `wiki/MMIO register table/PPU`, fullsnes "SNES PPU" chapters [doc]. The parts that make the SNES hard, each a section of timing.md or undocumented-behavior.md: **modes 0-7** (2/4/8-bpp layers, priorities per mode, `wiki/Background modes`, `wiki/Priority`); **mode 7** (affine math with 13-bit signed scroll/centre, the 8.8 matrix and the PPU's own truncations; `wiki/Mode 7 transform`; the `$2134` product doubles as a fast signed multiplier); **windows** (two windows, per-layer OR/AND/XOR/XNOR, `wiki/Windowing`); **colour math** (main/sub screens, add/subtract, half, clip/prevent regions, fixed colour; `wiki/Color math`); **mosaic**; **offset-per-tile** (modes 2, 4, 6: BG3's map supplies per-column scroll for BG1/BG2; `wiki/Offset-per-tile`); **hi-res** (modes 5/6: 512 dots from main and sub screens interleaved) and **pseudo-hires** (SETINI bit 3 in any mode); **interlace** (448/478 lines, field bit); **sprites** (128 entries, 32 per line "range over", 34 8x1 slivers per line "time over", `$213E` bits 6/7; `wiki/Sprites`, `wiki/OAM layout`). ### Access rules (the board must enforce, not just document) - **VRAM, OAM and CGRAM** "can be accessed only during V-Blank, or Forced Blank" (fullsnes l.240, l.770) [doc]; what a VRAM access during active display does (lost write, which byte a read returns) is the board's to settle against probes (undisbeliever `vram-mid-scanline-test`) and the detectors. - **OAM** writes during active display go to an address the PPU is using at that moment (the "OAM address invalidation" quirk); the OAM address resets to OAMADD at the start of VBlank if not force-blanked. **CGRAM** writes during active display land at the address the PPU is reading (fullsnes, `wiki/Errata`) [doc]. Games that rely on these exist (`wiki/Tricky-to-emulate games`). - **Mid-frame writes**: scroll, mode, window, colour-math and INIDISP writes take effect at the dot they land on; HDMA exists to make this deliberate. A scanline renderer cannot pass the HDMA test ROMs: the PPU here is drawn dot by dot (as famicom-python and gameboy-python do), from the start. - **Open bus per PPU**: reads of write-only PPU registers return the CPU's MDR; the unused bits of PPU1 reads (`$2134-$2136`, `$2138-$213A`, `$213E` bit 4) return PPU1's own latch, those of PPU2 reads (`$213B-$213D`, `$213F` bit 5) PPU2's latch (fullsnes l.1357-1397) [doc]. Two latches, one per chip, besides the CPU MDR. ### Output format A frame is up to 512 x 478 **15-bit BGR values after brightness**, i.e. the value S-PPU2 sends to the DAC. In normal modes each pixel is doubled horizontally to 512 or kept at 256 with a flag; pseudo-hires and modes 5/6 fill 512 distinct values. Keep the raw 15-bit value and the per-line "hires" flag in the frame; colour conversion is the frontend's. The suites compare pictures in 8-bit RGB: MAME writes full blue as 247 where krom's pictures have 248 (measured 2026-10-21), so the harness compares at 5 bits (`scripts/mame_screens.py`). ## The APU side (an S-SMP module and an S-DSP module in this board) - **S-SMP = `spc700-python` + the board's thin `smp` module** (Aubrey, 2026-10-21; it wraps `spc700_python.SPC700`, prices wait states from `cpu.opcode`/`cpu.cycle`, and shares ARAM with the DSP as one `bytearray`). The core is the SPC700 CPU only (sibling repository, `../spc700-python`). The board owns the rest of the S-SMP die: the 64-byte IPL ROM overlay at `$FFC0` (bring your own: it is Nintendo's; MAME's `snes.zip` holds it as `spc700.rom`, 64 bytes, SHA-1 `97e352553e94242ae823547cd853eecda55c20f0`), the CONTROL (`$F1`), TEST (`$F0`) and DSP address/data (`$F2/$F3`) registers, AUXIO (`$F8/$F9`, RAM-like), the four ports (`$F4-$F7` on the SMP side, `$2140-$2143` on the CPU side, each direction its own latch), and **three timers**: T0/T1 at 8 kHz, T2 at 64 kHz, each an 8-bit target and a 4-bit up-counter read at `$FD-$FF` and cleared by the read (fullsnes "SNES APU SPC700 I/O Ports" [doc]; blargg's `test_timer_*` probes, his `notes.txt` table of timer periods per TEST speed setting [hw-observed, by blargg, 2010]). - **S-DSP = an in-board `dsp` module.** Decided by Aubrey 2026-10-21: the S-DSP stays in this repository (SNES-only, so no separate core), behind a pin-shaped interface with an import-boundary test, like gameboy-python's APU. Its research (registers, BRR, envelopes, echo, the 32-step schedule, the interface) is in [`docs/dsp/`](dsp/README.md); this document stays at the integration level. The board-facing facts: the DSP owns ARAM (every S-SMP RAM access goes through it, interleaved with its own two accesses per SMP cycle), is clocked once per S-SMP clock, and is reached by the SMP through `$F2/$F3` only ([`dsp/interface.md`](dsp/interface.md)). Both run at 1.024 MHz (SMP) / 32 kHz (DSP samples) from the 24.576 MHz resonator. The board's output is `take_samples()` at 32 kHz stereo, resampled by the frontend. ## Boot and BIOS The S-CPU has no boot ROM: it reads the reset vector at `$00:FFFC` from the cartridge. The only Nintendo ROM inside the console is the **S-SMP IPL ROM** (64 bytes). It is bring-your-own, by path or from a MAME `snes.zip`, read in place, checked by SHA-1; never committed. For headless .spc playback and tests a "skip IPL" path loads an S-SMP state directly. Several emulators carry the IPL bytes in their source; this board must not copy them from any. ## A suggested module shape ``` src/snes_python/ machines.py # the machine table above, as data board.py # the A bus: decodes each 65816 cycle, chooses its length, open bus clock.py # master-clock timeline, H/V counters, refresh, the APU clock ratio cpu_io.py # $4200-$421F: NMI/IRQ timer, mul/div, WRIO latch, joypad auto-read dma.py # 8 channels, GP-DMA and HDMA, the DMA divider bbus.py # B-bus decode, WMDATA/WMADD ppu/ # dot-accurate: VRAM/OAM/CGRAM, BG fetch, sprites, windows, colour math, mode 7 smp/ # S-SMP side: IPL overlay, the $F0-$FF page, timers, ports; wraps spc700_python.SPC700 dsp/ # S-DSP behind its pin interface (docs/dsp/), imports nothing from the board; # python -m snes_python.dsp = the DSP-only replay host / .spc player cartridge/ # maps, SRAM, coprocessors behind the connector protocol (cartridge.md) controllers.py state.py, debug.py, trace.py, frontend.py ``` `dsp/`, `cartridge/` and the coprocessor modules import nothing from the rest of the board; a test enforces it.