# SPDX-License-Identifier: MIT """The static walk for an unknown 16K dump: one chip or two? (open decision 3). A raw 16K dump cannot say whether it was one 16K ROM or two 8K ROMs; the two readings differ at $6000-$9FFF (docs/cartridge.md "The 16K problem"). The walk follows the program from the start vector under both readings, through documented opcodes, branches, JSR and JMP; the wrong reading soon lands in data or in the other chip's code and stops, the right one keeps going. Reach decides. Measured on the 60 16K images whose class MAME's CC0 software list states: agrees on 48, abstains on 12, contradicts none (scripts/count_carts.py, which holds the same walk for the census). This is this repository's own code; no emulator's detection code is used. Evidence tags: the opcode lengths are DOC (the NMOS 6502's documented instruction set). The decision rule (the right reading reaches at least four times as many instructions and at least 200; the 20,000-instruction limit) is FITTED: chosen against the 60 16K images MAME's CC0 list classifies (48 agree, 12 abstain, 0 contradict); a fault-count rule was tried first and was wrong on 9 of 43. Against the 39 observed boots it was wrong once. The walk only makes a labelled guess; it never decides a table row. """ from __future__ import annotations _LEN1 = "00 08 0a 18 28 2a 38 40 48 4a 58 60 68 6a 78 88 8a 98 9a a8 aa b8 ba c8 ca d8 e8 ea f8" _LEN3 = ( "0d 0e 19 1d 1e 20 2c 2d 2e 39 3d 3e 4c 4d 4e 59 5d 5e 6c 6d 6e 79 7d 7e 8c 8d 8e" " 99 9d ac ad ae b9 bc bd be cc cd ce d9 dd de ec ed ee f9 fd fe" ) _LEN2 = ( "01 05 06 09 10 11 15 16 21 24 25 26 29 30 31 35 36 41 45 46 49 50 51 55 56 61 65 66" " 69 70 71 75 76 81 84 85 86 90 91 94 95 96 a0 a1 a2 a4 a5 a6 a9 b0 b1 b4 b5 b6 c0 c1" " c4 c5 c6 c9 d0 d1 d5 d6 e0 e1 e4 e5 e6 e9 f0 f1 f5 f6" ) LENGTH = [0] * 256 for _text, _n in ((_LEN1, 1), (_LEN2, 2), (_LEN3, 3)): for _op in _text.split(): LENGTH[int(_op, 16)] = _n BRANCHES = frozenset({0x10, 0x30, 0x50, 0x70, 0x90, 0xB0, 0xD0, 0xF0}) STOPS = frozenset({0x00, 0x40, 0x60, 0x4C, 0x6C}) # BRK, RTI, RTS, JMP, JMP () def _offset(address: int, kind: str) -> int: if kind == "two": return ((address - 0x4000) & 0x1FFF) + (0x2000 if address >= 0x8000 else 0) return address & 0x3FFF def walk(data: bytes, kind: str, limit: int = 20000) -> int: """Instructions reached from the start vector under reading ``kind`` ("one"/"two").""" start = data[_offset(0xBFFE, kind)] | data[_offset(0xBFFF, kind)] << 8 todo = [start] seen: set[int] = set() while todo and len(seen) < limit: pc = todo.pop() while pc not in seen and len(seen) < limit: if pc >= 0xC000 or pc < 0x4000 or pc + 2 >= 0xC000: break opcode = data[_offset(pc, kind)] length = LENGTH[opcode] if not length: break seen.add(pc) low = data[_offset(pc + 1, kind)] high = data[_offset(pc + 2, kind)] if opcode in BRANCHES: todo.append((pc + 2 + (low - 256 if low & 0x80 else low)) & 0xFFFF) elif opcode in (0x20, 0x4C): todo.append(low | high << 8) if opcode in STOPS: break pc += length return len(seen) def guess_16k(data: bytes) -> tuple[str | None, str]: """("16k-one" | "16k-two" | None for "either", detail) for a 16K payload.""" if data[:0x2000] == data[0x2000:]: return "16k-two", "halves identical: either reading gives the same window" one, two = walk(data, "one"), walk(data, "two") detail = f"walk: one-chip reaches {one} instructions, two-chip {two}" low, high = sorted((one, two)) if high < 4 * max(low, 1) or high < 200: return None, detail return ("16k-one" if one > two else "16k-two"), detail