// SPDX-License-Identifier: MIT //! The snapshot's byte form is exact: a run saved to bytes at frame N and loaded into //! a fresh board continues as the run that was never interrupted, frame for frame //! (picture, sound, the three RAMs, counters) for 3,000 frames, in attract mode and in //! play; the bytes of the loaded board are the bytes saved; and bytes cut short or //! with a byte out of range are refused. Needs the ROM set at rom/galaxian/; skips //! without it. use std::fs; use std::path::PathBuf; use z80_wasm::galaxian::{roms, Galaxian, Snapshot}; const ALL: [&str; 5] = ["coin_1", "start_1", "player_1_left", "player_1_right", "player_1_fire"]; /// The controls a frame holds in play: a coin, start, then moving and firing. fn play(frame: u32) -> Vec<&'static str> { match frame { 400..=405 => vec!["coin_1"], 500..=505 => vec!["start_1"], f if f > 700 => { let mut held = vec![if (f / 50) % 2 == 0 { "player_1_left" } else { "player_1_right" }]; if f % 8 < 2 { held.push("player_1_fire"); } held } _ => vec![], } } type Seen = (Vec, Vec, Vec, Vec, Vec, u64, u64); fn step(game: &mut Galaxian, held: &[&str]) -> Seen { for name in ALL { game.machine.board_mut().set_input(name, held.contains(&name)).unwrap(); } game.run_frame().unwrap(); let mut rgba = vec![0; 224 * 256 * 4]; game.frame.portrait_rgba(&mut rgba); let board = game.machine.board(); ( rgba, game.pcm.to_vec(), board.work_ram.to_vec(), board.video_ram.to_vec(), board.object_ram.to_vec(), game.machine.vblank_count, board.instruction_count, ) } fn round_trip(label: &str, save_at: u32, controls: fn(u32) -> Vec<&'static str>) { let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("rom/galaxian"); if !dir.exists() { eprintln!("rom/galaxian/ not present; skipping"); return; } let roms = roms::load(|name| fs::read(dir.join(name)).ok()).unwrap(); let mut game = Galaxian::new(&roms, true).unwrap(); for frame in 0..save_at { step(&mut game, &controls(frame)); } let bytes = game.snapshot().to_bytes(); let mut fresh = Galaxian::new(&roms, true).unwrap(); fresh.restore(&Snapshot::from_bytes(&bytes).unwrap()); assert_eq!(fresh.snapshot().to_bytes(), bytes, "{label}: the loaded board saves other bytes"); for frame in save_at..save_at + 3000 { let held = controls(frame); let (a, b) = (step(&mut game, &held), step(&mut fresh, &held)); assert!(a == b, "{label}: frame {frame} differs after loading the state saved at {save_at}"); } eprintln!("{label}: {} bytes saved at frame {save_at}, 3000 frames identical", bytes.len()); // Refused: cut short, one byte long, a yes-or-no that is neither. assert!(Snapshot::from_bytes(&bytes[..bytes.len() - 1]).is_err()); let mut longer = bytes.clone(); longer.push(0); assert!(Snapshot::from_bytes(&longer).is_err()); let mut bad = bytes.clone(); bad[8 + 5 * 2 + 2] = 7; // iff1 assert!(Snapshot::from_bytes(&bad).is_err()); } #[test] fn a_state_saved_in_attract_mode_loads_into_a_fresh_board() { round_trip("attract", 600, |_| vec![]); } #[test] fn a_state_saved_in_play_loads_into_a_fresh_board() { round_trip("play", 1000, play); }