//! The verified Galaxian parent set. Transcribed from galemu's `roms.py` and //! `docs/rom-manifest.md`: every file must match its size, CRC32 and SHA-1 //! before a byte of it reaches the bus. Nothing here reads archives; the //! caller (the page's unzip, or a test reading an extracted directory) hands //! over named byte strings. use sha1::{Digest, Sha1}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct RomSpec { pub filename: &'static str, pub offset: usize, pub size: usize, pub crc32: u32, pub sha1_hex: &'static str, } const fn spec( filename: &'static str, offset: usize, size: usize, crc32: u32, sha1_hex: &'static str, ) -> RomSpec { RomSpec { filename, offset, size, crc32, sha1_hex, } } pub const PROGRAM_ROM_SPECS: [RomSpec; 5] = [ spec( "galmidw.u", 0x0000, 0x0800, 0x745E2D61, "e65f74e35b1bfaccd407e168ea55678ae9b68edf", ), spec( "galmidw.v", 0x0800, 0x0800, 0x9C999A40, "02fdcd95d8511e64c0d2b007b874112d53e41045", ), spec( "galmidw.w", 0x1000, 0x0800, 0xB5894925, "0046b9ed697a34d088de1aead8bd7cbe526a2396", ), spec( "galmidw.y", 0x1800, 0x0800, 0x6B3CA10B, "18d8714e5ef52f63ba8888ecc5a25b17b3bf17d1", ), spec( "7l", 0x2000, 0x0800, 0x1B933207, "8b44b0f74420871454e27894d0f004859f9e59a9", ), ]; pub const PROGRAM_ROM_SIZE: usize = 0x2800; pub const GRAPHICS_ROM_SPECS: [RomSpec; 2] = [ spec( "1h.bin", 0x0000, 0x0800, 0x39FB43A4, "4755609bd974976f04855d51e08ec0d62ab4bc07", ), spec( "1k.bin", 0x0800, 0x0800, 0x7E3F56A2, "a9795d8b7388f404f3b0e2c6ce15d713a4c5bafa", ), ]; pub const GRAPHICS_ROM_SIZE: usize = 0x1000; pub const PALETTE_PROM_SPEC: RomSpec = spec( "6l.bpr", 0x0000, 0x0020, 0xC3AC9467, "f382ad5a34d282056c78a5ec00c30ec43772bae2", ); /// Every file the set needs, in manifest order. pub fn required_files() -> impl Iterator { PROGRAM_ROM_SPECS .iter() .chain(GRAPHICS_ROM_SPECS.iter()) .chain(core::iter::once(&PALETTE_PROM_SPEC)) } /// The three verified regions, ready for the board and the video decoder. #[derive(Clone, Debug, PartialEq, Eq)] pub struct RomSet { pub program: Vec, pub graphics: Vec, pub palette: Vec, } /// Check one file against its spec: size, CRC32, SHA-1, in that order, with /// galemu's messages. pub fn validate(label: &str, contents: &[u8], spec: &RomSpec) -> Result<(), String> { if contents.len() != spec.size { return Err(format!( "{label}: expected {} bytes, found {} bytes", spec.size, contents.len() )); } let actual_crc32 = crc32fast::hash(contents); if actual_crc32 != spec.crc32 { return Err(format!( "{label}: expected CRC32 {:08x}, found {actual_crc32:08x}", spec.crc32 )); } let actual_sha1 = hex(&Sha1::digest(contents)); if actual_sha1 != spec.sha1_hex { return Err(format!( "{label}: expected SHA-1 {}, found {actual_sha1}", spec.sha1_hex )); } Ok(()) } /// Build the set from a lookup of file name to contents. Names are matched /// case-insensitively on the basename, as galemu's archive reader does. pub fn load(mut file: impl FnMut(&str) -> Option>) -> Result { let mut read = |spec: &RomSpec, region: &str| -> Result, String> { let contents = file(spec.filename) .ok_or_else(|| format!("missing required {region}: {}", spec.filename))?; validate(spec.filename, &contents, spec)?; Ok(contents) }; let mut program = vec![0u8; PROGRAM_ROM_SIZE]; for spec in &PROGRAM_ROM_SPECS { let contents = read(spec, "program ROM")?; program[spec.offset..spec.offset + spec.size].copy_from_slice(&contents); } let mut graphics = vec![0u8; GRAPHICS_ROM_SIZE]; for spec in &GRAPHICS_ROM_SPECS { let contents = read(spec, "graphics ROM")?; graphics[spec.offset..spec.offset + spec.size].copy_from_slice(&contents); } let palette = read(&PALETTE_PROM_SPEC, "palette PROM")?; Ok(RomSet { program, graphics, palette, }) } fn hex(bytes: &[u8]) -> String { bytes.iter().map(|b| format!("{b:02x}")).collect() }