summaryrefslogtreecommitdiff
path: root/src/stack.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2023-12-24 19:54:50 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2023-12-24 19:54:50 +1300
commit2b16a99ba3d1ba6536a1db28471288a0c2274a71 (patch)
tree7df2fc16f6cf81a4d7c313a6c65e53183c721d9d /src/stack.rs
downloadbedrock-core-4e9e851914a4c21e1fbac009ebae8c9d5fd5f6f2.zip
First commitv1.0.0
Diffstat (limited to 'src/stack.rs')
-rw-r--r--src/stack.rs40
1 files changed, 40 insertions, 0 deletions
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;
+ }
+}