// SPDX-License-Identifier: MIT //! Rung 5: the frame (6502-rust's `Frame` and `Part`) carries the four 65C02 //! rows, with none of the NMOS part's code. //! //! Each test drives the core only through the frame's API (`Part::step`, the //! requests, `note_stall`) and checks what the reference's directed tests //! (rung 6 of w65c02-python, `tests/test_directed.py`) pin for each row: reset //! per row, the poll and `CLI`'s delay, one-cycle NOPs, `WAI` and `STP`, a //! stretched write cycle, the signals inside a callback, `step()`'s count for //! every opcode of every row, and state capture. `scripts/rung5.sh` adds the //! check that no `Mos6502` symbol is in any built binary. use w65c02::{ Bus, Cmos65c02, CpuState, Frame, Part, Row, Signals, G65SC02, LYNX2_CELL, R65C02, ROWS, W65C02S, }; /// An action a test runs inside one access's callback: (access index, action). type Event = (usize, fn(&mut Frame)); /// Flat memory that logs every access with its signals and can raise requests. struct Host { memory: Vec, log: Vec<(char, u16, u8, Signals)>, events: Vec, } impl Host { fn new() -> Self { Host { memory: vec![0; 0x10000], log: Vec::new(), events: Vec::new(), } } fn fire(&mut self, frame: &mut Frame) { let index = self.log.len(); for (at, action) in self.events.clone() { if at == index { action(frame); } } } } impl Bus for Host { fn read(&mut self, address: u16, frame: &mut Frame, signals: Signals) -> u8 { self.fire(frame); let value = self.memory[address as usize]; self.log.push(('r', address, value, signals)); value } fn write(&mut self, address: u16, value: u8, frame: &mut Frame, signals: Signals) { self.fire(frame); self.log.push(('w', address, value, signals)); self.memory[address as usize] = value; } } /// A CPU of `row` with `program` at $0200 and the vectors: NMI $0300, reset $0200, IRQ $0400. fn cpu(row: Row, program: &[u8]) -> Cmos65c02 { let mut host = Host::new(); host.memory[0x0200..0x0200 + program.len()].copy_from_slice(program); host.memory[0xFFFA..].copy_from_slice(&[0x00, 0x03, 0x00, 0x02, 0x00, 0x04]); host.memory[0x0300] = 0x40; // RTI host.memory[0x0400] = 0x40; // RTI Cmos65c02::new(row, host) } /// Step through the frame's trait, as any part on the frame is stepped. fn step(part: &mut P) -> u32 { Part::step(part) } #[test] fn reset_is_seven_cycles_with_sync_in_the_first_and_the_rows_stack_cycles() { for row in ROWS { let mut cpu = cpu(row, &[0xEA]); cpu.s = 0xFD; cpu.p = 0x2C; // D and I set cpu.frame.request_reset(); assert_eq!(step(&mut cpu), 7, "{}", row.name); let log = &cpu.bus.log; assert_eq!(log.len(), 7); assert!( log[0].3.sync && !log[1].3.sync, "{}: SYNC in the first cycle only", row.name ); let writes = log.iter().filter(|access| access.0 == 'w').count(); assert_eq!( writes, if row.reset_writes_stack { 3 } else { 0 }, "{}", row.name ); assert_eq!(cpu.s, 0xFA, "{}: S three lower", row.name); assert_eq!(cpu.p & 0x0C, 0x04, "{}: I set, D cleared", row.name); assert_eq!(cpu.pc, 0x0200); } } #[test] fn cli_admits_a_pending_irq_after_the_next_instruction() { for row in ROWS { // SEI; CLI; NOP; NOP let mut cpu = cpu(row, &[0x78, 0x58, 0xEA, 0xEA]); cpu.pc = 0x0200; step(&mut cpu); // SEI cpu.frame.request_maskable_interrupt(); step(&mut cpu); // CLI: its poll saw I set assert_eq!(cpu.pc, 0x0202); step(&mut cpu); // NOP runs assert_eq!(cpu.pc, 0x0203, "{}", row.name); assert_eq!(step(&mut cpu), 7, "{}: then the IRQ", row.name); assert_eq!(cpu.pc, 0x0400); } } #[test] fn a_request_in_the_last_cycle_waits_one_instruction() { for row in ROWS { // NOP (2 cycles: accesses 0, 1); NOP; NOP let mut cpu = cpu(row, &[0xEA, 0xEA, 0xEA]); cpu.pc = 0x0200; cpu.p = 0x20; cpu.bus.events = vec![(1, |frame| frame.request_maskable_interrupt())]; step(&mut cpu); step(&mut cpu); assert_eq!(cpu.pc, 0x0202, "{}: the second NOP ran", row.name); assert_eq!(step(&mut cpu), 7, "{}", row.name); } } #[test] fn one_cycle_nops_do_not_poll() { for row in ROWS { // $03 $03 then NOP let mut cpu = cpu(row, &[0x03, 0x03, 0xEA]); cpu.pc = 0x0200; cpu.p = 0x20; assert_eq!(step(&mut cpu), 1); cpu.frame.request_maskable_interrupt(); assert_eq!( step(&mut cpu), 1, "{}: the IRQ waits behind a one-cycle NOP", row.name ); assert_eq!( step(&mut cpu), 2, "{}: and behind the second: $EA runs", row.name ); assert_eq!(cpu.pc, 0x0203); assert_eq!(step(&mut cpu), 7, "{}", row.name); } } #[test] fn wai_waits_and_wakes_and_stp_stops_until_reset_on_the_w65c02s_only() { for row in ROWS { // CLI; WAI; INX; STP; INX let mut cpu = cpu(row, &[0x58, 0xCB, 0xE8, 0xDB, 0xE8]); cpu.pc = 0x0200; step(&mut cpu); let wai = step(&mut cpu); if row.wai_stp { assert_eq!(wai, 3); assert!(cpu.halted() && cpu.waiting); assert_eq!(step(&mut cpu), 1, "a waiting cycle"); assert_eq!( cpu.bus.log.last().unwrap().1, 0x0202, "reads the next opcode's address" ); cpu.frame.request_maskable_interrupt(); assert_eq!(step(&mut cpu), 7, "the waking step is the entry"); assert!(!cpu.waiting); cpu.frame.clear_maskable_interrupt(); step(&mut cpu); // RTI step(&mut cpu); // INX assert_eq!(step(&mut cpu), 3, "STP"); assert!(cpu.stopped && cpu.halted()); cpu.frame.request_non_maskable_interrupt(); assert_eq!(step(&mut cpu), 1, "an NMI does not end STP"); cpu.frame.request_reset(); assert_eq!(step(&mut cpu), 7); assert!(!cpu.stopped && !cpu.halted()); } else { assert_eq!(wai, 1, "{}: $CB is a one-cycle NOP", row.name); assert!(!cpu.halted()); } } } #[test] fn a_write_cycle_held_by_rdy_polls_requests_raised_while_held() { // STA $10 (accesses 0-2, the write is 2) then NOP: a request raised during // the held write is seen by STA's poll (U12, the Lynx's request 3). for row in ROWS { let mut cpu = cpu(row, &[0x85, 0x10, 0xEA]); cpu.pc = 0x0200; cpu.p = 0x20; cpu.bus.events = vec![(2, |frame| { frame.note_stall(); frame.request_maskable_interrupt(); frame.note_stall(); })]; assert_eq!(step(&mut cpu), 3); assert_eq!( step(&mut cpu), 7, "{}: taken at the next boundary", row.name ); } } #[test] fn signals_inside_the_callback() { // LDA $1234,X across no page: opcode fetch (sync, fetch), two operand // fetches (fetch), the data read (neither); opcode from the second cycle. let mut cpu = cpu(G65SC02, &[0xBD, 0x34, 0x12]); cpu.pc = 0x0200; cpu.opcode = 0x99; step(&mut cpu); let signals: Vec<(bool, bool, u8)> = cpu .bus .log .iter() .map(|access| (access.3.sync, access.3.fetch, access.3.opcode)) .collect(); assert_eq!( signals, [ (true, true, 0x99), (false, true, 0xBD), (false, true, 0xBD), (false, false, 0xBD) ] ); } #[test] fn step_returns_its_callbacks_for_every_opcode_of_every_row() { for row in ROWS { for opcode in 0..=255u8 { for decimal in [false, true] { let mut cpu = cpu(row, &[opcode, 0x80, 0x12, 0x00]); cpu.pc = 0x0200; cpu.x = 0xF0; cpu.y = 0x90; cpu.s = 0xF0; cpu.p = if decimal { 0x28 } else { 0x20 }; let cycles = step(&mut cpu) as usize; assert_eq!(cycles, cpu.bus.log.len(), "{} ${opcode:02X}", row.name); assert!(cpu.bus.log[0].3.sync); } } } } #[test] fn the_rows_differ_where_the_reference_says() { // $07 (RMB0 $10): five cycles with the bit instructions, else one. for (row, cycles) in [(W65C02S, 5), (R65C02, 5), (G65SC02, 1), (LYNX2_CELL, 5)] { let mut cpu = cpu(row, &[0x07, 0x10]); cpu.pc = 0x0200; assert_eq!(step(&mut cpu), cycles, "{}", row.name); } } #[test] fn state_round_trips_and_validates() { let mut cpu = cpu(W65C02S, &[0x58, 0xCB]); cpu.pc = 0x0200; step(&mut cpu); // CLI: polled_i set let state = cpu.capture_state(); assert_eq!(state.polled_i, Some(true)); let mut other = self::cpu(W65C02S, &[]); other.restore_state(&state).unwrap(); assert_eq!(other.capture_state(), state); step(&mut cpu); // WAI let waiting = cpu.capture_state(); assert!(waiting.waiting && waiting.halted); other.restore_state(&waiting).unwrap(); assert_eq!(other.capture_state(), waiting); let bad = CpuState { waiting: true, halted: false, ..CpuState::default() }; assert!(other.restore_state(&bad).is_err()); }