# SPDX-License-Identifier: MIT """Build a synthetic mapper-235 CIRAM test ROM; the ROM stays out of the repo. The test writes one byte to each of $2000, $2400, $2800 and $2C00, reads back $2000, and paints the screen: - horizontal: $2000 aliases $2400, so $2000 holds the $2400 byte: green. - vertical: $2000 aliases $2800, so $2000 holds the $2800 byte: red. - one-screen: all four alias, so $2000 holds the $2C00 byte: blue. - anything else: dark grey. The ROM has a mapper-235 header with horizontal mirroring. ``--mapper 0`` builds a 32 KiB NROM control with ``--mirroring h`` or ``v``. ``--header-prg`` sets the header's PRG size (64 = 1 MiB, 192 = 3 MiB); ``--body-prg`` sets the actual body size. A header/body mismatch is a deliberate black-box probe, not a valid ROM. Nothing here is game data. python tools/make_mirror_test.py --header-prg 64 --out /m235.nes python tools/make_mirror_test.py --header-prg 64 --no-latch --out /m235-none.nes python tools/make_mirror_test.py --mapper 0 --header-prg 2 --mirroring v --out /nrom-v.nes """ from __future__ import annotations import argparse import hashlib from pathlib import Path OPS = { ("lda", "#"): (0xA9, 1), ("lda", "a"): (0xAD, 2), ("sta", "a"): (0x8D, 2), ("ldx", "#"): (0xA2, 1), ("ldy", "#"): (0xA0, 1), ("cmp", "#"): (0xC9, 1), ("beq", "r"): (0xF0, 1), ("bne", "r"): (0xD0, 1), ("bpl", "r"): (0x10, 1), ("bmi", "r"): (0x30, 1), ("jmp", "a"): (0x4C, 2), ("bit", "a"): (0x2C, 2), ("dex", "i"): (0xCA, 0), ("dey", "i"): (0x88, 0), ("rti", "i"): (0x40, 0), ("sei", "i"): (0x78, 0), } GREEN, RED, BLUE, GREY = 0x1A, 0x16, 0x12, 0x0F DEFAULT_LATCH = ((0x80E0, 0xC0), (0x80E0, 0x80), (0x8000, 0x02), (0x80E0, 0x1F), (0x80E0, 0x8B)) def assemble(items: list[tuple]) -> bytes: labels: dict[str, int] = {} pc = 0 for item in items: if item[0] == "label": labels[item[1]] = pc continue pc += OPS[(item[0], item[1])][1] + 1 out = bytearray(pc) pc = 0 for item in items: if item[0] == "label": continue opcode, size = OPS[(item[0], item[1])] out[pc] = opcode if size: operand = item[2] if item[1] == "r": delta = labels[operand] - (pc + 2) if not -128 <= delta <= 127: raise ValueError(f"branch out of range: {item}") out[pc + 1] = delta & 0xFF else: value = 0x8000 + labels[operand] if isinstance(operand, str) else operand out[pc + 1] = value & 0xFF if size == 2: out[pc + 2] = value >> 8 pc += size + 1 return bytes(out) def program(latch_writes: list[tuple[int, int]]) -> bytes: code: list[tuple] = [] def emit(*items: tuple) -> None: code.extend(items) def paddr(address: int) -> None: emit( ("lda", "#", address >> 8), ("sta", "a", 0x2006), ("lda", "#", address & 0xFF), ("sta", "a", 0x2006), ) emit( ("sei", "i"), ("lda", "#", 0x40), ("sta", "a", 0x4017), ) for address, value in latch_writes: emit(("lda", "#", value), ("sta", "a", address)) emit( ("lda", "#", 0), ("sta", "a", 0x2000), ("sta", "a", 0x2001), ) emit( ("ldx", "#", 2), ("label", "wait"), ("bit", "a", 0x2002), ("bpl", "r", "wait"), ("label", "wait_clear"), ("bit", "a", 0x2002), ("bmi", "r", "wait_clear"), ("dex", "i"), ("bne", "r", "wait"), ) paddr(0x0000) emit( ("lda", "#", 0), ("ldx", "#", 16), ("label", "chr"), ("sta", "a", 0x2007), ("dex", "i"), ("bne", "r", "chr"), ) for address, value in ((0x2000, 0x11), (0x2400, 0x22), (0x2800, 0x33), (0x2C00, 0x44)): paddr(address) emit(("lda", "#", value), ("sta", "a", 0x2007)) paddr(0x2000) emit( ("lda", "a", 0x2007), ("lda", "a", 0x2007), ("sta", "a", 0x00), ("lda", "a", 0x00), ("cmp", "#", 0x22), ("beq", "r", "horizontal"), ("cmp", "#", 0x33), ("beq", "r", "vertical"), ("lda", "#", BLUE), ("jmp", "a", "set"), ("label", "horizontal"), ("lda", "#", GREEN), ("jmp", "a", "set"), ("label", "vertical"), ("lda", "#", RED), ("jmp", "a", "set"), ("label", "set"), ("sta", "a", 0x00), ) paddr(0x3F00) emit(("lda", "a", 0x00), ("sta", "a", 0x2007)) paddr(0x2000) emit( ("lda", "#", 0), ("ldx", "#", 4), ("ldy", "#", 0), ("label", "fill"), ("sta", "a", 0x2007), ("dey", "i"), ("bne", "r", "fill"), ("dex", "i"), ("bne", "r", "fill"), ) emit( ("lda", "#", 0), ("sta", "a", 0x2005), ("sta", "a", 0x2005), ("sta", "a", 0x2000), ("lda", "#", 0x0A), ("sta", "a", 0x2001), ("label", "loop"), ("jmp", "a", "loop"), ) return assemble(code) def make_rom( header_prg: int, body_prg: int, code: bytes, mapper: int = 235, mirroring: str = "h" ) -> bytes: if mapper == 0: if header_prg != 2 or body_prg != 2: raise ValueError("NROM control must be 32 KiB (2 x 16 KiB)") header = b"NES\x1a" + bytes((2, 0, 1 if mirroring == "v" else 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) else: header = b"NES\x1a" + bytes( (header_prg, 0, 0xB0, 0xE8, 0, 0, 0, 0x07, 0, 0, 0, 0x2A) ) body = bytearray(b"\xff" * (body_prg * 0x4000)) 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)) return header + bytes(body) def main() -> None: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--mapper", type=int, choices=(0, 235), default=235) parser.add_argument("--mirroring", choices=("h", "v"), default="h") parser.add_argument("--header-prg", type=int, default=64) parser.add_argument("--body-prg", type=int) parser.add_argument("--no-latch", action="store_true") parser.add_argument("--latch-write", action="append", default=[]) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() body_prg = args.body_prg if args.body_prg is not None else args.header_prg if args.no_latch: writes = [] elif args.latch_write: writes = [tuple(int(x, 16) for x in item.split("=")) for item in args.latch_write] else: writes = list(DEFAULT_LATCH) image = make_rom( args.header_prg, body_prg, program(writes), args.mapper, args.mirroring ) args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_bytes(image) digest = hashlib.sha256(image).hexdigest() print( f"{args.out}: mapper {args.mapper}, header {args.header_prg} x 16 KiB, " f"body {body_prg} x 16 KiB, mirroring {args.mirroring}, latch writes {len(writes)}" ) print(f"sha256 {digest}") print("expected green=horizontal, red=vertical, blue=one-screen") if __name__ == "__main__": main()