From 030ca2281206d0b8d48d3e590becd3932ef9051f Mon Sep 17 00:00:00 2001 From: pjht Date: Thu, 7 Dec 2023 13:56:41 -0600 Subject: [PATCH] Allow dissasembly errors to include abnormal traps --- src/m68k.rs | 26 +++++++++++++++++++------- src/main.rs | 3 ++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/m68k.rs b/src/m68k.rs index adbdd1d..8649612 100644 --- a/src/m68k.rs +++ b/src/m68k.rs @@ -144,8 +144,14 @@ impl From for InsExecError { } } - - +impl Display for InsExecError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + InsExecError::BusError(err) => f.write_fmt(format_args!("{}", err)), + InsExecError::AbnormalTrap => f.write_str("Abnormal trap"), + } + } +} #[derive(Debug)] pub struct M68K { @@ -243,11 +249,14 @@ impl M68K { pub fn disassemble( &mut self, loc: u32, - ) -> Result<(Instruction, u32), DisassemblyError> { + ) -> Result<(Instruction, u32), DisassemblyError> { disas::disasm(loc, &mut |addr| { - self.read_word(addr).map_err(|err| DetailedBusError { - cause: BusErrorCause::ReadingInstruction, - ..err.try_into_bus_error().unwrap() + self.read_word(addr).map_err(|err| match err { + InsExecError::AbnormalTrap => InsExecError::AbnormalTrap, + InsExecError::BusError(err) => InsExecError::BusError(DetailedBusError { + cause: BusErrorCause::ReadingInstruction, + ..err + }), }) }) } @@ -316,7 +325,10 @@ impl M68K { } let (ins, new_pc) = match self.disassemble(self.pc) { Ok(ins) => ins, - Err(DisassemblyError::InvalidInstruction) => panic!("Invalid instruction"), + Err(DisassemblyError::InvalidInstruction) => { + self.trap(4)?; + return self.step_ret_berr(); + } Err(DisassemblyError::ReadError(e)) => return Err(e.into()), }; self.pc = new_pc; diff --git a/src/main.rs b/src/main.rs index 4960222..06bace2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use clap::Parser; use disas::DisassemblyError; use indexmap::IndexSet; use itertools::Itertools; +use m68k::InsExecError; use parse_int::parse; use reedline_repl_rs::{ clap::{builder::BoolishValueParser, Arg, ArgAction, Command}, @@ -527,7 +528,7 @@ fn disas_fmt( cpu: &mut M68K, addr: u32, symbol_tables: &SymbolTables, -) -> (String, Result>) { +) -> (String, Result>) { let addr_fmt = if let Some((table, symbol, offset)) = symbol_tables.address_to_symbol(addr) { format!("{table}:{symbol} + {offset} (0x{addr:x})") } else {