//! The four parts this core models, as data. //! //! Section numbers ("spec 6.2") are those of sn76489-python's `docs/spec.md`; S, C and U //! numbers are its sources, contradictions and unknowns. Each field says where its values //! come from and how far they can be trusted: //! //! - *documented*: a manufacturer's or community document says so; //! - *measured*: a capture from hardware says so; //! - *provisional*: no source settles it; the value is the hand-off brief's decision //! (D numbers, sn76489-python's `docs/handoff-brief.md`) and a measurement would change the //! value, not the code; //! - *choice*: nothing speaks to it and this core had to pick. //! //! No logic anywhere asks which variant it is running: it reads these fields. //! //! A transcription of sn76489-python's `src/sn76489_python/variants.py` at f8157f4. Where Rust //! forced a difference: //! //! - Python computes `IDEAL_VOLUME_TABLE` at import as `10 ** (-0.1 * step)`. A Rust `const` //! cannot call `powf`, and the variants must be `const` values, so the sixteen numbers are //! written out: each is the shortest decimal that reads back as exactly the `f64` Python //! computes (glibc's `pow`, the same function Rust's `powf` calls). `tests/variants.rs` //! checks every entry against `10f64.powf(-0.1 * step as f64)` bit for bit. //! - `PSGVariant` is a `Copy` struct of `pub` fields: Python's frozen dataclass. A board //! replaces a field with struct update syntax, `PSGVariant { volume_table: measured, //! ..SEGA_VDP_PSG }`, where Python writes `dataclasses.replace(SEGA_VDP_PSG, volume_table=...)`. //! - `name` is a `&'static str`. With the `serde` feature a variant is saved with every field //! and its name, and read back only when the name is one of the four parts below (a board's //! replaced variant keeps its part's name, as `dataclasses.replace` does). //! - Integer fields are `u32`; the attenuation and latch fields `u32` too, as the chip's state. /// Amplitude for each 4-bit attenuation: 2 dB a step (a voltage ratio of 10^-0.1), /// value 0 loudest, value 15 off whatever the step before it was. Documented: spec /// 7.1 and 7.2 (TI's data sheet, Table 1; Sega's manuals print all sixteen rows). /// It is the nominal curve: TI allows each weight a tolerance (spec 7.3) and no /// measured curve of any Sega part is in hand (U4). /// /// The values of `10 ** (-0.1 * step)` for steps 0 to 14, then 0.0 (see the module header). #[rustfmt::skip] pub const IDEAL_VOLUME_TABLE: [f64; 16] = [ 1.0, 0.7943282347242815, 0.6309573444801932, 0.5011872336272722, 0.3981071705534972, 0.31622776601683794, 0.25118864315095796, 0.19952623149688792, 0.15848931924611134, 0.12589254117941673, 0.1, 0.07943282347242814, 0.0630957344480193, 0.05011872336272722, 0.03981071705534971, 0.0, ]; /// What differs between the parts. Configuration: nothing here changes at run time. #[derive(Clone, Copy, Debug, PartialEq)] pub struct PSGVariant { /// A label for messages and reports. Nothing compares against it. pub name: &'static str, /// Stages in the noise shift register. 15 on the TI chip: measured (spec 6.2.1, /// Kortink's BBC Micro capture repeats after 32,767 bits). 16 on Sega's parts: /// documented (Sega's Game Gear manual, tier 2; Maxim's page, tier 3). pub lfsr_bits: u32, /// Stages exclusive-ORed to make the new bit in white noise, as a mask with the /// output end at bit 0. $0003 on the TI chip: measured (spec 6.2.3, the rule /// reproduces Kortink's capture). $0009 on Sega's parts: documented at tier 3 /// only (spec 6.2.4, Maxim's page); no capture of a Sega part is in hand (U3). pub white_noise_taps: u32, /// Whether the Game Gear's stereo register exists. Documented (spec section 9). pub stereo: bool, /// The period, in ticks between toggles, that a tone value of 0 stands for. /// Sega's parts: 1, the same as value 1. PROVISIONAL (D3; spec 5.6, C2, U1: /// Maxim says a constant +1, MAME's note says "verified on SMS hardware"). /// TI chip: 1,024, what a 10-bit down counter loaded with 0 would count. /// PROVISIONAL (D4; spec 5.7, C3, U2: MAME's note plus an inference from TI's /// description of the counter; emulator tier). pub tone_zero_period: u32, /// The attenuation all four channels hold at power-on. Sega's parts: 15, silent. /// Documented for the Game Gear (spec 10.1, Sega's manual), tier 3 and hedged /// for the Master System (spec 10.2), unsettled for the Mega Drive (U12). /// TI chip: 0, full volume: the chip has no reset and "from a cold boot the PSG /// emits a medium volume tone" (spec 10.4, MacDonald's SC-3000H notes); the /// exact value is MAME's rendering. PROVISIONAL (D5, U13). pub power_on_attenuation: u32, /// The register code (0 to 7, spec 2.2) latched at power-on, which matters only /// to a program whose first write is a data byte. 0, the first tone value, as /// Maxim's 2003 text has it. PROVISIONAL (D6; spec 10.5, C8, U14: MAME says 3). pub power_on_latch: u32, /// Whether a data byte (bit 7 clear) reaches the attenuation and noise registers. /// Documented for Sega's part at tier 3 (spec 3.6, Maxim: "The data byte is NOT /// ignored"). On the TI chip only MAME says so: PROVISIONAL (U8). pub data_byte_reaches_all_registers: bool, /// At noise rate 3 the shift register is clocked by the third tone generator's /// output (documented: spec 6.1.3, TI's block diagram and Sega's manual). This is /// the level that output must change *to* for a shift. No source says which edge /// (U6). 1 is a CHOICE, by analogy with Maxim's "when its input changes from 0 /// to 1" for the fixed rates. pub noise_shift_level: u32, /// Whether the Mega Drive's test override exists (spec section 11). /// PROVISIONAL and untested (D7, U15): one paragraph on Plutiedev is the only /// source. pub test_override: bool, /// Input clocks for which READY is low after a write: about 32 on the TI chip /// (documented: spec 3.8, TI's data sheet), none on Sega's parts ("there is no /// need for a wait", Sega's Game Gear manual). The core does nothing with it: a /// board that wires READY to the CPU's WAIT, as the SC-3000 does, reads it here. pub ready_clocks: u32, /// Amplitude per attenuation value. The ideal table unless a board passes a /// measured one (D11): `PSGVariant { volume_table: ..., ..variant }`. pub volume_table: [f64; 16], } impl PSGVariant { /// The dataclass's defaults: every field but the name, the register's length and the /// taps, as `variants.py` declares them. const fn with_defaults(name: &'static str, lfsr_bits: u32, white_noise_taps: u32) -> Self { PSGVariant { name, lfsr_bits, white_noise_taps, stereo: false, tone_zero_period: 1, power_on_attenuation: 15, power_on_latch: 0, data_byte_reaches_all_registers: true, noise_shift_level: 1, test_override: false, ready_clocks: 0, volume_table: IDEAL_VOLUME_TABLE, } } } /// The discrete Texas Instruments chip of the SG-1000 and SC-3000 (spec section 1). pub const SN76489AN: PSGVariant = PSGVariant { tone_zero_period: 1024, power_on_attenuation: 0, ready_clocks: 32, ..PSGVariant::with_defaults("SN76489AN", 15, 0x0003) }; /// The PSG inside the Mark III's and Master System's VDP. pub const SEGA_VDP_PSG: PSGVariant = PSGVariant::with_defaults("Sega VDP PSG", 16, 0x0009); /// The Game Gear's: the same with a stereo register. pub const GAME_GEAR_PSG: PSGVariant = PSGVariant { stereo: true, ..PSGVariant::with_defaults("Game Gear PSG", 16, 0x0009) }; /// The PSG inside the Mega Drive's VDP (YM7101). That its noise is the Master /// System's rests on one line of Maxim's history and on MAME (spec 6.3). pub const YM7101_PSG: PSGVariant = PSGVariant { test_override: true, ..PSGVariant::with_defaults("YM7101 PSG", 16, 0x0009) }; /// The four parts, in the order the reference's tests list them (`ALL`). Not in /// `variants.py`: the ladder's tools and a save state find a part by its name here. pub const VARIANTS: [PSGVariant; 4] = [SN76489AN, SEGA_VDP_PSG, GAME_GEAR_PSG, YM7101_PSG]; /// The part with this name (`PSGVariant::name`), if it is one of the four. pub fn variant(name: &str) -> Option { VARIANTS.iter().copied().find(|v| v.name == name) } #[cfg(feature = "serde")] mod serde_impl { //! Every field, the name included; the name is matched back to a part's `&'static str`. use super::{variant, PSGVariant}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[derive(Serialize, Deserialize)] struct Saved { name: String, lfsr_bits: u32, white_noise_taps: u32, stereo: bool, tone_zero_period: u32, power_on_attenuation: u32, power_on_latch: u32, data_byte_reaches_all_registers: bool, noise_shift_level: u32, test_override: bool, ready_clocks: u32, volume_table: [f64; 16], } impl Serialize for PSGVariant { fn serialize(&self, serializer: S) -> Result { Saved { name: self.name.to_string(), lfsr_bits: self.lfsr_bits, white_noise_taps: self.white_noise_taps, stereo: self.stereo, tone_zero_period: self.tone_zero_period, power_on_attenuation: self.power_on_attenuation, power_on_latch: self.power_on_latch, data_byte_reaches_all_registers: self.data_byte_reaches_all_registers, noise_shift_level: self.noise_shift_level, test_override: self.test_override, ready_clocks: self.ready_clocks, volume_table: self.volume_table, } .serialize(serializer) } } impl<'de> Deserialize<'de> for PSGVariant { fn deserialize>(deserializer: D) -> Result { let s = Saved::deserialize(deserializer)?; let part = variant(&s.name) .ok_or_else(|| serde::de::Error::custom(format!("unknown PSG part {}", s.name)))?; Ok(PSGVariant { name: part.name, lfsr_bits: s.lfsr_bits, white_noise_taps: s.white_noise_taps, stereo: s.stereo, tone_zero_period: s.tone_zero_period, power_on_attenuation: s.power_on_attenuation, power_on_latch: s.power_on_latch, data_byte_reaches_all_registers: s.data_byte_reaches_all_registers, noise_shift_level: s.noise_shift_level, test_override: s.test_override, ready_clocks: s.ready_clocks, volume_table: s.volume_table, }) } } }