// SPDX-License-Identifier: MIT // Holds data from MAME 0.285 hash/a7800.xml (CC0-1.0, via mame_softlist.json) and the // slot names of MAME's bus/a7800/a78_slot.cpp and a78_carts.h (BSD-3-Clause, Fabio // Priuli; facts only, no code). Notices and attribution: NOTICE, sections 1 and 2. //! The payload-hash table: which hardware a headerless dump needs. //! //! Two kinds of rows, and only two, may be committed (the reference's //! docs/handoff-brief.md "Open decisions" 6, decided (b)): //! //! 1. **softlist**: MAME 0.285's `hash/a7800.xml`, CC0-1.0, copied by the reference's //! `scripts/make_cart_table.py` into `mame_softlist.json` (the same file, byte for //! byte, here). An entry matches a payload when its ROM chips, concatenated in list //! order, are the payload (SHA-1 per chip, as the list keys them). //! 2. **confirmed**: rows in [`CONFIRMED`], each confirmed by the reference //! from the dump's own bytes or its behaviour on its board, written in its //! own words, each citing how it was confirmed. Seventeen today: Double Dragon //! (NTSC), Double Dragon (PAL) and Rampage (NTSC), the Activision dumps whose //! bytes are the CC0 list's entries with the 8 KiB halves of every bank //! exchanged (the reference's `scripts/outside_softlist.py`; the facts are the CC0 //! entries', the chip order ours), and 14 SuperGame dumps with bank 6 at //! $4000, confirmed from their own code ([`BANK6`]). No row comes from //! a GPL table. //! //! The GPL tables themselves (ProSystem.dat, A7800DS's Database.c) are never //! read here. //! //! Rust-forced differences (a7800-python `cartridge/table.py` at d6b220d): the JSON is //! compiled in (`include_str!`) and parsed once (`functools.cache` is a `OnceLock`); the //! dictionaries are arrays of pairs; `Row.pokey` is a `Vec`. use std::collections::BTreeMap; use std::sync::OnceLock; use sha1::Sha1; use sha2::{Digest, Sha256}; use super::description::hex; const DATA: &str = include_str!("mame_softlist.json"); /// One cartridge's hardware, as a table states it. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Row { pub source: String, // "softlist" or "confirmed" pub key: String, // software name (softlist) or payload SHA-256 (confirmed) pub title: String, pub scheme: String, pub at_4000: String, pub pokey: Vec, pub hsc: bool, pub xm: bool, pub region: String, pub lightgun: bool, pub citation: String, } // MAME slot feature -> (scheme, $4000 option, POKEY bases, hsc, xm). Read off // MAME 0.285's src/devices/bus/a7800/a78_slot.cpp slot names and // a78_carts.h descriptions (OBSERVED: MAME 0.285's statement of each board); the // 144 KiB "a78_sg9" boards carry a ninth ROM bank at $4000 and the 128 KiB one // bank 6 there (C9, settled per dump by size; DOC: Stolberg's options). pub const SLOTS: [(&str, (&str, &str, &[u16])); 16] = [ ("a78_rom", ("linear", "none", &[])), ("a78_pokey", ("linear", "pokey", &[0x4000])), ("a78_sg", ("supergame", "none", &[])), ("a78_sg_pokey", ("supergame", "pokey", &[0x4000])), ("a78_sg_ram", ("supergame", "ram16", &[])), ("a78_sg9", ("supergame", "rom", &[])), ("a78_mram", ("linear", "mirror-ram-2k", &[])), ("a78_abs", ("absolute", "none", &[])), ("a78_act", ("activision", "none", &[])), ("a78_hsc", ("linear", "none", &[])), ("a78_xboard", ("linear", "none", &[0x450])), ("a78_xm", ("linear", "none", &[0x450])), ("a78_p450_t0", ("linear", "none", &[0x450])), ("a78_p450_t1", ("linear", "pokey", &[0x4000, 0x450])), ("a78_p450_t6", ("supergame", "ram16", &[0x450])), ("a78_p450_ta", ("supergame", "rom", &[0x450])), ]; fn slot(name: &str) -> Option<(&'static str, &'static str, &'static [u16])> { SLOTS.iter().find(|(n, _)| *n == name).map(|(_, v)| *v) } /// One software-list entry (the fields the table reads). #[derive(Clone, Debug, serde::Deserialize)] pub struct Entry { pub name: String, pub description: String, pub compatibility: Option, pub slot: String, pub lightgun: bool, pub ram: u64, pub roms: Vec<(usize, String, String)>, } #[derive(serde::Deserialize)] struct SoftList { mame_version: String, entries: Vec, } fn softlist_row(entry: &Entry) -> Row { let (scheme, mut at_4000, pokey) = slot(&entry.slot).expect("a known slot"); let size: usize = entry.roms.iter().map(|r| r.0).sum(); if (entry.slot == "a78_sg9" || entry.slot == "a78_p450_ta") && size == 0x20000 { at_4000 = "fixed-bank"; // 128 KiB: no ninth bank, bank 6 at $4000 (C9) } if (entry.slot == "a78_sg_ram" || entry.slot == "a78_p450_t6") && entry.ram == 0x2000 { at_4000 = "ram8-mirror"; // 8 KiB RAM in the list (OBSERVED: MAME 0.285); DOC: Stolberg } let region = match entry.compatibility.as_deref().unwrap_or("") { "NTSC" => "ntsc", "PAL" => "pal", _ => "unknown", }; Row { source: "softlist".to_string(), key: entry.name.clone(), title: entry.description.clone(), scheme: scheme.to_string(), at_4000: at_4000.to_string(), pokey: pokey.to_vec(), hsc: entry.slot == "a78_hsc", xm: entry.slot == "a78_xm" || entry.slot == "a78_xboard", region: region.to_string(), lightgun: entry.lightgun, citation: format!("MAME 0.285 hash/a7800.xml '{}' slot {}", entry.name, entry.slot), } } struct Loaded { version: String, by_size: BTreeMap>, } fn softlist() -> &'static Loaded { static LIST: OnceLock = OnceLock::new(); LIST.get_or_init(|| { let data: SoftList = serde_json::from_str(DATA).expect("mame_softlist.json"); let mut by_size: BTreeMap> = BTreeMap::new(); for entry in data.entries { if !entry.roms.is_empty() && slot(&entry.slot).is_some() { by_size .entry(entry.roms.iter().map(|r| r.0).sum()) .or_default() .push(entry); } } Loaded { version: data.mame_version, by_size, } }) } pub fn softlist_version() -> &'static str { &softlist().version } // Rows confirmed by the reference (see the module header). Payload SHA-256 -> // Row, each saying how it was confirmed. // // The three Activision dumps: MAME's CC0 list has these ROMs, but stores them // with the two 8 KiB halves of every 16 KiB bank exchanged (its Activision // mapping reads $A000 from a bank's second half; Stolberg's guide, whose // layout this board follows, from its first). Confirmed by the dumps' own // bytes: each payload, so rearranged, has exactly the SHA-1 of the named list // entry (scripts/outside_softlist.py, milestone 3). The facts are the CC0 // list's; only the chip order is ours. const ACTIVISION: &str = "confirmed: = MAME 0.285 hash/a7800.xml '{}' (CC0) with 8 KiB halves exchanged"; pub const CONFIRMED: [(&str, &str, &str, &str); 3] = [ // (payload SHA-256, key, title, region): scheme activision, nothing at $4000 ( "12e5272697550c7b4d40db1937f157a3fa9c65a8fa0f4b40aba5a96e780f3fad", "ddragonu", "Double Dragon (NTSC)", "ntsc", ), ( "126fbe59060f41861f3c779d8df7e0b70e2454ad9c6d273a64b7dafd57ed5973", "ddragon", "Double Dragon (PAL)", "pal", ), ( "5cdd510c97021f7c74bb635398278185efd265c14cd0bb29510159c4704bb049", "rampage", "Rampage (NTSC)", "ntsc", ), ]; fn confirmed(digest: &str) -> Option { CONFIRMED .iter() .find(|(d, ..)| *d == digest) .map(|(_, key, title, region)| Row { source: "confirmed".to_string(), key: key.to_string(), title: title.to_string(), scheme: "activision".to_string(), at_4000: "none".to_string(), pokey: Vec::new(), hsc: false, xm: false, region: region.to_string(), lightgun: false, citation: ACTIVISION.replace("{}", key), }) } // Bank 6 fixed at $4000-$7FFF on 128 KiB SuperGame boards: what the CC0 list // does not say. It types every plain 128 KiB SuperGame board `a78_sg` and // states nothing about $4000-$7FFF; Stolberg's guide (tier 3) says "some games // have bank 6 fixed at this area" without naming them. The rule used here: // a dump gets bank 6 at $4000 when **its own bytes** show the need, i.e. code // that the fixed bank (bank 7 at $C000) reaches by its own flow from the // vectors calls or jumps into $4000-$7FFF, and bank 6, placed there, is code // from those targets (thousands of documented instructions, none // undocumented). The reference's `scripts/scan_4000.py` makes that check; of the 64 and // 128 KiB SuperGame dumps in the licensed set exactly these 14 meet it, and no // other dump has a single such call. Evidence per row: (first call site -> // target, calls found, bank-6 instructions reached from them). The first // four were found by the milestone-3 boot sweep (black at frame 400 with // nothing at $4000; Basketbrawl and Scrapyard Dog fell into 2600 mode, the // BIOS's signature check failing), Xenophobe (USA) by the frame compare (its // call from $F7E5 is the instruction stream MAME 0.285's trace runs there); // the rest by the scan. The rest of each row is the CC0 list's entry. Evidence: the // option DOC (Stolberg); which dumps, the dumps' own code (read, not run). pub const BANK6: [(&str, (&str, &str)); 14] = [ ( "050ad25f716eaa9df63ea3bd897656b2742d3db32a936a6ce720c5c026f9612e", ("Basketbrawl (USA)", "reset vector -> $FD35 JMP $4D64, 1 call, 4,704"), ), ( "6b7cde57e026d97b7696aff8a1ff24afac50bec960dc31a63aed50750052bb7f", ("Basketbrawl (Europe)", "reset vector -> $FD35 JMP $4D70, 1 call, 4,677"), ), ( "7fcd51b4c54feb8eebae9f2c2e0c63f1ebaf86bd04f9238825d1e4d4e99d0468", ("Scrapyard Dog (USA)", "$F7A5 -> $4002, 1 call, 2,612"), ), ( "29fc4bb1ecad0f4c5af85164fe13498a8f96fb59eb7a4eca11f388dbe36aefb4", ("Scrapyard Dog (Europe)", "$F7A5 -> $4002, 1 call, 2,612"), ), ( "3d17789bf2cfa187e3cbdec6a7c8fd65924efcccec90d0ce54917a9252878680", ("Midnight Mutants (USA)", "$FF21 -> $4000, 1 call, 3,464"), ), ( "49f0dfe29c05a93178cd8dcd2e317dadb43793f29a777b8114d1a6d63a51dee8", ("Midnight Mutants (Europe)", "$FF21 -> $4000, 1 call, 3,464"), ), ( "501af1f3c2a8ceee19d35113de0b68ce26374ad24a1b3a85ba88fff30dd25c7f", ("Meltdown (USA)", "$C81D -> $4000, 1 call, 1,223"), ), ( "0921f020d97a0de260a4fe2ed548703cef436abc068faa174821653b373443a2", ("Meltdown (Europe)", "$C81D -> $4000, 1 call, 1,223"), ), ( "3239db5465f42d4e05220a50fd7a489687c441539dcd287b57ea7208f9753f28", ("Xenophobe (USA)", "$F7E5 -> $6EA4, 35 calls, 453"), ), ( "5233eb4782033ede4044a83815a78b93a339899cfac5cd0b255513946de27296", ("Xenophobe (Europe)", "$F7E5 -> $6EE8, 35 calls, 453"), ), ( "aabbf94532d88428a7df26c27c5b4286f3848dc2f9fc39a13fb65b661622064b", ("Fatal Run (USA)", "$D333 -> $685D, 19 calls, 1,871"), ), ( "5d00b144b258f38f30a26654479ba257f9cd59ec20222510fe8232fa1c00e736", ("Fatal Run (Europe)", "$D333 -> $685D, 19 calls, 1,871"), ), ( "9a2d9bda7c19386a4a1111090174ec0fc39fbacfd2529874e83dacd65de7cf9f", ("Touchdown Football (USA)", "$D0AB -> $6000, 14 calls, 538"), ), // 64 KiB: "bank 6" is the dump's third 16 KiB (banks masked to four). // Stolberg names this one: "PAL Karateka ... only uses banks 4-7 ... and // bank 6 is also fixed at $4000-$7fff". ( "b733ceffcb351ac7e56a9cb4a8a98251c77ecb82e2246b5f24682f26a6500961", ( "Karateka (Europe)", "reset vector $4000, NMI $58E5, IRQ $59E4, 3 calls, 164", ), ), ]; // The same board option shown by the dump's behaviour instead of its code: run // from the BIOS with nothing at $4000 (the reference's `scripts/uses_4000.py`, frame 400), // each of these fetches from $4000-$7FFF, as display-list or graphics reads by // MARIA (DMA) or as CPU reads of tables, with no write to the address first // (a write-then-read is a RAM probe: Planet Smashers and Sentinel probe // $4000-$5DFF that way, and Planet Smashers does nothing else there, so it gets // no row). What sits there is bank 6: with it, each title's frame 400 shows // the picture that was garbled or missing (Ace of Aces, Mean 18, Klax: the // title art and text are fetched there). Evidence: (first access, counts). Tag: the // option DOC (Stolberg); the choice per dump FITTED to its frame-400 picture on the // reference's board (the alternative tried: nothing at $4000, garbled or missing art). pub const BANK6_BEHAVIOUR: [(&str, (&str, &str)); 12] = [ ( "749cbebd7f950223efe7b24f43a4baa368f6068cc4078600918a4b3dbb7713b9", ( "Ace of Aces (USA)", "DMA from $4726 at frame 161, 63,096 DMA and 552 CPU reads", ), ), ( "4f1d3158b8915388a3b7be01e446c4abdff69764576f79faa563113903373ed4", ( "Ace of Aces (Europe)", "DMA from $4726 at frame 5, 104,224 DMA and 552 CPU reads", ), ), ( "02eeb391cd00f500d40ba5afc902090ab2753b163ac90e72c52c3bb1b0985433", ("Dark Chambers (USA)", "DMA from $731A at frame 128, 7,725 DMA reads"), ), ( "37ff94f060480bda1f66b540c29709bbefae1a44c51994b28520a4e8d7fd8100", ("Fight Night (USA)", "CPU reads from $4C3C at frame 162, 474 reads"), ), ( "df321876394ed8fe2471f95c1499b610c213ca0f90ceebd73f81717c9552574e", ("Klax (USA) (Proto)", "DMA from $7373 at frame 165, 107,015 DMA reads"), ), ( "645fa4dec063e18fa4ccc3de83fa1839b30d01151881e108a13cd6ca26d6d521", ( "Mean 18 Ultimate Golf (USA)", "DMA from $7F53 at frame 163, 1,213,440 DMA reads", ), ), ( "6592c476a80076fa7c133ca1283c88c9ecdfc10a314ccb6a675769100b8e8a96", ( "Mean 18 Ultimate Golf (Europe)", "DMA from $7F53 at frame 8, 2,006,720 DMA reads", ), ), ( "b5bb5a789a55ef60235edc43da72a8a88c4cb23e9fa99ac6acf9f62bbf810b3d", ("Ninja Golf (USA)", "DMA from $5555 at frame 161, 381,400 DMA reads"), ), ( "41efbf839627f6d8cc4e289cfdad604ad6fb54771194994731053f7b30a8dca0", ("Ninja Golf (Europe)", "DMA from $5500 at frame 6, 635,277 DMA reads"), ), ( "68767f30282bddb1a46b830cf7722a32ecb29c2e0dd80a14fe568c78153aa8f0", ( "Rampart (USA) (Proto)", "DMA from $7060 and CPU reads from $783C at frame 163", ), ), ( "9ed55ee94011b76f630841a19c13e18f628510271471a88381d20bed6566e16f", ( "Sentinel (USA) (Proto)", "DMA from $7743 at frame 166, 832,088 DMA reads", ), ), ( "807ee2cc97963f6132e47beef76ca553b62b2c0c2cea69d2365ee0e6f36ee4cc", ("Sentinel (Europe)", "DMA from $7743 at frame 6, 1,411,824 DMA reads"), ), ]; fn lookup_pair<'a>(table: &'a [(&str, (&str, &str))], digest: &str) -> Option<&'a (&'a str, &'a str)> { table.iter().find(|(d, _)| *d == digest).map(|(_, v)| v) } /// The table's row for this exact payload, or None. pub fn lookup(payload: &[u8]) -> Option { let digest = hex(&Sha256::digest(payload)); if let Some(row) = confirmed(&digest) { return Some(row); } let entries = softlist().by_size.get(&payload.len())?; 'entries: for entry in entries { let mut offset = 0; for (size, _crc, sha1) in &entry.roms { if hex(&Sha1::digest(&payload[offset..offset + size])) != *sha1 { continue 'entries; } offset += size; } let mut row = softlist_row(entry); let code = lookup_pair(&BANK6, &digest); let behaviour = lookup_pair(&BANK6_BEHAVIOUR, &digest); if code.is_some() || behaviour.is_some() { let citation = if let Some((_, evidence)) = code { format!( "confirmed from the dump's own bytes (scripts/scan_4000.py): bank 6 at $4000, fixed-bank code calls it ({evidence} instructions of bank 6); otherwise {}", row.citation ) } else { let (_, evidence) = behaviour.expect("a behaviour row"); format!( "confirmed from the dump's behaviour (scripts/uses_4000.py): bank 6 at $4000, read with nothing written there ({evidence}); otherwise {}", row.citation ) }; row = Row { source: "confirmed".to_string(), at_4000: "fixed-bank".to_string(), citation, ..row }; } return Some(row); } None } /// True if `payload` is the High Score Cartridge's ROM as MAME lists it ('hsc'). pub fn hsc_rom_row(payload: &[u8]) -> bool { matches!(lookup(payload), Some(row) if row.hsc) }