# SPDX-License-Identifier: MIT """Rung 4: interrupts as software sees them (B4.x in docs/behaviour.md). Timer 1 at the machine clock with AUDF1 = 8: STIMER at T, fire (latch) tick F = T + 15, IRQST first readable at F + 1 = T + 16 (B2.3; Acid800 pokey_irqtiming.s states IRQST bit 0 at STIMER + 16 for this setting). """ from __future__ import annotations import pytest from pokey_python import POKEY from pokey_python.chip import AUDC1, AUDCTL, AUDF1, IRQEN, IRQST, SKCTL, STIMER N = 8 F = 7 + N # the latch tick, counted from the STIMER write (B2.3) def run(writes: dict[int, tuple[int, int]], irqen0: int = 0, cycles: int = 30): """STIMER at cycle 0; ``writes`` maps a cycle to (register, value). Returns IRQST bit 0 as each cycle's read sees it (0 = pending), and the /IRQ level after each tick, cycle by cycle. """ chip = POKEY() for register, value in ((SKCTL, 3), (AUDCTL, 0x40), (AUDF1, N), (AUDC1, 0), (IRQEN, 0)): chip.write(register, value) chip.tick() chip.write(STIMER, 0) chip.tick() seen, line = [1], [chip.irq] for cycle in range(1, cycles): seen.append(chip.read(IRQST) & 1) if cycle == 3 and irqen0: # after any fire the STIMER did not cancel (B2.4) chip.write(IRQEN, irqen0) if cycle in writes: chip.write(*writes[cycle]) chip.tick() line.append(chip.irq) return seen, line def test_irqst_first_reads_at_stimer_plus_16(): # B2.3 (AHRM 5.3; pokey_irqtiming.s's comment: IRQST bit 1 asserted at 16 cycles) seen, line = run({}, irqen0=1) assert seen[F] == 1 and seen[F + 1] == 0 assert not line[F - 1] and line[F] @pytest.mark.parametrize("w", [F - 4, F - 2, F - 1]) def test_enable_up_to_the_cycle_before_the_latch_tick_latches(w): # B4.1: AHRM 5.7 "four cycles before" the acknowledge at A = F + 3 seen, line = run({w: (IRQEN, 1)}) assert seen[F + 1] == 0 and line[F] @pytest.mark.parametrize("w", [F, F + 1]) def test_enable_in_or_after_the_latch_tick_is_lost(w): # B4.1: "or else the interrupt will not be latched in IRQST" seen, line = run({w: (IRQEN, 1)}) assert all(seen[c] == 1 for c in range(F + 1, F + N + 4)) assert not any(line[: F + N + 4]) @pytest.mark.parametrize("w", [F - 2, F - 1, F]) def test_disable_up_to_the_latch_tick_shows_nothing(w): # B4.1, B4.2: a disable at W <= F leaves no visible bit and no pulse on the line seen, line = run({w: (IRQEN, 0)}, irqen0=1) assert all(seen[c] == 1 for c in range(F + 1, 30)) assert not any(line) def test_disable_after_the_latch_tick_shows_the_bit_until_its_tick(): # B4.1: the bit is seen by reads in cycles F+1..W w = F + 2 seen, line = run({w: (IRQEN, 0)}, irqen0=1) assert [seen[c] for c in range(F, w + 3)] == [1, 0, 0, 1, 1] assert [line[c] for c in range(F - 1, w + 2)] == [False, True, True, False, False] def test_a_disabled_source_stays_locked_while_another_latches_with_it(): # B1.4, B4.1: timer 1 (AUDF1 = 0: fires T+7, T+11, ...) and timer 4 (channels 3+4 joined at # 1.79 MHz, AUDF3 = AUDF4 = 0: fires T+10, T+17, ...; B2.6) latch in the same tick at T+31; # only timer 4 is enabled, so IRQST bit 0 stays 1 while bit 2 shows chip = POKEY() for register, value in ((SKCTL, 3), (AUDCTL, 0x68), (AUDF1, 0), (AUDF1 + 4, 0), (AUDF1 + 6, 0), (IRQEN, 0x04)): # fmt: skip chip.write(register, value) chip.tick() chip.write(STIMER, 0) chip.tick() reads = [] for _ in range(40): reads.append(chip.read(IRQST)) chip.tick() assert any(not value & 0x04 for value in reads) assert all(value & 0x01 for value in reads) def test_on_irq_reports_each_change_once(): # B4.2: the line settles once per tick chip = POKEY() changes = [] chip.on_irq = changes.append chip.write(IRQEN, 0x08) # B1.5: the idle serial output asserts the line while enabled chip.tick() chip.write(IRQEN, 0x00) chip.tick() assert changes == [True, False]