#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Export this fixture's own unpacked DECB placement; no game/host discovery.""" import hashlib import json from pathlib import Path import re import struct import sys ROOT = Path(__file__).resolve().parent SOURCE_EXTENSIONS = {".c", ".h", ".s", ".asm", ".lwasm", ".inc"} SECTION = re.compile(r"^Section: ([^\s]+) \((.+)\) load at ([0-9A-Fa-f]{4}), length ([0-9A-Fa-f]{4})$") def digest(path): return hashlib.sha256((ROOT / path).read_bytes()).hexdigest() def decb(data): segments, used, entry, cursor = [], set(), None, 0 while cursor < len(data): if cursor + 5 > len(data): raise ValueError("Truncated fixture DECB record") kind, length, address = struct.unpack_from(">BHH", data, cursor) cursor += 5 if kind == 255: if length or cursor != len(data): raise ValueError("Invalid fixture DECB trailer") entry = address break if kind or not length or cursor + length > len(data) or address + length > 0x8000: raise ValueError("Fixture must contain ordinary load segments below $8000") span = set(range(address, address + length)) if span & used: raise ValueError("Overlapping fixture DECB segments") used |= span segments.append((address, data[cursor:cursor + length])) cursor += length if entry is None or not segments: raise ValueError("Fixture DECB requires segments and an entry trailer") low = min(address for address, _ in segments) high = max(address + len(payload) for address, payload in segments) image = bytearray(high - low) for address, payload in segments: image[address - low:address - low + len(payload)] = payload return low, high, entry, image def main(): low, high, entry, image = decb((ROOT / "FIXTURE.BIN").read_bytes()) placements = [] for line in (ROOT / "FIXTURE.map").read_text().splitlines(): match = SECTION.fullmatch(line) if match: name, _, address, length = match.groups() address, length = int(address, 16), int(length, 16) if address + length > 0x8000: raise ValueError("Fixture section storage must stay below $8000") placements.append((name, address, length)) elif line.startswith("Section:"): raise ValueError("Unsupported fixture linker-map section") if not placements: raise ValueError("Fixture map has no section placements") sections = sorted({name for name, _, _ in placements}) if "code" not in sections: raise ValueError("Fixture map has no executable code section") raw = "obj/fixture.raw" (ROOT / raw).write_bytes(image) files = sorted(path.relative_to(ROOT).as_posix() for path in ROOT.rglob("*") if path.is_file()) sources = {path: digest(path) for path in files if Path(path).suffix.lower() in SOURCE_EXTENSIONS} artifact_paths = {"FIXTURE.BIN", "FIXTURE.dsk", "FIXTURE.map", raw} artifact_paths.update(path for path in files if Path(path).suffix.lower() in {".o", ".lst"}) artifacts = {path: digest(path) for path in sorted(artifact_paths)} # The fixture uses BASIC task 0's standard low-memory mapping. This is a # declaration for these own sections, not an assumption about other apps. logical_pages = set(range(low // 8192, (high - 1) // 8192 + 1)) for _, address, length in placements: if length: logical_pages.update(range(address // 8192, (address + length - 1) // 8192 + 1)) if any(page >= 4 for page in logical_pages): raise ValueError("Fixture section requires an unsupported MMU mapping") manifest = dict( version=1, entryPoint=entry, binary=dict(path="FIXTURE.BIN", sha256=artifacts["FIXTURE.BIN"]), disk=dict(path="FIXTURE.dsk", sha256=artifacts["FIXTURE.dsk"]), linkMap=dict(path="FIXTURE.map", sha256=artifacts["FIXTURE.map"]), sources=sources, artifacts=artifacts, modules=[dict(id="fixture", sections=sections, codeSections=["code"], image=dict(path=raw, logicalAddress=low), pages=[dict(logicalPage=page, physicalPage=56 + page) for page in sorted(logical_pages)])], framebuffers=[dict(id="page_a", physicalAddress=0x40000, mode="hscreen2"), dict(id="page_b", physicalAddress=0x48000, mode="hscreen2")]) if len(sys.argv) > 1 and sys.argv[1] == "CustomGIME": manifest["framebuffers"] = [dict(id="canvas_a", physicalAddress=0, mode="current_gime", canvasBytes=65536), dict(id="canvas_b", physicalAddress=0x20000, mode="current_gime", canvasBytes=65536)] (ROOT / "fixture.debug.json").write_text(json.dumps(manifest, indent=2) + "\n") if __name__ == "__main__": main()