// 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. //! //! A transcription of a5200-python's `cartridge/walk.py` (at cf82d44). Rust-forced //! differences: the `LENGTH` table is built by a function from the same three opcode lists; //! `seen` is a `HashSet`; `kind` is a `&str` ("one" / "two"); `guess_16k` returns //! `(Option<&str>, String)`. use std::collections::HashSet; const LEN1: &str = "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"; const LEN3: &str = concat!( "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" ); const LEN2: &str = concat!( "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" ); fn length_table() -> [usize; 256] { let mut length = [0; 256]; for (text, n) in [(LEN1, 1), (LEN2, 2), (LEN3, 3)] { for op in text.split_whitespace() { length[usize::from_str_radix(op, 16).unwrap()] = n; } } length } const BRANCHES: [u8; 8] = [0x10, 0x30, 0x50, 0x70, 0x90, 0xB0, 0xD0, 0xF0]; const STOPS: [u8; 5] = [0x00, 0x40, 0x60, 0x4C, 0x6C]; // BRK, RTI, RTS, JMP, JMP () fn _offset(address: usize, kind: &str) -> usize { if kind == "two" { return ((address - 0x4000) & 0x1FFF) + if address >= 0x8000 { 0x2000 } else { 0 }; } address & 0x3FFF } /// Instructions reached from the start vector under reading `kind` ("one"/"two"). pub fn walk(data: &[u8], kind: &str, limit: usize) -> usize { let length_of = length_table(); let start = data[_offset(0xBFFE, kind)] as usize | (data[_offset(0xBFFF, kind)] as usize) << 8; let mut todo = vec![start]; let mut seen: HashSet = HashSet::new(); while !todo.is_empty() && seen.len() < limit { let mut pc = todo.pop().unwrap(); while !seen.contains(&pc) && seen.len() < limit { if pc >= 0xC000 || pc < 0x4000 || pc + 2 >= 0xC000 { break; } let opcode = data[_offset(pc, kind)]; let length = length_of[opcode as usize]; if length == 0 { break; } seen.insert(pc); let low = data[_offset(pc + 1, kind)] as i64; let high = data[_offset(pc + 2, kind)] as usize; if BRANCHES.contains(&opcode) { let rel = if low & 0x80 != 0 { low - 256 } else { low }; todo.push(((pc as i64 + 2 + rel) & 0xFFFF) as usize); } else if opcode == 0x20 || opcode == 0x4C { todo.push(low as usize | high << 8); } if STOPS.contains(&opcode) { break; } pc += length; } } seen.len() } /// ("16k-one" | "16k-two" | None for "either", detail) for a 16K payload. pub fn guess_16k(data: &[u8]) -> (Option<&'static str>, String) { if data[..0x2000] == data[0x2000..] { return ( Some("16k-two"), "halves identical: either reading gives the same window".to_string(), ); } let (one, two) = (walk(data, "one", 20000), walk(data, "two", 20000)); let detail = format!("walk: one-chip reaches {one} instructions, two-chip {two}"); let (low, high) = if one <= two { (one, two) } else { (two, one) }; if high < 4 * low.max(1) || high < 200 { return (None, detail); } (Some(if one > two { "16k-one" } else { "16k-two" }), detail) }