Digital Design with the Verilog HDLChapter 6: FSM with Verilog Dr.. Explicit State Machines• Declare registers to store explicit states • Combination logic circuit controls states • Veri
Trang 1Digital Design with the Verilog HDL
Chapter 6: FSM with Verilog
Dr Phạm Quốc Cường
Trang 2Explicit State Machines
• Declare registers to store explicit states
• Combination logic circuit controls states
• Verilog:
– Edge-trigger behaviour synchronizing the states
– Level-trigger behaviour describing the next states and
output logic
2
Trang 3Mealy machine vs Moore machine
Block Diagram of a Mealy sequential machine
Trang 4BCD to Excess-3 Converter - FSM
Next state/Output table
State
Next state/output
input
S_0 S_1 S_2 S_3 S_4 S_5 S_6
S_1/1 S_3/1 S_4/0 S_5/0 S_5/1 S_0/0 S_0/1
S_2/0 S_4/0 S_4/1 S_5/1 S_6/0 S_0/1
-/-State transition graph
State transition table
4
Trang 5BCD to Excess-3 Converter - Verilog
moduleBCD_to_Excess3(B_out, B_in, clk, reset);
inputB_in, clk, reset;
outputB_out;
parameter S_0 = 3’b000, //state encoding
S_1 = 3’b001, 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;
regB_out;
always@(posedge clk, negedge reset) //edge-trigger behaviour
if(reset == 1’b0) state <= S_0; elsestate <= next_state;
Trang 6BCD to Excess-3 Converter - Verilog
always@(state, B_in) begin
B_out = 0;
case(state) S_0: if(B_in == 1’b0) begin
next_state = S_1;
B_out = 1’b1; end else if(B_in == 1’b1) next_state = S_2;
S_1: if(B_in == 1’b0) begin
next_state = S_3;
B_out = 1’b1; end else if(B_in == 1’b1) next_state = S_4;
S_2: … S_3: … S_4: … S_5: … S_6: …
endcase end
6
Trang 7Synthesized Circuit
Phát biểu case không đủ tất cả các trường hợp
Trang 8BCD to Excess-3 Converter - Verilog
always@(state, B_in) begin
B_out = 0;
case(state) S_0: if(B_in == 1’b0) begin
next_state = S_1;
B_out = 1’b1; end else if(B_in == 1’b0) next_state = S_2;
S_1: if(B_in == 1’b0) begin
next_state = S_3;
B_out = 1’b1; end else if(B_in == 1’b0) next_state = S_4;
S_2: … S_3: … S_4: … S_5: … S_6: …
default: next_state = dont_care_state;
endcase end
8
Trang 9Synthesized Circuit
Trang 10Sequence Recognizer: Mealy
module Seq_Rec_3_1s_Mealy (output D_out, input D_in, En, clk, reset);
parameter S_idle = 0, S_0 = 1, S_1 = 2, S_2 = 3; // Binary code
reg [1: 0] state, next_state;
always @ (negedge clk)
if (reset == 1) state <= S_idle; else state <= next_state;
always @ (state, D_in, En) begin
case (state)
S_idle: if ((En == 1) && (D_in == 1)) next_state = S_1; else
if ((En == 1) && (D_in == 0)) next_state = S_0;
else next_state = S_idle;
S_0: if (D_in == 0) next_state = S_0; else
if (D_in == 1) next_state = S_1; else next_state = S_idle;
S_1: if (D_in == 0) next_state = S_0; else
if (D_in == 1) next_state = S_2; else next_state = S_idle;
S_2: if (D_in == 0) next_state = S_0; else
if (D_in == 1) next_state = S_2; else next_state = S_idle;
default: next_state = S_idle;
endcase
end
assign D_out = ((state == S_2) && (D_in == 1 )); // Mealy output
endmodule
Recommended Style!
Trang 11Sequence Recognizer: Mealy –
Synthesized Circuit
reset_b
clk
En
D_out
dffrpb_a
inv_a
D_in
esdpupd
inv_a
aoi21_a
dffrpb_a
dffrpb_a
nor2_a
aoi211_a
and2i_a
and3_a
Trang 12Sequence Recognizer: Mealy –
Synthesized Circuit
12
Trang 13Sequence Recognizer: Moore
module Seq_Rec_3_1s_Moore (output D_out, input D_in, En, clk, reset);
parameter S_idle = 0, S_0 = 1, S_1 = 2, S_2 = 3, S_3 = 4;
reg [2: 0] state, next_state;
always @ (negedge clk)
if (reset == 1) state <= S_idle; else state <= next_state;
always @ (state or D_in) begin next_state = S_idle;
case (state)
S_idle: if ((En == 1) && (D_in == 1)) next_state = S_1;
else if ((En == 1) && (D_in == 0)) next_state = S_0;
// else next_state = S_idle; // Remove!
S_0: if (D_in == 0) next_state = S_0;
else if (D_in == 1) next_state = S_1; // else next_state = S_idle;
S_1: if (D_in == 0) next_state = S_0;
else if (D_in == 1) next_state = S_2; // else next_state = S_idle;
S_2, S_3: if (D_in == 0) next_state = S_0; else if (D_in == 1) next_state = S_3;
// else next_state = S_idle;
default: next_state = S_idle; // Why not 3'bx?
endcase
end
assign D_out = (state == S_3); // Moore output
endmodule
Prevent accidental
latches!
Trang 14Sequence Recognizer: Moore Synthesize Circuit
CSE/EE 40462 FSMs, Datapath
reset
D_in
En
D_out
esdpupd
aoi211_a inv_a
dffrpb_a dffrpb_a
nor2_a
dffrpb_a
and2i_a
aoi211_a inv_a
inv_a
nand2_a inv_a nor2_a
mux2_a nand2_a
inv_a
clk
Trang 15Mealy machine vs Moore machine
Block Diagram of a Mealy sequential machine
Trang 16Simulation Results
16
Valid output Mealy
glitch Mealy
glitch
Trang 17Registered Output
Trang 18Registered Output
18
Mealy Type
Moore Type