diff --git a/src/arm7tdmi/cpu.rs b/src/arm7tdmi/cpu.rs index 06b455a..ea1b8fe 100644 --- a/src/arm7tdmi/cpu.rs +++ b/src/arm7tdmi/cpu.rs @@ -1,3 +1,5 @@ +use std::convert::TryFrom; + use crate::num_traits::FromPrimitive; use super::arm::*; @@ -5,9 +7,16 @@ use super::sysbus::SysBus; #[derive(Debug, PartialEq)] pub enum CpuError { + ArmDecodeError(ArmDecodeError), IllegalInstruction, } +impl From for CpuError { + fn from(e: ArmDecodeError) -> CpuError { + CpuError::ArmDecodeError(e) + } +} + pub type CpuResult = Result; #[derive(Debug, PartialEq)] @@ -92,7 +101,12 @@ impl Core { } fn step_arm(&mut self, sysbus: &mut SysBus) -> CpuResult<()> { - Err(CpuError::IllegalInstruction) + // fetch + let insn = sysbus.read_32(self.pc); + // decode + let insn = ArmInstruction::try_from((insn, self.pc))?; + + Ok(()) } pub fn step(&mut self, sysbus: &mut SysBus) -> CpuResult<()> { diff --git a/src/debugger.rs b/src/debugger.rs index 07348ea..0aa26f8 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -26,9 +26,16 @@ pub struct Debugger { #[derive(Debug, PartialEq)] pub enum DebuggerError { ParsingError(String), + CpuError(cpu::CpuError), InvalidCommand(String) } +impl From for DebuggerError { + fn from(e: cpu::CpuError) -> DebuggerError { + DebuggerError::CpuError(e) + } +} + impl From> for DebuggerError { fn from(e: nom::Err<(&str, nom::error::ErrorKind)>) -> DebuggerError { DebuggerError::ParsingError("parsing of command failed".to_string()) @@ -39,11 +46,13 @@ type DebuggerResult = Result; #[derive(Debug, PartialEq)] enum DebuggerCommand { + Info, SingleStep, Continue, Disass { addr: u32, num_opcodes: usize }, AddBreakpoint(u32), ListBreakpoints, + Reset, Quit, Nop, } @@ -79,6 +88,7 @@ fn parse_debugger_command(input: &str) -> DebuggerResult { // TODO this code is shit! let (input, command_name) = parse_word(input)?; match command_name { + "i" | "info" => Ok(Info), "s" | "step" => Ok(SingleStep), "c" | "continue" => Ok(Continue), "d" | "disass" => { @@ -96,6 +106,7 @@ fn parse_debugger_command(input: &str) -> DebuggerResult { } "bl" => Ok(ListBreakpoints), "q" | "quit" => Ok(Quit), + "r" | "reset" => Ok(Reset), "" => Ok(Nop), _ => Err(DebuggerError::InvalidCommand(command_name.to_string())) } @@ -119,8 +130,13 @@ impl Debugger { let command = parse_debugger_command(&line); match command { Ok(Nop) => (), + Ok(Info) => { + println!("cpu info: {:#?}", self.cpu) + } Ok(SingleStep) => { - self.cpu.step(&mut self.sysbus).unwrap() + println!("single step:"); + self.cpu.step(&mut self.sysbus)?; + () }, Ok(Quit) => { print!("Quitting!"); @@ -141,6 +157,11 @@ impl Debugger { println!("[{}] 0x{:08x}", i, b) } } + Ok(Reset) => { + println!("resetting cpu..."); + self.cpu.reset(); + println!("cpu is restarted!") + } Err(DebuggerError::InvalidCommand(command)) => { println!("invalid command: {}", command) }