# play-md The Sega Mega Drive / Genesis in the browser, live at . Choose a cartridge image (`.md`, `.bin`, `.gen`, `.smd`, or a `.zip` holding one); it is read on the visitor's device and never uploaded. Recent games and battery saves are kept in the browser (IndexedDB). `?rom=` fetches one from a site that allows it (archive.org `/download/` links are rewritten to its CORS-enabled `/cors/` path). The console is [megadrive-rust-fast](https://github.com/alewman/megadrive-rust-fast) (private), pinned by commit in `Cargo.toml`, unchanged: `src/lib.rs` is a thin wasm-bindgen surface over its `System`, the same calls its `md-play` window makes. It runs on the page's main thread a frame at a time, paced by the clock, and its sound goes to an AudioWorklet (`web/audio-worklet.js`) at the context's own rate. ## Controls Keyboard: arrows, Z X C (or J K L) for A B C, A S D for X Y Z, Enter Start, Right Shift (or Q) Mode, F2 reset, F5 save state, F7 load state, R or Backspace (held) rewind, P or Pause pause, F9 sound on and off. Controllers go by button position (the browser's standard mapping), as `md-play`: A B C on the left, bottom and right face buttons, X Y Z on left shoulder, top face button and right shoulder, Back is Mode; Settings has the Super Nintendo fighting layout. An unmapped pad's hat-switch d-pad (Firefox on Windows) is read as play-nes reads it, the left trigger rewinds while held, and the right stick pressed (R3) pauses. A second controller is player 2. Touch controls appear on touch screens, with X Y Z and a Mode pill when the cartridge asks for a 6-button pad; on a phone on its side Start and Mode sit top-left. Settings can buzz on each touch press (Android; off at first). The pad and the console (Genesis, PAL or Japanese Mega Drive) come from the cartridge header unless chosen in Settings. The controls are the family's (emulator-lineage `docs/proposals/2026-11-play-controls.md`): `web/controls.js` is a copy of play-nes's, its second line naming the play-nes commit, checked by play-nes's `scripts/controls-drift.sh`. ## Save states, rewind, run-ahead The board's own (megadrive-rust-fast `src/state.rs`, transcribed from megadrive-python's `state.py`, 2026-11-23), wired as play-nes and play-pce wire theirs: - **Save states**: one per game, kept in IndexedDB beside the battery save under the cartridge's SHA-1 (F5 saves, F7 loads; the bar's two arrow buttons). The board refuses a state of another cartridge, console or pad, or of another version of its format, before it changes anything, so a refused load leaves the game running. - **Rewind**: held (R or Backspace, the left trigger, the bar's button or a finger on it). A deflated snapshot every 4 frames, about a minute or 64 MiB, whichever comes first; each frame held steps back 4. - **Run-ahead**: Off, 1 or 2 frames (Settings). The console's own frame gives the sound; then a snapshot, the frames ahead, their picture shown, and the snapshot taken back. `scripts/check-run-ahead.mjs` proves it changes nothing: against a plain run, the same sound every frame, the same state at the end, and the picture the plain run shows that many frames later. Frames the page does not show are run undrawn (`VdpState::undrawn_frame`): the frames catching up, the console's own frame under run-ahead, and all but the last frame ahead; the console's frame whose snapshot goes into the rewind window is drawn, so a rewind step shows a real picture. Since megadrive-rust-fast `1be71d8` (megadrive-python `5351daf`, `efd80fc`) drawing off changes only the pixels: the sprite scan and the collision flag run either way, and the VDP draws the next frame again from its first line by itself. `scripts/check-run-ahead.mjs` checks it: a run shown every frame and one shown in none end in the same machine state (`Console.machine_state`, everything but the picture), a run drawn one frame in three shows the plain run's pictures, and run-ahead gives the same sound, state and pictures (Sonic, Overdrive 2 and Virtua Racing, all ok). What it saves, Node, ms a frame, best of 5 runs of 300 frames, the old build (`4493cbc`, which drew every frame) and this one alternated on one core: | | Sonic the Hedgehog | Overdrive 2 (PAL) | Virtua Racing | | --- | --- | --- | --- | | A shown frame | 3.29, 3.41 | 4.01, 4.02 | 4.62, 4.38 | | A frame catching up | 3.37, 3.01 | 3.81, 3.45 | 4.53, 3.91 | | Run-ahead 1 | 8.42, 8.10 | 8.92, 8.56 | 10.38, 9.68 | | Run-ahead 2 | 11.28, 9.92 (-12 %) | 12.56, 11.61 (-8 %) | 15.20, 13.00 (-15 %) | Measured 2026-11-23 (board `a95b0b3`, this machine, shared): | | Sonic the Hedgehog | Overdrive 2 (PAL) | | --- | --- | --- | | A frame, Node | 2.51 ms | 3.16 ms | | Run-ahead 1, Node | 5.49 ms | 7.87 ms | | Run-ahead 2, Node | 8.27 ms (50 % of 16.69) | 11.14 ms (55 % of 20.12; 67 % of an NTSC 16.69) | | Run-ahead 0 and 2, headless Chromium (`check-browser.mjs`) | 3.47, 7.84 ms, 60 fps both | 3.50, 9.87 ms, 49.6 fps both | | A snapshot (raw) | 481 KiB, 0.6 ms to take | 481 KiB, 1.0 ms | | A save state (zlib) | 56 KiB; 8.5 ms to save, 8.1 to load | 24 KiB; 18.8 and 25.1 ms | | The rewind window after a minute | 54.6 s in 64 MiB (the budget) | 72.4 s in 44.8 MiB | | A rewind step | 1.3 ms | 2.1 ms | So run-ahead 2 fits a 60 Hz frame on both, on a desktop, with room; on a phone it is unmeasured. The worst single frames in Node reach 26-59 ms (a collection or the shared machine), which the page's catch-up absorbs. ## Build, check, deploy ```text scripts/build-dist.sh # wasm (toolchain 1.93.1, wasm-bindgen 0.2.128, wasm-opt) -> dist/ ROM=game.md node web/check-browser.mjs # headless Chromium: load, run, Start, the page's keys, # states, rewind, run-ahead 0 and 2, a faked # controller, touch layouts, screenshots ROM=game.md node scripts/check-run-ahead.mjs # Node: run-ahead 1 and 2 against a plain run, # a state into a fresh console, rewind, costs node web/check-touch.mjs # the touch pad under fingers, on a phone scripts/deploy.sh # dist/ -> /docker/appdata/play-md, nginx behind Traefik URL=https://play-md.supertux.com/ ROM=game.md node web/check-browser.mjs # the live site ``` The check prints the frame rate (the console's 59.92 Hz), the sound frames sent, the recent list and the touch layout, and saves screenshots to `target/check-browser/`. `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 (ssp1601-rust's MAME BSD-3 notice among them), linked from the start panel and Settings, About. It stops the build when a family crate has no LICENSE file. A notice a pinned crate owes but does not carry goes in `notices/` (none here). The page opens with the included games: each one's terms, author and attribution are in `notices/games.json`, with its licence and info files quoted verbatim (Titan's from inside their zips). The build stops when a file in `web/games/` or `INCLUDED` has no entry, when a game's SHA-1 is not its entry's, or when an entry lists terms not recorded (`unrecorded`): Rick Roll MD (the video and song), Meowdoku (two FM patches from commercial games) and Bad Apple MD (the song and video) list theirs, so the build stops until the owner records a ruling. ## Titan's demos Overdrive, Overdrive 2 and MD-NICCC are in `web/games/` and on the public list with Titan's permission (November 2026). Titan asked that each release's info file come with it: the zips keep them, and `info` in `INCLUDED` (`web/app.js`) names the file the "info" link beside the title shows. MD-NICCC's release has none. Do not strip these files when repacking a zip. ## Known limits - A game the MAME software list doesn't hold gets its board from the header. The list's boards are built in (`src/boards.tsv`, from `scripts/make-boards.py`), so listed EEPROM, mapper and SVP cartridges work. - 7-Zip archives can't be opened in the browser: extract or re-zip them.