# ym2612-rust The Yamaha YM2612 and YM3438 (OPN2) FM sound chip, the discrete chip of Mega Drive model 1 boards and the YM3438 as measured on the model 2's ASIC, transcribed from **ym2612-python** at `a6e8e5d` and proven equivalent to it: the same frames, status reads, `irq`, channel values and internal state, for both variants. ym2612-python is written from documents and hardware measurements under written rules (its `docs/restart.md`): no GPL or LGPL source read, every rule citing its source. This crate is written under the same rules, and its only source is ym2612-python itself (`src/`, `tests/`, `scripts/`, `validation/*.py`, `docs/`). No Nuked or other GPL/LGPL code, and none of the withdrawn earlier cores, was read for it; no closed-box model was run. Used by megadrive-rust and megadrive-rust-fast. `src/variant.rs`, `src/tables.rs`, `src/operator.rs` and `src/chip.rs` are the reference's four modules, function for function, in the same order, with the same names, comments, spec citations and CHOICE/PROVISIONAL labels; `src/lib.rs` is its `__init__.py`. Nothing is redesigned; where Rust forced a difference, the module header says what it is. The package is `ym2612-rust`, the crate `ym2612`; no dependencies by default; not published to crates.io. ## Using the core ```rust use ym2612::{YM2612, YM2612_DISCRETE}; let mut fm = YM2612::new(YM2612_DISCRETE); // or YM3438_ASIC for later boards fm.write(0, 0x22); // address, part I (ports 0/2 address, 1/3 data) fm.write(1, 0x00); // data fm.run(7_670_453); // one second of the chip's clock pin (NTSC) let frames = fm.take_samples(); // Vec<(i32, i32)>, one per 144 clocks let status: Option = fm.read(0); // None where the part leaves the bus undriven ``` [docs/status.md](docs/status.md) lists the whole API a board uses. What the core claims about the chip, and how well each behaviour is known, is the reference's business: its README and `docs/claims.md`. In short, it follows Yamaha's and Sega's manuals and what Nemesis and others read out of real chips, steps once per sample, takes writes at sample boundaries, and claims no exactness to any console; the discrete chip's output stage uses provisional constants. **Save states: the `serde` feature.** With `features = ["serde"]` the whole `YM2612` is `Serialize`/`Deserialize` (every field; the variant as its record, its name one of `YM2612`, `YM3438`). ym2612-python has no restore: this is the Rust crate's own. ## Status All four rungs pass. Results and exact commands: [docs/status.md](docs/status.md). | Rung | What | Result | | --- | --- | --- | | 1 | ym2612-python's five test files, transcribed | 135 of 135 test functions (the reference's 186 cases) | | 2 | Register-stream lockstep: seeded random and scripted streams, both variants, every frame, read, irq, channel value, state dump and table | 193 of 193 scripts identical (4.37 million frames) | | 3 | The 55 register streams of the reference's `validation/closed_box.py` (both of its input tables) and its status stream, through both cores | 111 of 111 identical | | 4 | The reference's 28 seeded faults, broken the same way here | 28 of 28 caught (rung 1: 28, rung 2: 28, rung 3: 27) | Speed, release build, six channels sounding: about 80 to 145 times real time on a loaded machine (the reference measured 4 to 6 times on PyPy beside it; its README gives about 9). ```text cargo test --release # rung 1 (and, with --features serde, the save-state test) scripts/fetch_ym2612python.sh # the reference at the pin into external/ (for rungs 2 to 4) scripts/rung1.sh scripts/rung2.sh [count] [seed] [steps] scripts/rung3.sh scripts/rung4.sh cargo run --release --example speed ``` ## History This crate and its reference were rebuilt in November 2026. The reference's predecessor followed an LGPL-licensed model's source code (Nuked-OPN2) too closely to be published under the MIT licence, and was withdrawn, with the Rust port made from it. ym2612-python is now written from manufacturer documents and hardware measurements under the rules in its `docs/restart.md`; the model is only run, as a closed box, to find differences. ## Licence MIT (`LICENSE`), like the reference.