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

CircuitOutput depends on…Example
Combinationalthe present inputs onlydecoder, adder
Storagethe previous state, not the present inputsflip-flop
Sequentialthe 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:

  1. The flip-flops hold the present state .
  2. The next-state logic uses and to compute the next state .
  3. At the active clock edge, the flip-flops load .
  4. 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
000
100
010
111

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:

StateRemembered information
Ano consecutive 1s detected yet (also the reset state)0
Bone 1 has been detected0
Ctwo or more consecutive 1s have been detected1

Figure 8.1: State diagram of the Moore sequence detector. The output z is written inside each state. 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 stateNext state ()Next state ()Output
AAB0
BAC0
CAC1

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
EdgeState after after
start–A0
11B0
21C1
31C1
40A0
51B0
60A0

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.

Interactive Moore state diagram (open this page on the site to use it).

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
00A
01B
10C
11unused

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)00010
01 (B)00100
10 (C)00101
11ddddd

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
EdgeNew
111001 (B)0
210110 (C)1
310110 (C)1
400000 (A)0

This matches Example 8.1, so the circuit really implements the state table.

The full design procedure

StepDesign activityMain result
1Read and clarify the specificationinputs, outputs, timing requirements
2Identify the states and transitionsstate diagram
3Record the transitions for every input casestate table
4Minimize the number of states if possiblereduced state table
5Choose state variables and assign codesstate-assigned table
6Choose the flip-flop type, derive next-state and output logicBoolean equations
7Implement and verify the circuitgate-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.

MooreMealy
output shown in the state circleoutput shown on the transition arrow
output changes only after the clock edgeoutput 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. Figure 8.2: State diagram of the Mealy sequence detector. Each arrow is labelled input / output.

Present stateNext state ()Next state ()Output
AAB0
BAB

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
Cycle123456
011100
Moore state (during the cycle)AABCCA
Moore 000110
Mealy state (during the cycle)AABBBA
Mealy 001100
  • 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.

Interactive Moore and Mealy state diagrams (open this page on the site to use it).

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 stateNext ()Next ()Output (count)
AAB0
BBC1
CCD2
DDE3
EEF4
FFG5
GGH6
HHA7

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.

StatePresent Next () Next () Count
A000000001000
B001001010001
C010010011010
D011011100011
E100100101100
F101101110101
G110110111110
H111111000111

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 goWhy
(stay 0)0not set; reset or hold both keep 0
(set)1set or toggle both give 1
(stay 1)0not reset; set or hold both keep 1
(reset)1reset 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: , ,
0000d, 0d, 0d0010d, 0d, 1d
0010d, 0d, d00100d, 1d, d1
0100d, d0, 0d0110d, d0, 1d
0110d, d0, d01001d, d1, d1
100d0, 0d, 0d101d0, 0d, 1d
101d0, 0d, d0110d0, 1d, d1
110d0, d0, 0d111d0, d0, 1d
111d0, d0, d0000d1, 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.