Separated debugging code into its own file

This commit is contained in:
transistor 2021-10-02 09:35:25 -07:00
parent 38bcf0af3f
commit 4b577ad403
4 changed files with 71 additions and 47 deletions

61
src/cpus/m68k/debugger.rs Normal file
View File

@ -0,0 +1,61 @@
use crate::error::Error;
use crate::memory::{Address, AddressSpace};
use super::execute::{MC68010};
use super::decode::{Instruction, Target, Size, Direction, Condition, ControlRegister, RegisterType};
pub struct M68kDebugger {
pub breakpoints: Vec<u32>,
pub use_tracing: bool,
pub use_debugger: bool,
}
impl M68kDebugger {
pub fn new() -> M68kDebugger {
M68kDebugger {
breakpoints: vec!(),
use_tracing: false,
use_debugger: false,
}
}
}
impl MC68010 {
pub fn enable_tracing(&mut self) {
self.debugger.use_tracing = true;
}
pub fn add_breakpoint(&mut self, addr: Address) {
self.debugger.breakpoints.push(addr as u32);
}
pub fn check_breakpoints(&mut self) {
for breakpoint in &self.debugger.breakpoints {
if *breakpoint == self.state.pc {
self.debugger.use_tracing = true;
self.debugger.use_debugger = true;
break;
}
}
}
pub 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.state.msp as Address, (0x200000 - self.state.msp) as Address),
"continue\n" => {
self.debugger.use_debugger = false;
return;
},
_ => { return; },
}
}
}
}

View File

@ -2,6 +2,7 @@
use crate::error::Error; use crate::error::Error;
use crate::memory::{Address, AddressSpace}; use crate::memory::{Address, AddressSpace};
use super::debugger::M68kDebugger;
use super::decode::{ use super::decode::{
M68kDecoder, M68kDecoder,
Instruction, Instruction,
@ -15,6 +16,7 @@ use super::decode::{
sign_extend_to_long sign_extend_to_long
}; };
/* /*
pub trait Processor { pub trait Processor {
fn reset(); fn reset();
@ -76,35 +78,23 @@ impl MC68010State {
pub struct MC68010 { pub struct MC68010 {
pub state: MC68010State, pub state: MC68010State,
pub decoder: M68kDecoder, pub decoder: M68kDecoder,
pub debugger: M68kDebugger,
pub breakpoints: Vec<u32>,
pub use_tracing: bool,
pub use_debugger: bool,
} }
impl MC68010 { impl MC68010 {
pub fn new() -> MC68010 { pub fn new() -> MC68010 {
MC68010 { MC68010 {
state: MC68010State::new(), state: MC68010State::new(),
decoder: M68kDecoder::new(0), decoder: M68kDecoder::new(0),
debugger: M68kDebugger::new(),
breakpoints: vec![],
use_tracing: false,
use_debugger: false,
} }
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.state = MC68010State::new(); self.state = MC68010State::new();
self.decoder = M68kDecoder::new(0); self.decoder = M68kDecoder::new(0);
self.debugger = M68kDebugger::new();
self.breakpoints = vec![];
self.use_tracing = false;
self.use_debugger = false;
} }
pub fn is_running(&self) -> bool { pub fn is_running(&self) -> bool {
@ -138,10 +128,6 @@ impl MC68010 {
println!(""); println!("");
} }
pub fn add_breakpoint(&mut self, addr: Address) {
self.breakpoints.push(addr as u32);
}
pub fn step(&mut self, space: &mut AddressSpace) -> Result<(), Error> { pub fn step(&mut self, space: &mut AddressSpace) -> Result<(), Error> {
match self.state.status { match self.state.status {
Status::Init => self.init(space), Status::Init => self.init(space),
@ -158,15 +144,9 @@ impl MC68010 {
self.decoder = M68kDecoder::decode_at(space, self.state.pc)?; self.decoder = M68kDecoder::decode_at(space, self.state.pc)?;
self.state.pc = self.decoder.end; self.state.pc = self.decoder.end;
for breakpoint in &self.breakpoints { self.check_breakpoints();
if *breakpoint == self.decoder.start {
self.use_tracing = true;
self.use_debugger = true;
break;
}
}
if self.use_tracing { if self.debugger.use_tracing {
// Print instruction bytes for debugging // Print instruction bytes for debugging
let ins_data: Result<String, Error> = let ins_data: Result<String, Error> =
(0..((self.state.pc - self.decoder.start) / 2)).map(|offset| (0..((self.state.pc - self.decoder.start) / 2)).map(|offset|
@ -175,29 +155,12 @@ impl MC68010 {
debug!("{:#010x}: {}\n\t{:?}\n", self.decoder.start, ins_data?, self.decoder.instruction); debug!("{:#010x}: {}\n\t{:?}\n", self.decoder.start, ins_data?, self.decoder.instruction);
} }
if self.use_debugger { if self.debugger.use_debugger {
self.run_debugger(space); self.run_debugger(space);
} }
Ok(()) 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.state.msp as Address, (0x200000 - self.state.msp) as Address),
"continue\n" => {
self.use_debugger = false;
return;
},
_ => { return; },
}
}
}
pub(crate) fn execute_current(&mut self, space: &mut AddressSpace) -> Result<(), Error> { pub(crate) fn execute_current(&mut self, space: &mut AddressSpace) -> Result<(), Error> {
match self.decoder.instruction { match self.decoder.instruction {
Instruction::ADD(src, dest, size) => { Instruction::ADD(src, dest, size) => {

View File

@ -1,7 +1,7 @@
mod decode; mod decode;
mod execute; mod execute;
//mod debugger; mod debugger;
mod tests; mod tests;
pub use self::execute::MC68010; pub use self::execute::MC68010;

View File

@ -30,7 +30,7 @@ fn main() {
//cpu.add_breakpoint(0x0838); //cpu.add_breakpoint(0x0838);
//cpu.add_breakpoint(0x0ea0); //cpu.add_breakpoint(0x0ea0);
cpu.use_tracing = true; cpu.enable_tracing();
while cpu.is_running() { while cpu.is_running() {
match cpu.step(&mut space) { match cpu.step(&mut space) {
Ok(()) => { }, Ok(()) => { },