Ee 705-Vlsi Design Lab: Describing A Finite State Machine (FSM)
Ee 705-Vlsi Design Lab: Describing A Finite State Machine (FSM)
ROLL-143070072
ELECTRICAL ENGINEERING
MICROELECTRONICS
IIT BOMBAY
Describing an FSM
Characteristics:
Input symbol set: {p,q}
Output symbol set: {M, N}
The machine outputs M if the input sequence seen thus far (including the
current input) is of the form ppp..pqqq..q, where the number of input ps
and number of qs are both even numbers.
Description:
The FSM should detect the sequence ppp..pqqq..q, where the number of input ps
and number of qs are both even numbers. The minimum detectable sequence is ppqq.
So, we need the following states: (starting state), p,pp,ppq,ppqq. We also need a state
where the FSM will end up in case of invalid sequences. We call that state dead.
Whenever there is a break in the build up to the required sequence, we send the FSM to
the dead state. The dead state can only be recovered using a reset signal.
The states and transitions:
a) State This is the starting state. On receiving input p, it moves to state p but on
receiving input q, it moves to state dead as its an invalid input stream.
b) State p- On receiving input p, it moves to state pp but on receiving input q, it
moves to state dead, as pq is an invalid stream.
c) State pp- On receiving input pp, it moves to state p but on receiving input q, it
moves to state ppq.
d) State ppq - On receiving input ppq, it moves to state p but on receiving input
dead, as it becomes an invalid stream, but on receiving q, it moves to state ppqq,
e) State ppqq - On receiving input p, it moves to state dead but on receiving input
q, it moves to state ppq.
f) State dead This is the state where the system ends up when an invalid input stream
occurs. On receiving input p or q, it remains in the dead state. Only a reset input
can move the system away from this state to state .
The outputs:
a) M When a valid input stream occurs ppppqqqq., output M occurs. Only a
transition from ppq to ppqq, will produce this output M. This will signify a
successful detection.
b) N In case of any other input stream, unsuccessful detection is signified by output
N.
State Diagram:
p/N
p/N
p/N
p
q/N
reset/N
q/N
pp
p/N
ppq
q/N
q/M
q/N
dead
reset/N
ppqq
p/N
p,q/N
X=p
p
pp
p
dead
dead
dead
Next State
X= q
dead
dead
ppq
ppqq
ppq
dead
X= Reset
X=p
N
N
N
N
N
N
Output
X= q
N
N
N
M
N
N
X= Reset
N
p
N
pp
N
ppq
N
Ppqq
N
dead
N
VHDL organization of DUT:
i)
Package Definition: We define a package in order to introduce new data types - i)
fsm_state ii)input_symbol, iii) output_symbol and the fsm component.
-----------------------------------------------------------package FsmPackage is --Package definition
type fsm_state is (s_phi, s_p, s_pp, s_ppq, s_ppqq,s_dead); -States defined
type input_symbol is (reset, p,q); --Inputs defined
type output_symbol is (M,N); --Outputs defined
component exp_fsm is --Component defined specifying input and
output ports and clock
port (X: in input_symbol;
Y: out output_symbol;
clk: in bit);
end component;
end FsmPackage;
-------------------------------------------------------------
ii)
Architecture and Process: We define a signal state, of type fsm_state which would
denote the current state. Signals denoting next state(nstate), present state(pstate) and
variable denoting the output(vY) is defined.
------------------------------------------------------------architecture fsm_behave of exp_fsm is
signal pstate: fsm_state; --Signal to store Present State
signal nstate: fsm_state; --Signal to store Next State
begin
FSM: process(X,pstate)
variable vY: output_symbol; --Variable to store output-------------------------------------------------------------
The start of the FSM process is stated by assigning current state to next state and N to Y.
------------------------------------------------------------nstate <= pstate;
vY := N;
-------------------------------------------------------------
Now using case command, we have realized the state transition table. For example, if the
current state is (s_phi), then on receiving input p, it moves to state p(s_p), or on
receiving input q, it moves to state dead (s_dead), else it stays in state . In case of
same state transition, there is no need to assign new state.
------------------------------------------------------------case pstate is
when s_phi =>
if(X = p) then
nstate <= s_p;
elsif(X =q) then
nstate <= s_dead;
else
nstate <= s_phi;
end if;
-------------------------------------------------------------
In this way, all the cases of the state transition table has been specified.
And when there is a correct sequence detected, output variable vY is assigned M.
At the end of case sequence, we apply vY to the output signal
------------------------------------------------------------if(X = reset) then
Y <= N;
else
Y <= vY;
end if;
end process FSM;
-------------------------------------------------------------
State transitions happen on the event of a clock, specifically a rising edge. So, we define
a process which causes the state transitions when rising edge of clock arrives.
------------------------------------------------------------STATE_TRAN: process
begin
wait until clk'event and clk = '1';
-------------------------------------------------------------
Let the reset transitions from each state be named as reset , reset p, reset pp and so on.
Number
Symbol
Current State
Next state
Transition
completed
0
1
2
3
4
5
reset
p
p
p
q
reset
p
pp
p
dead
p
pp
p
dead
Reset (0)
1
2
3
6
8
Expected
output
stream
N
N
N
N
N
N
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
p
p
q
p
reset
p
p
q
q
q
q
p
p
q
reset
q
reset
p
reset
p
p
reset
p
p
q
reset
p
p
q
q
reset
p
pp
ppq
dead
p
pp
ppq
ppqq
ppq
ppqq
dead
dead
dead
dead
p
pp
p
pp
ppq
p
pp
ppq
ppqq
p
pp
ppq
dead
p
pp
ppq
ppqq
ppq
ppqq
dead
dead
dead
dead
p
pp
p
pp
ppq
p
pp
ppq
ppqq
1
2
4
5
8
1
2
4
10
11
10
9
12
13
8
7
8
1
Reset p
1
2
Reset pp
1
2
4
Reset ppq
1
2
4
10
Reset ppqq
N
N
N
N
N
N
N
N
M
N
M
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
N
M
N
We define the clock of period 10 ns, with 50% duty cycle using the following process:
--------------------------------------------------------------SIM: process --Process to generate clock
begin
if end_of_sim = false then --Apply Clock
clk<='0';
wait for 5 ns;
clk<='1';
wait for 5 ns;
else
wait;
end if;
end process SIM;
-------------------------------------------------------------
The end_of_sim is a shared variable defined to stop the vhdl program when the test input stream ends. It
is assigned a default value false so that the clock generation starts. However in the next process, it is set
to true when the input stream ends.
And for the 16 input symbols, we match the expected output with the output produced by our FSM at
each rising edge of the clock.
Points to Note:
We have applied the input synchronously with the clock. However, a special characteristic of
Mealy machine is that the output changes asynchronously whenever input changes. We have not
shown that characteristic in this experiment.
Reporting of output:
We report all the transitions that takes place during the processing of the input stream using the
following piece of code:
-------------------------------------------------------------------------------
Here, pstate stores the present state, nstate stores the next state, X and Y are the input and output
symbols.
Sample output:
WAVE:
ZOOM2(90ns-180ns)
ZOOM3(180ns-270ns)
ZOOM4(270ns-360ns)