//! Every path mod in mods/ is sound: it assembles, its runs in the original //! ROM replace whole ops, the modded program decodes without overlap, and //! the game played with it (the TAS movie's inputs, 20,000 frames) keeps //! every live motion-queue pointer on an op start of the modded program and //! actually runs the mod's new code, and no enemy jumps in plain view (a //! flying enemy inside the screen moving more than 24 px in a frame, as //! when a path re-enters from the top before the enemy has left the //! screen, or a path ending while the enemy is in view; the unmodified game //! never does either outside transients, which the check leaves out). The //! game itself clears and resets enemies around a lost fighter, a capture //! and game over (measured: a bee vanishing in view after a game over, a //! capture boss after its beam), so the check stands aside for 240 frames //! after any of them. Needs rom/galaga. use std::path::Path; use emu_galaga::galaga::movie::Movie; use emu_galaga::galaga::objects::{objects, Object}; use emu_galaga::galaga::paths::{PathMod, FREE_SPACE}; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::Machine; /// Frames to stand aside after a disturbance. const QUIET: u64 = 240; /// Whether play is undisturbed: in play (0x9201 = 3), the fighter alive /// (slot 0x31's state 0x80) and no capture boss (0x9828 = 1). fn undisturbed(machine: &Machine) -> bool { let s = machine.shared.borrow(); // In play, the fighter not exploding, no capture under way, and not the // game over that follows the last ship (reserve 0xff: the game state // still reads play while it clears the enemies, measured 2026-10-02). s.ram2[0x201] == 3 && s.ram1[0x62] == 0x80 && s.ram3[0x28] == 0x01 && s.ram3[0x20] != 0xff } #[test] fn every_path_mod_is_sound_in_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 movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); let mut checked = 0; for entry in std::fs::read_dir(root.join("mods")).unwrap() { let path = entry.unwrap().path(); if path.extension().is_none_or(|e| e != "paths") { continue; } let path_mod = PathMod::load(&path).unwrap(); let program = path_mod.check(&roms.sub).unwrap(); let mut machine = Machine::new(&roms); machine.set_dips(movie.dsw_a, movie.dsw_b); path_mod.apply(&mut machine).unwrap(); let mut new_code_frames = 0; let mut calm_since = 0; let mut before = objects(&machine.shared.borrow()); for c in movie.inputs().into_iter().take(20_000) { machine.set_controls(c); machine.run_frame(); let s = machine.shared.borrow(); let now = objects(&s); drop(s); if !undisturbed(&machine) { calm_since = machine.frame; } if machine.frame - calm_since > QUIET { if let Some(what) = visible_teleport(&before, &now) { panic!("{}: frame {}: {what}", path_mod.name, machine.frame); } } let s = machine.shared.borrow(); before = now; for k in 0..12 { let e = 0x100 + 0x14 * k; 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", path_mod.name, machine.frame ); if FREE_SPACE.contains(&pointer) { new_code_frames += 1; } } } assert!( new_code_frames > 0, "{}: the new code never ran", path_mod.name ); eprintln!( "{}: sound; its new code flew objects for {new_code_frames} entry-frames", path_mod.name ); checked += 1; } assert!(checked > 0, "no mods in mods/"); } /// A flying enemy (not a transient) that jumps more than 24 px in one frame /// while inside the screen, staying in the same flying state, or that stops /// being an object there without exploding (a path ending in view). fn visible_teleport(before: &[Object; 64], now: &[Object; 64]) -> Option { for slot in (0x04..0x1c).chain(0x20..0x30) { let (a, b) = (&before[slot], &now[slot]); if matches!(a.state, 3 | 9) && b.state == 0x80 { let (ax, ay) = a.center(); if (8..=216).contains(&ax) && (24..=280).contains(&ay) { return Some(format!( "slot {slot:02x} vanished at ({ax},{ay}) in plain view" )); } } if !matches!(a.state, 3 | 9) || a.state != b.state { continue; } let (ax, ay) = a.center(); let (bx, by) = b.center(); let inside = (8..=216).contains(&ax) && (24..=280).contains(&ay); let jump = (((ax - bx).pow(2) + (ay - by).pow(2)) as f64).sqrt(); if inside && jump > 24.0 { return Some(format!( "slot {slot:02x} jumped from ({ax},{ay}) to ({bx},{by}) in plain view" )); } } None } #[test] fn the_unmodified_game_never_teleports_in_view() { 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 movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); let mut machine = Machine::new(&roms); movie.setup(&mut machine).unwrap(); let mut calm_since = 0; let mut before = objects(&machine.shared.borrow()); for c in movie.inputs() { machine.set_controls(c); machine.run_frame(); let now = objects(&machine.shared.borrow()); if !undisturbed(&machine) { calm_since = machine.frame; } if machine.frame - calm_since > QUIET { if let Some(what) = visible_teleport(&before, &now) { panic!("the unmodified game: frame {}: {what}", machine.frame); } } before = now; } } #[test] fn modded_movies_never_teleport_or_vanish_in_view() { 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; }; for entry in std::fs::read_dir(root.join("movies")).unwrap() { let path = entry.unwrap().path(); let movie = Movie::from_json(&std::fs::read_to_string(&path).unwrap()).unwrap(); if movie.path_mod.is_none() && movie.patches.is_empty() { continue; } let mut machine = Machine::new(&roms); movie.setup(&mut machine).unwrap(); let mut calm_since = 0; let mut before = objects(&machine.shared.borrow()); for c in movie.inputs() { machine.set_controls(c); machine.run_frame(); let now = objects(&machine.shared.borrow()); if !undisturbed(&machine) { calm_since = machine.frame; } if machine.frame - calm_since > QUIET { if let Some(what) = visible_teleport(&before, &now) { panic!("{}: frame {}: {what}", path.display(), machine.frame); } } before = now; } } } /// The daughter-card space: the unmodified game never depends on what the /// sub CPUs read at 0x1000-0x3fff (unconnected on the real board, where a /// card carrying a mod's new code would answer), so a card there leaves the /// original game alone. The 20-minute movie replays to the same RAM with /// that range all 0xff instead of MAME's zeros. #[test] fn the_original_game_ignores_the_daughter_card_space() { use emu_galaga::galaga::bus::Cpu; use emu_galaga::galaga::movie::ram_crc32; 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 movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); let mut machine = Machine::new(&roms); movie.setup(&mut machine).unwrap(); machine .patch_rom(Cpu::Sub, 0x1000, &[0xff; 0x3000]) .unwrap(); machine .patch_rom(Cpu::Sub2, 0x1000, &[0xff; 0x3000]) .unwrap(); for c in movie.inputs() { machine.set_controls(c); machine.run_frame(); } assert_eq!(ram_crc32(&machine), movie.final_ram_crc32); } /// A mod with a new title screen boots past the power-on test (every ROM /// checksum kept) into attract mode, and its logo is in tile RAM exactly as /// its title_run lines say once the title screen is up. #[test] fn title_screens_boot_and_draw_their_logo() { use emu_galaga::galaga::title::tile_address; 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 mut checked = 0; for entry in std::fs::read_dir(root.join("mods")).unwrap() { let path = entry.unwrap().path(); if path.extension().is_none_or(|e| e != "paths") { continue; } let path_mod = PathMod::load(&path).unwrap(); let Some(title) = &path_mod.title else { continue; }; let mut machine = Machine::new(&roms); path_mod.apply(&mut machine).unwrap(); for _ in 0..1350 { machine.run_frame(); } let s = machine.shared.borrow(); assert_eq!( s.ram2[0x201], 1, "{}: not in attract mode: the power-on test stopped it", path_mod.name ); for run in &title.runs { for i in 0..run.count { let a = usize::from(tile_address(run.row, run.col + i) - 0x8000); assert_eq!( (s.video_ram[a], s.video_ram[0x400 + a]), (run.code, run.colour), "{}: logo cell row {} col {}", path_mod.name, run.row, run.col + i ); } } checked += 1; } assert!(checked > 0, "no mod with a title"); } /// A mod's sprite lines give exactly the picture a replacement sprite ROM /// would: a frame of play rendered with `PathMod::apply_video` equals the /// same frame rendered by a pipeline built from the patched sprite ROM. #[test] fn sprite_and_tile_lines_are_replacement_roms() { use emu_galaga::galaga::video::{Video, SCREEN_HEIGHT, SCREEN_WIDTH}; 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 movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); for entry in std::fs::read_dir(root.join("mods")).unwrap() { let path = entry.unwrap().path(); if path.extension().is_none_or(|e| e != "paths") { continue; } let path_mod = PathMod::load(&path).unwrap(); if path_mod.sprites.is_empty() && path_mod.tiles.is_empty() { continue; } let mut patched = roms.clone(); for (code, bytes) in &path_mod.sprites { let at = 64 * usize::from(*code); patched.sprites[at..at + 64].copy_from_slice(bytes); } for (code, bytes) in &path_mod.tiles { let at = 16 * usize::from(*code); patched.chars[at..at + 16].copy_from_slice(bytes); } let mut from_rom = Video::new(&patched); let mut from_mod = Video::new(&roms); path_mod.apply_video(&mut from_mod); let mut machine = Machine::new(&roms); movie.setup(&mut machine).unwrap(); let (mut a, mut b) = ( vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4], vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4], ); for (f, c) in movie.inputs().into_iter().enumerate().take(4000) { machine.set_controls(c); machine.run_frame(); if f % 50 == 0 { let s = machine.shared.borrow(); from_rom.render(&s, machine.videolatch_at_vblank_end); from_mod.render(&s, machine.videolatch_at_vblank_end); from_rom.screen_rgba(&mut a); from_mod.screen_rgba(&mut b); assert!(a == b, "{}: frame {f} differs", path_mod.name); } } } }