// SPDX-License-Identifier: MIT //! a5200 ROM --frames N --png out.png --wav out.wav //! //! A transcription of a5200-python's `__main__.py` (at cf82d44), with the same options. //! Speed is printed in multiples of real time as well as frames a second. use std::path::PathBuf; use std::time::Instant; use a5200::board::{FRAME_LINES, FRAME_WIDTH}; use a5200::cartridge::{load_cartridge, Source}; use a5200::media::{write_png, AudioTap, PALETTE_LABEL}; use a5200::{machine_by_name, Console}; const USAGE: &str = "usage: a5200 ROM [--frames N] [--png F] [--wav F] [--machine a5200|a5200_2port] \ [--mapping M] [--bios F] [--info]"; fn main() { let args: Vec = std::env::args().skip(1).collect(); let mut rom: Option = None; let mut frames = 300u64; let mut png: Option = None; let mut wav: Option = None; let mut machine = "a5200".to_string(); let mut mapping: Option = None; let mut bios: Option = None; let mut info = false; let mut i = 0; while i < args.len() { let a = args[i].as_str(); let mut value = || { i += 1; args.get(i).cloned().unwrap_or_else(|| { eprintln!("{USAGE}"); std::process::exit(2) }) }; match a { "--frames" => frames = value().parse().expect("--frames N"), "--png" => png = Some(value().into()), "--wav" => wav = Some(value().into()), "--machine" => machine = value(), "--mapping" => mapping = Some(value()), "--bios" => bios = Some(value().into()), "--info" => info = true, "-h" | "--help" => { println!("{USAGE}"); return; } _ if a.starts_with("--") => { eprintln!("{USAGE}"); std::process::exit(2) } _ => rom = Some(a.into()), } i += 1; } let Some(rom) = rom else { eprintln!("{USAGE}"); std::process::exit(2) }; let Some(machine) = machine_by_name(&machine) else { eprintln!("--machine: a5200 or a5200_2port"); std::process::exit(2) }; let cart = match load_cartridge(Source::Path(&rom), mapping.as_deref(), None) { Ok(cart) => cart, Err(e) => { eprintln!("{e}"); std::process::exit(1) } }; println!( "{}: {}K {} (from {}), sha1 {}", if cart.title.is_empty() { "?" } else { &cart.title }, cart.size() / 1024, cart.mapping, cart.mapping_from, cart.sha1 ); for note in &cart.notes { println!("note: {note}"); } if info { return; } let mut console = match Console::new(cart, machine, None, bios.as_deref()) { Ok(console) => console, Err(e) => { eprintln!("{e}"); std::process::exit(1) } }; if wav.is_some() { console.board_mut().audio = Some(AudioTap::new(machine.cycle_hz(), 44100, 8)); } let start = Instant::now(); console.run_frames(frames); let seconds = start.elapsed().as_secs_f64(); println!( "{frames} frames in {seconds:.1} s: {:.2} frames/s ({:.0} k cycles/s, {:.2}x real time)", frames as f64 / seconds, console.board().cycles as f64 / seconds / 1000.0, frames as f64 / seconds / machine.frame_hz() ); if let Some(png) = png { write_png(&png, &console.board().frame, FRAME_WIDTH, FRAME_LINES, None).unwrap(); println!("wrote {} (palette: {PALETTE_LABEL})", png.display()); } if let Some(wav) = wav { console.board().audio.as_ref().unwrap().write(&wav).unwrap(); println!("wrote {}", wav.display()); } }