// SPDX-License-Identifier: MIT //! The benchmark: run games headless from power-on for N frames, as a player //! has them (a television on `on_line`, the audio samples taken every frame), //! and print the speed with a checksum of every finished line and every audio //! sample, so a faster build that changes the output is caught here too. //! //! a2600-bench [MACHINE:]ROM [...] [--frames N] [--snapshots] //! //! MACHINE is ntsc (default), pal or secam. The time is this thread's time on //! the CPU (`/proc/thread-self/schedstat`, nanoseconds run), not the wall //! clock: on a shared machine the wall clock counts the time other processes //! had the CPU. The speed is printed as frames per second and in multiples of //! real time (CPU cycles emulated over the machine's CPU clock). //! `--snapshots` also times save_state/load_state at the end. use std::cell::RefCell; use std::path::Path; use std::rc::Rc; use a2600::cartridge::{Options, Rom}; use a2600::machine::machine; use a2600::state::{load_state, save_state}; use a2600::System; /// Nanoseconds this thread has run on a CPU (wall clock where there is no schedstat). fn cpu_ns() -> u64 { match std::fs::read_to_string("/proc/thread-self/schedstat") { Ok(text) => text.split_whitespace().next().unwrap().parse().unwrap(), Err(_) => std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_nanos() as u64, } } fn main() { let mut frames = 1200u64; let mut games = Vec::new(); let mut snapshots = false; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "--frames" => frames = args.next().expect("--frames N").parse().expect("a number"), "--snapshots" => snapshots = true, _ => games.push(arg), } } for game in &games { let (key, rom) = match game.split_once(':') { Some((k, r)) if ["ntsc", "pal", "secam"].contains(&k) => (k, r), _ => ("ntsc", game.as_str()), }; let row = machine(&format!("Atari VCS ({})", key.to_uppercase())).expect("ntsc, pal or secam"); let mut system = System::from_rom(Rom::Path(Path::new(rom)), row, None, Options::default()).expect("load"); // every finished line into the checksum, as a television takes it let crc = Rc::new(RefCell::new(crc32fast::Hasher::new())); let shared = crc.clone(); system.board_mut().tia.on_line = Some(Box::new(move |tia| shared.borrow_mut().update(&tia.pixels))); let cycles = system.cycles(); let began = cpu_ns(); for _ in 0..frames { system.run_frames(1); let samples = system.board_mut().tia.audio.take_samples(); let mut crc = crc.borrow_mut(); for sample in samples { crc.update(&sample.to_le_bytes()); } } let seconds = (cpu_ns() - began) as f64 / 1e9; let emulated = (system.cycles() - cycles) as f64 / system.machine.timing.cpu_hz(); let name = Path::new(rom).file_stem().unwrap().to_string_lossy(); let check = crc.borrow().clone().finalize(); println!( "{key}:{name}: {frames} frames in {seconds:.3} s, {:.1} frames/s, {:.3}x real time, check {check:08X}", frames as f64 / seconds, emulated / seconds, ); if snapshots { let n = 200; let began = cpu_ns(); let mut size = 0; for _ in 0..n { size = save_state(&system).len(); } let save = (cpu_ns() - began) as f64 / 1e6 / n as f64; let state = save_state(&system); let began = cpu_ns(); for _ in 0..n { load_state(&mut system, &state).expect("load"); } let load = (cpu_ns() - began) as f64 / 1e6 / n as f64; println!(" snapshot: {size} bytes, save {save:.3} ms, load {load:.3} ms"); } } }