# SPDX-License-Identifier: MIT """A stand-in that times things the way Nuked-PSG's OUTPUT was seen to: a measuring tool. This is not part of the core and nothing imports it but ``closed_box.py``. Its only use is to answer one question: are the differences between the core and the closed box all accounted for by the few behaviours named in docs/validation.md, or is there something else? If the core, changed in just those ways, matches the box at every tick of every stream, the list is complete. Each behaviour below was inferred from the box's output on streams written for the purpose (``closed_box.py --sweeps`` reproduces the tables). None was learned from the model's source, which was not read (docs/restart.md rule 8), and none is backed by a document: they are a detector's opinion on spec unknowns U6, U7, U9 and U11, and the core does not adopt them. 1. A write to a tone value or to the noise control reaches the generators one tick later than the core applies it: the tick after the write still runs on the old value. (An attenuation write shows at once in both.) 2. A tone value governs the half-cycle in progress, not only the next one: the half-cycle ends when the ticks it has lasted reach the value in force, at once if they already have. The core, following Maxim's wording (spec 5.1, 5.4), reloads a down counter only when it runs out. The fixed-rate noise divider behaves the same way. 3. At noise rate 3 the register shifts one tick after the third tone's output rises, where the core shifts on the tick it rises. """ from __future__ import annotations from sn76489_python import CLOCKS_PER_SAMPLE, SN76489 class AsObserved(SN76489): """The core with the three timing behaviours above. Slow and plain on purpose.""" def reset(self, *, silent_start: bool = False) -> None: super().reset(silent_start=silent_start) self.elapsed = [0, 0, 0] # ticks each tone's half-cycle has lasted self.noise_elapsed = 0x7F # so the divider turns over on the first tick, as the core's self.live_tone = [0, 0, 0] # the values the generators see (behaviour 1) self.live_noise = 0 self.pending_noise_reset = False self.tone2_rose = False def write(self, value: int) -> None: before = self.lfsr self.lfsr = -1 super().write(value) if self.lfsr != -1: # A noise write: its restart of the register waits a tick (behaviour 1). self.pending_noise_reset = True self.lfsr = before def run(self, clocks: int) -> None: total = self.clock_remainder + clocks ticks, self.clock_remainder = divmod(total, CLOCKS_PER_SAMPLE) variant = self.variant top = variant.lfsr_bits - 1 for _ in range(ticks): shift = self.tone2_rose and self.live_noise & 3 == 3 # behaviour 3 self.tone2_rose = False for channel in range(3): self.elapsed[channel] += 1 if self.elapsed[channel] >= (self.live_tone[channel] or 1): # behaviour 2 self.elapsed[channel] = 0 self.outputs[channel] ^= 1 if channel == 2 and self.outputs[2] == 1: self.tone2_rose = True if self.live_noise & 3 != 3: self.noise_elapsed += 1 if self.noise_elapsed >= 0x10 << (self.live_noise & 3): self.noise_elapsed = 0 self.noise_input ^= 1 shift = self.noise_input == 1 if shift: taps = variant.white_noise_taps if self.live_noise & 4 else 1 self.lfsr = (self.lfsr >> 1) | (((self.lfsr & taps).bit_count() & 1) << top) self.samples.append(self._mix()) # Behaviour 1: what was written before this tick takes hold after it, # and after this tick's level has been given. self.live_tone = list(self.tone) self.live_noise = self.noise if self.pending_noise_reset: self.pending_noise_reset = False self.lfsr = 1 << top def _mix(self) -> tuple[float, float]: table = self.variant.volume_table bits = [*self.outputs, self.lfsr & 1] level = sum(table[self.volume[channel]] for channel in range(4) if bits[channel]) return (level, level)