diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-03-25 12:00:15 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-03-25 12:00:15 +1300 |
commit | c23c47325a7818c4df4097878f101cd80e2fe361 (patch) | |
tree | f7d591c90428d43064be10de660021c2cd6dc863 /src/components/stack.rs | |
parent | 179bd6a13d91f0a1137ee8ed6aebb7e226e99b5d (diff) | |
download | bedrock-core-c23c47325a7818c4df4097878f101cd80e2fe361.zip |
Partially restructure the library
This also changes bit-shifting semantics, shifting left and then right
instead of the other way around.
Diffstat (limited to 'src/components/stack.rs')
-rw-r--r-- | src/components/stack.rs | 39 |
1 files changed, 39 insertions, 0 deletions
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]) + } +} |