# SPDX-License-Identifier: MIT """Rung 3 (in progress): the output stage's documented rules (B3.x).""" from __future__ import annotations import pytest from pokey_python import LINEAR, POKEY from pokey_python.chip import AUDC1, AUDC2, AUDC3, AUDC4, AUDCTL, AUDF1, AUDF3, SKCTL, STIMER def write(chip: POKEY, register: int, value: int) -> None: chip.write(register, value) chip.tick() def test_stimer_output_polarity_c2(): # B3.7, B3.6: AHRM 5.3: after STIMER, channels 1 and 2 off, 3 and 4 on (high-pass off) chip = POKEY() for register in (AUDC1, AUDC2, AUDC3, AUDC4): write(chip, register, 0xAF) write(chip, STIMER, 0) chip.run(3) assert chip.levels() == (0, 0, 15, 15) def test_volume_only_overrides_after_high_pass(): # B3.8: AHRM 5.3: volume-only channels add in any combination chip = POKEY() for register in (AUDC1, AUDC2, AUDC3, AUDC4): write(chip, register, 0x1F) assert chip.levels() == (15, 15, 15, 15) def test_square_toggles_each_fire(): # B3.5, B3.1: pure tone at 1.79 MHz, AUDF1 = 0: the output changes every 4 cycles chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDC1, 0xA1) write(chip, SKCTL, 3) seen = [] for _ in range(40): chip.tick() seen.append(chip.levels()[0]) edges = [i for i in range(1, 40) if seen[i] != seen[i - 1]] assert {b - a for a, b in zip(edges, edges[1:])} == {4} def test_noise_is_zero_in_init_mode(): # B3.3: AHRM 5.5: the audio sees 0 from the 4-bit and 9/17-bit generators in init mode for audc in (0xC1, 0x81): chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDC1, audc) chip.run(100) assert chip.ff[0] == 0 FIVE = "0000011100100010101111011010011" # AHRM 5.5, the 5-bit pattern FOUR = "000011101100101" # AHRM 5.5; the same from a real chip [Plogue] # Plogue: a real C012294 clocked slowly, channel 1 at the machine clock, AUDF1 = 0, the # poly-5-gated square: the output level once per period repeats with this 62-bit pattern. PLOGUE_5BIT_SQUARE = "00000010111000011001010010011101111110100011110011010110110001" def is_rotation(seq: str, pattern: str) -> bool: return len(seq) == len(pattern) and seq in pattern * 2 def is_sampled(seq: str, pattern: str, stride: int) -> bool: """``seq`` is ``pattern`` read every ``stride`` bits from some starting bit (AHRM 5.3, "Noise sampling artifacts": when the two periods share a factor the start matters).""" n = len(pattern) return any( seq == "".join(pattern[(start + k * stride) % n] for k in range(len(seq))) for start in range(n) ) def per_fire(audc: int, audf: int, fires: int, field: str = "level") -> str: """Channel 1 at the machine clock: its output level (or toggle) once per period.""" chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDF1, audf) write(chip, AUDC1, audc) write(chip, SKCTL, 3) chip.run(200) out = [] for _ in range(300 * (fires + 1)): if len(out) > fires: break chip.tick() if chip.fired[0]: chip.run(2) # B3.1: the output changes in the tick after the fire out.append(1 if chip.levels()[0] else 0) if field == "toggle": return "".join(str(out[i] ^ out[i - 1]) for i in range(1, len(out))) return "".join(str(v) for v in out[1:]) def test_5bit_gated_square_is_the_hardware_recording(): # B3.2, B3.5 TEST (hardware recording, [Plogue]): AUDF1 = 0, the 62-bit level pattern levels = per_fire(0x21, 0, 62) assert is_rotation(levels, PLOGUE_5BIT_SQUARE) @pytest.mark.parametrize("audf", [0, 1, 2, 3, 4]) def test_5bit_gate_samples_every_period(audf): # B3.2 [Plogue]: the toggles are the 5-bit pattern (1 = pass) taken every N + 4 bits toggles = per_fire(0x21, audf, 31, "toggle") assert is_sampled(toggles, FIVE, audf + 4) @pytest.mark.parametrize("audf", [0, 1, 2, 3, 4]) def test_4bit_noise_samples_every_period(audf): # B3.3 [Plogue], AHRM 5.5: the 4-bit pattern taken every N + 4 bits; channel 1's # level is the flip-flop XOR the disabled high-pass flip-flop (1), so inverted levels = per_fire(0xC1, audf, 15) assert is_sampled(levels, FOUR.translate(str.maketrans("01", "10")), audf + 4) def test_gate_opens_while_ungated(): # B3.2 OBSERVED (closed box): a channel left ungated for a cycle passes its next fire # even when it is gated again before that fire and its 5-bit bit is 0 chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDF1, 26) # period 30, against the 5-bit's 31 write(chip, AUDC1, 0x21) write(chip, SKCTL, 3) chip.run(100) for _ in range(2000): if chip.fired[0] and chip.gate5[0] == 0: break chip.tick() assert chip.fired[0] and chip.gate5[0] == 0 write(chip, AUDC1, 0xA1) write(chip, AUDC1, 0x21) assert chip.gate5[0] == 1 def gated_square_channel1(audf: int = 26) -> POKEY: chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDF1, audf) write(chip, AUDC1, 0x21) write(chip, SKCTL, 3) chip.run(100) return chip def bit5(chip: POKEY, ch: int = 0) -> int: return (chip.poly5 >> (chip.TAP5 - ch)) & 1 def test_gate_load_counts_a_write_in_the_fire_cycle(): # B3.2 OBSERVED (closed box): an AUDC write made in the fire's own cycle counts for # the load: ungated -> gated in that cycle loads the 5-bit bit, not "open" chip = gated_square_channel1() write(chip, AUDC1, 0xA1) for _ in range(2000): # the cycle before a fire whose load would take a 0 probe = POKEY() probe.restore(chip.snapshot()) probe.write(AUDC1, 0x21) probe.tick() if probe.fired[0] and probe.gate5[0] == 0: break chip.tick() chip.write(AUDC1, 0x21) chip.tick() assert chip.fired[0] and chip.gate5[0] == 0 def test_gate_is_open_in_init_mode(): # B3.2 DOC: AHRM 5.5, the 5-bit generator is a constant 1 in init mode: a gated # square at the machine clock toggles on every fire (the closed box does not: D6) chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDC1, 0x21) seen = [] for _ in range(80): chip.tick() seen.append(chip.ff[0]) assert sum(seen[i] != seen[i - 1] for i in range(1, 80)) >= 19 def test_stimer_loads_the_gates_in_t_plus_2(): # B3.2 OBSERVED (closed box, the first STIMER after init mode): the gates take the # 5-bit bits of tick T+2 for wait in range(0, 31, 5): chip = gated_square_channel1(200) # AUDC2-4 = 0: gated too chip.run(wait) write(chip, STIMER, 0) # T chip.tick() # T+1 chip.tick() # T+2 assert chip.gate5 == [bit5(chip, ch) for ch in range(4)] def test_high_pass_copies_the_flip_flop_before_its_clock(): # B3.6 OBSERVED (closed box, the manual's $3B/$3C case): channel 3 firing one tick # after channel 1 copies channel 1's flip-flop as it stood before that tick's toggle, # so channel 1's output stays on chip = POKEY() write(chip, AUDCTL, 0x64) # channels 1 and 3 at the machine clock, high-pass 1 by 3 write(chip, AUDC1, 0xAF) write(chip, SKCTL, 3) write(chip, AUDF1, 0) write(chip, AUDF3, 1) write(chip, STIMER, 0) chip.run(3) write(chip, AUDF3, 0) # channel 3: one period of 5, then 4: one tick behind channel 1 chip.run(20) seen = set() for _ in range(40): chip.tick() seen.add(chip.levels()[0]) assert seen == {15} def test_stimer_holds_the_flip_flops_two_ticks(): # B3.7 OBSERVED (closed box): a fire in tick T+2 of a STIMER does not toggle chip = POKEY() write(chip, AUDCTL, 0x40) write(chip, AUDC1, 0xA1) write(chip, SKCTL, 3) chip.run(50) for _ in range(8): if chip.fired[0]: break chip.tick() chip.run(1) # three ticks before the next fire (period 4): write STIMER now write(chip, STIMER, 0) # T; the fire comes in tick T+2 assert not chip.fired[0] chip.tick() chip.tick() assert chip.fired[0] chip.run(3) assert chip.ff == [1, 1, 1, 1] def test_mix_curve_meets_the_manuals_words(): # B3.9 FITTED: f(30) = 1.5 f(15); f(60) about 2 f(15); linear to 15 from pokey_python import C012294 def f(total: float) -> float: return C012294.mix_curve[int(total * 4 + 0.5)] assert abs(f(30) / f(15) - 1.5) < 0.01 assert 1.9 < f(60) / f(15) < 2.1 assert abs(f(10) / f(5) - 2.0) < 1e-9 steps = [C012294.dac[v + 1] - C012294.dac[v] for v in range(15)] assert steps[3] > steps[2] and steps[7] > steps[6] and steps[11] > steps[10] chip = POKEY(LINEAR) for register in (AUDC1, AUDC2, AUDC3, AUDC4): write(chip, register, 0x1F) assert chip.mix() == 1.0