# play-nes The NES and Famicom in the browser, for . Choose a game (`.nes`, `.fds`, `.qd`, or a `.zip` or `.7z` holding one); it is read on the visitor's device and never uploaded. Recent games, battery saves, a save state per game, saved Disk System disks and the Disk System BIOS are kept in the browser (IndexedDB). `?rom=` fetches one from a site that allows it. The console is [famicom-rust-fast](https://github.com/alewman/famicom-rust-fast), pinned by commit in `Cargo.toml`, unchanged; it is held trace-identical to famicom-rust, which is held cycle for cycle to [famicom-python](https://github.com/alewman/famicom-python). `src/lib.rs` is a thin wasm-bindgen surface. It runs on the page's main thread a frame at a time, paced by the clock, and its sound goes to an AudioWorklet at the context's own rate. Under Node the wasm runs six to seven and a half times real time (Super Mario Bros. 3 449 frames a second, Castlevania III 366), about what the native board does; Lagrange Point, whose VRC7 runs an FM chip, about five times. The cartridges' own sound chips are the board's: the VRC6, VRC7 (ym2413-rust), Sunsoft 5B (Gimmick!, on ay8910-rust), Namco 163, MMC5 and the Disk System's, heard when the console is a Famicom (the board's NES passes no cartridge sound). ## Input: the board's own host model The page does not decide what a key or a click means. It hands the board's `host` module (the input half of famicom-python's window, which famicom-libretro uses too, checked against the window frame by frame on every device) what a desktop window would: up to four controllers by button position, the keys held by the window's pygame names (`KeyZ` is `K_z`), and the mouse in picture pixels with its motion and buttons. `host::apply` then drives the pads and whatever is plugged in, so every peripheral the board has works here as it does in the window: | What | How | | --- | --- | | Pads, Four Score, Famicom four players | arrows, Z/X (or J/K) B/A, Right Shift Select, Enter Start; controllers 1-4 | | Zapper, two Zappers | the mouse aims, the left button fires, the right shoots off screen; on a touch screen, a tap on the picture | | Arkanoid Vaus | the mouse (or a finger on the picture) turns the knob; its button, or the pad's A or B, fires | | Power Pad, Family Trainer | 1 2 3 4 / Q W E R / A S D F | | Family BASIC, Subor keyboards | the keyboard (the pad keys go to the keyboard then) | | Super NES and Subor mice | the mouse; a click locks the pointer, Esc frees it | | Super NES pads | controllers 1-2, X and the shoulders as well | | R.O.B. | its moves shown on the picture | | Famicom microphone | M | The peripheral is the cartridge's own (its NES 2.0 header), or Settings' choice per game. The page's own keys: F5 save state, F7 load it, F2 or F6 reset, P or Pause pause, R or Backspace (held) rewind, F9 sound on and off. A plugged-in Family BASIC or Subor keyboard takes P, R, Backspace, F2 and F9 for itself (F5, F6, F7 and Pause stay the page's). On a controller the left trigger rewinds and the right stick pressed (R3) pauses. ## The family's controls: `web/controls.js` The play-* sites share one control scheme (emulator-lineage `docs/proposals/2026-11-play-controls.md`, decided 2026-11-23) and one small file for it, `web/controls.js`, whose home is here. It holds what is the same everywhere: the page's keys (F2 reset, F5/F7 save/load, F8 slot, F9 mute, P and Pause, R and Backspace held for rewind), controllers read by button position with the hat-switch d-pad of unmapped pads (`createPads`; LT rewinds, R3 pauses), the touch pad worked out from every finger at each touch event (an 8-way d-pad, a 2-way lever or a paddle strip, each owned by the finger that lands on it), the bar's hold-to-rewind, haptics (a Settings switch, off at first; Android only, iPhones cannot vibrate) and letting go of everything on blur. Each site keeps its own button-to-bit table, game keys and help. The other sites copy the file, and its second line names the play-nes commit it came from. Change it here, copy it out with the new commit on that line, and run `scripts/controls-drift.sh`, which says for each sibling checkout whether its copy equals play-nes's file at that commit and whether it is current. Controllers go by the standard mapping's button positions. A pad the browser leaves unmapped (`mapping` "", as Firefox on Windows gives a Switch Pro controller) keeps the same order for its first ten buttons on the common pads, but reports its d-pad as a hat switch on one axis: that axis is found by its rest value (about 9/7, outside -1..1) and read in eighths. A controller that goes dead when Windows names it is usually Steam Input taking it over; quitting Steam gives it back. ## Run-ahead and rewind Both rest on the board's `state::save_snapshot` / `load_snapshot`: the save state's body without its header (ROM hash, version) or zlib, so a frame can afford one. - **Run-ahead** (Settings: Off, 1 or 2 frames). Each frame: the console's real frame runs and gives the sound; then a snapshot, `n` frames more with the same buttons, paint, and the snapshot back (`Console::run_frame_ahead`). A game that answers a button `n` frames late shows the answer in the frame the button went down. Three frames was dropped: the picture jumps more than it helps. - **Rewind** (hold R or Backspace, a controller's left trigger, or the bar's button; a finger on it counts like the d-pad does). A deflate-fast snapshot every 4 frames, about a minute kept, capped at 48 MB (a snapshot holds the picture too); holding loads the newest each frame and forgets it, so it goes back 4x as fast as the game ran, silently, and play goes on from wherever it stops (`Console::rewind_step`). Cost under Node: 4-10% more time a frame; a minute is 6 MB (Super Mario Bros.) to 15 MB (Kirby's Adventure). A save state is the board's, so a board whose state format grew refuses an older one. Board `c7c8ade` (the Sunsoft 5B's sound) writes states of version 2 and refuses every state saved before it ("a state of version 1, not 2"), as `886f96b` (the VRC7's sound) refused older states of VRC7 games. Since `c7c8ade` a refusal leaves the console as it was (`load_state` is all or nothing); before, it could leave it without its cartridge, so the page saves the console first and, on a refusal, starts the game again where it was and says so; F5 then saves a new state. Rewind's snapshots live in memory only, so a new board never meets old ones. `check-browser.mjs` checks both: run-ahead with `RUN_AHEAD=n`, rewind every run (a 2 s hold of Backspace goes back about 8 s, then the game runs on at full speed). ## Included game [Super Tilt Bro.](https://sgadrat.itch.io/super-tilt-bro) by sgadrat (WTFPL; the author: "absolutely no issue redistributing the game"), the itch.io build, in `web/games/`: listed under Games so a phone can play without a file. Its credits are on the About page. ## The console, the palette, the BIOS The console is the header's (NES 2.0 timing: PAL, Dendy), else for an iNES 1 header the file's No-Intro region ("(Europe)" PAL NES, "(USA)" NES), else the board's own choice (a Famicom; the NES for NES-only peripherals). Settings overrides it per game. nes20db, which corrects dumps whose headers are wrong, carries no licence and is not built in. Colours are pally's `2C02G_U_wiki.pal` and `2C07_wiki.pal` ([Gumball2415/pally](https://github.com/Gumball2415/pally), MIT-0), the palettes famicom-python's window uses, built into the wasm (`src/palettes/`). The picture is 256 × 240 at a television's 8:7 pixels; Settings can square them or hide the overscan. The Disk System BIOS is Nintendo's and is not shipped: a visitor chooses their own `disksys.rom` once in Settings, and it is kept on their device. Disk sides change as the game asks (the board's AutoDisk) or from the disk button; a disk the game has written to is kept whole and loaded in place of the original next time. Not here (famicom-libretro has them): the Turbo File's memory and the Data Recorder's tape between visits. ## Build, check, deploy ```text scripts/build-dist.sh # wasm (toolchain 1.93.1, wasm-bindgen 0.2.128, wasm-opt) -> dist/ ROM=game.zip node scripts/check-browser.mjs # headless Chromium: load, run, Start, rewind, the page's # keys, a faked controller (R3, LT, hat), touch in portrait # and landscape, haptics, d-pad scripts/controls-drift.sh # the other sites' copies of web/controls.js against this one ROM=game.zip node scripts/bench-node.mjs # the wasm's speed under Node scripts/deploy.sh # dist/ -> /docker/appdata/play-nes, nginx behind Traefik ``` ## Licence MIT (see `LICENSE`). `src/palettes/` are pally's output, MIT-0. No ROMs or BIOS are included but Super Tilt Bro. (above), under its own licence. `scripts/build-dist.sh` first writes `web/notices.html` (`python3 -I scripts/notices.py`): every component the wasm is built from, its licence, and its LICENSE and NOTICE files and the licence notices kept in its sources, verbatim, linked from the start panel and Settings, About. It stops the build when a family crate has no LICENSE file. `notices/` holds what a pinned crate owes but does not carry: famicom-rust-fast c7c8ade has MAME BSD-3 tables but not yet its `NOTICE.md` (drop the file when the pin moves past f2e3840). The page opens with the included game's terms, credits and licence (`notices/games.json`; its WTFPL text is served beside it as `web/games/super-tilt-bro-LICENSE.txt`). The build stops when a file in `web/games/` or `INCLUDED` has no entry there, or a game's SHA-1 is not its entry's.