L18 – HACK CPU
Hack computer
input
instruction memory
instruction
address
of next instruction
data out
ALU data memory
output
Inthepreviousmodulewe learntabout theconstructionofthe HACKcomputer
andtheInstructionMemory
Computer System CPU
D register A register PC
andtheabstractionandimplementationof theDatamemory RAM screenandkeyboard
data in
credit: nand2tetris.org
2
Hack CPU
input
instruction memory
instruction
address
of next instruction
data out
ALU data
a
output
Computer System CPU
In thismodule we willdiscuss
If memory data in
D register
A register
O
PC
Hack CPU: A 16-bit processor, designed to:
• Execute the current instruction: dataOut = instruction(dataIn) • Figureoutwhichinstructiontoecrxedeict:unatned2nteetrxis.to.rg
3
Hack CPU Interface
From
INM
from
themes 16 1
Data
data
To
11 WRITEM toMdDaetadmmoy
115 memory AddversM
pcout Instruction 1S to To
instrucMtioenmory
memorays 15 bitgnstruction
memory
OU M
from instFrurcotimon
mIenmsotruyction Memory
from the user
External
INSTRUCTION
I 16
Reset
credit: nand2tetris.org
4
data cont logic signabdtoudameftao
Hack CPU Implementation
O f
LD
EI
I
C instruction
AlnstrO
21 1 A 21 IiEnmi.o
icontroi
controllogic
no f i fx 3
y kg
qs.is
ou
credit: nand2tetris.org
generic
notation 5
Eo.rnest ogsgY9i
pc
Tnnneg.number OC binarytranslation
smc
Hack CPU Implementation
same picture asthe one in
previous slide
pc
(each “C” symbol represents a control bit) credit: nand2tetris.org
6
HACK CPU OPERATION
• Instruction Handling • Interpret
• Execute
• Program Control
7
HACK Instructions
credit: nand2tetris.org 8
CPU operation: handling A-instructions – interpret
@5 0000000000000101 GO
A-instruction
content A
to beloadedinto refuter
CPU handling of an A-instruction: interpret interpretation of an A Instruction simplynests in
examining bit 15 ofthe instruction Instruction is D 0 means it is an A Instruction
credit: nand2tetris.org 9
CPU operation: handling A-instructions – execute at
@5 0000000000000101 A-instruction
Td
a
CPU handling of an A-instruction: execute control signal
I
Load2 26 Mbda
generate the for and AregisterLoad 2
select A
credit: nand2tetris.org
10
My
CPU operation: handling C-instructions – interpret
f jump D=D+1;JMP 1110011111010111
dest
CPU handling of a C-instruction: interpret
C-instruction
i
AU control
Decodingoftheinstmet opcode
ALUcontrol b Destinationbits
Jumpbits
credit: nand2tetris.org 11
CPU operation: handling C-instructions – execute 011111
11…01
O
111001f1111010111
CPU handling of a C-instruction: ALU inputs
00
writeM
10…10
ALU data inputs:
• Input 1: from the D-register
• Input 2: from either:
A-register, or data memory
ALU control inputs:
• control bits
(from the instruction)
credit: nand2tetris.org
12
CPU operation: handling C-instructions – execute
Ff 1
011111
0
11…01
1110011111010111
A Dtl JMP O
10…10
O
o
01 … 11
writeM
D Dtl JMP
0
24h8
CPU handling of a C-instruction: ALU outputs
ALU data output:
• Result of ALU calculation
ALU contrIol outputs:
• is the output negative?
• is the output zero?
• Fed simultaneously to: D-register, A-register, data memory
• WhichdestinationactuallycommitstotheALUoutputisdeterminedby
the instruction’s destination bits.
credit: nand2tetris.org 13
pc
CPU operation: program control
credit: nand2tetris.org 14
CPU operation: program control
1
loa d
16
pc
PC operation (abstraction)
Emits the address of the next instruction:
address of next instruction
16
• restart:
• no jump: • goto:
PC = 0 PC++
PC=A
• conditional goto: if (condition) PC=A else PC++
credit: nand2tetris.org
15
CPU operation: program control
111 a c c c c c c d d d j j j
O
PC operation (implementation) if (reset==1) PC=0
else
// in the course of handling the current instruction:
address of next instruction
load = f (jump bits, ALU control outputs) if (load == 1) PC=A // jump
else PC++ // next instruction
credit: nand2tetris.org
E DOJE
16
1
16
load
16
pc
AN control ZERO
NG
Hack CPU Implementation
O O
O
O
O O
O
That’s It!
credit: nand2tetris.org
17
pc
Project – P5
credit: nand2tetris.org 18
Hardware projects
computer
p5
memory
p5
RAM chips p3
registers
CPU
p5
PC
p3
adder
ALU
p2
p3
p2
elementary logic gates
p1
credit: nand2tetris.org
19
Abstraction
Hack Computer
credit: nand2tetris.org 20
Implementation
credit: nand2tetris.org 21
CPU Abstraction
Hack CPU
credit: nand2tetris.org 22
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.
credit: nand2tetris.org 23
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;
OUT outM[16]
writeM,
addressM[15],
pc[15];
PARTS:
// Put you code here:
// Signals whether to re-start the current program
// (reset == 1) or continue executing the current
// program (reset == 0).
// value to write into M = RAM[A]
// Write into M?
// RAM address (of M)
// ROM address (of next instruction)
credit: nand2tetris.org 24
Hack Computer implementation
✔
credit: nand2tetris.org 25
Memory implementation
Memory
credit: nand2tetris.org 26
Memory implementation
/** Memory of 16K 16-bit registers */
CHIP RAM16K {
IN
address[14],
in[16],
/** Memory of 8K 16-bit registers
OUT
load;
* with a display unit side effect. */
CHIP Screen {
out[16];
IN
address[13],
BUILTIN RAM16K;
in[16],
CLOCKED in, load;
CHIP Keyboard {
BUILTIN Screen;
OUT
CLOCKED in, load;
out[16];
BUILTIN Keyboard;
}
}
load; OUT
/** 16-bit register with a out[16]; * keyboard input side effect */
}
built in project 3
buiilltt-iin chiips
Implementation tips:
• Usethethreechip-parts:
RAM16K, Screen, and Keyboard
• Route the address input to the correct address input of the relevant chip-part.
credit: nand2tetris.org
27
Hack Computer implementation
Implementation tip:
Use the built-in ROM32K chip.
✔
✔
credit: nand2tetris.org 28
Hack Computer implementation
✔
✔
✔
credit: nand2tetris.org 29
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.
}
credit: nand2tetris.org
30
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.
credit: nand2tetris.org 31
Perspective: from here to a “Traditional” computer
Caching Pipelining/Super-Scalar More I/O units
Special-purpose processors (I/O, graphics, communications, …)
32
Perspective: from here to a “Traditional” computer
Multi-core / parallelism/
Efficiency
Energy consumption considerations
Security
33
Perspective: some issues we haven’t discussed (among many)
Hardware diversity: desktop, laptop, hand-held, game machines, …
General-purpose vs. embedded computers Silicon compilers
And more …
34