#!/usr/bin/env python3 """Print the equations of a GAL16V8 fuse map as MAME's ROM sets store it. The B-board, A-board and C-board PALs in the MAME CPS-1 sets are 279-byte files: a 4-byte big-endian fuse count (0x892 = 2194, the GAL16V8) followed by the fuses packed eight to a byte, fuse 0 in bit 0 (MAME's "jedbin" layout). This script reads one such file (or a member of a ROM zip) and prints, per output pin, the product terms in the pin names of the GAL16V8 datasheet. It decides nothing about what a pin is wired to on the board: docs/gfx-mapping.md does that with the schematics and MAME's comments. python -I scripts/decode_gal16v8.py SET.zip MEMBER python -I scripts/decode_gal16v8.py FILE Fuse layout (Lattice GAL16V8 datasheet): 0-2047 AND array, 64 rows of 32 columns, rows 0-7 feed pin 19 ... rows 56-63 feed pin 12; 2048-2055 XOR (polarity), one per output, pin 19 first; 2056-2119 signature; 2120-2127 AC1, pin 19 first; 2128-2191 product-term disable; 2192 SYN; 2193 AC0. A fuse value of 0 connects the column to the product term. """ from __future__ import annotations import sys import zipfile from pathlib import Path # Column pair -> pin, for the three GAL16V8 modes. Each pin owns two # adjacent columns: true then complement. In simple mode pins 15 and 16 have # no column and pins 19, 18, 17, 14, 13, 12 feed back. Checked here: PRG1 # decodes to the equations MAME quotes, and S9263B's pins 14/15 take pin 13 # as MAME's comment says (docs/gfx-mapping.md). SIMPLE = [2, 1, 3, 19, 4, 18, 5, 17, 6, 14, 7, 13, 8, 12, 9, 11] COMPLEX = [2, 1, 3, 18, 4, 17, 5, 16, 6, 15, 7, 14, 8, 13, 9, 11] REGISTERED = [2, 19, 3, 18, 4, 17, 5, 16, 6, 15, 7, 14, 8, 13, 9, 12] OUTPUTS = [19, 18, 17, 16, 15, 14, 13, 12] def load(args: list[str]) -> bytes: if len(args) == 2: with zipfile.ZipFile(args[0]) as z: return z.read(args[1]) return Path(args[0]).read_bytes() def fuses(data: bytes) -> list[int]: count = int.from_bytes(data[:4], "big") if count != 2194: raise SystemExit(f"fuse count {count}: not a GAL16V8 image") body = data[4:] return [(body[i // 8] >> (i % 8)) & 1 for i in range(count)] def main(argv: list[str]) -> int: if len(argv) not in (1, 2): print(__doc__) return 2 f = fuses(load(argv)) syn, ac0 = f[2192], f[2193] mode = {(1, 0): "simple", (1, 1): "complex", (0, 1): "registered"}.get((syn, ac0), "?") cols = {"simple": SIMPLE, "complex": COMPLEX, "registered": REGISTERED}.get(mode, COMPLEX) print(f"mode {mode} (SYN={syn} AC0={ac0})") for k, pin in enumerate(OUTPUTS): xor = f[2048 + k] ac1 = f[2120 + k] rows = range(k * 8, k * 8 + 8) terms = [] for r in rows: if not f[2128 + r]: continue # product term disabled row = f[r * 32 : r * 32 + 32] if all(v == 0 for v in row): terms.append("0") # every column connected: always false continue lits = [] for c in range(16): t, n = row[2 * c], row[2 * c + 1] if t == 0 and n == 0: lits = ["0"] break if t == 0: lits.append(f"I{cols[c]}") elif n == 0: lits.append(f"/I{cols[c]}") terms.append(" & ".join(lits) if lits else "1") live = [t for t in terms if t != "0"] if mode == "complex" and terms: oe, sop = terms[0], [t for t in terms[1:] if t != "0"] print(f"pin {pin}: xor={xor} ac1={ac1} OE=({oe})") print(f" {'' if xor else '/'}O{pin} = " + (" | ".join(f"({t})" for t in sop) or "0")) else: print(f"pin {pin}: xor={xor} ac1={ac1}") print(f" {'' if xor else '/'}O{pin} = " + (" | ".join(f"({t})" for t in live) or "0")) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))