# SPDX-License-Identifier: MIT """Rung 5: the pot scan (B5.x in docs/behaviour.md), from AHRM 5.9's numbers.""" from __future__ import annotations from pokey_python import POKEY from pokey_python.chip import ALLPOT, POTGO, SKCTL LIVE = [0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x04, 0x06, 0x00, 0x08, 0x08, 0x0A, 0x08, 0x0C, 0x0C, 0x0E, 0x00] # fmt: skip AHRM 5.9 "Reading live counters" def step(chip: POKEY, register: int | None = None, value: int = 0) -> None: if register is not None: chip.write(register, value) chip.tick() def fast_chip() -> POKEY: chip = POKEY() step(chip, SKCTL, 0x07) # fast scan, out of init mode step(chip) return chip def test_fast_scan_live_reads(): # B5.1, B5.2, B5.5: the 17 values read from the cycle after POTGO chip = fast_chip() step(chip, POTGO) seen = [] for _ in range(17): seen.append(chip.read(0)) chip.tick() assert seen == LIVE def test_fast_scan_end(): # B5.4: AHRM 5.9 "Scan timing": 229 latched, ALLPOT $00 from 231 cycles after POTGO chip = fast_chip() step(chip, POTGO) for _ in range(229): # cycles W+1 .. W+229 chip.tick() assert chip.read(ALLPOT) == 0xFF and chip.read(0) == 0xE4 # W+230: transitional 228 AND 229 chip.tick() assert chip.read(ALLPOT) == 0x00 and chip.read(0) == 0xE5 # W+231 chip.run(300) assert chip.read(0) == 0xE5 and chip.read(ALLPOT) == 0 def test_slow_scan_first_increment_and_end(): # B5.2, B5.4, B5.5: AHRM 5.9 "Scan timing": first increment 81 cycles after the SKCTL write that # leaves init mode (transitional $00, then $01); 227 x 114 later $E0, then $E4, then ALLPOT $00 chip = POKEY() step(chip, POTGO) chip.run(5) step(chip, SKCTL, 3) # T chip.run(80) # ticks T+1 .. T+80 assert chip.read(0) == 0x00 # T+81: 0 AND 1 chip.tick() assert chip.read(0) == 0x01 # T+82 chip.run(113) assert chip.read(0) == 0x00 # T+81+114: 1 AND 2 chip.tick() assert chip.read(0) == 0x02 chip.run(227 * 114 - 115) # to cycle I = T+81+227*114 assert chip.read(0) == 0xE0 and chip.read(ALLPOT) == 0xFF chip.tick() assert chip.read(0) == 0xE4 and chip.read(ALLPOT) == 0xFF chip.tick() assert chip.read(0) == 0xE4 and chip.read(ALLPOT) == 0x00 def test_crossings_latch_on_any_cycle_and_resume(): # B5.3: a pin above threshold holds its register; falling back below resumes updating chip = fast_chip() above = set() chip.pot_level = lambda n: n in above step(chip, POTGO) chip.run(50) # counter 49 after tick W+50 above.add(3) chip.tick() # tick W+51 steps to 50; pot 3 is above threshold and keeps 49 assert chip.read(ALLPOT) == 0xF7 and chip.read(3) == 49 chip.run(20) assert chip.read(3) == 49 above.clear() chip.tick() assert chip.read(ALLPOT) == 0xFF and chip.pot[3] == chip.pot_count == 71 def test_pins_already_above_threshold_never_update(): # AHRM 5.9 "Truncated scans": inputs over threshold at the restart are not updated at all chip = fast_chip() chip.pot[5] = 0x55 chip.pot_level = lambda n: n == 5 step(chip, POTGO) chip.run(300) assert chip.read(5) == 0x55 and chip.read(ALLPOT) == 0 def test_slow_scan_suspended_in_init_mode(): # B5.2: AHRM 5.9 "Suspended scans": init mode stops the 15 kHz clock chip = POKEY() step(chip, SKCTL, 3) step(chip, POTGO) chip.run(1000) held = chip.read(0) step(chip, SKCTL, 0) chip.run(1000) assert chip.read(0) == held and chip.read(ALLPOT) == 0xFF def test_dump_transistors(): # B5.6: on while slow scan is selected and no scan runs; off during a scan and in fast scan chip = POKEY() changes = [] chip.on_dump = changes.append assert chip.dump_active step(chip, SKCTL, 3) step(chip, POTGO) assert chip.dump_active # the scan starts in the next tick chip.tick() assert not chip.dump_active step(chip, SKCTL, 0x07) step(chip, POTGO) chip.run(240) # the fast scan ends assert not chip.dump_active # fast scan disables them step(chip, SKCTL, 0x03) assert chip.dump_active assert changes == [False, True] def test_snapshot_keeps_the_scan(): chip = fast_chip() step(chip, POTGO) chip.run(40) other = POKEY() other.restore(chip.snapshot()) chip.run(10) other.run(10) assert other.read(0) == chip.read(0) and other.snapshot() == chip.snapshot() def test_rc_helper_gives_a_paddle_count(): # B5.8: a target of 100 lines of charging reads about 100 in a slow scan; then a second # scan after the dump transistors have drained the capacitor reads the same from pokey_python.rc import PotRC chip = POKEY() rc = PotRC(chip, [114 * 100] + [None] * 7) chip.pot_level = rc.level chip.on_dump = rc.dumped step(chip, SKCTL, 3) for _ in range(2): step(chip, POTGO) chip.run(229 * 114 + 400) # the scan ends, then the dump transistors drain the pin assert 99 <= chip.read(0) <= 101 assert chip.read(1) == 228 # unconnected: never crosses def test_the_chip_never_imports_the_rc_helper(): # decision 4: rc.py is board-side import ast from pathlib import Path source = Path(__file__).resolve().parents[1] / "src" / "pokey_python" / "chip.py" names = [n.module for n in ast.walk(ast.parse(source.read_text())) if isinstance(n, ast.ImportFrom)] assert not any(name and name.endswith("rc") for name in names)