// SPDX-License-Identifier: MIT //! Reset, IRQ, NMI, ABORT, WAI and STP: what SingleStepTests has no cases of //! (the reference's `tests/test_interrupts.py`, transcribed), driven through //! 6502-rust's `Frame` and `Part` alone. //! //! Manufacturer tier, as the reference records it: DS Tables 5-2 and 5-3 //! (vectors), Table 5-7 row 22a (the entry's cycles), notes 7 and 10, section //! 2.25 (reset), U8, U11, U12. The poll (end of the second-to-last cycle) is //! the frame's, tested in 6502-python and 6502-rust. use w65c816::{Bus, Frame, Part, Signals, W65C816}; const PROGRAM: usize = 0x8000; /// A flat 16 MiB bus that counts callbacks and records kinds. struct Board { memory: Vec, callbacks: u32, log: Vec<(char, u32, bool)>, } impl Bus for Board { fn read(&mut self, address: u32, _: &mut Frame, signals: Signals) -> u8 { self.callbacks += 1; self.log.push(('r', address, signals.vpb())); self.memory[address as usize] } fn write(&mut self, address: u32, value: u8, _: &mut Frame, signals: Signals) { self.callbacks += 1; self.log.push(('w', address, signals.vpb())); self.memory[address as usize] = value; } fn idle(&mut self, address: u32, _: &mut Frame, _: Signals) { self.callbacks += 1; self.log.push(('i', address, false)); } } fn board(program: &[u8]) -> W65C816 { let mut memory = vec![0u8; 0x1000000]; memory[PROGRAM..PROGRAM + program.len()].copy_from_slice(program); for (vector, target) in [ (0xFFFC, 0x8000), (0xFFEE, 0x9000), (0xFFEA, 0x9100), (0xFFE8, 0x9200), (0xFFFE, 0xA000), (0xFFFA, 0xA100), (0xFFF8, 0xA200), ] { memory[vector] = (target & 0xFF) as u8; memory[vector + 1] = (target >> 8) as u8; } W65C816::new(Board { memory, callbacks: 0, log: Vec::new(), }) } /// One step, checking that a step's cycles are its callbacks. fn step(cpu: &mut W65C816) -> u32 { let before = cpu.bus.callbacks; let cycles = Part::step(cpu); assert_eq!( cycles, cpu.bus.callbacks - before, "a step's cycles are its callbacks" ); cycles } const NOPS: [u8; 16] = [0xEA; 16]; /// Reset, then (settle) run the first instruction: reset does not poll, so /// nothing requested before that instruction is taken at its boundary. fn booted(program: &[u8], native: bool, settle: bool) -> W65C816 { let mut cpu = board(program); cpu.frame.request_reset(); step(&mut cpu); if native { cpu.e = 0; } if settle { step(&mut cpu); } cpu.bus.log.clear(); cpu } #[test] fn nothing_is_taken_before_the_first_instruction_after_reset() { let mut cpu = booted(&NOPS, false, false); cpu.frame.request_non_maskable_interrupt(); cpu.request_abort(); assert_eq!(step(&mut cpu), 2); // the NOP at the reset vector runs first step(&mut cpu); assert_eq!(cpu.pc, 0xA200); // then ABORT (emulation vector) } #[test] fn reset_enters_emulation_mode_from_the_vector_in_seven_cycles() { let mut cpu = board(&NOPS); cpu.e = 0; cpu.p = 0x08; cpu.d = 0x1234; cpu.dbr = 0x7E; cpu.pbr = 0x12; cpu.x = 0xABCD; cpu.frame.request_reset(); assert_eq!(step(&mut cpu), 7); assert_eq!( (cpu.e, cpu.pc, cpu.pbr, cpu.dbr, cpu.d), (1, 0x8000, 0, 0, 0) ); assert!(cpu.p & 0x34 == 0x34 && cpu.p & 0x08 == 0); // M X I set, D clear assert!(cpu.x == 0xCD && cpu.s >> 8 == 0x01); assert_eq!( cpu.bus .log .iter() .filter(|(kind, _, _)| *kind == 'w') .count(), 0 ); // note 10: no writes } #[test] fn native_irq_pushes_pbr_pc_p_and_takes_the_native_vector() { let mut cpu = booted(&NOPS, true, true); cpu.p &= !0x04; cpu.p |= 0x08; cpu.pbr = 0x12; cpu.pc = 0x3456; cpu.s = 0x01FF; cpu.frame.request_maskable_interrupt(); assert_eq!(step(&mut cpu), 8); assert_eq!((cpu.pbr, cpu.pc), (0, 0x9000)); assert!(cpu.p & 0x04 != 0 && cpu.p & 0x08 == 0); // I set, D cleared (U8) let stack = &cpu.bus.memory; assert_eq!( (stack[0x01FF], stack[0x01FE], stack[0x01FD]), (0x12, 0x34, 0x56) ); let last: Vec = cpu.bus.log[cpu.bus.log.len() - 2..] .iter() .map(|e| e.2) .collect(); assert_eq!(last, [true, true]); } #[test] fn emulation_irq_pushes_three_bytes_with_b_clear() { let mut cpu = booted(&NOPS, false, true); cpu.p &= !0x04; cpu.s = 0x01FF; cpu.frame.request_maskable_interrupt(); assert_eq!(step(&mut cpu), 7); // note 7: no PBR push assert_eq!(cpu.pc, 0xA000); assert_eq!(cpu.bus.memory[0x01FD] & 0x10, 0); // B clear for IRQ (U8) } #[test] fn nmi_is_taken_whatever_i_says_and_wins_over_irq() { let mut cpu = booted(&NOPS, true, true); cpu.frame.request_maskable_interrupt(); cpu.frame.request_non_maskable_interrupt(); step(&mut cpu); assert_eq!(cpu.pc, 0x9100); } #[test] fn abort_comes_first() { let mut cpu = booted(&NOPS, true, true); cpu.frame.request_non_maskable_interrupt(); cpu.request_abort(); step(&mut cpu); assert_eq!(cpu.pc, 0x9200); } #[test] fn wai_with_i_set_resumes_after_wai_without_a_vector() { let mut cpu = booted(&[0xCB, 0xEA, 0xEA], false, false); // WAI; NOP assert_eq!(step(&mut cpu), 3); assert!(cpu.waiting && cpu.halted()); assert_eq!(step(&mut cpu), 1); // waiting: one IO cycle cpu.frame.request_maskable_interrupt(); // I is set after reset cpu.bus.log.clear(); assert_eq!(step(&mut cpu), 1); // the wake-up cycle (U11) assert_eq!(cpu.bus.log, [('r', 0x8001, false)]); // a program read at PC, byte dropped assert!(!cpu.waiting); step(&mut cpu); assert_eq!(cpu.pc, 0x8002); // ran the NOP after WAI, took no vector } #[test] fn wai_with_i_clear_takes_the_interrupt() { let mut cpu = booted(&[0x58, 0xCB, 0xEA], false, false); // CLI; WAI step(&mut cpu); step(&mut cpu); cpu.frame.request_maskable_interrupt(); step(&mut cpu); // wakes step(&mut cpu); // the boundary takes the IRQ assert_eq!(cpu.pc, 0xA000); } #[test] fn stp_stops_until_reset() { let mut cpu = booted(&[0xDB], false, false); step(&mut cpu); cpu.frame.request_non_maskable_interrupt(); for _ in 0..3 { assert_eq!(step(&mut cpu), 1); } assert!(cpu.stopped); cpu.frame.request_reset(); step(&mut cpu); assert!(!cpu.stopped && cpu.pc == 0x8000); } #[test] fn state_round_trip_keeps_the_poll_and_abort() { // Not in the reference's test file: capture and restore carry every field, // ABORT's latch included (cpu.py's capture_state/restore_state). let mut cpu = booted(&NOPS, true, true); cpu.request_abort(); cpu.frame.request_maskable_interrupt(); let state = cpu.capture_state(); assert!(state.abort_pending && state.maskable_interrupt_pending); let mut other = board(&NOPS); other.restore_state(&state).unwrap(); assert_eq!(other.capture_state(), state); step(&mut other); assert_eq!(other.pc, 0x9200); }