//! The benchmark: run games headless from power-on (skip-boot) for N slices of 70,224 T each, //! with the gate's input script, and print the speed, with a checksum of every finished picture //! and every audio sample (44.1 kHz capture on, as a player has it), so a faster build that //! changes the output is caught here too. //! //! gameboy-bench [MACHINE:]ROM [...] [--slices N] [--input FILE] [--snapshots] //! //! `--snapshots` also times save_state/load_state at the end (what rewind and run-ahead pay). //! //! MACHINE is a key of `machines.rs` (default dmg). 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. Real time is 59.73 slices per second. use std::collections::HashMap; use std::path::{Path, PathBuf}; use gameboy_rust::boot::read_image; use gameboy_rust::{load_cartridge, machine, Clock, GameBoy}; const SLICE_T: u64 = 70224; const BUTTONS: [&str; 8] = ["a", "b", "select", "start", "right", "left", "up", "down"]; /// Nanoseconds this thread has run on a CPU. fn cpu_ns() -> u64 { let text = std::fs::read_to_string("/proc/thread-self/schedstat").expect("schedstat"); text.split_whitespace().next().unwrap().parse().unwrap() } /// The gate's input script: `slice buttons...` lines, `-` for none (gameboy-trace's format). fn read_inputs(path: &Path) -> HashMap> { let mut out = HashMap::new(); let text = std::fs::read_to_string(path).expect("input file"); for line in text.lines() { let words: Vec<&str> = line .split('#') .next() .unwrap_or("") .split_whitespace() .collect(); if let Some(first) = words.first() { let names = words[1..] .iter() .filter(|w| **w != "-") .map(|w| w.to_string()) .collect(); out.insert(first.parse().expect("slice"), names); } } out } fn main() { let mut slices = 600u64; let mut input = PathBuf::from(concat!( env!("CARGO_MANIFEST_DIR"), "/scripts/inputs/poke.txt" )); 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() { "--slices" => slices = args.next().expect("--slices N").parse().expect("a number"), "--snapshots" => snapshots = true, "--input" => input = PathBuf::from(args.next().expect("--input FILE")), _ => games.push(arg), } } let inputs = read_inputs(&input); for game in &games { let (key, rom) = match game.split_once(':') { Some((k, r)) if machine(k).is_some() => (k, r), _ => ("dmg", game.as_str()), }; let data = read_image(Path::new(rom)).unwrap_or_else(|e| panic!("{rom}: {e}")); let cart = load_cartridge(&data, Clock::Board).expect("cartridge"); let mut gb = GameBoy::new(cart, machine(key).unwrap(), None).expect("board"); gb.hw.apu.capture = true; let mut crc = crc32fast::Hasher::new(); let mut pressed: Vec = Vec::new(); let began = cpu_ns(); for index in 0..slices { if let Some(wanted) = inputs.get(&index) { for name in BUTTONS { let (was, is) = ( pressed.iter().any(|p| p == name), wanted.iter().any(|w| w == name), ); if was != is { gb.set_pressed(name, is); } } pressed = wanted.clone(); } let end = (index + 1) * SLICE_T; while gb.hw.t < end { gb.step(); } for pixel in &gb.hw.ppu.frame { crc.update(&pixel.to_le_bytes()); } for sample in gb.hw.apu.take_samples() { crc.update(&sample.to_le_bytes()); } } let seconds = (cpu_ns() - began) as f64 / 1e9; let name = Path::new(rom).file_stem().unwrap().to_string_lossy(); println!( "{key}:{name}: {slices} slices in {seconds:.3} s, {:.1} slices/s, check {:08X}", slices as f64 / seconds, crc.finalize() ); if snapshots { // what rewind and run-ahead pay: a save and a load of the state at the end let n = 200; let began = cpu_ns(); let mut size = 0; for _ in 0..n { size = gb.save_state().len(); } let save = (cpu_ns() - began) as f64 / 1e6 / n as f64; let state = gb.save_state(); let began = cpu_ns(); for _ in 0..n { gb.load_state(&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"); } } }