From 79dfb820aca53761175d60c97bca87c48e9568df Mon Sep 17 00:00:00 2001 From: pjht Date: Tue, 24 Jan 2023 10:25:59 -0600 Subject: [PATCH] Make peek's format argument optional --- src/main.rs | 23 ++++++++++++++++------- src/peek.rs | 4 ++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index d306bf2..52e84ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ struct EmuState { cpu: M68K, symbol_tables: SymbolTables, address_breakpoints: IndexSet, + last_peek_format: (peek::Format, peek::Size), } /// 68K Backplane Computer Emulator @@ -75,6 +76,7 @@ fn main() -> Result<(), anyhow::Error> { cpu: M68K::new(backplane), symbol_tables, address_breakpoints: IndexSet::new(), + last_peek_format: (peek::Format::Hex, peek::Size::Byte), }; if args.run { let mut out = String::new(); @@ -241,23 +243,30 @@ fn main() -> Result<(), anyhow::Error> { .arg( Arg::new("fmt") .short('f') - .required(true) .takes_value(true) .help("The format to print the values in ()"), ) .arg(Arg::new("addr").required(true)) .about("Peek a memory address"), |args, state| { - let fmt_str = args.get_one::("fmt").unwrap(); - if fmt_str.len() != 2 { - return Err(anyhow!("Peek format length must be 2")); - } - let fmt = peek::Format::try_from(fmt_str.chars().next().unwrap())?; - let size = peek::Size::try_from(fmt_str.chars().nth(1).unwrap())?; + let (fmt, size) = if let Some(fmt_str) = args.get_one::("fmt") { + if fmt_str.len() != 2 { + return Err(anyhow!("Peek format length must be 2")); + } + let fmt = peek::Format::try_from(fmt_str.chars().next().unwrap())?; + let size = peek::Size::try_from(fmt_str.chars().nth(1).unwrap())?; + state.last_peek_format = (fmt, size); + (fmt, size) + } else { + state.last_peek_format + }; let count = parse::(args.get_one::("count").map_or("1", String::as_str))?; let addr = state .symbol_tables .parse_location_address(args.get_one::("addr").unwrap())?; + if (size != peek::Size::Byte) & ((addr & 0x1) != 0) { + return Err(anyhow!("Cannot peek words from a non-aligned address")); + } let mut data = Vec::new(); let bus = state.cpu.bus_mut(); for i in 0..count { diff --git a/src/peek.rs b/src/peek.rs index b132ff5..157268f 100644 --- a/src/peek.rs +++ b/src/peek.rs @@ -1,6 +1,6 @@ use thiserror::Error; -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Format { Octal, Hex, @@ -47,7 +47,7 @@ impl TryFrom for Format { } } -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Size { Byte, Word,