From 131b0f312db3e9676aa9eb617ee1da28820653dd Mon Sep 17 00:00:00 2001 From: Sam M W Date: Sun, 2 Apr 2023 21:35:18 +0100 Subject: [PATCH] remove get_byte_mut_ref method from Memory This is the right thing to do, because the actual hardware will perform a read and then perform a write. This could have observable side effects on a real system --- src/cpu.rs | 42 ++++++++++++++++++------------------------ src/memory.rs | 4 ---- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index ff81865..c4208bc 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -103,10 +103,9 @@ impl CPU { self.registers.accumulator = val as i8; } (Instruction::ASL, OpInput::UseAddress(addr)) => { - CPU::shift_left_with_flags( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::shift_left_with_flags(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::BCC, OpInput::UseRelative(rel)) => { @@ -208,10 +207,9 @@ impl CPU { } (Instruction::DEC, OpInput::UseAddress(addr)) => { - CPU::decrement( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::decrement(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::DEY, OpInput::UseImplied) => { @@ -231,10 +229,9 @@ impl CPU { } (Instruction::INC, OpInput::UseAddress(addr)) => { - CPU::increment( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::increment(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::INX, OpInput::UseImplied) => { CPU::increment(&mut self.registers.index_x, &mut self.registers.status); @@ -282,10 +279,9 @@ impl CPU { self.registers.accumulator = val as i8; } (Instruction::LSR, OpInput::UseAddress(addr)) => { - CPU::shift_right_with_flags( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::shift_right_with_flags(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::ORA, OpInput::UseImmediate(val)) => { @@ -327,10 +323,9 @@ impl CPU { self.registers.accumulator = val as i8; } (Instruction::ROL, OpInput::UseAddress(addr)) => { - CPU::rotate_left_with_flags( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::rotate_left_with_flags(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::ROR, OpInput::UseImplied) => { // Accumulator mode @@ -339,10 +334,9 @@ impl CPU { self.registers.accumulator = val as i8; } (Instruction::ROR, OpInput::UseAddress(addr)) => { - CPU::rotate_right_with_flags( - self.memory.get_byte_mut_ref(addr), - &mut self.registers.status, - ); + let mut operand: u8 = self.memory.get_byte(addr); + CPU::rotate_right_with_flags(&mut operand, &mut self.registers.status); + self.memory.set_byte(addr, operand); } (Instruction::SBC, OpInput::UseImmediate(val)) => { diff --git a/src/memory.rs b/src/memory.rs index 1929365..097c110 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -69,10 +69,6 @@ impl Memory { self.bytes[address as usize] } - pub fn get_byte_mut_ref(&mut self, address: u16) -> &mut u8 { - &mut self.bytes[address as usize] - } - pub fn get_slice(&self, start: u16, diff: u16) -> &[u8] { let orig: usize = start.into(); let end = orig + diff as usize;