diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2025-07-03 14:53:16 +1200 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2025-07-03 14:53:16 +1200 |
commit | 701448be2c3c58e30960d46f090bf08adfc02832 (patch) | |
tree | 409be74cdbb4bf311cb98fbee14f67fcd9c680d0 /src/components/stack.rs | |
download | bedrock-core-701448be2c3c58e30960d46f090bf08adfc02832.zip |
Initial commit
Diffstat (limited to 'src/components/stack.rs')
-rw-r--r-- | src/components/stack.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/components/stack.rs b/src/components/stack.rs new file mode 100644 index 0000000..3a5bdd7 --- /dev/null +++ b/src/components/stack.rs @@ -0,0 +1,43 @@ +pub struct Stack { + pub mem: [u8; 256], + pub sp: u8, +} + +impl Stack { + pub fn new() -> Self { + Self { + mem: [0; 256], + sp: 0, + } + } + + // Push a u8 to the stack. + pub fn push_u8(&mut self, val: u8) { + self.mem[self.sp as usize] = val; + self.sp = self.sp.wrapping_add(1); + } + + // Pop a u8 from the stack. + pub fn pop_u8(&mut self) -> u8 { + self.sp = self.sp.wrapping_sub(1); + self.mem[self.sp as usize] + } + + // Push a u16 to the stack. + pub fn push_u16(&mut self, val: u16) { + let [high, low] = u16::to_be_bytes(val); + self.mem[self.sp as usize] = high; + self.sp = self.sp.wrapping_add(1); + self.mem[self.sp as usize] = low; + self.sp = self.sp.wrapping_add(1); + } + + // Pop a u16 from the stack. + pub fn pop_u16(&mut self) -> u16 { + self.sp = self.sp.wrapping_sub(1); + let low = self.mem[self.sp as usize]; + self.sp = self.sp.wrapping_sub(1); + let high = self.mem[self.sp as usize]; + u16::from_be_bytes([high, low]) + } +} |