diff --git a/src/cpu_65816/cpu.rs b/src/cpu_65816/cpu.rs index 2fbedec..137fc38 100644 --- a/src/cpu_65816/cpu.rs +++ b/src/cpu_65816/cpu.rs @@ -91,18 +91,19 @@ where "{} - {}\n --> {}", self.cycles, self.regs, - self.peek_next_instr().unwrap() + self.peek_next_instr() ) } /// Fetches and decodes the next instruction at PC - pub fn peek_next_instr(&self) -> Result { + pub fn peek_next_instr(&self) -> Instruction { let mut busiter = BusIterator::new_from(&self.bus, self.regs.get_full_pc()); Instruction::decode( &mut busiter, self.regs.test_flag(Flag::M), self.regs.test_flag(Flag::X), ) + .expect("65816 instruction decode error") } /// Fetches and decodes the next instruction at PC @@ -116,8 +117,8 @@ where p += 1; match Instruction::decode(&mut fetched.clone().into_iter(), m, x) { - Err(_) => fetched.push(self.read_tick(pc)), - Ok(i) => break i, + None => fetched.push(self.read_tick(pc)), + Some(i) => break i, } }; diff --git a/src/cpu_65816/instruction.rs b/src/cpu_65816/instruction.rs index c9143ec..fca1f1a 100644 --- a/src/cpu_65816/instruction.rs +++ b/src/cpu_65816/instruction.rs @@ -2,7 +2,6 @@ use std::fmt; use anyhow::Result; use arrayvec::ArrayVec; -use thiserror::Error; use super::instruction_table::INSTRUCTION_TABLE; @@ -229,12 +228,6 @@ pub enum InstructionType { Undefined, } -#[derive(Debug, Error)] -enum DecodeErr { - #[error("End of instruction stream")] - EndOfStream, -} - /// A definition in the instruction (op code) table pub struct InstructionDef { /// String representation @@ -269,12 +262,13 @@ pub struct Instruction { impl Instruction { /// Try to decode a single instruction from an iterator. - pub fn decode(stream: &mut impl Iterator, m: bool, x: bool) -> Result { + pub fn decode(stream: &mut impl Iterator, m: bool, x: bool) -> Option { let mut raw: ArrayVec = ArrayVec::new(); - let mut rd = || -> Result { - let b = stream.next().ok_or(DecodeErr::EndOfStream)?; + let mut rd = || -> Option { + // Assuming the stream wraps at the end, no errors will be thrown. + let b = stream.next()?; raw.push(b); - Ok(b) + Some(b) }; let opcode = rd()?; @@ -295,7 +289,7 @@ impl Instruction { _ => immediate[0] = u32::from_le_bytes(args), } - Ok(Instruction { + Some(Instruction { def, immediate, raw,