Verilog Programing - ch3 - FSM - Handouts
Verilog Programing - ch3 - FSM - Handouts
Module Unit
Topics Ref. Hrs.
No. No.
4 Finite State Machines 1,2 2
4.1 Verilog code for both Mealy & Moore FSM
Mealy model
• State register: state variable
• Next state function: determines the next state (combinatorial logic)
• Output function: generates outputs
—Moore: based on the state register
—Mealy: based on the state registers and the current inputs
2 Moulding Engineers Who Can Build the Nation
Finite State Machine
A Finite State Machine is described by:
l Its states and a description of their physical meaning.
l The way the machine makes the transitions to the next state. These must be based on
the present state and/or the present inputs only. The state is the collective contents of
all Flip-flops; The state memory is a set of n flip-flops that store the current state of
the machine, and it has 2n distinct states.
l The outputs from each state.
Mealy versus Moore FSM
Moore outputs depend only on the state of the machine. Moore machines
are easier to design.
Mealy outputs depend on the present state and the inputs. Mealy machines
usually have fewer states and thus are often smaller.
//state flip-flops
reg [2:0] state, next_state;
//state definitions
parameter S0=2’b00 S1=2’b 01, S2=2’b 10, S3=2’b11,…
// State machine descriptions
//Next state calculations
always @(state or….)
begin
case (state)
…..
end
//register or flip-flop calculation
always@(posedge clk)
state<=next_state
//Output calculations
Output=f(state, inputs)
RY
FSM example-1
always @ (*)
case(state_reg)
module light( Input clk, rst, output reg [2:0] led); RED: begin
if (timer == 0)
next_state <= RY;
parameter RED = 2’b00; else
parameter RY = 2'b01; next_state <= R;
parameter GREEN = 2’b10; end
RY: begin
parameter YELLOW = 2’b11; if (timer == 0)
next_state <= GREEN; else
reg [15:0] timer; next_state <= RY;
end
reg [1:0] state_reg; YELLOW: begin
reg [1:0] next_state; if (timer == 0)
next_state <= RED;
else
always @ (posedge clk) next_state <= YELLOW; end
if (rst) GREEN: begin
state_reg = RED; if (timer == 0)
next_state <= YELLOW;
else else
state_reg = next_state; next_state <= GREEN;
end default:
next_state <= 3'bxxx;
endcase
Accelerate
SLOW MEDIUM
Brake
Accelerate
Accelerate
Brake Brake
~Brake
STOP FAST
A Better Way!
• We keep a separate control part where the next state is calculated.
• The other part generates the output from the next state.
module fsm_car_speed_2(clk, keys, brake, accelerate, speed); fast:
input clk, keys, brake, accelerate;
if(brake)
output [1:0] speed; slow: newspeed = mdium;
reg [1:0] speed; if(brake) else
reg [1:0] newspeed; newspeed = stop; newspeed = fast;
else if(accelerate) default:
parameter stop = 2'b00, newspeed = mdium;
slow = 2'b01, newspeed = stop;
else endcase
mdium = 2'b10,
newspeed = slow; end
fast = 2'b11;
mdium:
always @(keys or brake or accelerate or speed)
if(brake) always @(posedge clk or negedge keys)
begin
case(speed) newspeed = slow; begin
stop: else if(accelerate) if(!keys)
if(accelerate) newspeed = fast; speed = stop;
newspeed = slow; else else
else newspeed = mdium; speed = newspeed;
newspeed = stop; end
endmodule
12 Moulding Engineers Who Can Build the Nation
Two Ways!