oolean lgebra oolean logic ased on symbolic logic, designed by George oole oolean variables take values as or. oolean expressions created from: NOT, ND, OR Introduction to Computer ung-u Chuang with slides by Sedgewick & Wayne (introcs.cs.princeton.edu), Nisan & Schocken (www.nand2tetris.org) and Harris & Harris (DDC) 2 NOT ND X X X X Digital gate diagram for NOT: Digital gate diagram for ND: NOT ND 3 4
OR Operator Precedence X+ Examples showing the order of operations: NOT > ND > OR Digital it gate diagram for OR: OR Use parentheses to avoid ambiguity 5 6 Defining a function Defining a function Description: square of x minus lgebraic form : x 2 - Enumeration: x f(x) 2 3 3 8 4 5 5 24 : : 7 Description: number of days of the x-th month of a non-leap pyear x f(x) lgebraic form:? 3 Enumeration: 2 28 3 3 4 3 5 3 6 3 7 3 8 3 9 3 3 3 2 3 8
Truth Table Truth table. Systematic method to describe oolean function. One row for each possible input combination. N inputs 2 N rows. Proving the equivalence of two functions Prove that x 2 -=(x+)(x-) Using algebra: (you need to follow some rules) (x+)(x-) = x 2 +x-x-= x 2 - x y x y ND truth table Using enumeration: x (x+)(x-) x 2-2 3 3 3 8 8 4 5 5 5 24 24 : : : 9 x + = x + = x x + x = x. = x x. = x.x = DeMorgan Law x.y = x + y Important laws x + y = y + x x + (y+z) = (x+y) + z x.y = y.x x.(y.z) = (x.y).z x.(y+z) = xy + xz Simplifying oolean Equations Example : = + Chapter 2 <2>
Simplifying oolean Equations Example : = + = ( + ) = () = Simplifying oolean Equations Example 2: = ( + C) Chapter 2 <3> Chapter 2 <4> Simplifying oolean Equations DeMorgan s Theorem Example 2: = ( + C) = (( + C)) = (()) = () = () = = = + = + = Chapter 2 <5> Chapter 2 <6>
ubble Pushing ubble Pushing ackward: ody changes dds bubbles to inputs What is the oolean expression for this circuit? Forward: ody changes dds bubble to output C D Chapter 2 <7> Chapter 2 <8> ubble Pushing What is the oolean expression for this circuit? C D = + CD ubble Pushing Rules egin at output, then work toward inputs Push bubbles on final output back Draw gates in a form so bubbles cancel C D Chapter 2 <9> Chapter 2 <2>
ubble Pushing Example ubble Pushing Example no output bubble C D C D Chapter 2 <2> Chapter 2 <22> ubble Pushing Example ubble Pushing Example no output bubble no output bubble C D C D bubble on input and output bubble on input and output C D C D no bubble on input and output C D = C + D Chapter 2 <23> Chapter 2 <24>
Truth Tables ( of 3) oolean function has one or more oolean inputs, and returns a single oolean output. truth table shows all the inputs and outputs of a oolean function Example: X Truth Tables (2 of 3) Example: X 25 26 Truth Tables (3 of 3) When s=, return x; otherwise, return y. Example: ( S) (X S) X S mux Z Two-input multiplexer Truth Table for Functions of 2 Variables Truth table. 6 oolean functions of 2 variables. every 4-bit value represents one x y ZERO ND x y XOR OR Truth table for all oolean functions of 2 variables x y NOR EQ y' x' NND Truth table for all oolean functions of 2 variables ONE 27 28
ll oolean functions of 2 variables Truth Table for Functions of 3 Variables Truth table. 6 oolean functions of 2 variables. 256 oolean functions of 3 variables. 2^(2^n) oolean functions of n variables! every 4-bit value represents one every 8-bit value represents one every 2 n -bit value represents one x y z ND OR MJ ODD some functions of 3 variables Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 29 3 Sum-of-Products Sum-of-products. Systematic procedure for representing a oolean function using ND, OR, NOT. Form ND term for each in oolean function. OR terms together. x y z MJ x'yz xy'z xyz' xyz expressing MJ using sum-of-products proves that { ND, OR, NOT } are universal x'yz + xy'z + xyz' + xyz Universality of ND, OR, NOT Fact. ny oolean function can be expressed using ND, OR, NOT. { ND, OR, NOT } are universal. Notation Ex: XOR(x,y) = xy' + x'y. x' NOT x x y x + y Expressing XOR Using ND, OR, NOT x y x' y' x'y xy' x'y + xy' x XOR y Exercise. Show {ND, NOT}, {OR, NOT}, {NND}, {NOR} are universal. Hint. DeMorgan's law: (x'y')' = x + y. Meaning x ND y x OR y 3 32
From Math to Real-World implementation We can implement any oolean function using NND gates only. Implementation of gates Fluid switch (http://www.cs.princeton.edu/introcs/lectures/fluid-computer.swf We talk about abstract oolean algebra (logic) so far. Is it possible to realize it in real world? The technology needs to permit switching and conducting. It can be built using magnetic, optical, biological, i l hydraulic and pneumatic mechanism. 33 Digital Circuits What is a digital system? nalog: signals vary continuously. Digital: signals are or. 3.3V 2.8V.5V Why digital systems?.v ccuracy and reliability. Staggeringly fast and cheap. asic abstractions. On, off. Wire: propagates on/off value. Switch: controls propagation p of on/off values through wires. Wires Wires. On (): connected to power. Off (): not connected to power. If a wire is connected to a wire that is on, that wire is also on. Typical drawing convention: "flow" from top, left to bottom, right. power connection 35 36
Controlled Switch Relay Controlled switch. [relay implementation] 3 connections: input, output, control. Magnetic force pulls on a contact that cuts electrical flow. Control wire affects output wire, but output does not affect control; establishes forward flow of information on over time. X X 37 38 Circuit natomy Logic Gates: Fundamental uilding locks 39 4
NOT NOT 4 42 OR Series relays = NOR 43 44
OR ND 45 46 ND Logic Gates: Fundamental uilding locks 47 48
What about parallel relays? =NND Can we implement ND/OR using parallel relays? Now we know how to implement ND,OR and NOT. We can just use them as black boxes without knowing how they were implemented. Principle of information hiding. 49 5 Multiway Gates Multiway gates. OR: if any input is ; otherwise. ND: if all inputs are ; otherwise. Generalized: negate some inputs. Multiway Gates Multiway gates. OR: if any input is ; otherwise. ND: if all inputs are ; otherwise. Generalized: negate some inputs. 5 52
Multiway Gates Multiway gates. Can also be built from 2-way ygates (less efficient but implementation independent) Example: build 4-way OR from 2-way ORs Translate oolean Formula to oolean Circuit Sum-of-products. XOR. 53 54 Translate oolean Formula to oolean Circuit Sum-of-products. XOR. Translate oolean Formula to oolean Circuit Sum-of-products. XOR. 55 56
Gate logic ODD Parity Circuit Interface a Xor b out ODD(x, y, z). if odd number of inputs are. otherwise. a b out a Implementation ti nd Not Or out b Not nd Xor(a,b) = Or(nd(a,Not(b)),nd(Not(a),b))) (b)) d(n ( ) b))) ODD Parity Circuit ODD(x, y, z). if odd number of inputs are. otherwise. ODD Parity Circuit ODD(x, y, z). if odd number of inputs are. otherwise. x y z ODD x'y'z x'yz' xy'z' xyz x'y'z+x'yz' y + xy'z' + xyz Expressing ODD using sum-of-products 59 6
ODD Parity Circuit ODD(x, y, z). if odd number of inputs are. otherwise. Expressing a oolean Function Using ND, OR, NOT Ingredients. ND gates. OR gates. NOT gates. Wire. Instructions. Step : represent input and output signals with oolean variables. Step 2: construct truth table to carry out computation. Step 3: derive (simplified) oolean expression using sum-of products. Step 4: transform oolean expression into circuit. 6 62 Translate oolean Formula to oolean Circuit Sum-of-products. Majority. Translate oolean Formula to oolean Circuit Sum-of-products. Majority. 63 64
Translate oolean Formula to oolean Circuit Sum-of-products. Majority. Translate oolean Formula to oolean Circuit Sum-of-products. Majority. 65 66 Simplification Using oolean lgebra Every function can be written as sum-of-product oolean expression simplification Karnaugh map Many possible circuits for each oolean function. Sum-of-products not necessarily optimal in: number of switches (space) depth of circuit (time) 67 68
Karnaugh Maps (K Maps) oolean expressions can be minimized by combining terms K maps minimize equations graphically P + P = P C C C C C C C C C C C K Map Circle s in adjacent squares In oolean expression, include only literals whose true and complement form are not in the circle C C = Chapter 2 <69> Chapter 2 <7> 3 Input K Map 3 Input K Map C C C C C C C C C C C C C C C C C C Truth Table C K-Map C Chapter 2 <7> Truth Table K-Map C C = + C Chapter 2 <72>
K Map Rules Every must be circled at least once Each circle must span a power of 2 (i.e., 2, 4) squares in each direction Each circle must be as large as possible circle may wrap around the edges don't care (X) is circled only if it helps minimize the equation 4 Input K Map C D CD Chapter 2 <73> Chapter 2 <74> 4 Input K Map 4 Input K Map C D CD C D CD = C + D + C + D Chapter 2 <75> Chapter 2 <76>
K Maps with Don t Cares K Maps with Don t Cares C D CD X X X X X X X C D X X X X X X X CD X X X X X X X Chapter 2 <77> Chapter 2 <78> K Maps with Don t Cares Example C D X X X X X X X CD X X X X X X X = + D + C xy z Chapter 2 <79> 8
Simplification Using oolean lgebra Many possible circuits for each oolean function. Sum-of-products not necessarily optimal in: number of switches (space) depth of circuit (time) MJ(x, y, z) = x'yz + xy'z + xyz' + xyz = xy + yz + xz. Layers of abstraction. Layers of bstraction uild a circuit from wires and switches. [implementation] Define a circuit by its inputs and outputs. [PI] To control complexity, encapsulate circuits. [DT] 8 82 Layers of bstraction Layers of abstraction. uild a circuit from wires and switches. [implementation] Define a circuit by its inputs and outputs. [PI] To control complexity, encapsulate circuits. [DT] Specification Step : identify input and output t Step 2: construct truth table Step 3: derive (simplified) oolean expression using sum-of products. Step 4: transform oolean expression into circuit/implement it using HDL. ou would like to test the gate before packaging. 83
HDL Example: uilding an nd gate a b nd out nd.cmp a b out Contract: When running your.hdl on our.tst, your.out should be the same as our.cmp. nd.hdl nd.tst CHIP nd load nd.hdl, { IN a, b; output-file nd.out, OUT out; compare-to nd.cmp, // implementation missing output-list a b out; } set a,set b,eval,output;, set a,set b,eval,output; set a,set b,eval,output; set a, set b, eval, output; Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 86 uilding an nd gate uilding an nd gate Interface: nd(a,b) = exactly when a=b= Implementation: nd(a,b) = Not(Nand(a,b)) a b nd out a b out nd.hdl CHIP nd { IN a, b; OUT out; // implementation missing } nd.hdl CHIP nd { IN a, b; OUT out; // implementation missing } Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 87 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 88
uilding an nd gate uilding an nd gate Implementation: nd(a,b) = Not(Nand(a,b)) Implementation: nd(a,b) = Not(Nand(a,b)) a b a b Nand out x in Not out out a a out NND x in NOT out b b out nd.hdl nd.hdl CHIP nd { IN a, b; OUT out; // implementation missing } CHIP nd { 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 : oolean Logic slide 89 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 9 Hardware simulator (demonstrating Xor gate construction) Hardware simulator HDL program test script HDL program Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 9 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 92
Hardware simulator Project materials: www.nand2tetris.org Project web site HDL program output file nd.hdl, nd.tst, nd.cmp files Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 93 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 94 Project tips Gates for project # (asic Gates) Read the Introduction + Chapter of the book Download the book s software suite Go through the hardware simulator tutorial Do Project (optional) ou re in business. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 95 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 96
Gates for project # Gates for project # (Multi-bit version) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 97 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 98 Gates for project # (Multi-way version) Gates for project # (Multi-way version) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 99 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide
Gates for project # (Multi-way version) Gates for project # (Multi-way version) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 2 Perspective Each oolean function has a canonical representation The canonical representation is expressed in terms of nd, Not, Or 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) nd (m Or w) is true. nd, Not, Or can be expressed in terms of Nand alone Ergo, every oolean function can be realized by a standard PLD consisting of Nand gates only Truth table of the "suspect" function s( a, m, w) a ( m w) Mass production a b c and Universal building blocks, unique topology. or f(a,b,c) Gates, neurons, atoms, and Canonical form: s( a, m, w) a m w a mw a mw Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 3 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 4
End notes: Canonical representation (cont.) End notes: Programmable Logic Device for 3-way functions a m w s( a, m, w) a ( m w) or and s s( a, m, w) a m w a mw a mw a b c 8 and terms connected to the same 3 inputs and. legend: active fuse blown fuse or f(a,b,c) a m w and and or s and single or term connected to the outputs of 8 and terms and _ 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 : oolean Logic slide 5 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 6 End notes: Programmable Logic Device for 3-way functions Two-level logic: NDs followed by ORs Example: = C + C + C C C minterm: C minterm: C minterm: C Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter : oolean Logic slide 7