0% found this document useful (0 votes)
6 views25 pages

Lecture 2-2 Model FSM 20250213

The document outlines the design of finite state machines (FSM) using Verilog and SystemVerilog, emphasizing behavioral modeling and coding styles. It discusses the differences between Moore and Mealy FSMs, implementation techniques, and best practices for coding styles to avoid common pitfalls. Additionally, it provides examples of FSM implementations and highlights the importance of using multiple always blocks for clarity and error reduction.

Uploaded by

Andy Yang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

Lecture 2-2 Model FSM 20250213

The document outlines the design of finite state machines (FSM) using Verilog and SystemVerilog, emphasizing behavioral modeling and coding styles. It discusses the differences between Moore and Mealy FSMs, implementation techniques, and best practices for coding styles to avoid common pitfalls. Additionally, it provides examples of FSM implementations and highlights the importance of using multiple always blocks for clarity and error reduction.

Uploaded by

Andy Yang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Outline

Part 2 (Behavior Verilog for design)


• Finite State Machine Design
• Behavior level modeling
• Tools and hardware specific
– Simulator and race conditions
• SystemVerilog for design
• Verilog Modeling Style for Synthesis
VLSI Signal Processing Lab.

• 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.

*without storage elements, the


CL circuits with feedback may
Become unstable! => oscillator

N Y C U . E E , Hsinchu, Taiwan
D Flip-Flop
• Input: D
• Output: Q
• Clock

• Q outputs a steady value


• changes Q to be D at clock edge
• Flip-flop stores state
• Allows sequential circuits to iterate
VLSI Signal Processing Lab.

*D-type FF is often exploited


as “register” due to simple connectivity; 一般數位電路不用latch
Latch is exploited as well but level sensitive; Why?
thus only half period is available.
N Y C U . E E , Hsinchu, Taiwan
Timing Diagram of Sequential Circuit
VLSI Signal Processing Lab.

輸入變,輸出就跟著變

輸入變,輸出等到
Clock edge 才變
N Y C U . E E , Hsinchu, Taiwan
CODING STYLE FOR FSM
VLSI Signal Processing Lab.

Source: C. Cummings, Synthesizable Finite State Machine Design Techniques


Using the New SystemVerilog 3.1 Enhancements
N Y C U . E E , Hsinchu, Taiwan
Moore v.s. Mealy
• Mealy and Moore FSM
– A Moore FSM is a state machine where the outputs are only a function
of the present state.
– A Mealy FSM is a state machine where one or more of the outputs is a
function of the present state and one or more of the inputs.
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.

• Compared to a Moore FSM, a Mealy FSM might...


– Be more difficult to conceptualize and design
– Have fewer states

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.

• Two Moore outputs rd=1


rd, ds (read & done-strobe) ds=0 ws=1
(rd=0 & ds=0 on reset)

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.

next state outputs


Next Present
Output
state State State
Logic
Logic FF's

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);

parameter IDLE = 2'b00,


READ = 2'b01, State assignment? Synopsys FSM compiler can help
DLY = 2'b11,
DONE = 2'b10;

logic [1:0] state, next;

always_ff@(posedge clk, negedge rst_n)


VLSI Signal Processing Lab.

if (!rst_n) state <= IDLE;


else state <= next;

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);

parameter IDLE = 2'b00,


READ = 2'b01,
DLY = 2'b11,
DONE = 2'b10;

logic [1:0] state, next; State variable

always_ff@(posedge clk, negedge rst_n)


VLSI Signal Processing Lab.

if (!rst_n) state <= IDLE; Remember to add reset state


else state <= next;

N Y C U . E E , Hsinchu, Taiwan
3. Next state/ always_comb begin //next state and output logic
next = 'bx;

output logic Use ’bx for logic min ds = 1'b0;


Default case rd = 1'b0;

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;

styles reg [1:0] state, next;

• Code the FSM design using two


always @(posedge clk or negedge rst_n)
if (!rst_n) state <= IDLE;

always blocks,
else state <= next;

– One for the sequential state register


always @(state or in1 or in2) begin
next = 2'bx; out1 = 1'b0;

– One for the combinational next-state


case (state)
IDLE : if (in1) next = BBUSY;
and combinational output logic. else next = IDLE;
BBUSY: begin
out1 = 1'b1;
VLSI Signal Processing Lab.

if (in2) next = BBUSY;


else next = BFREE;
end
//...
endcase
end

Combinational logic 和sequential logic 分開


N Y C U . E E , Hsinchu, Taiwan
One Always Block FSM Style (Avoid This Style!)
• One of the most common FSM
parameter [1:0]
IDLE=2'b00, BBUSY=2'b01,BFREE=2'b10;

coding styles in use today


reg [1:0] state;
always @(posedge clk or negedge rst_n)
if (!rst_n) begin
– It is more verbose state <= IDLE;
out1 <= 1'b0;
– It is more confusing end
else begin
– It is more error prone (comparable state <= 2'bx; out1 <= 1'b0;

two always block coding style)


case (state)
IDLE : if (in1) begin
state <= BBUSY;
out1 <= 1'b1;
end
VLSI Signal Processing Lab.

else state <= IDLE;


BBUSY: if (in2) begin
state <= BBUSY;
out1 <= 1'b1;
end
else state <= BFREE;
endcase

Combinational logic 和sequential logic 放在一起


N Y C U . E E , Hsinchu, Taiwan
One Always Block FSM Style (Avoid This Style!)
parameter [1:0]
1. A declaration is made for state. IDLE=2'b00, BBUSY=2'b01,BFREE=2'b10;
Not for next. reg [1:0] state;
always @(posedge clk or negedge
1

2. The state assignments do not rst_n) if (!rst_n) begin


state <= IDLE;
correspond to the current state of out1 <= 1'b0;
end
the case statement, but the state else begin
state <= 2'bx; out1 <= 1'b0;
that case statement is case (state)
IDLE : if (in1) begin
transitioning to. state <= BBUSY; 2
out1 <= 1'b1;
end
else state <= IDLE;
VLSI Signal Processing Lab.

BBUSY: if (in2) begin


• This is error prone (but it does state <= BBUSY;
out1 <= 1'b1;
work if coded correctly). end
else state <= BFREE;
endcase
end

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.

BBUSY: if (in2) begin


can be generated from a single state <= BBUSY;
synchronous always block. out1 <= 1'b1; 4
end
else state <= BFREE;
endcase
end

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

http://www.cnblogs.com/oomusou/archive/2011/06/14/verdi_fsm_state.html N Y C U . E E , Hsinchu, Taiwan


使用前,讀進*.fsdb檔後,FSM state信號,預設只會顯示
state encoding所代表的數值

若能顯示state名稱,相信可讀性更高,更方便debug
Tools –> Extract Interactive FSM…
VLSI Signal Processing Lab.

N Y C U . E E , Hsinchu, Taiwan

You might also like