# SPDX-License-Identifier: MIT """The three program image formats (handoff-brief decision 7; start-here.md "Program and data ROM images"; fullsnes "ROM-Images"). - **PROM layout** (fullsnes' "Oldest Files"; MAME's device sets): each instruction word is three big-endian bytes and one dummy byte, then the data ROM as big-endian 16-bit words. This is the uPD77P25's own PROM map (DS p.27): 10,240 bytes for the uPD7725 (data at 2000h), 69,632 for the uPD96050 (data at 10000h). - **raw big-endian** ("Old Files"): three bytes per word, then the data ROM, big-endian: 8,192 or 53,248 bytes. - **little-endian** ("Newer Files"; ares ``load.cpp`` reads it): the same sizes little-endian, or a ``.program.rom`` + ``.data.rom`` pair. Every original program has ``JRQM $`` (97C00xh) in its first four words (fullsnes), which tells the byte order of the 3-byte formats; without it fullsnes says "best default to Newer". Nothing is fetched or bundled: images are the user's (BYOR). """ from __future__ import annotations import zipfile from pathlib import Path from upd7725_python.model import UPD7725_MODEL, UPD96050_MODEL, Model Source = bytes | bytearray | str | Path # Image sizes by (program words, data words). _SHAPES = ( (UPD7725_MODEL.program_words, UPD7725_MODEL.data_rom_words), (UPD96050_MODEL.program_words, UPD96050_MODEL.data_rom_words), ) def _read(source: Source) -> bytes: if isinstance(source, bytes | bytearray): return bytes(source) path = Path(source) if path.suffix.lower() == ".zip": # a MAME device set: its one .bin member with zipfile.ZipFile(path) as archive: members = [n for n in archive.namelist() if not n.endswith("/")] if len(members) != 1: raise ValueError(f"{path} holds {len(members)} files; expected one image") return archive.read(members[0]) return path.read_bytes() def has_jrqm_signature(program: list[int]) -> bool: """fullsnes' rule: ``JRQM $`` (97C000h + 4 x address) in one of the first four words.""" return any(program[i] == 0x97C000 | i << 2 for i in range(min(4, len(program)))) def _three_byte(data: bytes, words: int, order: str) -> list[int]: return [int.from_bytes(data[3 * i : 3 * i + 3], order) for i in range(words)] def _sixteen(data: bytes, order: str) -> list[int]: return [int.from_bytes(data[i : i + 2], order) for i in range(0, len(data) - 1, 2)] def load_image(source: Source, data: Source | None = None) -> tuple[list[int], list[int]]: """Read a program image as ``(program words, data ROM words)``. ``source`` is bytes, a file, or a MAME device-set ``.zip``; with ``data`` it is a ``.program.rom`` (little-endian 3-byte words) and ``data`` the ``.data.rom``. """ image = _read(source) if data is not None: if len(image) % 3: raise ValueError("a .program.rom holds 3 bytes per word") program = _three_byte(image, len(image) // 3, "little") if not has_jrqm_signature(program): big = _three_byte(image, len(image) // 3, "big") if has_jrqm_signature(big): program = big return program, _sixteen(_read(data), "little") for program_words, data_words in _SHAPES: if len(image) == 4 * program_words + 2 * data_words: # PROM layout program = [ int.from_bytes(image[4 * i : 4 * i + 3], "big") for i in range(program_words) ] return program, _sixteen(image[4 * program_words :], "big") if len(image) == 3 * program_words + 2 * data_words: split = 3 * program_words big = _three_byte(image, program_words, "big") if has_jrqm_signature(big): return big, _sixteen(image[split:], "big") return _three_byte(image, program_words, "little"), _sixteen(image[split:], "little") raise ValueError(f"no known DSP image is {len(image)} bytes long") def model_for(program: list[int]) -> Model: """The chip an image needs: more than 2,048 program words means the uPD96050.""" return UPD96050_MODEL if len(program) > UPD7725_MODEL.program_words else UPD7725_MODEL __all__ = ["has_jrqm_signature", "load_image", "model_for"]