diff --git a/src/main.rs b/src/main.rs index 4f1c1c7..a7202f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod disas; mod instruction; mod location; mod m68k; +mod peek; mod ram; mod rom; mod storage; @@ -24,104 +25,15 @@ use disas::DisassemblyError; use indexmap::IndexSet; use itertools::Itertools; use parse_int::parse; +use peek::{PeekFormat, PeekSize}; use reedline_repl_rs::{ clap::{builder::BoolishValueParser, Arg, ArgAction, Command}, Error as ReplError, Repl, }; use serde::Deserialize; use serde_yaml::Mapping; -use std::{convert::TryFrom, fs, path::Path, process}; +use std::{fs, path::Path, process}; use symbol_tables::SymbolTables; -use thiserror::Error; - -#[derive(Copy, Clone, Debug)] -enum PeekFormat { - Octal, - Hex, - Decimal, - UnsignedDecimal, - Binary, -} - -impl PeekFormat { - pub fn format(self, num: u32, size: PeekSize) -> String { - match self { - Self::Octal => format!("Oo{:0>width$o}", num, width = size.byte_count()), - Self::Hex => format!("0x{:0>width$x}", num, width = size.byte_count() * 2), - Self::Decimal => { - let num = match size { - PeekSize::Byte => num as u8 as i8 as i32, - PeekSize::Word => num as u16 as i16 as i32, - PeekSize::LongWord => num as i32, - }; - format!("{}", num) - } - Self::UnsignedDecimal => format!("{}", num), - Self::Binary => format!("0b{:0>width$b}", num, width = size.byte_count() * 8), - } - } -} - -#[derive(Debug, Copy, Clone, Error)] -#[error("Invalid peek format")] -struct InvalidPeekFormat; - -impl TryFrom for PeekFormat { - type Error = InvalidPeekFormat; - - fn try_from(value: char) -> Result { - match value { - 'o' => Ok(Self::Octal), - 'x' => Ok(Self::Hex), - 'd' => Ok(Self::Decimal), - 'u' => Ok(Self::UnsignedDecimal), - 'b' => Ok(Self::Binary), - _ => Err(InvalidPeekFormat), - } - } -} - -#[derive(Copy, Clone, Debug)] -enum PeekSize { - Byte, - Word, - LongWord, -} - -impl PeekSize { - fn byte_count(self) -> usize { - match self { - PeekSize::Byte => 1, - PeekSize::Word => 2, - PeekSize::LongWord => 4, - } - } - - fn chunk_size(self) -> usize { - match self { - PeekSize::Byte => 8, - PeekSize::Word => 8, - PeekSize::LongWord => 4, - } - } -} - -#[derive(Debug, Copy, Clone, Error)] -#[error("Invalid peek size")] -struct InvalidPeekSize; - -impl TryFrom for PeekSize { - type Error = InvalidPeekSize; - - fn try_from(value: char) -> Result { - match value { - 'b' => Ok(Self::Byte), - 'w' => Ok(Self::Word), - 'l' => Ok(Self::LongWord), - _ => Err(InvalidPeekSize), - } - } -} #[derive(Deserialize, Debug)] struct CardConfig<'a> { diff --git a/src/peek.rs b/src/peek.rs new file mode 100644 index 0000000..f01bfe8 --- /dev/null +++ b/src/peek.rs @@ -0,0 +1,90 @@ +use thiserror::Error; + +#[derive(Copy, Clone, Debug)] +pub enum PeekFormat { + Octal, + Hex, + Decimal, + UnsignedDecimal, + Binary, +} + +impl PeekFormat { + pub fn format(self, num: u32, size: PeekSize) -> String { + match self { + Self::Octal => format!("Oo{:0>width$o}", num, width = size.byte_count()), + Self::Hex => format!("0x{:0>width$x}", num, width = size.byte_count() * 2), + Self::Decimal => { + let num = match size { + PeekSize::Byte => num as u8 as i8 as i32, + PeekSize::Word => num as u16 as i16 as i32, + PeekSize::LongWord => num as i32, + }; + format!("{}", num) + } + Self::UnsignedDecimal => format!("{}", num), + Self::Binary => format!("0b{:0>width$b}", num, width = size.byte_count() * 8), + } + } +} + +#[derive(Debug, Copy, Clone, Error)] +#[error("Invalid peek format")] +pub struct InvalidPeekFormat; + +impl TryFrom for PeekFormat { + type Error = InvalidPeekFormat; + + fn try_from(value: char) -> Result { + match value { + 'o' => Ok(Self::Octal), + 'x' => Ok(Self::Hex), + 'd' => Ok(Self::Decimal), + 'u' => Ok(Self::UnsignedDecimal), + 'b' => Ok(Self::Binary), + _ => Err(InvalidPeekFormat), + } + } +} + +#[derive(Copy, Clone, Debug)] +pub enum PeekSize { + Byte, + Word, + LongWord, +} + +impl PeekSize { + pub fn byte_count(self) -> usize { + match self { + PeekSize::Byte => 1, + PeekSize::Word => 2, + PeekSize::LongWord => 4, + } + } + + pub fn chunk_size(self) -> usize { + match self { + PeekSize::Byte => 8, + PeekSize::Word => 8, + PeekSize::LongWord => 4, + } + } +} + +#[derive(Debug, Copy, Clone, Error)] +#[error("Invalid peek size")] +pub struct InvalidPeekSize; + +impl TryFrom for PeekSize { + type Error = InvalidPeekSize; + + fn try_from(value: char) -> Result { + match value { + 'b' => Ok(Self::Byte), + 'w' => Ok(Self::Word), + 'l' => Ok(Self::LongWord), + _ => Err(InvalidPeekSize), + } + } +}