#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Behaviour check: which SuperGame dumps touch $4000-$7FFF when nothing is mapped there. Companion to ``scan_4000.py`` (which reads the dump's code). Each SuperGame dump whose description puts nothing at $4000 is started from its region's BIOS with $4000-$7FFF left empty and run to ``--frame``; every access the board makes there is counted, split into CPU accesses (opcode and operand fetches, reads, writes) and MARIA's DMA reads (``halt`` true: display lists and graphics), with the frame of the first one and the busiest 1 KiB page. A commercial program has no reason to read an unconnected area (it would see the floating bus), so a dump that does needs something there; what (bank 6, RAM) is then read off the accesses and the dump's bytes. nice -n 19 python scripts/uses_4000.py --frame 400 --jobs 1 [--only NAME] """ from __future__ import annotations import argparse import json import sys from collections import Counter from concurrent.futures import ProcessPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from a7800_python import romsets # noqa: E402 LICENSED = romsets.no_intro(romsets.LICENSED_7800) def candidates(only: str | None) -> list[tuple[str, str]]: from a7800_python.cartridge import formats chosen = [] for path in sorted(LICENSED.glob("*.zip")): if "(Unl)" in path.name or (only and only not in path.name): continue try: desc = formats.load(path) except Exception: # the documented refusal continue if desc.console == "7800" and desc.scheme == "supergame" and desc.at_4000 == "none": chosen.append((str(path), desc.region)) return chosen def run_one(job: tuple[str, str, int]) -> dict: from a7800_python.machine import NTSC, PAL from a7800_python.system import System from a7800_python.television import Television path, region, frame = Path(job[0]), job[1], job[2] system = System.from_rom(path, PAL if region == "pal" else NTSC) board = system.board tv = Television(board.maria, keep=1) access = board.cartridge.access counts: Counter = Counter() pages: Counter = Counter() first: dict = {} def watched(address, data, read, halt, clock): if 0x4000 <= address < 0x8000: kind = "dma" if halt else ("cpu_read" if read else "cpu_write") counts[kind] += 1 pages[address >> 10] += 1 first.setdefault(kind, (tv.count, address)) return access(address, data, read, halt, clock) board.cartridge.access = watched while tv.count < frame: system.step() busiest = pages.most_common(1) return {"name": path.stem, "region": region, "mode": system.mode, **counts, "first": {k: [f, f"${a:04X}"] for k, (f, a) in first.items()}, "busiest_page": f"${busiest[0][0] << 10:04X}" if busiest else None} # fmt: skip def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--frame", type=int, default=400) parser.add_argument("--jobs", type=int, default=1) parser.add_argument("--only") args = parser.parse_args() jobs = [(p, r, args.frame) for p, r in candidates(args.only)] print(f"{len(jobs)} SuperGame dumps with nothing at $4000", flush=True) with ProcessPoolExecutor(max_workers=min(args.jobs, 4)) as pool: for result in pool.map(run_one, jobs): print(json.dumps(result), flush=True) return 0 if __name__ == "__main__": sys.exit(main())