Professional Documents
Culture Documents
ECEn 220
Fundamentals of Digital Systems
© 2018 BYU
Components of an FSM
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
Output Outputs
Forming
Logic
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
1. State memory
Output Outputs
• Flip-flops to hold the current state
Forming
• Sequential Logic
Logic
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
1. State memory
Output Outputs
• Flip-flops to hold the current state
Forming
• Sequential Logic
Logic
2. Input Forming Logic (IFL)
• Logic to determine the next state of the FSM
• Combinational Logic
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
1. State memory
Output Outputs
• Flip-flops to hold the current state
Forming
• Sequential Logic
Logic
2. Input Forming Logic (IFL)
• Logic to determine the next state of the FSM
• Combinational Logic
3. Output Forming Logic (OFL)
• Logic to determine FSM outputs
• Combinational logic
© 2018 BYU ECEn 220 5
SystemVerilog FSMs
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
Output Outputs
Forming
Logic
if (reset)
reset ns = s0;
S0
else
Xin case (cs)
s0: if (!Xin)
ns = s1;
s1: if (Xin)
S3 Z S1 Xin ns = s2;
s2: if (Xin)
Xin ns = s3;
Xin else
ns = s1;
S2 Xin s3: Z = 1’b1; // Moore
endcase
end
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
Output Outputs
always_comb Forming
begin Logic
end
A Sequence Detector
ns = cs;
Z = 0;
Xin if (reset)
ns = s0;
else
case (cs)
reset s0: if (!Xin)
S0
ns = s1;
Xin s1: if (Xin)
ns = s2;
s2: if (Xin)
ns = s3;
S3 Z S1 Xin else
ns = s1;
Xin s3: Z = 1’b1;
Xin endcase
end
S2 Xin
Assign Z=(cs == S3) ? 1’b1 : 1’b0;
Use dataflow for Moore output
always_ff @(posedge clk)
cs <= ns;
© 2018 BYU 11
typedef enum {S0, S1, S2, S3} StateType;
A Better Sequence
StateType ns, cs;
always_comb
Detector begin
ns = cs;
Z = 0;
if (reset)
reset Xin ns = S0;
else
Xin case (cs)
S0 S0: if (!Xin) ns = S1;
Xin Xin S1: if (Xin) ns = S2;
S2: if (Xin) ns = S3;
else ns = S1;
Xin S3: begin
Z S3 S1 Xin Z = 1’b1;
if (!Xin) ns = S1;
else ns = S0;
Xin Xin
end
endcase
S2 Xin
end
always_ff @(posedge clk)
cs <= ns;
© 2018 BYU 12
State Transition and Output
Current
State State
Next Memory
Input State
Inputs D Q
Forming D Q
Logic
Output Outputs
Forming
Logic
Defensive Coding
stateType ns, cs;
Style always_comb
begin
Xin ns = ERR;
Z = 0;
if (reset)
ns = s0;
reset else
S0 case (state)
Xin s0: if (!Xin) ns = s1;
else ns = s0;
s1: if (Xin) ns = s2;
else ns = s1;
Xin / Z S1 Xin s2: if (Xin)
begin
ns = s0;
Xin Z = 1; // Mealy output
end
S2 Xin
else ns = s1;
endcase
end
S2 Xin
always_ff @(posedge clk)
case (current_state)
S0: if (Xin == 1’b0) current_state <= S1
S1: if (Xin == 1’b1) current_state <= S2
S2: if (Xin == 1’b1) current_state <= S3
else current_state <= S1;
S3: if (Xin == 1’b0) current_state <= S1
else current_state <= S0;
endcase
endmodule
CLK
Xin
current
S0 S0 S1 S1 S2 S3 S1 S2
_state
CLK
Xin
current
S0 S0 S1 S1 S2 S3 S1 S2
_state