diff --git a/src/cpus/m68k/execute.rs b/src/cpus/m68k/execute.rs index ef91223..dc50b3a 100644 --- a/src/cpus/m68k/execute.rs +++ b/src/cpus/m68k/execute.rs @@ -177,14 +177,24 @@ impl MC68010 { } if self.use_debugger { - // Single Step - self.dump_state(space); - let mut buffer = String::new(); - std::io::stdin().read_line(&mut buffer).unwrap(); + self.run_debugger(space); } Ok(()) } + fn run_debugger(&mut self, space: &mut AddressSpace) { + self.dump_state(space); + let mut buffer = String::new(); + + loop { + std::io::stdin().read_line(&mut buffer).unwrap(); + match buffer.as_ref() { + "dump\n" => space.dump_memory(self.msp as Address, (0x200000 - self.msp) as Address), + _ => { return; }, + } + } + } + fn execute_current(&mut self, space: &mut AddressSpace) -> Result<(), Error> { match self.current_instruction { Instruction::ADD(src, dest, size) => { diff --git a/src/devices/mc68681.rs b/src/devices/mc68681.rs index d204f9d..54c7136 100644 --- a/src/devices/mc68681.rs +++ b/src/devices/mc68681.rs @@ -8,6 +8,7 @@ use nix::pty::{self, PtyMaster}; use nix::fcntl::OFlag; use nix::unistd::sleep; use nix::poll::{poll, PollFd, PollFlags}; +use nix::fcntl::{fcntl, FcntlArg}; use crate::error::Error; use crate::memory::{Address, Addressable}; @@ -52,16 +53,16 @@ const DEV_NAME: &'static str = "mc68681"; pub struct MC68681 { pub tty: Option, - pub status: [u8; 1], - pub input: [u8; 1], + pub status: u8, + pub input: u8, } impl MC68681 { pub fn new() -> Self { MC68681 { tty: None, - status: [0x0C], - input: [0], + status: 0x0C, + input: 0, } } @@ -74,7 +75,9 @@ impl MC68681 { Ok(master) => { let name = unsafe { pty::ptsname(&master).map_err(|_| Error::new("Unable to get pty name"))? }; println!("Open {}", name); + fcntl(master.as_raw_fd(), FcntlArg::F_SETFL(OFlag::O_NONBLOCK)).unwrap(); self.tty = Some(master); + Command::new("x-terminal-emulator").arg("-e").arg(&format!("pyserial-miniterm {}", name)).spawn().unwrap(); sleep(1); Ok(()) @@ -85,30 +88,26 @@ impl MC68681 { pub fn step(&mut self) -> Result<(), Error> { if !self.rx_ready() && self.tty.is_some() { - self.poll_one_byte().map(|byte| { - self.input[0] = byte; - self.status[0] |= SR_RX_READY; - }); + let mut buf = [0; 1]; + let tty = self.tty.as_mut().unwrap(); + match tty.read(&mut buf) { + Ok(count) => { + println!("READ {:?}", count); + self.input = buf[0]; + self.status |= SR_RX_READY; + }, + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { }, + Err(err) => { + println!("ERROR: {:?}", err); + } + } } Ok(()) } pub fn rx_ready(&self) -> bool { - (self.status[0] & SR_RX_READY) != 0 - } - - fn poll_one_byte(&mut self) -> Option { - if self.tty.is_none() { - return None; - } - - let tty = self.tty.as_mut().unwrap(); - let mut fds = [PollFd::new(tty.as_raw_fd(), PollFlags::POLLIN); 1]; - match poll(&mut fds, 0) { - Ok(byte) => Some(byte as u8), - Err(_) => None, - } + (self.status & SR_RX_READY) != 0 } } @@ -124,50 +123,16 @@ impl Addressable for MC68681 { self.step(); match addr { - REG_SRA_RD => data[0] = self.status[0], - REG_TBA_RD => { - data[0] = self.input[0]; + REG_SRA_RD => { + data[0] = self.status }, - _ => { println!("{}: reading from {:0x}", DEV_NAME, addr); data[0] = self.input[0]; }, - } - - data - } - - fn write(&mut self, mut addr: Address, data: &[u8]) { - match addr { - REG_TBA_WR => { - println!("{}: {}", DEV_NAME, data[0] as char); - self.tty.as_mut().map(|tty| tty.write_all(&[data[0]])); - }, - _ => { println!("{}: writing {:0x} to {:0x}", DEV_NAME, data[0], addr); }, - } - } -} - - -/* -impl Addressable for MC68681 { - fn len(&self) -> usize { - 0x30 - } - - fn read(&mut self, addr: Address, count: usize) -> Vec { - let mut data = vec![0; count]; - - // TODO this is temporary - self.step(); - - match addr { - REG_SRA_RD => { data[0] = self.status }, REG_RBA_RD => { - if self.rx_ready() { - data[0] = self.input; - self.status = self.status & !SR_RX_READY; - } + data[0] = self.input; + self.status &= !SR_RX_READY; }, - _ => { println!("{}: reading from {:0x}", DEV_NAME, addr); }, + _ => { println!("{}: reading from {:0x}", DEV_NAME, addr); data[0] = self.input; }, } + data } @@ -181,4 +146,4 @@ impl Addressable for MC68681 { } } } -*/ + diff --git a/src/main.rs b/src/main.rs index e5b19b4..d09c7c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { space.insert(0x00700000, Box::new(serial)); let mut cpu = MC68010::new(); - //cpu.set_breakpoint(0x02bc); + //cpu.set_breakpoint(0x0224); cpu.use_tracing = true; while cpu.is_running() { match cpu.step(&mut space) { diff --git a/src/memory.rs b/src/memory.rs index 49d358c..bac10c4 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -110,7 +110,8 @@ impl AddressSpace { pub fn dump_memory(&mut self, mut addr: Address, mut count: Address) { while count > 0 { let mut line = format!("{:#010x}: ", addr); - for i in 0..8 { + let to = if count < 16 { count / 2 } else { 8 }; + for i in 0..to { line += &format!("{:#06x} ", self.read_beu16(addr).unwrap()); addr += 2; count -= 2;