#!/usr/bin/env python3 """Decode the GPU's microcode ROM into one readable row per opcode. The processor has no micro-program: one 27-bit control word per opcode, looked up in a 64-entry ROM (cell RA6032A in INS_EXEC.NET) and latched into the pipeline. The words are in reference/src/JagNetlists/soft/Mcode.rom, written by soft/mcodegen.c (the generator INS_EXEC.NET tells its reader to keep in step: "THESE MUST MATCH THE ROM CONTENTS IN mcodegen.c"). The bit layout below is mcodegen.c's own. The DSP's ROM is NOT in the public netlists (docs/netlists.md). ``--dsp`` applies the six words the MiSTer core substitutes when its JERRY parameter is set (rtl/jaguar_common/ra6032a.v): a reconstruction by that core's authors, HDL tier. python -I scripts/mcode_table.py # GPU python -I scripts/mcode_table.py --dsp # DSP, with MiSTer's six words python -I scripts/mcode_table.py --check # recompute the ROM from mcodegen.c's table Columns: flags = which flags the word loads and where C comes from (ARITH.NET: Z and N always come from the result; C comes from the adder's carry when the result selector is even, from the barrel shifter's carry output when it is odd); wr = how the destination register is written (res: through the result register two ticks later; mov: directly, one tick later; imm: after the two immediate words); rd = whether the destination register is read (the scoreboard protects only registers an instruction reads). """ from __future__ import annotations import argparse import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] NETLISTS = ROOT / "reference" / "src" / "JagNetlists" MISTER_ROM = ROOT / "reference" / "src" / "Jaguar_MiSTer" / "rtl" / "jaguar_common" / "ra6032a.v" RESSEL = ["alu", "shift", "sat", "mult", "mantissa", "normi", "pack", "unpack/mirror"] ALU = ["add", "adc", "sub", "sbc", "and", "or", "xor", "abs"] SRC = {0: "reg", 1: "n 0-31", 2: "n*4", 3: "-n (32-n)", 4: "0", 5: "n -16..15", 6: "-1", 7: "pc", 8: "n 1-32", 9: "1< dict[str, int]: return { "ressel": word & 7, "brm_mlt": word >> 3 & 3, "satsz": (word >> 5 & 1) | (word >> 26 & 1) << 1, "mac": word >> 6 & 1, "alufunc": word >> 7 & 7, "indsel": word >> 10 & 1, "flagld": word >> 11 & 1, "resld": word >> 12 & 1, "srcdat": word >> 13 & 15, "reswr": word >> 17 & 1, "dstrrd": word >> 18 & 1, "memrw": word >> 19 & 1, "memwe": word >> 20 & 1, "precomp": word >> 21 & 1, "immld": word >> 22 & 1, "jumprel": word >> 23 & 1, "jumpabs": word >> 24 & 1, "dstrwr": word >> 25 & 1, } def gpu_rom() -> list[int]: text = (NETLISTS / "soft" / "Mcode.rom").read_text() return [int(line, 16) for line in text.split()] def dsp_rom() -> list[int]: rom = gpu_rom() text = MISTER_ROM.read_text() for number, word in re.findall( r"rom_blk\['h(\w+)\] <=\s*\n.*\n\s*: 32'h(\w+); //dsp", text ): rom[int(number, 16)] = int(word, 16) return rom def recompute() -> list[int]: """The ROM as mcodegen.c's main() packs it, from that file's initialiser table.""" text = (NETLISTS / "soft" / "mcodegen.c").read_text(encoding="latin-1") words = [] for row in re.findall(r"^\{([\d,\s]+)\},\s*/\*", text, re.M): (ressel, brm, satsz, mac, alu, ind, flagld, resld, srcdat, reswr, dstrrd, memrw, memwe, precomp, immld, jrel, jabs, dstrwr) = (int(n) for n in row.split(",")) words.append( ressel | brm << 3 | (satsz & 1) << 5 | mac << 6 | alu << 7 | ind << 10 | flagld << 11 | resld << 12 | srcdat << 13 | reswr << 17 | dstrrd << 18 | memrw << 19 | memwe << 20 | precomp << 21 | immld << 22 | jrel << 23 | jabs << 24 | dstrwr << 25 | (satsz & 2) << 25 ) return words def describe(word: int) -> str: f = fields(word) if f["memrw"]: size = ["byte", "word", "long", "phrase"][f["brm_mlt"]] what = ("store " if f["memwe"] else "load ") + size if f["precomp"]: what += f", address R1{4 + f['indsel']} + {SRC.get(f['srcdat'], '?')}" elif f["jumprel"] or f["jumpabs"]: what = "jump " + ("relative" if f["jumprel"] else "absolute") elif f["immld"]: what = "immediate, two words" elif f["mac"]: what = "multiply (signed), accumulate one tick later" elif f["resld"]: what = RESSEL[f["ressel"]] if f["ressel"] in (0, 4, 6, 2): what += f" {ALU[f['alufunc']]}" if f["ressel"] == 1: what += " " + {0: "logical", 2: "rotate", 3: "arithmetic"}.get(f["brm_mlt"], "?") if f["ressel"] == 3: what += " signed" if f["brm_mlt"] & 2 else " unsigned" if f["ressel"] == 2: what += f" size {f['satsz']}" elif f["dstrwr"]: what = "move" + (" to other bank" if f["ressel"] == 2 else " from other bank" if f["ressel"] == 1 else "") else: what = "-" flags = "-" if f["flagld"]: flags = "ZN C<-" + ("shifter" if f["ressel"] & 1 else "adder") write = "res" if f["reswr"] else "mov" if f["dstrwr"] else "imm" if f["immld"] else "-" return (f"{what:44} src={SRC.get(f['srcdat'], '?'):10} flags={flags:13} " f"wr={write:3} rd={'yes' if f['dstrrd'] else 'no'}") def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--dsp", action="store_true", help="apply the MiSTer core's DSP words") parser.add_argument("--check", action="store_true", help="compare the ROM with mcodegen.c") args = parser.parse_args() raise SystemExit(STOPPED) if args.check: rom, table = gpu_rom(), recompute() wrong = [n for n in range(64) if rom[n] != table[n]] print(f"Mcode.rom has {len(rom)} words; mcodegen.c's table gives {len(table)}; " f"{len(wrong)} differ" + (f": {wrong}" if wrong else "")) return 1 if wrong else 0 rom = dsp_rom() if args.dsp else gpu_rom() for number, word in enumerate(rom): name = DSP_NAMES.get(number, GPU_NAMES[number]) if args.dsp else GPU_NAMES[number] print(f"{number:2} {name:12} {word:08X} {describe(word)}") return 0 if __name__ == "__main__": sys.exit(main())