summaryrefslogtreecommitdiff
path: root/src/components/stack.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/stack.rs')
-rw-r--r--src/components/stack.rs39
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])
+ }
+}