You are on page 1of 3

Contemporary Logic Design, 2 Ed.

Spring 2008 Katz and Borriello

A Simple Vending Machine


To illustrate the basic design procedure, we will advance through the implementation of a
simple finite state machine that controls a vending machine. Here is how the control is
supposed to work. The vending machine delivers an item after it has received 15 cents in
coins. The machine has a single coin slot that accepts nickels and dimes, one coin at a
time. A mechanical sensor indicates whether a dime or a nickel has been inserted into the
coin slot. The controller's output causes a single item to be released down a chute to the
customer.

We will make two additional assumptions to further simplify our specification. First, we
will design our machine so it does not give change. A customer who pays with two
dimes will lose the five extra cents! Second, we will expect our machine to be reset
before each new use. This might be accomplished through a separate mechanism the
customer uses to select the item to be purchased.

Coin N Gum
Vending- Open
Sensor D Machine Release
FSM Mechanism
Reset

Clk

Figure 7.35 Vending-machine block diagram

Understanding the Problem


The first step in the finite state machine design process is to understand the problem.
Start by drawing a block diagram to understand the inputs and outputs. Figure 7.35 is a
good example. Let's assume that N is asserted for one clock period when a nickel is
inserted into the coin slot and that D is asserted when a dime has been deposited.
Furthermore, we'll postulate that it is enough if the machine asserts Open for one clock
period to release an item after 15 cents (or more) has been deposited since the last reset.
It is important to list these assumptions, as they will need to be verified later with other
members of the vending machine design team.

The specification may not completely define the behavior of the finite state machine. For
example, what happens if someone inserts a penny into the coin slot? Sometimes we have
to make reasonable assumptions. We'll assume that the coin sensor is designed so that it
returns any coins it does not recognize, leaving N and D unasserted. As you can see,
these assumptions can have significant implications for how other parts of the vending
machine will have to be designed and implemented. In a design team, it is crucial to have
a process that gets these assumptions aired early and keeps track of them as the design
progresses.

Copyright 2005, Pearson Education Inc. 1


Contemporary Logic Design, 2 Ed. Spring 2008 Katz and Borriello

Abstract Representations
Once you understand the behavior reasonably well, it is time to map the specification into
a more suitable abstract representation. A good way to begin is by enumerating the
possible unique sequences of inputs or configurations of the system. These will help
define the states of the finite state machine.

For this problem, it is not too difficult to enumerate all the possible input sequences that
lead to releasing an item:

• Three nickels in sequence: N, N, N


• Two nickels followed by a dime: N, N, D
• A nickel followed by a dime: N, D
• A dime followed by a nickel: D, N
• Two dimes in sequence: D, D

Reset
S0

N D

S1 S2

N D N D

S3 S4 S5 S6
[Open] [Open] [Open]

N D

S7 S8
[Open] [Open]

Figure 7.36 Initial vending-machine state diagram

This can be represented as a state diagram, as shown in Figure 7.36. For example, the
machine will pass through the states S0, S1, S3, S7 if the input sequence is three nickels.

To keep the state diagram simple and readable, we include only transitions that explicitly
cause a state change (in other words, we leave out the self-arcs). For example, in state S0
if neither input N or D is asserted, we assume the machine remains in state S0 (the

Copyright 2005, Pearson Education Inc. 2


Contemporary Logic Design, 2 Ed. Spring 2008 Katz and Borriello

specification allows us to assume that N and D are never asserted at the same time).
Also, we include the output Open only in states in which it is asserted. Open is implicitly
unasserted in any other state.

State Minimization
This nine-state description isn't the "best" possible. For one thing, since states S4, S5, S6,
S7 and S8 have identical behavior, they can be combined into a single state.

To reduce the number of states even further, we can think of each state as representing
the amount of money received so far. For example, it shouldn't matter whether the state
representing 10 cents was reached through two nickels or one dime.

A state diagram derived in this way is shown in Figure 7.37. We capture the behavior in
only four states, compared with nine in Figure 7.36. Also, as another illustration of a
useful shorthand, notice the transition from state 10¢ to 15¢. We interpret the notation
"N, D" associated with this transition as "go to state 15¢ if N is asserted OR D is
asserted " and could have also written it as N + D.

Reset 0¢

5¢ D

D 10¢

15¢
[Open]

Figure 7.37 Minimized vending-machine state diagram.

Copyright 2005, Pearson Education Inc. 3

You might also like