//! The verified Galaga parent set (`docs/rom-manifest.md`): every member must //! match its size, CRC32 and SHA-1 before a byte of it reaches the bus. //! Nothing here reads archives; the caller hands over named byte strings. use sha1::{Digest, Sha1}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Region { /// Main CPU program, 16 KB at 0x0000. Main, /// Sub CPU program, 4 KB at 0x0000. Sub, /// Sub2 (sound) CPU program, 4 KB at 0x0000. Sub2, /// 8x8 character tiles, 4 KB. Chars, /// 16x16 sprite tiles, 8 KB. Sprites, /// Palette (32), character lookup (256), sprite lookup (256): 0x220 bytes. Proms, /// WSG wave table, 256 bytes. Wave, /// Timing PROM; not used by the emulation, verified because the set is the set. Timing, /// 51xx MCU program; verified, never executed (HLE, docs/build-approach.md). Mcu51, /// 54xx MCU program; verified, never executed. Mcu54, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RomSpec { pub filename: &'static str, pub region: Region, pub offset: usize, pub size: usize, pub crc32: u32, pub sha1_hex: &'static str, } const fn spec( filename: &'static str, region: Region, offset: usize, size: usize, crc32: u32, sha1_hex: &'static str, ) -> RomSpec { RomSpec { filename, region, offset, size, crc32, sha1_hex, } } /// The sixteen members of MAME's `galaga` set, in MAME's load order. pub const ROM_SPECS: [RomSpec; 16] = [ spec( "gg1_1b.3p", Region::Main, 0x0000, 0x1000, 0xAB036C9F, "ca7f5da42d4e76fd89bb0b35198a23c01462fbfe", ), spec( "gg1_2b.3m", Region::Main, 0x1000, 0x1000, 0xD9232240, "ab202aa259c3d332ef13dfb8fc8580ce2a5a253d", ), spec( "gg1_3.2m", Region::Main, 0x2000, 0x1000, 0x753CE503, "481f443aea3ed3504ec2f3a6bfcf3cd47e2f8f81", ), spec( "gg1_4b.2l", Region::Main, 0x3000, 0x1000, 0x499FCC76, "ddb8b121903646c320939c7d13f4aa4ebb130378", ), spec( "gg1_5b.3f", Region::Sub, 0x0000, 0x1000, 0xBB5CAAE3, "e957a581463caac27bc37ca2e2a90f27e4f62b6f", ), spec( "gg1_7b.2c", Region::Sub2, 0x0000, 0x1000, 0xD016686B, "44c1a04fba3c7c826ff484185cb881b4b22e6657", ), spec( "gg1_9.4l", Region::Chars, 0x0000, 0x1000, 0x58B2F47C, "62f1279a784ab2f8218c4137c7accda00e6a3490", ), spec( "gg1_11.4d", Region::Sprites, 0x0000, 0x1000, 0xAD447C80, "e697c180178cabd1d32483c5d8889a40633f7857", ), spec( "gg1_10.4f", Region::Sprites, 0x1000, 0x1000, 0xDD6F1AFC, "c340ed8c25e0979629a9a1730edc762bd72d0cff", ), spec( "prom-5.5n", Region::Proms, 0x0000, 0x0020, 0x54603C6B, "1a6dea13b4af155d9cb5b999a75d4f1eb9c71346", ), spec( "prom-4.2n", Region::Proms, 0x0020, 0x0100, 0x59B6EDAB, "0281de86c236c88739297ff712e0a4f5c8bf8ab9", ), spec( "prom-3.1c", Region::Proms, 0x0120, 0x0100, 0x4A04BB6B, "cdd4bc1013f5c11984fdc4fd10e2d2e27120c1e5", ), spec( "prom-1.1d", Region::Wave, 0x0000, 0x0100, 0x7A2815B4, "085ada18c498fdb18ecedef0ea8fe9217edb7b46", ), spec( "prom-2.5c", Region::Timing, 0x0000, 0x0100, 0x77245B66, "0c4d0bee858b97632411c440bea6948a74759746", ), spec( "51xx.bin", Region::Mcu51, 0x0000, 0x0400, 0xC2F57EF8, "50de79e0d6a76bda95ffb02fcce369a79e6abfec", ), spec( "54xx.bin", Region::Mcu54, 0x0000, 0x0400, 0xEE7357E0, "01bdf984a49e8d0cc8761b2cc162fd6434d5afbe", ), ]; pub const MAIN_ROM_SIZE: usize = 0x4000; pub const SUB_ROM_SIZE: usize = 0x1000; pub const CHAR_ROM_SIZE: usize = 0x1000; pub const SPRITE_ROM_SIZE: usize = 0x2000; pub const PROM_SIZE: usize = 0x220; pub const WAVE_SIZE: usize = 0x100; /// The verified set, assembled by region. #[derive(Clone, Debug, PartialEq, Eq)] pub struct RomSet { pub main: Vec, pub sub: Vec, pub sub2: Vec, pub chars: Vec, pub sprites: Vec, pub proms: Vec, pub wave: Vec, } impl RomSet { /// Build the set from named members. Every one of the sixteen must be /// present and match; any extra member is ignored (a nested archive /// path or a different case in the name is the caller's business: /// `find_member`). Errors name the member and what was wrong. pub fn from_members(mut lookup: F) -> Result where F: FnMut(&str) -> Option>, { let mut set = RomSet { main: vec![0; MAIN_ROM_SIZE], sub: vec![0; SUB_ROM_SIZE], sub2: vec![0; SUB_ROM_SIZE], chars: vec![0; CHAR_ROM_SIZE], sprites: vec![0; SPRITE_ROM_SIZE], proms: vec![0; PROM_SIZE], wave: vec![0; WAVE_SIZE], }; let mut missing = Vec::new(); for spec in &ROM_SPECS { let Some(bytes) = lookup(spec.filename) else { missing.push(spec.filename); continue; }; verify(spec, &bytes)?; let target: Option<&mut Vec> = match spec.region { Region::Main => Some(&mut set.main), Region::Sub => Some(&mut set.sub), Region::Sub2 => Some(&mut set.sub2), Region::Chars => Some(&mut set.chars), Region::Sprites => Some(&mut set.sprites), Region::Proms => Some(&mut set.proms), Region::Wave => Some(&mut set.wave), Region::Timing | Region::Mcu51 | Region::Mcu54 => None, }; if let Some(target) = target { target[spec.offset..spec.offset + spec.size].copy_from_slice(&bytes); } } if !missing.is_empty() { return Err(format!( "not the Galaga (Namco rev. B) set: missing {}", missing.join(", ") )); } Ok(set) } } /// Size, then CRC32, then SHA-1; the first mismatch is the error. pub fn verify(spec: &RomSpec, bytes: &[u8]) -> Result<(), String> { if bytes.len() != spec.size { return Err(format!( "{}: expected {} bytes, got {}", spec.filename, spec.size, bytes.len() )); } let crc = crc32fast::hash(bytes); if crc != spec.crc32 { return Err(format!( "{}: CRC32 {:08x}, expected {:08x}", spec.filename, crc, spec.crc32 )); } let sha = Sha1::digest(bytes); let sha_hex: String = sha.iter().map(|b| format!("{b:02x}")).collect(); if sha_hex != spec.sha1_hex { return Err(format!( "{}: SHA-1 {}, expected {}", spec.filename, sha_hex, spec.sha1_hex )); } Ok(()) } /// galemu's member matching: compare the basename, case-insensitively, after /// normalising backslashes; on a duplicate basename the size decides. pub fn find_member<'a>( names: &[&'a str], wanted: &str, size: usize, sizes: &[usize], ) -> Option<&'a str> { let wanted = wanted.to_ascii_lowercase(); let mut candidates = names.iter().zip(sizes).filter(|(name, _)| { let base = name.replace('\\', "/"); let base = base.rsplit('/').next().unwrap_or(""); base.to_ascii_lowercase() == wanted }); let first = candidates.next()?; if let Some(second) = candidates.next() { return [first, second] .into_iter() .chain(candidates) .find(|(_, s)| **s == size) .map(|(n, _)| *n); } Some(*first.0) } /// Load an extracted set from a directory (tests only; the page never touches /// a file system). pub fn load_directory(dir: &std::path::Path) -> Result { RomSet::from_members(|name| std::fs::read(dir.join(name)).ok()) }