//! What differs between the parts, as data. //! //! Nothing in `chip.rs` asks which part it is running: it reads these fields. //! Each field names its spec section (`docs/spec.md`) and how well it is known. //! //! Transcribed from ym2612-python's `variant.py`. Where Rust forced a difference: //! //! - Python's frozen dataclass is a `Copy` struct of plain values; its constants are `const`s, //! so nothing can change them (the frozen record's guarantee). `status_ports` is an array. //! - `dataclasses.replace(...)` is struct update syntax: `FMVariant { ladder: false, ..YM2612_DISCRETE }`. //! - With the `serde` feature a variant is saved as its whole record. `name` is a //! `&'static str`, so on loading the name must be one of the two parts' names //! (`YM2612`, `YM3438`); the other fields are taken from the saved record. /// One part. A frozen record of plain values. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct FMVariant { /// For messages and save-state labels. pub name: &'static str, /// Whether the output stage is the discrete chip's (spec 15.2). The discrete /// YM2612's converter has a step at mid-scale, measured on several consoles and /// described by Yamaha as a muted channel being "faintly audible"; the YM3438 /// and the ASIC do not show it (spec 15.3, measured). pub ladder: bool, /// Added, in steps of the 9-bit converter, to a slot whose value is zero or /// positive, to a slot whose value is negative, and to a slot whose channel is /// panned away from this side (spec 15.2.5, unknown U12, decision T10). /// PROVISIONAL, emulator tier: +4, -3 and +4 are ymfm's (BSD-3-Clause), which /// gives no source for them. Two hardware reports of the mid-scale step alone /// are smaller (GManiac, "3-4x"; Kabuto, "a jump of 3"). Not measured, not /// fitted to anything. pub ladder_positive: i32, pub ladder_negative: i32, pub ladder_idle: i32, /// Which of the four addresses return the status byte; the others leave the bus /// undriven and `read` returns None (spec 12.5, contradiction C4, decision /// T3). PROVISIONAL: sources disagree. The discrete chip: address 0 only /// (Yamaha's YM2612 manual, Sega's Technical Bulletin 11, Eke's measurement on a /// VA4 model 1). The ASIC: all four (Eke's measurement on a VA0 model 2, and one /// game that plays slowly there). Stef measured the reverse in 2023. pub status_ports: [bool; 4], /// The accumulator's upper limit (spec 14.2, contradiction C8, decision T5). /// PROVISIONAL: +255, a 9-bit signed value. GManiac measured +256, "AFAIR". pub accumulator_max: i32, } /// The discrete NMOS YM2612 of Mega Drive model 1 boards. pub const YM2612_DISCRETE: FMVariant = FMVariant { name: "YM2612", ladder: true, ladder_positive: 4, ladder_negative: -3, ladder_idle: 4, status_ports: [true, false, false, false], accumulator_max: 255, }; /// The YM3438's behaviour, as measured mostly on the FM block of the model 2's /// ASIC (decision T18). It also stands for a discrete YM3438 until a source shows /// the two differ (unknown U20, decision T17). Reported and not measured, so not /// modelled: the ASIC being louder and clipping lower (unknown U13). pub const YM3438_ASIC: FMVariant = FMVariant { name: "YM3438", ladder: true, ladder_positive: 4, ladder_negative: -3, ladder_idle: 4, status_ports: [true, true, true, true], accumulator_max: 255, }; #[cfg(feature = "serde")] mod saved { //! Save states: the whole record, its name interned to one of the parts' names. use super::{FMVariant, YM2612_DISCRETE, YM3438_ASIC}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[derive(Serialize, Deserialize)] struct Record { name: String, ladder: bool, ladder_positive: i32, ladder_negative: i32, ladder_idle: i32, status_ports: [bool; 4], accumulator_max: i32, } impl Serialize for FMVariant { fn serialize(&self, serializer: S) -> Result { Record { name: self.name.to_string(), ladder: self.ladder, ladder_positive: self.ladder_positive, ladder_negative: self.ladder_negative, ladder_idle: self.ladder_idle, status_ports: self.status_ports, accumulator_max: self.accumulator_max, } .serialize(serializer) } } impl<'de> Deserialize<'de> for FMVariant { fn deserialize>(deserializer: D) -> Result { let record = Record::deserialize(deserializer)?; let name = [YM2612_DISCRETE.name, YM3438_ASIC.name] .into_iter() .find(|known| *known == record.name) .ok_or_else(|| { serde::de::Error::custom(format!("unknown variant name {:?}", record.name)) })?; Ok(FMVariant { name, ladder: record.ladder, ladder_positive: record.ladder_positive, ladder_negative: record.ladder_negative, ladder_idle: record.ladder_idle, status_ports: record.status_ports, accumulator_max: record.accumulator_max, }) } } }