// SPDX-License-Identifier: MIT //! The board's timeline, decoder and HALT/RDY placement, on a synthetic BIOS //! (a5200-python's tests/test_board.py, transcribed). mod common; use std::cell::RefCell; use std::rc::Rc; use a5200::antic::ANTIC; use a5200::Running; use common::{fake_board, JMP_F800}; #[test] fn frame_is_29868_cycles() { let mut cpu = fake_board(&JMP_F800, None); cpu.run_frame(); let mut marks = Vec::new(); for _ in 0..3 { let start = cpu.bus.cycles; cpu.run_frame(); marks.push(cpu.bus.cycles - start); } // A frame mark falls inside an instruction, so each run overshoots by at // most one instruction; the total over frames is exact. let total: u64 = marks.iter().sum(); assert!((total as i64 - 3 * 29868).abs() <= 7); assert_eq!(cpu.bus.antic.vcount, 0); } #[test] fn cycles_per_frame_exact_on_the_antic_counter() { let mut cpu = fake_board(&JMP_F800, None); let seen: Rc>> = Rc::default(); let sink = Rc::clone(&seen); cpu.bus.on_frame = Some(Box::new(move |b| sink.borrow_mut().push(b.cycles))); while seen.borrow().len() < 4 { cpu.board_step(); } let seen = seen.borrow(); let gaps: Vec = seen.windows(2).map(|w| w[1] - w[0]).collect(); assert_eq!(gaps, vec![29868, 29868, 29868]); } #[test] fn memory_map_and_floating_bus() { let mut cpu = fake_board(&JMP_F800, None); let board = &mut cpu.bus; board._write(0x1234, 0x5A); assert_eq!(board._read(0x1234), 0x5A); assert_eq!(board._read(0x4000 - 1), 0); // RAM top board.data_bus = 0x77; assert_eq!(board._read(0xD000), 0x77); // unmapped: floating bus (U5) let vcount = board.antic.read(0x0B); assert_eq!(board._read(0xD40B), vcount); // VCOUNT assert_eq!(board._read(0xD4F5), 0xFF); // ANTIC unassigned reads $FF let f800 = board._read(0xF800); assert_eq!(board._read(0xF000), f800); // C8 mirror default assert_eq!(board._read(0xC014), 0x0F); // NTSC GTIA PAL register } #[test] fn wsync_stretches_reads_until_cycle_105() { // STA $D40A ; NOP ; NOP ; JMP * let program = [0x8D, 0x0A, 0xD4, 0xEA, 0xEA, 0x4C, 0x05, 0xF8]; let mut cpu = fake_board(&program, None); let log: Rc>> = Rc::default(); let sink = Rc::clone(&log); cpu.bus.bus_observers.push(Box::new( move |k: char, a: i32, _v: u8, _c: u64, antic: &ANTIC| { sink.borrow_mut().push((k, a, antic.hcount)) }, )); cpu.board_step(); // reset cpu.board_step(); // STA WSYNC cpu.board_step(); // NOP (first cycle runs, then RDY holds the second) let log = log.borrow(); assert!(log.iter().any(|&(k, _, _)| k == 'S')); let first_read_after = log .iter() .find(|&&(k, a, _)| k == 'R' && a == 0xF804) .unwrap() .2; assert_eq!(first_read_after, 105); } #[test] fn dli_less_frame_has_one_vbi_nmi() { // Enable VBI: LDA #$40 ; STA NMIEN ; JMP * (NMI vector -> $F800 restarts it) let program = [0xA9, 0x40, 0x8D, 0x0E, 0xD4, 0x4C, 0x05, 0xF8]; let mut cpu = fake_board(&program, None); // Rust-forced: Python spies on cpu.request_non_maskable_interrupt; here an observer // sees ANTIC's nmi_fire from the cycle before (an observer runs before the cycle's // end()), with the counters where that end() left them, which is where Python's spy reads // them. let fired: Rc>> = Rc::default(); let sink = Rc::clone(&fired); cpu.bus.bus_observers.push(Box::new( move |_k: char, _a: i32, _v: u8, _c: u64, antic: &ANTIC| { if antic.nmi_fire { sink.borrow_mut().push((antic.vcount, antic.hcount)); } }, )); cpu.run_frame(); cpu.run_frame(); let fired = fired.borrow(); assert_eq!(cpu.bus.nmis_lost, 0); assert!(!fired.is_empty() && fired.iter().all(|&pos| pos == (248, 9))); // ended cycle 8 of line 248 }