FSMs
FSMs
Vasudeva Murthy T
1
Session Objectives
2
Session Topics
• FSMs
• Functions
• Tasks
• Automatic
• UDPs
• Specify Blocks
3
Finite State Machines
4
Moore FSM
• Output Is a Function of Present State Only
• Describe Outputs as Concurrent Statements Depending on State
Only
Transition state 1/
Inputs function output 1
Next State Present State transition
condition 1
Memory
(register)
transition
condition 2
Output Outputs state 2/
function output 2
5
Mealy FSM
• Output Is a Function of a Present State and Inputs
• Describe Outputs as Concurrent Statements Depending on State
and Inputs
state 2
6
Moore vs. Mealy FSM
7
Moore FSM - Example 1
Moore FSM that Recognizes Sequence 10
0 1
0
1
S0 / 0 S1 / 0 1 S2 / 1
reset 0
S0: No
Meaning S1: “1” S2: “10”
elements
of states: observed observed
of the
sequence
observed
8
Mealy FSM - Example 1
Mealy FSM that Recognizes Sequence 10
S0 S1
reset 0/1
S0: No S1: “1”
Meaning elements observed
of states: of the
sequence
observed
9
Moore & Mealy FSMs – Example 1
clock
0 1 0 0 0
input
S0 S1 S2 S0 S0
Moore
S0 S1 S0 S0 S0
Mealy
10
Finite string pattern recognizer (step 1)
• Finite string pattern recognizer
– one input (X) and one output (Z)
– output is asserted whenever the input sequence …010… has
been
observed, as long as the sequence …100… has never been seen
X: 0 0 1 0 1 0 1 0 0 1 0 …
Z: 0 0 0 1 0 1 0 1 0 0 0 …
X: 1 1 0 1 1 0 1 0 0 1 0 …
Z: 0 0 0 0 0 0 0 1 0 0 0 …
11
Finite string pattern recognizer (step 2)
S2 S5
[0] [0]
0 0
S3 S6 0 or 1
[1] [0]
12
Finite string pattern recognizer
• Exit conditions from state S3: have
recognized …010
– if next input is 0 then have …0100
= ...100 (state S6)
– if next input is 1 then have …0101
= …01 (state S2) reset S0
[0]
• Exit conditions from S1: recognizes 0 1
strings of form …0 (no 1 seen) S1 S4
0 1
– loop back to S1 if input is 0 [0] ...0 ...1 [0]
• Exit conditions from S4: recognizes 1 0
strings of form …1 (no 0 seen) S2 S5
– loop back to S4 if input is 1 [0] [0]
...01
0 1 0
S3 S6 0 or 1
...010 [1] ...100 [0]
13
Finite string pattern recognizer
• S2 and S5 still have incomplete transitions
– S2 = …01; If next input is 1,
then string could be prefix of (01)1(00)
14
Finite string pattern recognizer
15
Finite string pattern recognizer
• Review of process
– understanding problem
• write down sample inputs and outputs to understand
specification
– derive a state diagram
• write down sequences of states and transitions for sequences
to be recognized
– minimize number of states
• add missing transitions; reuse states as much as possible
– state assignment or encoding
• encode states with unique patterns
– simulate realization
• verify I/O behavior of your state diagram to ensure it
matches specification
16
Implementation
•Implementation
– NS0 = CS0Q2’ + CS2’X + CS1CS2X’ + CS0X’
– NS1 = CS1X’ + CS0CS2 + CS0CS1 + CS2X
– NS2 = CS0’CS2’X’ + CS1’CS2’X’ + CS0’CS1’X’
– Z = CS0’CS1CS2
CS0 CS1 CS2 X NS0 NS1 NS2
0 0 0 0 0 0 1
0 0 0 1 1 0 0 P010
0 0 1 0 0 0 1
0 0 1 1 0 1 0
0 1 0 0 0 1 1 P100
0 1 0 1 1 0 0 Reset
0 1 1 0 1 1 0 R R
0 1 1 1 0 1 0
X D Q D Q
1 0 0 0 1 0 1
1 0 0 1 1 0 0
1 0 1 0 1 1 0 Clk
1 0 1 1 0 1 0
1 1 0 0 1 1 0
1 1 0 1 1 1 0
17
1 1 1 0 X X X
Complex counter
18
Complex counter (state diagram)
reset S0 0 S1 0 S2 0 S3 0 S4 0 S5 0 S6 0 S7
[000] [001] [010] [011] [100] [101] [110] [111]
1
1 1 1
1
1 1 1
19
Complex counter (state encoding)
module string (clk, M, rst, Z0, Z1, Z2); always @(posedge clk) begin
input clk, X, rst; if rst state = S0;
output Z0, Z1, Z2; else
case (state)
parameter S0 = [0,0,0]; S0: state = S1;
parameter S1 = [0,0,1]; S1: if (M) state = S3 else state = S2;
parameter S2 = [0,1,0]; S2: if (M) state = S6 else state = S3;
parameter S3 = [0,1,1]; S3: if (M) state = S2 else state = S4;
parameter S4 = [1,0,0]; S4: if (M) state = S0 else state = S5;
parameter S5 = [1,0,1]; S5: if (M) state = S4 else state = S6;
parameter S6 = [1,1,0]; S6: if (M) state = S7 else state = S7;
parameter S7 = [1,1,1]; S7: if (M) state = S5 else state = S0;
endcase
reg state[0:2];
end
assign Z0 = state[0];
assign Z1 = state[1]; endmodule
assign Z2 = state[2];
20
State Encoding Problem
• State Encoding Can Have a Big Influence on Optimality of the
FSM Implementation
– No methods other than checking all possible encodings are known to
produce optimal circuit
– Feasible for small circuits only
• Binary (Sequential) – States Encoded as Consecutive Binary
Numbers
– Small number of used flip-flops
– Potentially complex transition functions leading to slow implementations
• One-Hot – Only One Bit Is Active
– Number of used flip-flops as big as number of states
– Simple and fast transition functions
– Preferable coding technique in FPGAs
21
State Encoding Example
22
RTL Design Components
Data Inputs Control Inputs
Datapath Control
Circuit Circuit
Data Outputs
• Datapath circuits : Provides all necessary resources and interconnects
among them to perform specified task
• Examples of Resources - Adders, Multipliers, Registers, Memories,
etc.
• Control Circuit : Controls data movements in operational circuit by
switching multiplexers and enabling or disabling resources
• Follows Some ‘Program’ or Schedule
• Usually Implemented as FSM
23
Moore Machine — 111 Detector
Q2
D1 Q1
z
reset
x
Q1
D2 Q2
Q2’
reset
Clock
Reset
24
Verilog Organization for FSM
Q2
D1 Q1
z
reset
x
Q1
D2 Q2
Q2’
reset
Clock
Reset
25
Verilog Behavioral Specification
endmodule
26
Verilog code for 11 seq FSM
27
Generic FSM Diagram
Mealy
Inputs
Next State Outputs
Output
Logic Logic
State Register
Next State
Current State
FF
28
State Diagram
Inputs a and b are 0,
unless specified otherwise
a=0 b=0 Outputs Y and Z are 0,
unless specified otherwise.
reset = 1 b = 1/ Z = 1
S2
29
State Diagram: Mealy!
reset = 1 b = 1/ Z = 1, Y=1
S2
30
Mealy – Verilog
31
Mealy – Verilog ….
//next state logic
always@(state, a , b)
case (state)
S0: if (a) next_state = S1;
else next_state = S0;
S1: if (b) next_state = S2;
else next_state = S1;
S2: next_state = S0;
default: next_state = 2’bx;
endcase
32
Mealy – Verilog ….
//output logic
always@(state, a, b) begin
Z = 0, Y = 0; // avoids latches
case (state)
S0: if (a) Z = 1;
S1: begin
Y = 1;
if (b) Z = 1;
end
S2: ;
default :
begin
Y = 1’bx;
Z = 1’bx;
end
endcase
endmodule
33
Mealy FSM
Inputs Outputs
Next State and
Output Logic
State Register
Current State
FF
34
Mealy – Example 2
module fsm_mealy2 (output reg Y, Z, input clk, reset, a, b);
35
Mealy – Example 2
// next state & output logic S1: begin
always@(state, a, b) Y = 1;
begin if (b)
begin
Y = 0; Z = 0;
next_state = S2;
case (state)
Z = 1;
S0: if (a)
end
begin else
next_state = next_state = S1;
S1; end
Z = 1; S2: next_state = S0;
end default: next_state = 2’bx;
else endcase
next_state = S0; endmodule
36
Registered Mealy FSM
Output
Register
Inputs Outputs
FF
Next State and
Output Logic
State Register
Current State
FF
37
Registered Mealy FSM…
For higher level modeling, easier to debug
Output Registers
Registered
Inputs Outputs
FF
Next State, Datapath
& Output Logic
Combinational
Outputs
State & Datapath Registers
Current State
FF
38
State Diagram: Moore
a=0 b=0
Inputs a and b are 0,
S0 S1 unless specified otherwise
a=1
Y=1
Outputs Y and Z are 0,
unless specified otherwise.
reset = 1 b=1
S2
Z=1
39
Moore FSM
Inputs
Next State Output Outputs
Logic Logic
State Register
Next State
Current State
FF
40
Verilog for Moore
module fsm_moore (output reg Y, Z, input clk, reset, a, b);
always@(posedge clk)
begin
if (reset)
state <= S0; // using non-blocking since sequential
else
state <= next_state; //continued on next slide
end
41
Verilog for Moore …………..
//next state logic //output logic
always@(state , a, b) always@(state)
case (state) begin
Z = 0, Y = 0; // avoids latches
S0: if (a) next_state = S1;
case (state)
else next_state = S0; S0: ;
S1: if (b) next_state = S2; S1: Y = 1;
else next_state = S1; S2: Z = 1;
S2: next_state = S0; default:
begin
default: next_state = 2’bxx;
Y = 1’bx;
endcase Z = 1’bx;
end
endcase
end
endmodule
42
Functions & Tasks
43
Functions
• A procedure used in any expression
• Declared and referenced within a module
• Can be written on RHS on any equation
• Must have at least one input
• Has no outputs but returns a single value
• Can not call a task
• Can not contain timing controls
• Models combinational logic
• Can call other functions and itself
• Function name is implicitly declared return variable
• Type and range of return value can be specified
– default is 1 -bit
44
Function Example
module and_func
(output [3:0] regx; functions can be called
input [3:0] in1, in2); from within procedural
blocks or continuous
assign regx = myandfunc(in1,in2); assignment statements
function [3:0] myandfunc(input [3:0] in1,in2);
myandfunc=(in1 & in2);
endfunction
more than one statement in
endmodule function block can be grouped with
begin and end
Note : The subsequent statements of a function call will not be executed unless
always@(a ,b)
bitoper(aand, aor, axor, a,b);
endmodule
M.S Ramaiah School of Advanced Studies - Bangalore 48
Task – Count # of 1’s
module bitcnt ( task countones(input [7:0] rega,
output reg [3:0] count);
input [7:0] data;
reg [7:0] temp_reg;
output reg [3:0] bitcount;
begin
count=0;
always @ (data)
temp_reg=rega;
countones(data,bitcount);
while (temp_reg)
begin
count=count +temp_reg[0];
temp_reg=temp_reg >> 1;
end
end
endtask
endmodule
M.S Ramaiah School of Advanced Studies - Bangalore 49
Re-entrant Functions and Tasks
• Function and task variables are static in nature
• Recursive functions/task calls will overwrite value of previous
variable
• To avoid the overwriting functions/ tasks should be declared
automatic
• Example
endmodule