Verilog PPT Presentation
Verilog PPT Presentation
1
Defining a module
■ A module is the main building block in Verilog
■ We first need to declare:
Name of the module
Types of its connections (input, output)
Names of its connections
a
Ve r i l o g
b y
Module
c
2
a
Verilog
b y
Module
c
// here comes
the circuit
description
3
■ You can also define multi-bit busses.
[ range_start : range_end ]
4
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 input B;
choice small i2 ( .B(C), output
.Y(Y), Y;
.A(n1) );
//
endmodule descriptio
n of small
5
endmodule
Bitwise Operators
module gates(input [3:0] a, b,
output [3:0] y1, y2, y3, y4,
y5);
/* gates on 4two-input
Five different bi busse logic
*/
acting t s
endmodule 7
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
8
Verilog Number Representation
Verilog Stored Number Verilog Stored Number
9
Precedence of Operations in Verilog
Highest ~ NOT
*, /, % mult, div, mod
+, - add,sub
<<, >> shift
10
Sequential Logic in Verilog
■ Define blocks that have memory
Flip-Flops, Latches, Finite State Machines
11
always Statement, Defining Processes
always @ (sensitivity
list) statement;
12
Example: D Flip-Flop
module clk
flop(input ,
input [3:0] d,
outpu reg q);
t [3:0]
always @ (posedge
clk) q <= d; // pronounced “q gets
d”
endmodule
13
Example: D Flip-Flop
module clk
flop(input ,
input [3:0] d,
outpu reg q);
t [3:0]
always @ (posedge
clk) q <= d; // pronounced “q gets
d”
endmodule
■ The posedge defines a rising edge (transition from 0 to 1).
■ This process will trigger only if the clk signal rises.
■ Once the clk signal rises: the value of d will be copied to q
14
Example: D Flip-Flop
module clk
flop(input ,
input [3:0] d,
outpu reg q);
t [3:0]
always @ (posedge
clk) q <= d; // pronounced “q gets
d”
endmodule
■ ‘assign’ statement is not used within always block
■ The <= describes a ‘non-blocking’ assignment
We will see the difference between ‘blocking assignment’ and
‘non-blocking’ assignment in a while
15
Example: D Flip-Flop
module clk
flop(input ,
input [3:0] d,
outpu reg q);
t [3:0]
always @ (posedge
clk) q <= d; // pronounced “q gets
d”
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).
■ We will see examples later
16
D Flip-Flop with Asynchronous Reset
module flop_ar clk,
(input reset
inpu ,
output
t reg q);
[3:0] d,
inpu
always @ (posedget clk, negedge reset)
begin
if (reset ‘0’) q <= 0; // when rese
== t
else q <= d; // when clk
end
endmodul
e
■ In this
examp
le: two
events
17
can
D Flip-Flop with Asynchronous Reset
module flop_ar clk,
(input reset
inpu ,
output
t reg [3:0]
[3:0] d,
q);
inpu
always @ (posedget clk, negedge
reset)
if (reset == ‘0’) q <=
begin // when
els
0; q <= reset
e d; // when clk
end
endmodul
■e For longer statements a begin end pair can be used
In this example it was not necessary
■ First
reset
is
checke
19
d, if
D Flip-Flop with Synchronous Reset
module flop_sr clk,
(input reset
inpu ,
output
t reg [3:0]
[3:0] d,
q);
inpu
always @ (posedget clk)
begin
if (reset ‘0’) <= 0; // when rese
== q t
else q <= d; // when clk
end
endmodul
e
■ The
proces
s is
only
sensiti 21
D Flip-Flop with Enable and Reset
module flop_ar clk,
(input reset
inpu , en,
t [3:0] d,
inpu
output reg [3:0] q);
t
inpu clk. negedge
always @ (posedge
reset) t
if (reset == ‘0’) q <=
begin // when reset
0;
else if q <= // when en AND
(en) d; clk
end
endmodul
■e A flip-flop with enable and reset
lat
[3:0] [3:0]
d[3:0] D[3:0] [3:0] [3:0]
Q[3:0] q[3:0]
clk C
q[3:0]
22
Summary: Sequential Statements
■ Sequential statements are within an ‘always’ block
■ The sequential block is triggered with a change in the
sensitivity list
■ Signals assigned within an always must be declared as
reg
23
Why does an always Statement Memorize?
module comb inv,
(input [3:0]
input
output regdata,
[3:0]
result);
always @ (inv, // trigger with inv,
data) data
if (inv) result <= ~data;/ result is inverted
/ data
else result <= data; / result is data
endmodule /
25
Always Statement is not Always Practical…
reg [31:0] result;
wire [31:0] a, b,
comb; wire sel,
endmodule
26
The case Statement
■ Like if .. then .. else can only be used in
always blocks
■ The result is combinational only if the output is defined for
all cases
Did we mention this before ?
27
Non-blocking and Blocking Statements
Non-blocking Blocking
always @ (a) always @ (a)
begin begin
a <= 2’b01; a = 2’b01;
b <= a; // a is
// all assignments are made 2’b01 b =
here a;
// b is not (yet) 2’b01 // b is now
end 2’b01 as
well
■ Values are assigned at the ■ Value is assigned
end
end of the block. immediately.
29
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 & cout = 0
cin) ; //
end
30
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 & ; // cout = 0
cin)
end
■ The process triggers
■ All values are updated in order
■ At the end, s = 1
31
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 & cout = 0
cin) ; //
end
32
Same Example: Non-Blocking Statements
■ Now a changes to
‘1’
always @ ( * )
p
begin <= a ^ b ; // p = 1
g <= a & b ; // g = 0
s <= p ^ cin ; // s = 0
cout <= g | (p & ; // cout = 0
cin)
end
■ The process triggers
■ All assignments are concurrent
■ When s is being assigned, p is still 0, result is still
0
33
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 & // cout = 0
cin) ;
end
■ Since there is a change in p, process triggers again
■ This time s is calculated with p=1
34
Rules for Signal Assignment
■ Use always @(posedge clk) and non-blocking
assignments (<=) to model synchronous sequential logic
always @ (posedge clk)
q <= d; // nonblocking
35
Rules for Signal Assignment (cont)
■ Use always @ (*) and blocking assignments (=) to
model more complicated combinational logic where the
always statement is helpful.
36
Finite State Machines (FSMs)
■ Each FSM consists of three separate parts:
next state logic
state register
output logic
CLK
M next next k N
inputs state k state state output
outputs
logic
logic
37
FSM Example: Divide by 3
S2
S0
S1
38
FSM in Verilog, Definitions
module divideby3FSM (input clk,
input reset,
output q);
parameter S0 =
2'b00; parameter S1
= 2'b01; parameter
S2 = 2'b10;
■ We define state
and nextstate as
2-bit reg
40
FSM in Verilog, Next State Calculation
// next state
logic
always @ (*)
S0 (state)
case nextstate =
: S1; nextstate
S1 = S2;
default:
: nextstate =
nextstate = S0;
endcase
S2 S0;
:
■ Based on the value of state we determine the value of
nextstate
41
FSM in Verilog, Output Assignments
// output logic
assign q = (state == S0);
42
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;
always @ (posedge
clk, posedge reset)
// state register
if (reset) state
<= S0
S0; nextstate =
:
else S1; nextstate
S1 <=
state = S2;
:
default:
nextstate; nextstate
nextstate = =
S2@ (*) S0;
alwaysS0;
endcase
: // output
assign
// nextqstate
= (state
logic== S0); logic
endmodule
case (state)
43
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
44