#!/usr/bin/env python """Fetch the MSM6295's references into reference/ (ignored), every item pinned. python scripts/fetch_references.py # fetch what is missing python scripts/fetch_references.py --verify # only check what is there python scripts/fetch_references.py --sf2ud # also Artemio Urbina's sf2ud.zip What comes from where (licences in docs/validation.md): - OKI's 16-page MSM6295 data sheet, the PDF kept in jotego/jt6295's doc/, by SHA-256. jt6295 itself (GPL-3.0 Verilog) and FinalBurn Neo's msm6295 (non-commercial) are no longer fetched (Aubrey, 2026-11-24): closed boxes, cited by URL and commit in docs/validation.md. - MAME 0.285 (tag mame0285 = commit f379cd0): the OKI sound devices and the three drivers that change pin 7 at run time, by SHA-256. - libvgm's okim6295/okiadpcm (BSD-3-Clause), by SHA-256 at a commit. - OKI's 1987 and 1994 voice synthesis data books (bitsavers) and Dialogic's ADPCM application note (Wayback Machine), by SHA-256. - The CPS-1 hardware recordings and test ROMs that Artemio Urbina and jotego attached to jtcores issue 559, by SHA-256 (licence not stated: cite, do not redistribute). sf2ud.zip holds Capcom's graphics and program ROMs as well as the test, so it is fetched only with --sf2ud. - Two jtcores issue threads as JSON from GitHub's API: live pages, unpinned. Then it derives, when missing, the files that are made from the fetched ones: each zip unpacked beside itself, pdftotext -layout of the two data books, page images of the data sheet (pdftoppm, 110 dpi) and their OCR (tesseract). Derived files are checked for presence only: poppler and tesseract versions change the bytes. """ from __future__ import annotations import argparse import hashlib import shutil import subprocess import sys import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent REF = ROOT / "reference" MAME = "f379cd060ac3b2bd567ebbd0957aed059d6895ff" # tag mame0285 MAME_RAW = f"https://raw.githubusercontent.com/mamedev/mame/{MAME}/src" LIBVGM = "c5c03a2c2514d38d2ab7b7e8dbd021af0b8292cb" LIBVGM_RAW = f"https://raw.githubusercontent.com/ValleyBell/libvgm/{LIBVGM}/emu/cores" BITSAVERS = "https://bitsavers.org/components/oki/_dataBooks" JTCORES_FILES = "https://github.com/jotego/jtcores/files" #: jotego/jt6295 (GPL-3.0 Verilog, 7d76b0be8cd8f85f3ae741178c9830b20e2071a1) is not cloned #: since 2026-11-24 (Aubrey's rule: no closed-box source in an MIT repository); only OKI's #: data sheet from its doc/ is fetched, as a file (FILES below). REPOS: list[dict] = [] # (local path under reference/, url, sha256, why) FILES = [ ("docs/okim6295.pdf", "https://raw.githubusercontent.com/jotego/jt6295/7d76b0be8cd8f85f3ae741178c9830b20e2071a1/doc/okim6295.pdf", "8b469cb8e8316d2679929e294fb174b2ca5e78ef03e23596a88dd3eedcf0108f", "OKI's 16-page MSM6295 data sheet, as kept in jt6295's doc/ (a manufacturer document)"), *[ (f"src/mame/{name}", f"{MAME_RAW}/devices/sound/{name}", sha, why) for name, sha, why in [ ("okim6295.cpp", "ff6f45f2e7db51cab3344ab3e1a55d0d95e1341c2994a35983e733fae022b18b", "MAME's MSM6295 device: the detector the whole-chip streams are compared with"), ("okim6295.h", "c2c75718818b24864911fc714c8cf61a278266c93f565d97831a81303c6db2a1", "its header: pin 7 enum, set_pin7"), ("okiadpcm.cpp", "a49dbd1089b3163a3a38480f6d877462c506b9f85e66a0cf5ea015b2a193dcd8", "MAME's OKI ADPCM decoder shared by the OKI devices, and the patent list"), ("okiadpcm.h", "28f70d023aa2f18a0287b1290ac108692b5f270ce91622c636742af2e8909ab4", "its header"), ("msm5205.cpp", "98d5a3ee601c596fa2dcf1ec57dec250e8d13b9551938a10b6f911d01c07c77c", "the MSM5205: a related chip, for the chip-rule question"), ("msm5205.h", "5415bbe80f9a5b7a68e85c05363f2222b9e6ebf509e2c766c6a3629a0b0bd5fc", "its header"), ("okim6258.cpp", "ed995583cc66d0422d77f5d07433dc9d9758dca1809476de8e67d36627ba80ff", "the MSM6258: a related chip"), ("okim6258.h", "5f8bcd4a50a97cebdb6dd016d1f38dce807951f6d5aa9712c329b463f32c8ebe", "its header"), ("okim6376.cpp", "d57e84d47b0cf989b14facb6d5388e6fd67048300a7f478cd89cc0ead846da83", "the MSM6376: a related chip (fruit machines)"), ("okim6376.h", "5adf8bff1571ec935470840a16a9341255452d4750e84fb1de0777f25f8a76d3", "its header"), ("okim9810.cpp", "c906d198a095eaa676c4a2082f3725b4c1871ca17738e377fecb14c1d013352d", "the MSM9810: a related chip"), ("okim9810.h", "6e71fc41755e8c390ed7275325e3b09abba8663d3b7fd1bf9b294edcc966fec8", "its header"), ] ], ("src/mame-drivers/cps1.cpp", f"{MAME_RAW}/mame/capcom/cps1.cpp", "e0b6df00a7d0efccc91bfe9c3e752ec36c6edc8c98757331d1f1ebc6f739532e", "CPS-1: the first consumer; pin 7 from Z80 port $F006 at run time"), ("src/mame-drivers/atarijsa.cpp", f"{MAME_RAW}/mame/atari/atarijsa.cpp", "7b3b7ea04c3d99575f94bf59e8cfada6e1e60f40e819558b5de0eb23b12697c3", "Atari JSA: pin 7 and the chip's reset from a port at run time"), ("src/mame-drivers/gottlieb_a.cpp", f"{MAME_RAW}/mame/shared/gottlieb_a.cpp", "733c093b19264513c82d703dbabdd40af7074b5a31e70a7e0e5e9b2d88911939", "Gottlieb System 3 sound: pin 7 and a ROM bank at run time"), ("src/libvgm/okim6295.c", f"{LIBVGM_RAW}/okim6295.c", "00778ea2bb630abf549ea25671e9d19df50d4ce4889be9e1fcfa6739b5ae6194", "libvgm's MSM6295, MAME-derived C (BSD-3-Clause): a detector, and the VGM ports"), ("src/libvgm/okim6295.h", f"{LIBVGM_RAW}/okim6295.h", "23b61cc3f4974b64d30a3c8c95eca9422d146598eaef664b7d79ff6f50b74153", "its header"), ("src/libvgm/okiadpcm.c", f"{LIBVGM_RAW}/okiadpcm.c", "95ad62ea67fb3f239873abf79200ce11d3d548c40022ec02da0dd4a3abaf5af1", "libvgm's decoder: MAME's before 2020, starting at -2"), ("src/libvgm/okiadpcm.h", f"{LIBVGM_RAW}/okiadpcm.h", "f850732fe0cdb032d2cedda758d4b74eb6562f701b80ec75d7a59d41cd243864", "its header"), ("docs/1987_OKI_Voice_Synthesis_LSI_Data_Book.pdf", f"{BITSAVERS}/1987_OKI_Voice_Synthesis_LSI_Data_Book.pdf", "4847d6ee582eb3c70f8de7cc55e8f6780b23fb185358b1850ebc19014de039f1", "OKI 1987 data book, first edition: the earlier MSM6295 sheet, the ADPCM formula"), ("docs/1994_OKI_Voice_Synthesis_ICs.pdf", f"{BITSAVERS}/1994_OKI_Voice_Synthesis_ICs.pdf", "582368add98884f605cf22cad81fdf450358b5e73ef8bb050e2355e5f728b0ac", "OKI '94 data book, fifth edition: the MSM6295 sheet of record (PDF pp. 64-79), MSM5205"), ("docs/dialogic-adpcm.pdf", "https://web.archive.org/web/2id_/http://www.multimedia.cx/mirror/dialogic-adpcm.pdf", "d6e14f5aa2ccc159a2e9a5dbc944eb0407e173f18b2f7a43245402fde5f885ae", "Dialogic, ADPCM Algorithm (1988): the step table and index adjustments in print"), ("artemio/CPS1-Z80loop.zip", f"{JTCORES_FILES}/14371981/CPS1-Z80loop.zip", "a89ecb3d0fade1ef87259cb65ed5def2659bc1e62d6ec400f8c6261b17af5e68", "Artemio Urbina's CPS-1 Z80 test loop and its sample ROM (jtcores #559)"), ("artemio/patches.bin.zip", f"{JTCORES_FILES}/14388091/patches.bin.zip", "22215698b7861fac6f77375fb4c56306a7980531e2fb8cc51ad325192cc3c126", "jotego's sine-wave sample ROM for the same loop (jtcores #559)"), ("artemio/ADPCM_Only.zip", f"{JTCORES_FILES}/14391441/ADPCM_Only.zip", "1a03406cdfab1bcab487607de93e8ee3f148b783aa37b4f9bf9d821e704ae756", "Magic Sword (88617A-7b) board recording, 24-bit 192 kHz, with MiSTer captures"), ("artemio/resultsFIR.zip", f"{JTCORES_FILES}/14466262/resultsFIR.zip", "ba5cf7528882a5b2893645b4b60e8debf44a271af7c8e09cf7f5948ee1982b5a", "the same and an SF2 (89626A-4) board recording, 24-bit 192 kHz"), ] #: Opt-in: the MDFourier CPS-1 test as a whole SF2 set, Capcom ROMs included. SF2UD = ("artemio/sf2ud.zip", f"{JTCORES_FILES}/14466265/sf2ud.zip", "e7de974cf3ff28865d2a653c957dbac547b70fee103a01aa5c1e1e85fde6f7f6", "MDFourier CPS-1 test ROM set (contains Capcom ROMs: opt-in)") #: Live pages: kept as snapshots, no pin. LIVE = [ ("docs/jtcores-issue-559.json", "https://api.github.com/repos/jotego/jtcores/issues/559/comments?per_page=100", "cps1: adpcm filter (the board recordings, the board filters, 'MAME doesn't handle Busy')"), ("docs/jtcores-issue-528.json", "https://api.github.com/repos/jotego/jtcores/issues/528/comments?per_page=100", "dd2: jt6295's channel value drifting after a phrase (fixed in jt6295 d1823f4)"), ] #: Zips unpacked beside themselves: zip -> directory. UNPACK = { "artemio/CPS1-Z80loop.zip": "artemio/z80loop", "artemio/patches.bin.zip": "artemio/patches", "artemio/ADPCM_Only.zip": "artemio/adpcm_only", "artemio/resultsFIR.zip": "artemio/results_fir", "artemio/sf2ud.zip": "artemio/sf2ud", } #: Of sf2ud.zip only the sound ROMs are unpacked. SF2UD_MEMBERS = ("sf2_9.12a", "sf2_18.11c", "sf2_19.12c") USER_AGENT = "reference-fetch/1.0" def sha256(path: Path) -> str | None: return hashlib.sha256(path.read_bytes()).hexdigest() if path.exists() else None def curl(url: str) -> bytes | None: done = subprocess.run( ["curl", "-sSfL", "--max-time", "600", "-A", USER_AGENT, url], capture_output=True ) if done.returncode: print(f" curl failed: {done.stderr.decode().strip()}") return None return done.stdout def fetch_file(rel: str, url: str, want: str, verify: bool) -> int: path = REF / rel have = sha256(path) if have == want: print(f"{rel}: ok") return 0 if verify: print(f"{rel}: {'wrong SHA-256 ' + have if have else 'missing'}") return 1 data = curl(url) if data is None: print(f"{rel}: not fetched") return 1 got = hashlib.sha256(data).hexdigest() if got != want: print(f"{rel}: fetched file has the wrong SHA-256 ({got}); not saved") return 1 path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(data) print(f"{rel}: fetched") return 0 def head(path: Path) -> str | None: if not (path / ".git").exists(): return None done = subprocess.run(["git", "-C", str(path), "rev-parse", "HEAD"], capture_output=True, text=True) return done.stdout.strip() or None def fetch_repo(entry: dict, verify: bool) -> int: path = ROOT / entry["path"] if head(path) == entry["rev"]: print(f"{entry['path']}: at {entry['rev'][:7]}") return 0 if verify: print(f"{entry['path']}: missing or not at {entry['rev'][:7]}") return 1 if not path.exists(): subprocess.run(["git", "clone", "-q", entry["url"], str(path)], check=True) else: subprocess.run(["git", "-C", str(path), "fetch", "-q", "origin"], check=True) subprocess.run(["git", "-C", str(path), "checkout", "-q", "--detach", entry["rev"]], check=True) if head(path) != entry["rev"]: print(f"{entry['path']}: fetched the wrong revision") return 1 print(f"{entry['path']}: fetched {entry['rev'][:7]}") return 0 def fetch_live(rel: str, url: str, verify: bool) -> int: path = REF / rel if path.exists(): print(f"{rel}: present (a live page, unpinned)") return 0 if verify: print(f"{rel}: missing (a live page; not counted as a failure)") return 0 data = curl(url) if data is None: return 0 path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(data) print(f"{rel}: fetched (a live page, unpinned)") return 0 def derive(verify: bool) -> int: """Unpack the zips and make the text and page images the docs cite.""" bad = 0 for zip_rel, dir_rel in UNPACK.items(): zip_path, out = REF / zip_rel, REF / dir_rel if not zip_path.exists(): continue with zipfile.ZipFile(zip_path) as archive: names = [n for n in archive.namelist() if not n.endswith("/")] if zip_rel.endswith("sf2ud.zip"): names = [n for n in names if Path(n).name in SF2UD_MEMBERS] missing = [n for n in names if not (out / Path(n).name).exists()] if not missing: print(f"{dir_rel}/: unpacked ({len(names)} files)") continue if verify: print(f"{dir_rel}/: {len(missing)} files not unpacked") bad += 1 continue out.mkdir(parents=True, exist_ok=True) for name in missing: (out / Path(name).name).write_bytes(archive.read(name)) print(f"{dir_rel}/: unpacked {len(missing)} files") tools = {tool: shutil.which(tool) for tool in ("pdftotext", "pdftoppm", "tesseract")} docs = REF / "docs" jobs = [ (docs / "db1987.txt", ["pdftotext", "-layout", str(docs / "1987_OKI_Voice_Synthesis_LSI_Data_Book.pdf"), str(docs / "db1987.txt")]), (docs / "db1994.txt", ["pdftotext", "-layout", str(docs / "1994_OKI_Voice_Synthesis_ICs.pdf"), str(docs / "db1994.txt")]), (docs / "png" / "ds-16.png", ["pdftoppm", "-r", "110", "-png", str(docs / "okim6295.pdf"), str(docs / "png" / "ds")]), (docs / "png" / "db87-048.png", ["pdftoppm", "-r", "92", "-png", "-f", "48", "-l", "48", str(docs / "1987_OKI_Voice_Synthesis_LSI_Data_Book.pdf"), str(docs / "png" / "db87")]), ] jobs += [ (docs / "png" / f"ds-{page:02d}.txt", ["tesseract", str(docs / "png" / f"ds-{page:02d}.png"), str(docs / "png" / f"ds-{page:02d}")]) for page in range(1, 17) ] for target, command in jobs: rel = target.relative_to(REF) if target.exists(): continue if verify: print(f"{rel}: not derived") bad += 1 continue if not tools.get(command[0]): print(f"{rel}: {command[0]} not installed; skipped") continue target.parent.mkdir(parents=True, exist_ok=True) subprocess.run(command, check=True, capture_output=True) print(f"{rel}: derived") if not bad: print("derived files: present") return bad def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--verify", action="store_true", help="check, do not fetch") parser.add_argument("--sf2ud", action="store_true", help="also fetch sf2ud.zip, which contains Capcom ROMs") args = parser.parse_args(argv) bad = sum(fetch_repo(entry, args.verify) for entry in REPOS) files = FILES + ([SF2UD] if args.sf2ud or (REF / SF2UD[0]).exists() else []) bad += sum(fetch_file(rel, url, sha, args.verify) for rel, url, sha, _ in files) bad += sum(fetch_live(rel, url, args.verify) for rel, url, _ in LIVE) bad += derive(args.verify) print("all pinned references verified" if not bad else f"{bad} problems") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())