Engineer at MX.com
Volunteer Teaching Refugees to Code
Musician!
Lover of Haskell!
From First Principles
Build a Modern Computer
From First Principles
1. Exercise & Tools Repo: https://gitlab.com/paniclater/nand-to-lambda-workshop
2. Haskell: https://www.haskell.org/platform/
3. Hardware Simulation: http://www.nand2tetris.org/software.php
CHIP MyChip {
IN a, b; // can be many ins
OUT out; // can be many outs
PARTS:
//Here we connect inputs and outputs using other chips!
//Wire the inputs a & b to BuiltInChip inputs,
//assign its output to a variable
BuiltInChip(aInput=a, bInput=b, out=myVar)
//Wire in the variable myVar (output of other chip) to BuiltInChips input,
//wire its output to the output of MyChip
BuiltInChip(a=myVar, b=myVar, out=out)
}
| Input A | Input B | Output |
|---|---|---|
| High | High | Low |
| High | Low | High |
| Low | High | High |
| Low | Low | High |
| Input A | Input B |
|---|---|
| High | Low |
| Low | High |
| Operand | Operand | Sum | Carry |
|---|---|---|---|
| 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 |
| 0 | 0 | 0 | 0 |
| Operand | Operand | Operand | Sum | Carry |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 0 | 0 |
Extra stars if you use monoid instances in your haskell implementation!
-x = 2^n - x
where x is a positive integer
n is the total binary digits availiable
In a 5 bit binary system n is 5
2^5 - 2 = 30
"two" x would be 00010
"thirty" is 11110
11110 + 00010 = 100000
Since there are only 5 bits, we trim off the 1 and get 00000
The system can code a total of 2^n signed numbers
Max and min numbers are (2^n) - 1 and -2^ (n-1)
All negative numbers begin with 1
All positive numbers begin with 0
To get (-x) from x one can leave all the trailing 0s, keep the least significant 1 intact, then flip all the remaining bits
No trailing zeros, keep 1.
Flip the other bits
lets try with 1
Keep the trailing zero and the first 1.
Flip the other bits
lets try with 6
Flip all the bits and add 1
lets try with 6
flip bits
add 1
We've already implemented increment by 1 gates!
We've already implemented bit flipping gates!
We've already solved negative numbers and subtraction!
Inputs:
16 bit bus x[16]
16 bit bus y[16]
Control bit zx
Control bit nx
Control bit zy
Control bit ny
Control bit f
Control bit no
Outputs:
Control bit zr
Control bit ng
16 bit bus z[16]
Rules:
// Computes R0 = 2 + 3 (R0 refers to RAM[0])
@2
D=A
@3
D=D+A
@0
M=D
/***
Predefined Symbols
A: Address Register.
D: Data Register.
M: Refers to the register in Main Memory whose address is currently stored in A.
SP: RAM address 0.
LCL: RAM address 1.
ARG: RAM address 2.
THIS: RAM address 3.
THAT: RAM address 4.
R0-R15: Addresses of 16 RAM Registers, mapped from 0 to 15.
SCREEN: Base address of the Screen Map in Main Memory, which is equal to 16384.
KBD: Keyboard Register address in Main Memory, which is equal to 24576.
**/A, D, M
If the address is concrete, store that address in the A register.
If the address is symbolic, look up the symbol's address and then store that address in the A register.
//Destination can be A, D, M or some combination
A
D
M
AD
ADM
//Computation with operators +,-,|,!,& and arguments A,D,M,1,0
A+D
D-M
!A
A&M
//Jump can be:
JMP
JEQ
JNE
JLT
JGE
JGT
JLE
//Computation only
D + M
//Computation and Destination
D=0
//Computation and Jump
D-1;JEQ
//All Three
M=A&D;JGT /***
Predefined Symbols
A: Address Register.
D: Data Register.
M: Refers to the register in Main Memory whose address is currently stored in A.
SP: RAM address 0.
LCL: RAM address 1.
ARG: RAM address 2.
THIS: RAM address 3.
THAT: RAM address 4.
R0-R15: Addresses of 16 RAM Registers, mapped from 0 to 15.
SCREEN: Base address of the Screen Map in Main Memory, which is equal to 16384.
KBD: Keyboard Register address in Main Memory, which is equal to 24576.
**/wraps a value into an applicative
applies a function contained by an applicative to a value held in another
the (<*>) that keeps trying!
Stephen Diel: Parsing
http://dev.stephendiehl.com/fun/002_parsers.html
Allele Dev: Applicatives and Alternatives
https://queertypes.com/posts/59-applicatives-alternatives.html
Allele Dev: Applicatives and Alternatives
https://queertypes.com/posts/59-applicatives-alternatives.html
Haskell Programming From First Principles: Chapter 14
http://haskellbook.com/