I am writing a gameboy emulator and I have this function:
pub fn execute_classic_instruction(&mut self, opcode: u8) -> u64 {
match opcode {
0x31 => self.load_word_immediate_into_reg(&mut self.registers.sp),
_ => unimplemented!(
"Unimplemented opcode: 0x{:X} at PC=0x{:X}",
opcode,
self.registers.pc.value() - 1
),
}
}
When opcode 0x31 is next it executes this:
pub fn load_word_immediate_into_reg<F>(&mut self, register: &mut Register) -> u64 {
let value = self.mmu.read_word(self.registers.pc.value());
self.registers.pc.add(2);
register.set_value(value);
12
}
The problem of course is the double borrow that happens due to '&mut self.registers.sp'. Is there any way around that without using unsafe
, or maybe another way to do it without writing a separate function for each opcode that changes different registers?