CH 03
CH 03
Digital Design
Chapter 3:
Sequential Logic Design
• In combinational circuits, we don’t have the concept of internal state. In other words, they
don’t need any memory to remember their internal state.
• In sequential circuits, we have the concept of internal state. In other words, they MUST
need some memory to remember their internal state.
• As combinational circuits don’t need any memory, so they only contain logic gates such
as and gate, or gate, not gate
• As sequential circuits MUST need memory, so they MUST contain logic gates (such as
and gate, or gate, not gate) as well as some memory element
• An simple example of memory element is flipflop. A single flipflop stores a single bit.
• The combination of flipflops is called a Register.
• It means that sequential circuits MUST have registers in addition to the logic gates.
D D D D
Q Q Q Q
Clk R R R R
Rst
Q3 Q2 Q1 Q0
Testbench
initial begin
Rst_s <= 1;
I_posedge s <= 4'b0000;
• Simulation results @(Clk_s); // the first positive edge
#5 Rst_s <= 0;
– Note that Q_s updated only I_s <= 4'b0000;
on rising clock edges @(posedge Clk_s); // the second positive edge
#5 Rst_s <= 0;
– Note Q_s thus unknown until I_s <= 4'b1010;
first clock edge @(posedge Clk_s); //the third positive edge
#5 Rst_s <= 0;
• Q_s is reset to “0000” on I_s <= 4'b1111;
first clock edge end
vldd_ch3_Reg4TB.v
Clk_s
Rst_s
Q_s
outputs
inputs
B X FSM
FSM
FSM
• State register
Combinational
• Combinational logic logic
– HDL model will reflect those two State
parts
Clk State register
StateNext
Verilog for Digital Design
Copyright © 2007 15
Frank Vahid and Roman Lysecky
Finite-State Machines (FSMs)—Sequential Behavior
Modules with Multiple Procedures and Shared Variables
`timescale 1 ns/1 ns ...
Inputs: B; Outputs: X S_On1: begin
X=0 module LaserTimer(B, X, Clk, Rst); X <= 1;
StateNext <= S_On2;
Off B input B; end
output reg X; S_On2: begin
B input Clk, Rst; X <= 1;
StateNext <= S_On3;
X=1 X=1 X=1
parameter S_Off = 0, S_On1 = 1, end
On1 On2 On3 S_On2 = 2, S_On3 = 3; S_On3: begin
X <= 1;
reg [1:0] State, StateNext; StateNext <= S_Off;
end
outputs
inputs
X
FSM
B endcase
FSM
// CombLogic
Combinational end
always @(State, B) begin
logic case (State)
S_Off: begin // StateReg
State always @(posedge Clk) begin
X <= 0;
if (B == 0) if (Rst == 1 )
Clk State register State <= S_Off;
StateNext <= S_Off;
else else
StateNext StateNext <= S_On1; State <= StateNext;
end end
... endmodule
• Code will be
Verilog for Digital Design explained on vldd_ch3_LaserTimerBeh.v
Copyright © 2007 following slides 16
Frank Vahid and Roman Lysecky
Finite-State Machines (FSMs)—Sequential Behavior
• Modules has two procedures `timescale 1 ns/1 ns
logic input B;
output reg X;
– One procedure for state register input Clk, Rst;
– But it's still a behavioral parameter S_Off = 0, S_On1 = 1,
description S_On2 = 2, S_On3 = 3;
outputs
inputs
B X
FSM
FSM
// CombLogic
Combinational always @(State, B) begin
logic ...
end
State
// StateReg
State register always @(posedge Clk) begin
Clk ...
end
StateNext endmodule
// StateReg
always @(posedge Clk) begin
...
end
endmodule
B X end
FSM
FSM
Combinational
logic // StateReg
State always @(posedge Clk) begin
...
Clk State register end
endmodule
StateNext
X
FSM
B endcase
FSM
// CombLogic
Combinational end
always @(State, B) begin
logic case (State)
S_Off: begin // StateReg
State always @(posedge Clk) begin
X <= 0;
if (B == 0) if (Rst == 1 )
Clk State register State <= S_Off;
StateNext <= S_Off;
else else
StateNext StateNext <= S_On1; State <= StateNext;
end end
... endmodule
10 20 30 40 50 60 70 80 90 100 110
time (ns)
Verilog for Digital Design
Copyright © 2007 vldd_ch3_LaserTimerTB.v 24
Frank Vahid and Roman Lysecky
Finite-State Machines (FSMs)—Sequential Behavior
Self-Checking Testbenches
• Reading waveforms is error-prone // Vector Procedure
• Create self-checking testbench initial begin
Rst_s <= 1;
– Use if statements to check for B_s <= 0;
@(posedge Clk_s);
expected values #5 if (X_s != 0)
• If a check fails, print error message $display("%t: Reset failed", $time);
• Ex: if X_s fell to 0 one cycle too Rst_s <= 0;
@(posedge Clk_s);
early, simulation might output: #5 B_s <= 1;
– 95: Third X=1 failed @(posedge Clk_s);
#5 B_s <= 0;
if (X_s != 1)
Clk_s $display("%t: First X=1 failed", $time);
@(posedge Clk_s);
#5 if (X_s != 1)
Rst_s $display("%t: Second X=1 failed", $time);
@(posedge Clk_s);
B_s #5 if (X_s != 1)
$display("%t: Third X=1 failed", $time);
X_s @(posedge Clk_s);
#5 if (X_s != 0)
$display("%t: Final X=0 failed", $time);
end
10 20 30 40 50 60 70 80 90 100 110
time (ns)
Verilog for Digital Design
Copyright © 2007 25
Frank Vahid and Roman Lysecky vldd_ch3_LaserTimerTBDisplay.v
Finite-State Machines (FSMs)—Sequential Behavior
$display System Procedure
// Vector Procedure
• $display – built-in Verilog system initial begin
procedure for printing information to Rst_s <= 1;
display during simulation B_s <= 0;
– A system procedure interacts with the @(posedge Clk_s);
simulator and/or host computer system #5 if (X_s != 0)
$display("%t: Reset failed", $time);
• To write to a display, read a file, get the Rst_s <= 0;
current simulation time, etc.
@(posedge Clk_s);
• Starts with $ to distinguish from regular #5 B_s <= 1;
procedures @(posedge Clk_s);
• String argument is printed literally... #5 B_s <= 0;
– $display("Hello") will print "Hello" if (X_s != 1)
$display("%t: First X=1 failed", $time);
– Automatically adds newline character @(posedge Clk_s);
• ...except when special sequences appear #5 if (X_s != 1)
$display("%t: Second X=1 failed", $time);
– %t: Display a time expression @(posedge Clk_s);
– Time expression must be next argument #5 if (X_s != 1)
• $time – Built-in system procedure that $display("%t: Third X=1 failed", $time);
returns the current simulation time @(posedge Clk_s);
– 95: Third X=1 failed #5 if (X_s != 0)
$display("%t: Final X=0 failed", $time);
end
outputs
inputs
creating structure B X
FSM
FSM
FSM
Combinational N1
• Capture behavior: FSM logic
N0
• Capture structure: Controller S1 S0
– Create architecture (state Clk State register
register and combinational logic)
– Encode states
– Create stable table (describes Inputs: B; Outputs: X LaserTimer example
combinational logic) X=0
– Implement combinational logic 00
B'
Off
B
Verilog for Digital Design X=1 X=1 X=1
Copyright © 2007 01 On1 10 On2 11 On3 28
Frank Vahid and Roman Lysecky
Top-Down Design – FSMs to Controller Structure s
tp
u
o
inputs
outputs
M
S
F
FSM
b x
FSM
Combinational n1 outpu
FSM
•
logic
Recall from Chapter 2 Inputs: B; Outputs: X s1 s0
n0
00
• Capture behavior, and simulate Off B'
• Capture structure (circuit),
simulate again B
X=1 X=1 X=1
• Gets behavior right first,
unfettered by complexity of 01 On1 10 On2 11 On3
creating structure
• Capture behavior: FSM
• Capture structure: Controller
– Create architecture (state
register and combinational logic)
– Encode states
– Create stable table (describes
combinational logic)
– Implement combinational logic
inputs
outputs
M
S
F
FSM
b x
FSM
Combinational n1 outpu
FSM
•
logic
Recall from Chapter 2 Inputs: B; Outputs: X s1 s0
n0
00
• Capture behavior, and simulate Off B'
• Capture structure (circuit),
simulate again B
X=1 X=1 X=1
• Gets behavior right first,
unfettered by complexity of 01 On1 10 On2 11 On3
creating structure
• Capture behavior: FSM
• Capture structure: Controller
– Create architecture (state
register and combinational logic)
– Encode states
– Create stable table (describes
combinational logic)
– Implement combinational logic
X = S1 + S0
Verilog for Digital Design N1 = S1’S0 + S1S0’
Copyright © 2007 30
Frank Vahid and Roman Lysecky N0 = S1’S0’B + S1S0’
Controller `timescale 1 ns/1 ns
Structure input B;
output reg X;
input Clk, Rst;
outpu
FSM
• Structural description
• Test with LaserTimerTB parameter S_Off = 2'b00;
X = S1 + S0 // CombLogic
N1 = S1’S0 + S1S0’ always @(State, B) begin
X <= State[1] | State[0];
N0 = S1’S0’B + S1S0’ StateNext[1] <= (~State[1] & State[0])
| (State[1] & ~State[0]);
outputs StateNext[0] <= (~State[1] & ~State[0] & B)
outputs
inputs
B X
FSM
Combinational N1 end
logic
Clk_s
N0 // StateReg
S1 S0 always @(posedge Clk) begin
Rst_s
if (Rst == 1 )
Clk State register State <= S_Off;
B_s
else
State <= StateNext;
X_s
end
endmodule
10 20 30 40 50 60 70 80 90 100 110
time (ns)
Verilog for Digital Design
Copyright © 2007 31
Frank Vahid and Roman Lysecky vldd_ch3_LaserTimerStruct.v
Controller `timescale 1 ns/1 ns
Structure input B;
output reg X;
input Clk, Rst;
outpu
FSM
• Initial state is S_Off
– Encoded as "00" parameter S_Off = 2'b00;
– State register set to reg [1:0] State, StateNext;
S_Off during FSM // State encodings:
reset // S_Off 00, S_On1 01, S_On2 10, S_On3 11
• Note that CombLogic // CombLogic
uses equations, not always @(State, B) begin
case statement X <= State[1] | State[0];
StateNext[1] <= (~State[1] & State[0])
– Actually CombLogic | (State[1] & ~State[0]);
is still behavioral StateNext[0] <= (~State[1] & ~State[0] & B)
– Do top-down design | (State[1] & ~State[0]);
end
again, this time on
CombLogic, to get // StateReg
structure always @(posedge Clk) begin
if (Rst == 1 )
State <= S_Off;
else
State <= StateNext;
end
endmodule
module SimEx1(Q);
• Instructive to consider how an HDL
output reg Q;
simulator works reg Clk, S;
– HDL simulation is complex; we'll introduce // P1
simplified form always begin
Clk <= 0;
• Consider example SimEx1 #10;
– Three reg variables – Q, Clk, S Clk <= 1;
#10;
– Three procedures – P1, P2, P3 end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v
36
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
@ (posedge Clk);
S <= 1;
Clk x 0 @ (posedge Clk);
S <= 0;
S x x end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 37
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
// P2
P3 Activate when Clk changes to 1. always @(S) begin
Q <= ~S;
end
Time (ns): Start 0 10 // P3
initial begin
Q x x x @ (posedge Clk);
Variables
S <= 1;
Clk x 0 1 @ (posedge Clk);
S <= 0;
S x x x end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 38
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
#10;
P2 Activate when S changes. end
@ (posedge Clk);
S <= 1;
Clk x 0 1 1 @ (posedge Clk);
S <= 0;
S x x x 1 end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 39
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
#10;
P2 Activate when S changes. end
Q <= 0 (~S), stop, activate when S changes.
// P2
P3 Activate when change on Clk to 1. always @(S) begin
Q <= ~S;
end
Time (ns): Start 0 10 10 10 // P3
initial begin
Variables
Q x x x x 0 @ (posedge Clk);
S <= 1;
Clk x 0 1 1 1 @ (posedge Clk);
S <= 0;
S x x x 1 1 end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 40
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
// P2
P3 Activate when change on Clk to 1. always @(S) begin
Q <= ~S;
end
Time (ns): Init 0 10 10 10 20 // P3
initial begin
Variables
Q x x x x 0 0 @ (posedge Clk);
S <= 1;
Clk x 0 1 1 1 0 @ (posedge Clk);
S <= 0;
S x x x 1 1 1 end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 41
Frank Vahid and Roman Lysecky
The Simulation Cycle `timescale 1 ns/1 ns
#10;
Clk Clk <= 1;
#10;
S x end
// P2
0 10 20 30 40 50 Time (ns) always @(S) begin
Q <= ~S;
end
Time (ns): Init 0 10 10 10 20 30 30 30 40 50 // P3
initial begin
Variables
Q x x x x 0 0 0 0 1 1 1 @ (posedge Clk);
S <= 1;
Clk x 0 1 1 1 0 1 1 1 0 1 @ (posedge Clk);
S <= 0;
S x x x 1 1 1 1 0 0 0 0 end
endmodule
Verilog for Digital Design
Copyright © 2007 vldd_ch3_SimEx1.v 42
Frank Vahid and Roman Lysecky
Variable Updates
• Assignment using "<=" ("non blocking assignment") • Simulation cycle (revised)
doesn't change variable's value immediately – Set time to next time at
which a procedure resumes
– Instead, schedules a change of value by placing an
event on an event queue – Execute active procedures
– Scheduled changes occur at end of simulation cycle – Update variables with
schedule values
• Important implications
– Procedure execution order in a simulation cycle doesn't Assume B is 0.
matter
Proc1:
• Assume procedures 1 and 2 are both active
B <= ~B;
– Proc1 schedules B to be 1, but does not change the present
value of B. B is still 0. Proc2:
– Proc2 schedules A to be 0 (the present value of B). A <= B;
– At end of simulation cycle, B is updated to 1 and A to 0 A will be 0, not 1.
– Order of assignments to different variables in a
procedure doesn't matter Proc3a: Same Proc3b:
• Assume C was 0. Scheduled values will be C=1 and D=0, C <= ~C; D <= C;
for either Proc3a or Proc3b. D <= C; C <= ~C;
– Later assignment in procedure effectively overwrites
earlier assignment
Proc4:
• E will be updated with 0, but then by 1; so E is 1 at the E <= 0;
end of the simulation cycle. ...
Verilog for Digital Design E <= 1;
Copyright © 2007 Recall FSM output assignment example,
43
Frank Vahid and Roman Lysecky in which default assignments were added
before the case statement.
Resets
I input [3:0] I;
Q output [3:0] Q;
reg [3:0] Q;
Clk
input Clk, Rst;
Rst
Asynchronous reset always @(posedge Clk, posedge Rst) begin
Rst=1 has almost immediate effect if (Rst == 1 )
Q <= 4'b0000;
I else
Q xxxx Q <= I;
end
Clk endmodule
Rst
Synchronous reset
Rst=1 has no effect until next rising clock
Verilog for Digital Design
Copyright © 2007 47
vldd_ch3_Reg4AsyRst.v
Frank Vahid and Roman Lysecky
Asynchronous Reset
...
• Could have used // StateReg
always @(posedge Clk) begin
asynchronous reset for FSM if (Rst == 1 )
state register too State <= S_Off;
else
Synchronous
State <= StateNext;
end
...
...
// StateReg
always @(posedge Clk, posedge Rst) begin
if (Rst == 1 )
State <= S_Off;
else Asynchronous
State <= StateNext;
end
...
vldd_ch3_LaserTimerBehAsyRst.v