// SPDX-License-Identifier: MIT //! Chip time over wall time, the way a board drives the chip: a Sunsoft 5B at the NTSC //! Famicom's CPU clock, three tones, the noise and the envelope sounding, polled each cycle //! (`tick`, then `levels` and `mix` when the codes change), as famicom-python's mapper 69 //! polls ay8910-python. Then an AY-3-8910 at the Spectrum 128's clock driven by `run` and //! `take_samples`, the queued path. scripts/speed_ref.py is the same on ay8910-python. //! //! ```text //! cargo run --release --example speed [seconds, default 20] //! ``` use std::time::Instant; use ay8910_rust::{PSGVariant, AY8910, PSG, SUNSOFT_5B}; const FAMICOM_HZ: u64 = 1_789_773; const SPECTRUM_HZ: u64 = 1_773_400; fn sounding(variant: PSGVariant, record: bool) -> PSG { let mut chip = PSG::with_record(variant, record); for (register, value) in [ (0, 0xFE), (1, 0), (2, 0x40), (3, 1), (4, 0x9C), (5, 0), (6, 7), (7, 0x30), (8, 12), (9, 0x10), (10, 9), (11, 0x20), (12, 0), (13, 0x0E), ] { chip.write_register(register, value); } chip } fn main() { let seconds: u64 = std::env::args() .nth(1) .map_or(20, |s| s.parse().expect("seconds")); let mut chip = sounding(SUNSOFT_5B, false); let start = Instant::now(); let (mut last, mut level, mut changes) = (chip.levels(), 0.0, 0u64); for _ in 0..seconds * FAMICOM_HZ { chip.tick(); let now = chip.levels(); if now != last { last = now; level = chip.mix(); changes += 1; } } let wall = start.elapsed().as_secs_f64(); println!( "Sunsoft 5B, polled: {seconds} s of chip time in {wall:.3} s: {:.1} x real time ({changes} changes, last level {level:.3})", seconds as f64 / wall ); let mut chip = sounding(AY8910, true); let start = Instant::now(); let mut heard = 0usize; for _ in 0..seconds { chip.run(SPECTRUM_HZ); heard += chip.take_samples().iter().filter(|&&v| v != 0.0).count(); } let wall = start.elapsed().as_secs_f64(); println!( "AY-3-8910, run + take_samples: {seconds} s of chip time in {wall:.3} s: {:.1} x real time ({heard} non-zero samples)", seconds as f64 / wall ); }