Sequential Circuits and DFAs

snick
∨
snack
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion
CPSC 121: Models of Computation
2012 Summer Term 2
– 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?
Building & Designing
Sequential Circuits
Steve Wolfman, based on notes by
Patrice Belleville and others
• Next Lecture Notes
2
1
Learning Goals: Pre-Class
Learning Goals: In-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:
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.
– Trace the operation of a deterministic finitestate 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
Where We Are in
The Big Stories
Theory
Hardware
How do we model
computational systems?
How do we build devices to
compute?
Now: With our powerful
modelling language (pred
logic), we can prove
things like universality
of NOR gates for
combinational circuits.
Our new model (DFAs) are
sort of full computers.
Now: Learning to build a
new kind of circuit with
memory that will be
the key new feature
we need to build fullblown computers!
(Something you’ve seen
in lab from a new angle.) 5
4
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
Problem: Push-Button Light Switch
A Light Switch “DFA”
Problem: Design a circuit to control a light
so that the light changes state any time its
“push-button” switch is pressed.
?
pressed
(Like the light switches in Dempster: Press and release,
and the light changes state. Press and release again,
and it changes again.)
light
off
light
on
pressed
?
7
Problem:
Light Switch
This Deterministic Finite Automaton (DFA) isn’t really about
8
accepting/rejecting; its current state is the state of the light.
COMPARE TO:
?
Lecture 2’s Light Switch
?
Problem: Design a circuit to control a light so that the light
changes state any time its “push-button” switch is
pressed.
Problem: Design a circuit to control a light so that the
light changes state any time its switch is flipped.
Identifying inputs/outputs: consider Which are most useful for
these possible inputs and outputs: this problem?
Identifying inputs/outputs: consider Which are most useful for
these possible inputs and outputs: this problem?
Input1: the button was pressed
Input2: the button is down
Output1: the light is on
Output2: the light changed states
Input1: the switch flipped
Input2: the switch is on
Output1: the light is on
Output2: the light changed states
a.
b.
c.
d.
e.
Input1 and Output1
Input1 and Output2
Input2 and Output1
Input2 and Output2
None of these
a.
b.
c.
d.
e.
Input1 and Output1
Input1 and Output2
Input2 and Output1
Input2 and Output2
None of these
9
10
Departures from
Combinational Circuits
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?
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.
• Next Lecture Notes
11
12
Problem:
How Do We Remember?
Worked Problem:
“Muxy Memory”
We want a circuit that:
• SometimesI remembers its current state.
• Other timesI loads a new state to remember.
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.
???
0
new data
1
output
control
Sounds like a choice.
13
What circuit element do we have for modelling choices?
Worked Problem:
Muxy Memory
Truth Table for “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.
Fill in the MM’s truth table:
0
old output (Q’)
output (Q)
1
new data (D)
14
control (G)
15
G
D
Q'
a.
Q
b.
Q
c.
Q
d.
Q
0
0
0
0
0
0
0
0
0
1
1
1
1
0
0
1
0
0
0
0
0
0
1
1
1
1
1
1
1
0
0
0
0
0
1
1
0
1
1
0
F
1
1
1
0
0
1
F
0
1
1
1
1
1
1
1
This violates our basic combinational constraint: no cycles.
e.
None
of
these
16
Worked Problem:
Truth Table for “Muxy Memory”
Worked Problem:
Truth Table for “Muxy Memory”
Worked Problem: Write a truth table for the
MM:
G
D
Q'
Q
Worked Problem: Write a truth table for the
MM:
G
D
Q'
Q
0
0
0
0
0
0
0
0
0
0
1
1
0
0
1
1
0
1
0
0
0
1
0
0
0
1
1
1
0
1
1
1
1
0
0
0
1
0
0
0
1
0
1
0
1
0
1
0
1
1
0
1
1
1
0
1
1
1
1
1
17
Like a “normal” mux table, but what happens when Q' ≠ Q?
1
1
1
1
18
Q' “takes on” Q’s value at the “next step”.
“D Latch” Symbol + Semantics
D Latch Symbol + Semantics
We call a “muxy memory” a “D latch”.
When G is low, the latch maintains its memory.
When G is high, the latch loads a new value from D.
When G is low, the latch maintains its memory.
When G is high, the latch loads a new value from D.
old output (Q’)
old output (Q’)
0
0
output (Q)
new data (D)
output (Q)
1
new data (D)
control (G)
1
control (G)
19
20
D Latch Symbol + Semantics
Hold On!!
When G is low, the latch maintains its memory.
When G is high, the latch loads a new value from D.
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).
D
new data (D)
Q
output (Q)
G
control (G)
21
22
Using the D Latch
for Circuits with Memory
Using the D Latch
for Our Light Switch
Problem: What goes in the cloud? What do
we send into G?
Problem: What do we send into G?
D
D
Q
??
G
Q
Combinational
Circuit to calculate
next state
??
current light state
G
no (0 bit) input
input
23
We assume we just want Q as the output.
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.
output
24
A Timing Problem:
We Need EVENTS!
A Timing Problem, Metaphor
(from MIT 6.004, Fall 2002)
What’s wrong with this
tollbooth?
Problem: What do we send into G?
D
“pulse”
when
button is
pressed
Q
current light state
G
output
button
pressed
As long as the button is down, D flows to Q
flows through the NOT gate and back to25D...
which is bad!
P.S. Call this a “bar”, not a “gate”,
or we’ll tie ourselves in (k)nots.
A Timing Solution, Metaphor
A Timing Problem
(from MIT 6.004, Fall 2002)
Is this one OK?
Problem: What do we send into G?
D
“pulse”
when
button is
pressed
Q
output
A Timing Solution (Almost)
G
D
Q
D
Q
G
Never raise both “bars” at
the same time.
button
pressed
As long as the button is down, D flows to Q
flows through the NOT gate and back to28D...
which is bad!
A Timing Solution
D
Q
current light state
G
button
pressed
27
D
26
G
output
??
Q
G
output
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).
29
30
Button/Clock is LO
(unpressed)
A Timing Solution
1
D
D
Q
Q
G
1
G
button
press
signal
output
D
Q
Q
G
0
G
output
0
LO
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).
button
pressed
32
31
Button goes HI
(is pressed)
Propagating signal..
left NOT, right latch
1
1
1
D
1
1
D
D
Q
Q
G
0
0
G
HI
1
D
output
1
D
Q
Q
G
1
G
output
1
HI
This stuff is processing a new signal.
This stuff is processing a new signal.
33
34
Propagating signal..
right NOT (steady state)
Button goes LO (released)
0
0
1
D
0
HI
D
Q
Q
G
1
D
1
0
G
output
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!
LO
D
Q
Q
G
1
G
0
output
This stuff is processing a new signal.
35
36
Propagating signal..
left NOT
Propagating signal..
left latch (steady state)
0
0
1
D
1
D
Q
Q
G
1
1
G
LO
0
D
output
0
LO
And, we’re done with one cycle.
38
How does this compare to our initial state?
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.
control
or
“clock”
signal
(CLK)
When CLK goes from low to high, the flip-flop loads a new
value from D.
Otherwise, it maintains its current value.
D
G
Q
output
(Q)
G
39
Master/Slave D Flip-Flop
Symbol + Semantics
D
control
or
“clock”
signal
(CLK)
G
D
Q
Q
G
control
or
“clock”
signal
(CLK)
output
(Q)
G
40
Q
clock signal
Q
D
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
(D)
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)
output
0
Master/Slave D Flip-Flop
Symbol + Semantics
Q
1
G
37
D
Q
G
This stuff is processing a new signal.
new
data
(D)
D
Q
output
output
(Q)
G
new data
41
D
We rearranged the clock and D inputs and the output to match Logisim.
42
Below we use a slightly different looking flip-flop.
Why Abstract?
Outline
Logisim (and real circuits) have lots of flip-flops that all
behave very similarly:
–
–
–
–
• Prereqs, Learning Goals, and !Quiz Notes
• Problems and Discussion
D flip-flops,
T flip-flops,
J-K flip-flops,
and S-R flip-flops.
They have slightly different implementationsI 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.
– 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
PlusI it means you only need to learn about D flip-flops’ guts.
43
The others are similar enough so we can just take the abstraction for granted.
44
Implementing the Branch
Predictor (First Pass)
Branch Prediction Machine
An “if” in hardware is called a “branch”.
Computers “pre-execute” instructions... but to
pre-execute a “branch”, they must guess
whether to execute the then or else part.
Here’s one reasonable guess: whatever the
last branch did, the next one will, too.
Why? In recursion, how often do we hit the
base case vs. the recursive case?
Here’s the corresponding DFA. (Instead of
accept/reject, we care about the current
taken ⇒
the last branch
state.)
not
taken
taken
taken
yes
not
taken
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
no
Experiments show it generally works well to add “inertia”
46
so that it takes two “wrong guesses” to change the predictionI
45
Implementing the Branch
Predictor (Final Version)
Abstract Template
for a DFA Circuit
Here’s a version that takes two wrong
guesses in a row to admit it’s wrong:
Each time the clock “ticks” move from one
state to the next.
taken
taken
YES!
not
taken
yes?
taken
taken
not
taken
no?
not
taken
clock
NO!
input
not
taken
Can we build a branch prediction circuit?
47
store
current
state
compute
next
state
48
How to Implement a DFA:
Slightly Harder
Template for a DFA Circuit
(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.
Each time the clock “ticks” move from one
state to the next.
D
Q
Combinational
circuit to calculate
next state/output
CLK
input
Each of these lines (except the clock) may carry multiple bits;
49
the D flip-flop may be several flip-flops to store several bits.
50
How to Implement a DFA:
Slightly Easier MUX Solution
Implementing the Predictor
Step 1
(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 flipflops.
With a separate circuit for each
51
state, they’re often very simple!
taken
taken
not
taken
taken
YES!
3
not
taken
yes?
2
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
Just Truth Tables...
not
taken
Reminder: taken = 0
not taken = 1
YES!
3
Just Truth Tables...
taken
taken
taken
no?
1
not
taken
yes?
2
Current State
input
New state
0
0
0
0
0
0
1
0
0
1
0
0
1
1
1
0
0
1
0
1
1
1
0
1
1
1
NO!
0
not
taken
As always, we use numbers
to represent the inputs:
taken = 1
52
not taken = 0
taken
taken
taken
no?
1
not
taken
not
taken
not
taken
taken
Reminder: taken = 0
not taken = 1
Current State
input
New state
0
0
0
0
0
0
1
0
0
1
0
1
0
1
0
0
0
0
1
1
1
1
1
0
0
0
0
1
0
1
1
1
1
1
0
1
0
1
1
1
1
1
53
taken
no?
1
yes?
2
not
taken
NO!
0
not
taken
NO!
0
What’s in this row?
a. 0 0
b. 0 1
c. 1 0
d. 1 1
e. None of these.
taken
YES!
3
not
taken
not
taken
54
Implementing the Predictor:
Step 3
Implementing the Predictor:
Step 3
We always use this pattern.
In this case, we need two flip-flops.
DD
Q
Q
??
state0
CLK
DD
input
Q
Q
Combinational
circuit to calculate
next state/output
CLK
???
state1
input
55
Let’s switch to Logisim schematics...
Implementing the Predictor:
Step 3
56
Implementing the Predictor:
Step 5 (easier than 4!)
state0
state0
The MUX “trick” here is much
like in the ALU from lab!
???
state1
input
state1
57
58
input
Implementing the Predictor:
Step 4
state0
state1
In state number 0, what should be the new value of state0?
Hint: look at the DFA, not at the circuit!
a. input
b. ~input
c. 1
d. 0
59
e. None of these. input
Implementing the Predictor:
Step 4
state0
state1
In state number 1, what’s the new value of state0?
a. input
b. ~input
c. 1
d. 0
60
e. None of these.
input
Implementing the Predictor:
Step 4
Implementing the Predictor:
Step 4
state0
state0
state1
state1
In state number 2, what’s the new value of state0?
a. input
b. ~input
c. 1
d. 0
61
e. None of these.
input
Implementing the Predictor:
Step 4
Just Truth Tables...
state0
state1
input
state0'
state1'
0
0
0
0
0
0
0
1
0
1
0
1
0
0
0
0
1
1
1
1
1
0
0
0
0
1
0
1
1
1
1
1
0
1
0
1
1
1
1
1
In state number 3, what’s the new value of state0?
a. input
b. ~input
c. 1
d. 0
62
e. None of these.
input
state0
state1
63
64
input
input
Implementing the Predictor:
Step 4
Implementing the Predictor:
Step 4
In state number 0, what’s the new value of state1?
a. input
b. ~input
c. 1
d. 0
e. None of these.
state0
state1
state1
65
input
TADA!!
66
input
You can often simplify,
but that’s not the point.
Just for Fun: Renaming to
Make a Pattern Clearer...
taken
taken
state0
taken
YES!
3
taken
no?
1
yes?
2
not
taken
NO!
0
not
taken
state1
67
input
Outline
pred
last
input
pred'
last'
0
0
0
0
0
0
0
1
0
1
0
1
0
0
0
0
1
1
1
1
1
0
0
0
0
1
0
1
1
1
1
1
0
1
0
1
1
1
1
1
not
taken
not
taken
68
Steps to Build a Powerful Machine
• 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
69
How Powerful Is a DFA?
DFAs can model situations with a finite
amount of 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.
71
(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 flipflops.
With a separate circuit for each
70
state, they’re often very simple!
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.
72
Outline
Learning Goals: In-Class
• Prereqs, Learning Goals, and !Quiz Notes
• Problems and Discussion
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.
– 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
73
Next Lecture Learning Goals:
Pre-Class
74
Next Lecture Prerequisites
By the start of class, you should be able to:
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.
– 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.
76
75
snick
∨
snack
Problem: Traffic Light
Problem: Design a DFA with outputs to
control a set of traffic lights.
More problems to solve...
(on your own or if we have time)
77
78
PROBLEM: Does this accept
the empty string?
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 “button
press” when it goes off.
Variants to try:
- Pedestrian cross-walks
- Turn signals
- Inductive sensors to indicate presence of cars
- Left-turn signals
79
“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...
81
a,b,c
b
1
b,c
a
2
a
3
c
80