Chapter 3 Memory
Chapter 3 Memory
Chapter 3 Memory
Nand to Tetris
Building a Modern Computer from First Principles
Chapter 3
Memory
These slides support chapter 3 of the book
The Elements of Computing Systems
By Noam Nisan and Shimon Schocken
MIT Press
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 1
Chapter 3: Memory
• Time matters
• Sequential logic
• Flip Flops
• Memory units
• Counters
• Project 3 overview
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Time-independent logic
• So far we ignored the issue of time
• The chip’s inputs were just “sitting there” – fixed and unchanging
• The chip’s output was a pure function of the current inputs,
and did not depend on anything that happened previously
• The output was computed “instantaneously”
• This style of gate logic is sometimes called:
q time-independent logic
q combinational logic.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Hello, time
Abstraction issues:
example:
• The hardware must support x = 17
maintaining “state”
example:
• The hardware must support for i = 0 … 99:
computations over time sum = sum + a[i]
Implementation issues:
• The hardware must handle the
physical time delays associated
with calculating and moving
data from one chip to another.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Physical time / clock time
physical
Arrow of time:
time:
Continuous
1
clock:
0
Discrete time:
State changes occur
only when advancing
time: 1 2 3 4 5 ... from one time unit to
another
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 5
Chip behavior over time (example)
physical
Arrow of time:
time:
Continuous
1
clock:
0
Discrete time:
State changes occur
only when advancing
time: 1 2 3 4 5 ... from one time unit to
another
in: 1
(example) 0
out: 1
(Not in) 0
(example)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 6
Chip behavior over time (example)
physical
Arrow of time:
time:
Continuous
1
clock:
0
Discrete time:
State changes occur
only when advancing
time: 1 2 3 4 5 ... from one time unit to
another
in: 1
(example) 0
out: 1
(Not in) 0
(example)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Chip behavior over time (example)
physical
Arrow of time:
time:
Continuous
1
clock:
0
Discrete time:
State changes occur
only when advancing
time: 1 2 3 4 5 ... from one time unit to
another
in: 1
(example) 0
out: 1
(Not in) 0
(example)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Chip behavior over time (example)
physical
Arrow of time:
time:
Continuous
1
clock:
0
Discrete time:
State changes occur
only when advancing
time: 1 2 3 4 5 ... from one time unit to
another
in: 1
(example) 0
out: 1
(Not in) 0
(example)
Combinational logic:
Sequential logic:
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Flip-Flop
in DFF out
out(t) = in(t - 1)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 11
Flip-Flop
in DFF out
out(t) = in(t - 1)
time: 1 2 3 4 5 ...
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 12
Flip-Flop
in DFF out
out(t) = in(t - 1)
time: 1 2 3 4 5 ...
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Flip-Flop
in DFF out
time: 1 2 3 4 5 ...
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 14
DFF implementation notes
A DFF bi-state architecture can be built from Nand gates:
• step 1: create an input-output loop,
achieving a combinational (un-clocked) flip-flop
• step 2: isolate across time steps using a “master-slave” architecture
• The resulting implementation is elegant, but conceptually confusing
Technical note
The implementation described above is impossible in our hardware simulator, since:
• The supplied simulator does not permit combinational loops
• A cycle in hardware connections is allowed only if the cycle passes through a
sequential (“clocked”) gate
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 15
Sequential chips
Sequential chips are capable of:
• maintaining state, and, optionally, state(t) = f (state(t-1), input(t))
• acting on the state, and on the current inputs
Example: DFF
• The DFF state: the value of the input from the previous time unit
• The simplest, most elementary sequential chip
Example: RAM
• The RAM state: the current values of all its registers
• given some address (input), the RAM emits the value of the selected register
Implementation note
• All combinational chips are constructed from Nand gates
• All sequential chips are constructed from DFF gates, and combinational chips.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Sequential chips
state(t) = f (state(t-1), input(t))
Implementation note
• All combinational chips are constructed from Nand gates
• All sequential chips are constructed from DFF gates, and combinational chips.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Sequential chip: 1-bit register
load
load load
in
in Bit out
out in Bit Bit ... Bit out
w w
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 18
1-bit register
load load
load: 1
(example) 0
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 19
1-bit register
load load
load: 1
(example) 0
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 20
1-bit register
load load
load: 1
(example) 0
in: 1
Resulting behavior:
(example) 0 Stores and emits a
value, until instructed
1 to load (and store) a
out:
new value
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 21
1-bit register implementation – first attempt
in out
out
in
DFF
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 22
1-bit register implementation
load
in
in
out out
out
MUX
DFF DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 23
1-bit register implementation
load
1
in
in 1
out 1 ? out
out
MUX
DFF ? DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 24
1-bit register implementation
load
1
in
in 1
out 1 ? out
out
MUX
DFF ? DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 25
1-bit register implementation
load
0
in
in 0
out 1 1 out
out
MUX
DFF 1 DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 26
1-bit register implementation
load
0
in
in 0
out 1 1 out
out
MUX
DFF 1 DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 27
1-bit register implementation
load
1
in
in 0
out 0 1 out
out
MUX
DFF 1 DFF 1
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 28
1-bit register implementation
load
0
in
in 0
out 0 0 out
out
MUX
DFF 0 DFF 0
in: 1
(example) 0
1
out:
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 29
1-bit register implementation
load
0
in
in 0
out 0 0 out
out
MUX
DFF 0 DFF 0
in: 1
Resulting behavior:
(example) 0 Stores and emits a
value, until instructed
1 to load (and store) a
out: new value
0
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 30
Chapter 3: Memory
• Time matters
• Sequential logic
• Flip Flops
• Memory units
• Counters
• Project 3 overview
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 31
Memory units
• 1-bit register:
• Designed to store a single bit
• Multi-bit register:
• Designed to store an w-bit value (in Hack, w = 16)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 32
Multi-bit register (also known as “register”)
(multi-bit register)
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 33
Register: abstraction
(multi-bit register)
Result:
To set Register = v q The Register’s state becomes v;
set in = v q From the next cycle onward,
out emits v
set load = 1
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 34
Register: implementation
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 35
Register chip in action
Run the clock
Inspect the
register’s output
Set in to 17
Inspect the
register’s contents
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 36
Register chip in action
Run the clock
Inspect the
register’s output
Inspect the
register’s contents
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 37
Register chip in action
Run the clock
Inspect the
register’s output
Set in to 17
Set load to 1
Inspect the
register’s contents
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 38
Register chip in action
Run the clock
Inspect the
register’s output
Inspect the
register’s contents
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 39
Memory units
• 1-bit register:
• Multi-bit register:
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 40
RAM
Architecture:
A sequence of n addressable registers,
with addresses 0 to n-1
Address width:
k = log2n
Word width:
No impact on the RAM logic
(Hack computer: w = 16]
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 41
RAM: abstraction
To read Register i :
Result:
set address = i out emits the value of Register i
probe out
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 42
RAM: abstraction
To set Register i to v :
Result:
set address = i
• The state of Register i becomes v
set in = v
• From the next cycle onward, out emits v
set load = 1
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 43
RAM: abstraction
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 44
A family of 16-bit RAM chips
chip name n k
RAM8 8 3
RAM64 64 6
RAM512 512 9
RAM4K 4096 12
RAM16K 16384 14
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 45
RAM chip in action
HW Simulator
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 46
Chapter 3: Memory
• Time matters
• Sequential logic
• Flip Flops
• Memory units
• Counters
• Project 3 overview
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 47
Where counters come to play
• The computer must keep track of which instruction should be fetched and executed
next
• This control mechanism can be realized by a register called Program Counter
• The PC contains the address of the instruction that will be fetched and executed next
• The PC is designed to support three possible control operations:
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 48
Program Counter
/**
* A 16-bit counter with load and reset control bits.
* if reset(t) out(t+1) = 0
* else if load(t) out(t+1) = in(t)
* else if inc(t) out(t+1) = out(t) + 1 (integer addition)
* else out(t+1) = out(t)
*/
CHIP PC {
IN in[16],load,inc,reset;
OUT out[16];
PARTS:
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Counter chip in action
HW Simulator
PC chip demo
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 50
Chapter 3: Memory
• Time matters
• Sequential logic
• Flip Flops
• Memory units
• Counters
• Project 3 overview
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 51
Project 3
Given:
q All the chips built in Projects 1 and 2
q Bit
q Register
q RAM8 A family of sequential chips, from
q RAM64 a 1-bit register to a 16K RAM unit.
q RAM512
q RAM4K
q RAM16K
q PC
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 52
1-bit register
Bit.hdl
/**
* 1-bit register:
* If load(t) then out(t+1) = in(t)
* else out(t+1) = out(t))
*/ Implementation tip:
CHIP Bit {
Can be built from a DFF
IN in, load; and a multiplexor.
OUT out;
PARTS:
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 53
16-bit Register
Register.hdl
/**
* 16-bit register:
* If load(t) then out(t+1) = in(t)
* else out(t+1) = out(t))
*/ Implementation tip:
CHIP Register { Can be built from an array
IN in[16], load;
OUT out[16]; of sixteen 1-bit registers.
PARTS:
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 54
8-Register RAM
RAM8.hdl
/*
* Let M stand for the state of the
* register selected by address.
* if load(t) then {M=in(t), out(t+1)=M}
* else out(t+1)=M
*/
CHIP RAM8 {
IN in[16], load, address[3];
OUT out[16];
PARTS:
// Put your code here:
}
Implementation tips:
q Feed the in value to all the registers, simultaneously
q Use mux / demux chips to select the register specified by address.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 55
Project 3
Given:
q All the chips built in Projects 1 and 2
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 56
RAM8, RAM64, … RAM16K
RAM512
RAM64
Implementation tips
• A RAM unit can be built by grouping smaller RAM-parts together
• Think about the RAM’s address input as consisting of two fields:
– one field can be used to select a RAM-part;
– the other field can be used to select a register within that RAM-part
• Use mux/demux logic to effect this addressing scheme.
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 57
Project 3
Given:
q All the chips built in Projects 1 and 2
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 58
Program Counter
Implementation tip:
Can be built from a register,
an incrementor, and some
logic gates.
/**
* A 16-bit counter with load, inc, and reset control bits.
*
* if reset(t) out(t+1)=0 // resetting: counter = 0
* else if load(t) out(t+1)=in(t) // setting counter = value
* else if inc(t) out(t+1)=out(t)+1 // incrementing: counter++
* else out(t+1)=out(t) // counter does not change
*/
CHIP PC {
IN in[16], load, inc, reset;
OUT out[16];
PARTS:
// Implementation comes here.
}
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 59
Project 3 resources
www.nand2tetris.org
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 60
More resources
• HDL Survival Guide
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 61
Best practice advice
• Try to implement the chips in the given order
• If you don’t implement some of the chips required in project 3, you can
still use them as chip-parts in other chips. Just rename the given stub-
files; this will cause the simulator to use the built-in versions of these
chips
• You can invent new, “helper chips”; however, this is not required: you
can build any chip using previously-built chips only
• Strive to use as few chip-parts as possible.
• For technical reasons, the HDL files of this project are organized in two
directories named a and b
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 62
Chapter 3: Memory
• Time matters
• Sequential logic
• Flip Flops
• Memory units
• Counters
• Project 3 overview
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 63
From Nand to Tetris
Building a Modern Computer from First Principles
Chapter 3
Memory
These slides support chapter 3 of the book
The Elements of Computing Systems
By Noam Nisan and Shimon Schocken
MIT Press
Nand to Tetris / www.nand2tetris.org / Chapter 3 / Copyright © Noam Nisan and Shimon Schocken Slide 64