add breakpoints

This commit is contained in:
Alex Janka 2023-04-20 18:01:45 +10:00
parent 7d90d516ed
commit 37be5599d2
2 changed files with 25 additions and 0 deletions

View file

@ -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<u16, u8>,
breakpoints: Vec<u16>,
}
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
}
}

View file

@ -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}",