# Validation: every oracle, its tier, and the plan ## Oracle tiers The family's rule (z80-python, sm83-python `docs/validation.md`): rank an oracle by **where its expected values came from**; a lower tier detects, the highest tier that covers a claim decides it. | Tier | Meaning here | | --- | --- | | **hardware-captured** | values recorded from a real 5A22/65C816 (logic-analyser captures in `SNES_MiSTer/hw/LA captures`: images of bus phases, none per instruction) | | **hardware-verified** | a self-checking ROM whose author states it passes on a real SNES (krom's CPU tests: "I only test with a real SNES using a SD2SNES Cartridge", `PeterLemon-SNES/README.md`) | | **manufacturer** | WDC's W65C816S datasheet (2024-03-13): the designer's description, cycle by cycle; known misprints (undocumented-behavior.md C6) | | **self-checking, hardware run unstated** | gilyon's cputest: expected values from his own generator (`make_cpu_tests.py`), written to run on a SNES and describing behaviours "tested" that only hardware could have shown, but the repository does not say on what it passed | | **research documentation** | fullsnes, the SNESdev wiki, 6502.org tutorials (Bruce Clark's decimal and opcode notes) | | **emulator-derived** | values produced by an emulator or hand-written HDL: SingleStepTests 65816 (Clock Signal), bsnes, ares, Mesen2, MAME, srg320's P65C816 VHDL. Detectors only | | **unknown** | generator not stated. None used | There is **no die-traced or transistor-level 65C816** (searched 2026-10-21: visual6502 has the 6502 and 6800; no 65816 netlist or die-derived HDL was found), and no hardware-captured per-instruction corpus. The bus schedule therefore rests on the datasheet, checked against SST; semantics on krom and gilyon plus agreeing emulators; the contested points (C1-C5) stay contested until someone puts a logic analyser on a 5A22. ## The oracles ### WDC W65C816S datasheet: manufacturer, the bus schedule - Pin: `reference/docs/w65c816s.pdf`, revision dated March 13, 2024, SHA-256 `b9177e1b…`, live URL (WDC replaces it in place), 1.5 MB. - Table 5-7 (pp. 36-42) gives every addressing mode's cycles with VPB, MLB, VDA, VPA, RWB, address and data; notes 1-17 (p. 43) the conditional cycles; Table 5-4 the opcode matrix; §7 the emulation-mode addressing rules; Table 7-1 the differences from the NMOS 6502 and the 65C02. - `scripts/datasheet_timing.py` transcribes Table 5-7 literally. ### SingleStepTests `65816`: emulator-derived, the volume detector - Pin: `SingleStepTests/65816` @ `307c63f5673c8c6b861c52f71ddb89212a227480` (2026-10-16), MIT, 2.76 GB checked out (`v1/*.json`). - **512 files**: each opcode in emulation (`XX.e`) and native (`XX.n`) mode, 10,000 cases each, MVN/MVP 100 each (whole moves of at most 1,000 bytes). Each case: initial and final `pc s p a x y dbr d pbr e`, sparse 24-bit RAM, and `cycles`, one `[address, value, "dpvrexml"]` per cycle (VDA, VPA, VPB, RWB, E, M, X, MLB); `value` is null when neither VDA, VPA nor VPB is active; WAI/STP end with a `[null, null, "--------"]` row. - Provenance: generated by Thomas Harte's emulator Clock Signal (`TomHarte/CLK`, pinned: `OSBindings/Mac/Clock SignalTests/ 65816ComparativeTests.mm` l. 628 "Generates an entirely new set of 65816 tests", file names `%02x.%c.json`). Emulator-derived; corrected by hand after outside reports (C7). - Measured here (2026-10-21), every case, against the literal datasheet (timing.md): cycle counts **5,080,400 / 5,080,400**; VDA/VPA/VPB/RWB/MLB on every cycle 5,020,400 (disagreeing only on `42`, `7C`, `FC`: C1, C2); addresses of access cycles 5,079,305 and of IO cycles 5,080,202, every disagreement an emulation-mode wrap (U1, U2). Its emulation-mode initial `s` values need SH forced to $01 before use. - Known gaps: no interrupts, ABORT, RDY or reset; no `(d)`/`(d),y`/`[d]` case with E = 1, D.l = 0 and the operand at $FF; implements U3 the other way (C4). - Use: rung 1, with C1/C2 kind masks and C4's cases excluded and counted. ### krom (Peter Lemon) SNES CPU tests: hardware-verified - Pin: `PeterLemon/SNES` @ `350b394e86ec` (2022-09-19), no licence stated, sparse `/CPUTest/`, `/LIB/`. 23 ROMs `CPUTest/CPU//CPU.sfc` (ADC AND ASL BIT BRA CMP DEC EOR INC JMP LDR LSR MOV MSC ORA PHL PSR RET ROL ROR SBC STR TRN) with bass sources and a reference `.png` each. - Each runs instructions over chosen inputs in 8- and 16-bit and BCD modes, compares the result and NVZC with values stored in the ROM, and draws PASS/FAIL per row through the PPU. `CPUMSC` covers NOP, WDM, BRK, COP, WAI, STP ("Please reset to pass STP"). - Run here on MAME 0.285 (`scripts/mame_trace.py ROM --no-trace --seconds 5`, 2026-10-21): the screen at 5 s of **all 23 shows PASS on every row**, no FAIL (each ROM's last screen only; montage inspected by eye). - Needs the PPU to show its result: rung 3 reads the text from VRAM writes in a minimal harness (below). ### gilyon `snes-tests` cputest: self-checking, hardware run unstated - Pin: source `gilyon/snes-tests` @ `5ecdf555da92` (2025-06-10), MIT; built ROMs `snes-tests.zip` release v1.4 (SHA-256 `c3a39e15…`, as GitHub publishes it), unzipped into `reference/roms/snes-tests-v1.4/`. - `cputest-full.sfc` runs **1,610** generated tests (`tests-full.txt`: each test's instruction, input registers and memory, expected outputs), `cputest-basic.sfc` 1,107 (skips emulation-mode wrapping). Every opcode except STP and WAI, every addressing mode, wrapping with and without emulation mode, decimal mode incl. invalid BCD, the U3 and C5 quirks. Not tested: cycle counts, interrupts, MMIO. Result: "Success" or "Failed" and a hex test number, drawn through the PPU. - Run here on MAME 0.285 (30 emulated seconds): **full: "Success", test number $0649 (= 1,609, the last); basic: "Success", $0452 (= 1,106)**. So MAME 0.285 passes every gilyon test, including U3. - Use: rung 2, in the minimal harness, success detected from the program reaching its success path (`tests-full.map` gives the addresses). ### undisbeliever `snes-test-roms`: hardware tests for the board - Pin: `undisbeliever/snes-test-roms` @ `ac6ef8006809` (2026-05-01), Zlib; release 2021-02-17 (SHA-256 `00945927…`): 29 built ROMs, all PPU/DMA/HDMA glitch tests (`hdmaen_latch_test`, `scpu-a-dma-bug-*`, `inidisp_*`). - Newer sources (`src/hardware-tests/`: `wrmpyb-in-flight`, `reset-position-test`, `auto-joypad`, `hdma-timing-demonstration`) need his toolchain; not built here. Results are described from his hardware runs in the sources. All board rungs (6), none board-free. ### bsnes, ares: emulator-derived, the reference of SNES emulation - bsnes: https://github.com/bsnes-emu/bsnes @ `78474e5d39f58872dc21aca18cf9eb583b147fe7` (2026-10-07), GPL-3.0-or-later for the whole program, `processor/` included (docs/provenance.md). Closed box only: its source is not fetched (Aubrey, 2026-11-24); it was read before the 2026-11-14 rule, and what was taken from that reading is marked HYPOTHESIS where it is cited. - Pin: `ares-emulator/ares` @ `9408cb43d494` (2026-10-06, ISC), read for the cycle placement (the poll before the last cycle), the E-mode RMW (C3) and the S-CPU around it. The S-CPU's memory timing (the 6/8/12 table) and DRAM refresh (40 clocks per line) are fullsnes's and SNESdev *Timing*'s. - Not built here: neither has a headless test runner in the sparse checkout, and their verdicts are the ROMs' verdicts above. ### Mesen2: emulator-derived, an independent second reading - https://github.com/SourMesen/Mesen2 @ `b9fa69ddc6d0a331fb103fdb5eef6904305703c2` (2026-06-04), GPL-3.0. Closed box only: its source is not fetched (Aubrey, 2026-11-24). Read before the 2026-11-14 rule, it was taken to agree on U3 and the interrupt sample point (HYPOTHESIS, for review); runs of it are snes-python's. ### MAME 0.285: emulator-derived, the real-game detector - Pin: `mamedev/mame` tag `mame0285` = `f379cd060ac3` (2026-01-29), the source of `/usr/games/mame` 0.285 here. `g65816` (Karl Stenerud's per-instruction core, one variant per M/X/E combination in `g65816o0`-`o4.cpp`, cycles charged by `CLK()` per instruction, not a bus-cycle model) and the 5A22 device; snes driver. - Passes krom's 23 and gilyon's full/basic (above). Its cycle totals are master clocks from a per-instruction table plus a per-access region charge (`bus_5A22_cycle_burst`); it does **not** model DRAM refresh (no refresh in `snes_m.cpp`). So its instruction stream and register values are a detector; its cycle deltas are a detector only between refreshes. - [mame-oracle.md](mame-oracle.md) has the recipe that boots commercial games without Nintendo's S-SMP boot ROM. ### srg320's P65C816 (MiSTer SNES core): emulator-derived HDL - Pin: `MiSTer-devel/SNES_MiSTer` @ `23026830ff81` (2026-10-04), GPL-3.0, sparse `rtl/65C816/`, `rtl/CPU.vhd` (the 5A22), `hw/LA captures/`. - VHDL written from documentation and tests and proven on FPGA against games; not traced from a die. Exposes A_OUT[23:0], VDA, VPA, VPB, MLB, WE, RDY_OUT per cycle. - Run here: `scripts/build_referees.py p65c816` analyses all 7 units with GHDL 7.0.0-dev (oss-cad-suite 2026-10-05) without errors, and `p65c816-tb` builds the rung 5 testbench (results below): it votes with the datasheet on C1, C2 and C4, with neither on C3, and departs from the datasheet and SST together on U1, emulation-mode RTI, MLB on `a,x` RMW and every IO cycle's address. - `hw/LA captures/`: 11 logic-analyser screenshots from a real SNES (S-CPU and S-SMP reset timing, WAI during Mode 7, HDMAEN latch, SA-1 ROM/WRAM access): hardware-captured, but of board phases, not CPU instructions. ### Research documentation - fullsnes (`docs/fullsnes.htm`/`.txt`, fetched 2026-10-21, live): memory map with speeds, MEMSEL, DMA/HDMA, the 5A22 registers, CPU vectors ("ABORT ... not used in SNES"). - SNESdev wiki at pinned revisions: *Timing* (1467), *DMA registers* (1503), *Memory map* (1504), *Errata* (1511), *65C816* (1413). - 6502.org (Wayback captures, gzip): Bruce Clark *65C816 Opcodes*, *Decimal Mode* (Appendix A: the 65C816's V and invalid-BCD results), BigDumbDinosaur *Investigating 65C816 Interrupts*. - WDC W65C02S datasheet: what emulation mode keeps from the 65C02. ### Not available - Eyes & Lichty, *Programming the 65816* (WDC's programming manual): sold in print and Kindle only; no free copy. The datasheet covers the bus; the 6502.org notes cover the semantics it would add. - Any die-level model, and any hardware-captured instruction corpus. - Nintendo's S-SMP boot ROM: not needed (mame-oracle.md). ## The plan, in oracle-tier order Each rung is cheaper than the next and earns one claim. Do not reorder. | Rung | Gate | Tier | Claim earned | | --- | --- | --- | --- | | 0 | Generated opcode table == `datasheet_timing.ROWS` (Table 5-4); for every opcode, mode (E, M, X) and path, the core's per-cycle (address, VDA, VPA, VPB, RWB, MLB) == `datasheet_timing.schedule` on random states, with the U1-U3 wraps layered on and C1/C2 as decided; readability test | manufacturer | the bus schedule is WDC's | | 1 | SingleStepTests 65816, 512 files, every case: registers incl. E, DBR, PBR, D; RAM; every cycle's address, value and VDA/VPA/VPB/RWB/E/M/X/MLB; masks for C1, C2; C4's cases excluded and counted | emulator-derived (detector, high volume) | agreement with Clock Signal everywhere except where a higher tier says otherwise | | 2 | gilyon cputest-full and -basic in `harness`: the success path reached, 1,610 / 1,107 tests | self-checking (hw run unstated) | semantics incl. wrapping and decimal mode as the cputest author's model (and MAME, bsnes, Mesen2) has them | | 3 | krom's 23 CPU tests in `harness`: every row PASS, decoded from VRAM writes | hardware-verified | instruction results as krom's SNES gave them | | 4 | MAME replay of real games (mame-oracle.md "Use"), I/O reads fed from MAME's log: per-instruction registers, and per-instruction CPU-cycle counts computed from the core's callbacks vs MAME's deltas between refreshes | emulator-derived | long real-code agreement; every divergence explained by a higher tier | | 5 | P65C816 VHDL testbench from SST states: per-cycle kinds on all opcodes, esp. C1-C3 | emulator-derived (independent HDL) | a third opinion where datasheet and corpus disagree | | 6 | Through snes-python: interrupt timing, WAI, DMA/HDMA halting, DRAM refresh, MEMSEL, U9; undisbeliever's hardware tests; games vs bsnes/Mesen2 | hardware-verified / emulator | the core inside the 5A22 as games see it | ### Results (2026-10-23, the core at `src/w65c816`) | Rung | Status | Measured | | --- | --- | --- | | 0 | **passed** | `tests/test_tables.py`: all 256 opcodes name Table 5-4's mnemonic and mode. `tests/test_schedule.py`: for every opcode in E = 1 and in E = 0 with each M/X pair, random states biased to the conditional cycles' paths run through the core and through `tests/datasheet.py` (Table 5-7 transcribed, with U1-U3, C1-C3 and C5 layered on): every cycle's address, VDA, VPA, VPB, RWB and MLB and the count agree, 24 states per opcode and mode in the quick loop (30,720), 2,000 under `-m slow` (2,560,000). States where the instruction writes over its own bytes are drawn again (the transcription reads them from the initial memory). Mutation-checked: removing U1, U2, U3 or note 4's store cycle from the fixture fails the quick run. `tests/test_readability.py`: every handler's headline names its mnemonic and cites its own page of DS Table 5-5 (pp. 32-34), lives in its group's module, and all 92 mnemonics have one | | 1 | **passed** | `scripts/run_single_step.py` (24 processes, PyPy 3.11) on SingleStepTests 65816 @ `307c63f`: **5,080,136 of 5,080,400 cases pass** on registers (incl. E, DBR, PBR, D), RAM, and every cycle's address, value and VDA/VPA/VPB/RWB/E/M/X/MLB; the other **264 are C4's**, excluded as decided; none fail. Masks: C1 (`7C`, `FC` pointer reads' VDA/VPA), C2 (`42` cycle 2's kind), C9 (`40`'s E/M/X pins after the P pull; 7,486 cases of `40.n` differ there). `pytest -m "not slow"` runs 100 per file (51,200) | | 2 | **passed** | `tests/test_cputest.py` (`-m slow`) in `w65c816.harness`: **cputest-full "Success" at test $0649 (1,610 tests), cputest-basic "Success" at $0452 (1,107)**, the verdicts the ROMs print, read back from VRAM; 4.9 and 3.4 million cycles, 40 s and 16 s on PyPy. Mutation-checked: the core without U3 stops at full's test $0027 with "Failed" | | 3 | **passed** | `tests/test_krom.py` (`-m slow`) in `w65c816.harness`: **all 23 of krom's CPU tests print PASS on every row, 977 rows**, no FAIL. Pages are cleared and reprinted, so the verdict is read from the VRAM writes: each `PrintText(Pass, $DEST, 4)` in the source must have printed "PASS" at `$DEST` as often as the source has it, and no Fail destination may show "FAIL" (a plain count of "PASS" would be fooled by CPUMSC's banner "Please Reset To PASS STP"). CPUMSC stops at STP and is reset once, WRAM kept, to reach its STP row. 12 s for all 23 on PyPy. Mutation-checked: ADC without decimal mode fails the ADC ROM at its first BCD row (4 of 120); CPUMSC without the reset misses its sixth row | | 4 | **passed** | `tests/test_mame_replay.py` (`-m slow`; `tests/mame_replay.py`) on seven commercial games, 10 emulated seconds each from power-on, traced by MAME 0.285 with every I/O and cartridge-RAM read logged (`scripts/mame_trace.py --io`, plus `--io-banks` for the mirror banks a game uses): Super Mario World, Super Metroid, A Link to the Past, F-Zero, Chrono Trigger, Final Fantasy III, Donkey Kong Country (LoROM and HiROM, SlowROM and FastROM, 3,160 NMIs, 2,277 IRQs). **55,576,306 instructions, zero register divergences** (PB PC A X Y S D DB P E after every instruction), zero reads answered by guesswork, and **every instruction's length in master clocks equal to MAME's or different from it exactly as MAME's own cycle constants predict** (`mame_extra_cycles`: XBA, RTI, JMP (a,x), and indexed modes' page-crossing and 16-bit-index cycles; 1,144,508 instructions), none unexplained; 15,688 steps holding DMA, WAI or an interrupt entry not timed. What the replay adjusts for MAME, each counted: its NMI entry leaves I clear (2,026 times), its MVN/MVP leave A unmasked, its WRAM powers up $55, and it took 30 interrupts a boundary before the core's poll would (after CLI/PLP/REP or an entry; bsnes's rule is the core's: rung 6 decides). Found on the way: the harness mapped HiROM banks $FE-$FF as WRAM (fixed). Mutation-checked on Super Mario World's first 1.5 million: XBA setting N and Z from all 16 bits diverges (5 at the cap), and dropping the XBA cycle rule leaves 85,769 steps unexplained. 1.5-4 min per game on PyPy | | 5 | **passed** | `tb_p65c816.vhd` (a GPL tool kept outside this repository since 2026-11-22, `$EMU_TOOLS_GPL/tools/65816-python/`) runs srg320's P65C816 under GHDL 7.0.0-dev (LLVM; `scripts/build_referees.py p65c816-tb`) on SingleStepTests initial states, each loaded by a loader program after a reset (`tests/p65c816.py`: TCD, PLB, a PEA that the final PLP pulls, 16-bit LDX/LDY, SEC/XCE for emulation mode, A, JML), and logs every cycle of the case's instruction; the core and the corpus give theirs, and each category of difference (cycle count, read/write, VDA, VPA, VPB, MLB, value, data address, IO-cycle address) is put to a vote. `scripts/run_p65c816.py` on **508,400 cases (1,000 per file, all 512 files, 2 min on 24 PyPy processes): the HDL never sides with the corpus against the core**, and the three never all differ. **The corpus stands alone only on C1 (`7C`/`FC` pointer reads, 4,000 cases), C2 (`42`, 2,000) and C4 (26 cases): P65C816 votes with the datasheet and the core on all three.** On C3 it sides with neither (an IO cycle at the address plus one; undocumented-behavior.md C3). Where the core and the corpus agree and the HDL alone differs, it is P65C816's own behaviour: IO cycles at its own addresses (230,805 cases; nothing reads them), the emulation-mode direct page wrapped whatever D.l is (U1), no MLB on `a,x` read-modify-writes (12 files), emulation-mode RTI in 5 cycles and native RTI's IO cycle after the pulls (`40`), and one emulation-mode MVN using a stale index high byte. `tests/test_p65c816.py` (`-m slow`, 50 cases per file: 512 pass in 17 min on PyPy) gates the first two findings; mutation-checked (a NOP with an extra IO cycle stands alone in all 50 `ea.n` cases) | | 6 | **in progress** | snes-python milestone 1 (its docs/validation.md "Results"): the core on the board with real cycle lengths, refresh, DMA/HDMA, the NMI/IRQ timer. gilyon full/basic "Success", krom's 23 CPU and 5 BANK ROMs PASS; MAME lockstep over the first 2,000,000 instructions: Super Mario World 1,813,618 and Chrono Trigger 1,826,048 aligned, **0 unexplained clock differences**, every resync with a named cause (timing-register reads and interrupts arriving at other instructions, as the two keep different time; the reset's S, U12). Donkey Kong Country 1,432,462 aligned, 0 unexplained clocks, until its game state parts from MAME's at line 1.52 M (a frame running over on one side: MAME charges no refresh or DMA time); bsnes's irqLock and undisbeliever's hardware tests still to do | Interrupts, reset, ABORT, WAI and STP have no corpus cases; `tests/test_interrupts.py` holds them to the datasheet (rows 22a, notes 7 and 10, section 2.25, U8, U11, U12). Rung 6 is the board's acceptance, not this repository's; failures it traces to the core come back here as issues or PRs with a failing core-level test. ### The minimal harness (rungs 2-4) Not a SNES: a LoROM/HiROM map (ROM, 128 KiB WRAM at $7E-$7F and mirrored at $0000-$1FFF of banks $00-$3F/$80-$BF), writes to $2100-$21FF accepted, VRAM writes through $2115/$2116/$2117/$2118/$2119 collected into a 64 KiB array (enough to read krom's and gilyon's text), $4210 RDNMI reading $80 once per "frame" of N cycles and an NMI raised at the same point (both ROMs wait for vblank), $4212 HVBJOY toggling bit 7, the S-SMP ports $2140-$2143 answering the boot handshake ($BBAA) so ROMs that check it proceed, and the stop condition each ROM's map gives. No PPU rendering. gilyon's `main.asm` uses DMA channel 0 twice at start-up (`sta $420B`, lines 133 and 151, to load its font and clear VRAM), polls `$4210`, and ends at its `success:` or `fail:` label (l. 305, 324): a harness can perform those two DMAs instantly between cycles (as the board will, minus timing) and stop at either label. krom's ROMs write their text with CPU stores to $2118 (`PrintText`) after loading palette and font with the `LIB/` macros (`LoadPAL`, `LoadVRAM`), which use DMA too: the same instant-DMA shortcut serves both. Built 2026-10-23 as `src/w65c816/harness.py`. cputest's map exports only the test labels, not `success:` or `fail:`, so the harness reads the ROM's own verdict instead: it stops when the ROM spins on a jump to itself (both end states do; `fail:` first waits for a key, which the test's `until` catches from the "Failed" text) and `Harness.screen()` returns the tilemap's text.