# CPSC 121: Models of Computation 2008/9 Winter Term 2

## Presentation on theme: "CPSC 121: Models of Computation 2008/9 Winter Term 2"— Presentation transcript:

CPSC 121: Models of Computation 2008/9 Winter Term 2
Sequential Circuits (“Mealy Machines”) Steve Wolfman, based on notes by Patrice Belleville and others

Lecture Prerequisites
Read Section 12.2 pages and “Designing a Finite Automaton” on (skipping part b of the examples). We’ll reread and revisit this later; focus on getting a strong intuitive sense of how DFAs work. Solve problems like Exercise Set 12.2 #1, #12a-c, #13a-c, and #14-19a. Complete the open-book, untimed quiz on Vista that was due before class.

Learning Goals: “Pre-Class”
We are mostly departing from the readings for next class 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.

Quiz 8: Does this accept the empty string?
a,b,c b 1 a 2 a 3 b,c c

Learning Goals: In-Class
By the end of this unit, you should be able to: Translate a DFA into a sequential circuit that implements the DFA. Explain how and why each part of the resulting circuit works. Discuss point of learning goals.

Problem: Toggle 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 Dempster.)

Concept Q: Toggle Switch
How many gates does the circuit take? One Two Three None of these (b/c it’s a different number) None of these (b/c it can’t be done)

A “DFA” for the Light Switch
toggle light off light on toggle This “DFA” isn’t really about accepting/rejecting. Instead, its current state indicates the state of the light.

Departures from Combinational Circuits
We need memory so we can “remember” what state the light is in. We need to act on an event (a button push/toggle) rather than in response to an input value.

Worked Problem: Muxy Memory
Worked Problem: How can we use a mux to store a bit of memory? Let’s have it output the stored bit when the control is 0 and load a new value when the control is 1. ??? output new data 1 control

Worked Problem: Muxy Memory
Worked Problem: How can we use a mux to store a bit of memory? Let’s have it output the stored bit when the control is 0 and load a new value when the control is 1. old output (Q’) output (Q) new data (D) 1 control (G) This violates our basic combinational constraint: no cycles.

Worked Problem: Truth Table for Muxy Memory (AKA D Latch)
Worked Problem: Write a truth table for the D Latch. G D Q' Q 1

Worked Problem: Truth Table for Muxy Memory (AKA D Latch)
Worked Problem: Write a truth table for the D Latch. G D Q' Q 1 Like a “normal” mux table, but what happens when Q'  Q?

Worked Problem: Truth Table for Muxy Memory (AKA D Latch)
Worked Problem: Write a truth table for the D Latch. G D Q' Q 1 Like a “normal” mux table, but Q' then takes on Q’s value.

D Latch Symbol + Semantics
When G is low, the latch maintains its memory. When G is high, the latch loads a new value from D. old output (Q’) output (Q) new data (D) 1 control (G)

D Latch Symbol + Semantics
When G is low, the latch maintains its memory. When G is high, the latch loads a new value from D. old output (Q’) output (Q) new data (D) 1 control (G)

D Latch Symbol + Semantics
When G is low, the latch maintains its memory. When G is high, the latch loads a new value from D. D G Q new data (D) output (Q) control (G)

Using the D Latch for Circuits with Memory
Problem: What do we send into G? D G Q Combinational Circuit to calculate next state ?? input output

Using the D Latch for Our Light Switch
Problem: What do we send into G? D G Q current light state ?? input output

“pulse” when button is pressed
A Timing Problem Problem: What do we send into G? D G Q current light state “pulse” when button is pressed output 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!

A Timing Problem, Metaphor (from MIT 6.004, Fall 2002)
What’s wrong with this tollbooth?

A Timing Solution, Metaphor (from MIT 6.004, Fall 2002)
Is this one OK?

“pulse” when button is pressed
A Timing Problem Problem: What do we send into G? D G Q current light state “pulse” when button is pressed output 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!

A Timing Solution (Almost)
D G Q D G Q Never raise both “bars” at the same time. output button pressed

A Timing Solution (Almost)
D G Q D G Q ?? output Any time we disable the first latch, we enable the second latch a moment later. Any time we enable the first latch, we disable the second latch a moment later (but not so long that any “cars” get through).

A Timing Solution (Almost)
Problem: What do we send into G? D G Q D G Q ?? output Unfortunately, our second latch enables (and so our solution loads new data) when the input signal goes low. We want it to work when the input goes high. So...

A Timing Solution D G Q D G Q button press signal output
button pressed

Master/Slave D Flip-Flop Symbol + Semantics
When CLK goes from low to high, the flip-flop loads a new value from D. Otherwise, it maintains its current value. new data (D) D G Q D G Q output (Q) control or “clock” signal (CLK)

Master/Slave D Flip-Flop Symbol + Semantics
When CLK goes from low to high, the flip-flop loads a new value from D. Otherwise, it maintains its current value. new data (D) D G Q D G Q output (Q) control or “clock” signal (CLK)

Master/Slave D Flip-Flop Symbol + Semantics
When CLK goes from low to high, the flip-flop loads a new value from D. Otherwise, it maintains its current value. new data (D) D G Q D G Q output (Q) control or “clock” signal (CLK)

Master/Slave D Flip-Flop Symbol + Semantics
When CLK goes from low to high, the flip-flop loads a new value from D. Otherwise, it maintains its current value. D Q new data output clock signal

A Timing Solution D Q button press signal output

General Implementation of DFAs as “Mealy Machines”
Each time the clock “ticks” move from one state to the next. D Q Combinational circuit to calculate next state/output CLK input output Each of these lines (except the clock) may carry multiple bits; the D flip-flop may be several flip-flops to store several bits.

A Mealy-Style “DFA” for the Light Switch
toggle, on no toggle, on off state on state no toggle, off toggle, off This “DFA” isn’t really about accepting/rejecting. Instead, we output a “command” to the light on every transition.

How to Implement a DFA as a “Mealy Machine”
(Re-)Number the states, starting with 0. Figure out how many bits you need to store the state number. Use that many D flip-flops to store the state. For each state, build a combinational circuit that determines the next state and the output given the input. (So, you’re building a separate circuit for each state!) Send all those “next states” and outputs from the circuits into multiplexers, and use the current state as the control signal. Send the output of the multiplexer back into your flip-flops.

Light Switch to Mealy Machine: Step 1
toggle, on no toggle, on 1 no toggle, off toggle, off

Light Switch to Mealy Machine: Step 2
toggle, on no toggle, on 1 no toggle, off toggle, off We only need 1 bit to store the state number, since there are only two states.

Light Switch to Mealy Machine: Step 3
We always use this pattern. In this case, we only need one bit of flip-flops. D Q Combinational circuit to calculate next state/output CLK input output

Light Switch to Mealy Machine: Step 4
Our input is toggle. If toggle is true, then the button was toggled on this clock cycle. If not, the button was not toggled. Our output indicates whether the light should be on or off this cycle. If we’re in state 0, output is false (off) if we didn’t toggle and true (on) if we did. If we’re in state 1, output is true (on) if we didn’t toggle and false (off) if we did.

Light Switch to Mealy Machine: Step 4
Our input is toggle. If toggle is true, then the button was toggled on this clock cycle. If not, the button was not toggled. Our output indicates whether the light should be on or off this cycle. In either state, our next state switches if we did toggle and stays the same if we didn’t.

Light Switch to Mealy Machine: Step 4
new state State 0 toggle output new state State 1 toggle output

Light Switch to Mealy Machine: Step 4
new state/ output State 0 toggle new state/ output State 1 toggle Since they’re identical, we collapse them. They’re not always identical!

Light Switch to Mealy Machine: Step 5
state new state/ output toggle 1 Note: moved the select line to the top of the mux to make the next drawing cleaner...

Light Switch to Mealy Machine: Step 6
1 toggle, on toggle, off no toggle, off no toggle, on D Q CLK 1 toggle output

Steps to Build a Powerful Machine
(Re-)Number the states, starting with 0. Figure out how many bits you need to store the state number. Use that many D flip-flops to store the state. For each state, build a combinational circuit that determines the next state and the output given the input. (So, you’re building a separate circuit for each state!) Send all those “next states” and outputs from the circuits into multiplexers, and use the current state as the control signal. Send the output of the multiplexer back into your flip-flops.

How Powerful Is a DFA? How does a DFA compare to a modern computer? Can a DFA take a DFA as input and run it?

Where We’ll Go From Here...
We’ll come back to DFAs again later in lecture. In two upcoming labs, you’ll: Build a circuit to perform a practical task using techniques like these. Explore how to build something like a DFA that can run other DFAs... a general purpose computer.

Learning Goals: In-Class
By the end of this unit, you should be able to: Translate a DFA into a sequential circuit that implements the DFA. Explain how and why each part of the resulting circuit works. Discuss point of learning goals.

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

Next Lecture Prerequisites
Read Section 4.1. Note: 4.1 is background information. We don’t intend to directly assess this material, but assignment and exam questions may require skills and knowledge from 4.1 to answer. Solve problems like Exercise Set 4.1, #1-8, 12-14, 18-31, 37-44, Complete the open-book, untimed quiz on Vista that’s due before the next class. TENTATIVE

(on your own or if we have time)
More problems to solve... (on your own or if we have time)

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

Problem: Traffic Light
Problem: Design a DFA with outputs to control a set of traffic lights. Thought: try allowing an output that sets a timer which in turn causes an input like our toggle when it goes off. Variants to try: - Pedestrian cross-walks - Turn signals - Inductive sensors to indicate presence of cars - Left-turn signals