use huc6280::{Bus, HuC6280, Pins}; use std::sync::atomic::{AtomicUsize, Ordering}; const CAPACITY: usize = 1 << 20; static mut SAMPLES: [usize; CAPACITY] = [0; CAPACITY]; static COUNT: AtomicUsize = AtomicUsize::new(0); extern "C" fn on_prof(_: libc::c_int, _: *mut libc::siginfo_t, context: *mut libc::c_void) { // SAFETY: the kernel passes a ucontext_t; only the RIP is read, and each slot is written // once, by this handler, on this thread. unsafe { let ucontext = &*(context as *const libc::ucontext_t); let rip = ucontext.uc_mcontext.gregs[libc::REG_RIP as usize] as usize; let n = COUNT.fetch_add(1, Ordering::Relaxed); if n < CAPACITY { let samples = &raw mut SAMPLES; (*samples)[n] = rip; } } } fn set_timer(usec: libc::suseconds_t) { let tick = libc::timeval { tv_sec: 0, tv_usec: usec, }; let timer = libc::itimerval { it_interval: tick, it_value: tick, }; // SAFETY: an interval timer for this process. unsafe { libc::setitimer(libc::ITIMER_PROF, &timer, std::ptr::null_mut()); } } const BANK: [u8; 8] = [0x10, 0x10, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17]; struct Ram(Vec); impl Bus for Ram { fn read(&mut self, address: u32, _: &mut Pins) -> u8 { self.0[address as usize] } fn write(&mut self, address: u32, value: u8, _: &mut Pins) { self.0[address as usize] = value; } } fn main() { let args: Vec = std::env::args().skip(1).collect(); if args.len() < 3 { eprintln!("usage: dormann TEST.bin START SUCCESS [--limit N]"); std::process::exit(2); } let image = std::fs::read(&args[0]).expect("read the image"); let start = u16::from_str_radix(&args[1], 16).expect("START"); let done = u16::from_str_radix(&args[2], 16).expect("SUCCESS"); let limit: u64 = match args.get(3).map(String::as_str) { Some("--limit") => args[4].parse().expect("N"), _ => 100_000_000, }; let mut memory = vec![0u8; 0x200000]; for page in [0usize, 2, 3, 4, 5, 6, 7] { // page 1 is page 0 again (MPR1 = MPR0) let base = (BANK[page] as usize) << 13; memory[base..base + 0x2000].copy_from_slice(&image[page * 0x2000..(page + 1) * 0x2000]); } let mut cpu = HuC6280::new(Ram(memory)); cpu.mpr = BANK; cpu.pc = start; cpu.s = 0xFF; cpu.p = 0x04; let test_case = |cpu: &HuC6280| cpu.bus.0[((BANK[0] as usize) << 13) | 0x202]; unsafe { let mut action: libc::sigaction = std::mem::zeroed(); action.sa_sigaction = on_prof as *const () as usize; action.sa_flags = libc::SA_SIGINFO | libc::SA_RESTART; libc::sigaction(libc::SIGPROF, &action, std::ptr::null_mut()); } set_timer(250); let began = std::time::Instant::now(); let mut instructions = 0u64; let mut reason = "limit"; while instructions < limit { let before = cpu.pc; cpu.step(); instructions += 1; if cpu.pc == before { // a jump or branch to itself: a trap, or success reason = if before == done { "success" } else { "trap" }; break; } } println!( "{reason} at ${:04X} after {instructions} instructions (test case {})", cpu.pc, test_case(&cpu) ); eprintln!("{:.3} s", began.elapsed().as_secs_f64()); set_timer(0); let maps = std::fs::read_to_string("/proc/self/maps").unwrap(); let exe = std::fs::read_link("/proc/self/exe").unwrap(); let base = maps .lines() .find(|line| line.ends_with(exe.to_str().unwrap())) .and_then(|line| usize::from_str_radix(line.split('-').next().unwrap(), 16).ok()) .expect("the executable's mapping"); let n = COUNT.load(Ordering::Relaxed).min(CAPACITY); let mut text = format!("{}\n", exe.display()); let samples = &raw const SAMPLES; for i in 0..n { // SAFETY: the timer is stopped, nothing writes SAMPLES any more. let rip = unsafe { (*samples)[i] }; text.push_str(&format!("{:x}\n", rip.wrapping_sub(base))); } std::fs::write("profile.txt", text).unwrap(); eprintln!("{n} samples to profile.txt"); }