// SPDX-License-Identifier: MIT //! INPT0-5: the trigger latch, the paddle dump and RC model, keypads (the //! reference's tests/test_inputs.py). use std::cell::RefCell; use std::rc::Rc; use a2600::controllers::{trip, Keypads, Paddles, CAPACITANCE, R_SERIES}; use a2600::riot::RIOT; use a2600::tia::{TIA, VBLANK}; const INPT0: u16 = 0x08; const INPT1: u16 = 0x09; const INPT4: u16 = 0x0C; const NTSC_CLOCK: f64 = 3_579_545.0; #[test] fn fire_latches_low_while_vblank_d6_is_set() { let mut tia = TIA::default(); tia.write(VBLANK as u8, 0x40); tia.run(4); assert_ne!(tia.read(INPT4) & 0x80, 0); tia.fire[0] = true; tia.run(1); tia.fire[0] = false; tia.run(1); assert_eq!(tia.read(INPT4) & 0x80, 0); // released, but the latch holds it low tia.write(VBLANK as u8, 0x00); tia.run(1); assert_ne!(tia.read(INPT4) & 0x80, 0); // clearing D6 releases the latch } #[test] fn vblank_d7_grounds_the_paddle_pads() { let mut tia = TIA::default(); assert_ne!(tia.read(INPT0) & 0x80, 0); tia.write(VBLANK as u8, 0x80); assert_eq!(tia.read(INPT0) & 0x80, 0); } #[test] fn a_paddle_trips_after_its_rc_time() { let mut tia = TIA::default(); let paddles = Paddles::new(&mut tia, NTSC_CLOCK); paddles.position.borrow_mut()[0] = Some(0.5); tia.write(VBLANK as u8, 0x80); tia.run(10); tia.write(VBLANK as u8, 0x00); let steps = paddles.trip_steps(0); // RC (-ln(1 - TRIP)) for the trip point; nocash's table puts 500 kOhm at 190 lines. let expected = (R_SERIES + 0.5e6) * CAPACITANCE * -(1.0 - trip()).ln() * 2.0 * NTSC_CLOCK; assert!((expected / (2.0 * 228.0) - 190.0).abs() < 1.0); assert!((steps - expected).abs() < 1.0); tia.run((steps / 2.0) as u32 - 2); assert_eq!(tia.read(INPT0) & 0x80, 0); tia.run(4); assert_ne!(tia.read(INPT0) & 0x80, 0); } #[test] fn a_paddle_turned_further_takes_longer() { let mut tia = TIA::default(); let paddles = Paddles::new(&mut tia, NTSC_CLOCK); paddles.position.borrow_mut()[1] = Some(0.2); let short = paddles.trip_steps(1); paddles.position.borrow_mut()[1] = Some(0.8); assert!(paddles.trip_steps(1) > 3.0 * short); } #[test] fn a_keypad_key_pulls_its_column_when_its_row_is_driven_low() { let mut tia = TIA::default(); let riot = Rc::new(RefCell::new(RIOT::new(0, 0, 1024, 1024))); let keypads = Keypads::new(&mut tia, riot.clone()); keypads.pressed.borrow_mut()[0].insert((1, 1)); // left keypad, second row, middle column: "5" riot.borrow_mut().write(0x281, 0xF0, 0); // SWACNT: the left port's four row lines are outputs riot.borrow_mut().write(0x280, 0xFF, 1); // no row driven low assert_ne!(tia.read(INPT1) & 0x80, 0); riot.borrow_mut().write(0x280, 0xDF, 2); // D5, the second row, low assert_eq!(tia.read(INPT1) & 0x80, 0); assert!(tia.read(INPT0) & 0x80 != 0 && tia.read(INPT4) & 0x80 != 0); // other columns stay high riot.borrow_mut().write(0x280, 0xEF, 3); // the first row instead assert_ne!(tia.read(INPT1) & 0x80, 0); }