CPSC 121: Models of Computation 2017S Building & Designing - - PowerPoint PPT Presentation

cpsc 121 models of computation 2017s
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation 2017S Building & Designing - - PowerPoint PPT Presentation

CPSC 121: Models of Computation 2017S Building & Designing Sequential Circuits Meghan Allen, based on notes by Steve Wolfman, Patrice Belleville and others 1 This work is licensed under a Creative Commons Attribution 3.0 Unported License.


slide-1
SLIDE 1

CPSC 121: Models of Computation 2017S

Building & Designing Sequential Circuits Meghan Allen, based on notes by Steve Wolfman, Patrice Belleville and others

1

This work is licensed under a Creative Commons Attribution 3.0 Unported License.

slide-2
SLIDE 2

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

2

slide-3
SLIDE 3

Learning Goals: Pre-Class

We are mostly departing from the readings to talk about a new kind of circuit on our way to a full computer: sequential circuits. The pre-class goals are to be able to:

– Trace the operation of a deterministic finite-state automaton (represented as a diagram) on an input, including indicating whether the DFA accepts or rejects the input. – Deduce the language accepted by a simple DFA after working through multiple example inputs.

3

slide-4
SLIDE 4

Learning Goals: In-Class

By the end of this unit, you should be able to:

– Translate a DFA to a corresponding sequential circuit, but with a “hole” in it for the circuitry describing the DFA’s transitions. – Describe the contents of that “hole” as a combinational circuitry problem (and therefore solve it, just like you do other combinational circuitry problems!). – Explain how and why each part of the resulting circuit works.

4

slide-5
SLIDE 5

Where We Are in The Big Stories

Theory How do we model computational systems? Now: With our powerful modelling language (pred. logic), we can prove things like universality

  • f NOR gates for

combinational circuits. Our new model (DFAs) are sort

  • f full computers.

Hardware How do we build devices to compute? Now: Learning to build a new kind of circuit with memory that will be the key new feature we need to build full- blown computers! (Something you’ve seen in lab from a new angle.)

5

slide-6
SLIDE 6

Outline

  • Prereqs, Learning Goals, and Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

6

slide-7
SLIDE 7

Problem: Push-Button Light Switch

Problem: Design a circuit to control a light so that the light changes state any time its “push- button” switch is pressed.

(Like the light switches in some buildings on campus: Press and release, and the light changes state. Press and release again, and it changes again.)

7

?

slide-8
SLIDE 8

A Light Switch “DFA”

8

light

  • ff

light

  • n

pressed pressed This Deterministic Finite Automaton (DFA) isn’t really about accepting/rejecting; its current state is the state of the light. ?

slide-9
SLIDE 9

Problem: Light Switch

Problem: Design a circuit to control a light so that the light

changes state any time its “push-button” switch is pressed.

9

?

Identifying inputs/outputs: consider these possible inputs and outputs: Input: the button was pressed or the button is down Output: the light is shining or the light changed states Which are most useful for this problem?

  • a. pressed and shining
  • b. pressed and changed
  • c. down and shining
  • d. down and changed
  • e. None of these
slide-10
SLIDE 10

COMPARE TO: Lecture 2’s Light Switch

Problem: Design a circuit to control a light so that the light changes state any time its switch is flipped.

10

Identifying inputs/outputs: consider these possible inputs and outputs: Input: the switch flipped or the switch is up Output: the light is shining or the light changed states Which are most useful for this problem?

  • a. flipped and shining
  • b. flipped and changed
  • c. up and shining
  • d. up and changed
  • e. None of these

?

slide-11
SLIDE 11

Outline

  • Prereqs, Learning Goals, and !Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

11

slide-12
SLIDE 12

Departures from Combinational Circuits

MEMORY: We need to “remember” the light’s state. EVENTS: We need to act on a button push rather than in response to an input value.

12

slide-13
SLIDE 13

Problem: How Do We Remember?

We want a circuit that:

  • Sometimes… remembers its current state.
  • Other times… loads a new state to remember.

13

Sounds like a choice. What circuit element do we have for modelling choices?

slide-14
SLIDE 14

Worked Problem: “Muxy Memory”

How do we use a mux to store a bit of memory? We choose to remember on a control value of 0 and to load a new state on a 1.

14

1

  • utput

(remember) ??? new data control

We use “0” and “1” because that’s how MUXes are usually labelled.

slide-15
SLIDE 15

Worked Problem: Muxy Memory

How do we use a mux to store a bit of memory? We choose to remember on a control value of 0 and to load a new state on a 1.

15

1

  • utput (Q)
  • ld output (Q’)

new data (D) control (G) This violates our basic combinational constraint: no cycles.

slide-16
SLIDE 16

Truth Table for “Muxy Memory”

16

Fill in the MM’s truth table:

G D Q' 1 1 1 1 1 1 1 1 1 1 1 1 a. b. c. d. e. Q 1 1 1 1 Q 1 1 1 1 Q 1 1 X X 1 Q 1 1 1 1 None

  • f

these

slide-17
SLIDE 17

Worked Problem: Truth Table for “Muxy Memory”

Worked Problem: Write a truth table for the MM:

17

G D Q' Q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Like a “normal” mux table, but what happens when Q'  Q?

slide-18
SLIDE 18

Worked Problem: Truth Table for “Muxy Memory”

Worked Problem: Write a truth table for the MM:

18

G D Q' Q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Q' “takes on” Q’s value at the “next step”.

slide-19
SLIDE 19

“D Latch” Symbol + Semantics

We call a “muxy memory” a “D latch”. When G is 0, the latch maintains its memory. When G is 1, the latch loads a new value from D.

19

1

  • utput (Q)
  • ld output (Q’)

new data (D) control (G)

slide-20
SLIDE 20

D Latch Symbol + Semantics

When G is 0, the latch maintains its memory. When G is 1, the latch loads a new value from D.

20

1

  • utput (Q)
  • ld output (Q’)

new data (D) control (G)

slide-21
SLIDE 21

D Latch Symbol + Semantics

When G is 0, the latch maintains its memory. When G is 1, the latch loads a new value from D.

21

  • utput (Q)

new data (D) control (G) D G Q

slide-22
SLIDE 22

Hold On!!

Why does the D Latch have two inputs and one output when the mux inside has THREE inputs and one output?

  • a. The D Latch is broken as is; it should have three inputs.
  • b. A circuit can always ignore one of its inputs.

c. One of the inputs is always true.

  • d. One of the inputs is always false.
  • e. None of these (but the D Latch is not broken as is).

22

DON’T PANIC!

slide-23
SLIDE 23

Using the D Latch for Circuits with Memory

Problem: What goes in the cloud? What do we send into G?

23

D G Q Combinational Circuit to calculate next state input ?? We assume we just want Q as the output.

slide-24
SLIDE 24

Using the D Latch for Our Light Switch

Problem: What do we send into G?

24

D G Q no (0 bit) input

  • utput

?? current light state

  • a. T if the button is down, F if it’s up.
  • b. T if the button is up, F if it’s down.
  • c. Neither of these.
slide-25
SLIDE 25

A Timing Problem: We Need EVENTS!

Problem: What do we send into G?

25

D G Q

  • utput

“pulse” when button is pressed current light state button pressed As long as the button is down, D flows to Q flows through the NOT gate and back to D... which is bad!

slide-26
SLIDE 26

A Timing Problem, Metaphor

(from MIT 6.004, Fall 2002)

What’s wrong with this tollbooth?

26

P.S. Call this a “bar”, not a “gate”,

  • r we’ll tie ourselves in (k)nots.
slide-27
SLIDE 27

A Timing Solution, Metaphor

(from MIT 6.004, Fall 2002)

Is this one OK?

27

slide-28
SLIDE 28

A Timing Problem

Problem: What do we send into G?

28

D G Q

  • utput

“pulse” when button is pressed current light state button pressed As long as the button is down, D flows to Q flows through the NOT gate and back to D... which is bad!

slide-29
SLIDE 29

A Timing Solution (Almost)

29

L

D G Q

  • utput

button pressed

R

D G Q Never raise both “bars” at the same time.

slide-30
SLIDE 30

A Timing Solution

30

  • utput

?? The two latches are never enabled at the same time (except for the moment needed for the NOT gate on the left to compute, which is so short that no “cars” get through).

L

D G Q

R

D G Q

slide-31
SLIDE 31

A Timing Solution

31

  • utput

button pressed button press signal

L

D G Q

R

D G Q

slide-32
SLIDE 32

Button/Clock is LO (unpressed)

32

  • utput

LO 1 1 1

We’re assuming the circuit has been set up and is “running normally”. Right now, the light is off (i.e., the output of the right latch is 0).

L

D G Q

R

D G Q

slide-33
SLIDE 33

Button goes HI (is pressed)

33

  • utput

HI 1 1 1 1 This stuff is processing a new signal.

L

D G Q

R

D G Q

slide-34
SLIDE 34

Propagating signal.. left NOT, right latch

34

  • utput

HI 1 1 1 1 This stuff is processing a new signal.

L

D G Q

R

D G Q

slide-35
SLIDE 35

Propagating signal.. right NOT (steady state)

35

  • utput

HI 1 1 1

Why doesn’t the left latch update? a. Its D input is 0. b. Its G input is 0. c. Its Q output is 1. d. It should update!

L

D G Q

R

D G Q

slide-36
SLIDE 36

Button goes LO (released)

36

  • utput

LO 1 1 This stuff is processing a new signal.

L

D G Q

R

D G Q

slide-37
SLIDE 37

Propagating signal.. left NOT

37

  • utput

LO 1 1 1 This stuff is processing a new signal.

L

D G Q

R

D G Q

slide-38
SLIDE 38

Propagating signal.. left latch (steady state)

38

  • utput

LO 1 1 And, we’re done with one cycle. How does this compare to our initial state?

L

D G Q

R

D G Q

slide-39
SLIDE 39

Master/Slave D Flip-Flop Symbol + Semantics

When CLK goes from 0 (low) to 1 (high), the flip-flop loads a new value from D. Otherwise, it maintains its current value.

39

  • utput

(Q) new data (D) control

  • r

“clock” signal (CLK) D G Q D G Q

slide-40
SLIDE 40

Master/Slave D Flip-Flop Symbol + Semantics

When CLK goes from 0 (low) to 1 (high), the flip-flop loads a new value from D. Otherwise, it maintains its current value.

40

  • utput

(Q) new data (D) control

  • r

“clock” signal (CLK) D G Q D G Q

slide-41
SLIDE 41

Master/Slave D Flip-Flop Symbol + Semantics

When CLK goes from 0 (low) to 1 (high), the flip-flop loads a new value from D. Otherwise, it maintains its current value.

41

  • utput

(Q) new data (D) control

  • r

“clock” signal (CLK) D G Q D G Q

slide-42
SLIDE 42

Master/Slave D Flip-Flop Symbol + Semantics

When CLK goes from 0 (low) to 1 (high), the flip-flop loads a new value from D. Otherwise, it maintains its current value.

42

new data clock signal D Q

  • utput

We rearranged the clock and D inputs and the output to match Logisim. Below we use a slightly different looking flip-flop.

slide-43
SLIDE 43

Why Abstract?

Logisim (and real circuits) have lots of flip-flops that all behave very similarly:

– D flip-flops, – T flip-flops, – J-K flip-flops, – and S-R flip-flops.

They have slightly different implementations… and one could imagine brilliant new designs that are radically different inside. Abstraction allows us to build a good design at a high-level without worrying about the details.

43

Plus… it means you only need to learn about D flip-flops’ guts. The others are similar enough so we can just take the abstraction for granted.

slide-44
SLIDE 44

Outline

  • Prereqs, Learning Goals, and !Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

44

slide-45
SLIDE 45

How Do Computers Execute Programs?

  • High-level languages (Java/BSL) are translated

into low-level machine language

  • A machine language program is a list of

instructions in a representation scheme close to “plain” binary (e.g., 4 bits for the type of instruction, 4 bits for what part of the computer the result goes to, etc.)

  • Each instruction has a (barely) human-readable

version

  • After it’s done with an instruction, the computer

(usually) executes the next instruction in the list

45

slide-46
SLIDE 46

Simplified Example (sum of 1..n)

(1) sum  0 (2) is n = 0? (3) if the answer was yes, go to (7) (4) sum  sum + n (5) n  n - 1 (6) go to (2) (7) halt

46

If the computer executed all instructions in

  • rder, there’d be no loops or recursion!

Fortunately, “branch” instructions (like (3)) tell the computer to go elsewhere.

slide-47
SLIDE 47

Simplified Example, Complicated Execution

(1) sum  0 (2) is n = 0? (3) if the answer was yes, go to (7) (4) sum  sum + n (5) n  n - 1 (6) go to (2) (7) halt

47

To run faster, the computer starts executing

  • ne instruction even before it finishes the last.

But then what does it do with (3)? Does it execute (4) or (7)?

slide-48
SLIDE 48

Branch Prediction Machine

To pre-execute a “branch”, the computer guesses which instruction comes next. Here’s one reasonable guess: If the last branch was “taken” (like going to (7) from (3)), take the next. If it was “not taken” (like going to (4) from (3)), don’t take the next.

48

Why? In recursion, how often do we hit the base case vs. the recursive case?

slide-49
SLIDE 49

Implementing the Branch Predictor (First Pass)

Here’s the corresponding DFA. (Instead of accept/reject, we care about the current state.)

49

yes no taken not taken taken not taken

Experiments show it generally works well to add “inertia” so that it takes two “wrong guesses” to change the prediction…

taken  the last branch was taken not taken  the last branch was not taken yes  we predict the next branch will be taken no  we predict the next branch will be not taken

slide-50
SLIDE 50

Implementing the Branch Predictor (Final Version)

Here’s a version that takes two wrong guesses in a row to admit it’s wrong: Can we build a branch prediction circuit?

50

YES! yes? no? NO! not taken taken not taken taken not taken taken taken not taken

slide-51
SLIDE 51

Abstract Template for a DFA Circuit

Each time the clock “ticks” move from one state to the next.

51

input clock compute next state store current state

slide-52
SLIDE 52

Template for a DFA Circuit

Each time the clock “ticks” move from one state to the next.

52

D Q Combinational circuit to calculate next state/output input CLK Each of these lines (except the clock) may carry multiple bits; the D flip-flop may be several flip-flops to store several bits.

slide-53
SLIDE 53

How to Implement a DFA: Slightly Harder

(1) Number the states and figure out b: the number of bits needed to store the state number. (2) Lay out b D flip-flops. Together, their memory is the state as a binary number. (3) Build a combinational circuit that determines the next state given the input and the current state. (4) Use the next state as the new state of the flip-flops.

53

slide-54
SLIDE 54

How to Implement a DFA: Slightly Easier MUX Solution

(1) Number the states and figure out b: the number of bits needed to store the state number. (2) Lay out b D flip-flops. Together, their memory is the state as a binary number. (3) For each state, build a combinational circuit that determines the next state given the input. (4) Send the next states into a MUX with the current state as the control signal: only the appropriate next state gets used! (5) Use the MUX’s output as the new state of the flip-flops.

54

With a separate circuit for each state, they’re often very simple!

slide-55
SLIDE 55

Implementing the Predictor Step 1

55

YES! 3 yes? 2 no? 1 NO! not taken taken not taken taken not taken taken taken not taken

What is b (the number of 1-bit flip-flops needed to represent the state)? a. 0, no memory needed b. 1 c. 2 d. 3 e. None of these

As always, we use numbers to represent the inputs: taken = 1 not taken = 0

slide-56
SLIDE 56

Just Truth Tables...

56

Current State input New state 1 1 1 1 1 1 1 1 1 1 1 1 1

YES! 3 yes? 2 no? 1 NO! not taken taken not taken taken not taken take taken not taken

What’s in this row?

  • a. 0 0
  • b. 0 1
  • c. 1 0
  • d. 1 1
  • e. None of these.

Reminder: not taken = 0 taken = 1

slide-57
SLIDE 57

Just Truth Tables...

57

Current State input New state 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

YES! 3 yes? 2 no? 1 NO! not taken taken not taken taken not taken take taken not taken

Reminder: not taken = 0 taken = 1

slide-58
SLIDE 58

Implementing the Predictor: Step 3

We always use this pattern. In this case, we need two flip-flops.

58

D Q Combinational circuit to calculate next state/output input CLK D Q Let’s switch to Logisim schematics...

slide-59
SLIDE 59

???

Implementing the Predictor: Step 3

59

D Q ?? input CLK D Q

sleft sright

slide-60
SLIDE 60

Implementing the Predictor: Step 3

60

???

sleft sright input

slide-61
SLIDE 61

Implementing the Predictor: Step 5 (easier than 4!)

61

The MUX “trick” here is much like in the ALU from lab!

sleft sright input

slide-62
SLIDE 62

Implementing the Predictor: Step 4

62

input sleft sright In state number 0, what should be the new value of sleft? Hint: look at the DFA, not at the circuit! a. input b. ~input c. 1 d. e. None of these.

slide-63
SLIDE 63

Implementing the Predictor: Step 4

63

input In state number 1, what’s the new value of sleft? a. input b. ~input c. 1 d. e. None of these. sleft sright

slide-64
SLIDE 64

Implementing the Predictor: Step 4

64

input In state number 2, what’s the new value of sleft? a. input b. ~input c. 1 d. e. None of these. sleft sright

slide-65
SLIDE 65

Implementing the Predictor: Step 4

65

input In state number 3, what’s the new value of sleft? a. input b. ~input c. 1 d. e. None of these. sleft sright

slide-66
SLIDE 66

Just Truth Tables...

66

sleft sright input sleft' sright' 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

input

slide-67
SLIDE 67

Implementing the Predictor: Step 4

67

input sleft sright

slide-68
SLIDE 68

Implementing the Predictor: Step 4

68

In state number 0, what’s the new value of sright? a. input b. ~input c. 1 d. e. None of these. input sright

slide-69
SLIDE 69

Implementing the Predictor: Step 4

69

TADA!!

input sleft sright

slide-70
SLIDE 70

You can often simplify, but that’s not the point.

70

input sleft sright

slide-71
SLIDE 71

Just for Fun: Renaming to Make a Pattern Clearer...

71

pred last input pred' last' 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

YES! 3 yes? 2 no? 1 NO! not taken taken not taken taken not taken take taken not taken

slide-72
SLIDE 72

Outline

  • Prereqs, Learning Goals, and !Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

72

slide-73
SLIDE 73

Steps to Build a Powerful Machine

(1) Number the states and figure out b: the number of bits needed to store the state number. (2) Lay out b D flip-flops. Together, their memory is the state as a binary number. (3) For each state, build a combinational circuit that determines the next state given the input. (4) Send the next states into a MUX with the current state as the control signal: only the appropriate next state gets used! (5) Use the MUX’s output as the new state of the flip-flops.

73

With a separate circuit for each state, they’re often very simple!

slide-74
SLIDE 74

How Powerful Is a DFA?

DFAs can model situations with a finite amount

  • f memory, finite set of possible inputs, and

particular pattern to update the memory given the inputs. How does a DFA compare to a modern computer?

  • a. Modern computer is more powerful.
  • b. DFA is more powerful.
  • c. They’re the same.

74

slide-75
SLIDE 75

Where We’ll Go From Here...

We’ll come back to DFAs again later in lecture. In lab you have been and will continue to explore what you can do once you have memory and events. And, before long, how you combine these into a working computer! Also in lab, you’ll work with a widely used representation equivalent to DFAs: regular expressions.

75

slide-76
SLIDE 76

Outline

  • Prereqs, Learning Goals, and !Quiz Notes
  • Problems and Discussion

– A Pushbutton Light Switch – Memory and Events: D Latches & Flip-Flops – General Implementation of DFAs (with a more complex DFA as an example) – How Powerful are DFAs?

  • Next Lecture Notes

76

slide-77
SLIDE 77

Learning Goals: In-Class

By the end of this unit, you should be able to:

– Translate a DFA to a corresponding sequential circuit, but with a “hole” in it for the circuitry describing the DFA’s transitions. – Describe the contents of that “hole” as a combinational circuitry problem (and therefore solve it, just like you do other combinational circuitry problems!). – Explain how and why each part of the resulting circuit works.

77

slide-78
SLIDE 78

Next Lecture Learning Goals: Pre- Class

By the start of class, you should be able to:

– Convert sequences to and from explicit formulas that describe the sequence. – Convert sums to and from summation/”sigma” notation. – Convert products to and from product/”pi” notation. – Manipulate formulas in summation/product notation by adjusting their bounds, merging or splitting summations/products, and factoring out values.

78

slide-79
SLIDE 79

Next Lecture Prerequisites

See the Mathematical Induction Textbook Sections at the bottom of the “Readings and Equipment” page. Complete the open-book, untimed quiz on Vista that’s due before the next class.

79

slide-80
SLIDE 80

More problems to solve...

(on your own or if we have time)

80

slide-81
SLIDE 81

Problem: Traffic Light

Problem: Design a DFA with outputs to control a set of traffic lights.

81

slide-82
SLIDE 82

Problem: Traffic Light

Problem: Design a DFA with outputs to control a set

  • f traffic lights.

Thought: try allowing an output that sets a timer which in turn causes an input like our “button press” when it goes off. Variants to try:

  • Pedestrian cross-walks
  • Turn signals
  • Inductive sensors to indicate presence of cars
  • Left-turn signals

82

slide-83
SLIDE 83

1 2 3

a a a,b,c b b,c c

PROBLEM: Does this accept the empty string?

83

slide-84
SLIDE 84

“Moore Machines”  DFAs whose state matters

A “Moore Machine” is a DFA that’s not about accepting or rejecting but about what state it’s in at a particular time. Let’s work with a specific example...

84