// SPDX-License-Identifier: MIT //! megadrive-rust's `md-trace` with a passive 32X in the cartridge slot: the ADEN = 0 //! rung (scripts/passive.py), the reference's `harness.py` (`insert_32x`) in Rust. //! //! ```text //! s32x-mdtrace CART [md-trace's options but --fake, --save-at, --rewind-at] [--out FILE] //! ``` //! //! The system is built as megadrive-rust's `trace::Run::build` builds it, but with //! `Sega32X(cartridge)` holding the cartridge, no boot ROMs, sockets 7 and 8 attached //! (`console::build_passive`); the run and the trace are `Run::trace`'s, line for line. //! Every line must match `md-trace`'s for the same case. use std::io::Write; use std::path::PathBuf; use megadrive_rust::cartridge::{load_image, Cartridge}; use megadrive_rust::io::Device; use megadrive_rust::machine; use megadrive_rust::trace::{frame_lines, step_line, Run, TRACE_HEADER}; fn trace(run: &Run, out: &mut impl Write) -> Result<(), String> { let io_err = |e: std::io::Error| e.to_string(); if run.fake.is_some() || !run.saves.is_empty() || !run.rewinds.is_empty() { return Err("--fake, --save-at and --rewind-at are not this rung's".into()); } let md = machine(&run.machine).ok_or(format!("unknown machine {}", run.machine))?; let image = load_image(&run.cartridge)?; let digest = sha1_smol::Sha1::from(&image).digest().to_string(); let sub = match &run.lock_on { Some(path) => Some(load_image(path)?), None => None, }; let cartridge = Cartridge::lock_on(image, sub, Vec::new())?; let slot = cartridge.slot.clone(); let pads = [Device::by_name(&run.pads[0])?, Device::by_name(&run.pads[1])?]; let boot = match &run.tmss_rom { Some(path) => Some(load_image(path)?), None => None, }; let (mut system, _device) = sega32x_rust::console::build_passive(md, Some(cartridge), Some(pads), boot); if run.no_fm { system.board().dev.fm.enabled = false; } writeln!(out, "{TRACE_HEADER}\n# {digest} slot={slot} machine={}", run.machine).map_err(io_err)?; let mut step_index = 0u64; 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); } if run.resets.contains(&frame) { system.press_reset(None); } let start = system.board().vdp.s.frame; while system.board().vdp.s.frame == start { match system.step() { Ok(elapsed) => { if let Some((a, b)) = run.steps { if (a..b).contains(&step_index) { writeln!(out, "{}", step_line(&system, step_index, elapsed)).map_err(io_err)?; } } step_index += 1; } Err(lockup) => { writeln!(out, "L {frame} {lockup}").map_err(io_err)?; return out.flush().map_err(io_err); } } } let mclk = system.mclk(); system.board().dev.sound_sync(mclk); out.write_all(frame_lines(&mut system, frame).as_bytes()).map_err(io_err)?; } out.flush().map_err(io_err) } 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 out_path: Option = None; while let Some(arg) = args.next() { if arg == "--out" { out_path = Some(PathBuf::from(args.next().ok_or("--out needs a value")?)); } 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("s32x-mdtrace 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 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())), }; trace(&run, &mut out) }