Lecture 3b Verilog FSM Examples
Lecture 3b Verilog FSM Examples
Examples
initial state
SECOND THIRD
!restart and !pause
– Gray 0
1
3'b000
3'b001
3'b000
3'b001
4'b0000
4'b0001
8'b00000001
8'b00000010
2 3'b010 3'b011 4'b0011 8'b00000100
– Johnson 3
4
3'b011
3'b100
3'b010
3'b110
4'b0111
4'b1111
8'b00001000
8'b00010000
5 3'b101 3'b111 4'b1110 8'b00100000
– One Hot 6
7
3'b110
3'b111
3'b101
3'b100
4'b1100
4'b1000
8'b01000000
8'b10000000
– Custom
• Your encoding selection may require more than
the minimally sufficient number of flip flops.
Finite State Machines
• Describe the state variables in Verilog.
• Provide a mechanism to force an initial state.
• Describe a function that maps inputs and
current state to a new, or next state.
– Literal transcription of excitation equations
– Behavioral description using case, if-else, etc…
• Some additional things to consider:
– Resets, synchronous or asynchronous?
– Unused states (error, or no resets) and recovery
Finite State Machines
• Describe it in Verilog just like the block diagram!
• I have selected a custom state encoding.
• Synchronous reset is
always_comb // combinational
begin
case(state)
FIRST: if (restart | pause) next_state = FIRST;
else next_state = SECOND;
SECOND: if (restart) next_state = FIRST;
else if (pause) next_state = SECOND;
implemented with logic
else next_state = THIRD;
THIRD: if (!restart & pause) next_state = THIRD;
else next_state = FIRST;
default: next_state = FIRST;
• Default clause covers the
one unused state
endcase
end
endmodule
THIRD:
else if (pause) state <= SECOND;
else state <= THIRD;
if (!restart & pause) state <= THIRD;
is made by sensitivity list
else state <= FIRST;
default: state <= FIRST;
endcase
end
• Default clause covers the
one unused state
end
endmodule
FIRST
• Suppose there are three even = 0
odd = 1
– odd terminal = 0
SECOND THIRD
– even even = 1
odd = 0 !restart and !pause
even = 0
odd = 1
terminal = 0
terminal = 0
Finite State Machines
• The “odd” output is asserted
in FIRST and THIRD.
• The “even” output is restart or pause
terminal = 0
is asserted to indicate
terminal = 0 terminal = 1
restart
terminal = 0
terminal = 0
Finite State Machines
• Outputs that require functions of only the
current state are Moore type outputs.
– This includes using state bits directly.
– Outputs “odd” and “even” are Moore outputs.
• Outputs that require functions of the current
state and the inputs are Mealy type outputs.
– Output “terminal” is a Mealy output.
• Consider the latency and cycle time tradeoffs.
Finite State Machines
• Describe the output functions in Verilog,
just as shown in the block diagram...
EXPLICIT OR IMPLICIT DESCRIPTION
NEXT
INPUTS
LOGIC VARIABLES LOGIC
always_comb // combinational
• Added three assignment
begin
case(state)
FIRST: if (restart | pause) next_state = FIRST;
else next_state = SECOND;
statements to create the
output functions.
SECOND: if (restart) next_state = FIRST;
else if (pause) next_state = SECOND;
else next_state = THIRD;
THIRD: if (!restart & pause) next_state = THIRD;
else next_state = FIRST;
default: next_state = FIRST;
endcase
end
endmodule
Finite State Machines
module fsm (state, odd, even, terminal, pause, restart, clk, rst);