This commit is contained in:
2021-04-21 17:01:42 +03:00
commit 281a96945a
613 changed files with 202255 additions and 0 deletions

BIN
projects/05/05.zip Normal file

Binary file not shown.

6
projects/05/Add.hack Normal file
View File

@@ -0,0 +1,6 @@
0000000000000010
1110110000010000
0000000000000011
1110000010010000
0000000000000000
1110001100001000

View File

@@ -0,0 +1,93 @@
|time| inM | instruction |reset| outM |writeM |addre| pc |
|0+ | 0|0011000000111001| 0 |*******| 0 | 0| 0|
|1 | 0|0011000000111001| 0 |*******| 0 |12345| 1|
|1+ | 0|1110110000010000| 0 |*******| 0 |12345| 1|
|2 | 0|1110110000010000| 0 |*******| 0 |12345| 2|
|2+ | 0|0101101110100000| 0 |*******| 0 |12345| 2|
|3 | 0|0101101110100000| 0 |*******| 0 |23456| 3|
|3+ | 0|1110000111010000| 0 |*******| 0 |23456| 3|
|4 | 0|1110000111010000| 0 |*******| 0 |23456| 4|
|4+ | 0|0000001111101000| 0 |*******| 0 |23456| 4|
|5 | 0|0000001111101000| 0 |*******| 0 | 1000| 5|
|5+ | 0|1110001100001000| 0 | 11111| 1 | 1000| 5|
|6 | 0|1110001100001000| 0 | 11111| 1 | 1000| 6|
|6+ | 0|0000001111101001| 0 |*******| 0 | 1000| 6|
|7 | 0|0000001111101001| 0 |*******| 0 | 1001| 7|
|7+ | 0|1110001110011000| 0 | 11110| 1 | 1001| 7|
|8 | 0|1110001110011000| 0 | 11109| 1 | 1001| 8|
|8+ | 0|0000001111101000| 0 |*******| 0 | 1001| 8|
|9 | 0|0000001111101000| 0 |*******| 0 | 1000| 9|
|9+ | 11111|1111010011010000| 0 |*******| 0 | 1000| 9|
|10 | 11111|1111010011010000| 0 |*******| 0 | 1000| 10|
|10+ | 11111|0000000000001110| 0 |*******| 0 | 1000| 10|
|11 | 11111|0000000000001110| 0 |*******| 0 | 14| 11|
|11+ | 11111|1110001100000100| 0 |*******| 0 | 14| 11|
|12 | 11111|1110001100000100| 0 |*******| 0 | 14| 14|
|12+ | 11111|0000001111100111| 0 |*******| 0 | 14| 14|
|13 | 11111|0000001111100111| 0 |*******| 0 | 999| 15|
|13+ | 11111|1110110111100000| 0 |*******| 0 | 999| 15|
|14 | 11111|1110110111100000| 0 |*******| 0 | 1000| 16|
|14+ | 11111|1110001100001000| 0 | -1| 1 | 1000| 16|
|15 | 11111|1110001100001000| 0 | -1| 1 | 1000| 17|
|15+ | 11111|0000000000010101| 0 |*******| 0 | 1000| 17|
|16 | 11111|0000000000010101| 0 |*******| 0 | 21| 18|
|16+ | 11111|1110011111000010| 0 |*******| 0 | 21| 18|
|17 | 11111|1110011111000010| 0 |*******| 0 | 21| 21|
|17+ | 11111|0000000000000010| 0 |*******| 0 | 21| 21|
|18 | 11111|0000000000000010| 0 |*******| 0 | 2| 22|
|18+ | 11111|1110000010010000| 0 |*******| 0 | 2| 22|
|19 | 11111|1110000010010000| 0 |*******| 0 | 2| 23|
|19+ | 11111|0000001111101000| 0 |*******| 0 | 2| 23|
|20 | 11111|0000001111101000| 0 |*******| 0 | 1000| 24|
|20+ | 11111|1110111010010000| 0 |*******| 0 | 1000| 24|
|21 | 11111|1110111010010000| 0 |*******| 0 | 1000| 25|
|21+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 25|
|22 | 11111|1110001100000001| 0 |*******| 0 | 1000| 26|
|22+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 26|
|23 | 11111|1110001100000010| 0 |*******| 0 | 1000| 27|
|23+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 27|
|24 | 11111|1110001100000011| 0 |*******| 0 | 1000| 28|
|24+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 28|
|25 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|25+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|26 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|26+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|27 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|27+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|28 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|28+ | 11111|1110101010010000| 0 |*******| 0 | 1000| 1000|
|29 | 11111|1110101010010000| 0 |*******| 0 | 1000| 1001|
|29+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001|
|30 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1002|
|30+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1002|
|31 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000|
|31+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|32 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|32+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|33 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001|
|33+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001|
|34 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1002|
|34+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1002|
|35 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|35+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|36 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|36+ | 11111|1110111111010000| 0 |*******| 0 | 1000| 1000|
|37 | 11111|1110111111010000| 0 |*******| 0 | 1000| 1001|
|37+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001|
|38 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1000|
|38+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000|
|39 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1001|
|39+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1001|
|40 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000|
|40+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000|
|41 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001|
|41+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001|
|42 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000|
|42+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000|
|43 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1001|
|43+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1001|
|44 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000|
|44+ | 11111|1110001100000111| 1 |*******| 0 | 1000| 1000|
|45 | 11111|1110001100000111| 1 |*******| 0 | 1000| 0|
|45+ | 11111|0111111111111111| 0 |*******| 0 | 1000| 0|
|46 | 11111|0111111111111111| 0 |*******| 0 |32767| 1|

View File

@@ -0,0 +1,150 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/CPU-external.tst
load CPU.hdl,
output-file CPU-external.out,
compare-to CPU-external.cmp,
output-list time%S0.4.0 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.3 addressM%D0.5.0 pc%D0.5.0;
set instruction %B0011000000111001, // @12345
tick, output, tock, output;
set instruction %B1110110000010000, // D=A
tick, output, tock, output;
set instruction %B0101101110100000, // @23456
tick, output, tock, output;
set instruction %B1110000111010000, // D=A-D
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000001111101001, // @1001
tick, output, tock, output;
set instruction %B1110001110011000, // MD=D-1
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1111010011010000, // D=D-M
set inM 11111,
tick, output, tock, output;
set instruction %B0000000000001110, // @14
tick, output, tock, output;
set instruction %B1110001100000100, // D;jlt
tick, output, tock, output;
set instruction %B0000001111100111, // @999
tick, output, tock, output;
set instruction %B1110110111100000, // A=A+1
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000000000010101, // @21
tick, output, tock, output;
set instruction %B1110011111000010, // D+1;jeq
tick, output, tock, output;
set instruction %B0000000000000010, // @2
tick, output, tock, output;
set instruction %B1110000010010000, // D=D+A
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110111010010000, // D=-1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110101010010000, // D=0
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110111111010000, // D=1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set reset 1;
tick, output, tock, output;
set instruction %B0111111111111111, // @32767
set reset 0;
tick, output, tock, output;

93
projects/05/CPU.cmp Normal file
View File

@@ -0,0 +1,93 @@
|time| inM | instruction |reset| outM |writeM |addre| pc |DRegiste|
|0+ | 0|0011000000111001| 0 |*******| 0 | 0| 0| 0 |
|1 | 0|0011000000111001| 0 |*******| 0 |12345| 1| 0 |
|1+ | 0|1110110000010000| 0 |*******| 0 |12345| 1| 12345 |
|2 | 0|1110110000010000| 0 |*******| 0 |12345| 2| 12345 |
|2+ | 0|0101101110100000| 0 |*******| 0 |12345| 2| 12345 |
|3 | 0|0101101110100000| 0 |*******| 0 |23456| 3| 12345 |
|3+ | 0|1110000111010000| 0 |*******| 0 |23456| 3| 11111 |
|4 | 0|1110000111010000| 0 |*******| 0 |23456| 4| 11111 |
|4+ | 0|0000001111101000| 0 |*******| 0 |23456| 4| 11111 |
|5 | 0|0000001111101000| 0 |*******| 0 | 1000| 5| 11111 |
|5+ | 0|1110001100001000| 0 | 11111| 1 | 1000| 5| 11111 |
|6 | 0|1110001100001000| 0 | 11111| 1 | 1000| 6| 11111 |
|6+ | 0|0000001111101001| 0 |*******| 0 | 1000| 6| 11111 |
|7 | 0|0000001111101001| 0 |*******| 0 | 1001| 7| 11111 |
|7+ | 0|1110001110011000| 0 | 11110| 1 | 1001| 7| 11110 |
|8 | 0|1110001110011000| 0 | 11109| 1 | 1001| 8| 11110 |
|8+ | 0|0000001111101000| 0 |*******| 0 | 1001| 8| 11110 |
|9 | 0|0000001111101000| 0 |*******| 0 | 1000| 9| 11110 |
|9+ | 11111|1111010011010000| 0 |*******| 0 | 1000| 9| -1 |
|10 | 11111|1111010011010000| 0 |*******| 0 | 1000| 10| -1 |
|10+ | 11111|0000000000001110| 0 |*******| 0 | 1000| 10| -1 |
|11 | 11111|0000000000001110| 0 |*******| 0 | 14| 11| -1 |
|11+ | 11111|1110001100000100| 0 |*******| 0 | 14| 11| -1 |
|12 | 11111|1110001100000100| 0 |*******| 0 | 14| 14| -1 |
|12+ | 11111|0000001111100111| 0 |*******| 0 | 14| 14| -1 |
|13 | 11111|0000001111100111| 0 |*******| 0 | 999| 15| -1 |
|13+ | 11111|1110110111100000| 0 |*******| 0 | 999| 15| -1 |
|14 | 11111|1110110111100000| 0 |*******| 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 |*******| 0 | 1000| 17| -1 |
|16 | 11111|0000000000010101| 0 |*******| 0 | 21| 18| -1 |
|16+ | 11111|1110011111000010| 0 |*******| 0 | 21| 18| -1 |
|17 | 11111|1110011111000010| 0 |*******| 0 | 21| 21| -1 |
|17+ | 11111|0000000000000010| 0 |*******| 0 | 21| 21| -1 |
|18 | 11111|0000000000000010| 0 |*******| 0 | 2| 22| -1 |
|18+ | 11111|1110000010010000| 0 |*******| 0 | 2| 22| 1 |
|19 | 11111|1110000010010000| 0 |*******| 0 | 2| 23| 1 |
|19+ | 11111|0000001111101000| 0 |*******| 0 | 2| 23| 1 |
|20 | 11111|0000001111101000| 0 |*******| 0 | 1000| 24| 1 |
|20+ | 11111|1110111010010000| 0 |*******| 0 | 1000| 24| -1 |
|21 | 11111|1110111010010000| 0 |*******| 0 | 1000| 25| -1 |
|21+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 25| -1 |
|22 | 11111|1110001100000001| 0 |*******| 0 | 1000| 26| -1 |
|22+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 26| -1 |
|23 | 11111|1110001100000010| 0 |*******| 0 | 1000| 27| -1 |
|23+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 27| -1 |
|24 | 11111|1110001100000011| 0 |*******| 0 | 1000| 28| -1 |
|24+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 28| -1 |
|25 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| -1 |
|25+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| -1 |
|26 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| -1 |
|26+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| -1 |
|27 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| -1 |
|27+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| -1 |
|28 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| -1 |
|28+ | 11111|1110101010010000| 0 |*******| 0 | 1000| 1000| 0 |
|29 | 11111|1110101010010000| 0 |*******| 0 | 1000| 1001| 0 |
|29+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001| 0 |
|30 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1002| 0 |
|30+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1002| 0 |
|31 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000| 0 |
|31+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 0 |
|32 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 0 |
|32+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| 0 |
|33 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001| 0 |
|33+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001| 0 |
|34 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1002| 0 |
|34+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1002| 0 |
|35 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| 0 |
|35+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 0 |
|36 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 0 |
|36+ | 11111|1110111111010000| 0 |*******| 0 | 1000| 1000| 1 |
|37 | 11111|1110111111010000| 0 |*******| 0 | 1000| 1001| 1 |
|37+ | 11111|1110001100000001| 0 |*******| 0 | 1000| 1001| 1 |
|38 | 11111|1110001100000001| 0 |*******| 0 | 1000| 1000| 1 |
|38+ | 11111|1110001100000010| 0 |*******| 0 | 1000| 1000| 1 |
|39 | 11111|1110001100000010| 0 |*******| 0 | 1000| 1001| 1 |
|39+ | 11111|1110001100000011| 0 |*******| 0 | 1000| 1001| 1 |
|40 | 11111|1110001100000011| 0 |*******| 0 | 1000| 1000| 1 |
|40+ | 11111|1110001100000100| 0 |*******| 0 | 1000| 1000| 1 |
|41 | 11111|1110001100000100| 0 |*******| 0 | 1000| 1001| 1 |
|41+ | 11111|1110001100000101| 0 |*******| 0 | 1000| 1001| 1 |
|42 | 11111|1110001100000101| 0 |*******| 0 | 1000| 1000| 1 |
|42+ | 11111|1110001100000110| 0 |*******| 0 | 1000| 1000| 1 |
|43 | 11111|1110001100000110| 0 |*******| 0 | 1000| 1001| 1 |
|43+ | 11111|1110001100000111| 0 |*******| 0 | 1000| 1001| 1 |
|44 | 11111|1110001100000111| 0 |*******| 0 | 1000| 1000| 1 |
|44+ | 11111|1110001100000111| 1 |*******| 0 | 1000| 1000| 1 |
|45 | 11111|1110001100000111| 1 |*******| 0 | 1000| 0| 1 |
|45+ | 11111|0111111111111111| 0 |*******| 0 | 1000| 0| 1 |
|46 | 11111|0111111111111111| 0 |*******| 0 |32767| 1| 1 |

72
projects/05/CPU.hdl Normal file
View File

@@ -0,0 +1,72 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/CPU.hdl
/**
* The Hack CPU (Central Processing unit), consisting of an ALU,
* two registers named A and D, and a program counter named PC.
* The CPU is designed to fetch and execute instructions written in
* the Hack machine language. In particular, functions as follows:
* Executes the inputted instruction according to the Hack machine
* language specification. The D and A in the language specification
* refer to CPU-resident registers, while M refers to the external
* memory location addressed by A, i.e. to Memory[A]. The inM input
* holds the value of this location. If the current instruction needs
* to write a value to M, the value is placed in outM, the address
* of the target location is placed in the addressM output, and the
* writeM control bit is asserted. (When writeM==0, any value may
* appear in outM). The outM and writeM outputs are combinational:
* they are affected instantaneously by the execution of the current
* instruction. The addressM and pc outputs are clocked: although they
* are affected by the execution of the current instruction, they commit
* to their new values only in the next time step. If reset==1 then the
* CPU jumps to address 0 (i.e. pc is set to 0 in next time step) rather
* than to the address resulting from executing the current instruction.
*/
CHIP CPU {
IN inM[16], // M value input (M = contents of RAM[A])
instruction[16], // Instruction for execution
reset; // Signals whether to re-start the current
// program (reset==1) or continue executing
// the current program (reset==0).
OUT outM[16], // M value output
writeM, // Write to M?
addressM[15], // Address in data memory (of M)
pc[15]; // address of next instruction
PARTS:
ALU(x=dreg, y=aluy, zx=instruction[11], nx=instruction[10], zy=instruction[9], ny=instruction[8], f=instruction[7], no=instruction[6], out=aluout, out=outM, zr=aluzr, ng=alung);
And(a=instruction[15], b=instruction[4], out=dload);
DRegister(in=aluout, load=dload, out=dreg);
Not(in=instruction[15], out=ainst);
Or(a=instruction[5], b=ainst, out=aload);
ARegister(in=aregin, load=aload, out=areg, out[0..14]=addressM);
Mux16(a=instruction, b=aluout, sel=instruction[15], out=aregin);
And(a=instruction[15], b=instruction[3], out=writeM);
Mux16(a=areg, b=inM, sel=instruction[12], out=aluy);
And(a=instruction[1], b=aluzr, out=jmpzr);
And(a=instruction[2], b=alung, out=jmpng);
Not(in=alung, out=alunng);
Not(in=aluzr, out=alunzr);
And(a=alunng, b=alunzr, out=alupz);
And(a=instruction[0], b=alupz, out=jmppz);
Or8Way(in[0]=jmpzr, in[1]=jmpng, in[2]=jmppz, out=jump);
And(a=jump, b=instruction[15], out=sjump);
PC(in=areg, load=sjump, inc=true, reset=reset, out[0..14]=pc);
}

150
projects/05/CPU.tst Normal file
View File

@@ -0,0 +1,150 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/CPU.tst
load CPU.hdl,
output-file CPU.out,
compare-to CPU.cmp,
output-list time%S0.4.0 inM%D0.6.0 instruction%B0.16.0 reset%B2.1.2 outM%D1.6.0 writeM%B3.1.3 addressM%D0.5.0 pc%D0.5.0 DRegister[]%D1.6.1;
set instruction %B0011000000111001, // @12345
tick, output, tock, output;
set instruction %B1110110000010000, // D=A
tick, output, tock, output;
set instruction %B0101101110100000, // @23456
tick, output, tock, output;
set instruction %B1110000111010000, // D=A-D
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000001111101001, // @1001
tick, output, tock, output;
set instruction %B1110001110011000, // MD=D-1
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1111010011010000, // D=D-M
set inM 11111,
tick, output, tock, output;
set instruction %B0000000000001110, // @14
tick, output, tock, output;
set instruction %B1110001100000100, // D;jlt
tick, output, tock, output;
set instruction %B0000001111100111, // @999
tick, output, tock, output;
set instruction %B1110110111100000, // A=A+1
tick, output, tock, output;
set instruction %B1110001100001000, // M=D
tick, output, tock, output;
set instruction %B0000000000010101, // @21
tick, output, tock, output;
set instruction %B1110011111000010, // D+1;jeq
tick, output, tock, output;
set instruction %B0000000000000010, // @2
tick, output, tock, output;
set instruction %B1110000010010000, // D=D+A
tick, output, tock, output;
set instruction %B0000001111101000, // @1000
tick, output, tock, output;
set instruction %B1110111010010000, // D=-1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110101010010000, // D=0
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set instruction %B1110111111010000, // D=1
tick, output, tock, output;
set instruction %B1110001100000001, // D;JGT
tick, output, tock, output;
set instruction %B1110001100000010, // D;JEQ
tick, output, tock, output;
set instruction %B1110001100000011, // D;JGE
tick, output, tock, output;
set instruction %B1110001100000100, // D;JLT
tick, output, tock, output;
set instruction %B1110001100000101, // D;JNE
tick, output, tock, output;
set instruction %B1110001100000110, // D;JLE
tick, output, tock, output;
set instruction %B1110001100000111, // D;JMP
tick, output, tock, output;
set reset 1;
tick, output, tock, output;
set instruction %B0111111111111111, // @32767
set reset 0;
tick, output, tock, output;

25
projects/05/Computer.hdl Normal file
View File

@@ -0,0 +1,25 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/Computer.hdl
/**
* The HACK computer, including CPU, ROM and RAM.
* When reset is 0, the program stored in the computer's ROM executes.
* When reset is 1, the execution of the program restarts.
* Thus, to start a program's execution, reset must be pushed "up" (1)
* and "down" (0). From this point onward the user is at the mercy of
* the software. In particular, depending on the program's code, the
* screen may show some output and the user may be able to interact
* with the computer via the keyboard.
*/
CHIP Computer {
IN reset;
PARTS:
Memory(in=memin, load=memload, address=memaddr, out=memout);
CPU(inM=memout, instruction=instr, reset=reset, outM=memin, writeM=memload, addressM=memaddr, pc=pct);
ROM32K(address=pct, out=instr);
}

View File

@@ -0,0 +1,15 @@
| time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 | 0 |
| 5 | 0 | 0 | 0 | 0 |
| 6 | 0 | 5 | 0 | 0 |
| 7 | 1 | 0 | 0 | 0 |
| 8 | 0 | 0 | 0 | 0 |
| 9 | 0 | 0 | 0 | 0 |
| 10 | 0 | 0 | 0 | 0 |
| 11 | 0 | 0 | 0 | 0 |
| 12 | 0 | 0 | 0 | 0 |
| 13 | 0 | 5 | 0 | 0 |

View File

@@ -0,0 +1,32 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerAdd-external.tst
load Computer.hdl,
output-file ComputerAdd-external.out,
compare-to ComputerAdd-external.cmp,
output-list time%S1.4.1 reset%B2.1.2 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Load a program written in the Hack machine language.
// The program adds the two constants 2 and 3 and writes the result in RAM[0].
ROM32K load Add.hack,
output;
// First run (at the beginning PC=0)
repeat 6 {
tick, tock, output;
}
// Reset the PC
set reset 1,
set RAM16K[0] 0,
tick, tock, output;
// Second run, to check that the PC was reset correctly.
set reset 0,
repeat 6 {
tick, tock, output;
}

View File

@@ -0,0 +1,15 @@
| time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0 | 0| 0 | 0 | 0 |
| 1 | 0 | 2 | 0 | 1| 0 | 0 | 0 |
| 2 | 0 | 2 | 2 | 2| 0 | 0 | 0 |
| 3 | 0 | 3 | 2 | 3| 0 | 0 | 0 |
| 4 | 0 | 3 | 5 | 4| 0 | 0 | 0 |
| 5 | 0 | 0 | 5 | 5| 0 | 0 | 0 |
| 6 | 0 | 0 | 5 | 6| 5 | 0 | 0 |
| 7 | 1 | 0 | 5 | 0| 0 | 0 | 0 |
| 8 | 0 | 2 | 5 | 1| 0 | 0 | 0 |
| 9 | 0 | 2 | 2 | 2| 0 | 0 | 0 |
| 10 | 0 | 3 | 2 | 3| 0 | 0 | 0 |
| 11 | 0 | 3 | 5 | 4| 0 | 0 | 0 |
| 12 | 0 | 0 | 5 | 5| 0 | 0 | 0 |
| 13 | 0 | 0 | 5 | 6| 5 | 0 | 0 |

View File

@@ -0,0 +1,32 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerAdd.tst
load Computer.hdl,
output-file ComputerAdd.out,
compare-to ComputerAdd.cmp,
output-list time%S1.4.1 reset%B2.1.2 ARegister[0]%D1.7.1 DRegister[0]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Load a program written in the Hack machine language.
// The program adds the two constants 2 and 3 and writes the result in RAM[0].
ROM32K load Add.hack,
output;
// First run (at the beginning PC=0)
repeat 6 {
tick, tock, output;
}
// Reset the PC
set reset 1,
set RAM16K[0] 0,
tick, tock, output;
// Second run, to check that the PC was reset correctly.
set reset 0,
repeat 6 {
tick, tock, output;
}

View File

@@ -0,0 +1,28 @@
| time |reset|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 3 | 5 | 0 |
| 1 | 0 | 3 | 5 | 0 |
| 2 | 0 | 3 | 5 | 0 |
| 3 | 0 | 3 | 5 | 0 |
| 4 | 0 | 3 | 5 | 0 |
| 5 | 0 | 3 | 5 | 0 |
| 6 | 0 | 3 | 5 | 0 |
| 7 | 0 | 3 | 5 | 0 |
| 8 | 0 | 3 | 5 | 0 |
| 9 | 0 | 3 | 5 | 0 |
| 10 | 0 | 3 | 5 | 0 |
| 11 | 0 | 3 | 5 | 0 |
| 12 | 0 | 3 | 5 | 5 |
| 13 | 0 | 3 | 5 | 5 |
| 14 | 0 | 3 | 5 | 5 |
| 15 | 1 | 3 | 5 | 5 |
| 15 | 0 | 23456 | 12345 | 5 |
| 16 | 0 | 23456 | 12345 | 5 |
| 17 | 0 | 23456 | 12345 | 5 |
| 18 | 0 | 23456 | 12345 | 5 |
| 19 | 0 | 23456 | 12345 | 5 |
| 20 | 0 | 23456 | 12345 | 5 |
| 21 | 0 | 23456 | 12345 | 5 |
| 22 | 0 | 23456 | 12345 | 5 |
| 23 | 0 | 23456 | 12345 | 5 |
| 24 | 0 | 23456 | 12345 | 5 |
| 25 | 0 | 23456 | 12345 | 23456 |

View File

@@ -0,0 +1,38 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerMax-external.tst
load Computer.hdl,
output-file ComputerMax-external.out,
compare-to ComputerMax-external.cmp,
output-list time%S1.4.1 reset%B2.1.2 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Load a program written in the Hack machine language.
// The program computes the maximum of RAM[0] and RAM[1]
// and writes the result in RAM[2].
ROM32K load Max.hack,
// first run: compute max(3,5)
set RAM16K[0] 3,
set RAM16K[1] 5,
output;
repeat 14 {
tick, tock, output;
}
// reset the PC
set reset 1,
tick, tock, output;
// second run: compute max(23456,12345)
set reset 0,
set RAM16K[0] 23456,
set RAM16K[1] 12345,
output;
// The run on these inputs needs less cycles (different branching)
repeat 10 {
tick, tock, output;
}

View File

@@ -0,0 +1,28 @@
| time |reset|ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0 | 0| 3 | 5 | 0 |
| 1 | 0 | 0 | 0 | 1| 3 | 5 | 0 |
| 2 | 0 | 0 | 3 | 2| 3 | 5 | 0 |
| 3 | 0 | 1 | 3 | 3| 3 | 5 | 0 |
| 4 | 0 | 1 | -2 | 4| 3 | 5 | 0 |
| 5 | 0 | 10 | -2 | 5| 3 | 5 | 0 |
| 6 | 0 | 10 | -2 | 6| 3 | 5 | 0 |
| 7 | 0 | 1 | -2 | 7| 3 | 5 | 0 |
| 8 | 0 | 1 | 5 | 8| 3 | 5 | 0 |
| 9 | 0 | 12 | 5 | 9| 3 | 5 | 0 |
| 10 | 0 | 12 | 5 | 12| 3 | 5 | 0 |
| 11 | 0 | 2 | 5 | 13| 3 | 5 | 0 |
| 12 | 0 | 2 | 5 | 14| 3 | 5 | 5 |
| 13 | 0 | 14 | 5 | 15| 3 | 5 | 5 |
| 14 | 0 | 14 | 5 | 14| 3 | 5 | 5 |
| 15 | 1 | 14 | 5 | 0| 3 | 5 | 5 |
| 15 | 0 | 14 | 5 | 0| 23456 | 12345 | 5 |
| 16 | 0 | 0 | 5 | 1| 23456 | 12345 | 5 |
| 17 | 0 | 0 | 23456 | 2| 23456 | 12345 | 5 |
| 18 | 0 | 1 | 23456 | 3| 23456 | 12345 | 5 |
| 19 | 0 | 1 | 11111 | 4| 23456 | 12345 | 5 |
| 20 | 0 | 10 | 11111 | 5| 23456 | 12345 | 5 |
| 21 | 0 | 10 | 11111 | 10| 23456 | 12345 | 5 |
| 22 | 0 | 0 | 11111 | 11| 23456 | 12345 | 5 |
| 23 | 0 | 0 | 23456 | 12| 23456 | 12345 | 5 |
| 24 | 0 | 2 | 23456 | 13| 23456 | 12345 | 5 |
| 25 | 0 | 2 | 23456 | 14| 23456 | 12345 | 23456 |

View File

@@ -0,0 +1,39 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerMax.tst
load Computer.hdl,
output-file ComputerMax.out,
compare-to ComputerMax.cmp,
output-list time%S1.4.1 reset%B2.1.2 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Load a program written in the Hack machine language.
// The program computes the maximum of RAM[0] and RAM[1]
// and writes the result in RAM[2].
ROM32K load Max.hack,
// first run: compute max(3,5)
set RAM16K[0] 3,
set RAM16K[1] 5,
output;
repeat 14 {
tick, tock, output;
}
// reset the PC
set reset 1,
tick, tock, output;
// second run: compute max(23456,12345)
set reset 0,
set RAM16K[0] 23456,
set RAM16K[1] 12345,
output;
// The run on these inputs needs less cycles (different branching)
repeat 10 {
tick, tock, output;
}

View File

@@ -0,0 +1,65 @@
| time |
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
| 31 |
| 32 |
| 33 |
| 34 |
| 35 |
| 36 |
| 37 |
| 38 |
| 39 |
| 40 |
| 41 |
| 42 |
| 43 |
| 44 |
| 45 |
| 46 |
| 47 |
| 48 |
| 49 |
| 50 |
| 51 |
| 52 |
| 53 |
| 54 |
| 55 |
| 56 |
| 57 |
| 58 |
| 59 |
| 60 |
| 61 |
| 62 |
| 63 |

View File

@@ -0,0 +1,26 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerRect-external.tst
load Computer.hdl,
output-file ComputerRect-external.out,
compare-to ComputerRect-external.cmp,
output-list time%S1.4.1;
// Load a program written in the Hack machine language.
// The program draws a rectangle of width 16 pixels and
// length RAM[0] at the top left of the screen.
ROM32K load Rect.hack,
echo "Before you run this script, select the 'Screen' option from the 'View' menu";
echo "A small rectangle should be drawn at the top left of the screen (the 'Screen' option of the 'View' menu should be selected.)";
// draw a rectangle 16 pixels wide and 4 pixels long
set RAM16K[0] 4,
output;
repeat 63 {
tick, tock, output;
}

View File

@@ -0,0 +1,65 @@
| time |ARegister|DRegister|PC[]|RAM16K[0]|RAM16K[1]|RAM16K[2]|
| 0 | 0 | 0 | 0| 4 | 0 | 0 |
| 1 | 0 | 0 | 1| 4 | 0 | 0 |
| 2 | 0 | 4 | 2| 4 | 0 | 0 |
| 3 | 23 | 4 | 3| 4 | 0 | 0 |
| 4 | 23 | 4 | 4| 4 | 0 | 0 |
| 5 | 16 | 4 | 5| 4 | 0 | 0 |
| 6 | 16 | 4 | 6| 4 | 0 | 0 |
| 7 | 16384 | 4 | 7| 4 | 0 | 0 |
| 8 | 16384 | 16384 | 8| 4 | 0 | 0 |
| 9 | 17 | 16384 | 9| 4 | 0 | 0 |
| 10 | 17 | 16384 | 10| 4 | 0 | 0 |
| 11 | 17 | 16384 | 11| 4 | 0 | 0 |
| 12 | 16384 | 16384 | 12| 4 | 0 | 0 |
| 13 | 16384 | 16384 | 13| 4 | 0 | 0 |
| 14 | 17 | 16384 | 14| 4 | 0 | 0 |
| 15 | 17 | 16384 | 15| 4 | 0 | 0 |
| 16 | 32 | 16384 | 16| 4 | 0 | 0 |
| 17 | 32 | 16416 | 17| 4 | 0 | 0 |
| 18 | 17 | 16416 | 18| 4 | 0 | 0 |
| 19 | 17 | 16416 | 19| 4 | 0 | 0 |
| 20 | 16 | 16416 | 20| 4 | 0 | 0 |
| 21 | 16 | 3 | 21| 4 | 0 | 0 |
| 22 | 10 | 3 | 22| 4 | 0 | 0 |
| 23 | 10 | 3 | 10| 4 | 0 | 0 |
| 24 | 17 | 3 | 11| 4 | 0 | 0 |
| 25 | 16416 | 3 | 12| 4 | 0 | 0 |
| 26 | 16416 | 3 | 13| 4 | 0 | 0 |
| 27 | 17 | 3 | 14| 4 | 0 | 0 |
| 28 | 17 | 16416 | 15| 4 | 0 | 0 |
| 29 | 32 | 16416 | 16| 4 | 0 | 0 |
| 30 | 32 | 16448 | 17| 4 | 0 | 0 |
| 31 | 17 | 16448 | 18| 4 | 0 | 0 |
| 32 | 17 | 16448 | 19| 4 | 0 | 0 |
| 33 | 16 | 16448 | 20| 4 | 0 | 0 |
| 34 | 16 | 2 | 21| 4 | 0 | 0 |
| 35 | 10 | 2 | 22| 4 | 0 | 0 |
| 36 | 10 | 2 | 10| 4 | 0 | 0 |
| 37 | 17 | 2 | 11| 4 | 0 | 0 |
| 38 | 16448 | 2 | 12| 4 | 0 | 0 |
| 39 | 16448 | 2 | 13| 4 | 0 | 0 |
| 40 | 17 | 2 | 14| 4 | 0 | 0 |
| 41 | 17 | 16448 | 15| 4 | 0 | 0 |
| 42 | 32 | 16448 | 16| 4 | 0 | 0 |
| 43 | 32 | 16480 | 17| 4 | 0 | 0 |
| 44 | 17 | 16480 | 18| 4 | 0 | 0 |
| 45 | 17 | 16480 | 19| 4 | 0 | 0 |
| 46 | 16 | 16480 | 20| 4 | 0 | 0 |
| 47 | 16 | 1 | 21| 4 | 0 | 0 |
| 48 | 10 | 1 | 22| 4 | 0 | 0 |
| 49 | 10 | 1 | 10| 4 | 0 | 0 |
| 50 | 17 | 1 | 11| 4 | 0 | 0 |
| 51 | 16480 | 1 | 12| 4 | 0 | 0 |
| 52 | 16480 | 1 | 13| 4 | 0 | 0 |
| 53 | 17 | 1 | 14| 4 | 0 | 0 |
| 54 | 17 | 16480 | 15| 4 | 0 | 0 |
| 55 | 32 | 16480 | 16| 4 | 0 | 0 |
| 56 | 32 | 16512 | 17| 4 | 0 | 0 |
| 57 | 17 | 16512 | 18| 4 | 0 | 0 |
| 58 | 17 | 16512 | 19| 4 | 0 | 0 |
| 59 | 16 | 16512 | 20| 4 | 0 | 0 |
| 60 | 16 | 0 | 21| 4 | 0 | 0 |
| 61 | 10 | 0 | 22| 4 | 0 | 0 |
| 62 | 10 | 0 | 23| 4 | 0 | 0 |
| 63 | 23 | 0 | 24| 4 | 0 | 0 |

View File

@@ -0,0 +1,26 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/ComputerRect.tst
load Computer.hdl,
output-file ComputerRect.out,
compare-to ComputerRect.cmp,
output-list time%S1.4.1 ARegister[]%D1.7.1 DRegister[]%D1.7.1 PC[]%D0.4.0 RAM16K[0]%D1.7.1 RAM16K[1]%D1.7.1 RAM16K[2]%D1.7.1;
// Load a program written in the Hack machine language.
// The program draws a rectangle of width 16 pixels and
// length RAM[0] at the top left of the screen.
ROM32K load Rect.hack,
echo "Before you run this script, select the 'Screen' option from the 'View' menu";
echo "A small rectangle should be drawn at the top left of the screen (the 'Screen' option of the 'View' menu should be selected.)";
// Draws a rectangle 16 pixels wide and 4 pixels long
set RAM16K[0] 4,
output;
repeat 63 {
tick, tock, output;
}

16
projects/05/Max.hack Normal file
View File

@@ -0,0 +1,16 @@
0000000000000000
1111110000010000
0000000000000001
1111010011010000
0000000000001010
1110001100000001
0000000000000001
1111110000010000
0000000000001100
1110101010000111
0000000000000000
1111110000010000
0000000000000010
1110001100001000
0000000000001110
1110101010000111

54
projects/05/Memory.cmp Normal file
View File

@@ -0,0 +1,54 @@
| in |load | address | out |
| -1 | 1 | 000000000000000 | 0 |
| -1 | 1 | 000000000000000 | -1 |
| 9999 | 0 | 000000000000000 | -1 |
| 9999 | 0 | 000000000000000 | -1 |
| 9999 | 0 | 010000000000000 | 0 |
| 9999 | 0 | 100000000000000 | 0 |
| 2222 | 1 | 010000000000000 | 0 |
| 2222 | 1 | 010000000000000 | 2222 |
| 9999 | 0 | 010000000000000 | 2222 |
| 9999 | 0 | 010000000000000 | 2222 |
| 9999 | 0 | 000000000000000 | -1 |
| 9999 | 0 | 100000000000000 | 0 |
| 9999 | 0 | 000000000000001 | 0 |
| 9999 | 0 | 000000000000010 | 0 |
| 9999 | 0 | 000000000000100 | 0 |
| 9999 | 0 | 000000000001000 | 0 |
| 9999 | 0 | 000000000010000 | 0 |
| 9999 | 0 | 000000000100000 | 0 |
| 9999 | 0 | 000000001000000 | 0 |
| 9999 | 0 | 000000010000000 | 0 |
| 9999 | 0 | 000000100000000 | 0 |
| 9999 | 0 | 000001000000000 | 0 |
| 9999 | 0 | 000010000000000 | 0 |
| 9999 | 0 | 000100000000000 | 0 |
| 9999 | 0 | 001000000000000 | 0 |
| 9999 | 0 | 010000000000000 | 2222 |
| 1234 | 1 | 001001000110100 | 0 |
| 1234 | 1 | 001001000110100 | 1234 |
| 1234 | 0 | 010001000110100 | 0 |
| 1234 | 0 | 110001000110100 | 0 |
| 2345 | 1 | 010001101000101 | 0 |
| 2345 | 1 | 010001101000101 | 2345 |
| 2345 | 0 | 000001101000101 | 0 |
| 2345 | 0 | 100001101000101 | 0 |
| 2345 | 0 | 110000000000000 | 75 |
| -1 | 1 | 100111111001111 | -1 |
| -1 | 1 | 101000001001111 | -1 |
| -1 | 1 | 000111111001111 | 0 |
| -1 | 1 | 010111111001111 | 0 |
| -1 | 0 | 100111111001110 | 0 |
| -1 | 0 | 100111111001101 | 0 |
| -1 | 0 | 100111111001011 | 0 |
| -1 | 0 | 100111111000111 | 0 |
| -1 | 0 | 100111111011111 | 0 |
| -1 | 0 | 100111111101111 | 0 |
| -1 | 0 | 100111110001111 | 0 |
| -1 | 0 | 100111101001111 | 0 |
| -1 | 0 | 100111011001111 | 0 |
| -1 | 0 | 100110111001111 | 0 |
| -1 | 0 | 100101111001111 | 0 |
| -1 | 0 | 100011111001111 | 0 |
| -1 | 0 | 101111111001111 | 0 |
| -1 | 0 | 110000000000000 | 89 |

46
projects/05/Memory.hdl Normal file
View File

@@ -0,0 +1,46 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/Memory.hdl
/**
* The complete address space of the Hack computer's memory,
* including RAM and memory-mapped I/O.
* The chip facilitates read and write operations, as follows:
* Read: out(t) = Memory[address(t)](t)
* Write: if load(t-1) then Memory[address(t-1)](t) = in(t-1)
* In words: the chip always outputs the value stored at the memory
* location specified by address. If load==1, the in value is loaded
* into the memory location specified by address. This value becomes
* available through the out output from the next time step onward.
* Address space rules:
* Only the upper 16K+8K+1 words of the Memory chip are used.
* Access to address>0x6000 is invalid. Access to any address in
* the range 0x4000-0x5FFF results in accessing the screen memory
* map. Access to address 0x6000 results in accessing the keyboard
* memory map. The behavior in these addresses is described in the
* Screen and Keyboard chip specifications given in the book.
*/
CHIP Memory {
IN in[16], load, address[15];
OUT out[16];
PARTS:
Not(in=address[14], out=stdmem);
And(a=stdmem, b=load, out=stdload);
RAM16K(in=in, load=stdload, address=address[0..13], out=stdout);
Mux16(a=ioout, b=stdout, sel=stdmem, out=out);
Keyboard(out=kbdout);
And(a=address[14], b=address[13], out=kbdmem);
And(a=address[14], b=load, out=scrload);
Screen(in=in, load=scrload, address=address[0..12], out=scrout);
Mux16(b[0..15]=kbdout, a=scrout, sel=kbdmem, out=ioout);
}

163
projects/05/Memory.tst Normal file
View File

@@ -0,0 +1,163 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/05/Memory.tst
load Memory.hdl,
output-file Memory.out,
compare-to Memory.cmp,
output-list in%D1.6.1 load%B2.1.2 address%B1.15.1 out%D1.6.1;
echo "Before you run this script, select the 'Screen' option from the 'View' menu";
set in -1, // Set RAM[0] = -1
set load 1,
set address 0,
tick,
output;
tock,
output;
set in 9999, // RAM[0] holds value
set load 0,
tick,
output;
tock,
output;
set address %X2000, // Did not also write to upper RAM or Screen
eval,
output;
set address %X4000,
eval,
output;
set in 2222, // Set RAM[2000] = 2222
set load 1,
set address %X2000,
tick,
output;
tock,
output;
set in 9999, // RAM[2000] holds value
set load 0,
tick,
output;
tock,
output;
set address 0, // Did not also write to lower RAM or Screen
eval,
output;
set address %X4000,
eval,
output;
set load 0, // Low order address bits connected
set address %X0001, eval, output;
set address %X0002, eval, output;
set address %X0004, eval, output;
set address %X0008, eval, output;
set address %X0010, eval, output;
set address %X0020, eval, output;
set address %X0040, eval, output;
set address %X0080, eval, output;
set address %X0100, eval, output;
set address %X0200, eval, output;
set address %X0400, eval, output;
set address %X0800, eval, output;
set address %X1000, eval, output;
set address %X2000, eval, output;
set address %X1234, // RAM[1234] = 1234
set in 1234,
set load 1,
tick,
output;
tock,
output;
set load 0,
set address %X2234, // Did not also write to upper RAM or Screen
eval, output;
set address %X6234,
eval, output;
set address %X2345, // RAM[2345] = 2345
set in 2345,
set load 1,
tick,
output;
tock,
output;
set load 0,
set address %X0345, // Did not also write to lower RAM or Screen
eval, output;
set address %X4345,
eval, output;
// Keyboard test
set address 24576,
echo "Click the Keyboard icon and hold down the 'K' key (uppercase) until you see the next message (it should appear shortly after that) ...",
// It's important to keep holding the key down since if the system is busy,
// the memory will zero itself before being outputted.
while out <> 75 {
eval,
}
clear-echo,
output;
// Screen test
set load 1,
set in -1,
set address %X4FCF,
tick,
tock,
output,
set address %X504F,
tick,
tock,
output;
set address %X0FCF, // Did not also write to lower or upper RAM
eval,
output;
set address %X2FCF,
eval,
output;
set load 0, // Low order address bits connected
set address %X4FCE, eval, output;
set address %X4FCD, eval, output;
set address %X4FCB, eval, output;
set address %X4FC7, eval, output;
set address %X4FDF, eval, output;
set address %X4FEF, eval, output;
set address %X4F8F, eval, output;
set address %X4F4F, eval, output;
set address %X4ECF, eval, output;
set address %X4DCF, eval, output;
set address %X4BCF, eval, output;
set address %X47CF, eval, output;
set address %X5FCF, eval, output;
set load 0,
set address 24576,
echo "Make sure you see ONLY two horizontal lines in the middle of the screen. Hold down 'Y' (uppercase) until you see the next message ...",
// It's important to keep holding the key down since if the system is busy,
// the memory will zero itself before being outputted.
while out <> 89 {
eval,
}
clear-echo,
output;

25
projects/05/Rect.hack Normal file
View File

@@ -0,0 +1,25 @@
0000000000000000
1111110000010000
0000000000010111
1110001100000110
0000000000010000
1110001100001000
0100000000000000
1110110000010000
0000000000010001
1110001100001000
0000000000010001
1111110000100000
1110111010001000
0000000000010001
1111110000010000
0000000000100000
1110000010010000
0000000000010001
1110001100001000
0000000000010000
1111110010011000
0000000000001010
1110001100000001
0000000000010111
1110101010000111