1use huc6280::{Bus, HuC6280, Pins};
18
19const BANK: [u8; 8] = [0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17];
20
21struct Ram(Vec<u8>);
22
23impl Bus for Ram {
24 fn read(&mut self, address: u32, _: &mut Pins) -> u8 {
25 self.0[address as usize]
26 }
27 fn write(&mut self, address: u32, value: u8, _: &mut Pins) {
28 self.0[address as usize] = value;
29 }
30}
31
32fn main() {
33 let args: Vec<String> = std::env::args().skip(1).collect();
34 if args.len() < 3 {
35 eprintln!("usage: dormann TEST.bin START SUCCESS [--limit N]");
36 std::process::exit(2);
37 }
38 let image = std::fs::read(&args[0]).expect("read the image");
39 let start = u16::from_str_radix(&args[1], 16).expect("START");
40 let done = u16::from_str_radix(&args[2], 16).expect("SUCCESS");
41 let limit: u64 = match args.get(3).map(String::as_str) {
42 Some("--limit") => args[4].parse().expect("N"),
43 _ => 100_000_000,
44 };
45 let mut memory = vec![0u8; 0x200000];
46 for page in [0usize, 2, 3, 4, 5, 6, 7] {
47 let base = (BANK[page] as usize) << 13;
49 memory[base..base + 0x2000].copy_from_slice(&image[page * 0x2000..(page + 1) * 0x2000]);
50 }
51 let mut cpu = HuC6280::new(Ram(memory));
52 cpu.mpr = BANK;
53 cpu.pc = start;
54 cpu.s = 0xFF;
55 cpu.p = 0x04;
56 let test_case = |cpu: &HuC6280<Ram>| cpu.bus.0[((BANK[0] as usize) << 13) | 0x202];
57 let began = std::time::Instant::now();
58 let mut instructions = 0u64;
59 let mut reason = "limit";
60 while instructions < limit {
61 let before = cpu.pc;
62 cpu.step();
63 instructions += 1;
64 if cpu.pc == before {
65 reason = if before == done { "success" } else { "trap" };
67 break;
68 }
69 }
70 println!(
71 "{reason} at ${:04X} after {instructions} instructions (test case {})",
72 cpu.pc,
73 test_case(&cpu)
74 );
75 eprintln!("{:.3} s", began.elapsed().as_secs_f64());
76 std::process::exit(if reason == "success" { 0 } else { 1 });
77}