//! The flight-path language is decoded right: from the known starts every //! reachable op decodes without overlap, the listing reassembles to the ROM's //! exact bytes, the main CPU's entrance table points at op starts, and every //! path pointer the game stores while playing the TAS movie (motion queue //! +08/+09, 0x9100 + 0x14k) is an op start, for every live entry (its //! object in state 3, 7 or 9 and pointing back at it). Needs rom/galaga. use std::collections::BTreeSet; use std::path::Path; use emu_galaga::galaga::movie::Movie; use emu_galaga::galaga::paths::{assemble, decode, listing, ROOTS}; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::Machine; #[test] fn paths_decode_reassemble_and_match_play() { let root = Path::new(env!("CARGO_MANIFEST_DIR")); let Ok(roms) = load_directory(&root.join("rom/galaga")) else { eprintln!("skipped: no extracted set at rom/galaga"); return; }; let program = decode(&roms.sub, &ROOTS).unwrap(); // Round trip: text back to the same bytes. let text = listing(&program); let runs = assemble(&text).unwrap(); let mut bytes = 0; for (at, run) in &runs { let at = usize::from(*at); assert_eq!( &roms.sub[at..at + run.len()], &run[..], "run at 0x{at:04x} reassembles differently" ); bytes += run.len(); } // The entrance table in main ROM: 24 words at 0x2a3c, bits 0-12 a path. for i in 0..24 { let word = u16::from(roms.main[0x2a3c + 2 * i]) | u16::from(roms.main[0x2a3d + 2 * i]) << 8; let path = word & 0x1fff; assert!( program.ops.contains_key(&path), "entrance table entry {i} points at 0x{path:04x}, not an op start" ); } // Play: every stored path pointer is an op start. let movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); let mut machine = Machine::new(&roms); machine.set_dips(movie.dsw_a, movie.dsw_b); let mut seen = BTreeSet::new(); for c in movie.inputs() { machine.set_controls(c); machine.run_frame(); let s = machine.shared.borrow(); for k in 0..12 { let e = 0x100 + 0x14 * k; // Live entries only, as the sub CPU judges them: active, and its // object (L at +0x10) in state 3, 7 or 9 pointing back at it. let l = usize::from(s.ram2[e + 0x10]); let live = s.ram2[e + 0x13] & 1 != 0 && l < 0x80 && matches!(s.ram1[l], 3 | 7 | 9) && usize::from(s.ram1[l + 1]) == 0x14 * k; if !live { continue; } let pointer = u16::from(s.ram2[e + 8]) | u16::from(s.ram2[e + 9]) << 8; assert!( program.ops.contains_key(&pointer), "frame {}: queue entry {k} points at 0x{pointer:04x}, not an op start", machine.frame ); seen.insert(pointer); } } eprintln!( "{} ops in {} labelled paths, {bytes} bytes round-trip; {} distinct pointers seen in play", program.ops.len(), program.labels.len(), seen.len() ); }