Skip to content

Commit

Permalink
feat: make the stack grow from 0x3000 to 0xFFFF
Browse files Browse the repository at this point in the history
  • Loading branch information
heypoom committed Oct 5, 2023
1 parent 584c1ae commit 244ba6a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 37 deletions.
31 changes: 9 additions & 22 deletions IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,23 @@ R14: 0x0E
Stores the status flags of the program execution.
```

### Base Memory Layout
### 04. Memory Layout

```
0x0000 - 0x1000
Code Segment: `text`
Stores the program instructions.
Read-only.
0x1000 - 0x2000
Initialized Data Segment: `data`
Contains the variables initialized in the code.
TODO: const variables should be read-only. needs separate sections.
0x2000 - 0x3000
Uninitialized Data Segment: `bss`
Initializes to zero.
Contains all global variables that are initialized to zero, or do not have an explicit initialization in code, such as `int i`
0x1000 - 0x3000
Read-only data, which is not code.
after 0x3000
Heap.
Contains all dynamically allocated memory.
Grows to larger addresses.
Use malloc, realloc and free.
below 0xFFFF
Stack.
Stack is LIFO.
Contains the program stack frames.
Stack frames contain the local variables of the function.
Makes recursion possible.
Stack, LIFO.
SP tracks the top of the stack.
Grows downwards.
Grows to 0xFFFF.
```

## 05. Stack Frames

- Stack frames contain the local variables of the function call.
4 changes: 2 additions & 2 deletions src/mem/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem::MAX_STACK_ADDR;
use crate::MIN_STACK_ADDR;

pub const MEMORY_SIZE: u16 = 0xFFFF;

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Memory {
}

pub fn read_stack(&self, count: u16) -> Vec<u16> {
self.read(MAX_STACK_ADDR - count, count)
self.read(MIN_STACK_ADDR, count)
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/mem/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ impl<'a> StackManager<'a> {
}

pub fn push(&mut self, val: u16) -> Result<(), Whatever> {
if self.top() < MIN_STACK_ADDR {
if self.top() > MAX_STACK_ADDR {
whatever!("stack overflow")
}

// Decrement the stack pointer.
self.reg.dec(SP);
// Increment the stack pointer.
self.reg.inc(SP);

// Save the value at the top of the stack.
self.write(val);
Expand All @@ -50,15 +50,17 @@ impl<'a> StackManager<'a> {
}

pub fn pop(&mut self) -> Result<u16, Whatever> {
if self.top() > MAX_STACK_ADDR {
if self.top() < MIN_STACK_ADDR {
whatever!("stack underflow")
}

let v = self.peek();

// Clear the value at the top of the stack.
self.write(0);

// Increment the stack pointer.
self.reg.inc(SP);
// Decrement the stack pointer.
self.reg.dec(SP);

// Return the value at the top of the stack.
Ok(v)
Expand Down
4 changes: 2 additions & 2 deletions src/register/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::mem::MAX_STACK_ADDR;
use crate::MIN_STACK_ADDR;
use crate::register::Register::{PC, SP};

const REG_COUNT: usize = 0xF;
Expand Down Expand Up @@ -28,7 +28,7 @@ impl Registers {

// Initialize the stack pointer.
v.set(PC, 0);
v.set(SP, MAX_STACK_ADDR);
v.set(SP, MIN_STACK_ADDR);

v
}
Expand Down
6 changes: 3 additions & 3 deletions tests/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ mod tests {

m.tick();
m.tick();
assert_eq!(m.mem.read_stack(3), [0, 10, 5]);
assert_eq!(m.mem.read_stack(3), [0, 5, 10]);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 15]);
assert_eq!(m.mem.read_stack(3), [0, 15, 0]);

m.tick();
assert_eq!(m.stack().peek(), 3);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 12]);
assert_eq!(m.mem.read_stack(3), [0, 12, 0]);
}
}
4 changes: 2 additions & 2 deletions tests/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod tests {
assert_eq!(m.mem.read_stack(5), [0, 0, 0, 0, 0]);

m.run();
assert_eq!(m.mem.read_stack(5), [111, 108, 108, 101, 104]);
assert_eq!(m.mem.read_stack(6), [0, 104, 101, 108, 108, 111]);
}

#[test]
Expand Down Expand Up @@ -50,6 +50,6 @@ mod tests {
let s_addr = ms.add_str("poom");
m.mem.load_code(vec![I::LoadString(s_addr)]);
m.tick();
assert_eq!(m.mem.read_stack(5), [0, 109, 111, 111, 112]);
assert_eq!(m.mem.read_stack(5), [0, 112, 111, 111, 109]);
}
}

0 comments on commit 244ba6a

Please sign in to comment.