This document provides an outline and introduction for the Nand2Tetris course, which teaches students to build a computer from basic logic gates up through an entire computer architecture. The outline covers building combinational logic circuits in part 1 and developing a virtual machine and compiler in part 2. Key concepts discussed include NAND gates, Boolean logic, ALU design, CPU implementation, assembly language, stack-based virtual machines, and high-level language compilers. Diagrams illustrate the course progression from basic gates to an entire computer system.
Introduction to the Nand2Tetris course designed by Noam Nisan and Shimon Schocken, outlining course content and resources. Detailed breakdown of course weeks, covering topics from basic logic gates to CPU architecture.
Introduction to Nand and its significance in circuits, leading to ALU and sequential logic.
Focus on D Flip-Flops and combinational circuits, explaining memory structures and CPU elements.
Overview of instruction formats in CPU operations, detailing ROM and memory structure.
Hack Assembly language structure, instructions, and example implementations.
Introduction to Stack Virtual Machine architecture, including push/pop management and command implementation.
Overview and implementation of Jack language, including compiler design and array handling.
Key functionalities of an OS including memory management, input processing, and math functions.Summary of the Nand2Tetris journey from hardware to software, with references for further learning.
About the Speaker
●YehBan (aka: Yodalee)
● Blogger: http://yodalee.blogspot.tw/
● Programmer: https://github.com/yodalee
3.
Outline
● Introduction toNand2Tetris
● Detail content in each week
○ Part 1:
■ Week 1, 2: Nand to ALU
■ Week 3, 5: Sequential Logic to CPU
■ Week 4, 6: Assembly and Assembler
○ Part 2:
■ Week 1, 2: Stack Virtual Machine
■ Week 4, 5: Jack Compiler
■ Week 6: Runtime and OS
4.
Introduction
● Course designedby Noam Nisan and Shimon Schocken
of Hebrew University of Jerusalem
● Build a computer from Nand Gate, and take a glance at
the mystery of computer
● Open on coursera
○ https://www.coursera.org/learn/build-a-computer
○ https://www.coursera.org/learn/nand2tetris2
https://www.ted.com/talks/shimon_schocken_the_self_organizing_computer_course
Part 1 week1, 2: Nand to ALU
Combinational Circuit
7.
From the Beginning:Nand
A B Nand(A, B)
0 0 1
0 1 1
1 0 1
1 1 0
● Nand is one kind of basic logic gate
○ Why Nand, not Nor?
● The truth table of Nand
https://electronics.stackexchange.com/questions/110649/why-is-nand-gate-preferred-over-nor-gate-in-industry
8.
Nand is functionallycomplete
● Nand(X, X) == Not(X)
● Not(Nand(X, Y)) == And(X, Y)
● Nand(Not(X), Not(X)) == Or(X, Y)
○ (A+B)’’ = (A’B’)’
● Or(And(X, Not(Y)), And(Not(X), Y)) == Xor(X, Y)
● Given A, B, sel
○ Or(And(A, sel), And(B, Not(sel))) => Mux(A, B, sel)
● Given in, sel
○ A = And(in, sel); B = And(in, not(sel)) => Demux(in, sel)
https://en.wikipedia.org/wiki/Functional_completeness#Minimal_functionally_complete_operator_sets
9.
More complex
● Extendto 16 bits gates
○ 16 way OR, AND, Mux, DMux ...
● Full Adder: Xor + And + Or
○ Extend to 16 bits Full Adder
● Custom ALU
○ Data in/out x, y/out
○ Control: zx, nx, zy, ny, f, no
○ Out bit: zr, ng
11.
ALU Component inDetail
mux for zx,
zy
mux for nx, ny
Select
x+y or x & y Mux with no
Hack Assembly
1. Ainstruction -> “@number” //load number into A register
2. Support label, like
(label) //define label
@label //load label address to A register
3. C instruction -> “dest = comp; jump"
a. Dest: combination of DMA
b. Comp: arithmetic on register
c. Jump: Jump condition
Ex. A = D+A; JMP
Implement an Assembler
1.Scan (label) in program, record the address
2. Translate @label, @const to A instruction bitcode
3. Translate C instruction
● Directly map “string” to bitcode
● If Dest has M -> A = 1
● M -> 001, D -> 010, MD -> 011 ...
● JGT -> 001, JEQ -> 010, JMP -> 111 …
4. Input Assembly; Output Bitcode that can be programmed
to instruction ROM
Stack Virtual Machine
●Push/Pop command, possible target
Constant Push only
Static File-scope static variable
Pointer Push/Pop to THIS/THAT register
Temp
Local Local variable in function
Argument Function argument
This Class
That Array
High Level ProgrammingLanguage and its Compiler
● Jack: high level programming language
○ Support Array and Class
○ No inheritance
● A compiler translate high level programming language to
VM implementation
○ Tokenizer
○ Syntax Analyzer
○ Code Generator
44.
Tokenizer
while (i <length) {
let i = i + 1;
}
whil
e
( i < length ) { let
i = i + 1 ; }
Code Generation: SymbolTable
● A table save the id of each variable
var Array a;
var int length;
var int i, sum;
let i = 0;
while (i < length)
a LCL 0
length LCL 1
i LCL 2
sum LCL 3
push constant 0
pop local 2
label Lwhile0
push local 2
push local 1
lt
not
if-goto Lwhile1
48.
Code Generation: Object
●Size of class is constant at compile time
● Translate object constructor (special name “new”):
○ Alloc memory
○ Save to THIS register
○ Return THIS register
● Translate object method: ex. obj.method(arg1, arg2):
○ Push obj as Argument 0
○ Push Argument 1, Argument 2
○ Call method
○ In method: Save Argument 0 to THIS register
49.
Code Generation: Array
●Array will call Array.new to allocate memory, save result to
variable (the address of Array)
● Access Array arr[expr]
○ Calculate index expr
○ Add arr address and index
○ Pop to THAT register
○ Push/Pop THAT register content
● Complex example: arrA[exprA] = arrB[exprB]
Implement OS service
●Array // Array function
● Keyboard // read keyboard input
● Math // multiply, divide, power
● Memory // peek, poke, alloc, dealloc
● Output // Print text on screen
● Screen // Draw line, rectangle, circle on Screen
● String // String related function
● Sys // init, halt
http://nand2tetris.org/projects/12/Jack%20OS%20API.pdf
52.
Memory: peek/poke
class Memory{
static array ram;
function void init() {
let ram = 0;
return;
}
function int peek(int address) {
return ram[address];
}
function void poke(
int address,
int value) {
let ram[address] = value;
return;
}
53.
Memory: manage heapspace
2786
2
15360
4
0
1024
2048 2786 15360
Next
Size
2786
2
3192
1
0
1024
2048 2789 3192
Next
Size
After Memory.Alloc(1)
0
1
data
2786
Alloc ListFree List
54.
Conclusion
● Computer isa structural design from simple hardware
elements, all from Nand and D Flip-Flop
● Be a “Fullstack Hello World Engineer”
○ 自定義一組指令集
○ port 組譯器
○ port 編譯器
○ port 作業系統
○ port libc
○ 寫一個 hello world