// SPDX-License-Identifier: MIT //! The benchmark (sega32x-rust-fast): run a 32X cartridge as a window would, without one, //! and time it. //! //! ```text //! s32x-bench CART --bios-dir DIR [--machine NAME] [--frames N] [--press BUTTONS@FROM:TO ...] //! [--no-fm] [--repeat N] //! ``` //! //! Each frame: the pad set, `run_frame`, the frame's sound taken (`take_audio`), and the //! finished picture as RGB through `frame_rgb` (with the 32X attached, the composite's //! rows). The time printed is the best of `--repeat` runs, each from power-on, and a //! checksum of every frame's sound and the last picture, so a faster build that is not the //! same board shows itself (megadrive-rust-fast's md-bench, the same shape). use std::path::PathBuf; use std::time::Instant; use megadrive_rust::trace::Press; use sega32x_rust::trace::{crc32, Run}; fn main() -> Result<(), String> { let mut args = std::env::args().skip(1); let mut run = Run { cartridge: PathBuf::new(), bios_dir: PathBuf::new(), machine: "genesis".into(), frames: 600, presses: Vec::new(), resets: Vec::new(), no_fm: false, bus_frames: 0, steps: None, saves: Vec::new(), }; let mut repeat = 1u32; let mut cartridge = None; while let Some(arg) = args.next() { let mut value = || args.next().ok_or(format!("{arg} needs a value")); match arg.as_str() { "--no-fm" => run.no_fm = true, "--bios-dir" => run.bios_dir = PathBuf::from(value()?), "--machine" => run.machine = value()?, "--frames" => run.frames = value()?.parse().map_err(|_| "bad --frames")?, "--press" => run.presses.push(Press::parse(&value()?)?), "--repeat" => repeat = value()?.parse().map_err(|_| "bad --repeat")?, _ if arg.starts_with("--") => return Err(format!("unknown argument {arg}")), _ if cartridge.is_none() => cartridge = Some(PathBuf::from(&arg)), _ => return Err(format!("unknown argument {arg}")), } } run.cartridge = cartridge.ok_or("s32x-bench CART --bios-dir DIR [options]")?; let mut best = f64::INFINITY; let mut check = 0u32; for _ in 0..repeat { let (seconds, sum) = once(&run)?; best = best.min(seconds); check = sum; } println!("{best:.3} s {} frames {:.1} fps check {check:08x}", run.frames, run.frames as f64 / best); Ok(()) } fn once(run: &Run) -> Result<(f64, u32), String> { let (_digest, mut system, _device) = run.build()?; let mut sound = Vec::new(); let mut picture = Vec::new(); let started = Instant::now(); for frame in 0..run.frames { let held = run.presses.iter().filter(|p| p.from <= frame && frame < p.to).fold(0, |held, p| held | p.buttons); if let Some(pad) = &mut system.board().dev.io.ports[0].device { pad.set_pressed(held); } system.run_frame().map_err(|l| format!("frame {frame}: {l}"))?; for (left, right) in system.take_audio() { sound.extend_from_slice(&left.to_le_bytes()); sound.extend_from_slice(&right.to_le_bytes()); } let vdp = &system.board().vdp; picture = vdp.renderer.frame_rgb(&vdp.s); } let seconds = started.elapsed().as_secs_f64(); let pixels: Vec = picture.iter().flatten().flat_map(|p| p.to_le_bytes()).collect(); Ok((seconds, crc32(&sound) ^ crc32(&pixels).rotate_left(1))) }