# ay8910-rust > **Lineage:** ay8910-python → **ay8910-rust** · > fixes start in ay8910-python and flow down by merges · > [family charter](https://github.com/alewman/emulator-lineage/blob/main/docs/charter.md) General Instrument's **AY-3-8910** programmable sound generator and its relatives, the **AY-3-8912** and **8913**, Yamaha's **YM2149** and the sound half of **Sunsoft's 5B**, transcribed from [ay8910-python](https://github.com/alewman/ay8910-python) at `a3bb485` and proven identical to it: the same three DAC codes at every input clock, the same bytes read, the same levels driven on the ports, the same floats, the same state. `src/chip.rs`, `src/variants.rs` and `src/conformance.rs` are ay8910-python's `chip.py`, `variants.py` and `conformance.py`, method for method, in the same order, with the same names and the same comments and citations. Nothing is redesigned; where Rust forced a difference the module header says what it is (summarised below). The parts are data on a `PSGVariant`, not branches, as there. The package is `ay8910-rust`, the crate `ay8910_rust`; it has no dependencies and is not on crates.io (use it by git revision or by path). Its first board is famicom-rust's mapper 69 (the Sunsoft 5B), with famicom-rust-fast; both pin this crate at `9b670a7`. ## Using the core ```rust use ay8910_rust::{PSG, YM2149}; const CLOCK: u64 = 2_000_000; // an Atari ST's let mut chip = PSG::new(YM2149); chip.write_register(7, 0x3E); // tone A alone chip.write_register(0, (284 & 0xFF) as u8); // 2,000,000 / (16 * 284) = 440 Hz chip.write_register(1, (284 >> 8) as u8); chip.write_register(8, 15); // full volume chip.run(CLOCK / 10); // a tenth of a second let codes = chip.take_outputs(); // [[a, b, c], ...], one per input clock, 0-31 let levels: Vec = codes.iter().map(|[a, _, _]| YM2149.dac[*a as usize]).collect(); ``` ### For a board: the Sunsoft 5B What famicom-python's mapper 69 calls on ay8910-python, and so what famicom-rust calls: ```rust use ay8910_rust::{PSG, SUNSOFT_5B}; let mut sound = PSG::with_record(SUNSOFT_5B, false); // polled: nothing queued sound.select(value); // a CPU write to $C000-$DFFF: the register address sound.write(value); // a CPU write to $E000-$FFFF: the data sound.tick(); // once per CPU cycle (M2); run(n) is n of them let [a, b, c] = sound.levels(); // the three DAC codes now, 0-31 let level = sound.mix(); // the same through SUNSOFT_5B.dac, added: 0.0-3.0 sound.reset(); // the cartridge's power-on ``` famicom-python calls `mix()` only when `levels()` changes, and scales it by its own `SUNSOFT_5B_GAIN` (0.364): the gain, the 5B's amplifier and the filters are the board's. One input clock per CPU cycle; the 5B halves it itself. Everything ay8910-python's README says under "For a board: the Sunsoft 5B" holds here unchanged. ### The API, against ay8910-python's contract ay8910-python's [docs/contract.md](https://github.com/alewman/ay8910-python/blob/main/docs/contract.md) is the contract. In Rust: | ay8910-python | ay8910-rust | | --- | --- | | `PSG(variant)`, `PSG(variant, record=False)` | `PSG::new(variant)`, `PSG::with_record(variant, false)`; `PSG::default()` is an AY-3-8910 | | `AY8910`, `AY8912`, `AY8913`, `YM2149`, `SUNSOFT_5B`; `VARIANTS[name]` | the same `const`s; `VARIANTS` (an array, Python's order); `variant(name)` | | `PSGVariant` fields, `.codes` | the same fields (`name`, `ports` and `dac` are `Cow`s); `.codes()` | | `select(v)`, `write(v)`, `read()`, `write_register(r, v)`, `read_register(r)` | the same, on `u8` | | `tick()`, `run(clocks)`, `clock`, `clock_divider` (the /SEL pin) | the same; `clock` is `u64` | | `levels()` (a tuple), `mix()`, `take_outputs()`, `take_samples()` | the same; levels are `[u8; 3]`, samples `f64` | | `reset()` | the same | | `port_a_in = callable` (the outside's levels) | `port_a_in = Some(levels)` (`None`: pull-ups); set it before a read | | `port_a_out = callable` (told what the chip drives) | `port_a_out = true`, then `take_port_changes()` returns `(register, value)` in the order Python would have called | | the chip's attributes (a board copies them for a save state) | public fields; with the `serde` feature `PSG` and `PSGVariant` are `Serialize`/`Deserialize`, every field included | | `ay8910_python.conformance` and `ay8910-conformance` | `ay8910_rust::conformance` and the `ay8910-conformance` binary (`trace`, `diff`, `check`) | The port callables became state so that `PSG` stays a plain value (`Clone`, comparable, serialisable) with no closures inside. Python's private attributes are public fields without the underscore (`divider_count`, `driven`, `envelope_top`; `_levels` is `levels_now`). **Save states: the `serde` feature.** With `features = ["serde"]`, a chip saved part way through a stream and restored carries on identically (rung 4 checks it at 21,786 points against ay8910-python's own traces). Without the feature the crate has no dependencies. ## What is known, and how **This crate claims one thing: it is identical to ay8910-python at `a3bb485`** on the streams below, every input clock of them compared. Every hardware claim is ay8910-python's, with its tiers, and is not strengthened by this port. In brief, from its README and docs/claims.md: - *die-derived (the judge)*: the three codes at every input clock (on the AY-3-8910 also the port pins and every read) equal lvd's model of the AY-3-8910's die, run as a closed box, on 24 synthetic streams (25,725,326 clocks, on each of the five parts, the Yamaha parts' codes halved) and on *Gimmick!* and six of rainwarrior's 5B test programs (132,911,140 die clocks); - *die-derived, one chip removed*: the Yamaha parts' waveforms equal the YM2203 die model's SSG on 208 cases; - **unverified** there: the Yamaha parts' timing inside a step (no YM2149 die is published), and the envelope's second-half restart is the die *model's* behaviour only. ay8910-python's docs/claims.md has each claim with its evidence. This crate was transcribed from ay8910-python alone ([docs/provenance.md](docs/provenance.md)); no GPL or LGPL source was read and no closed-box model was run. ## Status All the rungs pass. Results, numbers and exact commands: [docs/status.md](docs/status.md). | Rung | What | Result | | --- | --- | --- | | 1 | ay8910-python's tests, transcribed; its 19 conformance examples traced; save states | 60 of 60 test functions; 19 of 19 traces identical | | 2 | Bus-stream lockstep: 240 seeded scripts over all five parts | 240 of 240 identical | | 3 | ay8910-python's own validation streams: 24 synthetic streams on five parts, 7 board streams on two | 134 of 134 identical, 529,772,275 clocks | | 4 | Rungs 2 and 3 again, the chip saved and restored at every checkpoint | 374 of 374 identical, 21,786 round trips | | 5 | ay8910-python's 61 seeded faults and 6 of this crate's own | 67 of 67 caught | ```text cargo test --release --features serde # rung 1; needs nothing else (CI runs it with fmt and clippy) scripts/fetch_ay8910python.sh # the reference at the pin, in a PyPy venv under external/ scripts/ladder.sh [--faults] # rungs 1-4 (and 5), then the speed report cargo run --release --example speed # 261 x real time polled (the 5B), 77 x through take_samples ``` ## Licence MIT (`LICENSE`), like the reference. `src/variants.rs` carries ayumi's MIT notice (Copyright (c) Peter Sovietov) beside `DAC_YM_MEASURED`, its YM2149 table used as data, as ay8910-python does; Matthew Westcott's AY voltages are public domain, cited there. The notice and the credits are also in [NOTICE.md](NOTICE.md).