# Status sn76489-rust transcribes sn76489-python at **`f8157f497d8a38d85c02198496761a66c1178818`** (`f8157f4`). All four rungs pass (2026-11-13). The core's history rewrite of 2026-11-22 took its GPL harness out of every commit: the reference is now fetched at `3c5d703`, `f8157f4` rewritten, the same code; the transcription's citations keep `f8157f4`. ## How it was made - The **only source** is sn76489-python at the pin: its code, tests, scripts and docs. No other emulator was read, no closed-box model was run, and nothing of the withdrawn predecessor (in Python or Rust) or of any GPL or LGPL emulator was opened. Of the reference's git-ignored `reference/` (fetched third-party files) only Kortink's hardware capture, `kortink/white_noise.bin`, is opened, by one test, as the reference's own test opens it. - **Transcribed, not redesigned**: function for function, in the same order, with the same names (Rust case aside), comments, spec citations and CHOICE/PROVISIONAL labels. Where Rust forces a difference the module header says so. - Where the Python looks wrong it is **not** fixed here: behaviour stays identical and the case is listed below under "Suspected errors in the reference". - The reference's rung 5 (`validation/closed_box.py`) drives Nuked-PSG; here only its *streams* are used, through the two cores. ## Results and the exact commands Run from the repository root. `cargo test --release` needs nothing else. Rungs 2-4 need the reference: `scripts/fetch_sn76489python.sh` clones it at the pin into the git-ignored `external/sn76489-python` (the default source is the checkout beside this one, `../sn76489-python`; `SN76489_PYTHON=` points the rung scripts at another). The Python side runs under PyPy when one is found (`~/.local/opt/pypy/bin/pypy3`, then `pypy3` on the `PATH`; `PYTHON=` overrides, `JOBS=` sets the parallelism, default 8). | Rung | Command | Result | | --- | --- | --- | | 1 | `scripts/rung1.sh` | PASS: 67 transcribed tests (contract 11, capture 4, manuals 24, community 14, provisional 14) | | 2 | `scripts/rung2.sh` (64 scripts per part, seed 1, 400 operations) | PASS: 284 of 284 traces identical (22,312,087 samples, 106,536 state lines) | | 3 | `scripts/rung3.sh` | PASS: 204 of 204 traces identical (51 streams x 4 parts; 8,216,724 samples, 28,372 state lines); `cargo test --test closed_box` 204 of 204 hashes | | 4 | `scripts/rung4.sh` | PASS: 22 of 22 faults caught | Also: `cargo fmt --check` clean; `cargo clippy --release --all-targets --features serde -- -D warnings` and `cargo clippy --all-targets -- -D warnings` clean with clippy 0.1.93 (a clippy older than the crate's `rust-version`, 1.85, refuses the crate). `cargo test --release --features serde`: 73 tests (the 67, the closed-box hashes, two checks of the variants module, one of the test helpers' random generator, two save-state tests). ### Rung 1: what was transcribed Every test of the reference's five files: `test_contract.py` (12), `test_capture.py` (4), `test_manuals.py` (24), `test_community.py` (14), `test_provisional.py` (14); 233 cases in pytest, 67 functions here with the parameters as loops. The reference's own suite passes at the pin under PyPy (`PYTHONPATH=src pypy -m pytest`: 232 passed, 1 skipped: the clone has no `reference/kortink/white_noise.bin`). Left out, with the reason: - `test_an_instance_stays_under_pypys_eighty_attributes`: PyPy's attribute limit has no Rust counterpart. Carried as far as Rust has an equivalent, said at each test: - `test_the_names_api_md_lists_are_the_public_surface`: the `use` of the seven names is the check (the file does not build without them). - `test_state_is_plain_attributes_and_copies_out_and_back`: every public field is copied by hand into a fresh chip, as a save-state walker sets Python's attributes; their plainness is their types. - `test_a_variant_is_a_frozen_value_of_plain_fields`: a `Copy` struct; changing a copy leaves the part's constant unchanged. - `test_the_logic_never_asks_which_variant_it_is`: the same scan of `src/chip.rs`, with the Rust spellings of the same questions added (`variant !=`, `matches!`, the other parts' names). - `write_test` on a part without the register returns `Err(NoTestRegister)` where Python raises `AttributeError`. The tests' random streams are the reference's own: `tests/common/mod.rs` carries Python's `random.Random` (MT19937, `randrange`, `random()`), checked against numbers Python prints (`tests/helpers_check.rs`). Two tests read files when present, as the reference's do: Kortink's capture (`$SN76489_REFERENCE`, else `external/sn76489-python/reference`, else `../sn76489-python/reference`) and the SMS Power wiki's zipped noise files (`reference/smspower-attach` in sms-python's checkout: `$SMS_PYTHON_DIR`, else the one beside this checkout); absent, they print why and pass, where pytest skips. Both were present for the results above. ### Rung 2: the lockstep `scripts/lockstep_gen.py` writes the scripts; `scripts/lockstep_ref.py` (sn76489-python, under PyPy) and `examples/sn76489-trace.rs` (this core) run them and print traces, compared with `cmp` by `scripts/compare_traces.sh`. Per part: 64 seeded random scripts (writes weighted toward latch, data and attenuation bytes, some wider than a byte; `run` by amounts from 0 to 400,000 clocks, odd and even; stereo writes; `write_test` with any 16-bit value on every part, so the missing register's error is compared too; resets with and without `silent_start`), a quarter of them with replaced variant fields first (latch, data-byte flag, shift level, tone-zero period, power-on attenuation, stereo, test override, register length and taps, a random volume table), and seven scripted ones (every tone value of interest on each channel, every noise control under five third-tone values with rewrites, the test override for every source, every stereo bit, power-on data bytes, small runs). The trace holds every state field after every call, every `write_test` outcome with its message, and every sample as its `f64` bits, run-length coded (losslessly). Levels are compared as bits, not within a tolerance. ### Rung 3: the reference's validation streams `scripts/export_closed_box.py` imports the reference's `validation/closed_box.py` and writes its `streams()` table (51 streams) as lockstep scripts, replayed as its `core_ticks` replays them (run to each event's clock, then the write or test bits, then run to the end). No model is built or run: `build()` is never called and the clone has no `reference/` to build one from. Two differences from the reference's own use, both because the model is not run: the streams run on all four parts (the reference compares only `YM7101_PSG`, against the model), and with each part's own volume table (the reference substitutes the model's measured levels). `scripts/rung3.sh` checks that the committed `tests/closed_box/*.txt` are what the exporter writes from the reference, compares the two cores' traces byte for byte, and runs the `cargo test` that checks this core's traces against the committed SHA-256 of the reference's. ### Rung 4: the seeded faults `scripts/seeded_faults.py` applies each of the reference's 22 faults (`validation/faults.py`) as the same rule broken the same way in `src/` (the volume-table fault replaces the written-out table by the one `10 ** (-0.1 * (step + 1))` gives), each in a copy under `external/faults/`, and judges it by every rung. The unbroken copy passes every rung first. | Rung | Faults caught | | --- | --- | | 1 (transcribed tests) | 22 of 22 | | 2 (lockstep) | 22 of 22 | | 3 (closed-box streams) | 21 of 22 (not "stereo: left reads the right-hand bits": the streams write no stereo register, so both sides carry every channel) | Per rung 1 file: contract 5, capture 5, manuals 13, community 13, provisional 16 of 22. These are the reference's own counts for its files, and fault by fault the same files catch the same faults (checked against `validation/faults.py` run at the pin under PyPy). An error in the fault harness, found and fixed on the way: the fault copies share four cargo target directories, cargo names this crate's artifacts the same whatever directory the copy is in, and it judges freshness by modification time. A copy's files kept their copied times, so a copy could be judged with an artifact built from another copy. The script now stamps every file of a copy just before it builds, while it holds its target directory alone. The table above is from after the fix. ### Speed `cargo run --release --example speed [seconds]`: all four channels sounding (three tones, white noise at rate 3), a `run(228)` per NTSC scan line, a volume write every line, `take_samples()` once a frame. Measured as the reference's README measures time: seconds of the Z80's 3,579,545 Hz clock per second of wall time. On this machine (32 cores shared, load 10 to 27 during the runs): about **1,400 to 1,650 times real time** for every part (600 s of clock in 0.42 s; 120 s in 0.072-0.077 s). The reference under PyPy 3.11 on the same load: 21.6 times real time (20 s of clock in 0.93 s; script not committed, the same loop in Python). ## The Rust API a board uses ```rust use sn76489::{ SN76489, PSGVariant, CLOCKS_PER_SAMPLE, // CLOCKS_PER_SAMPLE: u64 = 16 SN76489AN, SEGA_VDP_PSG, GAME_GEAR_PSG, YM7101_PSG, NoTestRegister, // write_test's error on parts without it IDEAL_VOLUME_TABLE, VARIANTS, variant, // the ideal curve; the four parts; by name }; let mut psg = SN76489::new(SEGA_VDP_PSG); // Python: SN76489(SEGA_VDP_PSG) let mut sg = SN76489::with_silent_start(SN76489AN, true); // SN76489(v, silent_start=True) let default = SN76489::default(); // SN76489(): the Master System's part psg.write(value); // u32, masked to a byte, as Python does psg.write_stereo(value); // Game Gear port $06; ignored elsewhere psg.write_test(debug_register_0)?; // Mega Drive only; Err(NoTestRegister) elsewhere psg.run(cycles); // u64 input clocks let samples: Vec<(f64, f64)> = psg.take_samples(); // one (left, right) per 16 clocks, 0.0-4.0 psg.reset(false); // reset(silent_start=False) psg.stereo; // readable state: every field is pub ``` State a save state carries (all `pub`): `variant`, `tone: [u32; 3]`, `noise`, `volume: [u32; 4]`, `stereo`, `latched`, `counters: [u32; 3]`, `outputs: [u32; 3]`, `noise_counter`, `noise_input`, `lfsr`, `test` (all `u32`), `clock_remainder: u64`, `samples: Vec<(f64, f64)>`. With the `serde` feature `SN76489` and `PSGVariant` are `Serialize`/`Deserialize`; a variant is read back only when its name is one of the four parts' (a replaced variant keeps its part's name). `variant.ready_clocks` is 32 on the discrete chip and 0 on Sega's parts; the core does nothing with it. ## Suspected errors in the reference None in behaviour. Nothing in the transcription or the four rungs turned up a case where sn76489-python looks wrong against its own spec or comments. One note on a comment, kept verbatim: - `chip.py`, `write()`: the comment "A data byte while an attenuation or the noise register is latched writes its low bits there, as a latch byte would (spec 3.6), on the parts where it does anything at all." sits in the branch that **drops** the byte (taken when `data_byte_reaches_all_registers` is false). The behaviour is right (the test `test_a_data_byte_reaches_every_register_on_the_discrete_chip_too` pins it); the comment describes the rule the branch is the exception to, which reads as if the branch did the writing. ## Open items - CI (`.github/workflows/ci.yml`) runs fmt, clippy and `cargo test --release --features serde` on main and pull requests. Rungs 2 and 4 need the reference checked out and are not part of it. - `tests/variants.rs` checks the written-out volume table against `10f64.powf(...)` bit for bit. That holds with glibc's `pow` (this machine and GitHub's Ubuntu runners); a platform whose `pow` rounds differently would fail that check without the core being wrong. - Rung 3 does not use the closed-box model's measured volume levels (the model is not run here), and the stand-in of `validation/as_observed.py` is not part of the core and is not transcribed.