// SPDX-License-Identifier: MIT //! Run a 32X cartridge and write the lockstep trace (`sega32x_rust::trace`). //! //! ```text //! s32x-trace CART --bios-dir DIR [--machine NAME] [--frames N] [--press BUTTONS@FROM:TO ...] //! [--reset-at FRAME ...] [--no-fm] [--bus-frames N] [--steps A:B] [--save-at FRAME[+STEPS] ...] [--out FILE] //! ``` //! //! `scripts/s32x_trace.py` takes the same arguments and runs sega32x-python. use std::io::Write; use std::path::PathBuf; use megadrive_rust::trace::Press; use sega32x_rust::trace::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: 60, presses: Vec::new(), resets: Vec::new(), no_fm: false, bus_frames: 0, steps: None, saves: Vec::new(), }; let mut cartridge = None; let mut out_path: Option = None; while let Some(arg) = args.next() { let mut value = || args.next().ok_or(format!("{arg} needs a value")); match arg.as_str() { "--out" => out_path = Some(PathBuf::from(value()?)), "--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()?)?), "--reset-at" => run.resets.push(value()?.parse().map_err(|_| "bad --reset-at")?), "--bus-frames" => run.bus_frames = value()?.parse().map_err(|_| "bad --bus-frames")?, "--save-at" => { let spec = value()?; let (frame, steps) = spec.split_once('+').unwrap_or((&spec, "0")); run.saves .push((frame.parse().map_err(|_| "bad --save-at")?, steps.parse().map_err(|_| "bad --save-at")?)); } "--steps" => { let spec = value()?; let (a, b) = spec.split_once(':').ok_or("--steps A:B")?; run.steps = Some((a.parse().map_err(|_| "bad --steps")?, b.parse().map_err(|_| "bad --steps")?)); } _ 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-trace CART --bios-dir DIR [options]")?; let mut out: Box = match out_path { Some(path) => Box::new(std::io::BufWriter::new( std::fs::File::create(&path).map_err(|e| format!("{}: {e}", path.display()))?, )), None => Box::new(std::io::BufWriter::new(std::io::stdout().lock())), }; run.trace(&mut out) }