//! The Namco 06xx bus controller (`docs/custom-io.md`), with the NMI cadence //! MAME's `namco06.cpp` gives it: a timer at twice the divided 48 kHz clock, //! alternating an active phase (chip selects and, except for the first one //! of a read, NMI asserted) and an idle phase (both cleared). Times are //! attoseconds on MAME's clock (`galaga::sched`): the 06xx's 48 kHz clock is //! not a whole number of Z80 cycles there, and its ticks cut slices. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct N06xx { pub control: u8, timer_state: bool, read_stretch: bool, /// The next timer callback (attoseconds), if running. next_tick: Option, period: u128, pub nmi_line: bool, } impl N06xx { pub fn new() -> N06xx { N06xx { control: 0, timer_state: false, read_stretch: false, next_tick: None, period: 0, nmi_line: false, } } pub fn read_mode(&self) -> bool { self.control & 0x10 != 0 } pub fn selected(&self, slot: u8) -> bool { self.control & (1 << slot) != 0 } /// `ctrl_w_sync`. Returns true when a read transfer starts, so the /// selected chip can rewind its reply sequence. pub fn control_write(&mut self, value: u8, now: u128) -> bool { self.control = value; if value & 0xe0 == 0 { self.next_tick = None; self.timer_state = false; self.nmi_line = false; return false; } let read = value & 0x10 != 0; if read { self.nmi_line = false; self.read_stretch = true; } else { self.read_stretch = false; } // Half a period of clock / 2^shifts, from the next 48 kHz edge. let (expire, period) = super::sched::Sched::n06_start(now, (value & 0xe0) >> 5); self.next_tick = Some(expire); self.period = period; read } /// The next callback time, if the timer runs. pub fn next_tick(&self) -> Option { self.next_tick } /// `nmi_generate`: one callback. Returns true when NMI is asserted, i.e. /// a rising edge the main CPU latches. pub fn tick(&mut self) -> bool { let next = self.next_tick.expect("tick with the timer stopped"); self.next_tick = Some(next + self.period); self.timer_state = !self.timer_state; let assert = self.timer_state && !self.read_stretch; self.nmi_line = assert; self.read_stretch = false; assert } } impl Default for N06xx { fn default() -> Self { N06xx::new() } }