lecture 01 boolean logic - the elements of computing …nand2tetris.org/lectures/pdf/lecture 01...

24
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 1 www.nand2tetris.org Building a Modern Computer From First Principles Boolean Logic

Upload: doanlien

Post on 15-Mar-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 1

www.nand2tetris.org

Building a Modern Computer From First Principles

Boolean Logic

Page 2: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 2

Boolean algebra

Some elementary Boolean functions:

� Not(x)

� And(x,y)

� Or(x,y)

� Nand(x,y)

x y z

0 0 0 00 0 1 00 1 0 10 1 1 01 0 0 11 0 1 01 1 0 11 1 1 0

zyxzyxf )(),,( +=

Boolean functions:

� 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.

x y Nand(x,y)0 0 10 1 1 1 0 1 1 1 0

x y Nand(x,y)0 0 10 1 1 1 0 1 1 1 0

x y And(x,y)0 0 00 1 0 1 0 0 1 1 1

x y And(x,y)0 0 00 1 0 1 0 0 1 1 1

x y Or(x,y)0 0 00 1 1 1 0 1 1 1 1

x y Or(x,y)0 0 00 1 1 1 0 1 1 1 1

x Not(x)0 11 0

x Not(x)0 11 0

Page 3: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 3

All Boolean functions of 2 variables

Page 4: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 4

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))

� Or(a,b) = Not(And(Not(a),Not(b)))

� Xor(a,b) = Or(And(a,Not(b)),And(Not(a),b)))

� Etc.

George Boole, 1815-1864

(“A Calculus of Logic”)

Page 5: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 5

Gate logic

� Gate logic – a gate architecture designed to implement a Boolean function

� Elementary gates:

� Composite gates:

� Important distinction: Interface (what) VS implementation (how).

Page 6: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 6

Gate logic

And

And Not

Or out

a

b

Not

Xor(a,b) = Or(And(a,Not(b)),And(Not(a),b)))

Implementation

Xora

bout

0 0 00 1 11 0 11 1 0

a b out

Interface

Claude Shannon, 1916-2001

(“Symbolic Analysis of Relay and Switching Circuits” )

Page 7: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 7

0 0 00 1 01 0 01 1 1

a b out

a b

out

power supply

AND gate

power supply

a

b

out

0 0 00 1 11 0 11 1 1

a b out

OR gate

a

out

b c

0 0 0 00 0 1 00 1 0 00 1 1 01 0 0 01 0 1 01 1 0 01 1 1 1

a b c out

AND (a,b,c)

aAND

b

ANDc

out

Circuit implementations

� From a computer science perspective, physical realizations of logic gates are irrelevant.

Page 8: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 8

Project 1: elementary logic gates

Given: Nand(a,b), false

Build:

� Not(a) = ...

� true = ...

� And(a,b) = ...

� Or(a,b) = ...

� Mux(a,b,sel) = ...

� Etc. - 12 gates altogether.

a b Nand(a,b)0 0 10 1 1 1 0 1 1 1 0

a b Nand(a,b)0 0 10 1 1 1 0 1 1 1 0

Q: Why these particular 12 gates?

A: Since …

� They are commonly used gates

� They provide all the basic building blocks needed to build our computer.

Page 9: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 9

Multiplexer

Proposed Implementation: based on Not, And, Or gates.

a

b

sel

outMux

a b sel out

0 0 0 0

0 0 1 0

0 1 0 0

0 1 1 1

1 0 0 1

1 0 1 0

1 1 0 1

1 1 1 1

a b sel out

0 0 0 0

0 0 1 0

0 1 0 0

0 1 1 1

1 0 0 1

1 0 1 0

1 1 0 1

1 1 1 1

sel out

0 a

1 b

sel out

0 a

1 b

Page 10: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 10

outa

bAnd

a b out0 0 00 1 0 1 0 0 1 1 1

a b out0 0 00 1 0 1 0 0 1 1 1

And.cmp

load And.hdl,

output-file And.out,

compare-to And.cmp,

output-list a b out;

set a 0,set b 0,eval,output;

set a 0,set b 1,eval,output;

set a 1,set b 0,eval,output;

set a 1, set b 1, eval, output;

load And.hdl,

output-file And.out,

compare-to And.cmp,

output-list a b out;

set a 0,set b 0,eval,output;

set a 0,set b 1,eval,output;

set a 1,set b 0,eval,output;

set a 1, set b 1, eval, output;

And.tstAnd.hdl

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

Example: Building an And gate

Contract:

When running your .hdl on our .tst, your .out should be the same asour .cmp.

Page 11: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 11

Building an And gate

outa

bAnd

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

And.hdl

Interface: And(a,b) = 1 exactly when a=b=1

Page 12: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 12

Implementation: And(a,b) = Not(Nand(a,b))

outa

b

Building an And gate

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

And.hdl

Page 13: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 13

outNot

a

b

outNanda in out

xb

Implementation: And(a,b) = Not(Nand(a,b))

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

CHIP And

{ IN a, b;

OUT out;

// implementation missing

}

And.hdl

Building an And gate

Page 14: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 14

CHIP And

{ IN a, b;

OUT out;

Nand(a = a,b = b,out = x);

Not(in = x, out = out)

}

CHIP And

{ IN a, b;

OUT out;

Nand(a = a,b = b,out = x);

Not(in = x, out = out)

}

Implementation: And(a,b) = Not(Nand(a,b))

outNOT

a

b

outNANDa in out

xb

Building an And gate

And.hdl

Page 15: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 15

Hardware simulator (demonstrating Xor gate construction)

test scriptHDL

program

Page 16: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 16

Hardware simulator

HDL program

Page 17: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 17

HDL program

Hardware simulator

output file

Page 18: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 18

Project materials: www.nand2tetris.org

Project 1 web site

And.hdl , And.tst , And.cmp files

Page 19: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 19

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.

Page 20: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 20

a

b

c

and

and

orf(a,b,c)...

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

� Mass production

� Universal building blocks,unique topology

� Gates, neurons, atoms, …

Page 21: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 21

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.

)(),,( wmawmas +⋅=Truth table of the "suspect" function

Canonical form: wmawmawmawmas ++=),,(

Page 22: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 22

End notes: Canonical representation (cont.)

and

s

a

m

w

or

)(),,( wmawmas +⋅=

am

w

s

and

and

and

or

wmawmawmawmas ++=),,(

Page 23: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 23

a

b

c

and

and

orf(a,b,c)8 and terms

connected to thesame 3 inputs

...

(the on/off states of the fuses determine which gates participate in the computation)

single or termconnected to theoutputs of 8 and terms

active fuse

blown fuse

legend:

PLD implementation of f(a,b,c)= a b c + a b c_ _ _

End notes: Programmable Logic Device for 3-way functions

Page 24: lecture 01 Boolean logic - The Elements of Computing …nand2tetris.org/lectures/PDF/lecture 01 Boolean logic.pdf ·  · 2012-07-24Elements of Computing Systems, Nisan & Schocken,

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 1: Boolean Logic slide 24

a

b

c

and

and

orf(a,b,c)...

End notes: universal building blocks, unique topology