//! Play the board with a scripted random player and record what the game //! does with its RAM, for the RAM map (`docs/ram-map.md`). Usage: //! galaga-probe [--rom DIR] [--out DIR] [--frames N] [--seed S] [--ppm-every K] //! //! Writes into DIR (default target/probe): //! ram.bin per frame: u32 frame, u8 in0, u8 in1, then video RAM (2 KB) //! and work RAM 1, 2, 3 (1 KB each): 7,174 bytes per frame //! profile.json the access profile (`galaga::profile`) //! frame-N.ppm the screen every K frames (0 = never) //! //! The player: a coin and a start every 30 s (both ignored mid-game), the //! stick held left, right or centred for random stretches, fire tapped at //! random. It is not good at the game; it makes the game do things. use std::io::Write; use std::path::PathBuf; use emu_galaga::galaga::profile::Profile; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::video::Video; use emu_galaga::galaga::Machine; struct Rng(u64); impl Rng { fn next(&mut self) -> u64 { self.0 ^= self.0 << 13; self.0 ^= self.0 >> 7; self.0 ^= self.0 << 17; self.0 } fn below(&mut self, n: u64) -> u64 { self.next() % n } } fn main() { let mut args = std::env::args().skip(1); let mut rom_dir = PathBuf::from("rom/galaga"); let mut out = PathBuf::from("target/probe"); let mut frames: u64 = 36_000; let mut seed: u64 = 0x9e37_79b9_7f4a_7c15; let mut ppm_every: u64 = 0; while let Some(arg) = args.next() { let mut value = || args.next().unwrap_or_else(|| panic!("{arg} needs a value")); match arg.as_str() { "--rom" => rom_dir = PathBuf::from(value()), "--out" => out = PathBuf::from(value()), "--frames" => frames = value().parse().expect("--frames N"), "--seed" => seed = value().parse().expect("--seed S"), "--ppm-every" => ppm_every = value().parse().expect("--ppm-every K"), _ => panic!("unknown argument {arg}"), } } let roms = load_directory(&rom_dir).unwrap_or_else(|e| { eprintln!("{}: {e}", rom_dir.display()); std::process::exit(2) }); std::fs::create_dir_all(&out).unwrap(); let mut machine = Machine::new(&roms); let mut video = Video::new(&roms); machine.shared.borrow_mut().profile = Some(Box::new(Profile::new())); let mut ram = std::io::BufWriter::new(std::fs::File::create(out.join("ram.bin")).unwrap()); let mut rng = Rng(seed | 1); let mut stick_until = 0; let mut rgba = vec![0u8; 224 * 288 * 4]; for frame in 0..frames { // Coin and start every 30 s once the self-test is over. match frame % 1800 { 900 => machine.set_control("coin1", true).unwrap(), 905 => machine.set_control("coin1", false).unwrap(), 960 => machine.set_control("start1", true).unwrap(), 990 => machine.set_control("start1", false).unwrap(), _ => {} } if frame >= stick_until { let choice = rng.below(3); machine.set_control("p1_left", choice == 0).unwrap(); machine.set_control("p1_right", choice == 1).unwrap(); stick_until = frame + 5 + rng.below(40); } machine.set_control("p1_fire", rng.below(6) == 0).unwrap(); machine.run_frame(); let ports = machine.ports(); { let s = machine.shared.borrow(); ram.write_all(&(frame as u32).to_le_bytes()).unwrap(); ram.write_all(&ports).unwrap(); ram.write_all(&s.video_ram).unwrap(); ram.write_all(&s.ram1).unwrap(); ram.write_all(&s.ram2).unwrap(); ram.write_all(&s.ram3).unwrap(); } if ppm_every > 0 && frame % ppm_every == 0 { video.render(&machine.shared.borrow(), machine.videolatch_at_vblank_end); video.screen_rgba(&mut rgba); let mut ppm = b"P6 224 288 255\n".to_vec(); for px in rgba.chunks(4) { ppm.extend_from_slice(&px[..3]); } std::fs::write(out.join(format!("frame-{frame}.ppm")), ppm).unwrap(); } } ram.flush().unwrap(); let profile = machine.shared.borrow_mut().profile.take().unwrap(); std::fs::write( out.join("profile.json"), serde_json::to_string(&profile.to_json()).unwrap(), ) .unwrap(); println!("{frames} frames into {}", out.display()); }