#!/usr/bin/env python3 """Fetch the references docs/phase0.md relies on into the git-ignored reference/. Every file is pinned twice: by the commit in its URL where the host has commits, and by the SHA-256 of the bytes. Nothing fetched here is committed or shipped. python scripts/fetch_references.py fetch what is missing, then verify python scripts/fetch_references.py --verify verify only: no network python scripts/fetch_references.py --large also fetch the 24 MB die photograph python scripts/fetch_references.py --local also check the files cited in place from sms-python and megadrive-python A file whose bytes do not match its pin is left where it is, reported, and the script exits 1. Nothing is ever deleted or overwritten: move a bad file into trash/ by hand and run again. What is deliberately NOT fetched (docs/phase0.md, "Sources not read"): the netlist, Verilog and Logisim files of emu-russia/SEGAChips VDP/PSG (decision D1: they may be run as a closed box, never read; not needed for the first build). Nuked-PSG (GPL-2.0-or-later) IS fetched, for the closed-box rung only, under restart.md rule 8: validation/nuked_harness.c is compiled against it and nothing else touches it. Only ympsg.h, LICENSE and README.md may be opened. ympsg.c is compiled and never opened, searched or quoted, including when a comparison fails. """ from __future__ import annotations import argparse import hashlib import socket import sys import urllib.request from dataclasses import dataclass from pathlib import Path ROOT = Path(__file__).resolve().parents[1] REFERENCE = ROOT / "reference" USER_AGENT = "reference-fetch" SEGACHIPS = "https://raw.githubusercontent.com/emu-russia/SEGAChips/1b378b45c8f0a88501ba593cc480dc5159149a8d" EMU76489 = "https://raw.githubusercontent.com/digital-sound-antiques/emu76489/c0fa097060e022db237163d79704025435042997" TESTROM = "https://raw.githubusercontent.com/JoppyFurr/SN76489-TestRom/7d2068dfc1713890844f47a0adc00a6561d07cf0" DIE = "https://raw.githubusercontent.com/gchiasso/76489A-analysis/c552112ad479aca30f93eef64d202bb875ca7f8e" NUKED_PSG = ( "https://raw.githubusercontent.com/nukeykt/Nuked-PSG/d15a168c676f4669e23660be9225b34ad7c1764e" ) KORTINK = "https://www.zeridajh.org/articles/me_sn76489_sound_chip_details" @dataclass(frozen=True) class Pin: path: str # under reference/ url: str sha256: str large: bool = False PINS: tuple[Pin, ...] = ( # John Kortink, "SN76489 sound chip details" (2002): a BBC Micro capture. # "Feel free to use this information ... if you give me a suitable credit." Pin( "kortink/index.html", f"{KORTINK}/index.html", "c891592325ec281cce69ee032f4e8f57047b428530dd0feeacea587c2018fdf5", ), Pin( "kortink/white_noise.bin", f"{KORTINK}/downloads/white_noise.bin", "ae43bc97f5c6d9524c56d7f1e8925242d6ac8a0bd40b33052c8eadd68bcbd6df", ), # emu-russia/SEGAChips, CC0-1.0: the prose pages of the Mega Drive VDP PSG # study and the two TI manuals it carries. The netlist files are not fetched. Pin( "segachips/LICENSE", f"{SEGACHIPS}/LICENSE", "a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499", ), Pin( "segachips/Readme.md", f"{SEGACHIPS}/Readme.md", "4caa609e8ee61cdf10802af83b563a9f6e54bf0942f02824bafec3ff352818f9", ), Pin( "segachips/VDP/PSG/Readme.md", f"{SEGACHIPS}/VDP/PSG/Readme.md", "073c81b55c0ce01b1fcd2e570671cc8d39370df3563b48b2e107dd5d5451ad59", ), Pin( "segachips/VDP/PSG/Docs/PSG_Other_Credits.txt", f"{SEGACHIPS}/VDP/PSG/Docs/PSG_Other_Credits.txt", "02e8089c5eea6ffd53205c06e14421da9701be05178b1486cfb8838d912ef305", ), Pin( "segachips/VDP/PSG/Docs/SN76489_Application_Manual.pdf", f"{SEGACHIPS}/VDP/PSG/Docs/SN76489_Application_Manual.pdf", "4e0e11a4b9843c9cb9a2e6de7a2c4e696eba96ba1b425e3f1d402c25ec7ea801", ), Pin( "segachips/VDP/PSG/Docs/SN76494_SN76494A_SN76496_SN76496A_Application_Manual.pdf", f"{SEGACHIPS}/VDP/PSG/Docs/SN76494_SN76494A_SN76496_SN76496A_Application_Manual.pdf", "3f55896359ad7aac955d492677e822d8df004d7021b0a1b685a535a97b0a960d", ), Pin( "segachips/VDP/PSG/PSG_WikiEng/Readme.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiEng/Readme.md", "e835d1c64f2413b268198c69cd5f52b4e0d7e8f1e7e8c45eb99daa5d44578a6c", ), Pin( "segachips/VDP/PSG/PSG_WikiEng/topo.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiEng/topo.md", "ee3f186a50e3a68e958649d29715fcc01074416533d53c4beca6ed0ca685788b", ), Pin( "segachips/VDP/PSG/PSG_WikiEng/dac.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiEng/dac.md", "95366c6e0206e60636f7f952799ccceeffa3f6f443292ea68a4159cd68792d43", ), Pin( "segachips/VDP/PSG/PSG_WikiEng/pads.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiEng/pads.md", "b894d049f0e98a7d5f8d08e30b66642807030cabbe5f40e5ff84aa94fa343e7c", ), Pin( "segachips/VDP/PSG/PSG_WikiRus/clk.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiRus/clk.md", "56087d0fd7327f7766ec87f06e102ffe3cd5269cb6290656c802081ee11690d4", ), Pin( "segachips/VDP/PSG/PSG_WikiRus/test.md", f"{SEGACHIPS}/VDP/PSG/PSG_WikiRus/test.md", "cd77b4a756d6abaf1695cf259e3fa4214a73a7e92c70542985026ceacb789030", ), # Mitsutaka Okazaki, emu76489, MIT: an emulator, tier 5. Pin( "emu76489/LICENSE", f"{EMU76489}/LICENSE", "e0db14654c1f977ec384c3fad3baf6e1f4d21c8369946823bbb6d4e06ebc372b", ), Pin( "emu76489/README.md", f"{EMU76489}/README.md", "09f75deafcd9c7485e37a6f38c0aac443879ad092ef022bb18f10d09cdbde85c", ), Pin( "emu76489/emu76489.h", f"{EMU76489}/emu76489.h", "00ef9706d0779c1e3e2ebb1346175c69c05009f6750940a74e8ab51d16be9596", ), Pin( "emu76489/emu76489.c", f"{EMU76489}/emu76489.c", "8680fd1139fe65b0ad339db1e38f4a83c8ac4c2fff9bded26d12b2752cb219db", ), # JoppyFurr, SN76489 TestRom, MIT: the source of the ROMs sms-python pins. Pin( "sn76489-testrom/LICENSE.txt", f"{TESTROM}/LICENSE.txt", "e2a17aabbc383691ebb2da310ac180b1fded360df214a3b543d5112cd474637f", ), Pin( "sn76489-testrom/README.md", f"{TESTROM}/README.md", "1fe5b1c0f91374fc4663dfea4531a4f5e40d62b8b639bbc704af8e70ca1bc1bc", ), Pin( "sn76489-testrom/source/main.c", f"{TESTROM}/source/main.c", "d9ac476785b81a92ba5dfa5661790135242dc5d2fc0caa7fe5ce8bab5da7ff97", ), Pin( "sn76489-testrom/source/register.c", f"{TESTROM}/source/register.c", "a5d2c6d15aebdc50ab92df3b5e13c95c506d3290a1f60c80c4de8dfe098e02c7", ), Pin( "sn76489-testrom/source/register.h", f"{TESTROM}/source/register.h", "a3e80842de9368eee62946817ef4b720af3096ab3f2529028f001466919f0d65", ), # Nuke.YKT, Nuked-PSG, GPL-2.0-or-later: a closed-box detector (rule 8). # RUN ONLY. ympsg.c must not be opened; the harness uses ympsg.h alone. Pin( "nuked-psg/LICENSE", f"{NUKED_PSG}/LICENSE", "8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643", ), Pin( "nuked-psg/README.md", f"{NUKED_PSG}/README.md", "75c64a34e7328d79ccf9ae47f64a36a0be2465e8227584a4ff341512f0bbf4ab", ), Pin( "nuked-psg/ympsg.h", f"{NUKED_PSG}/ympsg.h", "3179e98bfdc02f0bb16f6ee678d6b796412525ac9a25f9114f63b4c3c0445082", ), Pin( "nuked-psg/ympsg.c", f"{NUKED_PSG}/ympsg.c", "3a28b826ebf76a11ba9d97bba91f85f5bd7e6ec5e0f149f053dfc5974cb630b2", ), # gchiasso, "76489A-analysis": an annotated die photograph. The repository # has no licence file; the image itself says "(CC BY-NC) 2015, 2017 gac, # photo: digshadow". Non-commercial: whether it may be studied is an open # decision (docs/handoff-brief.md, D2). It is fetched only with --large. Pin( "die-76489a/README.md", f"{DIE}/README.md", "2693d73050de3b5359afa7de75b4fa8700de45b2b4389503e6211a45da655cc6", ), Pin( "die-76489a/76489_r1_big.jpg", f"{DIE}/76489_r1_big.jpg", "d47a1193f716d14893fa896610ff53561abfb82ccb78b5226ebf43ebad60fae2", large=True, ), ) #: Files read in place from the sibling repositories: (path, sha256). They are #: not copied here. ``--local`` checks that they are still the bytes that were read. LOCAL: tuple[tuple[str, str], ...] = ( ( "/data/emu/sms-python/reference/smspower-wiki/SN76489.pmwiki", "59c0015be73a7a7be112b3c3e19024113cab02886b82a5d6390c62afd3f28e4f", ), ( "/data/emu/sms-python/reference/smspower-wiki/GameGearStereo.pmwiki", "31e0bcc892c89e30e1f1dc6cd03e203ac4863eef58b4d19a36d278b165f6ddec", ), ( "/data/emu/sms-python/reference/manuals/TI_SN76489_datasheet_console5.pdf", "ad2b99beee0f4f7979828c4f3d9f2bf013deac39fa6263309875232e78a99f91", ), ( "/data/emu/sms-python/reference/manuals/TI_SN76489AN_application_manual.pdf", "4e0e11a4b9843c9cb9a2e6de7a2c4e696eba96ba1b425e3f1d402c25ec7ea801", ), ( "/data/emu/sms-python/reference/smspower/SN76489-20030421.txt", "f7713973198fd2fcda5f296c8140acd8ef7431adcb66d0274f655bb9a8e55aa8", ), ( "/data/emu/sms-python/reference/smspower/sc3000h-20040729.txt", "f9f3b4dab18f4f625470c18f720c13dddd6b448062b86f4010cc5cb5dc849944", ), ( "/data/emu/sms-python/reference/smspower/msvdp-20021112.txt", "dc16f8e687b8b137d9f7d457659135ebb7b21e6e7874250fac0719dd1eb6207c", ), ( "/data/emu/sms-python/reference/smspower-forum/17121-HelpSoundDifferenceInMasterSystemVDPRevisions.txt", "ee38c8c7021a63b61487188d2d9cf8844fe117698ec958c8b4a718e34b476140", ), ( "/data/emu/sms-python/reference/smspower-forum/topic-20066.txt", "27205482c3d9e3bb8621474641dae2f83b8304b1404085c7383ae7747548e3c6", ), ( "/data/emu/sms-python/reference/smspower-attach/SegaVDPWhiteNoise.zip", "e49b1a3c26cb51a76c009adb3e713bc375f5e5f9c569104fd5eb99a3c705e576", ), ( "/data/emu/sms-python/reference/smspower-attach/SN76489WhiteNoise.zip", "dfb670d472471f7e3d20969cc07574af6ca4f2af7517391e786ea3022dd9177d", ), ( "/data/emu/sms-python/reference/smspower-attach/sms-officialdocs-txt-bmp.zip", "84234b2a4849988f6e36a3fe80e116af6255fac595c90fbc4b7f9828a0a19f39", ), ( "/data/emu/sms-python/reference/smspower-attach/gg-officialdocs-png.zip", "e616ff32ba6a17621eeeca8ec5e1ed62fdfa0d5a06853a73a3820fc98afce1dc", ), ( "/data/emu/sms-python/reference/src/mame/src/devices/sound/sn76496.cpp", "eb4db2fa887fecc07aa1c8f890446966cbd5ca74040d02b21f9dabb3dcaee6d5", ), ( "/data/emu/sms-python/reference/src/mame/src/devices/sound/sn76496.h", "e9af1f7718d6cc7732ad8fd3d5dd54782cc52cad22b7be93e95cee5490a3dca3", ), ( "/data/emu/sms-python/tests/test_roms/smspower/SN76489TestRom-SMS-1.00.zip", "7b92df368b417b2834f5c1cff2f4eb769cdb05d6044fbdb5e052409520eced3b", ), ( "/data/emu/sms-python/tests/test_roms/smspower/SN76489TestRom-GG-1.00.zip", "ffac96623c60d9a410767c8b47f4d2c8e53cb47facecf5ff75567e4034a8b9f8", ), ( "/data/emu/sms-python/tests/test_roms/smspower/SN76489TestRom-SG-1.00.zip", "35f103ec7063657b9dd71b1339cafcabd480f8f035b65829c28a247b21898fdb", ), ( "/data/emu/megadrive-python/reference/manuals/GenesisTechnicalOverview.pdf", "2a34c6b54bd9f4da170c7329c0f8d83bb266f7324d01dc3ba234cb273cae9a9e", ), ) def sha256_of(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for block in iter(lambda: handle.read(1 << 20), b""): digest.update(block) return digest.hexdigest() def prefer_ipv4() -> None: """Try IPv4 addresses first. On a host with no IPv6 route, urllib waits out the whole timeout on each IPv6 address before it tries IPv4 (seen here: two minutes a file). """ resolve = socket.getaddrinfo def ipv4_first(*args, **kwargs): # type: ignore[no-untyped-def] return sorted(resolve(*args, **kwargs), key=lambda entry: entry[0] != socket.AF_INET) socket.getaddrinfo = ipv4_first # type: ignore[assignment] def download(pin: Pin, destination: Path) -> None: destination.parent.mkdir(parents=True, exist_ok=True) request = urllib.request.Request(pin.url, headers={"User-Agent": USER_AGENT}) partial = destination.with_name(destination.name + ".part") with urllib.request.urlopen(request, timeout=120) as response, partial.open("wb") as out: for block in iter(lambda: response.read(1 << 20), b""): out.write(block) partial.rename(destination) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--verify", action="store_true", help="check pins only; no network") parser.add_argument("--large", action="store_true", help="include the 24 MB die photograph") parser.add_argument("--local", action="store_true", help="also check files cited in place") args = parser.parse_args() if not args.verify: prefer_ipv4() bad = missing = 0 for pin in PINS: destination = REFERENCE / pin.path if not destination.exists(): if pin.large and not args.large: print(f"skipped {pin.path} (large: pass --large)") continue if args.verify: print(f"MISSING {pin.path}") missing += 1 continue try: download(pin, destination) except OSError as error: print(f"FAILED {pin.path}: {error}") missing += 1 continue actual = sha256_of(destination) if actual == pin.sha256: print(f"ok {pin.path}") else: print(f"MISMATCH {pin.path}\n pinned {pin.sha256}\n found {actual}") bad += 1 if args.local: for name, expected in LOCAL: path = Path(name) if not path.exists(): print(f"MISSING {name}") missing += 1 elif sha256_of(path) == expected: print(f"ok {name}") else: print(f"MISMATCH {name}") bad += 1 print(f"{bad} mismatched, {missing} missing") return 1 if bad or missing else 0 if __name__ == "__main__": sys.exit(main())