Week10 Sequential Circuits
Week10 Sequential Circuits
Week10 Sequential Circuits
Prepared MODELING
by : SEQUENTIAL
Dabudz
CIRCUITS IN VERILOG
PRESENTATION OUTLINE
Modeling Latches and Flip-Flops
procedural statements
end
Otherwise, the always block does nothing until another change occurs on a signal
in the sensitivity list
GUIDELINES FOR
SENSITIVITY LIST
For combinational logic, the sensitivity list must include ALL the signals that are
read inside the always block
For sequential logic, the sensitivity list may not include all the signals that are read
inside the always block
The positive edge or negative edge of each signal can be specified in the
sensitivity list
MODELING A D LATCH
WITH ENABLE
// Modeling a D Latch with Enable and output Q
// Output Q must be of type reg
// Notice that the if statement does NOT have else
// If Enable is 0, then value of Q does not change
// The D_latch stores the old value of Q
SYNCHRONOUS RESET
module D_FF3 (input D, Clk, Reset, output reg Q, Qbar);
// always block is NOT sensitive to Reset or D
// Updates happen only at positive edge of Clk
// Reset is Synchronized with Clk
always @(posedge Clk)
if (Reset)
{Q, Qbar} <= 2'b01;
else
{Q, Qbar} <= {D, ~D};
endmodule
D-TYPE FLIP-FLOP WITH
ASYNCHRONOUS RESET
// Modeling a D Flip-Flop with Asynchronous Reset input
module D_FF4 (input D, Clk, Reset, output reg Q, Qbar);
// Q and Qbar change at the positive edge of Clk
// Or, at the positive edge of Reset
// Reset is NOT synchronized with Clk
always @(posedge Clk, posedge Reset)
if (Reset)
{Q, Qbar} <= 2'b01;
else
{Q, Qbar} <= {D, ~D};
endmodule
PROCEDURAL ASSIGNMENT
Procedural assignment is used inside a procedural block only
Two types of procedural assignments:
Blocking assignment:
variable = expression; // = operator
Variable is updated before executing next statement
Similar to an assignment statement in programming languages
Non-Blocking assignment:
variable <= expression; // <= operator
Variable is updated at the end of the procedural block
Does not block the execution of next statements
BLOCKING VERSUS NON-
BLOCKING ASSIGNMENT
Guideline: Use Non-Blocking Assignment for Sequential Logic
q2 q1 q2 q1
in D Q D Q D Q out in D Q out
clk clk
module nonblocking module blocking
(input in, clk, output reg out); (input in, clk, output reg out);
reg q1, q2; reg q1, q2;
always @ (posedge clk) begin always @ (posedge clk) begin
q2 <= in; q2 = in;
q1 <= q2; Read: in, q2, q1 q1 = q2; // q1 = in
out <= q1; out = q1; // out = in
end Parallel Assignment at the end end
endmodule endmodule
BLOCKING VERSUS NON-
BLOCKING ASSIGNMENT
Guideline: Use Blocking Assignment for Combinational Logic
a a
x x
b b
Old x is
y Old x
c Latched y
c
3. When modeling both sequential and combinational logic within the same always
block, use non-blocking assignments
4. Do NOT mix blocking with non-blocking assignments in the same always block
5. Do NOT make assignments to the same variable from more than one always block
STRUCTURAL MODELING OF
SEQUENTIAL CIRCUITS
// Mixed Structural and Dataflow
Modeling the Circuit Structure module Seq_Circuit_Structure
(input x, Clock, output y);
// Modeling logic
assign DA = (A & x) | (B & x);
assign DB = Ab & x;
assign y = (A | B) & ~x;
endmodule
SEQUENTIAL CIRCUIT TEST
BENCH
module Seq_Circuit_TB; // Test Bench
reg x, clk;
wire y;
// Instantiate structural sequential circuit
// Inputs: x and clk
// Output: y
Seq_Circuit_Structure test1 (x, clk, y);
// Generate a clock with period = 10 ns
initial clk = 1;
always #5 clk = ~clk;
// Test sequence: x = 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, . . .
initial begin
x=0; #12 x=1; #10 x=0; #10 x=1; #20 x=0; #10 x=1; #40 x=0;
end
endmodule
SIMULATION WAVEFORMS
Structural and behavioral descriptions have identical waveforms
MODELING A STATE
DIAGRAM
A state diagram can be modeled directly in Verilog
0/0
0/0 1/1
0/0
0/0
0/0 1/1
0/0
reset 00 1/0 01 1/0 10
MODELING A MOORE STATE
DIAGRAM
module Moore_Comparator (input A, B, clk, rst, output GT, LT, EQ);
reg [1:0] state; // present state 00, 11
assign GT = state[1];
assign LT = state[0]; 00
rst
assign EQ = ~(GT | LT); 001
always @(posedge clk, posedge rst)
01 10
if (rst) state <= 'b00;
else case (state)
01
'b00: state <= ({A,B}=='b01)?'b01: 01 10
({A,B}=='b10)?'b10:'b00; 010 100
'b01: state <= ({A,B}=='b10)?'b10:'b01; 10
'b10: state <= ({A,B}=='b01)?'b01:'b10; 00 00
endcase 11 11
01 10
endmodule
TEST BENCH FOR THE
MOORE COMPARATOR
module Moore_Comparator_TB; // Test Bench
reg A, B, clk, rst;
wire GT, LT, EQ;
Moore_Comparator test (A, B, clk, rst, GT, LT, EQ);
// Reset pulse
initial begin #2 rst = 1; #4 rst = 0; end
// Generate clock
initial clk = 1;
always #5 clk = ~clk;
// Generate input test sequence
initial begin
{A,B}='b00; #12 {A,B}='b11; #10 {A,B}='b01; #10 {A,B}='b11;
#10 {A,B}='b10; #10 {A,B}='b00; #10 {A,B}='b01; #20 {A,B}='b10;
end
endmodule
MOORE COMPARATOR
WAVEFORMS
MODELING A REGISTER WITH
PARALLEL
module LOAD
Register #(parameter n = 4)
(input [n-1:0] Data_in, input load, clock, reset,
output reg [n-1:0] Data_out);
always @(posedge clock, posedge reset) // Asynchronous
reset
if (reset) Data_out <= 0;
else if (load) Data_out <= Data_in;
endmodule
MODELING A SHIFT
REGISTER
module Shift_Register #(parameter n = 4)
(input Data_in, clock, reset, output Data_out);
reg [n-1:0] Q;
assign Data_out = Q[0];
always @(posedge clock, negedge reset) // Asynchronous
reset
if (!reset) Q <= 0; // Active Low reset
else Q <= {Data_in, Q[n-1:1]}; // Shifts to the right
endmodule
MODELING A COUNTER
WITH PARALLEL LOAD
module Counter_with_Load #(parameter n = 4) // n-bit counter
( input [n-1:0] D, input Load, EN, clock,
output reg [n-1:0] Q, output Cout);
Q <= Q + 1;
endmodule 𝑄 3𝑄 2𝑄1𝑄 0
MODELING A GENERIC UP-
DOWN COUNTER
module Up_Down_Counter #(parameter n = 16) // n-bit counter