#!/usr/bin/env python3 """Lay three opcode tables side by side, GPU and DSP, and report where they differ. Sources (all under reference/, fetched by scripts/fetch_references.py): - manual Atari's Technical Reference Manual rev. 8, the GPU and DSP "Instruction Set" sections (pdftotext of the pinned PDF) - jrisc cubanismo/jrisc_tools jrisc_optable.h (CC0-1.0) - mame gpu_op_table / dsp_op_table in MAME 0.285's jaguar.cpp (BSD-3-Clause) Until 2026-11-25 the table had four more columns, each parsed from source that is not MIT-compatible: JagNetlists' microcode generator (no licence), the MiSTer core's RTL (GPL-2.0, empty since 2026-11-24), rmac's encoder (no licence in the fork) and vasm's jagrisc table (non-commercial licence). A program parsing source is reading it (decision L4, docs/provenance.md), so those readers are gone. Re-adding the assemblers is owed as a closed-box run: assemble every opcode with a built rmac or vasm and decode the output words. python -I scripts/opcode_tables.py # the grid, then the differences python -I scripts/opcode_tables.py --quiet # the differences only Names are normalised (lower case, the addressing-mode variants of load/store folded to load14n, store15r and so on) so that only real disagreements print. """ from __future__ import annotations import argparse import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "reference" / "src" MANUAL = SRC / "Jaguar_MiSTer" / "resources" / "Jaguar Programming Manual.PDF" Table = dict[int, set[str]] def fold(name: str, operands: str = "") -> str: """One spelling for each instruction, whatever the source calls it.""" name = name.lower() ops = operands.lower().replace(" ", "") if name in ("load", "store"): for base in ("14", "15"): if f"r{base}+n" in ops: return f"{name}{base}n" if f"r{base}+rn" in ops: return f"{name}{base}r" if name == "move" and "pc" in ops: return "movepc" return name def manual_tables() -> tuple[Table, Table]: text = subprocess.run( ["pdftotext", "-layout", str(MANUAL), "-"], capture_output=True, text=True, check=True ).stdout # The GPU list runs from the first "Instruction Set" heading after the # contents pages to "Internal Registers"; the DSP list from the second to # "DSP Flags Register". starts = [m.start() for m in re.finditer(r"^Instruction Set\s*$", text, re.M)] gpu_text = text[starts[0] : text.index("Internal Registers", starts[0])] dsp_text = text[starts[1] : text.index("DSP Flags Register", starts[1])] row = re.compile(r"^\s{0,6}(\d{1,2})\s+([A-Z][A-Z0-9]+)\s*(\S*(?:\s\S+)?)?\s{2,}", re.M) def read(block: str) -> Table: table: Table = {} for number, name, operands in row.findall(block): table.setdefault(int(number), set()).add(fold(name, operands)) return table return read(gpu_text), read(dsp_text) def jrisc_tables() -> tuple[Table, Table]: text = (SRC / "jrisc_tools" / "jrisc_optable.h").read_text() gpu: Table = {} dsp: Table = {} rename = {"loadr14n": "load14n", "loadr15n": "load15n", "loadr14r": "load14r", "loadr15r": "load15r", "storer14n": "store14n", "storer15n": "store15n", "storer14r": "store14r", "storer15r": "store15r"} for name, number, who in re.findall( r"^JRISC_OP\((\w+),\s*(\d+),[^)]*?(JRISC_\w+)\)", text, re.M ): name = rename.get(name, name) if who != "JRISC_dsp": gpu.setdefault(int(number), set()).add(name) if who != "JRISC_gpu": dsp.setdefault(int(number), set()).add(name) return gpu, dsp def mame_tables() -> tuple[Table, Table]: text = (SRC / "mame" / "src" / "devices" / "cpu" / "jaguar" / "jaguar.cpp").read_text() rename = {"load_r14n": "load14n", "load_r15n": "load15n", "load_r14rn": "load14r", "load_r15rn": "load15r", "store_rn_r14n": "store14n", "store_rn_r15n": "store15n", "store_rn_r14rn": "store14r", "store_rn_r15rn": "store15r", "move_pc": "movepc"} def read(which: str) -> Table: block = text[text.index(f"{which}[64] =") :] block = block[: block.index("};")] table: Table = {} for number, handler in enumerate(re.findall(r"jaguar_cpu_device::(\w+)", block)): base = handler for long in rename: if handler.startswith(long): base = long if base not in rename: base = re.sub(r"(_rn|_n|_cc)+$", "", handler) table[number] = {rename.get(base, base)} return table gpu = read("gpu_op_table") gpu[63].add("unpack") # pack_rn handles both, by the reg1 field return gpu, read("dsp_op_table") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--quiet", action="store_true", help="print the differences only") args = parser.parse_args() manual_gpu, manual_dsp = manual_tables() jrisc_gpu, jrisc_dsp = jrisc_tables() mame_gpu, mame_dsp = mame_tables() columns_gpu = {"manual": manual_gpu, "jrisc": jrisc_gpu, "mame": mame_gpu} columns_dsp = {"manual": manual_dsp, "jrisc": jrisc_dsp, "mame": mame_dsp} differences = 0 for label, columns in (("GPU", columns_gpu), ("DSP", columns_dsp)): if not args.quiet: print(f"{label:3} " + " ".join(f"{name:16}" for name in columns)) for number in range(64): cells = [columns[name].get(number, set()) for name in columns] if not args.quiet: print(f"{number:3} " + " ".join(f"{'/'.join(sorted(c)) or '-':16}" for c in cells)) # A source with no entry for a slot is left out of the comparison # unless every other source is silent too. keys = {frozenset(c) for c in cells if c} if len(keys) > 1: differences += 1 shown = ", ".join( f"{name}={'/'.join(sorted(c)) or '-'}" for name, c in zip(columns, cells) ) print(f" differ: {label} {number}: {shown}") print(f"{differences} opcode slot(s) where the sources differ") return 0 if __name__ == "__main__": sys.exit(main())