# 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 ``scripts/count_bankswitch.py``; the disagreements are listed in docs/claims.md. Every rule here is a heuristic, not hardware behaviour. """ from __future__ import annotations # 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. ABSOLUTE = frozenset( (0xAD, 0xAE, 0xAC, 0x2C, 0xCD, 0xEC, 0xCC, 0x8D, 0x8E, 0x8C, 0xEE, 0xCE, 0x2D, 0x0D, 0x4D, 0x6D, 0xED, 0x0C) ) # fmt: skip def absolute_targets(image: bytes) -> list[int]: """Every 13-bit address an absolute-mode instruction in ``image`` names (a sliding scan).""" return [ (image[i + 1] | image[i + 2] << 8) & 0x1FFF for i in range(len(image) - 2) if image[i] in ABSOLUTE ] def count_in(targets: list[int], low: int, high: int) -> int: return sum(low <= t <= high for t in targets) def stores_to_3f(image: bytes) -> int: """STA/STX/STY $3F (zero page), Tigervision's bank select (DOC Horton).""" return sum( image[i] in (0x85, 0x86, 0x84) and image[i + 1] == 0x3F for i in range(len(image) - 1) ) def jsr_into(image: bytes, high_nibble: int) -> int: """JSRs in ``image`` whose target's top four address bits are ``high_nibble``.""" return sum(image[i] == 0x20 and image[i + 2] >> 4 == high_nibble for i in range(len(image) - 2)) def detect(image: bytes) -> str: """A scheme name for ``image`` (``SC`` suffix handled by the caller).""" size = len(image) if size and size % 8448 == 0: return "AR" # 8,448 bytes per tape load (supercharger.py) if size in (0x28FF, 0x2900, 0x2800): return "DPC" # 8K program + 2K graphics (+ padding) if size == 0x800: return "2K" # CommaVid's 2K dumps are told apart only by MAME's list if size == 0x1000: return "4K" targets = absolute_targets(image) if size == 0x2000: groups = [count_in(targets, low, low + 7) for low in (0x1FE0, 0x1FE8, 0x1FF0)] hotspots = count_in(targets, 0x1FF8, 0x1FF9) if all(groups) and sum(groups) >= 5 and sum(groups) > 2 * hotspots: return "E0" # slices chosen in all three windows, more than F8 switches if groups[0] >= 3 and not groups[1] and not groups[2]: return "E7" # an 8K M-Network board: only $1FE0-$1FE7 touched if stores_to_3f(image) >= 3: return "3F" if count_in(targets, 0x0220, 0x0220) and count_in(targets, 0x0240, 0x0240): return "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 and jsr_into(image[:0x1000], 0xD) >= 3: return "FE" return "F8" if size == 0x3000: if count_in(targets, 0x1FE0, 0x1FE7) >= 2: return "E7" return "FA" if size == 0x4000: if count_in(targets, 0x1FE0, 0x1FE7) >= 2 and not count_in(targets, 0x1FF6, 0x1FF9): return "E7" return "F6" if size == 0x8000: return "F4" if size == 0x10000: if count_in(targets, 0x1FF0, 0x1FF0) >= 4: return "F0" return "32IN1" raise ValueError(f"no scheme known for a {size}-byte image")