Skip to content

Commit

Permalink
65816: remove hidden allocation (in anyhow) from instruction decode
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Jun 8, 2024
1 parent ba02a92 commit e4f2f80
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/cpu_65816/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instruction> {
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
Expand All @@ -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,
}
};

Expand Down
18 changes: 6 additions & 12 deletions src/cpu_65816/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::fmt;

use anyhow::Result;
use arrayvec::ArrayVec;
use thiserror::Error;

use super::instruction_table::INSTRUCTION_TABLE;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Item = u8>, m: bool, x: bool) -> Result<Instruction> {
pub fn decode(stream: &mut impl Iterator<Item = u8>, m: bool, x: bool) -> Option<Instruction> {
let mut raw: ArrayVec<u8, MAX_INSTRUCTION_LEN> = ArrayVec::new();
let mut rd = || -> Result<u8> {
let b = stream.next().ok_or(DecodeErr::EndOfStream)?;
let mut rd = || -> Option<u8> {
// 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()?;
Expand All @@ -295,7 +289,7 @@ impl Instruction {
_ => immediate[0] = u32::from_le_bytes(args),
}

Ok(Instruction {
Some(Instruction {
def,
immediate,
raw,
Expand Down

0 comments on commit e4f2f80

Please sign in to comment.