You are on page 1of 92

From

Nand to Tetris
Building a Modern Computer from First Principles

Chapter 5

Computer Architecture
These slides support chapter 5 of the book
The Elements of Computing Systems
By Noam Nisan and Shimon Schocken
MIT Press

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 1
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Universality

Same hardware can run many different software programs

Theory Practice

Alan Turing: John Von Nuemann:


Universal Turing Machine Stored Program Computer

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Computer architecture
Memory CPU

program
Registers
intput ALU output
Ÿ
Ÿ

data Ÿ

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

control bus

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 5
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 6
Computer architecture
Memory CPU

program
Registers
ALU
ALU

Ÿ
Ÿ

data Ÿ

control bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Computer architecture
Memory CPU

Registers
ALU

Ÿ
Ÿ

data Ÿ

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 11
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ
Ÿ

control bus

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 12
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

control bus

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 14
Basic CPU loop
Repeat:

• Fetch an instruction from the program memory

• Execute the instruction.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 15
Fetching
• Put the location of the next instruction in the Memory address input
• Get the instruction code by reading the contents at that Memory location
Memory

program
Memory
output
instruction

data

Memory
address • Default: PC++
input • Jump: PC = address
Program
Counter

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Executing
• The instruction code specifies “what to do”
q Which arithmetic or logical instruction to execute
q Which memory address to access (for read / write)
q If / where to jump
q …
different subsets of the
instruction bits control different
aspects of the operation

• Executing the instruction involves:


q accessing registers
q and / or:
q accessing the data memory.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Computer architecture
Memory CPU

program
Registers
ALU

Ÿ
Ÿ

data Ÿ

control bus

address bus

data bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Fetch – Execute
Memory

program data

ALU
instruction

data

instruction data
address address

control bus

address bus

address bus (data flows not shown, to minimize clutter)


Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 19
Fetch – Execute clash
Memory

program data

ALU
instruction

data

If the Memory is one address space:


instruction data This scheme will not work:
address address
• one Memory input
• one Memory output

control bus

address bus

address bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 20
Fetch – Execute clash
Memory

program
Memory
output

data

If the Memory is one address space:


Memory
address This scheme will not work:
input
• one Memory input
• one Memory output
instruction data
address address

control bus

address bus

address bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 21
Solution: multiplex
Memory

program data, when executing


Memory
output

instruction, when fetching


data

Memory
address
input

mux
fetch /
data execute
instruction
address address bit

control bus

address bus

address bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 22
Solution: multiplex, using an instruction register
Memory

program data, when executing


Memory
output
ALU

instruction instruction
register
data
load
on
fetch
Memory
address
input

mux
fetch /
data execute
instruction
address address bit

control bus

address bus

address bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 23
Solution: multiplex, using an instruction register
Memory

program data, when executing


Memory
output
ALU
instruction instruction
instruction
register
data
load
on
fetch
Memory
address
input

mux
fetch /
data execute
instruction
address address bit

control bus

address bus

address bus
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 24
Solution: multiplex, using an instruction register
Memory

data
program
Memory
output
ALU
instruction instruction
instruction
register
data
load
on
fetch
Memory
address
input

mux
fetch /
data execute
instruction
address address bit

control bus

address bus

address bus (data flows not shown, to minimize clutter)


Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 25
Simpler solution: separate memory units
Variant of von Neumann Architecture (used by the Hack computer):

Two physically separate memory units:


q Instruction memory
Each can be addressed and manipulated
q Data memory seperately, and simultaneously

• Advantage:
q Complication avoided Sometmes called
“Harvard Architecture”
• Disadvantage:
q Two memory chips instead of one

q The size of the two chips is fixed.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 26
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 27
Hack computer
Computer System

CPU

instruction data out


ALU
instruction data
input output
memory memory
address data in
of next
instruction
D register

A register

PC

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 28
Hack CPU
Computer System

CPU

instruction data out


ALU
instruction data
input output
memory memory
address data in
of next
instruction
D register

A register

PC

Hack CPU: A 16-bit processor, designed to:


• Execute the current instruction: dataOut = instruction(dataIn)
• Figure out which instruction to execute next.
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 29
Hack CPU Interface

data in

data out

address of
restarting next
signal instruction

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 30
Hack CPU Implementation

pc

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 31
Hack CPU Implementation

pc

(each "C" symbol represents a control bit)


Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 32
Hack CPU Implementation

Disclaimer:
• In what follows we don’t explain all the details of the
CPU implementation, intentionally
• We give enough information to let you figure out the
implementation on your own, when you will actually
build the CPU in project 5.

pc

(each "C" symbol represents a control bit)


Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 33
CPU operation

pc

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 34
CPU operation: instruction handling

pc

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 35
CPU operation: instruction handling

@5 0000000000000101

A-instruction

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 36
CPU operation: handling A-instructions

@5 0000000000000101

A-instruction
CPU handling of an A-instruction:
• Decodes the instruction into:
q op-code

q 15-bit value

• Stores the value in the A-register


• Outputs the value (not shown in this diagram).

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 37
CPU operation: instruction handling

D=D+1;JMP 1110011111010111

C-instruction

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 38
CPU operation: handling C-instructions

D=D+1;JMP 1110011111010111

C-instruction CPU handling of a C-instruction:


• Decodes the instruction bits into:
q Op-code
q ALU control bits
q Destination load bits
q Jump bits
• Routes these bits to their chip-part destinations
• The chip-parts (most notably, the ALU) execute the instruction.
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 39
CPU operation: handling C-instructions

011111

11…01

1110011111010111
10…10

writeM

ALU data inputs: ALU control inputs:


• Input 1: from the D-register • control bits
(from the instruction)
• Input 2: from either:
q A-register, or
q data memory

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 40
CPU operation: handling C-instructions

1 011111
0
11…01

01 … 11

1110011111010111
10…10

writeM
0

ALU data output:


• Result of ALU calculation
• Fed simultaneously to: D-register, A-register, data memory
• Which destination actually commits to the ALU output is determined by
the instruction’s destination bits.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 41
CPU operation: handling C-instructions

011111

11…01

01 … 11

1110011111010111
10…10

writeM

ALU control outputs:


• is the output negative?
• is the output zero?

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 42
CPU operation: control

pc

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 43
CPU operation: control

reset reset
computer exterior
(well, kind of)

• The computer is loaded with some program;


• Pushing reset causes the program to start running.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 44
CPU operation: control

pc

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 45
CPU operation: control

16

1
loa
d
pc
16

PC operation (abstraction)
Emits the address of the next instruction: address of next
instruction
q restart: PC = 0
q no jump: PC++
q goto: PC=A
q conditional goto: if (condition) PC = A else PC++

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 46
CPU operation: control

111 a c c c c c c d d d j j j
16

1
loa
d
pc
16

PC operation (implementation)
if (reset==1) PC = 0 address of next
instruction
else
// in the course of handling the current instruction:
load = f (jump bits, ALU control outputs)
if (load == 1) PC = A // jump
else PC++ // next instruction
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 47
Hack CPU Implementation

pc

That’s It!
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 48
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Hack Computer

instruction data out


instruc-
CPU data
tion
memory
memory
address data in

Abstraction:
A computer capable of running programs written in the Hack machine language

Implementation:
Built from the Hack chip-set.
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 50
Hack CPU

instruction data out


instruc-
CPU data
tion
memory
memory
address data in

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 51
Hack CPU

CPU abstraction:
Executes a Hack instruction and figures out which instruction to execute next

CPU Implementation:
Discussed before.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 52
Hack Computer


instruction data out
instruc-
CPU data
tion
memory
memory
address data in

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 53
Memory

instruction data out


instruc-
tion CPU Memory
memory
address data in

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 54
Memory: abstraction
load

Memory
0

in RAM
16 (16K)

out
16,383
16
16,384 Screen
address
(8K memory
15 map)
24,575
24,576 Keyboard

q Address 0 to 16383: data memory


q Address 16384 to 24575: screen memory map
q Address 24576: keyboard memory map

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 55
Memory: implementation
load

Memory
0

in RAM
16 (16K)

out
16,383
16
16,384 Screen
address
(8K memory
15 map)
24,575
24,576 Keyboard

q Address 0 to 16383: data memory


q Address 16384 to 24575: screen memory map
q Address 24576: keyboard memory map

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 56
RAM
load

Memory
0

in RAM
16 (16K)

out
16,383
16
16,384 Screen
address built in
(8K memory
map) project 3
15 24,575
24,576 Keyboard

The Hack RAM is realized by the RAM16K chip implemented in project 3.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 57
Screen

Memory
0

in RAM
16 (16K)

out
16,383
16
16,384
address Screen
(8K memory
15 24,575 map)
24,576 Keyboard

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 58
Screen memory map
Display Unit (256 by 512, b/w)
Screen
0 1 2 3 4 5 6 7 Ÿ ŸŸ 511
(16384) 0 1111010100000000 0 Ÿ ŸŸ
1 0000000000000000 1 Ÿ ŸŸ
Ÿ row 0
Ÿ
Ÿ
Ÿ
31 0011000000000001
refresh Ÿ
Ÿ Ÿ
Ÿ Ÿ
32 0000101000000000
33 0000000000000000
Ÿ row 1 255 Ÿ ŸŸ
Ÿ
Ÿ

63 0000000000000000
Ÿ To set pixel (row,col) on/off:
Ÿ
Ÿ
(1) word = RAM[16384 + 32*row + col/16]
8159 0000000000000000
8160 0000000000000000 (2) Set the (col % 16)th bit of word to 0 or 1
Ÿ row 255
Ÿ
Ÿ (3) RAM[ i ] = word
8191 1011010100000000

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 59
Screen

Memory
0

in RAM
16 (16K)
built-in
out
chip
16,383
16
16,384
address Screen
(8K memory
15 24,575 map)
24,576 Keyboard

• The Hack screen is realized by a built-in chip named Screen

• Screen: a regular RAM + display output side-effect.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 60
Keyboard

Memory
0

in RAM
16 (16K)

out
16,383
16
16,384 Screen
address
(8K memory
15 map)
24,575
24,576 Keyboard

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 61
Keyboard memory map

Keyboard
0000000001101011
0000000001001011
0000000000000000
0000000000110100 k

Scan-code of ‘k’ = 75

The Keyboard chip emits the scan-code of the currently pressed key,
or 0 if no key is pressed.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 62
The Hack character set
key code key code key code key code key code
(space) 32 0 48 A 65 a 97 newline 128
! 33 1 49 B 66 b 98 backspace 129
“ 34 … … C … c 99 left arrow 130
# 35 9 57 … … … … up arrow 131
$ 36 Z 90 z 122 right arrow 132
: 58
% 37 down arrow 133
; 59 [ 91 { 123
& 38 home 134
< 60 / 92 | 124
‘ 39 end 135
= 61 ] 93 } 125
( 40 Page up 136
> 62 ^ 94 ~ 126
) 41 Page down 137
? 63 _ 95
* 42 insert 138
@ 64 ` 96
+ 43 delete 139
, 44 esc 140
- 45 f1 141
. 46 … …
/ 47 f12 152

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 63
Keyboard

Memory
0

in RAM built-in
(16K) chip
16
out
16,383 16
16,384 Screen
address
(8K memory
15 map)
24,575
24,576 Keyboard

• Realized by a built-in chip named Keyboard

• Keyboard: A read-only 16-bit register + a keyboard input side-effect.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 64
Memory implementation
load

Memory
0

in RAM
RAM
16 (16K)
(16K)
out
16,383 16
16,384 Screen
address Screen
(8K memory
(8K memory
15 map)
map)
24,575
24,576 Keyboard

Implementation outline:
• Uses the three chip-parts RAM16K, Screen, and Keyboard (as just described)
• Routes the address input to the correct address input of the relevant chip-part.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 65
Hack Computer

✔ ✔
instruc- instruct. data out
CPU data
tion
memory
memory
address data in

reset

reset
rese
t

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 66
Instruction memory
Hack Computer

Hack Program
0000000000001101
1110101001010101
0000000000000001
instruc- instruct. data out
1110101001101011
CPU data
0000001100110101 load tion
1110010111011111
memory memory
Ÿ address data in
Ÿ
Ÿ
1111001001100111

reset

To run a program on the Hack computer:


q Load the program into the Instruction Memory
Load a program
q Press “reset” into the Instruction
q The program starts running. Memory? How?
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 67
Instruction memory
Hack Computer

Hack Program
0000000000001101
1110101001010101
0000000000000001
instruc- instruct. data out
1110101001101011
CPU data
0000001100110101 load tion
1110010111011111
memory memory
Ÿ address data in
Ÿ
Ÿ
1111001001100111

reset

Loading a program into the Instruction Memory:


• Hardware implementation: plug-and-play ROM chips
(each comes pre-loaded with a program’s code)
• Hardware simulation: programs are stored in text files;
The simulator’s software features a load-program service.
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 68
Instruction memory

(to:
(from: the address out the CPU’s
CPU’s pc ROM32K instruction
14 16
output) output)

Built-in
chip

• The Hack Instruction Memory is realized by a built-in chip named ROM32K

• ROM32K: a read-only, 16-bit, 32K RAM chip + program loading side-effect.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 69
Hack Computer implementation

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 70
Hack Computer implementation

That’s it!

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 71
Hack Computer implementation

“We ascribe beauty to that which is simple; which has no superfluous


parts; which exactly answers its end; which stands related to all things;
which is the mean of many extremes.”
-- Ralph Waldo Emerson

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 72
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 73
Hardware organization: a hierarchy of chip parts

computer

memory CPU

RAM units PC ALU

registers adder

elementary
logic gates
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 74
Hardware projects

computer
p5

memory CPU
p5 p5

RAM chips PC ALU


p3 p3 p2

registers adder
p3
p2

elementary
logic gates p1
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 75
Project 5: building the Hack Computer

computer
p5

memory CPU
p5 p5

RAM chips PC ALU

registers adder

elementary
logic gates
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 76
Abstraction

Hack Computer

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 77
Implementation

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 78
CPU Abstraction

Hack CPU

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 79
CPU Implementation

111 a cccccc ddd jjj

Implementation tips:
• Chip-parts: Mux16, ARegister, DRegister, PC, ALU, …
• Control: use HDL subscripting to parse and route the instruction bits to the
control bits of the relevant chip-parts.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 80
CPU Implementation
CPU.hdl
/**
* The Central Processing unit (CPU).
* Consists of an ALU and a set of registers,
* designed to fetch and execute instructions
* written in the Hack machine language.
*/
CHIP CPU {

IN
inM[16], // value of M = 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] // value to write into M = RAM[A]
writeM, // Write into M?
addressM[15], // RAM address (of M)
pc[15]; // ROM address (of next instruction)

PARTS:
// Put you code here:
}

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 81
Hack Computer implementation

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 82
Memory implementation

Memory

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 83
Memory implementation
/** Memory of 16K 16-bit registers */
CHIP RAM16K { built in
IN project 3
address[14],
in[16],
/** Memory of 8K 16-bit registers
load;
OUT * with a display unit side effect. */
CHIP Screen {
out[16];
IN built-in
built-in
address[13], chips
chips
BUILTIN RAM16K;
in[16],
CLOCKED in, load;
} load;
OUT /** 16-bit register with a
out[16];
* keyboard input side effect */
CHIP Keyboard {
BUILTIN Screen;
OUT
CLOCKED in, load;
} out[16];

Implementation tips: BUILTIN Keyboard;


}
• Use the three chip-parts:
RAM16K, Screen, and Keyboard

• Route the address input to the correct address input of the relevant chip-part.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 84
Hack Computer implementation

✔ ✔

Implementation tip:
Use the built-in ROM32K chip.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 85
Hack Computer implementation

✔ ✔ ✔

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 86
Hack Computer implementation

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.
*/

CHIP Computer {

IN reset;

PARTS:
// Put your code here.
}

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 87
Project 5 resources

www.nand2tetris.org

All the necessary project 5


files are available in:
nand2tetris / projects / 05
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 88
More resources
• HDL Survival Guide

• Hardware Simulator Tutorial

• nand2tetris Q&A forum

All available in: www.nand2tetris.org

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 89
Best practice advice
• Try to implement the chips in the given order
• Strive to use as few chip-parts as possible

• You will have to use chips that you’ve implemented in previous projects

• The best practice is to use their built-in versions.

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 90
Computer Architecture: lecture plan

• Von Neumann Architecture

• Fetch-Execute Cycle

• The Hack CPU

• The Hack Computer

• Project 5 Overview

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 91
From Nand to Tetris
Building a Modern Computer from First Principles

Chapter 5

Computer Architecture
These slides support chapter 5 of the book
The Elements of Computing Systems
By Noam Nisan and Shimon Schocken
MIT Press

Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 92

You might also like