# SPDX-License-Identifier: MIT """Rung 2: the hardware numbers of docs/timing.md as tests (rules B2.x). Convention (B0.1): ``cycle(chip, register, value)`` performs one bus access and then the tick of that cycle; a read returns the state the previous tick left. """ from __future__ import annotations import pytest from pokey_python import POKEY from pokey_python.chip import ( AUDC1, AUDCTL, AUDF1, AUDF2, IRQEN, IRQST, RANDOM, SKCTL, STIMER, ) # fmt: skip def write(chip: POKEY, register: int, value: int) -> None: chip.write(register, value) chip.tick() def read(chip: POKEY, register: int) -> int: value = chip.read(register) chip.tick() return value def idle(chip: POKEY, cycles: int) -> None: chip.run(cycles) def first_irq(chip: POKEY, bit: int, limit: int = 2000) -> int: """Reads of IRQST from the next cycle on; return k for the first read at T+k showing ``bit``.""" for k in range(1, limit): if not read(chip, IRQST) & bit: return k raise AssertionError("no interrupt") def running(audctl: int, irqen: int = 0x07) -> POKEY: chip = POKEY() write(chip, AUDCTL, audctl) write(chip, IRQEN, irqen) write(chip, SKCTL, 3) idle(chip, 50) return chip def stimer_then_irq(audctl: int, audf1: int, audf2: int = 0, bit: int = 1) -> int: """IRQST cycle k after a STIMER at T (k = 1 is the cycle after the write).""" chip = running(audctl) armed_stimer(chip, audf1, audf2) # cycle T; the next read is at T+1 return first_irq(chip, bit) def armed_stimer(chip: POKEY, audf1: int, audf2: int = 0) -> None: """The sequence of scripts/run_vhdl.py's timer probe, ending with STIMER at cycle T. A first STIMER with AUDF = $FF stops the old period; IRQEN is cleared and set while nothing can fire; then the AUDF values and the STIMER of record. """ write(chip, AUDF1, 0xFF) write(chip, AUDF2, 0xFF) write(chip, STIMER, 0) write(chip, IRQEN, 0) write(chip, IRQEN, 0x07) write(chip, AUDF1, audf1) write(chip, AUDF2, audf2) write(chip, STIMER, 0) @pytest.mark.parametrize("n", [0, 1, 5, 40]) def test_stimer_to_irqst_at_179mhz(n): # B2.3: AHRM 5.3 "Reload timing", Figure 8: T+8+N assert stimer_then_irq(0x40, n) == 8 + n @pytest.mark.parametrize("n", [0, 1, 5, 255]) def test_period_at_179mhz(n): # B2.2, B2.3: AHRM 5.3: first fire in tick T+7+N, then every N + 4 chip = running(0x40) write(chip, AUDF1, n) write(chip, STIMER, 0) start = chip.clock # the tick of cycle T has run ticks = [t - start for t in fire_ticks(chip, 0, 4 * (n + 4) + 12) if t - start > 3] assert ticks[0] == 7 + n assert {b - a for a, b in zip(ticks, ticks[1:])} == {n + 4} def test_linked_pair_cycles(): # B2.6: AHRM 5.3 Figure 8: timer 1 at T+8, timer 2 at T+11 (AUDF1 = AUDF2 = 0) assert stimer_then_irq(0x50, 0, 0, bit=1) == 8 assert stimer_then_irq(0x50, 0, 0, bit=2) == 11 def fire_ticks(chip: POKEY, channel: int, cycles: int) -> list[int]: ticks = [] for _ in range(cycles): chip.tick() if chip.fired[channel]: ticks.append(chip.clock) return ticks @pytest.mark.parametrize("low,high", [(0, 0), (0x28, 0), (5, 1), (0xFF, 2)]) def test_linked_period_n_plus_7(low, high): # B2.6: AHRM 5.3: N + 7 at 1.79 MHz, N = AUDF1 + 256 AUDF2; low fires AUDF2 + 1 times n = low + 256 * high chips = [] for channel in (1, 0): chip = running(0x50) write(chip, AUDF1, low) write(chip, AUDF2, high) write(chip, STIMER, 0) start = chip.clock ticks = fire_ticks(chip, channel, 5 * (n + 7) + 12) chips.append([t - start for t in ticks if t - start > 3]) highs, lows = chips assert highs[0] == 10 + n # B2.6: T+10 for N = 0 (IRQST at T+11) assert {b - a for a, b in zip(highs, highs[1:])} == {n + 7} assert len([t for t in lows if highs[0] < t <= highs[1]]) == high + 1 @pytest.mark.parametrize("audctl,n,period", [(0x00, 0, 28), (0x00, 9, 280), (0x01, 0, 114), (0x01, 2, 342)]) def test_slow_clock_periods(audctl, n, period): # B2.2: AHRM 5.3: (N + 1) x 28 or x 114 chip = running(audctl) write(chip, AUDF1, n) write(chip, STIMER, 0) ticks = fire_ticks(chip, 0, 4 * period + 200) assert {b - a for a, b in zip(ticks, ticks[1:])} == {period} @pytest.mark.parametrize("audctl,expected", [(0x00, 25), (0x01, 84)]) def test_first_slow_interrupt_after_init(audctl, expected): # B2.1b: Acid800 pokey_inittiming.s: period-0 timer on that clock, IRQST first reads it # 25 / 84 cycles after the SKCTL write (the AHRM 5.2 sentence says 24 / 83: C10) chip = POKEY() write(chip, AUDCTL, audctl) write(chip, STIMER, 0) write(chip, IRQEN, 0x01) idle(chip, 20) write(chip, SKCTL, 3) assert first_irq(chip, 1) == expected def test_stimer_preemption_deadline(): # B2.4: AHRM 5.3 "STIMER preemption": a second STIMER at T+4 blocks the IRQ due at T+8 for again, blocked in ((4, True), (5, False)): chip = running(0x40) armed_stimer(chip, 0) idle(chip, again - 1) write(chip, STIMER, 0) # at T+again statuses = [read(chip, IRQST) & 1 for _ in range(8 - again - 1)] # T+again+1 .. T+7 assert all(statuses) assert bool(read(chip, IRQST) & 1) == blocked # T+8 if blocked: # the reload in tick T+7 starts a new period: next IRQST at T+12, nothing before assert [read(chip, IRQST) & 1 for _ in range(3)] == [1, 1, 1] # T+9 .. T+11 assert not read(chip, IRQST) & 1 # T+12 def test_timer4_and_channel3_fast_clock(): # B2.5, B2.6, B2.3: channel 3 on 1.79 MHz (AUDCTL bit 5) joined to 4 (bit 3): timer 4 like timer 2 assert stimer_then_irq(0x28, 0, 0, bit=4) == 11 chip = running(0x20) write(chip, AUDF1 + 4, 5) # AUDF3 write(chip, STIMER, 0) start = chip.clock ticks = [t - start for t in fire_ticks(chip, 2, 40) if t - start > 3] assert ticks[:2] == [12, 21] def test_audf_deadline_first_period(): # B2.3: AHRM 5.3 "Reload timing": the last AUDF1 write for the first period is T+2 for at, n in ((2, 5), (3, 0)): chip = running(0x40) armed_stimer(chip, 0) idle(chip, at - 1) write(chip, AUDF1, 5) assert first_irq(chip, 1) + at == 8 + n def test_random_after_init_9bit(): # B2.8: AHRM 5.5: $FF $7F $3F $1F at T+1..T+4 (back-to-back STA SKCTL / LDA RANDOM = $1F) chip = POKEY() write(chip, AUDCTL, 0x80) idle(chip, 30) write(chip, SKCTL, 3) assert [read(chip, RANDOM) for _ in range(4)] == [0xFF, 0x7F, 0x3F, 0x1F] @pytest.mark.parametrize("audctl,expected", [(0x80, 0x95), (0x00, 0x08)]) def test_random_114_after_init(audctl, expected): # B2.7: Acid800 pokey_noise.s: RANDOM read 114 cycles after SKCTL = 3 chip = POKEY() write(chip, AUDCTL, audctl) idle(chip, 30) write(chip, SKCTL, 3) idle(chip, 113) assert read(chip, RANDOM) == expected def test_random_once_per_line_run(): # B2.7: AHRM 5.5: 9-bit, one read per 114 cycles includes 00 DF EE 16 B9 chip = POKEY() write(chip, AUDCTL, 0x80) write(chip, SKCTL, 3) values = [] for _ in range(511 + 5): values.append(read(chip, RANDOM)) idle(chip, 113) text = bytes(values).hex() assert "00dfee16b9" in text def test_init_mode_random_ff_after_nine(): # B2.8: AHRM 5.5: RANDOM reads $FF from nine cycles after entering init mode chip = running(0x00) idle(chip, 300) write(chip, SKCTL, 0) seen = [read(chip, RANDOM) for _ in range(12)] assert seen[8:] == [0xFF] * 4 # reads at T+9 .. T+12 def test_stimer_does_not_move_slow_clock_phase(): # B2.4: AHRM 5.3: STIMER has no effect on the 15/64 kHz phase chip = running(0x00) write(chip, AUDF1, 0) a = fire_ticks(chip, 0, 200) write(chip, STIMER, 0) b = fire_ticks(chip, 0, 200) assert (b[-1] - a[0]) % 28 == 0 @pytest.mark.parametrize( "name,stage,pattern", [ ("poly4", 5, "000011101100101"), # B2.7: AHRM 5.5 (the same from a real chip: Plogue) ("poly5", 5, "0000011100100010101111011010011"), # B2.7: AHRM 5.5 ], ) def test_generator_patterns_after_init(name, stage, pattern): # B2.7, B2.8: from a zeroed register the output end shows the manual's pattern chip = POKEY() idle(chip, 20) write(chip, SKCTL, 3) bits = "" for _ in range(2 * len(pattern)): bits += str(getattr(chip, name) >> stage & 1) chip.tick() assert bits == pattern * 2 def test_no_slow_clock_in_init_mode(): # B1.7, B2.1: AHRM 5.2: the 15/64 kHz timers do not count in init mode chip = POKEY() write(chip, AUDC1, 0) assert fire_ticks(chip, 0, 500) == []