Lecture 4 FSM
Lecture 4 FSM
Machines
1 9/18/2020
Modeling Finite State Machines (FSMs)
“Manual” FSM design & synthesis process:
1. Design state diagram (behavior)
2. Derive state table
3. Reduce state table
4. Choose a state assignment
5. Derive output equations
6. Derive flip-flop excitation equations
Steps 2-6 can be automated, given a state diagram
1. Model states as enumerated type
2. Model output function (Mealy or Moore model)
3. Model state transitions (functions of current state and inputs)
4. Consider how initial state will be forced
2 9/18/2020
FSM structure
Inputs Outputs
X Y
Combinational
Circuit
Memory
Elements
Clock
3 9/18/2020
Mealy Machine and Moore Machine
Mealy Machine
clock
Moore Machine
clock
4 9/18/2020
FSM example – Mealy model
0/0
Present Input x
X/Z 0 1
A state
A A/0 B/0
1/1 1/0 B A/0 C/1
0/0 C C/0 A/1
0/0
C B Next state/output
1/1
entity seqckt is
port ( x: in std_logic; -- FSM input
z: out std_logic; -- FSM output
clk: in std_logic ); -- clock
end seqckt;
5 9/18/2020
FSM example - behavioral model
architecture behave of seqckt is
type states is (A,B,C); -- symbolic state names
(enumerate)
signal state: states; --state variable
begin
6 9/18/2020
FSM example – state transitions
process (clk) – trigger state change on clock transition
begin
if rising_edge(clk) then -- change state on rising clock edge
case state is -- change state according to x
when A => if (x = ‘0’) then
state <= A;
else -- if (x = ‘1’)
state <= B;
end if;
when B => if (x=‘0’) then
state <= A;
else -- if (x = ‘1’)
state <= C;
end if;
when C => if (x=‘0’) then
state <= C;
else -- if (x = ‘1’)
state <= A;
end if;
end case;
end if;
end process;
7 9/18/2020
FSM example – alternative model
architecture behave of seqckt is
type states is (A,B,C); -- symbolic state names
(enumerate)
signal pres_state, next_state: states;
begin
-- Model the memory elements of the FSM
process (clk)
begin
if (clk’event and clk=‘1’) then
pres_state <= next_state;
end if;
end process;
entity FSM is
port (CLK, EN, TDI: in bit;
RST, SHIFT: out
bit);
end entity FSM;
12 9/18/2020
Write a VHDL code using three process blocks!
13 9/18/2020
How Verilog Explicit FSM Works
The nonblocking and blocking assignments are scheduled
in the same time step of the simulation in a particular order
1. The nonblocking assignments in the edge-sensitive behavior are
sampled first at the beginning of the time step (i.e. before any
assignments are made)
2. The blocking assignments in level-sensitive behavior are then
executed (with the previous register value because there is no
assignment done in Step 1)
3. After Step 2, the nonblocking assignments are completed by
assigning LHS variables with the values that were sampled at Step 1
Verilog Explicit FSM Design and Synthesis Tips
Use 2 cyclic behaviors for an explicit state machine
• One level-sensitive behavior for combinational logic to describe the
next state and output logic
• One edge-sensitive behavior for state flip-flops to synchronize state
transition
In the level-sensitive behavior for N/S and O/P
• Use blocked assignments/procedural assignments “=“
• Completely specify all outputs
Can be achieved by initializing all outputs in the beginning
In the edge-sensitive behavior for state transition
• Use nonblocking assignments “<=“
For state transition
For register transfer of a data path
Always decode all possible states in the level sensitive
behavior
• To avoid unnecessary latches
Decode All Possible States!
Matching simulation results between behavioral model and a
synthesized circuit does NOT guarantee that an implementation is
correct !
• Unless exercising all possible input sequences
Which is almost impossible to do
• Because, if the testbench exercises the circuit only allowable input
sequences, then it is not sufficient to verify the circuit’s behaviors that are
not covered by the exercise of the testbench
Verilog: Mealy Machine
17 9/18/2020
Verilog: Mealy Machine– Cont.
*Xilinx Documentation
18 9/18/2020
Verilog: Mealy Machine– Cont.
19 9/18/2020
Verilog: Moore Machine
20 9/18/2020
FSM Example: BCD-to-Excess-3 Code Converter (Mealy)
0 0000 0011
1 0001 0100 9’s complement
2 0010 0101 can be obtained by
3 0011 0110 inverting
4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
BCD-to-Excess-3 Code Converter (cont.)
Bin = 8 bcd Bout = 8Excess-3
LSB MSB MSB
Bin Bout
0 0 0 1 Excess-3
Code
t Converter 1 1 0 1 t
1 0 0 0 clk
+
0 0 1 1
1 0 1 1
MSB LSB
0/1 1/0
Bin(0) S_0
1/0
S_1 S_2
Bin(1)
0/1
0/0, 1/1
S_3 S_4
Bin(2) 0/1
0/0, 1/1 1/0
S_5 S_6
Bin(3)
0/0, 1/1 0/1
BCD-to-Excess-3 Code Converter (cont.)
module BCD_to_Excess_3b (B_out, B_in, clk, reset_b);
output B_out;
input B_in, clk, reset_b;
parameter S_0 = 3'b000, // State assignment, which may be omitted
S_1 = 3'b001, // If omitted, allow synthesis tool to assign
S_2 = 3'b101,
S_3 = 3'b111,
S_4 = 3'b011,
S_5 = 3'b110,
S_6 = 3'b010,
dont_care_state = 3'bx,
dont_care_out = 1'bx;
reg[2: 0] state, next_state;
reg B_out;
BCD-to-Excess-3 Code Converter (cont.)
always @ (posedge clk or negedge reset_b) // edge-sensitive behavior with NBAs
if (reset_b == 0) state <= S_0; else state <= next_state;
31 9/18/2020
Zero Detector: Mealy Machine
32 9/18/2020
Zero Detector: Mealy Machine
//Verilog 2001, 2005 syntax
module Mealy_Zero_Detector ( always @ (state, x_in) // Mealy output
output reg y_out, case (state)
input x_in, clock, reset S0: y_out = 0;
); S1, S2, S3: y_out = ~x_in;
reg [1: 0] state, next_state; endcase
parameter S0 = 2'b00, S1 = 2'b01,
S2 = 2'b10, S3 = 2'b11; endmodule
34 9/18/2020
Binary Counter: Moore Machine
Write a Verilog code for Binary Counter (Moore
Machine).
35 9/18/2020