Lec 07
Lec 07
1
Outline
• FSM Examples
• Verilog Model
2
Sequential Circuit: FSM
outputs
a x
FSM
inputs
FSM
Combinational n1
b Circuit
n0
s1 s0
clk State register/ History/
Sequence
Clock Signal s
Next state = F1(current state, inputs)
Output = F2(current state)
5
Clocked synchronous FSM structure
• States: Determined by possible values in sequential storage
elements
• Transitions: Change of state
• Clock: Controls when state can change by controlling storage
Inputs Combinational Outputs
Logic
x
FSM
We often draw FSM
graphically, known as state
diagram
T D TQ’+T’Q
0 Q 0Q’+1Q
T D Q
0 Q’ 1Q’+0Q
CLK C Q’
D= T Q’ + T’ Q
Verilog Code for FSM of T-FF
//Code for Data Flipflop; will be used in FSM
implementation
module DFF( input wire D, input wire clk, input rst,
output reg Q)
always @ (posedge rst) begin
D Q
if (rst) Q=0;
end
CLK C Q’
always @(posedge clk) begin
if (!rst) Q <= D;
end
endmodule 25
Verilog Code for FSM of T-FF
module tff_fsm (input clk, input t, input rst, output q);
wire NS, S;
endmodule
Verilog Code for FSM of TFF: Test bench
module tff_fsm_test; initial begin
iCLK=0;
reg t, iCLK, rst; for(i=0;i<20; i++) begin
#10; iCLK = ~iCLK;
wire Q; end
end
integer i;
tff_fsm T0(iCLK, t, rst, Q); // Inst initial begin
//Part I $dumpfile("tff_fsm.vcd");
$dumpvars;
//Part II t = 0;rst=1;
initial #10 rst=0;
$monitor ("iCLK = %b, t = %b, Q = %b", #25 t = 1;
iCLK, t, Q); #30 t = 0;
end #25 t = 1;
endmodule #40 $finish;
end
Waveform T-FF
Q changes When T =1
FSM Example 3
JK-FF
Finite State Machine: JK FF
• Step1: English like statement
– When output reset when K=1, J=0, toggle when
JK=11, set to 1 when J=1, k=0 and no change for
JK=00
• Step2: Model the problem using FSM
– Two states: “Off” (S=0), and “On” (S=1)
• Transition on rising clock edge
– Arrow with no starting state points to
• initial state (when circuit first starts)
FSM for JK-FFState: 0, 1
Input : 00,
01 01,10,11
Output: 0,1
11
01 00
00
0 1 10
11
10
31
FSM for JK-FF
Step2: Model the problem using FSM State: 0, 1
Input : 00, 01,10,11
01 Output: 0,1
01 11
00
00
11 10
10
Step3,4,5: Trivial
32
FSM for JK-FF
Step 6: Derivation of Next State State: 0, 1
PS= Q(t) Input(JK) NS =Q(t+1) Input : 00, 01,10,11
0 00 0 Output: 0,1
0 01 0
0 10 1
Step 7: Derive for NS logic, OP logic
0 11 1 NS =JQ’(t)+K’Q(t); X=Q;
1 00 1
1 01 0
1 10 1
1 11 0 33
FSM Controller for JK-FF
Step 8: Realize FSM Q(t+1)=JQ’(t)+K’Q(t)
by Connecting the NS, OP logic, State Reg and
Clock
s1
Input=J JQ’(t)+K’Q(t)
Input=K
s1 Q(t) n0 Q(t+1)
clk D-FF
34
FSM Controller for JK-FF
Same as : Given a D FF, Construct JK FF
K
D Q
J
CLK C Q’
D= JQ’(t)+K’Q(t)
Verilog Code for FSM of JK-FF
module jkff_fsm (input clk, input j, s1
input k, input rst, output q);
wire NS, S;
endmodule clk
D-FF
Verilog Code for FSM of JKFF: Test bench
module tff_fsm_test; initial begin
iCLK=0;
reg j, k, iCLK, rst; for(i=0;i<20; i++) begin
#10; iCLK = ~iCLK;
wire Q; end
end
integer i;
initial begin
jkff_fsm JK0(iCLK, j, k, rst, Q); // Inst
$dumpfile(“jkff_fsm.vcd");
//Part I $dumpvars;
//Part II j = 0; k=0; rst=1;
initial #10 rst=0;
$monitor ("iCLK = %b, j = %b, k=%b, Q = %b", #21 j = 1; k =1;
iCLK, j, k, Q); #32 j = 0; k =1;
end #23 j = 1; k =0;
endmodule #42 $finish;
end
Waveform T-FF
01 00
00
0 1 10
10
11 11
X 40
FSM for RS-FF
Step 6: Derivation of Next State State: 0, 1
PS= Q(t) Input(RS) NS =Q(t+1) Input : 00, 01,10,11
0 00 0 Output: 0,1
0 01 0
0 10 1 Q(t+1)=R+Q(t).S’
0 11 x
1 00 1
1 01 0
1 10 1
1 11 x
41
FSM Controller for RS-FF
Step 8: Realize FSM
by Connecting the NS, OP logic, State Reg and Clock Same as
Given a D FF, Construct
s1 RS FF
Q(t+1)=R+Q(t).S’
Input=R R+Q(t).S’
Input=S
s1 Q(t) n0 Q(t+1)
clk D-FF
42
FSM Example 5
(General : Two bit binary Counter)
Two bit counter: 00,01,10,11,repeat
• Step1: Want 00, 10, 10, 11, repeat
– Each value for one clock cycle
• Step2: Can describe as FSM: Four states,
Transition on rising clock edge to next state
State bits = Output bits
A B C D
00 01 10 11
FSM of Counter : 2 bit
Step 3: Represent the FSM in Tabular Form I/P O/P
s1 s0 n0 n1
Step 4: Derive number of FF require
Four States: 2 FF : Obvious A 0 0 0 1
Step 5: State Encoding B 0 1 1 0
State A: 00 State B: 01, C 1 0 1 1
State C: 10, State D:11 B 1 1 0 0
s1
Combinational s0
logic n1 Step 6: Derivation of Next
n0 State
s1 s0
clk State register
FSM Controller: Binary Counter
A B C D Step 8: Realize FSM
00 01 10 11 by Connecting the NS, OP logic, State
Reg and Clock
Step 7: Derive for NS logic, OP logic
NS/OP : n1 =S1 xor S0; n0=s0’ n1 = s1 xor s0
n0 = s0’ s1
I/P O/P s0
s1 s0 n0 n1
A 0 0 0 1
B 0 1 1 0 s1 s0 n0 n1
C 1 0 1 1 clk State register
B 1 1 0 0
Verilog Code for FSM of 2 bit CTR
module ctr2bit_fsm (input clk, input rst, output q1, output q0);
wire NS0, NS1, NS2, S0, S1, S2;
0 1 2 3 0 1 2 0
Counting
RST=1
RST=1
FSM Example 6
(General : sequence generator )
FSM Example: 0,1,1,1,repeat
• Step1: Want 0, 1, 1, 1, 0, 1, 1, 1, ...
– Each value for one clock cycle
• Step2: Can describe as FSM: Four states,
Transition on rising clock edge to next state
clk
State O ff On1 On2 On3 O ff On1 On2 On3 O ff
Outputs:
x
FSM Example: 0,1,1,1,repeat
• Step1: Want 0, 1, 1, 1, 0, 1, 1, 1, ...
– Each value for one clock cycle
• Step2: Can describe as FSM: Four states, Transition on
rising clock edge to next state
Outputs: x
clk^ clk^ clk^
x=0 x=1 x=1 x=1
Off On1 On2 On3
clk^
FSM Example 7
(General Example: Three-Cycles High
Laser Timer)
Extend FSM to Three-Cycles High Laser Timer
• Step1: Specify the problem in English like Stmts
– Four states: Wait in “Off” state while b is 0 (b’)
– When b=1 (& rising clock edge), transition to On1
• Sets X=1
– On next two clock edges, transition to On2, then On3,
which also set x=1
– So x=1 for three cycles after button pressed
Extend FSM to Three-Cycles High Laser Timer
clk
Inputs:
b
State Off Off Off Off Off On1 On2 On3 Off
Out put s:
x
Specify: FSM of Three-Cycles High Laser Timer
Step2: Model the problem using FSM
Inputs: b; Outputs: x
x=0 clk^
Off b’*clk^
b*clk^ clk^ clk^
x=1 x=1 x=1
On1 On2 On3
FSM Simplification: Rising Clock Edges Implicit
• Showing rising clock on every transition: cluttered
• Make implicit -- assume every edge has rising clock
• What if we wanted a transition without a rising edge
– Asynchronous FSMs -- less common, and advanced topic
– We consider synchronous FSMs
– All transition on rising edge
Extend FSM to Three-Cycles High Laser Timer
Inputs: b; Outputs: x
x=0 clk^
Off b’*clk^
b*clk^ clk^ clk^
x=1 x=1 x=1
On1 On2 On3
Note: Transition with no associated condition thus transitions to next state on next clock cycle
Extend FSM to Three-Cycles High Laser Timer
Inputs: b; Outputs: x
x=0 clk^
Off b’*clk^
b*clk^ clk^ clk^
x=1 x=1 x=1
On1 On2 On3
Note: Transition with no associated condition thus transitions to next state on next clock cycle
Extend FSM to Three-Cycles High Laser Timer
Inputs: b; Outputs: x
x=0
Off b’
b
x=1 x=1 x=1
On1 On2 On3
Note: Transition with no associated condition thus transitions to next state on next clock cycle
FSM Implementation
Step 4: Derive number of FF require Step 5: State Encoding
Four States: 2 FF 0ff: 00 On1: 01
On2: 10 on3: 11
Inputs: b; Outputs: x Step 6: Derivation of Next State
x=0
PS b NS X
Off b’
00 0 00 0
b 00 1 01 0
x=1 x=1 x=1 01 x 10 1
On1 On2 On3 10 x 11 1
11 x 00 1
FSM Implementation
PS b NS X Step 8: Realize FSM
00 0 00 0 by Connecting the NS, OP logic, State Reg and
Clock
00 1 01 0
b X=P1+P0 X
01 x 10 1 N1=P0 xor P1
N0
10 x 11 1 N0=P1’ P0+b. P0
N1
11 x 00 1
P1 P0
Step 7: Derive for NS logic, OP logic
N1=P0 xor P1
CLK
N0=P1’ P0+b. P0 D1 D0
X=P1+P0
Once we specify FSM for a
problem/system
=== >
Implementation is not
difficult
FSM Example 8:
Parity Encoder
FSM Example 8: Parity Encoder
• Input: 1 or 0 // entering as stream
• Out put: output a 1 when total number of 1 is even
0
Reset Even [0]
Output=0’=1
1 1 Input
Odd [1] Output is
T- FF : designed dependent only
using D-FF 0 on current state
FSM Example 9:
Button Press Synchronizer
FSM : Button Press Synchronizer
• English Language Specification
• We want simple sequential circuit
– Converts button press to single cycle duration
– Regardless of length of time that button actually pressed
S0 S1 S2 S3
clk ^ 70
FSM Example 10-E1: Sequencegenerator
• Generate 4 bit integer output sequence
– X= 0, A, 7, 5….repeat
Outputs: X
X=0 X=A X=5
clk ^ clk ^ X=7 clk ^
S0 S1 S2 S3
clk ^
71
FSM Example 10-E2: Sequence generator
• Generate 8 bit integer output sequence
– X= 72, 53, 11, 192,….repeat
Outputs: X
X=72 clk ^ X=53 clk ^ X=11 clk ^ X=192
S0 S1 S2 S3
clk ^
72
FSM Example 10-E2: Sequence generator
• Generate 8 bit integer output sequence
– X= 72, 53, 11, 192,….repeat
72 53 11 192
Outputs: X
X=72 X=53 X=1 X=192
clk^ clk ^ clk^
1
2 bit MUX
S0 S1 S2 S3
CTR 4x1
clk^ X
A 0 0 0 1
B 0 1 1 0
s1 s0 n0 n1
C 1 0 1 1
clk State register
B 1 1 0 0
FSM Example 10-E2: Sequence generator
• Generate 8 bit integer output sequence
– X= 72, 53, 11, 192,….repeat
A B C D
Outputs: X
X=72 X=53 X=1 X=192
clk^ clk ^ clk^
1
2 bit MUX
S0 S1 S2 S3
CTR 4x1
clk^ X
A,B,C,D values can stored in 4 register
(With Mux it act as memory)
FSM Output logic
Simpler to Implement
may be Simpler than
Output logic: Eight 2
Register and Mux
input binary function 75
Thanks
76