lecture 01 Boolean logic

Boolean Logic
Building a Modern Computer From First Principles
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 1
Boolean algebra
Some elementary Boolean functions:
Not(x)
xx
00
11
Not(x)
Not(x)
11
00
And(x,y)
xx
00
00
11
11
yy
00
11
00
11
And(x,y)
And(x,y)
00
00
00
11
xx
00
00
11
11
yy
00
11
00
11
Nand(x,y)
Nand(x,y)
11
11
11
00
Or(x,y)
Nand(x,y)
xx
00
00
11
11
Boolean functions:
x
y
z
f ( x, y , z ) = ( x + y ) z
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
0
0
1
0
1
0
1
0
yy
00
11
00
11
Or(x,y)
Or(x,y)
00
11
11
11
A Boolean function can be expressed using a
functional expression or a truth table expression
Important observation:
Every Boolean function can be expressed using
And, Or, Not.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 2
All Boolean functions of 2 variables
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 3
Boolean algebra
Given: Nand(a,b), false
We can build:
Not(a) = Nand(a,a)
true = Not(false)
And(a,b) = Not(Nand(a,b))
George Boole, 1815-1864
(“A Calculus of Logic”)
Or(a,b) = Not(And(Not(a),Not(b)))
Xor(a,b) = Or(And(a,Not(b)),And(Not(a),b)))
Etc.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 4
Gate logic
Gate logic – a gate architecture designed to implement a Boolean function
Elementary gates:
Composite gates:
Important distinction: Interface (what) VS implementation (how).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 5
Gate logic
Interface
a
Xor
out
b
Claude Shannon, 1916-2001
a
b
out
0
0
1
1
0
1
0
1
0
1
1
0
(“Symbolic Analysis of Relay and
Switching Circuits” )
Implementation
a
And
Not
Or
out
Not
b
And
Xor(a,b) = Or(And(a,Not(b)),And(Not(a),b)))
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 6
Circuit implementations
a
a
b
a
0
0
1
1
AND gate
b
0
1
0
1
out
0
0
0
1
b
OR gate
power supply
b
0
1
0
1
out
0
1
1
1
power supply
out
a
b
a
0
0
0
0
1
1
1
1
a
0
0
1
1
b
0
0
1
1
0
0
1
1
c out
0 0
1 0
0 0
1 0
0 0
1 0
0 0
1 1
out
c
a
AND (a,b,c)
b
AND
out
c
AND
out
From a computer science perspective,
physical realizations of logic gates are irrelevant.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 7
Project 1: elementary logic gates
Given: Nand(a,b), false
aa
00
00
11
11
Build:
bb
00
11
00
11
Nand(a,b)
Nand(a,b)
11
11
11
00
Not(a) = ...
true = ...
And(a,b) = ...
Q: Why these particular 12 gates?
A: Since …
Or(a,b) = ...
Mux(a,b,sel) = ...
Etc. - 12 gates altogether.
They are commonly used gates
They provide all the basic
building blocks needed to build
our computer.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 8
Multiplexer
aa
00
00
00
00
11
11
11
11
bb sel
sel
00 00
00 11
11 00
11 11
00 00
00 11
11 00
11 11
out
out
00
00
00
11
11
00
11
11
sel
sel out
out
00
aa
11
bb
a
Mux
out
b
sel
Proposed Implementation: based on Not, And, Or gates.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 9
Example: Building an And gate
Contract:
And.cmp
a
b
And
out
And.hdl
CHIP
CHIP And
And
{{ IN
IN a,
a, b;
b;
OUT
out;
OUT out;
//
// implementation
implementation missing
missing
}}
aa
00
00
11
11
bb out
out
00 00
11 00
00 00
11 11
When running your
.hdl on our .tst,
your .out should
be the same as
our .cmp.
And.tst
load
load And.hdl,
And.hdl,
output-file
output-file And.out,
And.out,
compare-to
compare-to And.cmp,
And.cmp,
output-list
output-list aa bb out;
out;
set
a
0,set
b
0,eval,output;
set a 0,set b 0,eval,output;
set
set aa 0,set
0,set bb 1,eval,output;
1,eval,output;
set
a
1,set
b
0,eval,output;
set a 1,set b 0,eval,output;
set
set aa 1,
1, set
set bb 1,
1, eval,
eval, output;
output;
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 10
Building an And gate
Interface: And(a,b) = 1 exactly when a=b=1
a
And
b
out
And.hdl
CHIP
CHIP And
And
{{ IN
IN a,
a, b;
b;
OUT
out;
OUT out;
//
// implementation
implementation missing
missing
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 11
Building an And gate
Implementation: And(a,b) = Not(Nand(a,b))
a
out
b
And.hdl
CHIP
CHIP And
And
{{ IN
IN a,
a, b;
b;
OUT
out;
OUT out;
//
// implementation
implementation missing
missing
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 12
Building an And gate
Implementation: And(a,b) = Not(Nand(a,b))
a
a
Nand
b
out
x
in
Not
out
out
b
And.hdl
CHIP
CHIP And
And
{{ IN
IN a,
a, b;
b;
OUT
out;
OUT out;
//
// implementation
implementation missing
missing
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 13
Building an And gate
Implementation: And(a,b) = Not(Nand(a,b))
a
a
NAND
b
out
x
in
NOT
out
out
b
And.hdl
CHIP
CHIP And
And
{{ IN
IN a,
a, b;
b;
OUT
out;
OUT out;
Nand(a
Nand(a == a,
a,
bb == b,
b,
out
=
out = x);
x);
Not(in
Not(in == x,
x, out
out == out)
out)
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 14
Hardware simulator (demonstrating Xor gate construction)
HDL
program
test
script
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 15
Hardware simulator
HDL
program
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 16
Hardware simulator
HDL
program
output
file
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 17
Project materials: www.nand2tetris.org
Project 1 web site
And.hdl ,
And.tst ,
And.cmp files
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 18
Project 1 tips
Read the Introduction + Chapter 1 of the book
Download the book’s software suite
Go through the hardware simulator tutorial
Do Project 0 (optional)
You’re in business.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 19
Perspective
Each Boolean function has a canonical representation
The canonical representation is expressed in terms of And, Not, Or
And, Not, Or can be expressed in terms of Nand alone
Ergo, every Boolean function can be realized by a standard PLD
consisting of Nand gates only
a
Mass production
b
and
c
Universal building blocks,
unique topology
..
.
or
f(a,b,c)
and
Gates, neurons, atoms, …
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 20
End notes: Canonical representation
Whodunit story: Each suspect may or may not have an alibi (a), a motivation to
commit the crime (m), and a relationship to the weapon found in the scene
of the crime (w). The police decides to focus attention only on suspects
for whom the proposition Not(a) And (m Or w) is true.
Truth table of the "suspect" function
Canonical form:
s (a, m, w) = a ⋅ ( m + w)
s ( a, m, w) = a m w + a m w + a m w
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 21
End notes: Canonical representation (cont.)
s (a, m, w) = a ⋅ ( m + w)
a
or
s
m
w
and
s ( a, m, w) = a m w + a m w + a m w
a
m
and
w
and
or
s
and
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 22
End notes: Programmable Logic Device for 3-way functions
a
legend:
active fuse
and
b
blown fuse
c
8 and terms
connected to the
same 3 inputs
..
.
and
or
f(a,b,c)
single or term
connected to the
outputs of 8 and terms
_
_ _
PLD implementation of f(a,b,c)= a b c + a b c
(the on/off states of the fuses determine which gates participate in the computation)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 23
End notes: universal building blocks, unique topology
a
b
and
c
..
.
or
f(a,b,c)
and
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic
slide 24