From 69588112045ebb4f1a8c6d561ac019be8dbbaf6d Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 2 Sep 2024 17:39:52 +1200 Subject: Improve efficiency of some instructions When a value is to be read and kept on the stack, instead of popping and immediately pushing it we now use a more efficient `get` stack method. The pop_u16 stack method has also been changed to a variation that assembles to fewer CPU instructions. --- src/stack.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/stack.rs') diff --git a/src/stack.rs b/src/stack.rs index 2a0a514..14e1799 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -28,8 +28,20 @@ impl Stack { } pub fn pop_u16(&mut self) -> u16 { - let byte_low = self.pop_u8(); - let byte_high = self.pop_u8(); + self.sp = self.sp.wrapping_sub(1); + let byte_low = self.mem[self.sp as usize]; + self.sp = self.sp.wrapping_sub(1); + let byte_high = self.mem[self.sp as usize]; + u16::from_be_bytes([byte_high, byte_low]) + } + + pub fn get_u8(&mut self) -> u8 { + self.mem[self.sp.wrapping_sub(1) as usize] + } + + pub fn get_u16(&mut self) -> u16 { + let byte_low = self.mem[self.sp.wrapping_sub(1) as usize]; + let byte_high = self.mem[self.sp.wrapping_sub(2) as usize]; u16::from_be_bytes([byte_high, byte_low]) } -- cgit v1.2.3-70-g09d2