# SPDX-License-Identifier: MIT """INPT0-5: the trigger latch, the paddle dump and RC model, keypads (tia.py, controllers.py).""" from __future__ import annotations import math from a2600_python.controllers import CAPACITANCE, R_SERIES, TRIP, Keypads, Paddles from a2600_python.riot import RIOT from a2600_python.tia import TIA, VBLANK INPT0, INPT1, INPT4 = 0x08, 0x09, 0x0C NTSC_CLOCK = 3_579_545.0 def test_fire_latches_low_while_vblank_d6_is_set(): tia = TIA() tia.write(VBLANK, 0x40) tia.run(4) assert tia.read(INPT4) & 0x80 tia.fire[0] = True tia.run(1) tia.fire[0] = False tia.run(1) assert not tia.read(INPT4) & 0x80 # released, but the latch holds it low tia.write(VBLANK, 0x00) tia.run(1) assert tia.read(INPT4) & 0x80 # clearing D6 releases the latch def test_vblank_d7_grounds_the_paddle_pads(): tia = TIA() assert tia.read(INPT0) & 0x80 tia.write(VBLANK, 0x80) assert not tia.read(INPT0) & 0x80 def test_a_paddle_trips_after_its_rc_time(): tia = TIA() paddles = Paddles(tia, NTSC_CLOCK) paddles.position[0] = 0.5 tia.write(VBLANK, 0x80) tia.run(10) tia.write(VBLANK, 0x00) steps = paddles.trip_steps(0) # RC (-ln(1 - TRIP)) for the trip point; nocash's table puts 500 kOhm at 190 lines. expected = (R_SERIES + 0.5e6) * CAPACITANCE * -math.log(1 - TRIP) * 2 * NTSC_CLOCK assert abs(expected / (2 * 228) - 190) < 1 assert abs(steps - expected) < 1 tia.run(int(steps / 2) - 2) assert not tia.read(INPT0) & 0x80 tia.run(4) assert tia.read(INPT0) & 0x80 def test_a_paddle_turned_further_takes_longer(): tia = TIA() paddles = Paddles(tia, NTSC_CLOCK) paddles.position[1] = 0.2 short = paddles.trip_steps(1) paddles.position[1] = 0.8 assert paddles.trip_steps(1) > 3 * short def test_a_keypad_key_pulls_its_column_when_its_row_is_driven_low(): tia, riot = TIA(), RIOT(0, 0, 1024, 1024) keypads = Keypads(tia, riot) keypads.pressed[0].add((1, 1)) # left keypad, second row, middle column: "5" riot.write(0x281, 0xF0, 0) # SWACNT: the left port's four row lines are outputs riot.write(0x280, 0xFF, 1) # no row driven low assert tia.read(INPT1) & 0x80 riot.write(0x280, 0xDF, 2) # D5, the second row, low assert not tia.read(INPT1) & 0x80 assert tia.read(INPT0) & 0x80 and tia.read(INPT4) & 0x80 # other columns stay high riot.write(0x280, 0xEF, 3) # the first row instead assert tia.read(INPT1) & 0x80