reading and writing words at addresses

This commit is contained in:
Martin Löffler 2023-01-28 05:13:10 +01:00
parent eb993eb1b4
commit 8a1c99ee39
Signed by: FatalErrorCoded
GPG key ID: FFEF368AC076566A

View file

@ -23,20 +23,32 @@ pub struct EmulatorState {
} }
impl 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 { pub fn next_byte(&mut self) -> u8 {
self.pc += 1; self.pc += 1;
self.memory[self.pc as usize] 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 { pub fn next_word(&mut self) -> u16 {
self.pc += 2; 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([ u16::from_le_bytes([
self.memory[self.pc as usize - 1], self.memory[address as usize],
self.memory[self.pc 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)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]