From 5c5a8722e4aee1bdfe52b28c36cd3755df36380f Mon Sep 17 00:00:00 2001 From: Ry Date: Sun, 18 Sep 2022 22:09:29 -0700 Subject: [PATCH] Possibly fix an issue where a 32 bit page fault would cause 4 PFs Same for 16 bit page faults causing 2 page faults, one for each byte. --- src/memory.rs | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/memory.rs b/src/memory.rs index 394d19a..57c8bd6 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -201,14 +201,12 @@ impl Memory { } } pub fn read_opt_16(&mut self, address: u32) -> Option { - Some( - (self.read_opt_8(address)? as u16) | + Some(self.read_opt_8(address)? as u16 | (self.read_opt_8(address + 1)? as u16) << 8 ) } pub fn read_opt_32(&mut self, address: u32) -> Option { - Some( - (self.read_opt_8(address)? as u32) | + Some(self.read_opt_8(address)? as u32 | (self.read_opt_8(address + 1)? as u32) << 8 | (self.read_opt_8(address + 2)? as u32) << 16 | (self.read_opt_8(address + 3)? as u32) << 24 @@ -226,26 +224,16 @@ impl Memory { } } pub fn read_16(&mut self, address: u32) -> Option { - let mut read_ok = true; - let value = self.read_8(address).unwrap_or_else(|| { read_ok = false; 0 }) as u16 | - (self.read_8(address + 1).unwrap_or_else(|| { read_ok = false; 0 }) as u16) << 8; - if read_ok { - Some(value) - } else { - None - } + Some(self.read_8(address)? as u16 | + (self.read_8(address + 1)? as u16) << 8 + ) } pub fn read_32(&mut self, address: u32) -> Option { - let mut read_ok = true; - let value = self.read_8(address).unwrap_or_else(|| { read_ok = false; 0 }) as u32 | - (self.read_8(address + 1).unwrap_or_else(|| { read_ok = false; 0 }) as u32) << 8 | - (self.read_8(address + 2).unwrap_or_else(|| { read_ok = false; 0 }) as u32) << 16 | - (self.read_8(address + 3).unwrap_or_else(|| { read_ok = false; 0 }) as u32) << 24; - if read_ok { - Some(value) - } else { - None - } + Some(self.read_8(address)? as u32 | + (self.read_8(address + 1)? as u32) << 8 | + (self.read_8(address + 2)? as u32) << 16 | + (self.read_8(address + 3)? as u32) << 24 + ) } pub fn write_8(&mut self, mut address: u32, byte: u8) -> Option<()> {