Add info and reset commands

This commit is contained in:
Michel Heily 2019-06-25 05:16:14 +03:00
parent fc28d89097
commit 9921f1c974
2 changed files with 37 additions and 2 deletions

View file

@ -1,3 +1,5 @@
use std::convert::TryFrom;
use crate::num_traits::FromPrimitive; use crate::num_traits::FromPrimitive;
use super::arm::*; use super::arm::*;
@ -5,9 +7,16 @@ use super::sysbus::SysBus;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum CpuError { pub enum CpuError {
ArmDecodeError(ArmDecodeError),
IllegalInstruction, IllegalInstruction,
} }
impl From<ArmDecodeError> for CpuError {
fn from(e: ArmDecodeError) -> CpuError {
CpuError::ArmDecodeError(e)
}
}
pub type CpuResult<T> = Result<T, CpuError>; pub type CpuResult<T> = Result<T, CpuError>;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -92,7 +101,12 @@ impl Core {
} }
fn step_arm(&mut self, sysbus: &mut SysBus) -> CpuResult<()> { 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<()> { pub fn step(&mut self, sysbus: &mut SysBus) -> CpuResult<()> {

View file

@ -26,9 +26,16 @@ pub struct Debugger {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum DebuggerError { pub enum DebuggerError {
ParsingError(String), ParsingError(String),
CpuError(cpu::CpuError),
InvalidCommand(String) InvalidCommand(String)
} }
impl From<cpu::CpuError> for DebuggerError {
fn from(e: cpu::CpuError) -> DebuggerError {
DebuggerError::CpuError(e)
}
}
impl From<nom::Err<(&str, nom::error::ErrorKind)>> for DebuggerError { impl From<nom::Err<(&str, nom::error::ErrorKind)>> for DebuggerError {
fn from(e: nom::Err<(&str, nom::error::ErrorKind)>) -> DebuggerError { fn from(e: nom::Err<(&str, nom::error::ErrorKind)>) -> DebuggerError {
DebuggerError::ParsingError("parsing of command failed".to_string()) DebuggerError::ParsingError("parsing of command failed".to_string())
@ -39,11 +46,13 @@ type DebuggerResult<T> = Result<T, DebuggerError>;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
enum DebuggerCommand { enum DebuggerCommand {
Info,
SingleStep, SingleStep,
Continue, Continue,
Disass { addr: u32, num_opcodes: usize }, Disass { addr: u32, num_opcodes: usize },
AddBreakpoint(u32), AddBreakpoint(u32),
ListBreakpoints, ListBreakpoints,
Reset,
Quit, Quit,
Nop, Nop,
} }
@ -79,6 +88,7 @@ fn parse_debugger_command(input: &str) -> DebuggerResult<DebuggerCommand> {
// TODO this code is shit! // TODO this code is shit!
let (input, command_name) = parse_word(input)?; let (input, command_name) = parse_word(input)?;
match command_name { match command_name {
"i" | "info" => Ok(Info),
"s" | "step" => Ok(SingleStep), "s" | "step" => Ok(SingleStep),
"c" | "continue" => Ok(Continue), "c" | "continue" => Ok(Continue),
"d" | "disass" => { "d" | "disass" => {
@ -96,6 +106,7 @@ fn parse_debugger_command(input: &str) -> DebuggerResult<DebuggerCommand> {
} }
"bl" => Ok(ListBreakpoints), "bl" => Ok(ListBreakpoints),
"q" | "quit" => Ok(Quit), "q" | "quit" => Ok(Quit),
"r" | "reset" => Ok(Reset),
"" => Ok(Nop), "" => Ok(Nop),
_ => Err(DebuggerError::InvalidCommand(command_name.to_string())) _ => Err(DebuggerError::InvalidCommand(command_name.to_string()))
} }
@ -119,8 +130,13 @@ impl Debugger {
let command = parse_debugger_command(&line); let command = parse_debugger_command(&line);
match command { match command {
Ok(Nop) => (), Ok(Nop) => (),
Ok(Info) => {
println!("cpu info: {:#?}", self.cpu)
}
Ok(SingleStep) => { Ok(SingleStep) => {
self.cpu.step(&mut self.sysbus).unwrap() println!("single step:");
self.cpu.step(&mut self.sysbus)?;
()
}, },
Ok(Quit) => { Ok(Quit) => {
print!("Quitting!"); print!("Quitting!");
@ -141,6 +157,11 @@ impl Debugger {
println!("[{}] 0x{:08x}", i, b) println!("[{}] 0x{:08x}", i, b)
} }
} }
Ok(Reset) => {
println!("resetting cpu...");
self.cpu.reset();
println!("cpu is restarted!")
}
Err(DebuggerError::InvalidCommand(command)) => { Err(DebuggerError::InvalidCommand(command)) => {
println!("invalid command: {}", command) println!("invalid command: {}", command)
} }