#!/usr/bin/env python3 """Decode the CPS-B-21 configuration keys and compare them with MAME's table. Three pinned sources meet here (all under the git-ignored ``reference/``): - Eduardo Cruz's desuicide sketch (``arcadehacker-cps1``): one 18-byte key per battery game, shifted into real chips MSB first, byte 0 first. - Loic Petit's trace of the DL-0921 die (``cps2-reverse``, ``DLs/DL-0921/doc/configuration-key.md``): what each of the 144 bits is. - MAME's ``cps1_v.cpp``: the ``CPS_B_21_*`` rows of the variants table. The script prints, per key, the fields the die says the key holds and the MAME row of the same name, and marks every field where they differ. It decides nothing: docs/cps-b.md reads the result. python -I scripts/decode_cpsb21_keys.py """ from __future__ import annotations import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SKETCH = ROOT / "reference/src/arcadehacker-cps1/ArcadeHacker_CPS1.ino" VIDEO = ROOT / "reference/src/mame/src/mame/capcom/cps1_v.cpp" # (first bit, width, name, kind) from configuration-key.md. Bit 0 is the first # bit shifted in. "addr": a register offset stored as five bits, bit 0 of the # offset dropped. "sel": a three-bit select of the layer-control bit that # enables one layer. "data": a value, most significant bit first. FIELDS = [ (0, 6, "id_low", "data"), (6, 5, "mult_x", "addr"), (11, 3, "layer_d2", "sel"), (14, 5, "id", "addr"), (19, 5, "mult_y", "addr"), (24, 5, "palette_control", "addr"), (29, 5, "mult_lsb", "addr"), (34, 5, "priority3", "addr"), (39, 3, "layer_d3", "sel"), (42, 5, "mult_msb", "addr"), (47, 5, "priority2", "addr"), (52, 5, "mult_tc", "addr"), (57, 5, "priority1", "addr"), (62, 16, "check2_data", "data"), (78, 3, "layer_d5", "sel"), (81, 5, "check1", "addr"), (86, 5, "priority0", "addr"), (91, 16, "check1_data", "data"), (107, 5, "check2", "addr"), (112, 3, "layer_d4", "sel"), (115, 5, "raster1", "addr"), (120, 5, "layer_control", "addr"), (125, 5, "raster2", "addr"), (130, 5, "raster3", "addr"), (135, 3, "layer_d1", "sel"), (138, 6, "id_high", "data"), ] # MAME row columns after the macro name: # id addr, id value, mult factor1, factor2, result lo, result hi, unknown1, # unknown2, unknown3, layer control, priority[4], palette control, enable[5]. MAME_COLUMNS = [ "id", "id_value", "mult_x", "mult_y", "mult_lsb", "mult_msb", "unknown1", "unknown2", "unknown3", "layer_control", "priority0", "priority1", "priority2", "priority3", "palette_control", "en_scroll1", "en_scroll2", "en_scroll3", "en_stars1", "en_stars2", ] # fmt: skip def keys() -> list[tuple[str, str, list[int]]]: found = [] for line in SKETCH.read_text(encoding="utf-8", errors="replace").splitlines(): match = re.match(r"\{([^}]*)\},?\s*//\s*(.*?)\s+(CPS_B_21_\w+)\s*$", line) if not match: continue data = [int(token, 16) for token in match.group(1).split(",")] bits = [(byte >> (7 - n)) & 1 for byte in data for n in range(8)] found.append((match.group(2), match.group(3), bits)) return found def decode(bits: list[int]) -> dict[str, int]: out = {} for first, width, name, kind in FIELDS: value = 0 for bit in bits[first : first + width]: value = (value << 1) | bit out[name] = value << 1 if kind == "addr" else value out["id_value"] = (out.pop("id_high") << 10) | out.pop("id_low") return out def mame_rows() -> dict[str, dict[str, int]]: rows = {} text = VIDEO.read_text(encoding="utf-8", errors="replace") for match in re.finditer(r"^#define (CPS_B_21_\w+)\s+(.*)$", text, re.MULTILINE): body = match.group(2).split("//")[0] numbers = [int(token, 0) for token in re.findall(r"-?0x[0-9a-fA-F]+|-?\d+", body)] rows[match.group(1)] = dict(zip(MAME_COLUMNS, numbers, strict=True)) return rows def main() -> int: if not SKETCH.is_file(): # The sketch has no licence, and since 2026-11-24 no unlicensed or closed-box # source is fetched (Aubrey's rule): ArcadeHacker/ArcadeHacker_CPS1 is cited by URL # and commit in docs/validation.md. Supply the file by hand to rerun this survey. print(f"missing {SKETCH.relative_to(ROOT)}: not fetched since 2026-11-24", file=sys.stderr) return 2 mame = mame_rows() for game, row_name, bits in keys(): key = decode(bits) row = mame[row_name] print(f"{row_name} ({game})") for name in ("id", "id_value", "mult_x", "mult_y", "mult_lsb", "mult_msb", "layer_control", "priority0", "priority1", "priority2", "priority3", "palette_control"): # fmt: skip theirs = row[name] note = "" if theirs == key[name] else " (MAME: not modelled)" if theirs == -1 else " DIFFERS" shown = "-1" if theirs == -1 else f"{theirs:#06x}" print(f" {name:<16} key {key[name]:#06x} MAME {shown}{note}") selects = [key[f"layer_d{n}"] for n in range(1, 6)] masks = [row[n] for n in ("en_scroll1", "en_scroll2", "en_scroll3", "en_stars1", "en_stars2")] print(f" layer selects d1..d5 {selects} -> bits {[f'{1 << s:#04x}' for s in selects]}") print(f" MAME enable masks {[f'{m:#04x}' for m in masks]}") extra = ("mult_tc", "check1", "check1_data", "check2", "check2_data", "raster1", "raster2", "raster3") print(" not in MAME's row: " + ", ".join(f"{n} {key[n]:#x}" for n in extra)) print(f" MAME unknown1..3 {[row[n] for n in ('unknown1', 'unknown2', 'unknown3')]}") print() return 0 if __name__ == "__main__": sys.exit(main())