// SPDX-License-Identifier: MIT //! The parts built around the PSG engine, and their DAC curves. //! //! What differs between General Instrument's AY-3-8910, 8912 and 8913, Yamaha's //! YM2149 and the sound half of Sunsoft's 5B is data on a [`PSGVariant`], not //! branches through the engine: how many envelope steps, which port pins are bonded //! out, what a register reads back, whether the clock can be halved, and the DAC. //! //! A DAC table maps the chip's digital output, the *code* a channel drives, to a //! level from 0.0 to 1.0. The AY parts have sixteen codes (the 4-bit volume; 0 turns //! the channel's output transistors off); the Yamaha parts have thirty-two (the 5-bit //! envelope; a 4-bit volume `v` drives code `2v + 1`). The tables are measurements //! or the manufacturers' figures, each cited where it is defined, and ay8910-python's //! docs/claims.md says how strongly each is known. The analogue path after the pins (the //! load the three outputs share, the board's filters and amplifier) belongs to the board. //! //! A transcription of ay8910-python's `variants.py` (at a3bb485): same names, same order, same //! comments. Where Rust forced a difference: //! //! - `PSGVariant` is a frozen dataclass in Python; here it is a struct whose five values are //! `const`s, so they cannot change. A new part is `PSGVariant { field: value, ..AY8910 }` //! (Python's `dataclasses.replace(AY8910, ...)`); Python's field defaults are //! [`PSGVariant::new`]. The five `const`s are written out field by field (a `const` cannot //! drop the struct a `..` update would copy from), with the same values as Python's. //! - `name`, `ports` and `dac` are `Cow`s, so that the `const`s borrow literals while a variant //! restored from a save state (the `serde` feature) owns its data. `PSGVariant` is therefore //! `Clone`, not `Copy`. //! - The four computed tables are written out as the numbers Python computes (printed by //! `repr`, which round-trips exactly), because Rust cannot call `powf` in a `const`. Each //! keeps its formula beside it, and `tests/variants.rs` computes the formula again and //! requires every entry to be the same `f64`, bit for bit. //! - `VARIANTS` is an array in Python's order; [`variant`] looks a part up by its name. use std::borrow::Cow; /// The bits each register has a use for (GI's data sheet, "Register array"). The /// AY's latches are exactly these widths: read after an address byte, the other /// bits are 0 on a real AY-3-8910 (MAME ay8910.cpp, BSD-3-Clause, "Tested and /// confirmed on hardware") and at the die model's pins (ay8910-python's docs/rederived.md, /// item 3). #[rustfmt::skip] pub const REGISTER_WIDTH_MASKS: [u8; 16] = [ 0xFF, 0x0F, 0xFF, 0x0F, 0xFF, 0x0F, 0x1F, 0xFF, 0x1F, 0x1F, 0x1F, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, ]; /// A part whose registers keep every bit written, used or not. pub const ALL_BITS: [u8; 16] = [0xFF; 16]; /// Python's `_normalised`: each voltage as a fraction of the way from the first to the last. pub fn normalised(volts: &[f64]) -> Vec { let (low, high) = (volts[0], volts[volts.len() - 1]); volts.iter().map(|v| (v - low) / (high - low)).collect() } /// Matthew Westcott's voltages, from which [`DAC_AY_MEASURED`] is [`normalised`]. #[rustfmt::skip] pub const WESTCOTT_VOLTS: [f64; 16] = [ 1.147, 1.162, 1.169, 1.178, 1.192, 1.213, 1.238, 1.299, 1.336, 1.457, 1.573, 1.707, 1.882, 2.060, 2.320, 2.580, ]; /// The AY-3-8910's sixteen levels, measured: Matthew Westcott's voltages on a ZX /// Spectrum 128's channel C pin for each fixed volume (comp.sys.sinclair, December /// 2001, placed in the public domain by him; quoted in MAME's ay8910.cpp, lines /// 681-716), with the level-0 voltage taken as silence and level 15 as 1.0. /// /// `normalised(WESTCOTT_VOLTS)`. #[rustfmt::skip] pub const DAC_AY_MEASURED: [f64; 16] = [ 0.0, 0.010467550593161132, 0.01535240753663644, 0.021632937892533087, 0.03140265177948355, 0.04605722260990932, 0.06350314026517792, 0.10607117934403343, 0.13189113747383116, 0.2163293789253315, 0.29727843684577804, 0.39078855547801816, 0.5129099790648987, 0.6371249127704117, 0.8185624563852058, 1.0, ]; /// The AY-3-8910's levels as a plain logarithmic ladder: each step down divides /// the voltage by the square root of two (3 dB), and level 0 is off. GI's data /// sheet says only that "the D/A conversion is performed in logarithmic steps with /// a normalized voltage range of from 0 to 1 Volt" and draws the curve without /// numbers (Fig. 3); the square root of two is Yamaha's figure for the same sixteen /// volumes (below), and the measurements run about 2.8 dB a step on average. /// /// `(0.0, *(2.0 ** ((code - 15) / 2) for code in range(1, 16)))`. // The numbers Python computes, written out: one of them is 1/sqrt(2) to the last bit. #[allow(clippy::approx_constant)] #[rustfmt::skip] pub const DAC_AY_LOGARITHMIC: [f64; 16] = [ 0.0, 0.0078125, 0.011048543456039806, 0.015625, 0.02209708691207961, 0.03125, 0.04419417382415922, 0.0625, 0.08838834764831845, 0.125, 0.1767766952966369, 0.25, 0.3535533905932738, 0.5, 0.7071067811865476, 1.0, ]; /// The YM2149's thirty-two levels as Yamaha's catalog draws them (Fig. 1, "Output /// level of DA convertor": 1 V, .841, .707, .595, .5 ...): each step down divides /// by the fourth root of two, 1.505 dB, so a volume step is the AY's 3 dB. Codes 0 /// and 1 are both silence here: a volume of 0 drives code 1, and "volume 0 is /// completely silent" on the 5B's hardware (Sunsoft_5B_audio.wiki, "Tone"). The /// YM2203's die model, run as a closed box, agrees: one 32-step ramp shows 31 /// levels, the lowest 0, and volume 0 drives it (ay8910-python's scripts/ssg_referee.py, /// `die_levels`). /// /// `(0.0, 0.0, *(2.0 ** ((code - 31) / 4) for code in range(2, 32)))`. // The numbers Python computes, written out: one of them is 1/sqrt(2) to the last bit. #[allow(clippy::approx_constant)] #[rustfmt::skip] pub const DAC_YM_DATASHEET: [f64; 32] = [ 0.0, 0.0, 0.0065695032441696445, 0.0078125, 0.009290680585958758, 0.011048543456039806, 0.013139006488339289, 0.015625, 0.018581361171917516, 0.02209708691207961, 0.026278012976678578, 0.03125, 0.03716272234383503, 0.04419417382415922, 0.052556025953357156, 0.0625, 0.07432544468767006, 0.08838834764831845, 0.10511205190671431, 0.125, 0.14865088937534013, 0.1767766952966369, 0.21022410381342863, 0.25, 0.29730177875068026, 0.3535533905932738, 0.42044820762685725, 0.5, 0.3535533905932738, 0.5, 0.7071067811865476, 1.0, ]; /// The YM2149's levels, measured: the table of Peter Sovietov's ayumi (ayumi.c /// `YM_dac_table`, MIT), taken from a real chip. The table is ayumi's, under /// its licence: /// /// ```text /// The MIT License /// /// Copyright (c) Peter Sovietov, http://sovietov.com /// /// Permission is hereby granted, free of charge, to any person obtaining a copy /// of this software and associated documentation files (the "Software"), to deal /// in the Software without restriction, including without limitation the rights /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell /// copies of the Software, and to permit persons to whom the Software is /// furnished to do so, subject to the following conditions: /// /// The above copyright notice and this permission notice shall be included in /// all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN /// THE SOFTWARE. /// ``` #[rustfmt::skip] pub const DAC_YM_MEASURED: [f64; 32] = [ 0.0, 0.0, 0.00465400167849, 0.00772106507973, 0.0109559777218, 0.0139620050355, 0.0169985503929, 0.0200198367285, 0.024368657969, 0.029694056611, 0.0350652323186, 0.0403906309606, 0.0485389486534, 0.0583352407111, 0.0680552376593, 0.0777752346075, 0.0925154497597, 0.111085679408, 0.129747463188, 0.148485542077, 0.17666895552, 0.211551079576, 0.246387426566, 0.281101701381, 0.333730067903, 0.400427252613, 0.467383840696, 0.53443198291, 0.635172045472, 0.75800717174, 0.879926756695, 1.0, ]; /// The Sunsoft 5B's levels: a logarithmic DAC of 1.5 dB a step with codes 0 and 1 /// silent, as rainwarrior measured on the cartridge (Sunsoft_5B_audio.wiki, /// "Envelope" and "Output"): Yamaha's ladder. The 5B's amplifier after it, which /// compresses above volume 14, is the board's. pub const DAC_5B: [f64; 32] = DAC_YM_DATASHEET; /// What differs between the parts. /// /// `envelope_steps`: 16 on the AY parts (a 4-bit envelope counter clocked every /// second time the period counter expires), 32 on Yamaha's (a 5-bit counter /// clocked every time), so a ramp lasts the same and has twice the steps. /// /// `ports`: which I/O ports reach pins: "AB" (AY-3-8910, YM2149), "A" /// (AY-3-8912) or "" (AY-3-8913, the 5B). Registers 14 and 15 exist on every part; /// a port that is not bonded out sees only its own pull-ups. /// /// `register_masks`: what each register keeps of a byte written, and so what /// a read returns. The AY parts' latches are only as wide as the register, and /// the missing bits read 0 after an address byte (MAME ay8910.cpp, BSD-3-Clause, /// the comment over its read masks: "Tested and confirmed on hardware"; and the /// die model's pins, ay8910-python's docs/rederived.md, item 3); the YM2149 keeps and /// returns all eight (the same comment: "YM2149: no anomaly"). /// /// `clock_divider`: input clocks per internal clock. 1 on the AY parts. The /// YM2149 has a pin for it (/SEL: high 1, low 2), so there it is only the pin's /// level at power-on and [`PSG::clock_divider`](crate::PSG::clock_divider) may be changed. /// The 5B is a YM2149 with /SEL low for good: 2. /// /// `dac`: the level of each code, 0.0 to 1.0, one entry per code (16 or 32). #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct PSGVariant { pub name: Cow<'static, str>, pub envelope_steps: u8, pub ports: Cow<'static, str>, pub register_masks: [u8; 16], pub clock_divider: u8, pub has_sel_pin: bool, pub dac: Cow<'static, [f64]>, } impl PSGVariant { /// A part with Python's defaults for every field but the name: the AY-3-8910's. pub const fn new(name: &'static str) -> PSGVariant { PSGVariant { name: Cow::Borrowed(name), envelope_steps: 16, ports: Cow::Borrowed("AB"), register_masks: REGISTER_WIDTH_MASKS, clock_divider: 1, has_sel_pin: false, dac: Cow::Borrowed(&DAC_AY_MEASURED), } } /// How many DAC codes a channel can drive: 16 or 32. pub fn codes(&self) -> usize { self.dac.len() } } /// General Instrument's 40-pin part: two I/O ports. pub const AY8910: PSGVariant = PSGVariant::new("AY-3-8910"); /// The same die in 28 pins: port A only. pub const AY8912: PSGVariant = PSGVariant { name: Cow::Borrowed("AY-3-8912"), envelope_steps: 16, ports: Cow::Borrowed("A"), register_masks: REGISTER_WIDTH_MASKS, clock_divider: 1, has_sel_pin: false, dac: Cow::Borrowed(&DAC_AY_MEASURED), }; /// The same die in 24 pins: no ports, BC2 tied high inside. pub const AY8913: PSGVariant = PSGVariant { name: Cow::Borrowed("AY-3-8913"), envelope_steps: 16, ports: Cow::Borrowed(""), register_masks: REGISTER_WIDTH_MASKS, clock_divider: 1, has_sel_pin: false, dac: Cow::Borrowed(&DAC_AY_MEASURED), }; /// Yamaha's SSG: a 32-step envelope, the /SEL pin, every register bit readable. pub const YM2149: PSGVariant = PSGVariant { name: Cow::Borrowed("YM2149"), envelope_steps: 32, ports: Cow::Borrowed("AB"), register_masks: ALL_BITS, clock_divider: 1, has_sel_pin: true, dac: Cow::Borrowed(&DAC_YM_MEASURED), }; /// The sound of Sunsoft's 5B mapper (Gimmick!): a YM2149 run with its clock halved, /// no ports, and nothing to read it with. pub const SUNSOFT_5B: PSGVariant = PSGVariant { name: Cow::Borrowed("Sunsoft 5B"), envelope_steps: 32, ports: Cow::Borrowed(""), register_masks: ALL_BITS, clock_divider: 2, has_sel_pin: false, dac: Cow::Borrowed(&DAC_5B), }; /// Every part, in Python's `VARIANTS` order. pub const VARIANTS: [PSGVariant; 5] = [AY8910, AY8912, AY8913, YM2149, SUNSOFT_5B]; /// Python's `VARIANTS[name]`: the part of that name. pub fn variant(name: &str) -> Option { VARIANTS.into_iter().find(|v| v.name == name) }