From 701448be2c3c58e30960d46f090bf08adfc02832 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Thu, 3 Jul 2025 14:53:16 +1200 Subject: Initial commit --- src/components/stack.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/components/stack.rs (limited to 'src/components/stack.rs') 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]) + } +} -- cgit v1.2.3-70-g09d2