Laboratory Exercise 7: Finite State Machines
Laboratory Exercise 7: Finite State Machines
Part I
We wish to implement a finite state machine (FSM) that recognizes two specific sequences of applied input sym-
bols, namely four consecutive 1s or four consecutive 0s. There is an input w and an output z. Whenever w = 1 or
w = 0 for four consecutive clock pulses the value of z has to be 1; otherwise, z = 0. Overlapping sequences are
allowed, so that if w = 1 for five consecutive clock pulses the output z will be equal to 1 after the fourth and fifth
pulses. Figure 1 illustrates the required relationship between w and z.
Clock
A state diagram for this FSM is shown in Figure 2. For this part you are to manually derive an FSM circuit that
implements this state diagram, including the logic expressions that feed each of the state flip-flops. To implement
the FSM use nine state flip-flops called y8 , . . . , y0 and the one-hot state assignment given in Table 1.
1
Reset
A/0
w=0 w=1
1
B/0 0 F/0
w=0 1
1 0
C/0 G/0
w=0 1 0 1
D/0 1 0 H/0
w=0 1
0 E/1 I/1 1
State Code
Name y8 y7 y6 y5 y4 y3 y2 y1 y0
A 000000001
B 000000010
C 000000100
D 000001000
E 000010000
F 000100000
G 001000000
H 010000000
I 100000000
2
4. Simulate the behavior of your circuit.
5. Once you are confident that the circuit works properly as a result of your simulation, download the circuit
into the FPGA chip. Test the functionality of your design by applying the input sequences and observing
the output LEDs. Make sure that the FSM properly transitions between states as displayed on the red LEDs,
and that it produces the correct output values on LEDR9 .
6. Finally, consider a modification of the one-hot code given in Table 1. It is often desirable to set all flip-flop
outputs to the value 0 in the reset state.
Table 2 shows a modified one-hot state assignment in which the reset state, A, uses all 0s. This is accom-
plished by inverting the state variable y0 . Create a modified version of your Verilog code that implements
this state assignment. (Hint: you should need to make very few changes to the logic expressions in your
circuit to implement the modified state assignment.)
7. Compile your new circuit and test it.
State Code
Name y8 y7 y6 y5 y4 y3 y2 y1 y0
A 000000000
B 000000011
C 000000101
D 000001001
E 000010001
F 000100001
G 001000001
H 010000001
I 100000001
Part II
For this part you are to write another style of Verilog code for the FSM in Figure 2. In this version of the code you
should not manually derive the logic expressions needed for each state flip-flop. Instead, describe the state table
for the FSM by using a Verilog case statement in an always block, and use another always block to instantiate
the state flip-flops. You can use a third always block or simple assignment statements to specify the output z. To
implement the FSM, use four state flip-flops y3 , . . . , y0 and binary codes, as shown in Table 3.
State Code
Name y3 y2 y1 y0
A 0000
B 0001
C 0010
D 0011
E 0100
F 0101
G 0110
H 0111
I 1000
3
A suggested skeleton of the Verilog code is given in Figure 3.
module part2 ( . . . );
. . . define input and output ports
. . . define signals
reg [3:0] y_Q, Y_D; // y_Q represents current state, Y_D represents next state
parameter A = 4’b0000, B = 4’b0001, C = 4’b0010, D = 4’b0011, E = 4’b0100,
F = 4’b0101, G = 4’b0110, H = 4’b0111, I = 4’b1000;
4
6. In step 3 you instructed the Quartus Synthesis tool to use the state assignment given in your Verilog code.
To see the result of removing this setting, open again the Quartus settings window by choosing Assign-
ments > Settings, and click on the Compiler Settings item on the left side of the window, then click
on the Advanced Settings (Synthesis) button. Change the setting for State Machine Processing from
User-Encoded to One-Hot. Recompile the circuit and then open the report file, select the Analysis and
Synthesis section of the report, and click on State Machines. Compare the state codes shown to those
given in Table 2, and discuss any differences that you observe.
Part III
The sequence detector can be implemented in a straightforward manner using shift registers, instead of using the
more formal approach described above. Create Verilog code that instantiates two 4-bit shift registers; one is for
recognizing a sequence of four 0s, and the other for four 1s. Include the appropriate logic expressions in your
design to produce the output z. Make a Quartus project for your design and implement the circuit on your DE-
series board. Use the switches and LEDs on the board in a similar way as you did for Parts I and II and observe
the behavior of your shift registers and the output z. Answer the following question: could you use just one 4-bit
shift register, rather than two? Explain your answer.
5
Part IV
In this part of the exercise you are to implement a Morse-code encoder using an FSM. The Morse code uses pat-
terns of short and long pulses to represent a message. Each letter is represented as a sequence of dots (a short
pulse), and dashes (a long pulse). For example, the first eight letters of the alphabet have the following represen-
tation:
A •—
B —•••
C —•—•
D —••
E •
F ••—•
G ——•
H ••••
Design and implement a Morse-code encoder circuit using an FSM. Your circuit should take as input one of
the first eight letters of the alphabet and display the Morse code for it on a red LED. Use switches SW2−0 and
pushbuttons KEY1−0 as inputs. When a user presses KEY1 , the circuit should display the Morse code for a letter
specified by SW2−0 (000 for A, 001 for B, etc.), using 0.5-second pulses to represent dots, and 1.5-second pulses
to represent dashes. Pushbutton KEY0 should function as an asynchronous reset.
A high-level schematic diagram of a possible circuit for the Morse-code encoder is shown in Figure 5.
Half-second counter
6
Copyright
c 1991-2016 Intel Corporation. All rights reserved. Intel, The Programmable Solutions Company,
the stylized Intel logo, specific device designations, and all other words and logos that are identified as trademarks
and/or service marks are, unless noted otherwise, the trademarks and service marks of Intel Corporation in the
U.S. and other countries. All other product or service names are the property of their respective holders. Intel
products are protected under numerous U.S. and foreign patents and pending applications, mask work rights, and
copyrights. Intel warrants performance of its semiconductor products to current specifications in accordance with
Intel’s standard warranty, but reserves the right to make changes to any products and services at any time without
notice. Intel assumes no responsibility or liability arising out of the application or use of any information, product,
or service described herein except as expressly agreed to in writing by Intel Corporation. Intel customers are ad-
vised to obtain the latest version of device specifications before relying on any published information and before
placing orders for products or services.
This document is being provided on an “as-is” basis and as an accommodation and therefore all warranties, rep-
resentations or guarantees of any kind (whether express, implied or statutory) including, without limitation, war-
ranties of merchantability, non-infringement, or fitness for a particular purpose, are specifically disclaimed.