09 Verilog Sequential
09 Verilog Sequential
http://www.syssec.ethz.ch/education/Digitaltechnik_14
1
Adapted from Digital Design and Computer Architecture, David Money Harris & Sarah L. Harris ©2007 Elsevier
What will we learn?
Short summary of Verilog Basics
2
Summary: Defining a module
A module is the main building block in Verilog
a Ve r i l o g
b y
M o d u le
c
3
Summary: Defining a module
a
Verilog
b y
Module
c
endmodule
4
Summary: What if we have busses ?
You can also define multi-bit busses.
[ range_start : range_end ]
5
Structural HDL Example
Short Instantiation
module top (A, SEL, C, Y);
input A, SEL, C;
output Y;
wire n1;
// alternative
small i_first ( A, SEL, n1 );
/* Shorter instantiation,
pin order very important */ module small (A, B, Y);
input A;
// any pin order, safer choice input B;
small i2 ( .B(C), output Y;
.Y(Y),
.A(n1) ); // description of small
endmodule endmodule
6
Summary: Bitwise Operators
module gates(input [3:0] a, b,
output [3:0] y1, y2, y3, y4, y5);
endmodule
7
Summary: Conditional Assignment
? : is also called a ternary operator because it operates on
3 inputs:
s
d1
d0.
assign y = s ? d1 : d0;
// if (s) then y=d1 else y=d0;
endmodule
8
Summary: How to Express numbers ?
N’Bxx
8’b0000_0001
(B) Base
Can be b (binary), h (hexadecimal), d (decimal), o (octal)
(xx) Number
The value expressed in base, apart from numbers it can also have X and Z
as values.
Underscore _ can be used to improve readability
9
Summary: Verilog Number Representation
Verilog Stored Number Verilog Stored Number
10
Precedence of Operations in Verilog
Highest ~ NOT
*, /, % mult, div, mod
+, - add,sub
<<, >> shift
11
Sequential Logic in Verilog
Define blocks that have memory
Flip-Flops, Latches, Finite State Machines
12
always Statement, Defining Processes
always @ (sensitivity list)
statement;
13
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
14
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
The posedge defines a rising edge (transition from 0 to 1).
15
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
‘assign’ statement is not used within always block
16
Example: D Flip-Flop
module flop(input clk,
input [3:0] d,
output reg [3:0] q);
endmodule
Assigned variables need to be declared as reg
The name reg does not necessarily mean that the value is
a register. (It could be, it does not have to be).
17
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
18
D Flip-Flop with Asynchronous Reset
module flop_ar (input clk,
input reset,
input [3:0] d,
output reg [3:0] q);
always @ (clk, d)
if (clk) q <= d; // latch is transparent when
// clock is 1
endmodule
la t
[3 :0 ] [3 :0 ]
d [3 :0 ] D [3 :0 ] [3 :0 ] [3 :0 ]
Q[3 :0 ] q [3 :0 ]
c lk C
q[3:0]
23
Summary: Sequential Statements so far
Sequential statements are within an ‘always’ block
24
Summary: Basics of always Statements
module example (input clk,
input [3:0] d,
output reg [3:0] q);
27
Why does an always Statement Memorize?
module flop (input clk,
input [3:0] d,
output reg [3:0] q);
28
Why does an always Statement Memorize?
module comb (input inv,
input [3:0] data,
output reg [3:0] result);
endmodule
29
Why does an always Statement Memorize?
module comb (input inv,
input [3:0] data,
output reg [3:0] result);
endmodule
31
Always Statement is not Always Practical…
reg [31:0] result;
wire [31:0] a, b, comb;
wire sel,
endmodule
32
Sometimes Always Statements are Great
module sevensegment (input [3:0] data,
output reg [6:0] segments);
endmodule
33
The case Statement
Like if .. then .. else can only be used in always
blocks
34
Non-blocking and Blocking Statements
Non-blocking Blocking
always @ (a) always @ (a)
begin begin
a <= 2’b01; a = 2’b01;
b <= a; // a is 2’b01
// all assignments are made here b = a;
// b is not (yet) 2’b01 // b is now 2’b01 as well
end end
36
Example: Blocking Statements
Assume all inputs are initially ‘0’
always @ ( * )
begin
p = a ^ b ; // p = 0
g = a & b ; // g = 0
s = p ^ cin ; // s = 0
cout = g | (p & cin) ; // cout = 0
end
37
Example: Blocking Statements
Now a changes to ‘1’
always @ ( * )
begin
p = a ^ b ; // p = 1
g = a & b ; // g = 0
s = p ^ cin ; // s = 1
cout = g | (p & cin) ; // cout = 0
end
At the end, s = 1
38
Same Example: Non-Blocking Statements
Assume all inputs are initially ‘0’
always @ ( * )
begin
p <= a ^ b ; // p = 0
g <= a & b ; // g = 0
s <= p ^ cin ; // s = 0
cout <= g | (p & cin) ; // cout = 0
end
39
Same Example: Non-Blocking Statements
Now a changes to ‘1’
always @ ( * )
begin
p <= a ^ b ; // p = 1
g <= a & b ; // g = 0
s <= p ^ cin ; // s = 0
cout <= g | (p & cin) ; // cout = 0
end
40
Same Example: Non-Blocking Statements
After the first iteration p has changed to ‘1’ as well
always @ ( * )
begin
p <= a ^ b ; // p = 1
g <= a & b ; // g = 0
s <= p ^ cin ; // s = 1
cout <= g | (p & cin) ; // cout = 0
end
41
Rules for Signal Assignment
Use always @(posedge clk) and non-blocking
assignments (<=) to model synchronous sequential logic
always @ (posedge clk)
q <= d; // nonblocking
42
Rules for Signal Assignment (cont)
Use always @ (*) and blocking assignments (=) to
model more complicated combinational logic where the
always statement is helpful.
43
Finite State Machines (FSMs)
Each FSM consists of three separate parts:
next state logic
state register
output logic
CLK
M next
next k state k state output N
inputs state outputs
logic
logic
44
FSM Example: Divide by 3
S2
S0
S1
45
FSM in Verilog, Definitions
module divideby3FSM (input clk,
input reset,
output q);
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
46
FSM in Verilog, State Register
// state register
always @ (posedge clk, posedge reset)
if (reset) state <= S0;
else state <= nextstate;
47
FSM in Verilog, Next State Calculation
// next state logic
always @ (*)
case (state)
S0: nextstate = S1;
S1: nextstate = S2;
S2: nextstate = S0;
default: nextstate = S0;
endcase
48
FSM in Verilog, Output Assignments
// output logic
assign q = (state == S0);
49
FSM in Verilog, Whole Code
module divideby3FSM (input clk, input reset, output q);
reg [1:0] state, nextstate;
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
50
What Did We Learn?
Basics of Defining Sequential Circuits in Verilog
Always statement
Is needed for defining memorizing elements (flip-flops, latches)
Can also be used to define combinational circuits
Writing FSMs
Next state calculation
Determining outputs
State assignment
51