Professional Documents
Culture Documents
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
• Fetch-Execute Cycle
• Project 5 Overview
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Universality
Theory Practice
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
• Fetch-Execute Cycle
• Project 5 Overview
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 14
Basic CPU loop
Repeat:
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
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
program data
ALU
instruction
data
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
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
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
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
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
• Advantage:
q Complication avoided Sometmes called
“Harvard Architecture”
• Disadvantage:
q Two memory chips instead of one
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 26
Computer Architecture: lecture plan
• Fetch-Execute Cycle
• Project 5 Overview
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 27
Hack computer
Computer System
CPU
A register
PC
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 28
Hack CPU
Computer System
CPU
A register
PC
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
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
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
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
011111
11…01
1110011111010111
10…10
writeM
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
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
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)
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
• Fetch-Execute Cycle
• Project 5 Overview
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Hack Computer
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
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
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
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
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
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
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
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
Hack Program
0000000000001101
1110101001010101
0000000000000001
instruc- instruct. data out
1110101001101011
CPU data
0000001100110101 load tion
1110010111011111
memory memory
address data in
1111001001100111
reset
(to:
(from: the address out the CPU’s
CPU’s pc ROM32K instruction
14 16
output) output)
Built-in
chip
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
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 72
Computer Architecture: lecture plan
• Fetch-Execute Cycle
• 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
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
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
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
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];
• 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
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
Nand to Tetris / www.nand2tetris.org / Chapter 5 / Copyright © Noam Nisan and Shimon Schocken Slide 90
Computer Architecture: lecture plan
• Fetch-Execute Cycle
• 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