// SPDX-License-Identifier: MIT //! Chip variants as data rows (docs/behaviour.md, rung 3: the output curve). //! //! No logic difference between makers or date codes of C012294 is known (U8), so //! there is one engine; a row carries what may differ: the analogue output curve. //! //! A transcription of pokey-python's `variants.py` (at 6ed1ffa): same names, same order, same //! comments. Where Rust forced a difference: //! //! - Python's module-level constants `C012294` and `LINEAR` are computed with `math.exp` at //! import; Rust cannot run `exp` in a `const`, so they are `LazyLock` statics built by the //! transcribed `_fitted()` and `_linear()` (here `fitted()` and `linear()`, public so a board //! can take an owned row). `f64::exp` and Python's `math.exp` both call the platform's //! libm, and the lockstep (rung 2) compares every `mix()` result bit for bit. //! - The tuples are `Vec` (a frozen dataclass's tuple of floats); the row is `Clone`, and //! a chip owns its copy. use std::sync::LazyLock; /// One row: a name and the output transfer curve used by `POKEY::mix()`. /// /// `dac` maps a volume code 0-15 to one channel's current, in units of the /// least significant step; `mix_curve` maps the summed current to the /// output, normalised so that four channels at volume 15 give 1.0. #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[allow(clippy::upper_case_acronyms)] pub struct POKEYVariant { pub name: String, pub dac: Vec, /// indexed by round(sum * 4), sum of dac values pub mix_curve: Vec, } /// `_linear()` in the reference. pub fn linear() -> POKEYVariant { let dac: Vec = (0..16).map(|v| v as f64).collect(); let top = 4.0 * dac[15]; let curve = (0..(top * 4.0) as usize + 1) .map(|i| (i as f64 / 4.0) / top) .collect(); POKEYVariant { name: "linear".to_string(), dac, mix_curve: curve, } } // B3.9 FITTED to the Altirra manual's words, not to any table (decision 5; // AHRM 5.3 "Volume control"): the two high volume bits are slightly mismatched // against the two low ones, so the steps 3-4, 7-8 and 11-12 are wider; the // summed output is linear up to one channel's range (15) and compresses above // it, two channels at 15 giving about 1.5 times one and the full 60 about twice // one. HIGH_BIT_RATIO and the knee are chosen to meet those three words, nothing // else; another fit meeting them is as good. /// FITTED: weight of bits 2-3 relative to binary; steps 3-4 etc. 1.2x wider pub const HIGH_BIT_RATIO: f64 = 1.05; /// FITTED: "nearly linear ... where the volume sum ... is 15 or less" const KNEE: f64 = 15.0; /// FITTED: with GAIN, f(30) = 1.5 f(15) and f(60) = 2.0 f(15) const TAU: f64 = 31.17; /// FITTED const GAIN: f64 = 19.63; /// `_fitted()` in the reference. pub fn fitted() -> POKEYVariant { let dac: Vec = (0..16u32) .map(|v| (v & 3) as f64 + HIGH_BIT_RATIO * (v & 12) as f64) .collect(); fn curve(s: f64) -> f64 { if s <= KNEE { return s; } KNEE + GAIN * (1.0 - (-(s - KNEE) / TAU).exp()) } let top = curve(4.0 * dac[15]); let steps = (4.0 * dac[15] * 4.0) as usize + 1; let mix_curve = (0..steps).map(|i| curve(i as f64 / 4.0) / top).collect(); POKEYVariant { name: "C012294".to_string(), dac, mix_curve, } } /// The chip, with the fitted output curve. pub static C012294: LazyLock = LazyLock::new(fitted); /// The same engine with a linear output: four codes summed and scaled to 1.0. pub static LINEAR: LazyLock = LazyLock::new(linear);