// SPDX-License-Identifier: MIT //! The ProLine stick: one- and two-button modes chosen by the 6532's port B (the //! reference's tests/test_controllers.py). use a7800::board::{Board, EXT, LOCK, MEN}; use a7800::cartridge::connector::Linear; use a7800::controllers::{LightGun, ProLine, Stick}; use a7800::machine::NTSC; use a7800::sound::{tia_rate, wav_bytes, SoundTap}; use sixfiveohtwo::Frame; fn setup() -> (Board, ProLine) { let mut board = Board::new(NTSC, Box::new(Linear::new(&[0; 0x4000]).unwrap()), None); board.inptctrl = MEN | EXT | LOCK; board.locked = true; let sticks = ProLine::new(&mut board.tia, board.riot.clone()); (board, sticks) } fn rd(board: &mut Board, address: u16) -> u8 { board.read_byte(address, &mut Frame::default()) } fn inpt(board: &mut Board, n: u16) -> u8 { rd(board, 0x08 + n) & 0x80 } #[test] fn one_button_mode_at_power() { let (mut board, sticks) = setup(); sticks.set( 0, Stick { right_button: true, ..Stick::default() }, ); assert!(inpt(&mut board, 4) == 0 && inpt(&mut board, 0) == 0); // fire low on INPT4, pots quiet } #[test] fn two_button_mode_when_port_b_drives_the_line_low() { let (mut board, sticks) = setup(); board.write_byte(0x0283, 0x14, &mut Frame::default()); // SWBCNT: bits 2 and 4 outputs board.write_byte(0x0282, 0x00, &mut Frame::default()); // SWCHB: driven low sticks.set( 0, Stick { left_button: true, ..Stick::default() }, ); assert!(inpt(&mut board, 1) == 0x80 && inpt(&mut board, 0) == 0 && inpt(&mut board, 4) == 0x80); sticks.set( 1, Stick { right_button: true, ..Stick::default() }, ); assert_eq!(inpt(&mut board, 2), 0x80); } #[test] fn directions_on_port_a() { let (mut board, sticks) = setup(); sticks.set( 0, Stick { up: true, left: true, ..Stick::default() }, ); sticks.set( 1, Stick { right: true, ..Stick::default() }, ); assert_eq!(rd(&mut board, 0x0280), 0xFF & !0x10 & !0x40 & !0x08); } #[test] fn light_gun_trigger_on_swcha_d4_and_sensor_on_inpt4() { let (mut board, _sticks) = setup(); LightGun::attach(&mut board, 0); board.light_gun.as_mut().unwrap().set(Some((100, 200)), true); assert_eq!(rd(&mut board, 0x0280) & 0x10, 0); // the Barnyard Blaster / Crossbow read { let maria = &mut board.maria; maria.enabled = true; maria.vblank = false; maria.line = 101; maria.registers[0x00] = 0x0F; // BACKGRND: luminance 15 everywhere maria.clock_in_line = 210; maria.rendered = 0; } assert_eq!(inpt(&mut board, 4), 0); // light seen: INPT4 low assert_eq!(board.light_gun.as_ref().unwrap().hits.get(), 1); board.maria.line = 120; board.maria.rendered = 0; assert_eq!(inpt(&mut board, 4), 0x80); // beam far from the aim point board.maria.line = 101; board.maria.rendered = 0; board.maria.registers[0x00] = 0x02; // dark there: no light assert_eq!(inpt(&mut board, 4), 0x80); } #[test] fn sound_tap_records_the_tia_twice_a_line() { let (mut board, _sticks) = setup(); let tap = SoundTap::new(&board, true).attach(&mut board); board.inptctrl = MEN | EXT | LOCK; let mut frame = Frame::default(); board.write_byte(0x15, 0x04, &mut frame); // AUDC0: pure tone board.write_byte(0x17, 0x01, &mut frame); board.write_byte(0x19, 0x0F, &mut frame); // AUDV0 15 for _ in 0..454 * 50 / 4 { // 50 lines board.read_byte(0x8000, &mut frame); } let samples = tap.take(&mut board); assert!(samples.len() == 99 || samples.len() == 100); assert!(samples.iter().cloned().fold(f64::MIN, f64::max) > 0.3); assert_eq!(samples.iter().cloned().fold(f64::MAX, f64::min), 0.0); let streams = tap.streams.borrow(); assert_eq!(streams.tia, samples); assert_eq!(streams.pokey, vec![0.0; samples.len()]); // no POKEY on this cart assert!((tia_rate(&NTSC) - 31399.5).abs() < 1.0); assert_eq!(&wav_bytes(&samples, tia_rate(&NTSC))[..4], b"RIFF"); }