Nand2Tetris is a computer science course by the MIT Press. It takes you through the steps of building a computer starting with nothing more than simple NAND gates. It then guides you through the process of building an assembler, virtual machine, and compiler for an objected oriented programming language. This repo contains only my own work and excludes the extra course files.
The hardware design used by the course is named HACK. Circuits are designed using a Hardware Description Language (HDL) with the .hdl
extension. Machine code has the .hack
extension.
- 16 bit
- 32K RAM
- 512x256 black & white screen
- Keyboard
Chapter 1 - Boolean Logic
NOT GateOR Gate
AND Gate
XOR Gate
Multiplexor
Demultiplexor
16 Bit NOT Gate
16 Bit OR Gate
16 Bit AND Gate
16 Bit Multiplexor
16 Bit Demultiplexor
4 Way Multiplexor
8 Way Multiplexor
4 Way Demultiplexor
8 Way Demultiplexor
Chapter 2 - Boolean Arithmetic
Half AdderFull Adder
16 Bit Adder
16 Bit Incrementer
ALU
Chapter 3 - Sequential Logic
BitProgram Counter
Register
RAM Chips
Chapter 4 - Machine Language
Assembly program to toggle screen colorAssembly program to multiple two integers
Chapter 5 - Computer Architecture
CPUMemory
Computer
The target language for the course's compiler is the Jack programming language, an object orient language that somewhat resembles Java. It supports strings, arrays, basic math, basic drawing, a keyboard, and direct memory access. The Jack standard library is, of course, written in Jack.
Jack Standard Library Source Code
The compilation process again resembles Java by being split into 3 parts; a compiler, a virtual machine, and an assembler. The compiler converts an OOP Jack program into a platform agnostic virtual machine language program. The virtual machine then translates the VM program into a hardware specific HACK program. Finally, the assembler converts the HACK program into machine code that is runnable by the HACK hardware described previously.
The Jack Compiler converts any Jack files it finds into a corresponding virtual machine file. The VM output is platform agnostic.
The VM Translator converts platform agnostic virtual machine code into HACK assembly code. To add support for a new hardware platform, you would modify the VM Translator and create a new assembler. Doing so would then allow you to use any language built on top of this VM implementation, in this case the Jack Compiler.
The assembler converts HACK assembly code into HACK machine code. This is a one to one conversion where each line of assembly code converts into exactly 16 bits of machine lanugage.