Lecture 2-2 Model FSM 20250213
Lecture 2-2 Model FSM 20250213
• Gotchas
1
N Y C U . E E , Hsinchu, Taiwan
Digital Circuits and Systems
Lecture 2-2 Model Finite State Machine
Tian Sheuan Chang
VLSI Signal Processing Lab.
有了記憶,人生變彩色
N Y C U . E E , Hsinchu, Taiwan
SEQUENTIAL CIRCUIT
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
Sequential logic has state
Output = f(input)
Combinational Logic
Sequential Logic
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
D Flip-Flop
• Input: D
• Output: Q
• Clock
輸入變,輸出就跟著變
輸入變,輸出等到
Clock edge 才變
N Y C U . E E , Hsinchu, Taiwan
CODING STYLE FOR FSM
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
FSM Implementation: Moore/Mealy trade-off
• Remember that the difference is in the output:
– Moore outputs are based on state only
– Mealy outputs are based on state and input
– Therefore, Mealy outputs generally occur one cycle earlier than a Moore
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
Asynchronous Mealy
• Registered output to ease logic synthesis
Logic will not be minimized across module boundaries
unless specified in synthesis tools
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
FSM: Simple Moore Machine Example
• 4-State Moore State Machine
!rst_n go=0
• Asynchronous low-true reset IDLE
rst_n rd=0
ds=0
go=1
• Clock signal name
clk
DONE READ
rd=0 rd=1
• Two inputs ds=1 ds=0
go, ws (wait-state)
ws =0
DLY
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
Moore State Machine Two Always Blocks
• Next state and output logic use one always_comb block
• Present state FF use always_ff
Next State Clocked Present Output combinational
combinational logic State logic logic
inputs
combinational sequential combinational
logic logic logic
VLSI Signal Processing Lab.
clock
N Y C U . E E , Hsinchu, Taiwan
Two always block
always_combbegin //next state and output logic
next= 'bx;
rd =1'b0;
ds =1'b0;
case(state)
module fsm_cc1_2 IDLE: if (go) next = READ;
(output logic rd, ds, else next = IDLE;
input go, ws, clk, rst_n); READ : begin
//binary encoding rd = 1'b1;
parameter IDLE = 2'b00, next = DLY;
READ = 2'b01, end
DLY = 2'b11, DLY : begin
DONE = 2'b10; rd = 1'b1;
if (!ws) next = DONE;
logic [1:0] state, next; else next = READ;
end
always_ff@(posedge clk, negedge rst_n)
VLSI Signal Processing Lab.
DONE : begin
if (!rst_n) state <= IDLE; ds = 1'b1;
else state <= next; next = IDLE;
end
endcase
end
endmodule
N Y C U . E E , Hsinchu, Taiwan
1. State definition
module fsm_cc1_2
(output logic rd, ds,
input go, ws, clk, rst_n);
N Y C U . E E , Hsinchu, Taiwan
Why not use define? It’s a global varaible
`define IDLE 3'b000
`define B1 3'b001 `define IDLE 2'b00
"Re-definition" `define READ 2'b01
`define B2 3'b010
warnings! `define S2 2'b10
`define B3 3'b101
`define READ 3'b100
module fsm2 ( ... );
module fsm1 ( ... ); ...
Multiple FSMs with
... endmodule
the same state names Bad coding
endmodule style!!
IDLE IDLE
VLSI Signal Processing Lab.
READ B1
`define creates a
global definition
B3 B2 S2 READ
N Y C U . E E , Hsinchu, Taiwan
State definition: use parameter (local
variable)
module fsm1 ( ... ); module fsm2 ( ... );
... No state definition ...
parameter problems! parameter IDLE = 2'b00,
IDLE = 3'b000,
READ = 2'b01,
B1 = 3'b001,
S2 = 2'b10;
B2 = 3'b010, ...
Multiple FSMs with
B3 = 3'b101, endmodule
the same state names Good coding
READ = 3'b100;
... style!!
endmodule
IDLE IDLE
VLSI Signal Processing Lab.
READ B1
parameters create
local definitions
B3 B2 S2 READ
N Y C U . E E , Hsinchu, Taiwan
2. Present state DFF
module fsm_cc1_2
(output logic rd, ds,
input go, ws, clk, rst_n);
N Y C U . E E , Hsinchu, Taiwan
3. Next state/ always_comb begin //next state and output logic
next = 'bx;
case (state)
IDLE : if (go) next = READ;
else next = IDLE;
每個case item放不同state READ : begin
Test each state rd = 1'b1;
next = DLY;
end
DLY : begin
rd = 1'b1;
Test input conditions if (!ws) next = DONE;
else next = READ;
end
VLSI Signal Processing Lab.
DONE : begin
Output logic ds = 1'b1;
next = IDLE;
end
endcase
end
endmodule Assign next state output
N Y C U . E E , Hsinchu, Taiwan
Two Always Coding Styles
• One of the best Verilog coding
parameter [1:0]
IDLE=2'b00, BBUSY=2'b01,BFREE=2'b10;
always blocks,
else state <= next;
N Y C U . E E , Hsinchu, Taiwan
One Always Block FSM Style (Avoid This Style!)
3. There is just one sequential always parameter [1:0]
block, coded using nonblocking IDLE=2'b00, BBUSY=2'b01,BFREE=2'b10;
reg [1:0] state;
assignments. always @(posedge clk or negedge
rst_n) if (!rst_n) begin
4. All outputs will be registered state <= IDLE;
(unless the outputs are placed out1 <= 1'b0;
end
into a separate combinational else begin
always block or assigned using state <= 2'bx; out1 <= 1'b0;
case (state)
3
continuous assignments). IDLE : if (in1) begin
state <= BBUSY;
out1 <= 1'b1;
end
• No asynchronous Mealy outputs else state <= IDLE;
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
TLDR
• Use two always or three always coding style
– Two always: one for state DFF, one for next state and output
– Three always: one for state DFF, one for next state, one for output
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan
如何在DEBUSSY / VERDI顯示STATE名稱幫
VLSI Signal Processing Lab.
助DEBUG
若能顯示state名稱,相信可讀性更高,更方便debug
Tools –> Extract Interactive FSM…
VLSI Signal Processing Lab.
N Y C U . E E , Hsinchu, Taiwan