// SPDX-License-Identifier: MIT //! The board's own scheme detector, for dumps MAME's software list does not carry. //! //! Written from the scheme descriptions (Horton's 2012 mapper document and his //! 1997 `sizes.txt`, DOC) and nothing else: no emulator's detection is read or //! ported. Each test looks for what a program written for the scheme must do, in //! the dump's own bytes; the order goes from the most specific signature to the //! size default. Its verdicts are counted against Stella's `-rominfo` (a closed //! box) by the reference's `scripts/count_bankswitch.py`; the disagreements are //! listed in its docs/claims.md. Every rule here is a heuristic, not hardware //! behaviour. //! //! Rust-forced differences (a2600-python `cartridge/detect.py` at 28fb095): //! `detect` returns `Result` where Python raises `ValueError`. /// Absolute-addressing opcodes that touch a location without indexing: /// LDA/LDX/LDY/BIT/CMP/CPX/CPY/STA/STX/STY/INC/DEC/AND/ORA/EOR/ADC/SBC/NOP abs. /// DOC: the 6502 opcode map (MCS6500 manual); NOP abs is the NMOS undocumented $0C. pub const ABSOLUTE: [u8; 18] = [ 0xAD, 0xAE, 0xAC, 0x2C, 0xCD, 0xEC, 0xCC, 0x8D, 0x8E, 0x8C, 0xEE, 0xCE, 0x2D, 0x0D, 0x4D, 0x6D, 0xED, 0x0C, ]; /// Every 13-bit address an absolute-mode instruction in `image` names (a sliding scan). pub fn absolute_targets(image: &[u8]) -> Vec { (0..image.len().saturating_sub(2)) .filter(|&i| ABSOLUTE.contains(&image[i])) .map(|i| (image[i + 1] as u16 | (image[i + 2] as u16) << 8) & 0x1FFF) .collect() } pub fn count_in(targets: &[u16], low: u16, high: u16) -> usize { targets.iter().filter(|&&t| low <= t && t <= high).count() } /// STA/STX/STY $3F (zero page), Tigervision's bank select (DOC Horton). pub fn stores_to_3f(image: &[u8]) -> usize { (0..image.len().saturating_sub(1)) .filter(|&i| [0x85, 0x86, 0x84].contains(&image[i]) && image[i + 1] == 0x3F) .count() } /// JSRs in `image` whose target's top four address bits are `high_nibble`. pub fn jsr_into(image: &[u8], high_nibble: u8) -> usize { (0..image.len().saturating_sub(2)) .filter(|&i| image[i] == 0x20 && image[i + 2] >> 4 == high_nibble) .count() } /// A scheme name for `image` (`SC` suffix handled by the caller). pub fn detect(image: &[u8]) -> Result<&'static str, String> { let size = image.len(); if size != 0 && size % 8448 == 0 { return Ok("AR"); // 8,448 bytes per tape load (supercharger.rs) } if size == 0x28FF || size == 0x2900 || size == 0x2800 { return Ok("DPC"); // 8K program + 2K graphics (+ padding) } if size == 0x800 { return Ok("2K"); // CommaVid's 2K dumps are told apart only by MAME's list } if size == 0x1000 { return Ok("4K"); } let targets = absolute_targets(image); if size == 0x2000 { let groups: Vec = [0x1FE0, 0x1FE8, 0x1FF0] .iter() .map(|&low| count_in(&targets, low, low + 7)) .collect(); let hotspots = count_in(&targets, 0x1FF8, 0x1FF9); let total: usize = groups.iter().sum(); if groups.iter().all(|&g| g != 0) && total >= 5 && total > 2 * hotspots { return Ok("E0"); // slices chosen in all three windows, more than F8 switches } if groups[0] >= 3 && groups[1] == 0 && groups[2] == 0 { return Ok("E7"); // an 8K M-Network board: only $1FE0-$1FE7 touched } if stores_to_3f(image) >= 3 { return Ok("3F"); } if count_in(&targets, 0x0220, 0x0220) != 0 && count_in(&targets, 0x0240, 0x0240) != 0 { return Ok("UA"); } // FE: the banks are assembled at $Fxxx and $Dxxx (DOC Horton), so the // second bank's reset vector points into $C000-$DFFF, and the first // bank JSRs into the second ("All the subroutines are in one bank"). if image[0x1FFD] >> 5 == 6 && jsr_into(&image[..0x1000], 0xD) >= 3 { return Ok("FE"); } return Ok("F8"); } if size == 0x3000 { if count_in(&targets, 0x1FE0, 0x1FE7) >= 2 { return Ok("E7"); } return Ok("FA"); } if size == 0x4000 { if count_in(&targets, 0x1FE0, 0x1FE7) >= 2 && count_in(&targets, 0x1FF6, 0x1FF9) == 0 { return Ok("E7"); } return Ok("F6"); } if size == 0x8000 { return Ok("F4"); } if size == 0x10000 { if count_in(&targets, 0x1FF0, 0x1FF0) >= 4 { return Ok("F0"); } return Ok("32IN1"); } Err(format!("no scheme known for a {size}-byte image")) }