#!/usr/bin/env python3 """Rung 6: Nuked-OPN2 run as a closed box beside the core (docs/validation.md). Nuked-OPN2 (Nuke.YKT, LGPL-2.1-or-later) is a clock-level model of the YM3438 with a YM2612 mode. Here it is a DETECTOR: a difference says where to look and proves nothing about a chip. Its source was not read (docs/restart.md rule 8); ``nuked_harness.c`` drives it through the calls its public header declares, and everything said about it below was learned from its output. Nothing in the core was fitted to it (decision P4). python validation/closed_box.py build if needed, run every stream python validation/closed_box.py --list name the streams python validation/closed_box.py NAME ... run some python validation/closed_box.py --probes three measurements of the model's output What is compared: each channel's value in each sample. Three things about the model's output were measured from the output itself and are applied to it: 1. One ``OPN2_Clock`` call is one internal clock; a sample is 24 of them, six slots of four clocks. Which slot is which channel is found by keying one channel at a time (``slot_map``). 2. In its YM3438 mode the model shows a slot's value on three of the slot's four clocks; in its YM2612 mode it shows three times an offset value on one clock and a resting level on the others. In both, the sum of a slot's four clocks divided by three is compared with the core's slot value after the core's own output stage. (For the YM2612 mode this is where the core's provisional ladder constants meet a second opinion.) 4. (Second table only.) In the model a key-on or key-off written anywhere in a sample is heard from operator 1 one sample after it is heard from operators 2, 3 and 4 (``--probes`` measures this). The core keys all four in the same sample: when a write lands inside a sample is not modelled (unknown U1), and no document says which operator takes it when. So that the rest of the digital path can be compared, the second table feeds the CORE each $28 write in two halves, operator 1's bit one sample after the others. This changes the core's input, not the core. The first table does not do it. 3. A clock-level model takes a write some clocks after it is made and the core takes it at the next sample boundary, so each channel of each stream is aligned by a whole number of samples between -2 and +8, chosen to give the most equal samples and printed as "lag". It is stated, not hidden. Every stream keeps Yamaha's waits (spec 2.7): an address, four internal clocks, the data, twenty internal clocks. Streams that break the waits were not run. Exit status is 0 whether or not streams differ: a difference is a finding, and docs/validation.md records each one. """ from __future__ import annotations import random import struct import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from ym2612_python import YM2612, YM2612_DISCRETE, YM3438_ASIC MODEL = ROOT / "reference" / "nuked-opn2" BUILD = ROOT / "validation" / "build" HARNESS = BUILD / "nuked_harness" OUT = ROOT / "validation" / "out" #: The header's chip types: 1 is its YM2612 mode, 2 its "status read on any port". MODEL_TYPE = {"YM3438": 2, "YM2612": 1} VARIANT = {"YM3438": YM3438_ASIC, "YM2612": YM2612_DISCRETE} OPERATOR_OFFSET = (0, 8, 4, 12) # Yamaha's operators 1 to 4 in the register order def build() -> None: """Compile the harness against the fetched model. Compiler messages are kept out of sight (-w, stderr to a file): they would quote the model's source.""" sources = [ROOT / "validation" / "nuked_harness.c", MODEL / "ym3438.c"] for source in sources: if not source.exists(): sys.exit(f"{source} is missing: run scripts/fetch_references.py") if HARNESS.exists() and all(HARNESS.stat().st_mtime > s.stat().st_mtime for s in sources): return BUILD.mkdir(parents=True, exist_ok=True) command = ["cc", "-O2", "-w", "-I", str(MODEL), "-o", str(HARNESS), *map(str, sources)] with (BUILD / "cc.log").open("wb") as log: if subprocess.run(command, stderr=log, check=False).returncode: sys.exit("the harness did not compile; validation/build/cc.log is not to be read") class Stream: """A list of events: ("w", port, value), ("c", internal clocks), ("r", port).""" def __init__(self, chip: str = "YM3438") -> None: self.chip = chip self.events: list[tuple] = [] def reg(self, address: int, value: int, part: int = 0) -> None: """One register write, taking exactly one sample and keeping the waits.""" self.events += [("w", part * 2, address), ("c", 4), ("w", part * 2 + 1, value), ("c", 20)] def op(self, channel: int, operator: int, base: int, value: int) -> None: part, index = divmod(channel, 3) self.reg(base + OPERATOR_OFFSET[operator] + index, value, part) def wait(self, samples: int) -> None: self.events.append(("c", 24 * samples)) def clocks(self, count: int) -> None: self.events.append(("c", count)) def read(self, port: int = 0) -> None: self.events.append(("r", port)) def rekey_after(self, change, channel: int = 0, mask: int = 0xF) -> None: """Key off, make a change, key on: so that the model's own delay in taking the change (unknown U1) does not leave the two phases apart.""" self.key(channel, 0) self.wait(8) change() self.wait(8) self.key(channel, mask) def frequency(self, channel: int, f_number: int, block: int) -> None: part, index = divmod(channel, 3) self.reg(0xA4 + index, (block << 3) | (f_number >> 8), part) self.reg(0xA0 + index, f_number & 0xFF, part) def key(self, channel: int, mask: int = 0xF) -> None: self.reg(0x28, (mask << 4) | (channel + channel // 3)) def voice(self, channel=0, algorithm=7, f_number=0x269, block=4, levels=(127, 127, 127, 0), attack=31, decay=0, sustain=0, release=15, sustain_level=0, feedback=0, multiple=1, detune=0, key_scale=0, ssg=0, pan=0xC0): # fmt: skip part, index = divmod(channel, 3) for operator in range(4): self.op(channel, operator, 0x30, (detune << 4) | multiple) self.op(channel, operator, 0x40, levels[operator]) self.op(channel, operator, 0x50, (key_scale << 6) | attack) self.op(channel, operator, 0x60, decay) self.op(channel, operator, 0x70, sustain) self.op(channel, operator, 0x80, (sustain_level << 4) | release) self.op(channel, operator, 0x90, ssg) self.reg(0xB0 + index, (feedback << 3) | algorithm, part) self.reg(0xB4 + index, pan, part) self.frequency(channel, f_number, block) def run_model(stream: Stream) -> tuple[list[tuple[int, int]], list[tuple[int, int, int]]]: """The model's (left, right) at every internal clock, and its reads.""" commands = [f"t {MODEL_TYPE[stream.chip]}"] commands += [" ".join(str(part) for part in event) for event in stream.events] result = subprocess.run( [str(HARNESS)], input="\n".join(commands).encode(), capture_output=True, check=True ) flat = struct.unpack(f"<{len(result.stdout) // 2}h", result.stdout) reads = [tuple(map(int, line.split())) for line in result.stderr.decode().splitlines()] return list(zip(flat[0::2], flat[1::2], strict=True)), reads def model_slots(clocks: list[tuple[int, int]], slot_of_channel: list[int]) -> list[list[int]]: """Per channel, the left figure of its slot in each sample: the slot's four clocks summed (normalisation 2; the division by three is done by the caller).""" samples = len(clocks) // 24 out = [[0] * samples for _ in range(6)] for channel in range(6): base = 4 * slot_of_channel[channel] row = out[channel] for sample in range(samples): start = 24 * sample + base row[sample] = ( clocks[start][0] + clocks[start + 1][0] + clocks[start + 2][0] + clocks[start + 3][0] ) return out def run_core( stream: Stream, key_skew: bool = False ) -> tuple[list[list[int]], list[tuple[int, int, int]]]: """The core's slot value per channel per sample (left side, after its output stage), and its reads as (internal clock, value, irq). ``key_skew`` is alignment 4 of the module docstring: operator 1's bit of each $28 write is given to the core one sample after the other three. """ variant = VARIANT[stream.chip] fm = YM2612(variant) out: list[list[int]] = [[] for _ in range(6)] reads = [] clock = 0 address = (0, 0) operator1_key = [0] * 8 pending: list[tuple[int, int]] = [] for event in stream.events: if event[0] == "w": port, value = event[1], event[2] if not port & 1: address = (port, value) elif key_skew and address == (0, 0x28): pending.append((clock + 24, value)) held = operator1_key[value & 7] operator1_key[value & 7] = (value >> 4) & 1 value = (value & ~0x10) | (held << 4) fm.write(port, value) elif event[0] == "r": value = fm.read(event[1]) reads.append((clock, 0 if value is None else value, int(fm.irq))) else: for _ in range(event[1]): before = len(fm.samples) fm.run(6) clock += 1 if pending and pending[0][0] == clock: fm.write(0, 0x28) fm.write(1, pending.pop(0)[1]) fm.busy_clocks = 0 fm.write(*address) # put the stream's own address back if len(fm.samples) != before: values = fm.channel_values() for channel in range(6): value = values[channel] if variant.ladder: if fm.channels[channel].left: value += ( variant.ladder_positive if value >= 0 else variant.ladder_negative ) else: value = variant.ladder_idle elif not fm.channels[channel].left: value = 0 out[channel].append(value) fm.take_samples() return out, reads def slot_map() -> list[int]: """Which of a sample's six slots carries each channel in the model's output, found by keying one channel at a time.""" found = [] for channel in range(6): stream = Stream() stream.voice(channel=channel, levels=(0, 0, 0, 0)) stream.key(channel) stream.wait(40) clocks, _ = run_model(stream) energy = [0] * 6 for index, (left, _right) in enumerate(clocks): energy[(index % 24) // 4] += abs(left) found.append(energy.index(max(energy))) if sorted(found) != [0, 1, 2, 3, 4, 5]: sys.exit(f"slot map is not a permutation: {found}") return found def compare(model: list[int], core: list[int]) -> tuple[int, int, int, int, int, int]: """Best whole-sample lag, then (lag, compared, equal, within one, more, worst). ``model`` holds three times the slot figure.""" best = None for lag in range(-2, 9): equal = within = more = worst = count = 0 for index in range(8, min(len(model), len(core)) - 9): there = index + lag difference = abs(model[there] - 3 * core[index]) count += 1 if difference == 0: equal += 1 elif difference <= 3: within += 1 else: more += 1 worst = max(worst, difference) result = (lag, count, equal, within, more, (worst + 2) // 3) if best is None or (result[2], result[3]) > (best[2], best[3]): best = result assert best is not None return best # -- the streams: one feature each, then mixed ones ------------------------------- def s_dac(chip="YM3438"): s = Stream(chip) s.reg(0x2B, 0x80) for value in list(range(0, 256, 5)) + [0x7F, 0x80, 0x81, 0x7E, 0x82]: s.reg(0x2A, value) s.reg(0x2C, 0x08) for value in (0x7F, 0x80, 0x81, 0x00, 0xFF): s.reg(0x2A, value) return s def s_tone(chip="YM3438", **voice): def make(): s = Stream(chip) s.voice(**voice) s.key(voice.get("channel", 0)) s.wait(1500) s.key(voice.get("channel", 0), 0) s.wait(500) return s return make def s_total_levels(): s = Stream() s.voice() s.key(0) for level in (0, 1, 7, 8, 9, 31, 48, 60, 64, 72, 96, 127): s.op(0, 3, 0x40, level) s.wait(200) return s def s_frequencies(): s = Stream() s.voice() s.key(0) for f_number, block in ((0x7FF, 7), (0x7FF, 0), (1, 0), (3, 1), (0x400, 3), (0x269, 5), (0, 4)): s.rekey_after(lambda f=f_number, b=block: s.frequency(0, f, b)) s.wait(250) return s def s_detune_multiple(): s = Stream() s.voice() s.key(0) for detune in range(8): for multiple in (0, 1, 2, 15): s.rekey_after(lambda d=detune, m=multiple: s.op(0, 3, 0x30, (d << 4) | m)) s.wait(120) s.frequency(0, 0, 1) # detune overflow: F-number 0 for detune in (5, 6, 7): s.rekey_after(lambda d=detune: s.op(0, 3, 0x30, (d << 4) | 1)) s.wait(300) return s def s_algorithm(algorithm): def make(): s = Stream() s.voice(algorithm=algorithm, levels=(24, 28, 20, 0), f_number=0x300, block=4) s.op(0, 0, 0x30, 2) s.op(0, 1, 0x30, 3) s.key(0) s.wait(1500) return s return make def s_feedback(): s = Stream() s.voice(algorithm=7, levels=(0, 127, 127, 127)) for feedback in range(8): s.rekey_after(lambda f=feedback: s.reg(0xB0, (f << 3) | 7), mask=0b0001) s.wait(400) return s def s_envelope(attack, decay, sustain, release, sustain_level, key_scale=0): def make(): s = Stream() s.voice(attack=attack, decay=decay, sustain=sustain, release=release, sustain_level=sustain_level, key_scale=key_scale, f_number=0x400, block=6) # fmt: skip s.key(0) s.wait(4000) s.key(0, 0) s.wait(1500) s.key(0) s.wait(600) return s return make def s_rate_changes(): s = Stream() s.voice(attack=10, decay=12, sustain=6, release=4, sustain_level=4, f_number=0x400, block=6) s.key(0) s.wait(300) s.op(0, 3, 0x50, 31) # switch to rate 62 during the attack: stall s.wait(300) s.op(0, 3, 0x50, 20) s.wait(600) s.op(0, 3, 0x60, 28) # a fast decay past the sustain level s.wait(300) s.op(0, 3, 0x80, 0xF4) # sustain level write after the decay ended s.wait(600) return s def s_ssg(ssg, attack=31): def make(): s = Stream() s.voice(attack=attack, decay=20, sustain=18, release=8, sustain_level=3, ssg=ssg, f_number=0x400, block=6) # fmt: skip s.key(0) s.wait(5000) s.key(0, 0) s.wait(1200) return s return make def s_lfo(tremolo_depth, vibrato_depth, rate): def make(): s = Stream() s.voice(f_number=0x6A5, block=4) s.op(0, 3, 0x60, 0x80) s.reg(0xB4, 0xC0 | (tremolo_depth << 4) | vibrato_depth) s.reg(0x22, 0x08 | rate) s.key(0) s.wait(128 * (109, 78, 72, 68, 63, 45, 9, 6)[rate] + 500 if rate >= 6 else 6000) return s return make def s_lfo_off_tremolo(): s = Stream() s.voice() s.op(0, 3, 0x60, 0x80) s.key(0) for depth in range(4): s.reg(0xB4, 0xC0 | (depth << 4)) s.wait(300) return s def s_channel3(mode): def make(): s = Stream() s.voice(channel=2, algorithm=7, levels=(8, 8, 8, 8)) for high, low, f_number in ((0xAD, 0xA9, 0x155), (0xAE, 0xAA, 0x2AA), (0xAC, 0xA8, 0x3F0)): s.reg(high, (5 << 3) | (f_number >> 8)) s.reg(low, f_number & 0xFF) s.reg(0x27, mode << 6) s.key(2) s.wait(1200) return s return make def s_csm(timer_a): def make(): s = Stream() s.voice(channel=2, release=6) s.reg(0x24, timer_a >> 2) s.reg(0x25, timer_a & 3) s.reg(0x27, 0x81) s.wait(1500) s.reg(0x27, 0x41) s.wait(500) return s return make def s_latch(): s = Stream() s.voice() s.reg(0xA4, 0x1E) s.reg(0xA0, 0xFF) s.reg(0xA4, 0x24) # pending: must not be heard yet s.key(0) s.wait(400) s.key(0, 0) s.wait(8) s.reg(0xA5, 0x2A) # a high byte for channel 2 ... s.reg(0xA0, 0x80) # ... and a low byte for channel 1 (contradiction C12) s.key(0) s.wait(400) return s def s_other_port(): s = Stream() s.voice() s.key(0) s.events += [("w", 0, 0x40 + 12), ("c", 4), ("w", 3, 0x20), ("c", 20)] # spec 2.5 s.wait(300) s.events += [("w", 0, 0x2B), ("c", 4), ("w", 3, 0x80), ("c", 20)] # C13: a global one s.wait(100) return s def s_clip(): s = Stream() s.voice(algorithm=7, levels=(0, 0, 0, 0)) s.key(0) s.wait(1500) return s def s_pan(chip="YM3438"): def make(): s = Stream(chip) s.voice() s.key(0) for pan in (0xC0, 0x80, 0x40, 0x00, 0xC0): s.reg(0xB4, pan) s.wait(200) return s return make def s_quiet(chip): def make(): s = Stream(chip) s.voice(levels=(127, 127, 127, 0x3C), f_number=0x100, block=2) s.key(0) s.wait(3000) return s return make def s_mixed(seed, chip="YM3438"): def make(): rng = random.Random(seed) s = Stream(chip) for channel in range(6): s.voice(channel=channel, algorithm=rng.randrange(8), f_number=rng.randrange(0x100, 0x7FF), block=rng.randrange(2, 7), levels=tuple(rng.randrange(0, 40) for _ in range(4)), attack=rng.randrange(18, 32), decay=rng.randrange(0, 20), sustain=rng.randrange(0, 12), release=rng.randrange(2, 12), sustain_level=rng.randrange(16), feedback=rng.randrange(8), multiple=rng.randrange(0, 6), detune=rng.randrange(8), key_scale=rng.randrange(4)) # fmt: skip for _ in range(40): channel = rng.randrange(6) s.key(channel, rng.choice((0xF, 0xF, 0))) s.wait(rng.randrange(20, 400)) return s return make def s_status(): """Status reads: BUSY's length after a data write, and the timer flags.""" s = Stream() s.wait(4) s.events += [("w", 0, 0x30), ("c", 4), ("w", 1, 0x01)] for _ in range(40): s.read(0) s.clocks(1) s.wait(4) s.reg(0x24, 0xFC) # Timer A: 16 samples s.reg(0x26, 0xFE) # Timer B: 2 steps of 16 samples s.reg(0x27, 0x0F) for _ in range(80): s.read(0) s.wait(1) s.reg(0x27, 0x3F) for _ in range(40): s.read(0) s.wait(1) return s STREAMS = { "dac": s_dac, "tone": s_tone(), "tone-channel-5": s_tone(channel=4), "total-levels": s_total_levels, "frequencies": s_frequencies, "detune-multiple": s_detune_multiple, **{f"algorithm-{n}": s_algorithm(n) for n in range(8)}, "feedback": s_feedback, "envelope-slow": s_envelope(10, 8, 4, 3, 5), "envelope-fast": s_envelope(28, 24, 14, 12, 9), "envelope-key-scale": s_envelope(14, 12, 8, 6, 2, key_scale=3), "envelope-sustain-15": s_envelope(31, 18, 0, 8, 15), "rate-changes": s_rate_changes, **{f"ssg-eg-{bits}": s_ssg(8 | bits) for bits in range(8)}, "ssg-eg-0-slow-attack": s_ssg(8, attack=14), "ssg-eg-2-slow-attack": s_ssg(10, attack=14), "ssg-eg-6-slow-attack": s_ssg(14, attack=14), "tremolo-depth-3-rate-7": s_lfo(3, 0, 7), "tremolo-depth-1-rate-6": s_lfo(1, 0, 6), "tremolo-depth-2-rate-3": s_lfo(2, 0, 3), "tremolo-lfo-off": s_lfo_off_tremolo, "vibrato-depth-7-rate-7": s_lfo(0, 7, 7), "vibrato-depth-3-rate-6": s_lfo(0, 3, 6), "vibrato-depth-5-rate-2": s_lfo(0, 5, 2), "channel-3-mode-01": s_channel3(1), "channel-3-mode-11": s_channel3(3), "csm-3f0": s_csm(0x3F0), "csm-3ff": s_csm(0x3FF), "f-number-latch": s_latch, "other-data-port": s_other_port, "clip": s_clip, "pan": s_pan(), "quiet-sine": s_quiet("YM3438"), "mixed-1": s_mixed(1), "mixed-2": s_mixed(2), "mixed-3": s_mixed(3), "ym2612-dac": lambda: s_dac("YM2612"), "ym2612-tone": s_tone("YM2612"), "ym2612-quiet-sine": s_quiet("YM2612"), "ym2612-pan": s_pan("YM2612"), "ym2612-mixed-1": s_mixed(1, "YM2612"), } def first_heard(make, slots: list[int], channel: int = 0) -> int: """How many samples later than the core the model first shows a change: ``make(False)`` and ``make(True)`` are a stream without and with it.""" plain, changed = make(False), make(True) model_a = model_slots(run_model(plain)[0], slots)[channel] model_b = model_slots(run_model(changed)[0], slots)[channel] core_a = run_core(plain)[0][channel] core_b = run_core(changed)[0][channel] in_model = next(i for i, (a, b) in enumerate(zip(model_a, model_b, strict=True)) if a != b) in_core = next(i for i, (a, b) in enumerate(zip(core_a, core_b, strict=True)) if a != b) return in_model - in_core def probes(slots: list[int]) -> list[str]: """Measurements of the model's OUTPUT that say which numbered unknown a difference belongs to. Nothing here changes the core.""" import ym2612_python.chip as chip_module lines = [ "", "PROBE 1 (unknown U1): samples by which the model hears a write later than the core", ] for what, base, value in (("key-on ($28)", None, 0), ("total level", 0x40, 0x10), ("multiple", 0x30, 0x02)): # fmt: skip row = [] for operator in range(4): def make(change, operator=operator, base=base, value=value): levels = [127] * 4 levels[operator] = 0 s = Stream() s.voice(levels=tuple(levels), f_number=0x400, block=6) if base is None: s.key(0) if change else s.wait(1) else: s.key(0) s.wait(100) s.op(0, operator, base, value) if change else s.wait(1) s.wait(100) return s row.append(first_heard(make, slots)) lines.append(f" {what:14} operators 1 to 4: {row}") lines.append("PROBE 2 (contradiction C14): the LFO streams with the core's divider table") lines.append(" swapped, in this process only, for the set that is one fewer") one_fewer = tuple(divider - 1 for divider in chip_module.LFO_DIVIDERS) manual = chip_module.LFO_DIVIDERS for name in ("tremolo-depth-3-rate-7", "tremolo-depth-1-rate-6", "tremolo-depth-2-rate-3", "vibrato-depth-7-rate-7", "vibrato-depth-3-rate-6", "vibrato-depth-5-rate-2"): # fmt: skip stream = STREAMS[name]() model = model_slots(run_model(stream)[0], slots)[0] results = [] for dividers in (manual, one_fewer): chip_module.LFO_DIVIDERS = dividers results.append(compare(model, run_core(stream, True)[0][0])) chip_module.LFO_DIVIDERS = manual lines.append( f" {name:26} equal of {results[0][1]}: manual's {results[0][2]}, " f"one fewer {results[1][2]} (worst {results[1][5]})" ) lines.append("PROBE 3 (unknown U4): the fast envelope, by where the differing samples fall") stream = STREAMS["envelope-fast"]() model = model_slots(run_model(stream)[0], slots)[0] core = run_core(stream, True)[0][0] lag = compare(model, core)[0] bad = [i for i in range(8, len(core) - 9) if model[i + lag] != 3 * core[i]] runs: list[list[int]] = [] for index in bad: if runs and index - runs[-1][1] <= 1: runs[-1][1] = index else: runs.append([index, index]) lengths = sorted({last - first + 1 for first, last in runs}) lines.append(f" {len(bad)} differing samples in {len(runs)} runs of lengths {lengths}") return lines def main() -> int: arguments = sys.argv[1:] if "--list" in arguments: print("\n".join([*STREAMS, "status"])) return 0 build() slots = slot_map() if "--probes" in arguments: text = "\n".join(probes(slots)) print(text) OUT.mkdir(parents=True, exist_ok=True) (OUT / "closed_box_probes.txt").write_text(text + "\n") return 0 lines = [f"slot of each channel 1 to 6 in the model's sample: {slots}"] lines.append( f"{'stream':28} {'ch':>2} {'lag':>3} {'samples':>7} {'equal':>7} {'by 1':>6} " f"{'more':>6} {'worst':>5}" ) names = [name for name in arguments if not name.startswith("--")] or [*STREAMS, "status"] header = lines.pop() for key_skew in (False, True): lines.append("") lines.append( "TABLE 2: the core is given operator 1's key bit one sample late (alignment 4)" if key_skew else "TABLE 1: the same writes to both" ) lines.append(header) identical = total = samples = equal_samples = 0 for name in names: if name == "status": continue total += 1 stream = STREAMS[name]() clocks, _ = run_model(stream) model = model_slots(clocks, slots) core, _ = run_core(stream, key_skew) stream_equal = True for channel in range(6): if not any(model[channel]) and not any(core[channel]): continue lag, count, equal, within, more, worst = compare(model[channel], core[channel]) stream_equal &= equal == count samples += count equal_samples += equal lines.append( f"{name:28} {channel + 1:>2} {lag:>3} {count:>7} {equal:>7} {within:>6} " f"{more:>6} {worst:>5}" ) identical += stream_equal lines.append( f"streams identical on every channel at their lags: {identical} of {total}; " f"channel samples equal: {equal_samples} of {samples}" ) if "status" in names: stream = s_status() _, model_reads = run_model(stream) _, core_reads = run_core(stream) same = sum( 1 for m, c in zip(model_reads, core_reads, strict=True) if m[1] == c[1] and m[2] == c[2] ) lines.append(f"status reads: {len(model_reads)} made, {same} equal in value and IRQ") busy_model = sum(1 for read in model_reads[:40] if read[1] & 0x80) busy_core = sum(1 for read in core_reads[:40] if read[1] & 0x80) lines.append( f"BUSY after a data write, read at every internal clock: model set for " f"{busy_model} reads, core for {busy_core}" ) for index, (m, c) in enumerate(zip(model_reads, core_reads, strict=True)): if (m[1], m[2]) != (c[1], c[2]): lines.append( f" first differing read: #{index} at clock {m[0]}: model {m}, core {c}" ) break text = "\n".join(lines) print(text) OUT.mkdir(parents=True, exist_ok=True) (OUT / "closed_box.txt").write_text(text + "\n") return 0 if __name__ == "__main__": sys.exit(main())