# Status 2026-11-13. **The ladder passes: all four rungs.** The crate is used by megadrive-rust and megadrive-rust-fast. Reference: ym2612-python `44d504e8fad57a387c108f2538a6350e654dc2f2` (`a6e8e5d` until the core's 2026-11-22 history rewrite took its GPL harness out of every commit; the same code; the transcription's citations name `a6e8e5d`), cloned into the git-ignored `external/ym2612-python` by `scripts/fetch_ym2612python.sh` from a sibling checkout (`../ym2612-python`, or the path given as its argument; the scripts refuse a copy that is not at the pin, and check that the reference they import is that copy). Later ym2612-python commits change only its documents and scripts' path handling, not the core. Run on Linux x86_64 with rustc 1.99.1 (rustup stable; the scripts prefer it, because the system clippy is older than the system rustc), the reference on PyPy 7.3.23 (Python 3.11), `JOBS=8`. | Rung | What | Result | Command | | --- | --- | --- | --- | | 1 | Every test in the reference's `tests/`: `test_contract.py`, `test_measured.py`, `test_manuals.py`, `test_community.py`, `test_provisional.py`, transcribed to `tests/contract.rs`, `measured.rs`, `manuals.rs`, `community.rs`, `provisional.rs` (parametrized cases folded into loops) | 135 of 135 test functions pass (11, 56, 29, 18, 21: the reference's 186 cases) | `scripts/rung1.sh` | | 2 | Register-stream lockstep (`scripts/lockstep_gen.py`): 96 seeded scripts per variant of 2,000 steps, plus one that dumps every table and `vibrato_offset` over a grid. Every register of both parts written at random through either data port (the other part's included), lone address and data writes, scripted voices with random parameters keyed on and off, `$28` with every select value, channel 3's special modes and own frequencies, CSM from Timer A, SSG-EG, the LFO, the timers' load/enable/reset bits, DAC bursts with the ninth bit, the test registers, reads at all four ports at odd times, `run()` by unaligned amounts and clock by clock, `reset_line` pulses with writes while held, `enabled` toggles. Both cores' traces compared byte for byte: every frame, read (with `irq`), `channel_values()`, and dumps of every field of the chip, its channels and operators | 193 of 193 identical: 4,371,944 frames, 43,339 reads, 38,982 channel-value lines, 4,114 state dumps. Also seed 2 (48 per variant, 4,000 steps): 97 of 97, 4,363,007 frames | `scripts/rung2.sh [count] [seed] [steps]` (defaults 96 1 2000) | | 3 | The register streams of the reference's `validation/closed_box.py`: its 55 `STREAMS` and `s_status`, imported from the reference (the harness is neither built nor run, and no model is involved), fed to both cores as its `run_core` feeds its core (one `run(6)` per internal clock), once as its table 1 (the same writes) and once as its table 2 (operator 1's key bit one sample late, with the extra `$28` write, BUSY clear and restored address where `run_core` makes them); the status stream as table 1 | 111 of 111 identical: 392,689 frames and as many six-channel values, 160 status reads with `irq` | `scripts/rung3.sh` | | 4 | The 28 faults of the reference's `validation/faults.py`, the same rules broken the same way in `src/` (`scripts/seeded_faults.py`), each judged by rungs 1, 2 and 3; the unbroken copy first | 28 of 28 caught. Rung 1 catches 28 (by file: contract 2, measured 19, manuals 12, community 12, provisional 8), rung 2 catches 28, rung 3 catches 27 (not "one detune entry moved", the entry for detune ±3 at key code 20, which no closed_box stream reaches; rungs 1 and 2 catch it) | `scripts/rung4.sh` | `cargo test --release` is rung 1 and needs nothing else; with `--features serde` it adds the save-state test (`tests/save_state.rs`: both variants, a busy stream through bincode half way, identical afterwards). CI (`.github/workflows/ci.yml`: fmt, clippy `-D warnings`, `cargo test --release --features serde`; main and pull requests, concurrency cancel) runs rung 1 only. Rungs 2 to 4 need the reference. Locally: `cargo fmt --check` and `cargo clippy --all-targets --features serde -- -D warnings` (clippy 0.1.99) are clean. ## Speed `cargo run --release --example speed [seconds]`: the discrete chip, six voices keyed on (algorithms 0 to 5, feedback 3, tremolo and vibrato on two), the LFO on, run at 7,670,453 Hz in video-frame steps with `take_samples()` after each. Measured 2026-11-13 with the machine's load average at 25 to 31 on 32 cores: **79 to 147 times real time** (ten runs; 10 s runs 79-92x, 30 s runs 110-147x). `scripts/speed_ref.py` runs the same setup on the reference: 4.3 and 6.3 times real time on PyPy at the same moments (its README's "about 9 times" was presumably measured on a quieter machine), with the same output checksum as the Rust run of equal length. Correct, not fast: the transcription is not tuned. ## The API a board uses Crate `ym2612` (package `ym2612-rust`; `ym2612-rust = { path = "../ym2612-rust" }`, then `use ym2612::...`). The names are the reference's `__all__` (its `docs/api.md` is the contract): ```rust use ym2612::{YM2612, FMVariant, YM2612_DISCRETE, YM3438_ASIC}; use ym2612::{CLOCKS_PER_SAMPLE, BUSY_CLOCKS, WAIT_AFTER_ADDRESS, // u64, clocks of the pin WAIT_AFTER_OPERATOR_DATA, WAIT_AFTER_CHANNEL_DATA}; // 144, 192, 17, 83, 47 let mut fm = YM2612::new(YM2612_DISCRETE); // YM2612::default() is the discrete chip fm.write(port, value); // write(port: u8, value: u8): 0/2 address for part I/II, 1/3 data let s: Option = fm.read(port);// the status byte; None where the part leaves the bus undriven fm.run(clocks); // run(clocks: u64): clocks of the chip's pin; the remainder is kept let frames: Vec<(i32, i32)> = fm.take_samples(); // one (left, right) per 144 clocks, within ±1,554 let line: bool = fm.irq(); // the /IRQ pin (Python's property is a method) fm.reset_line(level); // reset_line(level: u8): 0 holds the chip in reset, else releases fm.enabled = false; // the board's switch: silent frames, timers still run let v: [i32; 6] = fm.channel_values(); // the six signed 9-bit values of the last sample ``` A new variant is struct update syntax (Python's `dataclasses.replace`): `FMVariant { ladder: false, ..YM2612_DISCRETE }`. Python's public attributes are public fields (`variant`, `enabled`, `samples`, `channels` with their `operators`, the registers and counters, with the reference's names), so a board or a test can reach them as in Python. The provisional switches are constants in `ym2612::chip`: `DELAYED_MODULATION`, `FNUM_LATCH_SHARED`, `ENVELOPE_COUNTER_SKIPS_ZERO`. Two hidden items exist for the transcribed tests only: `YM2612::set_frequency` (Python's `_set_frequency`) and `run_with_delayed_modulation::()` (the T4 test hears the other setting of the constant). **Save states** (the `serde` feature): every field of `YM2612` is serialised, internal ones included; the variant is saved as its whole record and its name must be `YM2612` or `YM3438` on loading (the name is a `&'static str`). PROVISIONAL: the format is serde's, so a field change changes it; a host should version its own states. ## Where Rust differs from the reference All stated in the module headers as well. - Integers are `i32` for register values and state (none comes near the range; `&` and `>>` on negative values match Python's) and `u64` for clock counts; BUSY's `max(0, busy - clocks)` is a saturating subtraction. - `write` takes bytes, so Python's `value &= 0xFF` is the type; `reset_line` takes the level as a `u8` (0 low, anything else high, as Python's truth test). - `LOG_SINE` and `EXPONENT` are built on first use (`LazyLock`) from the same `f64` formulas; every entry is compared with the reference's in rung 2. The integer tables are `const`s built by `const fn`s from the same rules. - `DELAYED_MODULATION` is a const generic parameter of the per-sample step (see above). - In the per-sample step, the DAC's value is read once before the channel loop rather than at channel 6, because Rust will not lend the chip while a channel is borrowed; nothing in the loop can change it. - The reference's Python-object-model checks in `test_contract.py` (a frozen dataclass, plain attributes, PyPy's 80-attribute limit) became their Rust meaning: a variant constant is unchanged by changing a copy; the state types are `Clone + PartialEq + Send + Sync` owned data; the records have the reference's field counts (20, and 12 plus four operators). The split-`run()` test draws its random piece lengths from a small generator of its own, not Python's `random`. ## Suspected errors in the reference Behaviour here is identical to the reference's in every case; nothing was fixed in Rust. - **`channel_values()` and the DAC.** Its docstring says "the six signed 9-bit values of the last sample, ... the sixth being the DAC value while the DAC is enabled", but channel 6's entry is worked out from `$2A`, `$2B` and `$2C` *as they are when it is called*, not as they were at the last sample: a `$2A` write, or the DAC switched on or off, between a sample and the call changes the value reported for that sample (the frames themselves are right). The FM channels report what the last sample stored (`channel.output`). The board and `closed_box.py` read it straight after a sample, where the two agree; a caller that writes first would see a value the chip never produced. Low impact (the method is "for validation"). Observations on the reference's tests (transcribed as they are; they still pass): - `test_provisional.py::test_t12_...`: inside the loop, `expected` is computed twice; the first list is discarded, and the second starts from the phase the first left behind, not from the `quiet.phase = 0` just set. Only the maximum is compared, so the result is the same. - `test_community.py::test_vibrato_is_applied_to_the_f_number_before_the_block`: the set of increments has `(-last) & 0xFFFFF`, computed from the final phase, removed before the subset test. That value has no evident meaning as an increment; the first increment after key-on (the phase from 0) is already a plain increment. Harmless: the test passes with or without it. ## How it was written The reference's `docs/restart.md` binds this crate as it binds the reference: - The only source is ym2612-python at the pin: `src/`, `tests/`, `scripts/`, `validation/*.py`, `docs/`. Its `reference/` directory was not opened (the clone in `external/` does not have it: it is git-ignored in the reference). No withdrawn core, no Nuked or other GPL/LGPL source, and none of megadrive-python's or megadrive-rust's source was opened. - Transcribed, not redesigned: function for function, in the same order, with the same names (Rust case aside) and the same comments, spec citations and CHOICE/PROVISIONAL labels. Where Rust forces a difference, the module header says so (listed above). - The provisional switches stay module-level constants and the variants stay data. - Where the Python looks wrong it is not fixed here: behaviour stays identical, and the case is listed above under "Suspected errors in the reference". - No closed-box model is run. `validation/nuked_harness.c` was not opened; `validation/closed_box.py` was read and imported for its stream definitions, and its `build()`, `run_model()` and `main()` were never called. Material seen and not used: listing the reference's working tree (`git ls-files`, `wc -l`) before the pinned clone existed printed the names of files under its `validation/build/faults/` copies (copies of its own `src/`). None was opened. Layout: `src/variant.rs`, `src/tables.rs`, `src/operator.rs`, `src/chip.rs` are the reference's four modules and `src/lib.rs` its `__init__.py`; `tests/` holds its five test files, transcribed, with `tests/helpers/mod.rs`; `examples/ym2612-trace.rs` prints a lockstep script's trace, as `scripts/lockstep_ref.py` does on the reference; `scripts/` holds the rung scripts and their helpers. ## Open - The wasm32 build is untried; the library is plain `std` with no dependencies. - Speed work belongs in a `-fast` fork, not here.