You are on page 1of 38

Lecture 16 --- State Machines

"The question of whether a computer


can think is no more interesting than
the question of whether a submarine
can swim.“

Edsger Dijkstra--- Computer Scientist


State Machine
• Goes by several names
– State Machine
– Finite State Machine (FSM)
– Controller
• A somewhat complex counter that can/will count
out of order depending on other signals from the
rest of the system
– The count value is often referred to as the state
• VHDL allows us to define the state machine in an
abstract way
– We should code for readability
State Machine Example

• The vending machine


– Keeps track of how much
money the user has put into
the machine
– Dispenses an item once
sufficient money is input
– Returns change (difference
between amount paid and
the price of item)
Vending Machine State Machine
• Let’s keep things simple:
– Vending machine only dispenses 1 product
– Product in the vending machine costs $1
– Vending machine only accepts quarters ($0.25), dimes
($0.10), and nickels ($0.05)
– Once a input amount reaches or exceeds $1, then the
item is immediately dispensed, and change is returned
– User can choose to return money at any time

• Note:
– The maximum dollar amount input is $1.20
Vending Machine State Machine

rec_coin

Initially, we will be in the receiving coins state. In a


real vending machine, this is where the machine
would wait for coins to be dropped in by the user.
Vending Machine State Machine

money_in >= target

rec_coin

If the total amount of money we’ve collected from a user


of the machine (money_in) meets or exceeds the
target (100 == $1), then we can move to the next state.
Vending Machine State Machine

money_in >= target

false rec_coin

But if it doesn’t, we
stay in the rec_coin
state.
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod

Once the money_in meets or exceeds the


target, we can enter the “dispense product”
or “disp_prod” state. A real vending
machine would release the desired product
from the holding area and drop it into a bin for
the user to collect.
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod

Once the product is dispensed, subtract disp_quarter


target (100) from money_in and begin
returning coins using the largest allowable
denomination possible first, the quarter (25).
This returns the money that the user overpaid.
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod

Until the money left, money_in, is less than a


quarter, stay in the disp_quarter state,
dispensing a quarter (and decrementing
money_in by 25) until the conditional
statement evaluates true. false

disp_quarter
money_in < 25
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod

When money_in < 25 happens, move to


the disp_dime state, which functions the
same as disp_quarter except the purpose
is to dispense dimes (10).
money_in < 10
false

false disp_dime disp_quarter


money_in < 25
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod


Move to the
disp_nickel state when
money_in = 0
the money remaining is less
than 10 to dispense nickels
false disp_nickel (5). Once money_in goes
to zero, we are back in the
state where we are receiving
money_in < 10
coins from a customer. false

false disp_dime disp_quarter


money_in < 25
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod

return_money = ‘1’
money_in = 0 Now add the capability for the user to get
their money back without purchasing a
product, equivalent to the “coin return”
false disp_nickel
function on a real vending machine

money_in < 10
false

false disp_dime disp_quarter


money_in < 25
Vending Machine State Machine

money_in >= target

false rec_coin disp_prod


0 1
return_money = ‘1’
money_in = 0

false disp_nickel
4
money_in < 10
false

false disp_dime disp_quarter


money_in < 25
3 2

Assign numbers to each state so we can enumerate them in VHDL


vending_machine.vhd
In VHDL, we define a state
machine (state_type) as
a new type. Then, we can
define 2 signals of the type,
“state” and
“nxt_state”. We can
think of these as the Q-
outputs and D-inputs of the
D-type flip flops used to
create the machine. All 5
states are represented in
what is essentially an
identifiers enumerated data type
Control signals are typical
outputs of a state machine
We need a temporary location to
store the result of adding or
removing coin values from the
current money in the machine
Initializing control signals at the
beginning of the state machine is
good coding practice

Assigning
control signals
to outputs

2-process state
machine
consists of
register portion
(i.e., flip-flops)

And the combinational


logic portion

vending_machine.vhd
vending_machine.vhd

The rest of the design is data


handling, or processes that
use the control outputs of
the state machine to make
the system functional.

In this case, we need a


process to keep track of the
money in the system
test_vending_machine.vhd

Download
vend_input.csv
from Isidore
test_vending_machine.vhd
Simulation Results
Simulation Results
More on State Machines…
• The state machine is the heart and soul of a VHDL
logic module (VHDL synthesizable file)
• Any module of sufficient complexity should use a
state machine as its controller
– One state machine per VHDL module
• Because of the abstraction allowed in VHDL, the
state machine is an excellent way to make your
code readable (or unreadable, for that matter)
– So, name your states and control signals carefully
User Interface with Sequential Circuits

• We interface with digital systems through


mechanical keys, buttons, and switches.
• Mechanical switches may take several milli-
seconds to cleanly transition from one state to
another (i.e. 0 to 1)
• 1 ms corresponds to 50000 clock cycles of a
50MHz clock
Example of Switch Bounce
“bounce”
De-bouncing Circuitry
• We want to add a delay (5-10 ms) from input
switches to the rest of our design
– This allows for the switch to bounce during
transition, and not affect the rest of the design

De-bouncing
switch_in switch_db
Circuitry

clk
debounce.vhd

Edge detector for the input


signal

input_change

in_follow

Lead FF Follow FF
switch_in in_lead
D Q D Q
D
clk Q clk Q
rst rst

reset
clk
debounce.vhd

Counter starts counting


once we encounter and
edge at the input

Once we wait for


“maxcount” clock cycles,
we transfer the input signal
to the output
test_debounce.vhd
Simulation Results

Glitches in the input don’t transfer to the output


Run for at least 10000 ns
vend_demo.vhd
• VHDL code to instantiate
vending_machine.vhd onto the DE2 board
– Use 7-seg displays for the output (i.e. money)
– Use LEDs for the other outputs (quarter_out,
dime_out, nickel_out, product)
– Use switches for the inputs (quarter_in,
dime_in, nickel_in)
– Use push-buttons for reset and return_money
• De-bouncing circuitry might not be required
– Implement both options on your DE2 board. Do you
notice a difference?
vend_demo entity

vending_machine
design is a component
inside of vend_demo
hex_to_7_seg and
debounce are also
components of
vend_demo

constants: we can
use or not use debounce
circuitry, and we can
change the length of
time the LEDs turn on

D
Counters to keep track
of LED “on” time

signals used for


switch edge detection

De-bounced inputs

Instantiations of the
vending_machine
and hex_to_7_seg
components
Use of 2 generate
statements allow us the
flexibility to use the de-
bounce circuitry or not.
You should try both!

D
Edge detection circuitry
for 4 input switches
Processes will allow for LEDs to light up for 1 second
sec_count = 50,000,000

nickel
quarter count
count process
process

product
dime count
count process
process
Pin Assignments

product_out

reset
quarter_out, quarter_in,
out_seg_1, out_seg_0 dime_out, dime_in, return_money
nickel_out nickel_in
How to Operate
Displays current Turns on Turns on Turns on Lights up green for
money in vending for 1s for for 1s for for 1s for 1s when product is
machine as a hex each each dime each nickel dispensed
value quarter dispensed dispensed
dispensed

Toggle
once per Toggle Toggle once
quarter once per per nickel Initiates coin Resets vending
dime input input return. LEDs above quarter, machine to
input
($0.10) ($0.05) dime, nickel switches will state 0
($0.25)
illuminate for 1s per coin
Lab 11 Time
Compile and Simulate:
vending_machine.vhd,
test_vending_machine.vhd
debounce.vhd,
test_debounce.vhd

Implement on your DE2 board:


vend_demo.vhd
What I Need to See
• vend_demo loaded onto DE-115 board
– Load 4 quarters (4 x $0.25 = $1.00) into vending
machine, see product dispensed
– Verify that 5 nickels (5 x $0.05 = $0.25) and 2 dimes
and one nickel (2 x $0.10 + $0.05 = $0.25) separately
result in the same value as a quarter ($0.25)
– Load 1 nickel, 4 quarters ($0.05 + 4 x $0.25 = $1.05)
into vending machine, see product dispensed ($1.00)
and 1 nickel ($0.05) dispensed
– Load 2 quarters, 1 nickel (2 x $0.25 + $0.05 = $0.55)
into vending machine, press coin return button, verify
2 quarters and one nickel dispensed. The quarter LED
will light for 2s and the nickel light will be on for 1s

You might also like