<<

SCIENCE COMPUTER SCIENCE SEDGEWICK/WAYNE SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY PART II: ALGORITHMS, MACHINES, and THEORY

Computer Science 19. Combinational Circuits 19. Combinational •Building blocks Computer • Science Circuits •Digital circuits An Interdisciplinary Approach •Adder circuit

R O B E R T S E D G E W I C K Section 6.1 KEVIN WAYNE •Arithmetic/logic unit

http://introcs.cs.princeton.edu

CS.19.A.Circuits.Basics

Context Wires

Q. What is a combinational circuit? Wires propagate on/off values • ON (1): connected to power. A. A digital circuit (all signals are 0 or 1) with no feedback (no loops). • OFF (0): not connected to power. analog circuit: signals vary continuously sequential circuit: loops allowed (stay tuned) • Any wire connected to a wire that is ON is also ON. • Drawing convention: "flow" from top, left to bottom, right. Q. Why combinational circuits?

A. Accurate, reliable, general purpose, fast, cheap. thick wires are ON

1

Basic abstractions power connection • On and off. 1 • Wire: propagates on/off value. • Switch: controls propagation of on/off values through wires. 1

0

thin wires are OFF Applications. Smartphone, tablet, game controller, antilock brakes, microprocessor, …

3 4 Controlled Switch Controlled Switch

Switches control propagation of on/off values through wires. Switches control propagation of on/off values through wires. • Simplest case involves two connections: control (input) and output. • General case involves three connections: control input, data input and output. • control OFF: output ON • control OFF: output is connected to input • control ON: output OFF • control ON: output is disconnected from input

control input OFF control input ON

control input OFF control input ON data input OFF output OFF data input OFF output OFF

control input OFF control input ON

output ON output OFF

data input ON output ON data input ON output OFF

Idealized model of pass transistors found in real integrated circuits. 5 6

Controlled switch: example implementation First level of abstraction

A relay is a physical device that controls a switch with a magnet Switches and wires model provides separation between • 3 connections: input, output, control. physical world and logical world. • Magnetic force pulls on a contact that cuts electrical flow. • We assume that switches operate as specified. • That is the only assumption. • Physical realization of switch is irrelevant to design.

schematic control of control on Physical realization dictates performance magnet on • Size. magnet pulls (off) connection contact up • Speed. contact broken • Power.

spring New technology immediately gives new computer.

Better switch? Better computer. all built with Basis of Moore's law. "switches and wires"

7 8 Switches and wires: a first level of abstraction Switches and wires: a first level of abstraction

technology switch VLSI = Very Large Scale Integration technology “information” switch relay Technology (1940s) Deposit materials on substrate.

pneumatic air pressure Key properties vacuum tube Lines are wires. Certain crossing lines are controlled switches.

water Key challenge in physical world fluid transistor pressure Fabricating physical circuits with billions of wires and controlled switches “pass transistor” in integrated circuit Key challenge in “abstract” world relay electric Understanding behavior of circuits with (now) potential atom-thick billions of wires and controlled switches transistor Bottom line. Circuit = Drawing (!) Amusing attempts that do not scale but prove the point Real-world examples that prove the point 9 10

Circuit anatomy COMPUTER SCIENCE SEDGEWICK/WAYNE

Image sources

http://upload.wikimedia.org/wikipedia/commons/f/f4/1965_c1960s_vacuum_tube%2C_7025A-12AX7A%2C_QC%2C_Philips%2C_Great_Britain.jpg http://electronics.howstuffworks.com/relay.htm

Need more levels of abstraction to understand circuit behavior

11 CS.19.A.Circuits.Basics COMPUTER SCIENCE Boolean algebra SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY Developed by George Boole in 1840s to study logic problems • Variables represent true or (1 or 0 for short). • Basic operations are AND, OR, and NOT (see table below). Widely used in mathematics, logic and computer science.

George Boole circuit design 18151864 operation Java notation logic notation 19. Combinational Circuits (this lecture) AND x && y x ⋀ y xy various notations •Building blocks OR x || y x ⋁ y x + y in common use •Boolean algebra NOT ! x ¬ x x'

•Digital circuits DeMorgan's Laws •Adder circuit (xy)' = (x' + y' ) Example: (stay tuned for proof) •Arithmetic/logic unit (x + y)' = x'y'

Relevance to circuits. Basis for next level of abstraction. Copyright 2004, Sidney Harris http://www.sciencecartoonsplus.com CS.19.B.Circuits.Algebra 14

Truth tables proofs

A truth table is a systematic way to define a Truth tables are convenient for establishing identities in Boolean logic • One row for each possible set of arguments. • One row for each possibility. • Each row gives the function value for the specified arguments. • Identity established if columns match. • N inputs: 2N rows needed.

Proofs of DeMorgan's laws NOR NOR (xy) (x + x y xy x y x' y' x' + y' x y x + y x y x' y' x'y' x x' x y xy x y x + y x y NOR x y XOR ' y)' 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 0 1 0 1 1 0 0 1 1 0 0 NOT 1 0 0 1 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 0 0 0 1 1 1 0 1 1 0 0 0

AND OR NOR XOR (xy)' = (x' + y' ) (x + y)' = x'y'

15 16 All Boolean functions of two variables Functions of three and more variables

Q. How many Boolean functions of two variables? Q. How many Boolean functions of three variables?

A. 16 (all possibilities for the 4 bits in the truth table column). A. 256 (all possibilities for the 8 bits in the truth table column).

all extend to N variables x y z AND OR NOR MAJ ODD Examples Truth tables for all Boolean functions of 2 variables AND logical AND 0 iff any inputs is 0 (1 iff all inputs 1) 0 0 0 0 0 1 0 0 x y ZERO AND x y XOR OR NOR EQ ¬y ¬x NAND ONE OR logical OR 1 iff any input is 1 (0 iff all inputs 0) 0 0 1 0 1 0 0 1 NOR logical NOR 0 iff any input is 1 (1 iff all inputs 0) 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 1 MAJ majority 1 iff more inputs are 1 than 0 0 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 ODD odd parity 1 iff an odd number of inputs are 1 0 1 1 0 1 0 1 0 1 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 Q. How many Boolean functions of N variables? 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 N number of Boolean functions with N variables 2 24 = 16 1 1 0 0 1 0 1 0 3 28 = 256 (2N ) 1 1 1 1 1 0 1 1 A. 2 4 216 = 65,536 5 232 = 4,294,967,296 Some Boolean functions of 3 variables 6 264 = 18,446,744,073,709,551,616 17 18

Universality of AND, OR and NOT COMPUTER SCIENCE SEDGEWICK/WAYNE Every Boolean function can be represented as a sum of products • Form an AND term for each 1 in Boolean function. • OR all the terms together. Image sources http://en.wikipedia.org/wiki/George_Boole#/media/File:George_Boole_color.jpg

x'yz + xy'z + xyz' + xyz = MAJ x y z MAJ x'yz xy'z xyz' xyz

0 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0

0 1 1 1 1 0 0 0 1 Def. A set of operations is universal if 1 0 0 0 0 0 0 0 0 every Boolean function can be expressed using just those operations. 1 0 1 1 0 1 0 0 1

1 1 0 1 0 0 1 0 1 Fact. { AND, OR, NOT } is universal. 1 1 1 1 0 0 0 1 1

Expressing MAJ as a sum of products 19 CS.19.B.Circuits.Algebra COMPUTER SCIENCE A basis for digital devices SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY Claude Shannon connected circuit design with Boolean algebra in 1937.

Claude Shannon “ Possibly the most important, and also the 19162001 19. Combinational Circuits most famous, master's thesis of the [20th] − Howard Gardner •Building blocks •Boolean algebra •Digital circuits Key idea. Can use Boolean algebra to •Adder circuit systematically analyze circuit behavior. •Arithmetic/logic unit

CS.19.C.Circuits.Digital 22

A second level of abstraction: logic gates Multiway OR gates

boolean classic notation truth table our symbol under the cover proof OR gates with multiple inputs. function symbol circuit (gate) • 1 if any input is 1. x x' x x' • 0 if all inputs are 0. NOT x' 0 1 x ¬ x' 1 iff x is 0 1 0

classic symbol our symbol x y NOR x y x y under the cover u v w x y z 0 0 1 1 iff x and y u v w x y z u v w x y z 0 1 0 (x+y)' (x+y)' NOR (x + y)' (x+y)' NOR u+v+w+x+y+z u+v+w+x+y+z 1 0 0 are both 0 OR 1 1 0 u+v+w+x+y+z 0 if inputs are 000000; 1 if any input is 1

x y OR x y x y examples 0 0 0 ¬ x+y x+y NOR OR x + y 0 1 1 OR 011000 1 0 1 x+y = ((x + y)')' 1 1 1 1 Multiway OR gates are oriented vertically in our circuits. Learn to recognize them! 000001 x y AND x y x y 1 0 0 0 ¬ ¬ AND xy 0 1 0 AND 000000 1 0 0 xy xy NOR 1 1 1 xy = (x' + y')' 0 23 24 Multiway generalized AND gates Pop quiz on generalized AND gates

Multiway generalized AND gates. xyz Q. Give the Boolean function computed by these gates. • 1 for exactly 1 set of input values. • 0 for all other sets of input values. Q. Also give the inputs for which the output is 1. xyz

gate function inputs that output 1 another set of inputs u v w x y z xyz u v w x y z 111111 011000

AND uvwxyz 1 0 xyz

u v w x y z 011001 xyz u v w x y z 011000

1 0 generalized u'vwx'y'z xyz

xyz u v w x y z 000000 010100 NOR u'v'w'x'y'z' 1 0 xyz same as (u + v + w + x + y + z)'

Might also call these "generalized NOR gates"; we consistently use AND. 25 26

Pop quiz on generalized AND gates A useful combinational circuit: decoder

110 = 6 xyz Q. Give the Boolean function computed by these gates. Decoder x'y'z' Example: 3-to-8 decoder 000 • n input lines (address). xyz Q. Also give the inputs for which the output is 1. • 2n outputs. 0 x'y'z 001 • Addressed output is 1. u v w x y z xyz • All other outputs are 0. 1 x'yz' 010 uv'wxy'z 101101 xyz 2

x'yz 011 u v w x y z 3 xyz outputs 0-5 and 7 are 0 u'vwx'y'z 011001 xy'z' 100 4 xyz

xy'z 101 5 xyz Get the idea? If not, replay this slide, like flash cards. output 6 6 is 1 xyz' 110 xyz Note. From now on, we will not label these gates. 7 xyz 111

27 28 A useful combinational circuit: decoder Another useful combinational circuit: demultiplexer (demux)

110 = 6 101 = 5 x Decoder Demultiplexer Example: 3-to-8 decoder Example: 3-to-8 demux • n input lines (address). • n address inputs. • 2n outputs. • 1 data input with value x. 0 • Addressed output is 1. • 2n outputs. • All other outputs are 0. • Addressed output has value x. 1 • All other outputs are 0. 2

Implementation 3 n outputs 0-5 outputs 0-4 • Use all 2 generalized AND gates with n inputs. and 7 are 0 and 6-7 are 0 • Only one of them matches the input address. 4

output 5 5 has value x

Application (next lecture) output 6 is 1 6 • Select a memory word for read/write. • [Use address bits of instruction from IR.] 7

29 30

Another useful combinational circuit: demultiplexer (demux) Decoder/demux 101 = 5 x 101 = 5 x Demultiplexer Decoder/demux Example: 3-to-8 demux Example: 3-to-8 decoder/demux • n address inputs. • n address inputs. 0 • 1 data input with value x. • 1 data input with value x. 0 • 2n outputs. • 2n output pairs. 1 • Addressed output has value x. • Addressed output pair has 1

• All other outputs are 0. 2 value (1, x ). 2 • All other outputs are 0.

3 Implementation 3 outputs 0-4 output pairs 0-4 • Start with decoder. and 6-7 are 0 and 6-7 are (0, 0) 4 • Add AND x to each gate. 4

5 output 5 5 output pair 5 has value x has value (1, x)

6 Application (next lecture) 6 • Turn on control wires to implement instructions. 7 • [Use opcode bits of instruction in IR.] 7

31 32 Decoder/demux Creating a digital circuit that computes a boolean function: majority

101 = 5 x Decoder/demux Use the truth table Example: 3-to-8 decoder/demux • n address inputs. • Identify rows where the function is 1. • 1 data input with value x. • Use a generalized AND gate for each. • 2n output pairs. • OR the results together. 110 • Addressed output pair has multiway value (1, x ). Example 1: Majority function xyz OR gate • All other outputs are 0. x y z MAJ MAJ 0 0 0 0 output pairs 0-4 0 0 1 0 Implementation and 6-7 are (0, 0) gate • Add decoder output to demux. 0 1 0 0 term 0 1 1 1 x'yz output pair 5 has value (1, x) 1 0 0 0 Application (next lecture) 1 0 1 1 xy'z • Access and control write of memory word 1 1 0 1 xyz' • [Use addr bits of instruction in IR.] 1 1 1 1 xyz MAJ MAJ is 1 MAJ = x'yz + xy'z + xyz' + xyz 33 majority circuit example 34

Creating a digital circuit that computes a boolean function: odd parity Combinational circuit design: Summary

Use the truth table Problem: Design a circuit that computes a given boolean function. x y z MAJ x y z ODD • Identify rows where the function is 1. 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 • Use a generalized AND gate for each. Ingredients 0 1 0 0 0 1 0 1 • OR the results together. • OR gates. 0 1 1 1 0 1 1 0 1 0 0 0 1 0 0 1 110 NOT gates. multiway • 1 0 1 1 1 0 1 0 Example 2: Odd parity function xyz OR gate use to make generalized AND gates • NOR gates. 1 1 0 1 1 1 0 0 x y z ODD ODD • Wire. 1 1 1 1 1 1 1 1 0 0 0 0 term gate xyz xyz 0 0 1 1 x'y'z Method MAJ ODD • Step 1: Represent input and output with Boolean variables. 0 1 0 1 x'yz' • Step 2: Construct truth table to define the function. 0 1 1 0 • Step 3: Identify rows where the function is 1. 1 0 0 1 xy'z' • Step 4: Use a generalized AND for each and OR the results. 1 0 1 0 1 1 0 0 Bottom line (profound idea): Yields a circuit for ANY function. 1 1 1 1 xyz Caveat: Circuit might be huge (stay tuned). ODD ODD is 0 ODD = x'y'z + x'yz' + xy'z' + xyz MAJ ODD odd parity circuit example 35 36 Pop quiz on combinational circuit design Pop quiz on combinational circuit design

Q. Design a circuit to implement XOR(x, y). Q. Design a circuit to implement XOR(x, y).

A. Use the truth table • Identify rows where the function is 1. • Use a generalized AND gate for each. • OR the results together.

XOR function circuit interface x y XOR xy 0 0 0 term gate 0 1 1 x'y 1 0 1 xy' 1 1 0

XOR XOR = x'y + xy' XOR

37 38

Encapsulation COMPUTER SCIENCE SEDGEWICK/WAYNE Encapsulation in hardware design mirrors familiar principles in software design • Building a circuit from wires and switches is the implementation. • Define a circuit by its inputs, controls, and outputs is the API. Image sources • We control complexity by encapsulating circuits as we do with ADTs. http://en.wikipedia.org/wiki/Claude_Shannon#/media/File:Claude_Elwood_Shannon_(1916-2001).jpg

NOT

AND

ODD DECODER DEMULTIPLEXER DECODER/DEMUX

MAJ OR XOR 39 CS.19.C.Circuits.Digital COMPUTER SCIENCE Let's make an adder circuit! SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY Adder • Compute z = x + y for n-bit binary integers.

• 2n inputs. 0 0 0 1 0 1 1 23 • n outputs. 0 0 1 1 0 0 0 + 49 19. Combinational Circuits • Ignore overflow. ADD

•Building blocks Example: 8-bit adder

•Boolean algebra carry out •Digital circuits 0 0 1 1 0 1 1 1 0 •Adder circuit 0 0 0 1 0 1 1 1 •Arithmetic/logic unit + 0 0 1 1 0 0 0 1 0 1 0 0 1 0 0 0

0 1 0 0 1 0 0 = 72 CS.19.D.Circuits.Adder 42

Let's make an adder circuit! Let's make an adder circuit!

Adder Goal: z = x + y for 8-bit integers. c8 c7 c6 c5 c4 c3 c2 c1 0 x7 x6 x5 x4 x3 x2 x1 x0 • Compute z = x + y for Strawman solution: Build truth tables for each output bit. n-bit binary integers. + y7 y6 y5 y4 y3 y2 y1 y0 z7 z6 z5 z4 z3 z2 z1 z0 • 2n inputs. x7 y7 x6 y6 x5 y5 x4 y4 x3 y3 x2 y2 x1 y1 x0 y0 • n outputs. x7 x6 x5 x4 x3 x2 x1 x0 y7 y6 y5 y4 y3 y2 y1 y0 c4 z7 z6 z5 z4 z3 z2 z1 z0 • Ignore overflow. ADD 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 8-bit adder Example: 8-bit adder truth table 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 1 216 = 65536 rows!

...... carry out 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 c8 c7 c6 c5 c4 c3 c2 c1 0

x7 x6 x5 x4 x3 x2 x1 x0

+ y7 y6 y5 y4 y3 y2 y1 y0 Q. Not convinced this a bad idea? z7 z6 z5 z4 z3 z2 z1 z0 A. 128-bit adder: 2256 rows >> # electrons in universe! z7 z6 z5 z4 z3 z2 z1 z0 43 44 Let's make an adder circuit! Let's make an adder circuit!

Goal: z = x + y for 8-bit integers. c8 c7 c6 c5 c4 c3 c2 c1 0 Goal: z = x + y for 4-bit integers. x7 x6 x5 x4 x3 x2 x1 x0 Do one bit at a time. A surprise! + y7 y6 y5 y4 y3 y2 y1 y0 Build truth table for carry bit. x7 y7 x6 y6 x5 y5 x4 y4 x3 y3 x2 y2 x1 y1 x0 y0 • • Carry bit is MAJ. z7 z6 z5 z4 z3 z2 z1 z0 c7 c6 c5 c4 c3 c2 c1 c0 • Build truth table for sum bit. • Sum bit is ODD. Do one bit at a time. • Carry bit is MAJ. xi yi ci ci+1 MAJ xi yi ci zi ODD • Sum bit is ODD. 0 0 0 0 0 0 0 0 0 0 • Chain 1-bit adders 0 0 1 0 0 0 0 1 1 1 to "ripple" carries.

0 1 0 0 0 0 1 0 1 1 carry bit 0 1 1 1 1 sum bit 0 1 1 0 0

1 0 0 0 0 1 0 0 1 1 c8 c7 c6 c5 c4 c3 c2 c1 0 x7 x6 x5 x4 x3 x2 x1 x0 1 0 1 1 1 1 0 1 0 0 + y7 y6 y5 y4 y3 y2 y1 y0 1 1 0 1 1 1 1 0 0 0 z7 z6 z5 z4 z3 z2 z1 z0 1 1 1 1 1 1 1 1 1 1 z7 z6 z5 z4 z3 z2 z1 z0

45 46

An 8-bit adder circuit Layers of abstraction

MAJ ODD Lessons for software design apply to hardware • Interface describes behavior of circuit. • Implementation gives details of how to build it. NOT OR AND • Exploit understanding of behavior at each level.

Layers of abstraction apply with a vengeance ADD • On/off. • Controlled switch. [relay, pass transistor] • Gates. [NOT, OR, AND] • Boolean functions. [MAJ, ODD] • Adder. • Arithmetic/Logic unit (next). • CPU (next lecture, stay tuned).

Vastly simplifies design of complex systems and enables use of new technology at any layer

47 48 COMPUTER SCIENCE COMPUTER SCIENCE SEDGEWICK/WAYNE SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY

19. Combinational Circuits

•Building blocks •Boolean algebra •Digital circuits •Adder circuit •Arithmetic/logic unit

CS.19.D.Circuits.Adder CS.19.E.Circuits.ALU

Next layer of abstraction: modules, busses, and control lines Arithmetic and logic unit (ALU) module

input Basic design of our circuits Ex. Three functions on 8-bit words busses • Organized as modules (functional units of TOY: ALU, memory, register, PC, and IR). • Two input busses (arguments). ALU (8-bit) • Connected by busses (groups of wires that propagate information between modules). • One output bus (result). • Controlled by control lines (single wires that control circuit behavior). • Three control lines.

Conventions input • Bus inputs are at the top, busses input connections are at the left. ADD • Bus outputs are at the bottom, control output connections are at the right. lines control • Control lines are blue. lines XOR

output bus AND These conventions make circuits easy to understand. (Like style conventions in coding.) output bus 51 52 Arithmetic and logic unit (ALU) module A simple and useful combinational circuit: one-hot multiplexer

input Ex. Three functions on 8-bit words busses One-hot multiplexer selection • Two input busses (arguments). • m selection lines inputs 0 all other • One output bus (result). • m data inputs 0 select data inputs input 3 ignored • Three control lines. • 1 output. 1 this is a precondition • Left-right shifter circuits omitted • At most one selection line is 1. unlike other circuits 1 data we consider input 3 is 0 (see book for details). • Output has value of selected input. 2

2 Implementation output 3 is 0 One circuit for each function. • 3 • Compute all values in parallel. ADD 4

data 4 control inputs Q. How do we select desired output? lines XOR data A. "One-hot muxes" (see next slide). input 3 is 1 AND

output output output "Calculator" at the heart of your computer. bus is 1 ✓ 53 54

A simple and useful combinational circuit: one-hot multiplexer Summary: Useful combinational circuit modules multiway OR gate One-hot multiplexer • m selection lines 0 ALU 0 • m data inputs AND gate select input 3 • 1 output. 1 • At most one selection line is 1. 1 data input 3 is 0 • Output has value of selected input. 2

2 output Implementation 3 is 0 DEMUX • AND corresponding selection and data inputs. 3

• OR all results (at most one is 1). 4 DECODER/DEMUX

4 Applications Arithmetic-logic unit (previous slide). • data input 3 • Main memory (next lecture). is 1

Next: Registers, memory, connections, and control. Important to note. No direct connection from input to output. a virtual selection switch output is 1 55 56 COMPUTER SCIENCE COMPUTER SCIENCE SEDGEWICK/WAYNE SEDGEWICK/WAYNE PART II: ALGORITHMS, MACHINES, and THEORY Computer Science

19. Combinational Computer Science Circuits An Interdisciplinary Approach

R O B E R T S E D G E W I C K Section 6.12 KEVIN WAYNE

http://introcs.cs.princeton.edu

CS.19.D.Circuits.Adder