// SPDX-License-Identifier: MIT //! Speed of `run()` on a running chip: the reference's `scripts/speed.py`, transcribed. //! //! cargo run --release --example speed [-- CYCLES RUNS] //! //! All four channels sound (two noise, one gated square, one high-passed tone), //! so no part of the chip idles. Best of RUNS by elapsed time (Rust's standard library has //! no process CPU clock; on a shared machine other work only slows a run, so the best of //! several is the speed), after the reference's 200,000-cycle warm-up. use std::time::Instant; use pokey::POKEY; const NTSC: f64 = 1_789_773.0; fn running_chip() -> POKEY { let mut chip = POKEY::default(); for (register, value) in [ (0, 40), (1, 0x2A), (2, 3), (3, 0xCA), (4, 90), (5, 0x0A), (6, 7), (7, 0xA9), (8, 0x64), (15, 3), (9, 0), ] { chip.write(register, value); chip.tick(); } chip } fn main() { let args: Vec = std::env::args() .skip(1) .map(|a| a.parse().expect("CYCLES RUNS")) .collect(); let cycles = args.first().copied().unwrap_or(20_000_000); let runs = args.get(1).copied().unwrap_or(7); let mut chip = running_chip(); chip.run(200_000); // warm-up, as the reference's let mut best = f64::INFINITY; for _ in 0..runs { let began = Instant::now(); chip.run(cycles); best = best.min(began.elapsed().as_secs_f64()); } let rate = cycles as f64 / best; println!( "pokey-rust: {:.2} M cycles/s ({:.2}x NTSC real time), best of {runs} x {cycles} (levels {:?})", rate / 1e6, rate / NTSC, chip.levels() ); }