diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-09-02 17:39:52 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-09-03 13:12:29 +1200 |
commit | 69588112045ebb4f1a8c6d561ac019be8dbbaf6d (patch) | |
tree | 2874329dc9f1f9cbcfe130f4771dcc505a3a1eef /src/stack.rs | |
parent | f6e9349f60d003fca8dce6d4f6c76a1ad0c0e56d (diff) | |
download | bedrock-core-69588112045ebb4f1a8c6d561ac019be8dbbaf6d.zip |
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.
Diffstat (limited to 'src/stack.rs')
-rw-r--r-- | src/stack.rs | 16 |
1 files changed, 14 insertions, 2 deletions
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]) } |