Move PeekSize and PeekFormat to their own file

This commit is contained in:
pjht 2022-10-23 17:20:34 -05:00
parent 4aec215c9f
commit ffba679046
2 changed files with 93 additions and 91 deletions

View File

@ -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<char> for PeekFormat {
type Error = InvalidPeekFormat;
fn try_from(value: char) -> Result<Self, Self::Error> {
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<char> for PeekSize {
type Error = InvalidPeekSize;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'b' => Ok(Self::Byte),
'w' => Ok(Self::Word),
'l' => Ok(Self::LongWord),
_ => Err(InvalidPeekSize),
}
}
}
#[derive(Deserialize, Debug)]
struct CardConfig<'a> {

90
src/peek.rs Normal file
View File

@ -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<char> for PeekFormat {
type Error = InvalidPeekFormat;
fn try_from(value: char) -> Result<Self, Self::Error> {
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<char> for PeekSize {
type Error = InvalidPeekSize;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'b' => Ok(Self::Byte),
'w' => Ok(Self::Word),
'l' => Ok(Self::LongWord),
_ => Err(InvalidPeekSize),
}
}
}