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