//! A hand-written model of the 54xx noise generator (the user's decision of //! 2026-09-11: HLE, no MB88xx core). What it reproduces was measured from //! MAME's instruction-level 54xx by tracing the MCU's port writes through //! the power-on bang and a player death (`docs/sound-hardware.md`, //! "What MAME's 54xx does for Galaga"): //! //! - Galaga only ever sends the boot burst (type-A and type-B parameters, //! then play A, play A, play B, play B) and, at each player death, the //! same four play bytes. Only output channel 1 (O4-O7, the 450 Hz //! band-pass) ever moves; channels 0 and 2 stay at 0. //! - The sound is a random bit times an amplitude: 13 for about 120 //! updates, then 15, 11, 8, 6, 4, 3, 2, 1 for about 784 updates each, //! then silence: 6,377 updates, 2.66 s. One update every 106.8 MCU //! cycles (417 µs). //! - The bit sequence is not a small LFSR (linear complexity 232) and //! differs between two triggers, so no generator can match MAME bit for //! bit; a 17-bit maximal LFSR gives the same white noise. //! //! A and B were never observed apart, because the game never sends them //! apart; both trigger the one schedule, and a trigger restarts it. /// One update every 106.8 MCU cycles at 256 kHz = 1,282 T-states at 3.072 MHz. pub const UPDATE_T_STATES: u64 = 1_282; /// (amplitude, updates) as measured; the last entry ends the sound. pub const SCHEDULE: [(u8, u32); 9] = [ (13, 120), (15, 784), (11, 784), (8, 784), (6, 784), (4, 784), (3, 784), (2, 784), (1, 784), ]; #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct N54xx { /// Parameter bytes still expected after a 0x3n/0x4n/0x6n command. args_remaining: u8, lfsr: u32, /// Position in the schedule, if a sound is playing. step: Option<(usize, u32)>, /// Absolute T-state of the next update. next_update: u64, /// The current channel-1 nibble. pub output: u8, /// Every byte received, for the record. pub bytes: Vec, } impl N54xx { pub fn new() -> N54xx { N54xx { args_remaining: 0, lfsr: 0x1_ffff, step: None, next_update: 0, output: 0, bytes: Vec::new(), } } pub fn reset(&mut self) { let lfsr = self.lfsr; *self = N54xx::new(); self.lfsr = lfsr; } /// A byte from the 06xx (write mode, slot 3) at T-state `now`. pub fn write(&mut self, value: u8, now: u64) { self.bytes.push(value); if self.args_remaining > 0 { self.args_remaining -= 1; return; } match value >> 4 { 0x1 | 0x2 | 0x5 => { self.step = Some((0, 0)); self.next_update = now; } 0x3 | 0x4 => self.args_remaining = 4, 0x6 => self.args_remaining = 5, _ => {} } } /// Advance to T-state `now`; the output nibble holds between updates. pub fn run_to(&mut self, now: u64) { while self.step.is_some() && self.next_update <= now { let (index, done) = self.step.unwrap(); let (amplitude, length) = SCHEDULE[index]; // x^17 + x^14 + 1, maximal length. let bit = (self.lfsr ^ (self.lfsr >> 3)) & 1; self.lfsr = (self.lfsr >> 1) | (bit << 16); self.output = if bit != 0 { amplitude } else { 0 }; self.next_update += UPDATE_T_STATES; let done = done + 1; self.step = if done >= length { if index + 1 < SCHEDULE.len() { Some((index + 1, 0)) } else { self.output = 0; None } } else { Some((index, done)) }; } } pub fn playing(&self) -> bool { self.step.is_some() } } impl Default for N54xx { fn default() -> Self { N54xx::new() } }