diff --git a/src/emulator/structs.rs b/src/emulator/structs.rs index df195c2..64599da 100644 --- a/src/emulator/structs.rs +++ b/src/emulator/structs.rs @@ -23,20 +23,32 @@ pub struct EmulatorState { } impl EmulatorState { - // Get the next byte from memory + // Read the next byte from memory pointed at by PC pub fn next_byte(&mut self) -> u8 { self.pc += 1; self.memory[self.pc as usize] } - // Get the next 16-bit word from memory, in little endian order + // Read the next 16-bit word from memory pointed at by PC, in little endian order pub fn next_word(&mut self) -> u16 { self.pc += 2; + self.read_word(self.pc - 1) + } + + // Read a 16-bit word at a specific address + pub fn read_word(&mut self, address: u16) -> u16 { u16::from_le_bytes([ - self.memory[self.pc as usize - 1], - self.memory[self.pc as usize], + self.memory[address as usize], + self.memory[address as usize + 1], ]) } + + // Write a 16-bit word at a specific address + pub fn write_word(&mut self, address: u16, value: u16) { + let [low, high] = u16::to_le_bytes(value); + self.memory[address as usize] = low; + self.memory[address as usize + 1] = high; + } } #[derive(Clone, Copy, PartialEq, Eq, Debug)]