sm64-binds
Interfaces for Mario 64 in Rust, Python and JavaScript, all running the same WebAssembly build of sm64-port through a WASM runtime.
To avoid shipping copyrighted ROM data, the WASM module is distributed XOR'd
against a US .z64 ROM. Each binding recombines it with the ROM you supply at
runtime. The ROM is required: a US z64 copy, exactly 8.00 MB.
| Binding | Package | Notes |
|---|---|---|
| Rust | crates.io/crates/sm64-binds | Most actively maintained. |
| Python | pypi.org/project/sm64-binds | |
| JavaScript | npmjs.com/package/sm64-binds-frontend | Browser frontend, see js_example/. |
Layout
| Path | Contents |
|---|---|
rust/ |
The Rust crate. src/lib.rs is the API, tests/ runs the game. |
python/ |
The Python package. |
javascript/ |
The npm package, sm64-binds-frontend. |
js_example/ |
A Vite + React app consuming the npm package. |
maketools/ |
Scripts that build and package the distributable WASM. |
scripts/ |
Release helpers: version reporting and artifact checks. |
docs/ |
Format and design notes: Mupen64 states (mupen64-st.md), portable states (a reader's guide to the translator, PORTABLE.md; design notes and measurements, portable-state.md). |
rust/benchmark/ |
Timing programs; see rust/benchmark/README.md. |
WASM_ZIP_XOR/ |
The distributable XOR'd payloads. |
sm64-port/ |
Vendored sm64-port checkout, only needed to rebuild the WASM. |
Usage
Rust:
use ;
let generator = new?;
let mut game = generator.create_game?;
let mut pad = default;
pad.button = A_BUTTON;
game.step_game?;
let state = game.get_game_state?;
println!; // numStars, position, levelNum, lives, health, ...
println!;
Python:
=
=
=
=
Clone a game to branch off from the same state:
=
Load a Mupen64 savestate (Mupen64-rr(-lua) or Mupen64Plus .st, US ROM)
into a game that is in a level (Rust only; see docs/mupen64-st.md):
let st = from_file?;
game.warp_to_mupen_area?; // if the game is in another area
println!;
Save a state in the headless build and load it in the browser's rendering build, or the
other way round (portable states; how the translator works: docs/PORTABLE.md, design notes: docs/portable-state.md):
let bytes = game.export_portable?.to_bytes;
engine.; // Sm64VisualEngine, from either build
const portable = engine.; // loads in Rust: game.load_portable(&Portable::from_bytes(..)?)
const snapshot = engine.; // the build's own raw snapshot, for seeking
Building the WASM
With a ROM at the repo root:
make sm64-build
If emscripten (emcc) is on PATH this rebuilds sm64-port and regenerates
WASM_ZIP_XOR/. Otherwise it recombines the existing payloads with your ROM,
which is enough to run every binding.
The devcontainer (.devcontainer/) provisions emscripten, wasm-pack,
wasm-bindgen and binaryen.
Tests
make test
Runs the Rust, Python, JavaScript and maketools suites. Each one is also runnable on its own:
make test-rust # cargo test, in rust/
make test-python # pytest, in python/
make test-javascript # node --test, in javascript/
make test-maketools # pytest, in maketools/
make portable # regenerate the portable state tables and translator after rebuilding the game
make lint # clippy, rustfmt and eslint
Tests that boot the game need a ROM. Point SM64_BINDS_ROM at it if it is not
at the repo root; otherwise those tests skip instead of failing, so the suite
passes on a checkout without the copyrighted file.
Releasing
Rust, Python and JavaScript all ship from this repo, one tag at a time:
make versions # see where each package stands
python scripts/bump_versions.py 1.0.25
Then commit, tag v1.0.25, and push the tag; GitHub Actions builds and
publishes all three. See RELEASING.md for the one-time token setup and the
release checklist.
Benchmarks
make bench # Rust: stepping, save states, instantiation
make bench-python # Python: the same paths
# Or individually, from rust/:
cargo bench --bench hot_paths -- ../baserom.us.z64
cargo bench --bench diagnostics -- ../baserom.us.z64
Both language harnesses share one measurement contract (rust/benchmark/harness.rs,
mirrored by python/benchs/harness.py): load and instantiation costs, stepping
through a scripted input, per-call overhead, and the save-state copy paths,
each with per-unit times, throughput and derived rows for per-frame figures.
Save-state copying is a single memcpy instead of the runtime's chunked
accessors, and step_game is unchanged because it is bounded by the emulated
game logic rather than by the binding — an empty WASM call through the same API
costs ~30 ns against a ~200 µs frame.