From c23c47325a7818c4df4097878f101cd80e2fe361 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 25 Mar 2025 12:00:15 +1300 Subject: Partially restructure the library This also changes bit-shifting semantics, shifting left and then right instead of the other way around. --- src/components/stack.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 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..bbd7b68 --- /dev/null +++ b/src/components/stack.rs @@ -0,0 +1,39 @@ +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 [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); + } + + 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