Commit b0a387d5 authored by Peter Fidelman's avatar Peter Fidelman
Browse files

Replace frustration.rs with literate-programmed version that matches the...

Replace frustration.rs with literate-programmed version that matches the README.  Use idiomatic Rust constructors.
parent 8d8c886c
......@@ -199,6 +199,18 @@ out with print!().
self.tos = (self.tos.wrapping_sub(1)) & (N - 1);
return val;
}
```
Finally, here is a function that creates a new stack.
Because these are circular stacks it doesn't matter where top-of-stack
(tos) starts off pointing. I arbitrarily set it to the highest index so
the first value pushed will wind up at index 0, again because this
makes the stack look nicer when printed out.
```rust
fn new() -> Stack<N> {
return Stack {tos: N-1, mem: [0; N]};
}
}
```
### Designing a stack CPU
......@@ -243,25 +255,21 @@ struct Core {
}
```
Now for a helper to initialize the CPU.
Finally, let's write a function that creates and returns a CPU for us to use.
```rust
fn new_core() -> Core {
let c = Core {
ram: [0; ADDRESS_SPACE],
ip: 0,
dstack: Stack {tos: 15, mem: [0; 16]},
rstack: Stack {tos: 31, mem: [0; 32]}};
use std::convert::TryInto;
return c;
}
impl Core {
fn new() -> Core {
return Core {
ram: [0; ADDRESS_SPACE],
ip: 0,
dstack: Stack::new(),
rstack: Stack::new()}
}
```
Because these are circular stacks it doesn't matter where top-of-stack
(tos) starts off pointing. I arbitrarily set it to the highest index so
the first value pushed will wind up at index 0, again because this
makes the stack look nicer when printed out.
## 1.1 - The instruction set
Now we have a CPU sitting there but it does nothing.
......@@ -290,13 +298,9 @@ So now we will make the CPU do those things.
We'll start off by teaching it how to access memory, and then we will
define the instruction set.
```rust
impl Core {
```
### Memory access
Start with a helper to read a number from the specified memory address.
Now let's write a function to read a number from the specified memory address.
```rust
fn load(&self, addr: u16) -> u16 {
......@@ -2642,7 +2646,7 @@ Finally, start the machine.
```rust
fn main() {
/* Create the machine */
let mut c = new_core();
let mut c = Core::new();
/* Put the dictionary into memory */
build_dictionary(&mut c);
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment