# SPDX-License-Identifier: MIT """Which NEC CPUs run encrypted code: the first byte each fetches after reset (FFFF0h). MAME_ROMPATH=... python -I scripts/reset_bytes.py # every NEC set (~2 min) MAME_ROMPATH=... python -I scripts/reset_bytes.py gunforce # named sets An unencrypted V30/V33/V35 program starts with DI (FAh) or a far jump (EAh) at FFFF0h. A V35 Software Guard (NEC uPD70337) translates the first byte of every opcode through a factory table (DOC: VDB91, uPD70337 "Security Mode Operation"), so any other byte marks a translated CPU, and sets whose first byte differs use different tables (the first plain instruction being one of the two). MAME 0.285 is run only to map the ROMs into the CPU's address space (scripts/mame_reset_bytes.lua reads raw data, which no translation table touches); one process at a time. Output: reference/mame/reset_bytes.txt. """ from __future__ import annotations import collections import os import subprocess import sys import xml.etree.ElementTree as ET from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "reference" / "mame" / "reset_bytes.txt" def nec_sets() -> list[str]: root = ET.parse(ROOT / "reference" / "mame" / "listxml.xml").getroot() # census.py makes it return [m.get("name") for m in root.findall("machine") if any((c.get("name") or "").startswith("NEC") for c in m.findall("chip"))] # fmt: skip def main() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (non-merged MAME sets)", file=sys.stderr) return 2 sets = sys.argv[1:] or nec_sets() home = ROOT / "reference" / "mame" / "rb" home.mkdir(parents=True, exist_ok=True) OUT.write_text("", encoding="utf-8") for name in sets: subprocess.run( ["nice", "taskset", "-c", "24-31", "mame", name, "-rompath", rompath, "-homepath", str(home), "-cfg_directory", str(home / "cfg"), "-nvram_directory", str(home / "nvram"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-seconds_to_run", "1", "-autoboot_script", str(ROOT / "scripts" / "mame_reset_bytes.lua")], env=dict(os.environ, IREM_RESET_OUT=str(OUT)), capture_output=True, timeout=120, check=False, ) # fmt: skip groups: dict[str, list[str]] = collections.defaultdict(list) for line in OUT.read_text(encoding="utf-8").splitlines(): name, tag, cpu, first = line.split()[:4] groups[f"{cpu} {first}"].append(f"{name}{tag}") for key in sorted(groups): plain = "" if key.split()[1] in ("FA", "EA") else " <- translated" print(f"{key}{plain}: {' '.join(groups[key])}") return 0 if __name__ == "__main__": sys.exit(main())