Overview
Readings Brown & Vranesic §8.1, 8.3, 8.7 · weeks 10–11
Ch 7 gave us the storage elements and counters showed one useful circuit built from them. This chapter turns that into a general method. A finite state machine (FSM) is a sequential circuit described by a finite number of states. The design process takes a written specification, turns it into a state diagram and a state table, assigns binary codes to the states, and then derives the logic for the flip-flops. It covers the two kinds of machine, Moore (output depends on the state only) and Mealy (output depends on the state and the input), and finishes by designing a modulo-8 counter with this method, once with D flip-flops and once with J-K flip-flops.
Notes
Sequential circuits and memory
| Circuit | Output depends on… | Example |
|---|---|---|
| Combinational | the present inputs only | decoder, adder |
| Storage | the previous state, not the present inputs | flip-flop |
| Sequential | the present inputs and stored information (the previous state) | counter, sequence detector |
The key idea is that a sequential circuit has memory. The state records whatever is relevant about the input history, and the circuit updates the state at specified clock edges.
There are two types:
- Synchronous: a clock signal controls the operation.
- Asynchronous: no clock is used (that’s Ch 9).
A synchronous sequential circuit is built from flip-flops and combinational logic. Its parts:
- Primary inputs (): the inputs to the circuit.
- Flip-flops (): store the present state.
- Next-state logic: a combinational circuit that uses the present state and the inputs to work out the next state ().
- Output logic: a combinational circuit that produces the outputs ().
How it runs, over and over:
- The flip-flops hold the present state .
- The next-state logic uses and to compute the next state .
- At the active clock edge, the flip-flops load .
- The circuit is now in the new state.
Because the flip-flops are edge-triggered, only one state transition can happen per clock cycle. Another name for a synchronous sequential circuit is a finite state machine (FSM).
Notation: lowercase is the present state (the flip-flop outputs) and uppercase is the next state (the values that will be loaded at the next edge). For a D flip-flop, .
Basic design steps (the sequence detector)
The running example for §8.1 is a sequence detector.
Specification: one input and one output . All state changes happen on the rising clock edge. when was 1 during the two immediately preceding clock cycles, and otherwise. So the circuit detects two or more consecutive 1s.
Why memory is needed
The same present input can need different outputs depending on what happened earlier:
| Previous | Present | Required |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Look at the rows with present : one needs and the other needs . So can’t be found from alone. The circuit must remember whether a 1 was seen in the previous clock cycle, and that stored information is the state.
State diagram
A state diagram shows the behaviour of the circuit graphically:
- Circles (nodes) are states.
- Directed arrows (arcs) are transitions between states.
- Labels on the arrows give the input condition for that transition.
Read an arrow as: if the machine is in the state at the tail of the arrow and the input condition is true at the active clock edge, it moves to the state at the head.
The detector needs three states:
| State | Remembered information | |
|---|---|---|
| A | no consecutive 1s detected yet (also the reset state) | 0 |
| B | one 1 has been detected | 0 |
| C | two or more consecutive 1s have been detected | 1 |
Figure 8.1: State diagram of the Moore sequence detector. The output is written inside each state.
Transitions:
- A: , stay in A. , go to B.
- B: , back to A. , go to C.
- C: , stay in C. , back to A.
The output is shown inside the state circles because it depends only on the present state. A machine like that is a Moore machine.
State table
The state table lists the next state and output for every present state and every input value.
| Present state | Next state () | Next state () | Output |
|---|---|---|---|
| A | A | B | 0 |
| B | A | C | 0 |
| C | A | C | 1 |
Notes on the design:
- State A is the starting state, and the machine stays in A while .
- State B records the first 1. The output stays 0.
- State C means two or more consecutive 1s have been detected, so .
- Reset forces the machine to the starting state A from any state. In hardware it’s implemented by an input on the flip-flops that clears all of them to 0. It’s shown separately instead of drawing a Reset arrow out of every state, so the diagram stays readable.
Example 8.1: Tracing the Moore detector
The machine starts in A (after Reset). The input at six successive rising edges is . Find the state and output after each edge.
Show answerHide answer
| Edge | State after | after | |
|---|---|---|---|
| start | – | A | 0 |
| 1 | 1 | B | 0 |
| 2 | 1 | C | 1 |
| 3 | 1 | C | 1 |
| 4 | 0 | A | 0 |
| 5 | 1 | B | 0 |
| 6 | 0 | A | 0 |
only becomes 1 after the edge at which the second 1 is sampled, because depends on the state and the state only changes at the edge.
Try it: flip and press the clock edge to step the Moore machine. The highlighted arrow is the transition that the next edge will take, and the table records each cycle. Try to reproduce Example 8.1.
State assignment
Flip-flops store 0s and 1s, so the symbolic states A, B, C have to be given binary codes. Each bit of the code is a state variable, and each state variable is one flip-flop.
flip-flops can encode states, so the number of flip-flops is
Example 8.2: How many state variables?
How many flip-flops are needed for (a) the 3-state detector, (b) an 8-state counter, (c) a 5-state machine?
Show answerHide answer
- (a) , so 2 flip-flops.
- (b) , so 3 flip-flops.
- (c) , so 3 flip-flops.
For the detector, use two variables :
| State | |
|---|---|
| 00 | A |
| 01 | B |
| 10 | C |
| 11 | unused |
The unused code 11 can be treated as a don’t-care when simplifying, which lets the K-map groups get bigger.
The state-assigned table shows next-state variables and the output . For a Moore machine, is a function of the present state only.
| Present state | : | : | |
|---|---|---|---|
| 00 (A) | 00 | 01 | 0 |
| 01 (B) | 00 | 10 | 0 |
| 10 (C) | 00 | 10 | 1 |
| 11 | dd | dd | d |
Here means don’t-care. This table is a truth table for three functions: , and .
Choosing flip-flops and deriving the logic
With D flip-flops the design is straightforward, because the next-state functions connect straight to the inputs:
Read the functions from the state-assigned table and simplify with K-maps (using 11 as don’t-care):
- only for with , so .
- for or with . With the don’t-care at 11, the groups grow to .
- , since only in state C () and 11 is a don’t-care.
Example 8.3: Timing of the circuit
Use equations (8.1) to follow the circuit from for .
Show answerHide answer
| Edge | New | ||||
|---|---|---|---|---|---|
| 1 | 1 | 1 | 0 | 01 (B) | 0 |
| 2 | 1 | 0 | 1 | 10 (C) | 1 |
| 3 | 1 | 0 | 1 | 10 (C) | 1 |
| 4 | 0 | 0 | 0 | 00 (A) | 0 |
This matches Example 8.1, so the circuit really implements the state table.
The full design procedure
| Step | Design activity | Main result |
|---|---|---|
| 1 | Read and clarify the specification | inputs, outputs, timing requirements |
| 2 | Identify the states and transitions | state diagram |
| 3 | Record the transitions for every input case | state table |
| 4 | Minimize the number of states if possible | reduced state table |
| 5 | Choose state variables and assign codes | state-assigned table |
| 6 | Choose the flip-flop type, derive next-state and output logic | Boolean equations |
| 7 | Implement and verify the circuit | gate-level circuit and timing check |
Always check that every state/input combination has a defined next state.
Mealy state model
In a Moore machine the output depends only on the present state, , and it’s drawn inside the state circle. In a Mealy machine the output depends on the present state and the present input, , and it’s drawn on the arrows as input / output.
| Moore | Mealy |
|---|---|
| output shown in the state circle | output shown on the transition arrow |
| output changes only after the clock edge | output can change as soon as the input changes |
Mealy machines can often use fewer states, and respond to an input without waiting for another state transition.
The same detector as a Mealy machine
New specification: becomes 1 during the same clock cycle in which the second consecutive 1 is present on . That can be done with only two states:
- A: the last input was not a 1 (or reset).
- B: the last input was a 1.
Figure 8.2: State diagram of the Mealy sequence detector. Each arrow is labelled input / output.
| Present state | Next state () | Next state () | Output |
|---|---|---|---|
| A | A | B | 0 |
| B | A | B |
In B with the second consecutive 1 is present right now, so in the same cycle. In B with the output is 0.
Let be the single state bit, with and :
- From either state, goes to A (next state 0) and goes to B (next state 1). So , and with a D flip-flop .
- In state A the output is always 0. In state B the output is 1 only when . So .
Compare this with the Moore version: three states, two flip-flops and equations (8.1), against two states, one flip-flop and two simple equations.
Example 8.4: Moore vs Mealy output timing
Both detectors are reset to A. The input is in clock cycles 1 to 6. Find for each.
Show answerHide answer
| Cycle | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| 0 | 1 | 1 | 1 | 0 | 0 | |
| Moore state (during the cycle) | A | A | B | C | C | A |
| Moore | 0 | 0 | 0 | 1 | 1 | 0 |
| Mealy state (during the cycle) | A | A | B | B | B | A |
| Mealy | 0 | 0 | 1 | 1 | 0 | 0 |
- Moore: rises in cycle 4, one cycle after the second 1 is applied (cycle 3). It also stays 1 for one cycle after falls.
- Mealy: rises in cycle 3, the cycle in which the second 1 is on . It also falls in cycle 5 as soon as is 0.
So the Mealy output is one clock cycle earlier. The price is that a Mealy output depends on the input, so if changes in the middle of a clock cycle, can change in the middle of the cycle too.
Try it: switch between Moore and Mealy, set , and clock twice. In the Mealy machine goes to 1 as soon as the second 1 is present (flip without clocking and watch follow it). In the Moore machine only changes after the clock edge.
Design of a counter: the sequential-circuit approach
In the counters chapter counters were built from cascaded stages with some gating logic. A counter is also just a synchronous FSM, so we can design one with the same steps. The specification:
- The count sequence is
- There’s an input , looked at in each clock cycle. If the count stays the same. If the count goes up by 1.
State diagram and state table for a modulo-8 counter
There is one state for each count: A is count 0, B is count 1, and so on up to H for count 7. The output is the count itself and depends only on the state, so this is a Moore machine.
| Present state | Next () | Next () | Output (count) |
|---|---|---|---|
| A | A | B | 0 |
| B | B | C | 1 |
| C | C | D | 2 |
| D | D | E | 3 |
| E | E | F | 4 |
| F | F | G | 5 |
| G | G | H | 6 |
| H | H | A | 7 |
State assignment
Eight states need three state variables . The simplest assignment is to encode each state with the number the counter should output in that state. Then the output signals are the same as the state variables, and no output logic is needed.
| State | Present | Next () | Next () | Count |
|---|---|---|---|---|
| A | 000 | 000 | 001 | 000 |
| B | 001 | 001 | 010 | 001 |
| C | 010 | 010 | 011 | 010 |
| D | 011 | 011 | 100 | 011 |
| E | 100 | 100 | 101 | 100 |
| F | 101 | 101 | 110 | 101 |
| G | 110 | 110 | 111 | 110 |
| H | 111 | 111 | 000 | 111 |
Next we pick the flip-flop type. They must be edge-triggered, so that only one transition happens per clock cycle.
Implementation using D flip-flops
Each next-state function goes to the input of flip-flop . K-maps of the table above give
The first one is just . Each equation is of the form “hold (, or with some lower bit 0) or toggle (all lower bits and are 1)”, which is the same toggle idea as the synchronous counters.
Example 8.5: Checking the D-flip-flop equations
Find the next state from with , and from with .
Show answerHide answer
- From , : . . . Next state 100. ✓
- From , : . . . Next state 010. ✓
Implementation using J-K flip-flops
J-K flip-flops are an attractive alternative, but now each flip-flop needs a and a function. What the inputs must be depends on the required transition, and one of the two inputs is often a don’t-care (it can be 0 or 1 and the transition is the same):
| Flip-flop must go | Why | ||
|---|---|---|---|
| (stay 0) | 0 | not set; reset or hold both keep 0 | |
| (set) | 1 | set or toggle both give 1 | |
| (stay 1) | 0 | not reset; set or hold both keep 1 | |
| (reset) | 1 | reset or toggle both give 0 |
Applying this to every row gives the excitation table for the counter. As an example, take the first row (): with the next state is also 000, so every flip-flop stays 0, giving , for all three. With the next state is 001, so flip-flops and still stay at 0 (, ) but goes , so , .
| Present | : , , | : next state | : , , |
|---|---|---|---|
| 000 | 0d, 0d, 0d | 001 | 0d, 0d, 1d |
| 001 | 0d, 0d, d0 | 010 | 0d, 1d, d1 |
| 010 | 0d, d0, 0d | 011 | 0d, d0, 1d |
| 011 | 0d, d0, d0 | 100 | 1d, d1, d1 |
| 100 | d0, 0d, 0d | 101 | d0, 0d, 1d |
| 101 | d0, 0d, d0 | 110 | d0, 1d, d1 |
| 110 | d0, d0, 0d | 111 | d0, d0, 1d |
| 111 | d0, d0, d0 | 000 | d1, d1, d1 |
(For the next state is always the present state, so only the J-K inputs are shown.)
This is a truth table with inputs and outputs . K-maps with the don’t-cares give
Every equals its , so each flip-flop is acting as a T flip-flop with a toggle condition of ” and all the lower bits are 1”. The circuit needs just two AND gates in total, and it extends to larger counters easily (stage gets one more AND input), while the D version needs bigger and bigger sum-of-products logic.
Example 8.6: Checking the J-K equations
Find the next state from with , and from with .
Show answerHide answer
- From , : , , . All three toggle, so 100. ✓
- From , : every , so all three hold, and the state stays 110. ✓
Key formulas
Moore and Mealy outputs
- : present state (flip-flop outputs); : present input
Number of state variables
Next state with D flip-flops
- : next-state function of state variable
J-K excitation
Moore 2-ones detector (states : A = 00, B = 01, C = 10)
Mealy 2-ones detector (, )
Modulo-8 counter with enable , J-K flip-flops
Common mistakes
- Putting the output on the wrong place in the diagram. A Moore output goes inside the state; a Mealy output goes on the arrow as
input / output. - Forgetting that a Moore output changes after the clock edge, so it comes one cycle later than the Mealy output for the same input.
- Mixing up (present state) and (next state). The next-state logic takes and in and puts out to the inputs.
- Leaving a state/input combination undefined. Every state has to have a next state for every input value.
- Forgetting the Reset. Without it the flip-flops power up in an unknown state, and the machine might start in the unused code (like 11).
- Using the minimum number of flip-flops but not treating the unused codes as don’t-cares, which makes the logic bigger than it needs to be.
- Using in the wrong J-K input. When a flip-flop is going or it’s that’s free; when going or it’s .
- Using level-sensitive latches for the state. The state needs edge-triggered flip-flops so that only one transition happens per clock cycle.