// SPDX-License-Identifier: MIT //! Random single instructions, recorded from ssp1601-python (scripts/make_vectors.py). use std::collections::HashMap; use ssp1601_rust::{Host, Ssp1601}; struct Machine { program: HashMap, ext: [u16; 8], bus: Vec, } impl Host for Machine { fn program_read(&mut self, address: u16) -> u16 { self.program .get(&address) .copied() .unwrap_or(address ^ 0x5A5A) } fn ext_read(&mut self, n: u16, _st: u16) -> u16 { self.bus.push(format!("r{n}")); self.ext[usize::from(n)] } fn ext_write(&mut self, n: u16, value: u16, _st: u16) { self.bus.push(format!("w{n}={value:X}")); } } fn state(cpu: &Ssp1601) -> String { let mut words: Vec = vec![ cpu.x.into(), cpu.y.into(), cpu.a.into(), cpu.st.into(), cpu.pc.into(), cpu.sp as u64, ]; words.extend(cpu.stack.iter().map(|&v| u64::from(v))); words.extend(cpu.r.iter().map(|&v| u64::from(v))); words .iter() .map(|w| format!("{w:X}")) .collect::>() .join(" ") } fn hex(text: &str) -> u64 { u64::from_str_radix(text, 16).unwrap() } #[test] fn every_recorded_instruction() { let text = include_str!("vectors.txt"); let mut cases = 0; for line in text.lines() { let f: Vec<&str> = line.split('|').collect(); let before: Vec = f[0].split(' ').map(hex).collect(); let words: Vec = f[1].split(' ').map(|w| hex(w) as u16).collect(); let salt = hex(f[2]) as u32; let ext: Vec = f[3].split(' ').map(|w| hex(w) as u16).collect(); let mut cpu = Ssp1601::new(); cpu.x = before[0] as u16; cpu.y = before[1] as u16; cpu.a = before[2] as u32; cpu.st = before[3] as u16; cpu.pc = before[4] as u16; cpu.sp = before[5] as i32; for i in 0..6 { cpu.stack[i] = before[6 + i] as u16; } for i in 0..8 { cpu.r[i] = before[12 + i] as u8; } for (i, word) in cpu.ram.iter_mut().enumerate() { *word = ((i as u32).wrapping_mul(0x9E37) ^ salt) as u16; } let mut machine = Machine { program: HashMap::from([(cpu.pc, words[0]), (cpu.pc.wrapping_add(1), words[1])]), ext: ext.clone().try_into().unwrap(), bus: Vec::new(), }; let cycles = cpu.step(&mut machine); let mut ram: u32 = 0; for &word in &cpu.ram { ram = ram.wrapping_mul(31).wrapping_add(u32::from(word)); } let bus = if machine.bus.is_empty() { "-".to_string() } else { machine.bus.join(",") }; assert_eq!(state(&cpu), f[4], "state after: {line}"); assert_eq!(u64::from(cycles), hex(f[5]), "cycles: {line}"); assert_eq!(u64::from(ram), hex(f[6]), "ram: {line}"); assert_eq!(bus, f[7], "bus: {line}"); assert_eq!(cpu.unhandled, hex(f[8]), "unhandled: {line}"); cases += 1; } assert_eq!(cases, 60000); }