# Timing: one master clock, the bus cycle, and what happens when Everything in this file is in **master clocks** (21.477272 MHz NTSC) unless it says dots (4 master clocks, two 6-clock dots per line) or S-SMP cycles. Tags as in [start-here.md](start-here.md): [doc] research document whose claims rest on experiments we cannot see, [emu] ares's source (ISC, file named) or a closed-box run of another emulator, [probe] a hardware probe ROM whose result on a console is described, not captured. bsnes, Mesen2 and snes9x are never cited for what their source does (docs/restart.md); where an earlier version of this file did, the statement is re-sourced to ares (which shares that code through its author, under ISC) or struck. The console's own results (probe suite, 2026-10-24/25) are [hw] and are in docs/probe-suite/. ## The design: the board advances inside the CPU's bus cycles As on famicom-python and gameboy-python, the 65816 core makes **one bus callback per CPU cycle, in chip order, including internal (no-access) cycles**, and the board advances every other unit inside that callback. What is new on the SNES is that **the cycle's length depends on the address**, so the core must say, for every cycle, what it is doing: | Callback | Cycle length the board charges | | --- | --- | | `read(address, kind)` | 6, 8 or 12 by the start-here.md table (MEMSEL for `$80-$FF`) | | `write(address, value, kind)` | same table | | `idle(address?)` | 6 (internal operation) | `kind` distinguishes opcode fetch, operand, data, stack, vector, dummy read and dummy write (the 65816 makes dummy accesses with defined addresses; a dummy read of `$2139`, `$4210` or `$4211` has side effects). The board needs the kind for its trace and for the VPA/VDA-like signals some coprocessors watch (SA-1 and the CIC-less copiers do not; the S-CPU's own open bus does), not to choose the length. **Where in the cycle the data moves**: ares reads 4 master clocks before the end of a read cycle and writes at the end of a write cycle (ares 9408cb4 (ISC) `sfc/cpu/memory.cpp` `read()`/`write()`) [emu]. The board therefore advances `length - 4` clocks, performs the read, advances 4 more; for a write it advances the whole length, then writes. A PPU register read at the cycle's sampling point is what the H/V-latch and `$4210`/`$4211` race tests measure. **DMA halts the CPU between cycles.** The board, not the core, runs DMA: when a `$420B` write or an HDMA trigger is pending, the board finishes the current CPU cycle, aligns to the DMA divider, runs the transfer on its own timeline (calling the PPU, WRAM and cartridge directly), aligns back to the CPU's clock, and then returns from the callback. The core never sees DMA; it only sees one long callback. The 65816 core therefore needs no RDY input (the famicom-python design, timing.md "DMA inside read_byte"). **Interrupts are lines the core samples.** The board drives `/NMI` (edge) and `/IRQ` (level) and the core samples them at its own point in each instruction (the 65816 samples on the last cycle; `WAI` ends 2 IO cycles after the line falls: anomie timing "Instructions" [doc]). The board needs the core to sample **after** the board has advanced the clock for that cycle, so the board must be able to set the line inside a callback and have it seen at the right cycle (handoff-brief.md "What the board needs from 65816-python"). ## The line, the dot, the frame - **1364 master clocks, 340 dots per line**; dots 323 and 327 are 6 clocks, the rest 4 (anomie timing "S-PPU Rendering", ares 9408cb4 (ISC) `sfc/ppu/counter/ inline.hpp` `hdot()`) [doc, emu]. The SNESdev wiki says "336 dots of 4-clocks, 4 dots of 5-clocks" (`wiki/Timing`) [doc]: **C1**. - **Short line**: NTSC, interlace off, field 1, V=240 is 1360 clocks with no long dots; **long line**: PAL, interlace on, field 1, V=311 is 1368 (`wiki/ Timing`, ares 9408cb4 (ISC) `sfc/ppu/counter/inline.hpp` `tickScanline()`) [doc, emu]. - **Frame**: 262 lines NTSC, 312 PAL, plus one on even fields with interlace on. VBlank begins at V=225 (V=240 with overscan, SETINI bit 2) and ends after V=261/311. Line 0 is rendered but never shown; lines 1-224 (1-239) are the picture (`wiki/Timing` "Scanline"). - **Active picture**: master clock 88 to 1112 of the line (`wiki/Timing`) [doc]; HBlank flag (`$4212` bit 6) set at H=274, cleared at H=1 (anomie timing "Registers") [doc]. MAME 0.285 places HBlank at counter 274 (`mame/nintendo/snes_m.cpp` l.997-999) [emu]. - **H/V counters and latching**: a read of `$2137`, or a 1-to-0 edge on WRIO bit 7 (`$4201`, "1 dot later than" `$2137`), or the light gun, latches H and V into OPHCT/OPVCT (`$213C/$213D`, each read twice: low 8 bits then the 9th bit, the other 7 PPU2 open bus) and sets `$213F` bit 6; reading `$213F` clears the flag and resets the read-twice flip-flops (anomie timing l.15-25, fullsnes l.1370-1400) [doc]. The latch happens at the read's sampling point, so this is the board's sub-cycle clock test. ## The DRAM refresh pause Once per line the S-CPU stops for **40 master clocks** to refresh WRAM: "The CPU is paused for 40 cycles beginning about 536 cycles after the start of each scanline ... the refresh pause begins at 538 cycles into the first scanline of the first frame, and thereafter some multiple of 8 cycles after the previous pause that comes closest to 536" (anomie timing l.68-72) [doc]. ares puts it at 538 at power-on and then, on CPU version 2, at 538 less the DMA divider's phase at the start of each line (version 1: always 530), and models it as five 8-clock steps with a mul/div step after each (ares 9408cb4 (ISC) `sfc/cpu/cpu.cpp` `power()`, `timing.cpp` `scanline()`, `step()`) [emu]. A comment there mentions a logic-analyser pattern; no capture is pinned, so that is not a hardware source here. The refresh is invisible to the CPU except as time; it shifts every later cycle of the line, which the IRQ and H-counter tests see. MAME 0.285 does not model it (its `snes_m.cpp` TODO: "DMA takes some Master CPU clock cycles that aren't taken into account"). ## DMA - **Divider**: DMA and HDMA run on an 8-clock grid counted from reset (ares's `dmaCounter()`, the low 3 bits of the CPU's clock count, ares 9408cb4 (ISC) `sfc/cpu/timing.cpp`) [emu]. - **General-purpose DMA** (`$420B`): after the write's cycle, the CPU is halted; the transfer waits to the next 8-clock boundary, has a fixed overhead (8 clocks start, 8 per channel), moves one byte per 8 clocks, then the CPU resumes aligned to a whole CPU cycle of the length its next access needs (anomie timing "DMA" with three worked examples ending "So this transfer took an extra 6 master cycles", l.230-246) [doc]; ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `dmaEdge()` does the same alignment both ways [emu]. - **Patterns** (`$43x0` bits 0-2): 1, 2, 2x1, 2x2, 4, and the undocumented 5-7 repeats; direction, fixed/increment/decrement A address; B address `$21xx` wraps within `$2100-$21FF`; the A-bus side cannot address `$2100-$21FF`, `$4300-$437F` or `$420B/$420C` (they read open bus or nothing): fullsnes "SNES DMA Transfer" [doc]. - **Count 0 = 65536** bytes; `$43x5/6` counts down and is left at 0. ## HDMA - **Initialisation** at V=0 (about H=6): for each channel enabled in `$420C`, load the table address and first line counter; ~18 clocks overhead plus 8 per direct and 24 per indirect channel (anomie timing "HDMA") [doc]. ares: H = 12 + 8 - (divider phase) on version 1, 12 + (divider phase) on version 2, phase taken at the start of line 0 (ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `scanline()`) [emu]. - **Per line** (visible lines only, V < 225/240): at **dot 278** (H=1104 in ares, ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `scanline()`), after the current CPU cycle: ~18 clocks overhead if any channel is still active, 8 per active channel, 16 to fetch a new indirect address, 8 per byte (anomie timing l.265-273) [doc]. - **HDMA preempts DMA**: a general DMA running when the HDMA point arrives is paused, HDMA runs, the DMA resumes [doc]. Channels used by both collide: the "HDMA and DMA on the same channel" and CPU version 1's "DMA fails if an HDMA to `$2100` happened on the previous line" are hardware bugs with probes (undisbeliever `scpu-a-dma-bug-*`, `dma-ends-at-hdma-start/`, `hdmaen_latch_test*`). - HDMA is turned off for the rest of the frame when a line count of `$00` is read; `$420C` writes mid-frame take effect at the next HDMA point. ## NMI and IRQ - **NMI**: the timer's NMI output goes low at H=0.5 dot of the first VBlank line; `$4210` bit 7 reflects it and its read clears it; clearing `$4200` bit 7 masks it; enabling it while the flag is set fires at once (anomie timing "Interrupts") [doc]; ares polls every 4 clocks with a 4-clock hold (ares 9408cb4 (ISC) `sfc/cpu/irq.cpp` `nmiPoll()`) [emu]. If the CPU is halted for DMA when NMI falls, the handler runs after the DMA, "a 24-30 cycle delay" [doc]. - **IRQ** (`$4200` bits 4/5, HTIME `$4207/8`, VTIME `$4209/A`): "If H=0, `$4211` bit 7 gets set 1374 master cycles after dot 0.0 of the previous scanline. Otherwise, it gets set 14+H*4 master cycles after dot 0.0 of the current scanline ... no IRQ will trigger for dot 153 on the short scanline in non-interlace mode, and no IRQ will trigger for dot 153 on the last scanline of any frame" (anomie timing l.113-122) [doc]. ares compares the counters as they were 10 clocks earlier with HTIME/VTIME and forbids the last dot of a field (ares 9408cb4 (ISC) `sfc/cpu/irq.cpp` `irqPoll()`) [emu]. **The console disagrees with anomie's exception**: the probe suite's IRQN test saw 4 IRQs in 4 frames at (240, 153) and (261, 153) on Aubrey's Super Famicom (2026-10-25) [hw]. The IRQ stays asserted until `$4211` is read or IRQs are disabled. Near's NMI/IRQ/HDMA test ROMs are not public: see validation.md "What is not available". ## The multiplier and divider Each write to WRMPYB (`$4203`) or WRDIVB (`$4206`) starts an 8-step or 16-step operation; one step completes per CPU cycle (6, 8 or 12 clocks, whatever the cycle is), and also per 8-clock slice of the refresh pause. ares steps the unit once per idle, read and write cycle and five times in the refresh pause, and ignores a new start while busy (ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `aluEdge()`, `memory.cpp`, `io.cpp` `writeCPU()`) [emu]. The partial results read early are the shift-add/shift-subtract intermediates. The "writing `$4203` twice too fast gives erroneous result (not emulated)" probe (SNESdev forum t=24087) and undisbeliever's `wrmpyb-in-flight` show this is still being mapped [probe]: open item U4. ## Auto-joypad read Starts in the first VBlank line, between dots 32.5 and 95.5, on a 256-clock grid (dot 74.5 on the first frame), lasts 4224 clocks with `$4212` bit 0 set; JOY1-4 read during it are wrong (anomie timing "Auto Joypad Read") [doc]. ares: start on the first VBlank line at H 130-384 on the 256-clock grid, one step every 128 clocks, 33 steps (ares 9408cb4 (ISC) `sfc/cpu/timing.cpp` `joypadEdge()`) [emu]. undisbeliever's 14 `auto-joypad/` probes measure the start, the clear, and what manual `$4016` access does during it [probe]. ## Interlace, overscan, hi-res - SETINI (`$2133`) bit 0 interlace: 263-line even fields, the field bit in `$213F` bit 7; bit 1 OBJ interlace; bit 2 overscan (239 lines, VBlank at 240); bit 3 pseudo-hires; bit 6 mode 7 EXTBG. - **When SETINI changes take effect** matters for VBlank start (a game that toggles overscan mid-frame moves VBlank) and is probed by undisbeliever's `setini-early-read-*` glitch tests [probe]. ## Two clock domains: the APU The S-SMP and S-DSP run from the 24.576 MHz resonator: one S-SMP cycle = 24 resonator clocks (1.024 MHz), one DSP sample = 768 (32 kHz) (`wiki/Timing` "Audio") [doc]. The board keeps the APU's time as an exact fraction of master time and catches the APU up: - **at every CPU access to `$2140-$2143`** (both directions), before the access is performed (`apu.APU.cpu_read`, `cpu_write`); - at the end of every frame (for audio output). The S-CPU leads and the S-SMP follows, in whole SPC700 instructions (`APU.catch_up` runs while the S-SMP's clock is short of the S-CPU's time), so after a catch-up the S-SMP stands **up to one instruction past** the S-CPU. Each side's port accesses are still placed at their own time [design, as two chips sharing a latch]: an S-SMP access in cycle *c* is made at the cycle's end (S-SMP clock *c* + 1); an S-CPU access at master clock *m* at S-SMP time *m* x ratio. A store is seen by every access of the other side made after it (`smp.CPUPorts`): - **S-SMP stores** are logged with their clock (`CPUPorts.smp_write`), so an S-CPU read made earlier than a store made in the overshoot sees the old value (`CPUPorts.cpu_read`; 36bf2cf, Rendering Ranger R2). - **S-CPU stores** are posted with their S-SMP cycle before the catch-up (`CPUPorts.cpu_post`); the S-SMP's port reads and CONTROL clears consult the posts by their own clock, so an S-SMP read in the store's cycle or later sees it and a clear in the store's cycle or later clears it, even inside the catch-up's last instruction (`smp_read`, `smp_clear`; `settle` applies the rest). A store that arrives while the S-SMP is already past it (an earlier catch-up's overshoot) is still cleared by a CONTROL clear made after it in time (`CPUPorts.cleared`). - **What remains asymmetric:** an S-SMP *read* made in an earlier catch-up's overshoot, before a later S-CPU store's time, has already returned the old value; undoing it would need the S-SMP to pause mid-instruction (a coroutine: the greenlet experiment, 16 % slower, status.md "The port races"). Measured 2026-11-25 over 400 frames: Zenki 0 such reads in 1,008,665 S-CPU stores, Kawasaki (USA) 16 in 87,111, the port-race probe 0 in 984,023 (docs/port-race.md "The overshoot, measured"). With exact rational time the interleaving is deterministic. Before 2026-11-25 the S-CPU's store was applied after the catch-up, so an S-SMP read or clear in the catch-up's last instruction saw it up to that instruction late (the port-race probe's window was one S-SMP cycle long; docs/port-race.md). MAME 0.285 runs its S-SMP device at 24.576 MHz / 12 = 2.048 MHz (`mame/nintendo/snes.cpp` l.1340), i.e. it counts half-cycles of the 1.024 MHz SPC700 cycle; its trace shows 526,292 S-SMP instructions in 2 s of Super Mario World (measured 2026-10-21) [measured, emu]. The **S-SMP timers**: T0/T1 tick at 8 kHz (every 128 SPC cycles), T2 at 64 kHz (every 16); each has an 8-bit compare and a 4-bit output counter read and cleared at `$FD-$FF`; enabling a timer in `$F1` resets its stage. The TEST register (`$F0`) changes the internal and external wait states and the timer clock; blargg measured the resulting timer periods on hardware in 2010 (his `notes.txt` in `blargg_2010-03-14.zip`: e.g. "base = 1 + (1 << clock_speed)", "clocks = base + 15 >> (3 - timer_speed)") [hw-observed, notes only]. The SPC700 core reports cycles; the board's `smp` module applies the TEST wait states: every SMP cycle is a callback (reads and writes with their addresses, internal cycles as `idle()` with no arguments), and the module reads `cpu.opcode`/`cpu.cycle` to price each one (spc700-python `docs/handoff-brief.md` "The bus contract"; Aubrey, 2026-10-21). ## Coprocessor clocks - SA-1: 10.74 MHz (master/2), its own 65C816, sharing ROM/BW-RAM/I-RAM with the S-CPU with bus conflicts (fullsnes "SNES Cart SA-1") [doc]; - Super FX: 10.74 MHz (GSU-1) or 21.47 MHz (GSU-2), from the master clock; - DSP-1..4, ST010/011 (NEC uPD7725 / uPD96050), Cx4 (Hitachi HG51B169), ST018 (ARMv3): their own oscillators (fullsnes gives each chip's), polled by the CPU through status bits or shared RAM; - S-DD1, SPC7110, OBC1, S-RTC: no program, decoders on the CPU's clock. Crystals other than the master clock are more clock domains with the same exact-fraction rule and synchronisation at every shared-register access (cartridge.md "Coprocessors").