# SPDX-License-Identifier: MIT """Build a mapper-235 R/P bank-marker test; the ROM stays out of the repo.""" from __future__ import annotations import argparse import hashlib from pathlib import Path from make_mirror_test import assemble TARGETS = {"r0p0": 0x8000, "r0p1": 0x9000, "r1p0": 0x8800, "r1p1": 0x9800} def routine(target: int) -> bytes: return bytes((0xA9, 0x00, 0x8D, target & 0xFF, target >> 8, 0x4C, 0x00, 0x02)) def program(target: int) -> bytes: items: list[tuple] = [ ("sei", "i"), ("lda", "#", 0x40), ("sta", "a", 0x4017), ] for index, value in enumerate(routine(target)): items.extend( ( ("lda", "#", value), ("sta", "a", 0x0200 + index), ) ) items.append(("jmp", "a", 0x0200)) return assemble(items) def make_rom(target: int) -> bytes: prg = 64 body = bytearray(b"\xFF" * (prg * 0x4000)) for bank in range(prg): offset = bank * 0x4000 + 0x100 body[offset : offset + 16] = bytes( (bank & 0xFF, 0xA5, 0x5A, (bank >> 8) & 0xFF) ) + bytes(12) code = program(target) if len(code) > 0x7FFA: raise ValueError("test code overlaps the vectors") body[: len(code)] = code body[0x7FFA:0x8000] = bytes((0x00, 0x80, 0x00, 0x80, 0x00, 0x80)) header = b"NES\x1a" + bytes((prg, 0, 0xB0, 0xE8, 0, 0, 0, 0x07, 0, 0, 0, 0x2A)) return header + bytes(body) def main() -> None: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--target", choices=sorted(TARGETS), required=True) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() image = make_rom(TARGETS[args.target]) args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_bytes(image) print(f"{args.out}: target {args.target} ${TARGETS[args.target]:04X}") print(f"sha256 {hashlib.sha256(image).hexdigest()}") print("markers: $8100 lower 16K bank, $C100 upper 16K bank") if __name__ == "__main__": main()