# SPDX-License-Identifier: MIT """Rung 1: the one capture from hardware in hand. John Kortink digitised the white noise of the SN76489 in his BBC Micro and published the 32,767-bit sequence ("SN76489 sound chip details", 15 March 2002, https://www.zeridajh.org/articles/me_sn76489_sound_chip_details/; phase0.md S9). Credit for the capture, and for the rule the discrete variant's noise follows, is his. What this rung can and cannot say (validation.md): it judges the discrete variant's register length, taps and sequence against one real TI chip. It says nothing of Sega's parts, and the capture fixes neither the polarity (U5) nor the alignment after a noise write (U7): both are stated below as found, not proved. """ from __future__ import annotations import os import zipfile from pathlib import Path import pytest from helpers import REFERENCE, noise_bits, sha256 from sn76489_python import SN76489AN #: SHA-256 of Kortink's `white_noise.bin` as fetched (scripts/fetch_references.py #: pins the same value). With it the comparison runs where the file is absent. CAPTURE_SHA256 = "ae43bc97f5c6d9524c56d7f1e8925242d6ac8a0bd40b33052c8eadd68bcbd6df" #: Where in the capture the bit after the first shift from a noise write falls, #: counting from 0. Kortink gives the capture's "skew" as 20,862: the same place #: within one, the one being whether the bit before or after the first shift is #: counted. FOUND by search against the capture, not predicted. ROTATION = 20_863 #: sms-python's checkout, beside this one unless SMS_PYTHON_DIR says otherwise. Its #: git-ignored reference/ holds SMS Power's attachments; absent, the test skips. SMS_PYTHON = Path( os.environ.get("SMS_PYTHON_DIR", Path(__file__).resolve().parents[2] / "sms-python") ) SMS_ATTACH = SMS_PYTHON / "reference" / "smspower-attach" def model_sequence() -> bytes: return bytes(noise_bits(SN76489AN, control=0x04, shifts=32_767)) def as_captured(model: bytes) -> bytes: # POLARITY: the capture is the inverse of the model's bit. Kortink's own rule # says so ("output of the generator: the inverse of new_value[0]"). The core # reports "bit set" as the higher level for every variant (spec section 8), so # the inversion is applied here, in the comparison, and is not a claim about # the pin (U5). inverted = bytes(1 - bit for bit in model) return inverted[-ROTATION:] + inverted[:-ROTATION] def test_discrete_white_noise_is_kortinks_capture_by_its_hash(): """Tier 1 (S9), spec 6.2.1 and 6.2.3: 32,767 of 32,767 bits. The model's sequence from a noise write, inverted and rotated by ROTATION, has the SHA-256 of the captured file: every bit agrees. """ assert sha256(as_captured(model_sequence())) == CAPTURE_SHA256 def test_discrete_white_noise_is_kortinks_capture_bit_for_bit(): """The same against the file itself, with a count of agreeing bits.""" path = REFERENCE / "kortink" / "white_noise.bin" if not path.exists(): pytest.skip("reference/kortink/white_noise.bin not fetched (scripts/fetch_references.py)") capture = path.read_bytes() assert sha256(capture) == CAPTURE_SHA256 assert set(capture) == {0, 1} and len(capture) == 32_767 ours = as_captured(model_sequence()) agreeing = sum(a == b for a, b in zip(ours, capture, strict=True)) assert agreeing == 32_767 # The other polarity matches at no rotation at all. assert model_sequence() not in capture * 2 def test_the_capture_repeats_after_32767_bits_and_no_sooner(): """Tier 1 (S9): "a cycle length of 32767 (2^15-1)". The model's sequence continues into a second, identical cycle and has no shorter period.""" twice = bytes(noise_bits(SN76489AN, control=0x04, shifts=2 * 32_767)) once = twice[:32_767] assert twice[32_767:] == once assert (once * 2).find(once, 1) == 32_767 def _wiki_file(name: str) -> bytes: archive = SMS_ATTACH / name if not archive.exists(): pytest.skip(f"{archive} is not on this machine") with zipfile.ZipFile(archive) as bundle: (member,) = bundle.namelist() return bundle.read(member) def test_the_wikis_two_noise_files_are_the_same_capture(): """A cross-check that the pinned files are what Phase 0 analysed (S8, C9). Seed: `test_discrete_noise_matches_the_wikis_bit_pattern`, kept and re-anchored. Its judge is now Kortink's capture above; this only records that the file SMS Power labels "Sega VDP white noise" is the 15-stage TI sequence from reset and its "SN76489 white noise" the same inverted and rotated, so neither is a capture of Sega's part. Corrected against the seed: the seed emitted the bit shifted *out*, this core the stage at the output end after the shift (spec 6.2.5), so the model's sequence leads the seed's by one bit and the "Sega VDP" file equals it rotated by one place rather than as it stands. """ model = model_sequence() sega_labelled = _wiki_file("SegaVDPWhiteNoise.zip") assert sega_labelled == model[-1:] + model[:-1] ti_labelled = _wiki_file("SN76489WhiteNoise.zip") assert ti_labelled in bytes(1 - bit for bit in model) * 2