# SPDX-License-Identifier: MIT """Rung 6: the keyboard scan (B6.x in docs/behaviour.md), from AHRM 5.8.""" from __future__ import annotations from pokey_python import POKEY from pokey_python.chip import IRQEN, IRQST, KBCODE, SKCTL, SKRES, SKSTAT LINE = 114 def scanning(skctl: int = 3, irqen: int = 0xC0) -> POKEY: """SKCTL written at T; returns after tick T+80, the first step (scan code $00).""" chip = POKEY() chip.write(IRQEN, irqen) chip.tick() chip.write(SKCTL, skctl) chip.tick() chip.run(80) return chip def steps(chip: POKEY, n: int) -> None: chip.run(n * LINE) def test_scan_counter_steps_on_the_15khz_clock(): # B6.1, B6.2 (B2.1b: first 15 kHz pulse in tick T+80) chip = POKEY() chip.write(SKCTL, 3) chip.tick() chip.run(79) assert chip.scan_code == 0x3F chip.tick() assert chip.scan_code == 0x00 steps(chip, 1) assert chip.scan_code == 0x01 steps(chip, 64) assert chip.scan_code == 0x01 def test_debounced_key_press_and_release(): # B6.4: registered on the second scan, released after two scans without it chip = scanning() held = {0x15} chip.key_response = lambda code: 1 if code in held else 0 steps(chip, 0x15) # first pass over $15: state 1 assert chip.read(IRQST) & 0x40 and chip.read(SKSTAT) & 0x04 steps(chip, 64) # second pass: registered assert not chip.read(IRQST) & 0x40 assert chip.read(KBCODE) == 0x15 and not chip.read(SKSTAT) & 0x04 held.clear() steps(chip, 64) assert not chip.read(SKSTAT) & 0x04 # state 2 steps(chip, 64) assert chip.read(SKSTAT) & 0x04 # state 0: released def test_two_keys_never_register_with_debounce(): # AHRM 5.8 "Keyboard debounce": two keys down at once are rejected chip = scanning() chip.key_response = lambda code: 1 if code in (0x08, 0x2A) else 0 steps(chip, 64 * 4) assert chip.read(IRQST) & 0x40 and chip.read(SKSTAT) & 0x04 def test_5200_keypad_aliasing_with_debounce_off(): # B6.7: K0 and K5 unconnected: the key answers at $0C, $0D, $2C, $2D; registered at $0D, # then again 32 steps later at $2D, released in between chip = scanning(skctl=0x02, irqen=0x40) chip.key_response = lambda code: 1 if code & 0x1E == 0x0C else 0 codes = [] held = [] for _ in range(128): steps(chip, 1) if not chip.read(IRQST) & 0x40: codes.append((chip.scan_code, chip.read(KBCODE))) chip.write(IRQEN, 0) # acknowledge, as a handler does chip.tick() chip.write(IRQEN, 0x40) chip.tick() held.append(not chip.read(SKSTAT) & 0x04) assert [kb for _, kb in codes] == [0x0D, 0x2D, 0x0D, 0x2D] assert codes[1][0] - codes[0][0] == 32 assert any(held) and not all(held) def test_shift_control_and_kbcode_bits(): # B6.6: Shift at $10 and Control at $00 on /KR2; SKSTAT bit 3 live, KBCODE bits 6-7 latched chip = scanning() chip.key_response = lambda code: (2 if code in (0x00, 0x10) else 0) | (1 if code == 0x3F else 0) steps(chip, 64 * 3) assert not chip.read(SKSTAT) & 0x08 assert chip.read(KBCODE) == 0xFF def test_break_interrupts_once_per_press(): # B6.6: a down transition at $30 on /KR2; no repeat while held chip = scanning() chip.key_response = lambda code: 2 if code == 0x30 else 0 steps(chip, 0x30) assert not chip.read(IRQST) & 0x80 chip.write(IRQEN, 0x40) chip.tick() chip.write(IRQEN, 0xC0) chip.tick() steps(chip, 64 * 2) assert chip.read(IRQST) & 0x80 # still held: no new interrupt def test_keyboard_overrun(): # B6.5: a second key registered while IRQST bit 6 is pending clears SKSTAT bit 6 chip = scanning() held = {0x05} chip.key_response = lambda code: 1 if code in held else 0 steps(chip, 64 * 2) assert chip.read(SKSTAT) & 0x40 held.clear() steps(chip, 64 * 2) held.add(0x22) steps(chip, 64 * 2) assert chip.read(KBCODE) == 0x22 and not chip.read(SKSTAT) & 0x40 chip.write(SKRES, 0) chip.tick() assert chip.read(SKSTAT) & 0x40 def test_disabling_the_scan_resets_the_state_machine(): # B6.2: SKSTAT bit 2 back to 1, counter held at $3F, KBCODE kept chip = scanning() chip.key_response = lambda code: 1 if code == 0x09 else 0 steps(chip, 64 * 2) assert not chip.read(SKSTAT) & 0x04 chip.write(SKCTL, 0x01) chip.tick() chip.tick() # B0.2: the tick after the write's applies it assert chip.read(SKSTAT) & 0x04 and chip.scan_code == 0x3F and chip.read(KBCODE) == 0x09 def test_a_held_key_registers_again_after_the_scan_is_re_enabled(): # B6.2: disabling forces state 0, so a key still held is registered afresh chip = scanning() chip.key_response = lambda code: 1 if code == 0x09 else 0 steps(chip, 64 * 2) chip.write(IRQEN, 0) chip.tick() chip.write(IRQEN, 0xC0) chip.tick() steps(chip, 64) assert chip.read(IRQST) & 0x40 # held: no new interrupt chip.write(SKCTL, 0x01) chip.tick() chip.tick() chip.write(SKCTL, 0x03) chip.tick() steps(chip, 64 * 3) assert not chip.read(IRQST) & 0x40