Digital Design With Verilog
Digital Design With Verilog
Mazen A. R. Saghir
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 1 / 51
Modules
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 2 / 51
Module syntax
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 4 / 51
Module declarations module module_name (port_list);
// module items
endmodule
A module maymodule
includemodule_name
a number of(port_list);
signal declarations. The syntax of
// module items
a declaration is:
endmodule
<type> name;
<type> name;
Examples:
wire node1; // wire signal
reg Q0, Q1, Q2; // three register signals
wire [63:0] bus1; // 64-bit wire vector signal
reg [7:0] count; // 8-bit register vector signal
integer i, j; // integer variables
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 6 / 51
integer i, j; // integer variables
assign signal_name = value;
Numbers
assign signal_name = value;
assign F1 = A & B; // assigns F1 the value of A & B
assign F2 = 1’b0; // assigns F2 a single bit = 0
assign
assign
By default, numbers are F1==as
F3
treated A &32-bit
B; // assigns
8’hAA; assigns F1 the
an value
F3However,
integers. ofVerilog
A & =B 10101
8-bit value
assign F2 = 1’b0; // assigns F2 a single bit = 0
enables numbers to beassign
specified
F3 = in different
8’hAA; bases
// assigns F3 and bit value
an 8-bit widths:
= 1010101
<size_in_bits>’<base><value>
<size_in_bits>’<base><value>
Numbers can be specified
‘b in the following
unsigned binary bases:
‘o’b unsigned
unsigned octal
binary
‘d’o unsigned
unsigned decimal
octal
‘h’d unsigned
unsigned hexadecimal
decimal
‘sb
’h signed binary
unsigned hexadecimal
‘so
’sb signed
signed octal
binary
‘sd
’so signed
signed decimal
octal
‘sh
’sd signed
signed hexadecimal
decimal
’sh signed hexadecimal
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 8 / 51
Bitwise logic operators
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 10 / 51
assign C = X | Y;
wire [3:0] X,Y,A,B,C,D,E,F,G; // C = 1111 (binary)
Boolean logic operators
assign D = X ^ Y; // D = 0101 (binary)
assign X = 4’b1010; // X =E1010
assign = X (binary)
~^ Y; // E = 1010 (binary)
assign Y = 4’b1111; // Y = 1111 (binary)
assign F = X << 3; // F = 0000 (binary)
assign A = ~X; // A = 0101 (binary)
Used to assign
develop X & Y;assign
B =conditional =G = Y(binary)
>> 2;
// B statements
1010 that// evaluate
G = 0011to(binary)
assign
TRUE (1) C = X |(0).
or FALSE Y; // C = 1111 (binary)
assign D = X ^ Y; // D = 0101 (binary)
assign E = X ~^ Y;! // E =logical negation
1010 (binary)
assign F = X << 3;&&// F =logical AND
0000 (binary)
assign G = Y >> 2;
|| // G =logical
0011 (binary)
OR
Can be applied
! to numerical
logical negation values where non-zero values are
&&TRUE
treated as logical
andAND !X is treated as
zero // TRUE if X = all zeros, FALSE oth
FALSE.
|| logical OR X && Y // TRUE if X & Y = all ones; FALSE
X || Y // TRUE if X | Y = all ones; FALSE
Examples:
!X // TRUE if X = zero, FALSE otherwise
X && Y // TRUE
== if Xequal
& Y = to
non-zero; FALSE otherwise
X || Y // TRUE
!= if Xnot
| Y equal
= non-zero;
to FALSE otherwise
< less than
== equal to
> greater than
M. Saghir
!= not equal to<=
(EECE 320 – Summer 2023)
less than or equal to
Introduction to Digital Design with Verilog 11 / 51
|| logical OR
Relational operators
!X // TRUE if X = zero, FALSE otherwise
X && Y // TRUE if X & Y = non-zero; FALSE otherwi
X || return
Compare quantities and Y // TRUE
TRUE (1) ififXthe
| Ycorresponding
= non-zero; FALSE otherwis
relation
is satisfied and FALSE (0) otherwise.
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
Examples:
X == Y // TRUE if X is equal to Y; FALSE otherwise
X != Y // TRUE if X is not equal to Y; FALSE otherwise
X<Y // TRUE if X is less than Y; FALSE otherwise
X>Y // TRUE if X is greater than Y; FALSE otherwise
X <= Y // TRUE if X is less than or equal to Y; FALSE otherwise
X >= Y // TRUE if X is greater than or equal to Y; FALSE otherwise
<target_net> = <Boolean_condition>
M. Saghir (EECE 320 – Summer 2023)
? <true_assignment> : < false_assignmen
Introduction to Digital Design with Verilog 12 / 51
Conditional assignment statement
X == Y // TRUE if X is equal to Y; FALSE otherwise
X != Y // TRUE if X is not equal to Y; FALSE otherwise
X<Y // TRUE if X is less than Y; FALSE otherwise
X>Y // TRUE if X is greater than Y; FALSE otherwise
X <= Y // TRUE if X is less than or equal to Y; FALSE otherwise
Uses
== the
X >= Y ? and : operators
//// TRUE
TRUE to express
ififXXisisgreater
equal to Y; FALSE
than anotherwise
or equal if/else
to Y; FALSEconditional
otherwise
X != Y //
assignment statement. TRUE if X is not equal to Y; FALSE otherwise
X<Y // TRUE if X is less than Y; FALSE otherwise
X >Y
<target_net> =// <Boolean_condition>
TRUE if X is greater ? than Y; FALSE otherwise
<true_assignment> : < false_assignment>;
X <= Y // TRUE if X is less than or equal to Y; FALSE otherwise
X–>=If Yboolean // TRUE
conditionif X is
is greater
TRUE,than or equal
target net = to trueY; FALSE otherwise
assignment.
assign F = (A == 1’b0) ? 1’b1 : 1’b0; // F = 1 if A equals 0; otherwise F = 0
– Else,
assign F =target
(sel ==net = false
1’b0) ? A : B;assignment. // F = A if sel equals 0; otherwise F = B
assign F = ((A == 1’b0) &&
<target_net> = <Boolean_condition> (B == 1’b0)) ? 1’b0 :
? <true_assignment> : < false_assignment>;
((A == 1’b0) && (B == 1’b1)) ? 1’b1 :
Examples: ((A == 1’b1) && (B == 1’b0)) ? 1’b1 : 1’b0; // nested conditional implements
assign F = (A == 1’b0) ? 1’b1 : 1’b0; // F =//1aif2-input
A equalsxor0;function
else F = 0
assign F
assign F == (!C
(sel&& ==(!A || B))
1’b0) ?A ? :1’b1
B; : 1’b0; // //F F= =C’.(A’ + B)equals 0; else F = B
A if sel
assign F = (!C && (!A || B)) ? 1’b1 : 1’b0; // F = C’.(A’ + B)
wire[7:0]
// nested bus1, bus2, assignment
conditional bus3, busC; statement implementing a 2-input xor function
wire[3:0] busA, busB;
assign F = ((A == 1’b0) && (B == 1’b0)) ? 1’b0 :
((A == 1’b0) && (B == 1’b1)) ? 1’b1 :
bus1[7:0] = {bus2[7:4], bus3[3:0]};
((A == 1’b1) && (B == 1’b0)) ? 1’b1 : 1’b0;
busC = {busA, busB};
M. Saghir busC[7:0]
(EECE = {4’b0000,
320 – Summer 2023) busA};
Introduction to Digital Design with Verilog 13 / 51
X != Y // TRUE if X is not equal to Y; FALSE otherwise
Nested
X <conditional
Y // TRUEassignment
if X is less than Y; statement
FALSE otherwise
X>Y // TRUE if X is greater than Y; FALSE otherwise
X <= Y // TRUE if X is less than or equal to Y; FALSE otherwise
X >= Y // TRUE if X is greater than or equal to Y; FALSE otherwise
{<number_of_replications>{<vector_to_be_replicated>}}
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 15 / 51
// a 2-input xor fun
((A ==F1’b1)
assign = (!C&&
&&(B
(!A== 1’b0))
|| B)) ? 1’b1
? 1’b1 : 1’b0; ////nested
: 1’b0; conditional
F = C’.(A’ + B) imple
Replication // a 2-input xor function
assign F = (!C && (!A || B)) ? 1’b1 : 1’b0; // F = C’.(A’ + B)
wire[7:0] bus1, bus2, bus3, busC;
wire[3:0] busA, busB;
wire[7:0] bus1, bus2, bus3, busC;
wire[3:0] busA, busB;
bus1[7:0] = {bus2[7:4], bus3[3:0]};
ReplicationbusCis the repeated concatenation of a bit vector with itself for
= {busA, busB};
a bus1[7:0]
specific number
=busC[7:0] of=times.
{bus2[7:4], busA};double curly brackets {{}} and an
It uses
bus3[3:0]};
{4’b0000,
busC =to{busA,
integer specifybusB};
the desired number of replications.
busC[7:0] = {4’b0000, busA};
{<number_of_replications>{<vector_to_be_replicated>}}
{<number_of_replications>{<vector_to_be_replicated>}}
busX = {4{bus1}}; // equivalent to busx = {bus1, bus1, bus1, bus
Examples: busY = {2{A,B}}; // equivalent to busY = {A,B,A,B}
busZ =
busX = {4{bus1}}; {bus1, {2{bus2}}}; // equivalent
// equivalent to busx =to{bus1,
busZ =bus1,
{bus1, bus2,
bus1, bus2}
bus1}
busY = {2{A,B}}; // equivalent to busY = {A,B,A,B}
busZ = {bus1, {2{bus2}}}; // equivalent to busZ = {bus1, bus2, bus2}
+ addition
- subtraction (binary operator)
- negation (unary operator)
+ addition
* multiplication
- subtraction
/ (binary operator)
division
- negation
% (unary
modulus operator)
* multiplication
** power
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 16 / 51
Numerical operators
busX = {4{bus1}}; // equivalent to busx = {bus1,
busY = {2{A,B}}; // equivalent to busY = {A,B,A
busZ = {bus1, {2{bus2}}};
Perform binary (two operand) and unary (one // equivalent to busZ = {bus1,
operand) arithmetic
operations.
+ addition
- subtraction (binary operator)
- negation (unary operator)
* multiplication
/ division
% modulus
** power
<<< logical shift left
>>> arithmetic shift right
Examples:
X+Y // add X to Y
X–Y // subtract Y from X
-X // negate X
X*Y // multiply X by Y
X/Y // divide X by Y
X%Y // remainder of X/Y
X <<< 3 // shift left 3 bits; fill vacated bits with zeros
X >>> 2 // shift right 2 bits; fill vacated bits with sign of X
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 18 / 51
Example: Canonical SOP Circuit
ple 3.2
national logic
M. Saghir (EECEusing continuous
320 – Summer 2023) assignment with
Introduction to Digital logical
Design operators
with Verilog 19 / 51
Gate-Level Primitives
Verilog provides a number of gate-level primitives that model the
behavior of various logic gates:
– not()
– and()
– nand()
– or()
– nor()
– xor()
– xnor()
Syntax:
Examples:
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 20 / 51
Example: Gate-Level Structural Model
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 21 / 51
Heirarchical Design
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 22 / 51
Heirarchical Structural Model
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 23 / 51
Module Instantiation
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 24 / 51
Verilog testbench
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 25 / 51
Verilog testbench (2)
Testbench
Unit Under
Driver
…
…
Test (UUT)
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 26 / 51
mux4to1 testbench
initial begin
TB_w0 = 1’b0;
TB_w1 = 1’b1;
TB_w2 = 1’b0;
TB_w3 = 1’b1;
TB_sel = 2’b00;
#10 TB_sel = 2’b01;
#10 TB_sel = 2’b10;
#10 TB_sel = 2’b11;
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 27 / 51
Notes on mux4to1 testbench
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 28 / 51
TB_w1 = 1’b1;
Procedural blocks
TB_w2 = 1’b0;
TB_w3 = 1’b1;
TB_sel = 2’b00;
#10 is
A procedural block TB_sel = 2’b01;
a Verilog construct that models circuit behavior
#10
using sequential TB_sel = 2’b10;
statements.
#10 TB_sel = 2’b11;
end
The two main procedural blocks in Verilog are initial and always.
A Verilog module may contain multiple initial and always blocks.
endmodule
Syntax of an always block:
always [@ (<sensitivity list>)] <statement(s)>
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 29 / 51
#10 TB_sel = 2’b10;
TB_sel = 2’b00;
#10 TB_sel = 2’b11;
always block sensitivity#10 lists
endTB_sel = 2’b01;
#10 TB_sel = 2’b10;
#10 TB_sel = 2’b11;
endmodule
A sensitivity list specifiesend
the signals or events that cause an always
block to be evaluated during simulation.
endmodule
always [@ (<sensitivity list>)] <statement(s)>
This always block will be evaluated any time there is a change in the
logic levels of signals I0always
, I1, or[@
sel :
(<sensitivity list>)] <statement(s)>
always @ (I0, I1, sel)
begin
// statements
always
end @ (I0, I1, sel)
begin
This always block will be//always statements
evaluated @ (posedge
on every clk) positive edge (i.e. 0-to-1
end
begin
transition) of the clk signal:
// statements
always
end @ (posedge clk)
begin
// statements
end
module mux2to1 (input I0, I1, sel, output y);
always @ (I0, I1, sel) begin
if (sel == 0)
M. Saghir (EECE 320 – Summer 2023) module
Introduction mux2to1
to Digital Design with (input
Verilog I0, I1, sel, output y); 30 / 51
always block sensitivity lists (2)
always @ (I0, I1, sel)
begin
// statements
An always
end block’s sensitivity list may include positive or negative
edge signal transitions (e.g. posedge, negedge).
always @ (posedge clk)
beginlogic level transition and signal edge transition events
However,
// statements
should never be combined in the same sensitivity list at the same
end
time.
always @ (I0, I1, sel, posedge clk, negedge reset_n) // incorrect
begin
// statements
end
module
Signal mux2to1 (input
edge transitions I0, I1,
should be sel,
usedoutput y); sequential circuits.
to model
always @ (I0, I1, sel) begin
if (sel == 0)
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 31 / 51
Procedural block assignments
Blocking assignments (=) are used to model combinational circuits.
reg T, F;
reg
alwaysT, F;@ (A, B, C)
begin
always
// RHS@ (A, B,are
values C) evaluated sequentially.
begin
T = A & B;
//F RHS
= T &values
C; are evaluated sequentially.
T = A & B;
end
F = T & C;
Non-blocking end
assignments (<=) are concurrent statements used to
model sequential reg Q0, Q1, Q2;
circuits.
reg Q0, Q1, Q2;
always @ (posedge clk)
begin
always @ (posedge clk)
// RHS values are evaluated concurrently,
begin
// as soon as the always block is triggered.
// RHS values are evaluated concurrently,
//Q0as<= w; as the always block is triggered.
soon
Q1 <=
Q0 <= w;
Q0;
Q2 <=
Q1 <= Q0;Q1;
end
Q2 <= Q1;
end
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 32 / 51
always @(2)
Procedural block assignments (posedge clk)
begin
// RHS values are evaluated concurrently,
// as soon as the always block is triggered.
Any signal or port assignedQ0 <= w; inside a procedural block must be
a value
declared as a reg. Q1 <= Q0;
Q2 <= Q1;
end
A signal or port must never be assigned in more than one procedural
block.
reg F;
always @ (A, B, C)
begin
F = A & B & C;
end
always @ (D, E)
begin
F = D ^ E;
end
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 34 / 51
if statement
module mux4to1 (input I0, I1, I2, I3, [1:0] sel, output reg y);
always @ (I0, I1, I2, I3, sel) begin
if (sel == 2’b00)
y = I0;
else if (sel == 2’b01)
y = I1;
else if (sel == 2’b10)
y = I2;
else
y = I3;
end
endmodule
module mux4to1 (input I0, I1, I2, I3, [1:0] sel, output reg y);
always @ (I0, I1, I2, I3, sel) begin
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 36 / 51
else
y = I3;
case statement end
endmodule
case statements are an efficient way to implement multi-way
branches:
case (<signal name>)
<signal value 1> : <clause 1>
<signal value 2> : <clause 2>
:
<signal value n> : < clause n>
default: <default clause>
endcase
A case statement evaluates a signal that can take multiple values and
executes the clause module mux4to1
matching (input I0, I1, value.
the corresponding I2, I3, [1:0] sel, output re
always @ (I0, I1, I2, I3, sel) begin
Each clause may includecase (sel)
one or more statements; if a clause contains
multiple statements, they must ybe
2’b00 : = I0;
enclosed in begin-end blocks.
2’b01 : y = I1;
A case statement may include 2’b10 : yan = I2;
optional default clause that gets
default : y = I3;
executed if the current endcase signal value does not match any of the
specified values. end
M. Saghir (EECE 320 – Summer 2023) endmodule
Introduction to Digital Design with Verilog 37 / 51
:
Example: 4:1
<signal multiplexer
value n> a case statement
using
n> : < clause
default: <default clause>
endcase
module mux4to1 (input I0, I1, I2, I3, [1:0] sel, output reg y);
always @ (I0, I1, I2, I3, sel) begin
case (sel)
2’b00 : y = I0;
2’b01 : y = I1;
2’b10 : y = I2;
default : y = I3;
endcase
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 38 / 51
Sequential circuits: T flip-flop
module DFF (input Din, clk, reset, output reg Q, reg Q_bar);
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 39 / 51
end
Sequential circuits: D flip-flop
endmodule
module DFF (input Din, clk, reset, output reg Q, reg Q_bar);
always @ (posedge clk) begin
if (reset == 1’b1)
begin
Q <= 1’b0;
Q_bar <= 1’b1;
end
else
begin
Q <= Din;
Q_bar <= ~Din;
end
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 40 / 51
Q_bar <= ~Din;
Sequential
end circuits: shift register
end
endmodule
module SHR4 (input clk, load, [3:0] Din, W, output reg [3:0] Q);
always @ (posedge clk) begin
if (load == 1’b1)
Q <= Din;
else
Q <= {W, Q[3:1]};
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 41 / 51
Sequential circuits: modulo-10 counter
module myTB();
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 42 / 51
#3 // advances simulation time by 3 ns
initial begin
Modeling finite #5.5 machines
state // advances simulation time by 5.5 ns
(FSMs)
#3 // advances
#0.001 simulation
// advances time by time
simulation 3 ns by 0.001 ns = 1 ps
#5.5 end // advances simulation time by 5.5 ns
#0.001 // advances simulation time by 0.001 ns = 1 ps
end endmodule
Declare two reg signals of appropriate width1 called currentState
and nextState
endmodule .
// 2-bit current-state and next-state signals
reg [1:0] currentState, nextState;
// 2-bit current-state and next-state signals
Use thereg [1:0] currentState,
parameter constructnextState;
to create symbolic state names and
// symbolic state
associate them with specific state name declarations
values.
parameter stateA = 2’b00, stateB = 2’b01, stateC = 2’b10;
// symbolic state name declarations
parameter stateA = 2’b00, stateB = 2’b01, stateC = 2’b10;
Use three always blocks to manage the current state, next state, and
FSM output, respectively.
1
width = log2 (number of states)
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 43 / 51
8.5, to indicate the structure of the circuit that implements the required fini
ne.Example:
Two flip-flops Pattern Detector
represent the stateFSM
variables. In the figure we have not sp
e of flip-flops to be used; this issue is addressed in the next subsection. Fr
A A B 0
B A C 0
C A C 1
We need logFigure 8.4 State table for the sequential circuit in Figure
2 (3) = 2 bits to represent each state.
We can use the following state assignments: A = 00, B = 01, C = 10.
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 44 / 51
Managing the current state
always
M. Saghir (EECE 320 – Summer 2023) @ (currentState)
Introduction begin
to Digital Design with Verilog 46 / 51
nextState <= stateA;
else
Managing the output nextState <= stateC;
endcase
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 47 / 51
// modulo-10 up counter (counts from 0 to 9 repeatedly)
The for loop
module mod10counter (input clk, reset_n, output reg [3:0] Q)
Verilog supports
always a@number of looping
(posedge constructs
clk, negedge for, while,
such asbegin
reset_n)
repeat, andifforever.
(reset_n == 1’b0 || Q == 4’b1001)
Q <= 4’b0000;
else
Loops are generally not used to model actual hardware; they are
more commonly used+in1;testbenches to help drive simulation.
Q <= Q
end
endmodule
A for loop is used to execute one or more statements a fixed number
of times. It requires an integer iteration variable and has the
following syntax:
for (<var initialization>; <condition>; <var update>)
<statement(s)>;
module myTB();
reg clk;
integer i;
initial begin
clk <= 0;
for (i = 0; i <= 50; i++) begin
#5 clk <= ~clk;
end
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 49 / 51
reg clk;
Controlling simulation
integertime
i;
initial begin
clk <= 0;
We have already learned that
for (i = 0; i <= 50;# i++)
the operator
beginis used to advance
simulation time by a#5specific
clk <= ~clk;
number of time units.
end
end
The ’timescale directive assigns a value to the simulation time unit to
provide accurateendmodule
time modeling. It is commonly used in the testbench
file and uses the following syntax:
`timescale <time unit>/<time precision>
– Both the time unit and time precision are expressed in metric units of
time: seconds (s), milliseconds (ms), microseconds (us), nanoseconds
(ns), picoseconds (ps), and femtoseconds (fs).
– The time precision specifies the fraction of the time unit that can be
used to advance simulation time. It must always be smaller than or
equal to the specified time unit.
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 50 / 51
Example: Setting ’timescale to 1 ns with 1 ps precision
module myTB();
initial begin
#3 // advances simulation time by 3 ns
#5.5 // advances simulation time by 5.5 ns
#0.001 // advances simulation time by 0.001 ns = 1 ps
end
endmodule
M. Saghir (EECE 320 – Summer 2023) Introduction to Digital Design with Verilog 51 / 51