// SPDX-License-Identifier: MIT //! Run a cart headless: `lynx CART --frames N --png out.png` (`python -m lynx_python`). //! //! The cart is any image the front door takes (.lnx, .lyx, BLL; loose, .zip, .7z), read //! in place. The boot ROM is found by hash through `$MAME_ROMPATH` (lynx::bootrom); //! without one the cart is not started (the host-decryption path is owed, as in the //! reference). `--png` writes the last finished frame, turned for the cart's rotation; //! `--wav` the run's sound (Mikey's mix, 32 kHz). //! //! lynx CART [--frames 60] [--machine lynx1|lynx2] [--png FILE] //! [--rotation none|left|right] [--wav FILE] [--eeprom TYPE[:ORG]] use std::path::PathBuf; use std::time::Instant; use lynx::cart::load_path; use lynx::machine_named; use lynx::mikey::SAMPLE_RATE; use lynx::picture::save; use lynx::run::{boot, run_frames}; use lynx::sound::write_wav; fn cpu_seconds() -> f64 { // process CPU time (the reference's time.process_time), from /proc/self/stat let stat = std::fs::read_to_string("/proc/self/stat").unwrap_or_default(); let fields: Vec<&str> = stat .rsplit(')') .next() .unwrap_or("") .split_whitespace() .collect(); let ticks = |i: usize| { fields .get(i) .and_then(|v| v.parse::().ok()) .unwrap_or(0.0) }; (ticks(11) + ticks(12)) / 100.0 } fn main() { let mut cart: Option = None; let mut frames: u64 = 60; let mut machine = "lynx2".to_string(); let mut png: Option = None; let mut rotation: Option = None; let mut wav: Option = None; let mut eeprom: Option = None; let mut args = std::env::args().skip(1); while let Some(a) = args.next() { match a.as_str() { "--frames" => frames = args.next().unwrap().parse().unwrap(), "--machine" => machine = args.next().unwrap(), "--png" => png = Some(args.next().unwrap().into()), "--rotation" => rotation = Some(args.next().unwrap()), "--wav" => wav = Some(args.next().unwrap().into()), "--eeprom" => eeprom = Some(args.next().unwrap()), "-h" | "--help" => { println!("lynx CART [--frames N] [--machine lynx1|lynx2] [--png FILE] [--rotation none|left|right] [--wav FILE] [--eeprom TYPE[:ORG]]"); return; } _ if cart.is_none() => cart = Some(a.into()), _ => { eprintln!("unknown argument {a}"); std::process::exit(2); } } } let Some(cart) = cart else { eprintln!("a cart is needed"); std::process::exit(2); }; let machine = machine_named(&machine).unwrap_or_else(|| { eprintln!("--machine: lynx1 or lynx2"); std::process::exit(2) }); let mut overrides = serde_json::Map::new(); if let Some(e) = eeprom { overrides.insert("eeprom".into(), serde_json::Value::String(e)); } let description = match load_path(&cart, Some(&overrides)) { Ok(d) => d, Err(e) => { eprintln!("error: {e}"); std::process::exit(1); } }; let mut board = boot(&description, machine).unwrap_or_else(|e| { eprintln!("{e}"); std::process::exit(1) }); if wav.is_some() { let tick = board.tick(); board.hw_mut().mikey.start_audio(tick); } let started = cpu_seconds(); let wall = Instant::now(); run_frames(&mut board, frames); let mut spent = cpu_seconds() - started; if spent <= 0.0 { spent = wall.elapsed().as_secs_f64(); } if let Some(wav) = wav { let samples = board.hw().mikey.samples.clone().unwrap_or_default(); write_wav(&wav, &samples, SAMPLE_RATE as u32, 2).expect("write the WAV"); } if let Some(png) = png { // The picture as the player sees it: turned for the cart's rotation field. let rotation = rotation.unwrap_or(description.rotation.clone()); save(&png, &board.hw().mikey.display.rgb(), &rotation).expect("write the PNG"); } let seconds = board.tick() as f64 / 16_000_000.0; println!( "{frames} frames, {seconds:.2} emulated s in {spent:.1} s CPU ({:.3}x real time)", if spent > 0.0 { seconds / spent } else { 0.0 } ); }