//! MAME's scheduler, reproduced (`emu/schedule.cpp`, `emu/screen.cpp`, //! `namco06.cpp`, `galaga.cpp`; read 2026-10-02 at 0.281, checked against //! the 0.285 binary). The CPUs race on shared RAM inside a frame, and where //! a read and a write fall in one slice the order MAME's scheduler gives //! them decides what the game sees; matching MAME means slicing as it does: //! //! - Time is attoseconds. A device's local time is its cycle count times //! its attoseconds per cycle (`HZ_TO_ATTOSECONDS`, truncated). //! - A slice targets base + 1/6000 s (`set_maximum_quantum`), or the next //! timer if sooner. Each device in list order (main, sub, sub2, then the //! 51xx and 54xx MCUs) runs the whole cycles that fit, computed MAME's way //! (a shifted 64/32 division); a Z80 overshoots by its last instruction. //! A device that ends short of the target (cut by a synchronizing write, //! or rounded down to its own cycle) pulls the target back to its time for //! the devices after it, and the next slice starts there. //! - The two MCUs (MB8843/MB8844 at 1.536 MHz / 6: one cycle per 12 Z80 //! cycles) take part as clocks only: our 51xx and 54xx are high level, but //! MAME's run, so their whole-cycle steps cut its slices. Whether they are //! held in reset or running, MAME advances them the same way; only an //! instruction overshoot or a sync of their own (not modelled) differs. //! - The 51xx's own syncs, approximated (Aubrey's choice, 2026-10-02, over //! running its firmware): MAME's 51xx program writes its O port twice a //! frame, and each write synchronizes, ending its slice there. Measured with //! MAME's debugger tracing the MCU (cycles counted from its release from //! reset, 4,224 to a frame): none in switch mode; in credit mode with no //! game, the first write at 1586, 1584, 1582, 1585, 1583 cycles into its //! frames in turn from the first credit-mode frame, the second 48 later; //! in a game at 1584 and 1639. The scheduler ends the 51xx's slice after //! each, as MAME does. //! - Timers: vblank start (frame period after the last), vblank end and //! scanline 0 (40 lines after it), the sub2 NMI timer (lines 64 and 192, //! `time_until_pos`), the 06xx clock, and the sound manager's 50 Hz update. /// Attoseconds per second. pub const AS: u128 = 1_000_000_000_000_000_000; /// A device's cycle in attoseconds and MAME's division for cycles-in-delta. #[derive(Clone, Copy, Debug)] pub struct Clock { pub aps: u128, shift: u32, divisor: u128, } impl Clock { const fn new(cycles_per_second: u128) -> Clock { let aps = AS / cycles_per_second; let mut attos = aps; let mut shift = 0; while attos >= 1 << 31 { shift += 1; attos >>= 1; } Clock { aps, shift, divisor: attos, } } /// `divu_64x32(delta >> divshift, divisor)`: the cycles MAME runs. pub fn cycles_in(&self, delta: u128) -> u64 { ((delta >> self.shift) / self.divisor) as u64 } } pub const Z80: Clock = Clock::new(3_072_000); pub const MCU: Clock = Clock::new(256_000); /// 1/6000 s. pub const QUANTUM: u128 = AS / 6000; /// The pixel clock's period, the frame, a scanline, a pixel, vblank. pub const PIXEL_CLOCK_AS: u128 = AS / 6_144_000; pub const FRAME: u128 = PIXEL_CLOCK_AS * 384 * 264; pub const SCANTIME: u128 = FRAME / 264; pub const PIXELTIME: u128 = FRAME / (264 * 384); pub const VBLANK_PERIOD: u128 = SCANTIME * 40; /// The sound manager's periodic stream update. pub const SOUND_UPDATE: u128 = AS / 50; /// The 06xx's 48 kHz clock: `HZ_TO_ATTOSECONDS(48000)`. pub const N06_CLOCK_HZ: u128 = 48_000; #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Sched { /// `m_basetime`. pub base: u128, /// The MCUs' cycle counts (local time = count x MCU.aps). pub mcu: [u64; 2], /// `m_vblank_start_time`. pub vblank_start: u128, pub next_vblank_begin: u128, pub next_vblank_end: Option, pub next_scanline0: u128, /// The sub2 NMI timer: its expiry and the scanline it fires for. pub next_sub2: u128, pub sub2_line: u32, pub next_sound: u128, /// The 51xx's cycle count at its last release from reset (its cycles /// are counted from there), and the frame (of 4,224 of its cycles) in /// which its credit-mode pattern starts. pub mcu51_reset: Option, pub mcu51_credit_frame: Option, } /// The 51xx's measured O-write offsets (`galaga::sched` docs). const O_IDLE: [u64; 5] = [1586, 1584, 1582, 1585, 1583]; const O_IDLE_GAP: u64 = 48; const O_GAME: [u64; 2] = [1584, 1639]; pub const MCU_FRAME: u64 = 4224; impl Sched { /// At power-on (time 0, a vblank start). pub fn new() -> Sched { let mut s = Sched { base: 0, mcu: [0, 0], vblank_start: 0, next_vblank_begin: FRAME, next_vblank_end: None, next_scanline0: 0, next_sub2: 0, sub2_line: 64, next_sound: SOUND_UPDATE, mcu51_reset: None, mcu51_credit_frame: None, }; s.next_scanline0 = s.time_until_pos(0, 0); s.next_sub2 = s.time_until_pos(64, 0); s } /// `screen_device::time_until_pos` from `now`, as an absolute time. pub fn time_until_pos(&self, vpos: u32, now: u128) -> u128 { let line = (u128::from(vpos) + 264 - 224) % 264; let mut target = line * SCANTIME; let cur = now - self.vblank_start; if target <= cur + PIXELTIME / 2 { target += FRAME; } while target <= cur { target += FRAME; } self.vblank_start + target } /// The 51xx's next O-port write at or after its cycle `from` (absolute, /// in its cycles), given the chip's state. pub fn next_o_write(&self, from: u64, state: Option) -> Option { let reset = self.mcu51_reset?; let in_game = state?; let rel = from.checked_sub(reset)?; let mut frame = rel / MCU_FRAME; loop { let offsets: [u64; 2] = if in_game { O_GAME } else { let start = self.mcu51_credit_frame?; if frame < start { frame = start; continue; } let first = O_IDLE[((frame - start) % 5) as usize]; [first, first + O_IDLE_GAP] }; for off in offsets { let at = reset + frame * MCU_FRAME + off; if at >= from { return Some(at); } } frame += 1; } } /// The next 06xx timer expiry after a control write at `now` with the /// divider in `shifts`, and its period (`ctrl_w_sync`). pub fn n06_start(now: u128, shifts: u8) -> (u128, u128) { let hz = N06_CLOCK_HZ / (1u128 << shifts); let period = (AS / hz) / 2; let tick = AS / N06_CLOCK_HZ; // now.as_ticks(48000): whole seconds times the clock, plus the // fraction's ticks (attotime * u32 carries exactly). let (secs, attos) = (now / AS, now % AS); let ticks = secs * N06_CLOCK_HZ + (attos * N06_CLOCK_HZ) / AS; let next = ticks + 1; // attotime::from_ticks(next, 48000). let expire = (next / N06_CLOCK_HZ) * AS + (next % N06_CLOCK_HZ) * tick; (expire, period) } } impl Default for Sched { fn default() -> Self { Sched::new() } }