//! The board against galemu: every run in oracle/runs.json produces, from //! the Rust board, the byte-for-byte JSON galemu produced for it //! (oracle/reference/.json). Needs the extracted ROM set at //! rom/galaxian/ (never committed); skips without it. use std::fs; use std::path::{Path, PathBuf}; use serde_json::Value; use z80_wasm::galaxian::board::Machine; use z80_wasm::galaxian::roms::{self, RomSet}; use z80_wasm::galaxian::trace::{parse_inputs, run_frames, run_trace}; fn root() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) } fn rom_set() -> Option { let dir = root().join("rom/galaxian"); if !dir.is_dir() { eprintln!("rom/galaxian/ not present; skipping"); return None; } Some(roms::load(|name| fs::read(dir.join(name)).ok()).expect("the ROM set verifies")) } fn first_difference(a: &str, b: &str) -> String { let index = a .bytes() .zip(b.bytes()) .position(|(x, y)| x != y) .unwrap_or(a.len().min(b.len())); let start = index.saturating_sub(120); format!( "first difference at byte {index} (lengths {} vs {}):\n ours: ...{}\n galemu: ...{}", a.len(), b.len(), &a[start..(index + 120).min(a.len())], &b[start..(index + 120).min(b.len())] ) } #[test] fn every_reference_run_is_identical() { let Some(roms) = rom_set() else { return }; let runs: Vec = serde_json::from_str(&fs::read_to_string(root().join("oracle/runs.json")).unwrap()) .unwrap(); let mut checked = 0; for run in &runs { let name = run["name"].as_str().unwrap(); let reference_path = root().join("oracle/reference").join(format!("{name}.json")); let Ok(reference) = fs::read_to_string(&reference_path) else { eprintln!( "{name}: no reference at {}; skipping", reference_path.display() ); continue; }; let inputs: Vec = run["inputs"] .as_array() .unwrap() .iter() .map(|v| v.as_str().unwrap().to_string()) .collect(); let inputs = parse_inputs(&inputs).unwrap(); let frames = run.get("frames").and_then(Value::as_u64); let vblank = frames.is_some() || run["vblank"].as_bool().unwrap_or(false); let mut machine = Machine::new(&roms.program, vblank).unwrap(); machine.board_mut().trace_memory_writes = run["memory_writes"].as_bool().unwrap(); let ours = match frames { Some(frames) => run_frames(&mut machine, frames, &inputs).unwrap(), None => { run_trace(&mut machine, run["instructions"].as_u64().unwrap(), &inputs).unwrap() } }; let theirs = reference.trim_end(); assert!( ours == theirs, "{name}: {}", first_difference(&ours, theirs) ); checked += 1; } assert!(checked > 0, "no reference run was checked"); eprintln!("{checked} reference runs identical"); } #[test] fn a_wrong_rom_is_refused_before_it_loads() { let Some(roms) = rom_set() else { return }; let mut bad = roms.program[..0x800].to_vec(); bad[0] ^= 1; let error = roms::load(|name| { if name == "galmidw.u" { Some(bad.clone()) } else { None } }) .err() .unwrap(); assert!(error.contains("expected CRC32 745e2d61"), "{error}"); let _ = Path::new(""); }