// SPDX-License-Identifier: MIT //! The real-time factor, in the reference README's terms: seconds of the Z80's 3,579,545 Hz //! clock (the PSG's input clock on these machines) run per second of wall time. //! //! cargo run --release --example speed [SECONDS, default 60] //! //! The load is a board's: all four channels sounding (three tones, white noise at rate 3), a //! `run` per NTSC scan line (228 clocks), a volume write every line, and `take_samples()` once //! a frame (262 lines), for every part. use std::time::Instant; use sn76489::{SN76489, VARIANTS}; fn main() { let seconds: u64 = std::env::args() .nth(1) .map(|s| s.parse().unwrap()) .unwrap_or(60); let clock = 3_579_545u64; for part in VARIANTS { let mut psg = SN76489::new(part); for (channel, value) in [(0u32, 254u32), (1, 190), (2, 3)] { psg.write(0x80 | (channel << 5) | (value & 0x0F)); psg.write((value >> 4) & 0x3F); } psg.write(0xE7); for channel in 0..4 { psg.write(0x90 | (channel << 5) | (2 * channel)); } let lines = clock * seconds / 228; let mut produced = 0usize; let start = Instant::now(); for line in 0..lines { psg.run(228); psg.write(0x90 | ((line as u32 & 3) << 5) | (line as u32 & 7)); if line % 262 == 261 { produced += psg.take_samples().len(); } } produced += psg.take_samples().len(); let wall = start.elapsed().as_secs_f64(); let emulated = (lines * 228) as f64 / clock as f64; println!( "{:14} {:.1} s of clock in {:.3} s: {:.1}x real time ({} samples)", part.name, emulated, wall, emulated / wall, produced ); } }