// SPDX-License-Identifier: MIT //! The bus contract of the reference's docs/handoff-brief.md, item by item, and //! the special states (the reference's `tests/test_core_contract.py`, //! transcribed). Its checks of the constructor's arguments and of `CPUState`'s //! ranges are Rust's types here, and are not repeated. use spc700::{Bus, CpuState, Signals, Spc700, FLAG_B, FLAG_I, FLAG_P}; #[derive(Debug, PartialEq, Eq, Clone)] enum Event { Read(u16, i32, bool, u8), // address, cycle, dummy, psw Write(u16, i32, u8), // address, cycle, psw Idle(u8, i32), // opcode, cycle } struct Host { memory: Vec, events: Vec, } impl Bus for Host { fn read(&mut self, address: u16, s: Signals) -> u8 { self.events .push(Event::Read(address, s.cycle, s.dummy, s.psw)); self.memory[address as usize] } fn write(&mut self, address: u16, value: u8, s: Signals) { self.events.push(Event::Write(address, s.cycle, s.psw)); self.memory[address as usize] = value; } fn idle(&mut self, s: Signals) { self.events.push(Event::Idle(s.opcode, s.cycle)); } } fn host() -> Spc700 { Spc700::new(Host { memory: vec![0; 0x10000], events: Vec::new(), }) } fn at(cpu: &mut Spc700, pc: u16) { cpu.restore_state(&CpuState { pc, ..CpuState::default() }); } struct Plain(Vec); impl Bus for Plain { fn read(&mut self, address: u16, _: Signals) -> u8 { self.0[address as usize] } fn write(&mut self, address: u16, value: u8, _: Signals) { self.0[address as usize] = value; } } #[test] fn idle_defaults_to_nothing_and_the_count_still_holds() { let mut memory = vec![0u8; 0x10000]; memory[0] = 0xCF; // MUL YA assert_eq!(Spc700::new(Plain(memory)).step(), 9); } #[test] fn cycle_is_the_index_within_the_instruction_and_opcode_is_current_in_idle() { let mut cpu = host(); cpu.bus.memory[0x300] = 0x9F; // XCN A at(&mut cpu, 0x300); assert_eq!(cpu.step(), 5); let cycles: Vec = cpu .bus .events .iter() .map(|e| match e { Event::Read(_, c, _, _) | Event::Write(_, c, _) | Event::Idle(_, c) => *c, }) .collect(); assert_eq!(cycles, [0, 1, 2, 3, 4]); assert_eq!( cpu.bus.events[2..], [ Event::Idle(0x9F, 2), Event::Idle(0x9F, 3), Event::Idle(0x9F, 4) ] ); } #[test] fn p_is_current_inside_the_write_callback() { // U9: SETP then a write: the board reads P at write time. let mut cpu = host(); cpu.bus.memory[0x300..0x303].copy_from_slice(&[0x40, 0xC4, 0xF0]); // SETP; MOV $F0,A at(&mut cpu, 0x300); cpu.step(); cpu.step(); let Event::Write(address, _, psw) = cpu.bus.events.last().unwrap().clone() else { panic!("the last event is not a write"); }; assert_eq!(address, 0x01F0); assert!(psw & FLAG_P != 0); } #[test] fn the_store_read_is_a_dummy_and_a_real_callback() { let mut cpu = host(); cpu.bus.memory[0x300..0x302].copy_from_slice(&[0xC4, 0xFD]); // MOV $FD,A at(&mut cpu, 0x300); cpu.step(); let dummies: Vec = cpu.bus.events[..3] .iter() .map(|e| matches!(e, Event::Read(_, _, true, _))) .collect(); assert_eq!(dummies, [false, false, true]); assert!(matches!(cpu.bus.events[2], Event::Read(0x00FD, _, _, _))); } fn stopped_is_one_idle_per_step_until_reset(opcode: u8) { let mut cpu = host(); cpu.bus.memory[0x300] = opcode; cpu.bus.memory[0xFFFE] = 0xC0; cpu.bus.memory[0xFFFF] = 0xFF; cpu.restore_state(&CpuState { pc: 0x300, psw: FLAG_P | FLAG_B | FLAG_I | 0x81, ..CpuState::default() }); assert_eq!(cpu.step(), 1); assert!(cpu.stopped); assert_eq!(cpu.pc, 0x301); cpu.bus.events.clear(); for _ in 0..5 { assert_eq!(cpu.step(), 1); } assert_eq!(cpu.bus.events, vec![Event::Idle(opcode, 0); 5]); assert!(cpu.stopped); cpu.bus.events.clear(); cpu.reset(); assert!(!cpu.stopped); assert_eq!(cpu.pc, 0xFFC0); assert_eq!(cpu.psw, 0x81); // P = B = I = 0, the others keep their values (U12) let addresses: Vec = cpu .bus .events .iter() .map(|e| match e { Event::Read(a, ..) => *a, other => panic!("not a read: {other:?}"), }) .collect(); assert_eq!(addresses, [0xFFFE, 0xFFFF]); } #[test] fn sleep_is_one_idle_per_step_until_reset() { stopped_is_one_idle_per_step_until_reset(0xEF); } #[test] fn stop_is_one_idle_per_step_until_reset() { stopped_is_one_idle_per_step_until_reset(0xFF); } #[test] fn constructed_state_is_u12() { let cpu = Spc700::new(Plain(vec![0; 0x10000])); assert_eq!( cpu.capture_state(), CpuState { a: 0, x: 0, y: 0, sp: 0xEF, pc: 0, psw: 2, stopped: false } ); } #[test] fn ya_is_y_and_a_paired() { let mut cpu = host(); cpu.set_ya(0x1234); assert_eq!((cpu.y, cpu.a), (0x12, 0x34)); assert_eq!(cpu.ya(), 0x1234); assert_eq!(cpu.capture_state().ya(), 0x1234); }