diff --git a/gb-emu/src/debug.rs b/gb-emu/src/debug.rs index ea8c247..e931298 100644 --- a/gb-emu/src/debug.rs +++ b/gb-emu/src/debug.rs @@ -18,6 +18,7 @@ pub enum CommandErr { pub enum Commands { Watch(u16), + Break(u16), Step, Continue, } @@ -38,6 +39,13 @@ impl FromStr for Commands { Err(CommandErr::InvalidArgument) } } + _ if let Some(addr) = lower.strip_prefix("break") => { + if let Ok(addr) = u16::from_str_radix(addr.trim().trim_start_matches("0x"), 16) { + Ok(Self::Break(addr)) + } else { + Err(CommandErr::InvalidArgument) + } + } _ => Err(CommandErr::InvalidCommand), } } @@ -49,6 +57,7 @@ pub struct Debugger { stepping: bool, last_command: String, watches: HashMap, + breakpoints: Vec, } impl Debugger { @@ -62,6 +71,7 @@ impl Debugger { stepping: true, last_command: String::from(""), watches: HashMap::new(), + breakpoints: Vec::new(), } } @@ -95,6 +105,11 @@ impl Debugger { } Commands::Step => self.stepping = true, Commands::Continue => self.stepping = false, + Commands::Break(address) => { + if !self.breakpoints.contains(&address) { + self.breakpoints.push(address) + } + } } } else { println!("Invalid command"); @@ -117,6 +132,12 @@ impl Debugger { *data = new_data; } } + for address in &self.breakpoints { + if self.core.pc() == *address { + println!("Breakpoint at 0x{address:0>4X} reached"); + should_pause = true; + } + } should_pause } } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 979c02d..0243603 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -134,6 +134,10 @@ where self.cpu.cycle_count } + pub fn pc(&self) -> u16 { + self.cpu.reg.pc + } + pub fn print_reg(&self) -> String { format!( "A:{:0>2X}, F:{}, BC:{:0>4X}, DE:{:0>4X}, HL:{:0>4X}, SP:{:0>4X}, PC:{:0>4X}\nLast instruction: {:0>2X} from 0x{:0>4X}",