# famicom-rust-fast A speed fork of [famicom-rust](https://github.com/alewman/famicom-rust), the Rust Famicom / NES board, held in lockstep with it: every change must leave this board's traces identical to famicom-rust's (which are identical to famicom-python's at `b5dbd2f`, the reference since 2026-11-24, whose `src/` is `c52a6d3`'s, which replaced `b3dbf1b`, `e3e1474` and before them the tag `famicom-rust-pin-1`) on every case, and must measure faster, or it is not kept. The fork point is famicom-rust `553ead2`; the library keeps the name `famicom_rust`, so famicom-libretro switches to it by one dependency line. ```text cargo build --release target/release/famicom ROM.zip --frames 600 --png last.png --wav sound.wav ``` famicom-rust's README holds the board's description and certification (the reference, its pins, rungs 1-6 and the save states); this one, the fork's rule, gate and speed. **Merged from famicom-rust on 2026-11-23** (`1b80370`, `d40f26e`), as they are: the Sunsoft 5B's sound (mapper 69 on ay8910-rust), mapper 559, save states that load all or nothing (format version 2: older states are refused), no build path baked into the library, and famicom-python's provenance rewording. The 5B is ticked every CPU cycle, which costs mapper 69: *Gimmick!* (Japan) on the Famicom ran 409.3 frames a second against 447.8 before (x0.914, best of six alternating 300-frame runs on one pinned core); `scripts/ab.py` on the five bench games (no 5B) gave x0.985, within what code layout moves here (an A/A run is within 1% overall). No 5B speed change was tried: the carry is kept plain. **Merged from famicom-rust on 2026-11-23** (`11a1b7c`): the reference moved to famicom-python `b3dbf1b` (z80-python `b59c80b` by commit, ay8910-python `a3bb485`). famicom-rust regenerated every reference trace against it, byte-for-byte the old caches' (918 of 918), so the caches in `external/` stand; no code changed. **Merged from famicom-rust on 2026-11-23** (`8eb496f`): the Vs. System (UniSystem and DualSystem, mapper 99, the Namco 108 protection chips, the RGB PPUs' rows and colours, the letterless 2A03), with its gate: every Vs. image the reference's `vs_images.py` assembles from `$MAME_ROMPATH` (`scripts/cases-vs.tsv`, `VS_IMAGES_DIR`, famicom-rust's `external/vs-images` by default), the Vs. rows on home test ROMs, the `eval_turn_off` probe on the RP2C03B, save states on every UniSystem image. The fork's PPU fast paths take the new PPU variants unchanged: none of their conditions read the Timing fields the RGB PPUs change (the skipped dot, the register swap and identifier are outside them), and with no OAM corruption on an RGB PPU the pixel dot's mask test is no longer a guard but the only thing between it and a wrong sprite evaluation: a seeded fault there, an equivalent mutant on the 2C02 until now, is caught by the probe on the RP2C03B. The carry cost ordinary games x0.970 / x0.988 / x0.986 (`scripts/ab.py` on the three code layouts, default, 32- and 64-byte block alignment, against `0414c49`), so the board's Vs. branches were moved off the per-cycle path, keeping every cycle: the mainboard's $4020-$7FFF reads and writes and its OUT lines out of line (`vs_read`, `vs_write`, `vs_set_out`, `#[cold]`), the address tested before the mainboard; the watchdog's $4017 time noted in the port read, not on every read (only a DMA's repeats of a stalled $4017 read also pass there, and the CPU's own read overwrites them before the instruction ends, the watchdog being checked between instructions); the external /IRQ tested before the cartridge's call; and famicom-rust's empty `clock` call on the mainboard dropped. After them: x1.006 / x0.998 / x0.997 (12 rounds, 300 frames, the five bench games), within an A/A run's noise (1% overall; Super Mario Bros. 3 on the 64-byte layout x0.976, the one game outside 2%). **Merged from famicom-rust on 2026-11-24** (`ef89507`, merge `ced0fab`): the reference at famicom-python `b5dbd2f` (no file under `src/` changed) and the PlayChoice-10's watchdog case, `pc10-watchdog-Super_Mario_Bros`: a supervisor program of the reference's own (`scripts/inputs/pc10/watchdog.hex`, through `famicom-trace --bios-program`) that lets the watchdog reset the Z80 every boot, which the real BIOS never does. Its reference trace is famicom-rust's, copied to `external/refcache-pc10`. Nothing on the cycle path changed (the tracer's arguments and a unit test), so no A/B was run. **Merged from famicom-rust on 2026-11-24** (`f2e3840`, `f0e8e22`): the PlayChoice-10 (`playchoice.rs`, `supervisor.rs` on z80-rust `7c61e5a`, the PlayChoice-10 row, `Board::idle_cycle`, the cabinet's save states), MAME's BSD-3 notice (`NOTICE.md`), rung 5's table at its cached 600 slices, and the reference at famicom-python `c52a6d3` (no traced code changed, so the caches stand). Its gate here: `scripts/cases-pc10.tsv` (the 56 images the reference's `pc10_images.py` assembles from `$MAME_ROMPATH`, famicom-rust's `external/pc10-images` by default, `PC10_IMAGES_DIR`; the BIOS from `MAME_ROMPATH`), 60 of 60 against the reference traces copied from famicom-rust (`external/refcache-pc10`), and cabinet save states on every image, 56 of 56 (`famicom-pc10-states`). The PlayChoice-10 adds nothing to a console's per-cycle path: its pads are the board's ordinary port devices, its held-2A03 cycle a function of its own. Yet the carry measured x1.002 / x0.989 / x0.998 on the three layouts (default, 32- and 64-byte block alignment, against `eefb681`, 12 rounds of 300 frames on the five bench games; `scripts/ab.sh`, new), and x0.985 when the 32-byte layout was run again, where an A/A run gave x1.003. `Apu::clock` had one caller, the board's CPU cycle, and `idle_cycle` made it two; with `Apu::clock` always inlined and `idle_cycle` cold and out of line: x1.006 / x0.995 / x0.996, within an A/A run's noise on every layout (no game below x0.989). That the second caller moved the inliner's choice is a hypothesis (the assembly was not compared); the two attributes are kept because they measure no worse on any layout. The cabinet itself was not made faster: it runs the Z80 and the game an instruction at a time, as famicom-rust does. ## Speed **x1.133** over the fork point, measured directly (`scripts/ab.py`, 12 alternating rounds of 600 frames a game): on one core of this loaded shared machine, 463.7 frames a second against 403.0 for Super Mario Bros. (NROM), 464.8 against 404.1 for The Legend of Zelda (MMC1), 424.1 against 370.9 for Super Mario Bros. 3 (MMC3), 369.3 against 328.8 for Castlevania III (MMC5) and 387.7 against 352.6 for Akumajou Densetsu (VRC6, its sound on the Famicom): x1.100 to x1.151 a game, 6.1 to 7.7 times real time (60.10 Hz) where the fork point ran 5.5 to 6.7. | Kept | A/B against the build before it | | --- | ---: | | The APU's frame counter: no scan of its step table inside a run of cycles known to hold no step (`#[serde(skip)]`, found again after a load) | x1.032 | | The PPU's pixel dot (a visible line's dots 2-256, rendering on and settled) straight from `step`, without `dot_work`'s and `render`'s tests for the other dots | x1.067 | | `run_to` takes that pixel dot itself when no register effect is due on it | x1.030 | The three multiply to x1.134; measured directly, x1.133. Their A/Bs were taken with a bench that spent about a tenth of its run on its own checksum (fixed in `famicom-bench` since), so each understates a little. Tried and not kept, with their numbers: [docs/not-kept.md](docs/not-kept.md). **Where the time goes.** At the fork point (five games, 9,008 samples): `dot_work` and `render` held the PPU's dot (35% and 23% inclusive), the APU's cycle 15%, the background fetches 11%, the pixel 10%. Now (7,125 samples), by self time: `run_to` with the pixel dot inside it 8.7%, the pixel 8.6%, `Apu::clock` 8.3%, sprite evaluation 6.9%, the background fetch 4.7%, the CPU bus (`Board::read`, `read_byte`, `begin_cycle`, `end_cycle`) 12% together, and no source line above 2.3%. Ablations (not identical, only to see where the time is) on Super Mario Bros. 3 at 434 fps: without the background fetches 575, without the APU 517, without sprite evaluation 503, without the pixel 495. The rest is the work itself; the experiments in docs/not-kept.md show its branches and calls already cost little. The next step up would be structural (a PPU that catches up only when the CPU side can see it, or a tile's eight dots at once), not another local change. ## The rule and the gate `scripts/ladder.sh` (about 10 minutes here, four boards at a time under `nice`), no Python: everything the reference ran is cached in `external/` (copied from famicom-rust; gitignored), and the ladder fails on a case without a cached trace. | Step | What | Count | | --- | --- | --- | | `cargo test` | incl. `tests/mapper_coverage.rs`: the reference's 296 mappers build; `tests/state_failed_load.rs`; the reference's unit tests for the 5B, 559 and 208, and its Vs. System tests (`tests/vs_system.rs`, 32) | | | rung 1 | nestest against nestest.log | 8,991 lines | | rung 2 | lockstep, every NROM test ROM (600 slices) and AccuracyCoin's run-all (3,000), `scripts/cases.tsv`; the probes, famicom-rust's own test ROMs (`probes/*.s`, assembled with cc65's ca65 and ld65 at `CC65_DIR`), `scripts/cases-probes.tsv` (`eval_turn_off` on the NES-001 and on the Vs. RP2C03B) | 200 + 2 cases | | rung 5 | lockstep, every board class on dumps (`external/cases-rung5.tsv`), the FDS on three disks (`cases-fds.tsv`), Lagrange Point's VRC7 FM (`cases-lagrange.tsv`, 3,000 slices), the Sunsoft 5B (`scripts/cases-5b.tsv`: rainwarrior's six 5B ROMs, copied under `external/reference/reference/src/nes-audio-tests/build`, and *Gimmick!*), mapper 559 (`scripts/cases-559.tsv`), AutoDisk | 581 + 6 + 1 + 9 + 2 + 3 | | rung 6 | lockstep, the machine rows (`scripts/cases-machines.tsv`, the Vs. rows among them), the peripherals (`scripts/cases-peripherals.tsv`), the host input (`scripts/cases-host.tsv`), the Vs. System (`scripts/cases-vs.tsv`: 43 UniSystem and 22 DualSystem images and a reset, 900 slices with coins, DIP switches, the panel, the Vs. Zapper, `--rgb`) and the PlayChoice-10 (`scripts/cases-pc10.tsv`: 56 images in the cabinet, 1,320 slices, four more cases, and the watchdog case under a supervisor program of our own; `PC10_IMAGES_DIR` and the BIOS in `MAME_ROMPATH`) | 33 + 54 + 36 + 66 + 61 | | rung 3 | the reference's test ROM tables, outcome for outcome (`famicom-testrom`, `famicom-testrom-compare`) | 197 | | rung 4 | AccuracyCoin: 147 of 147 on the reference's 126,984,584 CPU cycles and 4,264 frames | 147 | | save states | `famicom-state-roundtrip`, one dump of every board class (`external/state-dumps.txt`); the 5B on the Famicom (*Gimmick!*, three 5B ROMs) and 559 on the Dendy; every Vs. UniSystem image, saved with a coin's contact closed (`scripts/inputs/vs-state.txt`); `famicom-pc10-states`, every PlayChoice-10 image in the cabinet, saved mid-game | 363 + 5 + 43 + 56 | `famicom-lockstep` is `scripts/lockstep.py --reference-cache` without the reference: it runs `famicom-trace` on each case and compares the trace with `/..ref`. The ladder needs `NES_TEST_ROMS` (default famicom-rust's reference checkout's test ROMs), `FAMICOM_FDS_BIOS` and nes20db at `external/nes20db-2026-09-26.7z` (`FAMICOM_NES20DB`; without it 16 cases identify their boards differently and differ), and pally's palettes under `external/reference/reference/nesdev-wiki/images` (`FAMICOM_REFERENCE`, copied from the reference's checkout; without them the Zapper judges light on a grey palette and three light-gun cases of the host gate differ, while the peripheral cases happened to pass: found 2026-11-08). The ladder refuses to run without either. The probes need cc65 (`CC65_DIR`, default `~/.local/opt/cc65-2.19`). **Seeded faults** (`scripts/seeded_faults.py`): one-line faults in the fork's own code, each built apart and run up the ladder until a rung fails. All six are caught (the frame counter's quiet run over the next step and from before its start; the pixel dot while waking, while dark, without dot 256's fine-Y increment, and taken by `run_to` over a due event), all by rung 2. Three candidates are equivalent and left out, the pixel dot's tests that are guards: `fading` is only set with rendering off; a corruption is spent before any visible pixel dot; and the mask test before sprite evaluation. That last one was first reported here as missed by the whole ladder, a gap in the gate (2026-11-08). It is not (shown 2026-11-09): the one way to have rendering on with the mask clear is a $2001 write of both bits off while rendering, which also arms the OAM corruption, and the pixel dot is refused until a write turns rendering back on. famicom-rust's rung 2 now has a probe written for it (`probes/eval_turn_off.s`), which turns rendering off mid-evaluation 329 times in 600 frames (dots 66-255, both parities) and reads OAM back where evaluation left OAMADDR. With a panic in place of the skipped evaluation, no case in any of the ladder's tables fired it, the probe included. The probe stays as the gate's direct case for that region. Since the Vs. System's merge (2026-11-23) there are ten, all caught (`external/faults-vs.log`, `faults-vs2.log`): the six above by rung 2; the mask test, which an RGB PPU leaves unguarded (it arms no OAM corruption), by the probe on the RP2C03B (`probes/eval-turn-off-vs`); and the three Vs. shortcuts, the mainboard's read taken for too few addresses, the watchdog's $4017 read not noted, the external /IRQ lost, by the Vs. table (*Vs. Ice Climber*, a DualSystem). The fault script had no `FAMICOM_REFERENCE`, so the host gate's light-gun cases ran on greys and failed on any build: a fault that got past the machine rows and peripherals was reported "caught by rung 6 host" whether the gate saw it or not. None of the six earlier faults reached that far; the script now sets it, and the three Vs. faults were run again with it. Since the PlayChoice-10's merge (2026-11-24) there are eight more (`external/faults-pc10-fresh.log`), the PlayChoice-10 table run right after rung 2. Six are caught by it: the stop flip-flop ignoring /GAME STOP, the PROM's data not inverted, the one-monitor screen never flipping, the held 2A03's cycle without the APU's clock, the panel shifting in zeros, and the Z80 losing a tie of the two clocks. Two are not caught by any lockstep table: the watchdog firing on the seventh blank (the real BIOS never lets it count, so no case fires it; the ladder's `cargo test` catches it, `tests/playchoice.rs` `the_watchdog_resets_on_the_eighth_blank`, checked on a fresh build), and the latch's /DISP MASK rule reading SDCS as high when it was low, which only a Z80 reset (the watchdog's, or power) can show: a recorded blind spot, owed a case in the reference first. A first run of these faults shared one target directory and copied the sources with their old timestamps, and one fault's tree was judged with another's build (a unit test passed on a fault it fails on afresh); the script now gives the copies fresh timestamps and each fault its own target directory, and the eight were run again that way. **Every fault again with the fixed script (2026-11-24).** The ten faults run before `ce18b7e` (the six above, the mask test and the three Vs. shortcuts) were judged by a script that could reuse a stale build, so they were run again on `ce18b7e`'s sources with fresh copies and a target each (`external/faults-rerun-ce18b7e.log`): 10 caught, 0 missed, by the same tables as before (rung 2 for the six and `apu-quiet-run-without-its-start`, the probe on the RP2C03B for the mask test, the Vs. table, *Vs. Ice Climber*, for the three); the earlier counts stand. The reference then gained the watchdog case (famicom-python `fa30248`, merged as `ced0fab`), and the PlayChoice-10's eight ran on `ced0fab`'s sources (`external/faults-pc10-ced0fab.log`): **8 caught, 0 missed, all by the PlayChoice-10 table**. `pc10-watchdog-on-the-seventh-blank` and `pc10-latch-sees-sdcs-late`, the two that escaped every lockstep table, are both caught by `pc10-watchdog-Super_Mario_Bros`: the blind spot is closed. In all, 18 seeded faults, 18 caught by the lockstep tables. ## Measuring The machine is shared (load 15 to 50) and `perf` is not permitted. - `famicom-bench ROM... [--frames N]` runs each game on the console it asks for, with the lockstep gate's start-mash input, timing the thread's own CPU time (`/proc/thread-self/schedstat`), and prints frames a second and a CRC of every picture, sample and the cycle count. - `scripts/ab.py A B [--rounds N] [--frames N]` alternates two bench builds game by game on one pinned core and compares each side's best run (other work only ever slows a run down); the checksums must agree. An A/A run is within 1% overall and 2% a game. - `scripts/try.sh NAME [BASE]`: one experiment, built in `target/exp` (so a ladder can keep its binaries), rung 2 as a quick check, then the A/B against `target/ab/BASE`. - The fork point's bench is kept as `target/fork-point/famicom-bench2` (`git archive 4169496` with today's `famicom-bench.rs`, built in `target/fork-point`), for the total measured directly. - `cargo build --release --features profile --bin famicom-profile`, then `famicom-profile ROM... --frames N --out external/profile.txt` samples the program counter on SIGPROF and `scripts/profile.py` folds the samples into functions and lines with `addr2line`, inlined frames included. ## Licence MIT, as the reference.