//! The benchmark: run a cartridge as `md-play` does, without a window, and time it. //! //! ```text //! md-bench CART [--machine NAME] [--frames N] [--press BUTTONS@FROM:TO ...] //! [--no-fm] [--pad KIND] [--pad2 KIND] [--repeat N] //! ``` //! //! Each frame is what the window asks of the board: the pads set, `run_frame`, the //! frame's sound taken and mixed (`take_audio`), and the finished picture as RGB //! (`frame_rgb_into`, as `frame_rgb` laid end to end). The time printed is the best of `--repeat` runs, each from power-on, //! and a checksum of the sound and the last picture, so a faster build that is not the //! same board shows itself. use std::time::Instant; use megadrive_rust::cartridge::{load_image, Cartridge}; use megadrive_rust::io::Device; use megadrive_rust::machine::machine; use megadrive_rust::trace::{crc32, Run}; use megadrive_rust::System; fn main() -> Result<(), String> { let mut args = std::env::args().skip(1); let mut run: Option = None; let mut options: Vec<(String, Option)> = Vec::new(); let mut repeat = 1u32; while let Some(arg) = args.next() { if arg == "--repeat" { repeat = args .next() .and_then(|v| v.parse().ok()) .ok_or("bad --repeat")?; } else if arg == "--no-fm" { options.push((arg, None)); } else if arg.starts_with("--") { let value = args.next().ok_or(format!("{arg} needs a value"))?; options.push((arg, Some(value))); } else if run.is_none() { run = Some(Run::new(arg)); } else { return Err(format!("unknown argument {arg}")); } } let mut run = run.ok_or("md-bench CART [options]")?; for (name, value) in options { let mut value = value.into_iter(); run.option(&name, &mut || { value.next().ok_or(format!("{name} needs a value")) })?; } let image = load_image(&run.cartridge)?; let mut best = f64::INFINITY; let mut check = 0u32; for _ in 0..repeat { let (seconds, sum) = once(&run, image.clone())?; best = best.min(seconds); check = sum; } println!( "{:.3} s {} frames {:.1} fps check {check:08x}", best, run.frames, run.frames as f64 / best ); Ok(()) } fn once(run: &Run, image: Vec) -> Result<(f64, u32), String> { let machine = machine(&run.machine).ok_or(format!("unknown machine {}", run.machine))?; let pads = [ Device::by_name(&run.pads[0])?, Device::by_name(&run.pads[1])?, ]; let mut system = System::new( machine, Some(Cartridge::from_image(image)), Some(pads), None, ); if run.no_fm { system.board().dev.fm.enabled = false; } 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(device) = &mut system.board().dev.io.ports[0].device { device.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; vdp.renderer.frame_rgb_into(&vdp.s, &mut picture); } let seconds = started.elapsed().as_secs_f64(); let pixels: Vec = picture.iter().flat_map(|p| p.to_le_bytes()).collect(); Ok((seconds, crc32(&sound) ^ crc32(&pixels).rotate_left(1))) }