Replace frustration.rs with literate-programmed version that matches the README. Use idiomatic Rust constructors.

This commit is contained in:
Peter Fidelman 2022-05-29 11:18:35 -07:00
parent 8d8c886cf4
commit b0a387d5a1
2 changed files with 1765 additions and 1475 deletions

View file

@ -199,6 +199,18 @@ out with print!().
self.tos = (self.tos.wrapping_sub(1)) & (N - 1); self.tos = (self.tos.wrapping_sub(1)) & (N - 1);
return val; 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 ### 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 ```rust
fn new_core() -> Core { use std::convert::TryInto;
let c = Core {
ram: [0; ADDRESS_SPACE],
ip: 0,
dstack: Stack {tos: 15, mem: [0; 16]},
rstack: Stack {tos: 31, mem: [0; 32]}};
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 ## 1.1 - The instruction set
Now we have a CPU sitting there but it does nothing. 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 We'll start off by teaching it how to access memory, and then we will
define the instruction set. define the instruction set.
```rust
impl Core {
```
### Memory access ### 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 ```rust
fn load(&self, addr: u16) -> u16 { fn load(&self, addr: u16) -> u16 {
@ -2642,7 +2646,7 @@ Finally, start the machine.
```rust ```rust
fn main() { fn main() {
/* Create the machine */ /* Create the machine */
let mut c = new_core(); let mut c = Core::new();
/* Put the dictionary into memory */ /* Put the dictionary into memory */
build_dictionary(&mut c); build_dictionary(&mut c);

File diff suppressed because it is too large Load diff