lec03 logic.ppt [相容模式]cyy/courses/introcs/18fall/... · 2018-09-03 · boolean algebra...

107
Boolean logic Introduction to Computer Yung-Yu Chuang with slides by Sedgewick & Wayne ( introcs.cs.princeton.edu ), Nisan & Schocken ( www.nand2tetris.org ) and Harris & Harris (DDCA)

Upload: others

Post on 17-Mar-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Boolean logic

Introduction to ComputerpYung-Yu Chuang

with slides by Sedgewick & Wayne (introcs.cs.princeton.edu), Nisan & Schocken (www.nand2tetris.org) and Harris & Harris (DDCA)

Page 2: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Boolean Algebra

Based on symbolic logic, designed by George Booley g , g y gBoolean variables take values as 0 or 1.Boolean expressions created from:p NOT, AND, OR

2

Page 3: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

NOT

Digital gate diagram for NOT:

X X’

Digital gate diagram for NOT:

NOT

3

Page 4: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

AND

XY XY

Digital gate diagram for AND:

ANDAND

4

Page 5: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

OR

X+Y

Di it l t di f ORDigital gate diagram for OR:

OR

5

Page 6: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Operator Precedence

Examples showing the order of operations: NOT > AND > ORNOT > AND > OR

Use parentheses to avoid ambiguity

6

Page 7: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Defining a function

Description: square of x minus 1Algebraic form : x2-1gEnumeration: x f(x)

1 01 02 33 84 155 24: :: :

7

Page 8: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Defining a function

Description: number of days of the x-th month of a non-leap year x f(x)p yAlgebraic form: ?Enumeration:

x f(x)1 312 283 314 305 316 307 318 319 3010 3111 30

8

11 3012 31

Page 9: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Table

Truth table. Systematic method to describe Boolean function.y One row for each possible input combination. N inputs 2N rows.

0 0

0 1

0

0

x y x y

0 1

1 0

1 1

0

0

1

AND truth table

9

Page 10: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Proving the equivalence of two functions

Prove that x2-1=(x+1)(x-1)

Using algebra: (you need to follow some rules)(x+1)(x-1) = x2+x-x-1= x2-1

Using enumeration: x (x+1)(x-1) x2-11 0 01 0 02 3 33 8 84 15 154 15 155 24 24: : :

10

: : :

Page 11: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Important laws

x + 1 = 1 x + 0 = x

x + y = y + x x + (y+z) = (x+y) + z

x + x = 1(y ) ( y)

x.1 = xx.0 = 0

x.y = y.xx.(y.z) = (x.y).z

x.x = 0

D M Lx.(y+z) = xy + xz

DeMorgan Law

x.y = x + y

11

Page 12: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplifying Boolean EquationsExample 1:

• Y = AB + AB

Chapter 2 <12> 

Page 13: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplifying Boolean EquationsExample 1:

• Y = AB + AB= B(A + A)= B(A + A)= B(1)= B

Chapter 2 <13> 

Page 14: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplifying Boolean Equations

Example 2:• Y = A(AB + ABC)

Chapter 2 <14> 

Page 15: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplifying Boolean Equations

Example 2:• Y = A(AB + ABC)

= A(AB(1 + C))= A(AB(1 + C))= A(AB(1))= A(AB)(AA)B= (AA)B

= AB

Chapter 2 <15> 

Page 16: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

DeMorgan’s Theorem

• Y = AB = A + B AB YB

AB YB

A• Y = A + B = A B AB Y

A YAB Y

Chapter 2 <16> 

Page 17: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing

• Backward:– Body changes– Adds bubbles to inputs

AB Y A

B Y

• Forward:– Body changes– Adds bubble to output

AB YA

B Y

Chapter 2 <17> 

Page 18: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing

• What is the Boolean expression for this circuit?

AB

YCDD

Chapter 2 <18> 

Page 19: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing

• What is the Boolean expression for this circuit?

AB

YCDD

Y = AB + CD

Chapter 2 <19> 

Page 20: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing Rules

• Begin at output, then work toward inputs• Push bubbles on final output back • Draw gates in a form so bubbles cancel

A

• Draw gates in a form so bubbles cancel

AB

C

DY

D

Chapter 2 <20> 

Page 21: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing ExampleAB

C YD

Chapter 2 <21> 

Page 22: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing ExampleAB

no outputbubble

C YD

Chapter 2 <22> 

Page 23: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing ExampleAB

no outputbubble

C YD

bubble oninput and outputA

B

C

DY

Chapter 2 <23> 

Page 24: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Bubble Pushing ExampleAB

no outputbubble

C YD

bubble oninput and outputA

B

C

DY

AB

no bubble oninput and output

C

DY

Chapter 2 <24> 

Y = ABC + D

Page 25: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Tables (1 of 3)

A Boolean function has one or more Boolean inputs and returns a single Boolean outputinputs, and returns a single Boolean output.A truth table shows all the inputs and outputs of a Boolean functionof a Boolean function

Example: X Y

25

Page 26: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Tables (2 of 3)

Example: X Y

26

Page 27: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Tables (3 of 3) S

When s=0, return x; otherwise, return y. mux

X

YZ

Example: (Y S) (X S) Two-input multiplexer

27

Page 28: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Table for Functions of 2 Variables

Truth table. 16 Boolean functions of 2 variables. every 4-bit value represents one

ZEROy xAND y XOR ORx ZEROy

0 0

0 1 0

0

0

0

1

x

0

0

AND

0

0

y

0

1

XOR

0

1

OR

0

1

x

0

Truth table for all Boolean functions of 2 variables

1 0 0

1 1 0

1

0

0

0

1

1

0

1

0

1

1

0

1

1

NORy

0 1

y'

1

x'

11

EQ

1 1

NAND

1

ONE

1

x

0 0 1

0 1 0

1 0 0

1

0

1

1

1

0

1

0

1

1

0

0

1

1

0

1

1

1

1

1

1

0

28

Truth table for all Boolean functions of 2 variables

1 1 0 0 011 1 0 1

Page 29: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

All Boolean functions of 2 variables

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

Page 30: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Truth Table for Functions of 3 Variables

Truth table. 16 Boolean functions of 2 variables. every 4-bit value represents one

8 bi l 256 Boolean functions of 3 variables. 2^(2^n) Boolean functions of n variables!

every 8-bit value represents one

every 2n-bit value represents one

ANDzyx OR MAJ ODD

0 0

0 1 0

1 0 0

00

0

0

0

1

1

0

0

0

0

1

1

1 1 00

0

0 1

01

1

0

0

1

1

1

1

0

1

0

1

00 1

1 0

1 1

1

1

1

0

0

1

1

1

1

1

1

1

0

0

1

30

some functions of 3 variables

Page 31: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Sum-of-Products

Sum-of-products. Systematic procedure for representing a Boolean function using AND, p g g ,OR, NOT. Form AND term for each 1 in Boolean function.

proves that { AND, OR, NOT }are universal

OR terms together.x'yzz xyz' xyzxy'zMAJyx x'yz + xy'z + xyz' + xyz

0

0

0

0

1

0

0

0

1

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

1

0

1

1

0

1

1

0

0

0

1

1

1

0

0

0

0

1

0

0

0

0

0

0

1

0

1

expressing MAJ using sum of products

1

1

0

1

1

1

1

1

0

0

0

0

1

0

0

1

1

1

31

expressing MAJ using sum-of-products

Page 32: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Universality of AND, OR, NOT

Fact. Any Boolean function can be expressed using AND, OR, NOT.g , , { AND, OR, NOT } are universal. Ex: XOR(x,y) = xy' + x'y. NOT xx'

x AND yx y

MeaningNotation

'

Expressing XOR Using AND, OR, NOT

' ' '''

x AND yx yx OR yx + y

x'y

0 1

1 1

x'y

0

1

x'y + xy'

0

1

xy'

0

0

y'

1

0

x XOR y

0

1

x

0

0

Exercise Show {AND NOT} {OR NOT} {NAND}

0 0

1 0

0

0

1

0

1

0

1

0

1

0

1

1

Exercise. Show {AND, NOT}, {OR, NOT}, {NAND}, {NOR} are universal.Hint. DeMorgan's law: (x'y')' = x + y.

32

H . D M g w ( y ) y.

Page 33: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

From Math to Real-World implementation

We can implement any Boolean function using NAND gates only.g y

We talk about abstract Boolean algebra g(logic) so far.

Is it possible to realize it in real world?

Th t h l d t it it hi d The technology needs to permit switching and conducting. It can be built using magnetic,

ti l bi l i l h d li d ti optical, biological, hydraulic and pneumatic mechanism.

33

Page 34: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Implementation of gates

Fluid switch (http://www.cs.princeton.edu/introcs/lectures/fluid-computer.swf

Page 35: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Digital Circuits

What is a digital system? Analog: signals vary continuously.g g y y Digital: signals are 0 or 1.

2.8V3.3V

0 1 0

Why digital systems? 0.0V0.5V

2.8V

y g y Accuracy and reliability. Staggeringly fast and cheap.

Basic abstractions.On off On, off.

Wire: propagates on/off value. Switch: controls propagation of on/off values

35

w p p g f / ff uthrough wires.

Page 36: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Wires

Wires. On (1): connected to power.p Off (0): not connected to power. If a wire is connected to a wire that is on, that

wire is also onwire is also on. Typical drawing convention: "flow" from top, left

to bottom, right.to bottom, r ght.

power

1

1pconnection 1

1

0

36

Page 37: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Controlled Switch

Controlled switch. [relay implementation] 3 connections: input, output, control.p p Magnetic force pulls on a contact that cuts

electrical flow.Control wire affects output wire but output does Control wire affects output wire, but output does not affect control; establishes forward flow of information over time.nformat on over t me.

X

37

X

Page 38: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Relay

38

Page 39: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Circuit Anatomy

39

Page 40: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Logic Gates: Fundamental Building Blocks

40

Page 41: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

NOT

41

Page 42: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

NOT

0

1

1

00

42

Page 43: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

OR

43

Page 44: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Series relays = NOR

0 10 0

01

101 1

00 00

44

Page 45: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

OR

45

Page 46: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

AND

46

Page 47: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

AND

47

Page 48: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Logic Gates: Fundamental Building Blocks

48

Page 49: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

What about parallel relays? =NAND

00

11

01

10

49

Page 50: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Can we implement AND/OR using parallel relays?

Now we know how to implement AND,OR and NOT. We can just use them as black boxes jwithout knowing how they were implemented. Principle of information hiding.p g

50

Page 51: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Multiway Gates

Multiway gates. OR: 1 if any input is 1; 0 otherwise.y p AND: 1 if all inputs are 1; 0 otherwise. Generalized: negate some inputs.

51

Page 52: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Multiway Gates

Multiway gates. OR: 1 if any input is 1; 0 otherwise.y p AND: 1 if all inputs are 1; 0 otherwise. Generalized: negate some inputs.

52

Page 53: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Multiway Gates

Multiway gates. Can also be built from 2-way gates (less efficient y g

but implementation independent) Example: build 4-way OR from 2-way ORs

53

Page 54: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. XOR.

54

Page 55: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. XOR.

55

Page 56: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. XOR.

56

Page 57: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gate logicInterface

Xora

out

Interface

Xorb

out

b t

I l t ti

0 0 00 1 11 0 1

a b out

Anda

Not

Implementation1 0 11 1 0

Not

Or out

Not

And Not

b

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

Page 58: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

ODD Parity Circuit

ODD(x, y, z). 1 if odd number of inputs are 1. p 0 otherwise.

Page 59: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

ODD Parity Circuit

ODD(x, y, z). 1 if odd number of inputs are 1. p 0 otherwise.

x'y'zz xy'z' xyzx'yz'ODDyx x'y'z + x'yz' + xy'z' + xyzy y yyy

0

1

1

0

1

0

0

0

1

0

0

0

0

1

0

0

0

1

0

0

0

0

0

0

y y y y

0

1

11

0

1

0

0

1

0

1

1

1

0

0

0

0

1

1

0

0

0

0

1

0

0

0

0

0

1

0

0

0

0

0

1

0

1

00

0

1

1

0

1

0

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

1

0

0

1

59

Expressing ODD using sum-of-products

Page 60: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

ODD Parity Circuit

ODD(x, y, z). 1 if odd number of inputs are 1. p 0 otherwise.

60

Page 61: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

ODD Parity Circuit

ODD(x, y, z). 1 if odd number of inputs are 1. p 0 otherwise.

61

Page 62: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Expressing a Boolean Function Using AND, OR, NOT

Ingredients. AND gates.g OR gates. NOT gates.

Wire Wire.

InstructionsInstructions. Step 1: represent input and output signals with

Boolean variables. Step 2: construct truth table to carry out

computation.Step 3: derive (simplified) Boolean expression using Step 3: derive (simplified) Boolean expression using sum-of products.

Step 4: transform Boolean expression into circuit.

62

p p

Page 63: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. Majority.

63

Page 64: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. Majority.

64

Page 65: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. Majority.

65

Page 66: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Translate Boolean Formula to Boolean Circuit

Sum-of-products. Majority.

66

Page 67: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplification Using Boolean Algebra

Every function can be written as sum-of-product

Many possible circuits for each Boolean function. Sum-of-products not necessarily optimal in:

– number of switches (space)– depth of circuit (time)

67

Page 68: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Boolean expression simplification

Karnaugh map

68

Page 69: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Karnaugh Maps (K‐Maps)

• Boolean expressions can be minimized by combining terms

• K‐maps minimize equations graphicallyK maps minimize equations graphically• PA + PA = P

Y AB YAB

B C0 0

A0 1

Y

C 00 01

0

11 10

1 0 0 0

C 00 01

0

11 10

ABC ABC ABC ABC

0 00 11 01 1

0000

0 01

11000

1 1 0 0 0 1 ABC ABC ABC ABC0 11 01 1

111

000

Chapter 2 <69> 

Page 70: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

K‐Map

• Circle 1’s in adjacent squaresj q• In Boolean expression, include only

literals whose true and complement form

Y

literals whose true and complement form are not in the circle

C 00 01

0

Y

11 10AB

0 0 01

B C0 00 11 0

A000

110

Y

0

1 1

0

0

0

0

0

0

11 01 1

00

0 00 11 0

111

00000 1 1 0 0 01 0

1 111

00

Y = AB

Chapter 2 <70> 

Page 71: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

3‐Input K‐Map

C 00 01

Y

11 10AB

C 00 01

0

11 10

ABC ABC ABC ABC

1 ABC ABC ABC ABC

B C Y0 0 0

Truth Table

C 00 01

Y

11 10ABA

0

K-Map

1

0 0 00 1 01 01 1 1

C 00 01

0

11 100000

0 0 011

0 0 00 1 01 0 01 1 1

1111

Chapter 2 <71> 

Page 72: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

3‐Input K‐Map

C 00 01

Y

11 10AB

C 00 01

0

11 10

ABC ABC ABC ABC

1 ABC ABC ABC ABC

B C Y0 0 0

Truth Table

C 00 01

Y

11 10ABA

0

K-Map

1 00 0 00 1 01 01 1 1

C 00 01

0

11 100000

0 0 01

1 1 01

0 0 00 1 01 0 01 1 1

1111

0 1 0 0

Chapter 2 <72> 

Y = AB + BC

Page 73: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

K‐Map Rules

• Every 1 must be circled at least once• Each circle must span a power of 2 (i.e. 1, 2, 4) squares in each direction4) squares in each direction

• Each circle must be as large as possible• A circle may wrap around the edges• A “don't care” (X) is circled only if it helps• A  don t care  (X) is circled only if it helps minimize the equation

Chapter 2 <73> 

Page 74: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

4‐Input K‐Map

01 1100 10AB

CD

Y

0

C D0 00 11 0

B000

1

1

YA000

01

001 01 1

00

0 00 11 0

111

11011

00000 01

11

1 01 1

11

11

00

0 00 11 0

000

111

11

1

10

1 01 1

00

0 00 11 0

111

11111

100001 0

1 111

11

00

Chapter 2 <74> 

Page 75: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

4‐Input K‐Map

01 1100 10AB

CD

Y

0

C D0 00 11 0

B000

1

1

YA000

1

0

0

1

0

0

1

101

001 01 1

00

0 00 11 0

111

11011

00000 0 1 0 101

1 1 0 011

1 01 1

11

11

00

0 00 11 0

000

111

11

1

1 1 0 110

1 01 1

00

0 00 11 0

111

11111

100001 0

1 111

11

00

Chapter 2 <75> 

Page 76: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

4‐Input K‐Map

01 1100 10AB

CD

Y

0

C D0 00 11 0

B000

1

1

YA000

1

0

0

1

0

0

1

101

001 01 1

00

0 00 11 0

111

11011

00000 0 1 0 101

1 1 0 011

1 01 1

11

11

00

0 00 11 0

000

111

11

1

1 1 0 110

1 01 1

00

0 00 11 0

111

11111

10000

Y = AC + ABD + ABC + BD1 01 1

11

11

00

Chapter 2 <76> 

Page 77: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

K‐Maps with Don’t Cares

0

C D0 00 11 0

B000

1

1

YA000

01 1100 10AB

CD

Y

1 01 1

00

0 00 11 0

111

110X1

00000 01

00

1 01 1

11

11

00

0 00 1

00

11 1

1

01

111 01 1

00

0 00 1

11

1111

XXXX

10

1 01 1

11

11

XX

Chapter 2 <77> 

Page 78: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

K‐Maps with Don’t Cares

0

C D0 00 11 0

B000

1

1

YA000

01 1100 10AB

CD

Y

1 01 1

00

0 00 11 0

111

110X1

00000

1

0

0

X

X

X

1

101

00

1 01 1

11

11

00

0 00 1

00

11 1

1

0 X X 101

1 1 X X111 01 1

00

0 00 1

11

1111

XXXX

1 1 X X10

1 01 1

11

11

XX

Chapter 2 <78> 

Page 79: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

K‐Maps with Don’t Cares

0

C D0 00 11 0

B000

1

1

YA000

01 1100 10AB

CD

Y

1 01 1

00

0 00 11 0

111

110X1

00000

1

0

0

X

X

X

1

101

00

1 01 1

11

11

00

0 00 1

00

11 1

1

0 X X 101

1 1 X X111 01 1

00

0 00 1

11

1111

XXXX

1 1 X X10

1 01 1

11

11

XX Y = A + BD + C

Chapter 2 <79> 

Page 80: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

ExampleExample

xy z

80

Page 81: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Simplification Using Boolean Algebra

Many possible circuits for each Boolean function. Sum-of-products not necessarily optimal in:p y p

– number of switches (space)– depth of circuit (time)

MAJ(x y z) = x'yz + xy'z + xyz' + xyz = xy + yz + xzMAJ(x, y, z) = x yz + xy z + xyz + xyz = xy + yz + xz.

81

Page 82: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Layers of Abstraction

Layers of abstraction. Build a circuit from wires and switches.

[implementation] Define a circuit by its inputs and outputs. [API]

To control complexity encapsulate circuits To control complexity, encapsulate circuits.[ADT]

82

Page 83: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Layers of Abstraction

Layers of abstraction. Build a circuit from wires and switches.

[implementation] Define a circuit by its inputs and outputs. [API]

To control complexity encapsulate circuits To control complexity, encapsulate circuits.[ADT]

83

Page 84: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Specification

St 1 id tif i t d t t Step 1: identify input and output Step 2: construct truth table Step 3: derive (simplified) Boolean expression using Step 3: derive (simplified) Boolean expression using

sum-of products. Step 4: transform Boolean expression into

/ l circuit/implement it using HDL.

Y u uld like t test the ate bef re packa inYou would like to test the gate before packaging.

Page 85: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

HDL

Page 86: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Example: Building an And gate

a b out

And.cmp Contract:

When running your

outa

bAnd

0 0 00 1 0 1 0 0 1 1 1

.hdl on our .tst, your .out should be the same as

our .cmp.

load And.hdl,

And.tstAnd.hdl

CHIP And output-file And.out,compare-to And.cmp,output-list a b out;set a 0,set b 0,eval,output;

{ IN a, b;OUT out;// implementation missing

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

}

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

Page 87: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Building an And gate

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

outa

bAnd

b

CHIP And

And.hdl

{ IN a, b;OUT out;// implementation missing

}}

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

Page 88: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Building an And gate

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

outa

bb

CHIP And

And.hdl

{ IN a, b;OUT out;// implementation missing

}}

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

Page 89: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Building an And gate

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

outNot

aoutNand

a in outxb

bb

CHIP And

And.hdl

{ IN a, b;OUT out;// implementation missing

}}

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

Page 90: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Building an And gate

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

outNOT

aoutNAND

a in outx

bb

b

CHIP And

And.hdl

{ IN a, b;OUT out;Nand(a = a,

b = b,,out = x);

Not(in = x, out = out)}

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

Page 91: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Hardware simulator (demonstrating Xor gate construction)

test scriptHDL

programprogram

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

Page 92: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Hardware simulator

HDL programprogram

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

Page 93: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Hardware simulator

HDL programprogram

output file

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

Page 94: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Project materials: www.nand2tetris.org

Project 1 web site

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

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

Page 95: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

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)Do Project 0 (optional)

You’re in business.

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

Page 96: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Basic Gates)

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

Page 97: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1

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

Page 98: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Multi-bit version)

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

Page 99: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Multi-way version)

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

Page 100: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Multi-way version)

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

Page 101: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Multi-way version)

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

Page 102: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Gates for project #1 (Multi-way version)

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

Page 103: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

Perspective

Each Boolean function has a canonical representation

The canonical representation is expressed in terms of And Not Or 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

b and

consisting of Nand gates only

Mass productionc

orf(a,b,c)... Universal building blocks,

unique topologyand

unique topology

Gates, neurons, atoms, …

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

Page 104: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

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 p pfor whom the proposition Not(a) And (m Or w) is true.

)(),,( wmawmas Truth table of the "suspect" function

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

Canonical form: wmawmawmawmas ),,(

Page 105: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

End notes: Canonical representation (cont.)

)(),,( wmawmas

sa

or

andm

wwmawmawmawmas ),,(

amw

andw

sand or

and

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

Page 106: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

End notes: Programmable Logic Device for 3-way functions

a

b and active fuse

blown fuse

legend:

c

blown fuse

orf(a,b,c)8 and terms

connected to thesame 3 inputs

...

single or term

andsingle or termconnected to theoutputs of 8 and terms

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

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

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

Page 107: lec03 logic.ppt [相容模式]cyy/courses/introCS/18fall/... · 2018-09-03 · Boolean Algebra Based on syyg,gygmbolic logic, designed by George Boole Boolean variables take values

End notes: Programmable Logic Device for 3-way functions

Two-level logic: ANDs followed by ORs

Example: Y = ABC + ABC + ABCBA C

A B C

minterm: ABC

minterm: ABC

minterm: ABC

Y

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