//! A movie on our board and on MAME 0.285, for movies of the stock game (MAME //! runs no mods: the remix's paths live on a daughter card MAME's board does //! not have). Usage: //! galaga-mamecheck schedule MOVIE the movie as GALAGA_INPUTS //! galaga-mamecheck compare MOVIE MAME.jsonl [FROM] [--workram] [--replies] //! //! `oracle/movie_on_mame.sh` runs MAME with the schedule and writes the //! JSONL. An input the movie holds during frame f is scheduled at f - 1: //! oracle/mame_dump.lua applies it after that frame. //! //! `compare` runs our board on the same schedule and compares, at every MAME //! record from frame FROM on, video and sprite RAM and the challenge hit //! counter (0x9288, MAME's "peek"). A difference lasting one or two frames //! is the known boundary race (a write landing as the frame ends, seen by //! one emulator's snapshot and not the other's) and is only counted; three //! in a row stop the comparison. `--workram` compares the three work RAMs //! instead (needs GALAGA_WORKRAM=1), skipping the CPU stacks; `--replies` //! lists every frame whose 51xx reply bytes differ (needs MAME's events). //! docs/mame-oracle.md, "Movies on MAME". use emu_galaga::galaga::machine::CONTROLS; use emu_galaga::galaga::movie::Movie; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::trace::{parse_inputs, parse_jsonl, run, Record, RecordEvent}; use emu_galaga::galaga::Machine; fn schedule(movie: &Movie) -> String { let mut out = Vec::new(); let mut held = [0u8, 0u8]; for &(f, in0, in1) in &movie.changes { let now = [in0, in1]; for &(name, port, mask) in CONTROLS.iter() { let (was, is) = (held[port] & mask != 0, now[port] & mask != 0); if was != is { assert!(f > 0, "an input on frame 0 cannot be scheduled"); out.push(format!("{}:{name}:{}", f - 1, u8::from(is))); } } held = now; } out.join(",") } /// The CPU stacks (main and sub in 0x9060-0x90ff, sub2 in 0x9a80-0x9aff, /// measured): what each CPU was doing as the frame ended, not game state. fn is_stack(ram: usize, offset: usize) -> bool { (ram == 2 && (0x60..0x100).contains(&offset)) || (ram == 3 && (0x280..0x300).contains(&offset)) } fn replies(events: &[RecordEvent]) -> String { events .iter() .filter(|e| e.kind == "bus06" && e.read) .map(|e| format!("{:x}={:02x}", e.addr & 0xf, e.data)) .collect::>() .join(" ") } fn main() { let args: Vec = std::env::args().skip(1).collect(); if args.len() < 2 { eprintln!("usage: galaga-mamecheck schedule MOVIE | compare MOVIE MAME.jsonl [FROM] [--workram] [--replies]"); std::process::exit(2); } let movie = Movie::from_json(&std::fs::read_to_string(&args[1]).unwrap()).unwrap(); assert!( movie.path_mod.is_none() && movie.patches.is_empty(), "MAME runs the stock ROMs only" ); if args[0] == "schedule" { println!("{}", schedule(&movie)); return; } let text = std::fs::read_to_string(&args[2]).unwrap(); let theirs = parse_jsonl(&text).unwrap(); let raw: Vec = text .lines() .map(|l| serde_json::from_str(l).unwrap()) .collect(); let from: u64 = args.get(3).and_then(|v| v.parse().ok()).unwrap_or(0); let workram = args.iter().any(|a| a == "--workram"); let show_replies = args.iter().any(|a| a == "--replies"); let roms = load_directory(std::path::Path::new("rom/galaga")).unwrap(); let mut m = Machine::new(&roms); m.set_dips(movie.dsw_a, movie.dsw_b); let inputs = parse_inputs(&schedule(&movie)).unwrap(); let last = theirs.last().unwrap().frame; let mut next = 0; let (mut same, mut blips, mut run_of_diffs) = (0, 0, 0); let mut by_frame = theirs.iter().zip(&raw).peekable(); for frame in 0..=last { let ours: Record = run(&mut m, 1, 1, &[]).unwrap().pop().unwrap(); while next < inputs.len() && inputs[next].frame <= frame { m.set_control(&inputs[next].control, inputs[next].pressed) .unwrap(); next += 1; } let Some(&(t, r)) = by_frame.peek() else { break; }; if t.frame != frame { continue; } by_frame.next(); if frame < from { continue; } if show_replies { let (a, b) = (replies(&ours.events), replies(&t.events)); if a != b { println!("frame {frame}: 51xx replies ours [{a}] MAME [{b}]"); } continue; } let mut diffs = Vec::new(); if workram { let s = m.shared.borrow(); for (n, (name, mine)) in [("ram1", &s.ram1), ("ram2", &s.ram2), ("ram3", &s.ram3)] .into_iter() .enumerate() { let theirs = r[name] .as_str() .expect("MAME record without work RAM: run with GALAGA_WORKRAM=1"); for i in (0..0x400).filter(|&i| !is_stack(n + 1, i)) { if format!("{:02x}", mine[i]) != theirs[2 * i..2 * i + 2] { diffs.push(format!( "{:04x}: ours {:02x} MAME {}", [0x8800, 0x9000, 0x9800][n] + i, mine[i], &theirs[2 * i..2 * i + 2] )); } } } } else { for (name, a, b) in [ ("vram", &ours.vram, &t.vram), ("sprite1", &ours.sprite1, &t.sprite1), ("sprite2", &ours.sprite2, &t.sprite2), ("sprite3", &ours.sprite3, &t.sprite3), ] { for i in (0..a.len() / 2).filter(|&i| a[2 * i..2 * i + 2] != b[2 * i..2 * i + 2]) { diffs.push(format!( "{name}+{i:03x}: ours {} MAME {}", &a[2 * i..2 * i + 2], &b[2 * i..2 * i + 2] )); } } } let counter = (m.shared.borrow().ram2[0x288], r["peek"]["37512"].as_u64()); if let Some(k) = counter.1 { if u64::from(counter.0) != k { diffs.push(format!("hit counter: ours {} MAME {k}", counter.0)); } } if diffs.is_empty() { same += 1; run_of_diffs = 0; continue; } run_of_diffs += 1; if run_of_diffs < 3 { blips += 1; continue; } println!( "frame {frame}: a lasting difference ({} bytes): {}", diffs.len(), diffs.iter().take(8).cloned().collect::>().join(", ") ); std::process::exit(1); } if !show_replies { println!( "identical at {same} records from frame {from} to {last}, {blips} one- or two-frame blips, no lasting difference" ); } }