From 2b16a99ba3d1ba6536a1db28471288a0c2274a71 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 24 Dec 2023 19:54:50 +1300 Subject: First commit --- src/stack.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/stack.rs (limited to 'src/stack.rs') diff --git a/src/stack.rs b/src/stack.rs new file mode 100644 index 0000000..2a0a514 --- /dev/null +++ b/src/stack.rs @@ -0,0 +1,40 @@ +pub struct Stack { + pub mem: [u8; 256], + pub sp: u8, +} + +impl Stack { + pub fn new() -> Self { + Self { + mem: [0; 256], + sp: 0, + } + } + + pub fn push_u8(&mut self, val: u8) { + self.mem[self.sp as usize] = val; + self.sp = self.sp.wrapping_add(1); + } + + pub fn pop_u8(&mut self) -> u8 { + self.sp = self.sp.wrapping_sub(1); + self.mem[self.sp as usize] + } + + pub fn push_u16(&mut self, val: u16) { + let [byte_high, byte_low] = u16::to_be_bytes(val); + self.push_u8(byte_high); + self.push_u8(byte_low); + } + + pub fn pop_u16(&mut self) -> u16 { + let byte_low = self.pop_u8(); + let byte_high = self.pop_u8(); + u16::from_be_bytes([byte_high, byte_low]) + } + + pub fn reset(&mut self) { + self.mem.fill(0); + self.sp = 0; + } +} -- cgit v1.2.3-70-g09d2