[05/CPU] CPU is done.

I prefered using Nands here, wherever possible.
This commit is contained in:
Nemo 2020-05-28 21:13:25 +05:30
parent e7d72b1020
commit 3dbb04035c
3 changed files with 97 additions and 8 deletions

View File

@ -61,8 +61,8 @@ Counting number of instructions by `wc -l $file.hack`
## [Project 5: Computer Architecture](https://www.nand2tetris.org/project05)
- [x] `Memory.hdl` (2xMux16, 2xNot, 3xAnd)
- [ ] `CPU.hdl`
- [x] `Memory.hdl` (2xMux16, 2xNot, 3xAnd 1 RAM16K)
- [x] `CPU.hdl` (6 And, 2 Nand, 3 Or, 1 Not, 1 Mux16, 1 Mux16, 2 Register, 1 PC, 1 ALU)
- [ ] `Computer.hdl`
Computer chip tests:

View File

@ -44,7 +44,6 @@ CHIP CPU {
// Specifically, when C instruction
// instruction[14] = 1
// instruction[13] = 1
// instruction[12] = a
// instruction[11] = c1
@ -91,10 +90,10 @@ CHIP CPU {
out=registerA, out[0..14]=addressM);
// Register D (instruction[4] = d2)
DRegister(in=aluoutput, load=instruction[4], out=registerD);
And(a=instruction[4], b=instruction[15], out=writeD);
DRegister(in=aluoutput, load=writeD, out=registerD);
// TODO: support jumps
PC(in=registerA, load=false, inc=true, reset=reset, out[0..14]=pc);
PC(in=registerA, load=jumpToAddress, inc=true, reset=reset, out[0..14]=pc);
// the "a" bit = instruction[12] decides whether Y = registerA | inM
Mux16(a=registerA, b=inM, sel=instruction[12], out=y);
@ -113,6 +112,28 @@ CHIP CPU {
out=outM,
zr=zr, ng=ng);
// (j2 & zr) || (j1 & ng) || (positive && j3)
// (i1 & zr) || (i2 & ng) || (positive && i0)
// jumpZero = i1 && zr
And(a=instruction[1], b=zr, out=jumpZero);
// jumpNegative = i2 && ng
And(a=instruction[2], b=ng, out=jumpNegative);
// Positive = Not(ng | zr)
Or(a=ng, b=zr, out=LessThanOne);
Not(in=LessThanOne, out=Positive);
// jumpPositive = i0 && Positive
And(a=Positive, b=instruction[0], out=jumpPositive);
// Final 3 way OR
Or(a=jumpZero, b=jumpNegative, out=tmpOut);
Or(a=tmpOut, b=jumpPositive, out=jumpBits);
// We are jumping to an address only if we are in a C instruction
// and the jump bits are on
And(a=instruction[15], b=jumpBits, out=jumpToAddress);
// if we are on instruction C, and d3 is true
And(a=instruction[3], b=instruction[15], out=writeM);
And(a=instruction[15], b=instruction[3], out=writeM);
}

View File

@ -22,4 +22,72 @@
|10+ | 11111|0000000000001110| 0 | 1000| 0 | 1000| 10| -1 |
|11 | 11111|0000000000001110| 0 | 14| 0 | 14| 11| -1 |
|11+ | 11111|1110001100000100| 0 | -1| 0 | 14| 11| -1 |
|12 | 11111|1110001100000100| 0 | -1| 0 | 14| 12| -1 |
|12 | 11111|1110001100000100| 0 | -1| 0 | 14| 14| -1 |
|12+ | 11111|0000001111100111| 0 | 1| 0 | 14| 14| -1 |
|13 | 11111|0000001111100111| 0 | 1| 0 | 999| 15| -1 |
|13+ | 11111|1110110111100000| 0 | 1000| 0 | 999| 15| -1 |
|14 | 11111|1110110111100000| 0 | 1001| 0 | 1000| 16| -1 |
|14+ | 11111|1110001100001000| 0 | -1| 1 | 1000| 16| -1 |
|15 | 11111|1110001100001000| 0 | -1| 1 | 1000| 17| -1 |
|15+ | 11111|0000000000010101| 0 | 1000| 0 | 1000| 17| -1 |
|16 | 11111|0000000000010101| 0 | 21| 0 | 21| 18| -1 |
|16+ | 11111|1110011111000010| 0 | 0| 0 | 21| 18| -1 |
|17 | 11111|1110011111000010| 0 | 0| 0 | 21| 21| -1 |
|17+ | 11111|0000000000000010| 0 | 21| 0 | 21| 21| -1 |
|18 | 11111|0000000000000010| 0 | 2| 0 | 2| 22| -1 |
|18+ | 11111|1110000010010000| 0 | 1| 0 | 2| 22| 1 |
|19 | 11111|1110000010010000| 0 | 3| 0 | 2| 23| 1 |
|19+ | 11111|0000001111101000| 0 | -1| 0 | 2| 23| 1 |
|20 | 11111|0000001111101000| 0 | -1| 0 | 1000| 24| 1 |
|20+ | 11111|1110111010010000| 0 | -1| 0 | 1000| 24| -1 |
|21 | 11111|1110111010010000| 0 | -1| 0 | 1000| 25| -1 |
|21+ | 11111|1110001100000001| 0 | -1| 0 | 1000| 25| -1 |
|22 | 11111|1110001100000001| 0 | -1| 0 | 1000| 26| -1 |
|22+ | 11111|1110001100000010| 0 | -1| 0 | 1000| 26| -1 |
|23 | 11111|1110001100000010| 0 | -1| 0 | 1000| 27| -1 |
|23+ | 11111|1110001100000011| 0 | -1| 0 | 1000| 27| -1 |
|24 | 11111|1110001100000011| 0 | -1| 0 | 1000| 28| -1 |
|24+ | 11111|1110001100000100| 0 | -1| 0 | 1000| 28| -1 |
|25 | 11111|1110001100000100| 0 | -1| 0 | 1000| 1000| -1 |
|25+ | 11111|1110001100000101| 0 | -1| 0 | 1000| 1000| -1 |
|26 | 11111|1110001100000101| 0 | -1| 0 | 1000| 1000| -1 |
|26+ | 11111|1110001100000110| 0 | -1| 0 | 1000| 1000| -1 |
|27 | 11111|1110001100000110| 0 | -1| 0 | 1000| 1000| -1 |
|27+ | 11111|1110001100000111| 0 | -1| 0 | 1000| 1000| -1 |
|28 | 11111|1110001100000111| 0 | -1| 0 | 1000| 1000| -1 |
|28+ | 11111|1110101010010000| 0 | 0| 0 | 1000| 1000| 0 |
|29 | 11111|1110101010010000| 0 | 0| 0 | 1000| 1001| 0 |
|29+ | 11111|1110001100000001| 0 | 0| 0 | 1000| 1001| 0 |
|30 | 11111|1110001100000001| 0 | 0| 0 | 1000| 1002| 0 |
|30+ | 11111|1110001100000010| 0 | 0| 0 | 1000| 1002| 0 |
|31 | 11111|1110001100000010| 0 | 0| 0 | 1000| 1000| 0 |
|31+ | 11111|1110001100000011| 0 | 0| 0 | 1000| 1000| 0 |
|32 | 11111|1110001100000011| 0 | 0| 0 | 1000| 1000| 0 |
|32+ | 11111|1110001100000100| 0 | 0| 0 | 1000| 1000| 0 |
|33 | 11111|1110001100000100| 0 | 0| 0 | 1000| 1001| 0 |
|33+ | 11111|1110001100000101| 0 | 0| 0 | 1000| 1001| 0 |
|34 | 11111|1110001100000101| 0 | 0| 0 | 1000| 1002| 0 |
|34+ | 11111|1110001100000110| 0 | 0| 0 | 1000| 1002| 0 |
|35 | 11111|1110001100000110| 0 | 0| 0 | 1000| 1000| 0 |
|35+ | 11111|1110001100000111| 0 | 0| 0 | 1000| 1000| 0 |
|36 | 11111|1110001100000111| 0 | 0| 0 | 1000| 1000| 0 |
|36+ | 11111|1110111111010000| 0 | 1| 0 | 1000| 1000| 1 |
|37 | 11111|1110111111010000| 0 | 1| 0 | 1000| 1001| 1 |
|37+ | 11111|1110001100000001| 0 | 1| 0 | 1000| 1001| 1 |
|38 | 11111|1110001100000001| 0 | 1| 0 | 1000| 1000| 1 |
|38+ | 11111|1110001100000010| 0 | 1| 0 | 1000| 1000| 1 |
|39 | 11111|1110001100000010| 0 | 1| 0 | 1000| 1001| 1 |
|39+ | 11111|1110001100000011| 0 | 1| 0 | 1000| 1001| 1 |
|40 | 11111|1110001100000011| 0 | 1| 0 | 1000| 1000| 1 |
|40+ | 11111|1110001100000100| 0 | 1| 0 | 1000| 1000| 1 |
|41 | 11111|1110001100000100| 0 | 1| 0 | 1000| 1001| 1 |
|41+ | 11111|1110001100000101| 0 | 1| 0 | 1000| 1001| 1 |
|42 | 11111|1110001100000101| 0 | 1| 0 | 1000| 1000| 1 |
|42+ | 11111|1110001100000110| 0 | 1| 0 | 1000| 1000| 1 |
|43 | 11111|1110001100000110| 0 | 1| 0 | 1000| 1001| 1 |
|43+ | 11111|1110001100000111| 0 | 1| 0 | 1000| 1001| 1 |
|44 | 11111|1110001100000111| 0 | 1| 0 | 1000| 1000| 1 |
|44+ | 11111|1110001100000111| 1 | 1| 0 | 1000| 1000| 1 |
|45 | 11111|1110001100000111| 1 | 1| 0 | 1000| 0| 1 |
|45+ | 11111|0111111111111111| 0 | 1| 0 | 1000| 0| 1 |
|46 | 11111|0111111111111111| 0 | 1| 0 |32767| 1| 1 |