# sn76489-rust The SN76489 programmable sound generator and its Sega relatives (the discrete TI part of the SG-1000 and SC-3000, the Master System's VDP PSG, the Game Gear's with its stereo register, the Mega Drive's YM7101 PSG with its test bits), transcribed from sn76489-python at `f8157f4` and proven equivalent to it: the same output level at every tick, the same state after every call, for every part. `src/variants.rs` and `src/chip.rs` are sn76489-python's `variants.py` and `chip.py`, function for function, in the same order, with the same names and the same comments, spec citations and CHOICE/PROVISIONAL labels. Nothing is redesigned; where Rust forced a difference the module header says what it is. It is the PSG of megadrive-rust and megadrive-rust-fast. The library has no dependencies. The package is `sn76489-rust`, the crate `sn76489`; it is not on crates.io (use it by path or git). **Where the behaviour comes from.** sn76489-python was written from documents under the rules in its `docs/restart.md`: TI's and Sega's manuals, Maxim's SMS Power page, and John Kortink's BBC Micro capture of the discrete chip's white noise, the one hardware capture it is judged against. No recording of any Sega console's PSG is in hand, so nothing here is claimed exact to a console; the reference's `docs/claims.md` says what each behaviour rests on. This crate claims one thing more: that it does exactly what sn76489-python does. Its only source was sn76489-python's code, tests, scripts and docs; no other emulator and no closed-box model was read or run for it. ## Using the core ```rust use sn76489::{SN76489, SEGA_VDP_PSG, CLOCKS_PER_SAMPLE}; let mut psg = SN76489::new(SEGA_VDP_PSG); psg.write(0x8E); // latch tone 0, low four bits psg.write(0x0F); // high six bits: n = 254, 440 Hz psg.write(0x90); // tone 0 at full volume psg.run(3_579_545); // one second of the Z80's clock let samples = psg.take_samples(); // one (left, right) pair per 16 clocks, 0.0 to 4.0 ``` The four parts are `PSGVariant` values: `SN76489AN`, `SEGA_VDP_PSG`, `GAME_GEAR_PSG`, `YM7101_PSG`. A board replaces a field with struct update syntax, for example a measured volume curve: `SN76489::new(PSGVariant { volume_table: measured, ..SEGA_VDP_PSG })`. [docs/status.md](docs/status.md) lists the whole Rust API a board uses. **Save states: the `serde` feature.** With `features = ["serde"]` the whole `SN76489` is `Serialize`/`Deserialize` (every field; the variant with all its fields and its part's name). sn76489-python's boards walk its attributes instead; this is the Rust crate's own. Without the feature the core has no dependencies. ## Status All four rungs pass. Results and exact commands: [docs/status.md](docs/status.md). | Rung | What | Result | | --- | --- | --- | | 1 | sn76489-python's tests, transcribed | 67 of 67 | | 2 | Register-stream lockstep: 284 seeded random and scripted scripts over the four parts and replaced fields; every sample and every state field after every call | 284 of 284 identical | | 3 | The 51 streams of the reference's `validation/closed_box.py`, through both cores, on every part | 204 of 204 identical | | 4 | The reference's 22 seeded faults, broken the same way here | 22 of 22 caught | Speed (release build, a board's load): about 1,400 to 1,650 times real time, in the reference README's terms (seconds of the Z80's 3,579,545 Hz clock per second of wall time), on a shared, loaded machine. sn76489-python under PyPy runs the same load at about 22 times real time. ```text cargo test --release # rungs 1 and 3; needs nothing else (CI runs it with fmt and clippy) scripts/fetch_sn76489python.sh # the reference at the pin into external/ (for rungs 2-4) scripts/rung1.sh scripts/rung2.sh [count] [seed] [steps] scripts/rung3.sh scripts/rung4.sh cargo run --release --example speed [seconds] ``` `tests/closed_box/` holds rung 3's scripts (the reference's own streams, written by `scripts/export_closed_box.py`) and the SHA-256 of sn76489-python's trace of each, so `cargo test` replays rung 3 without Python. [docs/status.md](docs/status.md) also records the rules the transcription was made under. ## Credits The behaviour is sn76489-python's, and through it the sources its README credits: John Kortink's capture and noise rule, Maxim's account of Sega's part with Charles MacDonald's captures, and Texas Instruments' and Sega's manuals. Two tests read files when they are on the machine: Kortink's `white_noise.bin` (from sn76489-python's fetched `reference/`) and the SMS Power wiki's two noise files (from sms-python's). Where they are absent the tests say so and pass, as the reference's skip; the comparison with Kortink's capture always runs by its SHA-256. ## History sn76489-python was rebuilt in November 2026. Its predecessor followed a GPL-licensed model's source code too closely to be published under the MIT licence, and was withdrawn, together with its Rust port. The reference 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. This crate transcribes the rebuilt reference; the withdrawn Rust port was not read. ## License MIT (`LICENSE`), like the reference.