Digital Design Using Verilog HDL
Digital Design Using Verilog HDL
in
JNTU World
Table of Contents
ld
JN
TU
or
iv
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
or
109
109
113
116
120
122
128
132
135
ld
JN
TU
v
Downloaded From JNTU World (http://www.alljntuworld.in)
175
JNTU World
JN
TU
or
ld
www.alljntuworld.in
www.alljntuworld.in
JNTU World
Introduction
Introduction
JN
TU
or
ld
The first integrated circuits that were developed in the early 1960s contained less
that 100 transistors on a chip and are called small-scale integrated (SSI) circuits.
Medium-scale integrated (MSI) circuits, developed in the late 1960s, contain up to
several hundreds of transistors on a chip. By the mid 1970s large-scale integrated (LSI)
circuits containing several thousands of transistors had been developed. Very-large-scale
integrated (VLSI) circuits containing over 100,000 transistors had been developed by the
early 1980s. This trend has continued to the present day with 1,000,000 transistors on a
chip by the late 1980s, 10,000,000 transistors on a chip by the mid-1990s, over
100,000,000 transistors by 2004, and up to 1,000,000,000 transistors on a chip today.
This exponential growth in the amount of digital logic that can be packed into a single
chip has produced serious problems for the digital designer. How can an engineer, or
even a team of engineers, design a digital logic circuit that will end up containing
millions of transistors?
In Appendix C we show that any digital logic circuit can be made from only three
types of basic gates: AND, OR, and NOT. In fact, we will see that any digital logic
circuit can be made using only NAND gates (or only NOR gates), where each NAND or
NOR gate contains four transistors. These basic gates were provided in SSI chips using
various technologies, the most popular being transistor-transistor logic (TTL). These
TTL chips were the mainstay of digital design throughout the 1960s and 1970s. Many
MSI TTL chips became available for performing all types of digital logic functions such
as decoders, adders, multiplexers, comparators, and many others.
By the 1980s thousands of gates could fit on a single chip. Thus, several different
varieties of programmable logic devices (PLDs) were developed in which arrays
containing large numbers of AND, OR, and NOT gates were arranged in a single chip
without any predetermined function. Rather, the designer could design any type of
digital circuit and implement it by connecting the internal gates in a particular way. This
is usually done by opening up fuse links within the chip using computer-aided tools.
Eventually the equivalent of many PLDs on a single chip led to complex programmable
logic devices (CPLDs).
www.alljntuworld.in
JNTU World
Introduction
CLB
FF
Slice
LUT
LUT
FF
Slice
FF
LUT
FF
FF
LUT
FF
LUT
CLB
or
LUT
ld
Each CLB in the Spartan-3E FPGA contains four slices, each of which contains
two 16 x 1 RAM look-up tables (LUTs), which can implement any combinational logic
function of four variables. In addition to two look-up tables, each slice contains two D
flip-flops which act as storage devices for bits. The basic architecture of a Spartan-3E
FPGA is shown in Fig. 1.
Slice
LUT
Slice
FF
LUT
CLB
FF
CLB
TU
IOBs
JN
The BASYS board from Digilent contains a Xilinx Spartan3E-100 TQ144 FPGA.
This chip contains 240 CLBs arranged as 22 rows and 16 columns. There are therefore
960 slices with a total of 1,920 LUTs and flip-flops. This part also contains 73,728 bits
of block RAM. Half of the LUTs on the chip can be used for a maximum of 15,360 bits
of distributed RAM.
By contrast the Nexys-2 board from Digilent contains a Xilinx Spartan3E-500
FG320 FPGA. This chip contains 1,164 CLBs arranged as 46 rows and 34 columns.
There are therefore 4,656 slices with a total of 9,312 LUTs and flip-flops. This part also
contains 368,640 bits of block RAM. Half of the LUTs on the chip can be used for a
maximum of 74,752 bits of distributed RAM.
In general, FPGAs can implement much larger digital systems than CPLDs as
illustrated in Table 1. The column labeled No. of Gates is really equivalent gates as we
have seen that FPGAs really dont have AND and OR gates, but rather just RAM look-up
tables. (Each slice does include two AND gates and two XOR gates as part of carry and
arithmetic logic used when implementing arithmetic functions including addition and
www.alljntuworld.in
JNTU World
Introduction
multiplication.) Note from Table 1 that FPGAs can have the equivalent of millions of
gates and tens of thousands of flip-flops.
Table 1 Comparing Xilinx CPLDs and FPGAs
No. of Gates
No. of I/Os No. of CLBs No. of Flip-flops
36 - 288
ld
5,000 40,000
15,000 200,000
23,000 600,000
50,000 5,000,000
100,000 1,600,000
57,906 1,124,022
71,693 4,074,387
40,960 8,388,608
34 192
77 224
100 784
86 284
96 1,176
182 514 384 3,456
124 784 192 8,320
108 376 240 3,688
180 512 384 6,144
176 804 384 16,224
88 1,108 64 11,648
360 2,016
642 5,556
16,384 57,344
2,082 15,366 32,768 294,912
2,280 71,264 73,728 1,916,928
1,920 29,505 73,728 663,552
2,076 26,112 32,768 131,072
1,888 66,504 65,536 851,968
1,040 99,832 73,728 3,096,576
FPGAs
Spartan
Spartan II
Spartan IIE
Spartan 3
Spartan-3E
Virtex
Virtex E
Virtex-II
800 6,400
or
Xilinx Part
CPLDs
9500 family
JN
TU
www.alljntuworld.in
JNTU World
Introduction
JN
TU
or
ld
(a)
(b)
1 http://www.aldec.com/education/
2 http://www.xilinx.com
3 http://www.digilentinc.com
4 Digital Design Using Digilent FPGA Boards Verilog / Active-HDL Edition; available
from www.lbebooks.com.
www.alljntuworld.in
JNTU World
Introduction
ld
Verilog
JN
TU
or
5 Introduction to Digital Design Using Digilent FPGA Boards Block Diagram / VHDL Examples, LBE
Books, 2009.
6 Digital Design Using Digilent FPGA Boards Verilog / Active-HDL Edition, LBE Books, 2009.
7 Digital Design Using Digilent FPGA Boards VHDL / Active-HDL Edition, LBE Books, 2009.
www.alljntuworld.in
JNTU World
Example 1
Example 1
ld
or
Prerequisite knowledge:
None
Learned in this Example:
Use of Aldec Active-HDL Appendix A
JN
TU
1.2 LEDs
A light emitting diode (LED) emits light when current flows through it in the
positive direction as shown in Fig. 1.2. Current flows through the LED when the voltage
www.alljntuworld.in
JNTU World
or
ld
on the anode side (the wide side of the black triangle) is made higher than the voltage on
the cathode side (the straight line connected to the apex of the black triangle). When
current flows through a lighted LED the forward voltage across the LED is typically
between +1.5 and +2.0 volts. If voltage V2 in Fig. 1.2 is less than or equal to voltage V1
then no current can flow through the LED and therefore no light will be emitted. If
voltage V2 is greater than voltage V1 then current will flow through the resistor R and the
LED. The resistor is used to limit the amount of current that flows through the LED.
Typical currents needed to light LEDs range from 2 to 15 milliamps.
JN
TU
# Pin assignment
NET "btn<3>" LOC
NET "btn<2>" LOC
NET "btn<1>" LOC
NET "btn<0>" LOC
"an<3>"
"an<2>"
"an<1>"
"an<0>"
LOC
LOC
LOC
LOC
=
=
=
=
"p26";
"p32";
"p33";
"p34";
www.alljntuworld.in
JNTU World
Example 1
or
ld
JN
TU
www.alljntuworld.in
JNTU World
or
ld
Compiling the file sw2led.bde generates the Verilog file sw2led.v shown in
Listing 1.2. Alternatively, by selecting the hardware description editor (HDE) the
module statement and port declarations are automatically generated but you will need to
write your own assign statement. This can lead to the simpler Verilog program shown in
Listing 1.3 where we have combined the module statement and port declarations in a
single module statement that conforms to the 2001 Verilog standard. This format makes
it easier to see the input and output signals. We can also write a single assign statement
to replace the two assign statements in Listing 1.2. It is unnecessary to define the
intermediate bus BUS7[7:0] and because sw and ld are the same size we don't need to
include the [7:0] in the assign statement.
//
TU
endmodule
JN
www.alljntuworld.in
10
JNTU World
Example 1
ld
1.
2.
3.
4.
5.
or
At this point the switches are connected to the LEDs. Turning on a switch will
light up the corresponding LED.
Problem
The four pushbuttons on the BASYS and Nexys-2 boards are connected to pins on
the FPGA using the circuit shown in Fig. 1.4. The value of R is 4.7 k on the
BASYS board and 10 k on the Nexys-2 board. When the pushbutton is up the
two resistors pull the input down to ground and the input btn(i) to the FPGA is read
as a logic 0. When the pushbutton is pressed the input is pulled up to 3.3 V and the
input btn(i) to the FPGA is read as a logic 1. Create a .bde file using Active-HDL
that will connect the four pushbuttons to the rightmost four LEDs. Compile and
implement the program. Download the .bit file to the FPGA board and test it by
pressing the pushbuttons.
TU
1.1
3.3 V
btn(i)
JN
www.alljntuworld.in
JNTU World
2-Input Gates
11
Example 2
2-Input Gates
or
Prerequisite knowledge:
Appendix C Basic Logic Gates
Appendix A Use of Aldec Active-HDL
ld
In this example we will design a circuit containing six different 2-input gates.
Example 2a will show the simulation results using Aldec Active-HDL and Example 2b
will show how to synthesize the program to a Xilinx FPGA on a Digilent board.
JN
TU
Part 4 of the tutorial in Appendix A shows how to connect two inputs a and b to
the inputs of six different gates using the block diagram editor (BDE) in Active-HDL.
The result is shown in Fig. 2.1. Note that we have named the outputs of the gates the
name of the gate followed by an underscore. Identifier names in Verilog can contain any
letter, digit, underscore _, or $. The identifier can not begin with a digit or be a keyword.
Verilog is case sensitive.
The name of this file is gates2.bde. When you compile this file the Verilog
program gates2.v shown in Listing 2.1 is generated. We have modified the module
statement to conform to the 2001 Verilog standard as described in Example 1.
www.alljntuworld.in
12
JNTU World
Example 2
and_ = b & a;
nand_ = ~(b & a);
or_ = b | a;
nor_ = ~(b | a);
xor_ = b ^ a;
xnor_ = ~(b ^ a);
endmodule
or
assign
assign
assign
assign
assign
assign
ld
TU
The logic diagram in Fig. 2.1 contains six different gates. This logic circuit is
described by the Verilog program shown in Listing 2.1. The first line in Listing 2.1 is a
comment. Comments in Verilog follow the double slash //. All Verilog programs begin
with a module statement containing the name of the module (gates2 in this case) followed
by a list of all input and output signals together with their direction and type. We will
generally use lower case names for signals. The direction of the input and output signals
is given by the Verilog statements input, output, or inout (for a bi-directional signal).
The type of the signal can be either wire or reg. In Listing 2.1 all of the signals are of
type wire which you can think of as a wire in the circuit in Fig. 2.1 where actual voltages
could be measured. We will describe the reg type in Example 5.
To describe the output of each gate in Fig. 2.1 we simply write the logic equation
for that gate preceded by the keyword assign. These are concurrent assignment
statements which means that the statements can be written in any order.
JN
Part 4 of the tutorial in Appendix A shows how to simulate this Verilog program
using Active-HDL. The simulation produced in Appendix A is shown in Fig. 2.2. Note
that the waveforms shown in Fig. 2.2 verify the truth tables for the six gates. Also note
that two clock stimulators were used for the inputs a and b. By making the period of the
clock stimulator for the input a twice the period of the clock stimulator for the input b all
four combinations of the inputs a and b will be generated in one period of the input a.
www.alljntuworld.in
JNTU World
13
or
ld
2-Input Gates
JN
TU
Part 5 of the tutorial in Appendix A shows how to create a top-level design for the
gates2 circuit. In order to use the constraint files basys2.ucf or nexys2.ucf described in
Example 1 we must name the switch inputs sw[i] and the LED outputs ld[i]. This toplevel design, as created in Part 5 of Appendix A is shown in Fig. 2.3. The module gates2
in Fig. 2.3 contains the logic circuit shown in Fig. 2.1. Note that each wire connected to
a bus must be labeled to identify its connection to the bus lines.
www.alljntuworld.in
14
JNTU World
Example 2
or
gates2 U1
(
.a(sw[1]),
.and_(ld[5]),
.b(sw[0]),
.nand_(ld[4]),
.nor_(ld[3]),
.or_(ld[2]),
.xnor_(ld[1]),
.xor_(ld[0])
);
ld
Compiling the top-level design shown in Fig. 2.3 will generate the Verilog
program shown in Listing 2.2. The inputs are now the two rightmost slide switches,
sw[1:0], and the outputs are the six right-most LEDs ld[5:0]. To associate these inputs
and outputs with the inputs a and b and the six output in the gates2 module in Fig. 2.1
and Listing 2.1 we use the Verilog instantiation statement
This Verilog instantiation statement begins with the name of the module being
instantiated, in this case gates2 from Listing 2.1. This is followed by an arbitrary name
for this module in the top-level design. Here we call it U1. Then in parentheses the
inputs and outputs in Listing 2.1 are associated with corresponding inputs and outputs in
the top-level design in Fig. 2.3. Note that we connect the input a in Listing 2.1 to the
input sw[1] on the FPGA board. The input b in Listing 2.1 is connected to sw[0] and the
outputs and_, nand_, or_, nor_, xor_, and xnor_ are connected to the corresponding LED
outputs ld[5:0].
Follow the steps in the tutorial in Appendix A and implement this design on the
FPGA board. Note that when you change the settings of the two right-most slide
switches the LEDs will indicate the outputs of the six gates.
TU
JN
gates2 U1
(
.a(sw[1]),
.and_(ld[5]),
.b(sw[0]),
.nand_(ld[4]),
.nor_(ld[3]),
.or_(ld[2]),
.xnor_(ld[1]),
.xor_(ld[0])
);
www.alljntuworld.in
JNTU World
Multiple-Input Gates
15
Example 3
Multiple-Input Gates
or
Prerequisite knowledge:
Appendix C Basic Logic Gates
Appendix A Use of Aldec Active-HDL
ld
The AND, OR, NAND, NOR, XOR, and XNOR gates we x[1]
studied in Example 1 had two inputs. The basic definitions hold x[2]
AND
z
for multiple inputs. A multiple-input AND gate is shown in Fig. x[n]
2.19. The output of an AND gate is HIGH only if all inputs are
HIGH. There are three ways we could describe this multipleFigure 3.1
input AND gate in Verilog. First we could simply write the Multiple-input AND gate.
logic equation as
.
TU
(3.1)
(3.2)
This produces the same result as the statement (3.1) with much less writing.
Finally, we could use the following gate instantiation statement for an AND gate.
(3.3)
JN
and(z,x[1],x[2],...,x[n]);
In this statement the first parameter in the parentheses is the name of the output port.
This is followed by a list of all input signals.
A multiple-input OR gate is shown in Fig. 3.2. The output of an OR gate is LOW
only if all inputs are LOW. Just as with the AND gate there are
three ways we can describe this multiple-input OR gate in x[1]
x[2]
OR
Verilog. We can write the logic equation as
z
x[n]
.
assign z = x[1] | x[2] | ... | x[n];
Figure 3.2
Multiple-input OR gate.
assign z = |x;
www.alljntuworld.in
16
JNTU World
Example 3
ld
A multiple-input NAND gate is shown in Fig. 3.3. The output of a NAND gate is
LOW only if all inputs are HIGH. We can write the logic
x[1]
equation as
x[2]
NAND
z
.
x[n]
Figure 3.3
Multiple-input NAND gate.
nand(z,x[1],x[2],...,x[n]);
or
A multiple-input NOR gate is shown in Fig. 3.4. The output of a NOR gate is
HIGH only if all inputs are LOW. We can write the logic
x[1]
equation as
x[2]
NOR
z
.
assign z = ~(x[1] | x[2] | ... | x[n]);
x[n]
Figure 3.4
Multiple-input NOR gate.
assign z = ~|x;
TU
JN
x[1]
x[2]
x[3]
x[4]
XOR
Figure 3.5
Multiple-input XOR gate.
x[1]
x[2]
XNOR
www.alljntuworld.in
JNTU World
Multiple-Input Gates
17
example for the answer.) Following the methods we used for the previous multiple-input
gates we can write the logic equation as
.
assign z = ~(x[1] ^ x[2] ^ ... ^ x[n]);
xnor(z,x[1],x[2],...,x[n]);
ld
or we can use the following gate instantiation statement for an XOR gate.
JN
TU
or
Use the block diagram editor (BDE) in Active-HDL to create the logic circuit
called gates4.bde shown in Fig. 3.7. A simulation of this circuit is shown in Fig. 3.8.
From this simulation we see that the output of an XOR gate is HIGH only if the number
of HIGH inputs is ODD.
If you look at the file gates4.v that is generated when you compile gates4.bde you
will see that Active-HDL defines separate modules for the 4-input AND, OR, and XOR
gates and then uses a Verilog instantiation statement to "wire" them together.
Alternatively, we could use the HDE editor to write the simpler Verilog program
called gates4b.v shown in Listing 3.1 that uses reduction operators to implement the
three 4-input gates. This Verilog program will produce the same simulation as shown in
Fig. 3.8.
www.alljntuworld.in
Example 3
ld
18
JNTU World
or
TU
endmodule
JN
Fig. 3.9 shows the block diagram of the top-level design gates4_top.bde. The
module gates4 shown in Fig. 3.9 contains the logic circuit shown in Fig. 3.4. If you
compile gates4_top.bde the Verilog program gates4_top.v shown in Listing 3.2 will be
generated. Compile, synthesize, implement, and download this design to the FPGA
board.
www.alljntuworld.in
JNTU World
Multiple-Input Gates
19
gates4 U1
(
.and4_(ld[2]),
.or4_(ld[1]),
.x(sw),
.xor4_(ld[0])
);
Problem
or
endmodule
ld
Use the BDE to create a logic circuit containing 4-input NAND, NOR, and XNOR
gates. Simulate your design and verify that the output of an XNOR gate is HIGH
only if the number of HIGH inputs is EVEN. Create a top-level design that connects
the four inputs to the rightmost four slide switches and the three outputs to the three
rightmost LEDs. Implement your design and download it to the FPGA board.
3.2
x[1]
x[0]
y[0]
y[1]
y[2]
y[3]
en
JN
TU
3.1
www.alljntuworld.in
20
JNTU World
Example 4
Example 4
Equality Detector
ld
In this example we will design a 2-bit equality detector using two NAND gates
and an AND gate.
or
Prerequisite knowledge:
Appendix C Basic Logic Gates
Appendix A Use of Aldec Active-HDL
The truth table for a 2-input XNOR gate is shown in Fig. 4.1. Note that the
output z is 1 when the inputs x and y are equal. Thus, the XNOR gate can be used as a
1-bit equality detector.
XNOR
x
y
z = ~(x ^ y)
z = x ~^ y
0
0
1
1
0
1
0
1
1
0
0
1
TU
JN
By using two XNOR gates and an AND gate we can design a 2-bit equality
detector as shown in Fig. 4.2. Use the BDE to create the file eqdet2.bde using ActiveHDL.
www.alljntuworld.in
JNTU World
Equality Detector
21
JN
TU
endmodule
or
ld
If you compile the file eqdet2.bde Active-HDL will generate the Verilog program
eqdet2.v shown in Listing 4.1. A simulation of eqdet2.bde is shown in Fig. 4.3. Note
that the output eq is 1 only if a[1:0] is equal to b[1:0].
Create a top-level design called eqdet2_top.bde that connects a[1:0] and b[1:0] to
the rightmost four slide switches and connects the output eq to ld[0]. Implement your
design and download it to the FPGA board.
www.alljntuworld.in
JNTU World
Example 5
22
Example 5
ld
In this example we will show how to design a 2-to-1 multiplexer and will
introduce the Verilog if statement. Section 5.1 will define a multiplexer and derive the
logic equations for a 2-to-1 multiplexer. Section 5.2 will illustrate the use of two
versions of the Verilog if statement.
or
Prerequisite knowledge:
Karnaugh Maps Appendix D
Use of Aldec Active-HDL Appendix A
5.1 Multiplexers
TU
~s & a | s & b
(5.1)
ab
00
01
JN
0
1
11
10
y = ~s & a | s & b
a
b
2x1
MUX
s
0
0
0
0
1
1
1
1
a
0
0
1
1
0
0
1
1
b
0
1
0
1
0
1
0
1
Figure 5.2
K-map for a 2-to-1 multiplexer
y
0
0
1
1
0
1
0
1
www.alljntuworld.in
JNTU World
23
ld
Use the BDE to create the block diagram mux21.bde shown in Fig. 5.3 that
implements logic equation (5.1). Compiling mux21.bde will generate a Verilog file,
mux21.v, that is equivalent to Listing 5.1. A simulation of mux21.bde is shown in Fig.
5.4. Note in the simulation that y = a if s = 0 and y = b if s = 1.
or
JN
TU
endmodule
www.alljntuworld.in
24
JNTU World
Example 5
or
ld
The Verilog if statement must be cont ained in an always block as shown in Listing 5.2.
Note that y must be declared to be of type reg because it is assigned a value within the
always block. The notation @(*) in the always statement is equivalent to @(a,b,s) where
a, b, s is called the sensitivity list. Any time any of these input values change the if
statement within the always block is executed. The use of the * notation is a convenience
that prevents you from omitting any of the signals or inputs used in the always block. A
Verilog program can contain more than one always blocks, and these always blocks are
executed concurrently. The Verilog code in Listing 5.2 will be compiled to produce the
logic circuit shown in Fig. 5.3. A simulation of the Verilog code in Listing 5.2 will
produce the same waveform as shown in Fig. 5.4.
TU
always @(*)
if(s == 0)
y = a;
else
y = b;
endmodule
JN
www.alljntuworld.in
JNTU World
25
Example 6
or
Prerequisite knowledge:
Example 5 2-to-1 Multiplexer
ld
In this example we will show how to design a quad 2-to-1 multiplexer. In Section
6.1 we will make the quad 2-to-1 multiplexer by wiring together four of the 2-to-1
multiplexers that we designed in Example 5. In Section 6.2 we will show how the quad
2-to-1 multiplexer can be designed using a single Verilog if statement. Finally, in
Section 6.3 we will show how to use a Verilog parameter to define a generic 2-to-1
multiplexer with arbitrary bus sizes.
JN
TU
Figure 6.1 The quad 2-to-1 MUX, mux24.bde, contains four 2-to-1 MUXs
www.alljntuworld.in
26
JNTU World
Example 6
If you compile the file mux24.bde Active-HDL will generate the Verilog program
mux24.v shown in Listing 6.1. A simulation of mux24.bde is shown in Fig. 6.2. Note
that the output y[3:0] will be either a[3:0] or b[3:0] depending on the value of s.
mux21 U2
(
.a(a[2]),
.b(b[2]),
.s(s),
.y(y[2])
);
or
mux21 U1
(
.a(a[3]),
.b(b[3]),
.s(s),
.y(y[3])
);
ld
mux21 U3
(
.a(a[1]),
.b(b[1]),
.s(s),
.y(y[1])
);
TU
mux21 U4
(
.a(a[0]),
.b(b[0]),
.s(s),
.y(y[0])
);
JN
endmodule
www.alljntuworld.in
JNTU World
27
or
ld
Use the BDE to create the top-level design called mux21_top.bde shown in Fig.
6.3. Note that a[3:0] are connected to the four leftmost slide switches, b[3:0] are
connected to the rightmost four slide switches, and y[3:0] are connected to the rightmost
LEDs. Also note that s is connected to btn[0], and the input btn[0:0] must be declared as
an array, even though there is only one element, so that we can use the constraint file
basys2.ucf or nexys2.ucf without change. Implement your design and download it to the
FPGA board. Test the operation of the quad 2-to-1 multiplexer by setting the switch
values and pressing pushbutton btn[0].
If you compile the file mux24_top.bde Active-HDL will generate the Verilog
program mux24_top.v shown in Listing 6.2. A simulation of mux24_top.bde is shown in
Fig. 6.4.
Figure 6.3 Top-level design for testing the quad 2-to-1 MUX
TU
JN
mux24 U1
(
.a(sw[7:4]),
.b(sw[3:0]),
.s(btn[0]),
.y(ld)
);
endmodule
www.alljntuworld.in
28
JNTU World
Example 6
ld
endmodule
always @(*)
if(s == 0)
y = a;
else
y = b;
or
JN
TU
www.alljntuworld.in
JNTU World
29
ld
always @(*)
if(s == 0)
y = a;
else
y = b;
or
endmodule
TU
mux2g #(
.N(8))
M8 (.a(a),
.b(b),
.s(s),
.y(y)
);
JN
endmodule
Figure 6.5 Simulation result from the Verilog program in Listing 6.5
www.alljntuworld.in
30
JNTU World
Example 7
Example 7
4-to-1 Multiplexer
ld
In this example we will show how to design a 4-to-1 multiplexer. In Section 7.1
we will make a 4-to-1 multiplexer by wiring together three of the 2-to-1 multiplexers that
we designed in Example 5. In Section 7.2 we will derive the logic equation for a 4-to-1
MUX. In Section 7.3 we will show how a 4-to-1 multiplexer can be designed using a
single Verilog case statement and in Section 7.4 we design a quad 4-to-1 multiplexer.
or
Prerequisite knowledge:
Example 5 2-to-1 Multiplexer
s1
0
0
1
1
s0
0
1
0
1
z
c0
c1
c2
c3
Figure 7.1
Truth table for a 4-to-1 MUX
JN
TU
Figure 7.2 The 4-to-1 MUX, mux41.bde, contains four 2-to-1 MUXs
www.alljntuworld.in
JNTU World
4-to-1 Multiplexer
31
ld
In Fig. 7.2 when s[1] = 0 it is v, the output of U2 that gets through to z. If s[0] = 0
in U2 then it is c[0] that gets through to v and therefore to z. If s[0] = 1 in U2 then it is
c[1] that gets through to v and therefore to z.
If, on the other hand, s[1] = 1 in U1 then it is w, the output of U3 that gets through
to z. If s[0] = 0 in U3 then it is c[2] that gets through to w and therefore to z. If s[0] = 1
in U3 then it is c[3] that gets through to w and therefore to z. Thus you can see that the
circuit in Fig. 7.2 will implement the truth table in Fig. 7.1.
When you compile the file mux41.bde Active-HDL will generate the Verilog
program mux41.v shown in Listing 7.1. A simulation of mux41.bde is shown in Fig. 7.3.
Note that the output z will be one of the four inputs c[3:0] depending on the value of
s[1:0].
or
// Internal signals
wire v;
// output of mux M1
wire w;
// output of mux M2
TU
// Module instantiations
mux21 U1
(
.a(v),
.b(w),
.s(s[1]),
.y(z)
);
mux21 U2
(
.a(c[0]),
.b(c[1]),
.s(s[0]),
.y(v)
);
JN
mux21 U3
(
.a(c[2]),
.b(c[3]),
.s(s[0]),
.y(w)
);
endmodule
If you were going to create this top-level design using HDE instead of BDE you
would begin by defining the inputs c[3:0] and s[1:0] and the output z and the two wires v
and w. You would then wire the three modules together using the three module
instantiation statements shown in Listing 7.1.
The easiest way to generate this module instantiation statement is to first compile
the file mux21.v from Example 5 using Active-HDL, expand the library icon (click the
www.alljntuworld.in
32
JNTU World
Example 7
or
ld
plus sign), right click on mux21, and select Copy Verilog Instantiation as shown in Fig.
7.4. Paste this into your top-level mux41.v file.
JN
TU
www.alljntuworld.in
JNTU World
4-to-1 Multiplexer
33
or
mux21 U1 (.a(v),
.b(w]),
.s(s[1]),
.y(z)
);
ld
Make three copies of this prototype and change the name of Label1 to U1, U2,
and U3 in the three statements. Now you just wire up each input and output variable
by changing the values in the parentheses to the signal that it is connected to. For
example, the mux U1 input a is connected to the wire v so we would write .a(v). In a
similar way the mux input b is connected to wire w and the mux input s is connected to
input s[1]. The mux output y is connected to the output z in Fig. 7.2. Thus, the final
version of this module instantiation statement would be
The other two modules, U2 and U3, are wired up using similar module
instantiation statements.
JN
TU
The 4-to-1 MUX designed in Fig. 7.2 can be represented by the logic symbol
shown in Fig. 7.5. This multiplexer acts like a digital switch in which one of the inputs
c[3:0] gets connected to the output z. The switch is controlled by the two control lines
s[1:0]. The two bits on these control lines select one of the four inputs to be "connected"
to the output. Note that we constructed this 4-to-1 multiplexer using three 2-to-1
multiplexers in a tree fashion as shown in Fig. 7.2.
Recall from Eq. (5.1) in Example 5 that the logic equation for a 2-to-1 MUX is
given by
y =
~s & a | s & b
(7.1)
www.alljntuworld.in
34
JNTU World
Example 7
Applying this equation to the three 2-to-1 MUXs in Fig. 7.2 we can write the
equations for that 4 x 1 MUX as follows.
v = ~s0 & c0 | s0 & c1
w = ~s0 & c2 | s0 & c3
z = ~s1 & v | s1 & w
ld
z = ~s1 & (~s0 & c0 | s0 & c1) | s1 & (~s0 & c2 | s0 & c3)
or
c0
c1
c2
c3
(7.2)
or
Equation (7.2) for z also follows from the truth table in Fig. 7.1. Note that the
tree structure in Fig. 7.2 can be expanded to implement an 8-to-1 multiplexer and a 16-to1 multiplexer.
A Verilog program that implements a 4-to-1 MUX using the logic equation (7.2)
is given in Listing 7.2. A simulation of this program will produce the same result as in
Fig. 7.3 (without the wire signals v and w).
TU
c[0]
c[1]
c[2]
c[3];
endmodule
JN
www.alljntuworld.in
JNTU World
4-to-1 Multiplexer
35
ld
will assign the value of c[2] to the output z when the input value s[1:0] is equal to 2
(binary 10). Note that the output z must be of type reg because its value is assigned
within an always clause.
In the case statement the alternative value preceding the colon in each line
represents the value of the case parameter, in this case the 2-bit input s. These values are
decimal values by default. If you want to write a hex value you precede the number with
h as in hA which is a hex value A. Similarly, a binary number is preceded with a b as
in b1010 which has the same value (10) as hA. Normally, binary numbers are preceded
with the number of bits in the number such as 4b107. Using this notation, the number
8b110011 will be the binary number 00110011.
or
always @(*)
case(s)
0: z = c[0];
1: z = c[1];
2: z = c[2];
3: z = c[3];
default: z = c[0];
endcase
endmodule
TU
All case statements should include a default line as shown in Listing 7.3. This is
because all cases need to be covered and while it looks as if we covered all cases in
Listing 7.3, Verilog actually defines four possible values for each bit, namely 0 (logic
value 0), 1 (logic value 1), Z (high impedance), and X (unkown value).
A simulation of the program in Listing 7.3 will produce the same result as in Fig.
7.3 (without the wire signals v and w).
JN
www.alljntuworld.in
36
JNTU World
Example 7
endcase
TU
endmodule
or
always @(*)
case(s)
0: z = x[3:0];
1: z = x[7:4];
2: z = x[11:8];
3: z = x[15:12];
default: z = x[3:0];
ld
JN
www.alljntuworld.in
JNTU World
37
Example 8
or
ld
The Nexys-2 board has an onboard 50 MHz clock. The BASYS board has a
jumper that allows you to set the clock to 100 MHz, 50 MHz, or 25 MHz. All of the
examples in this book will assume an input clock frequency of 50 MHz. If you are using
the BASYS board you should remove the clock jumper, which will set the clock
frequency to 50 MHz. This 50 MHz clock signal is a square wave with a period of 20 ns.
The FPGA pin associated with this clock signal is defined in the constraints file
basys2.ucf or nexys2.ucf with the name mclk.
In this example we will show how to design an N-bit counter in Verilog and how
to use a counter to generate clock signals of lower frequencies.
Prerequisite knowledge:
Appendix A Use of Aldec Active-HDL
TU
The BDE symbol for an N-bit counter is shown in Fig. 8.1. If the input clr = 1
then all N of the outputs q[i] are cleared to zero asynchronously, i.e., regardless of the
value of the input clk. If clr = 0, then on the next rising edge of the clock input clk the Nbit binary output q[N-1:0] will be incremented by 1. That is, on the rising edge of the
clock the N-bit binary output q[N-1:0] will count from 0 to N-1 and then wrap around to
0.
JN
The Verilog program shown in Listing 8.1 was used to generate the symbol
shown in Fig. 8.1. Note that the sensitivity list of the always statement contains the
phrase
posedge clk or posedge clr
This means that the if statement within the always block will execute whenever either clr
or clk goes high. If clr goes high then the output q[N-1:0] will go to zero. On the other
hand if clr = 0 and clk goes high then the output q[N-1:0] will be incremented by 1.
The default value of the parameter N in Listing 8.1 is 4. A simulation of this 4-bit
counter is shown in Fig. 8.2. Note that this counter counts from 0 to F and then wraps
www.alljntuworld.in
38
JNTU World
Example 8
around to 0. To instantiate an 8-bit counter from Listing 8.1 that would count from 0
255 (or 00 FF hex) you would use an instantiation statement something like
counter #(
.N(8))
cnt16 (.clr(clr),
.clk(clk),
.q(q)
);
or
ld
You can also set the value of the parameter N from the block diagram editor (BDE) by
right-clicking on the symbol in Fig. 8.1 and selecting Properties and then the Parameters
tab.
//
N-bit counter
always @(posedge clk or posedge clr)
begin
if(clr == 1)
q <= 0;
else
q <= q + 1;
end
JN
TU
endmodule
www.alljntuworld.in
JNTU World
39
ld
In the simulation in Fig. 8.2 note that the output q[0] is a square wave at half the
frequency of the input clk. Similarly, the output q[1] is a square wave at half the
frequency of the input q[0], the output q[2] is a square wave at half the frequency of the
input q[1], and the output q[3] is a square wave at half the frequency of the input q[2].
Note how the binary numbers q[3:0] in Fig. 8.2 count from 0000 to 1111.
The simulation shown in Fig. 8.2 shows how we can obtain a lower clock
frequency by simply using one of the outputs q[i]. We will use this feature to produce a
24-bit clock divider in the next section.
or
The simulation in Fig. 8.2 shows that the outputs q[i] of a counter are square
waves where the output q[0] has a frequency half of the clock frequency, the output q[1]
has a frequency half of q[0], etc. Thus, a counter can be used to divide the frequency f of
a clock, where the frequency of the output q(i) is fi = f 2i +1 . The frequencies and
periods of the outputs of a 24-bit counter driven by a 50 MHz clock are shown in Table
8.1. Note in Table 8.1 that the output q[0] has a frequency of 25 MHz, the output q[17]
has a frequency of 190.73 Hz, and the output q[23] has a frequency of 2.98 Hz.
JN
TU
www.alljntuworld.in
40
JNTU World
Example 8
The Verilog program shown in Listing 8.2 is a 24-bit counter that has three
outputs, a 25 MHz clock (clk25), a 190 Hz clock (clk190), and a 3 Hz clock (clk3). You
can modify this clkdiv module to produce any output frequency given in Table 8.1. We
will use such a clock divider module in many of our top-level designs.
or
ld
// 24-bit counter
always @(posedge clk or posedge clr)
begin
if(clr == 1)
q <= 0;
else
q <= q + 1;
end
assign clk190 = q[17];
assign clk25 = q[0];
assign clk3 = q[23];
TU
endmodule
// 190 Hz
// 25 MHz
// 3 Hz
JN
Note in Listing 8.2 that we define the internal signal q[23:0] of type reg. It must
be of type reg because its value is assigned within an always block. The BDE symbol
generated by compiling Listing 8.2 is shown in Fig. 8.3. You can edit either Listing 8.2
or the block diagram shown in Fig. 8.3 to bring out only the clock frequencies you need
in a particular design. For example, the top-level design shown in Fig. 8.4 will cause the
eight LEDs on the FPGA board to count in binary at a rate of about three counts per
second. The corresponding top-level Verilog program is shown in Listing 8.3.
www.alljntuworld.in
JNTU World
41
ld
or
TU
clkdiv U1
(
.clk3(clk3),
.clr(btn[3]),
.mclk(mclk)
);
counter
#(
.N(8))
U2
(
.clk(clk3),
.clr(btn[3]),
.q(ld[7:0])
);
JN
endmodule
www.alljntuworld.in
42
JNTU World
Example 9
Example 9
7-Segment Decoder
ld
In this section we will show how to design a 7-segment decoder using Karnaugh
maps and write a Verilog program to implement the resulting logic equations. We will
also solve the same problem using a Verilog case statement.
or
Prerequisite knowledge:
Karnaugh maps Appendix D
case statement Example 7
LEDs Example 1
Seven LEDs can be arranged in a pattern to form different digits as shown in Fig.
9.1. Digital watches use similar 7-segment displays using liquid crystals rather than
LEDs. The red digits on digital clocks are LEDs. Seven segment displays come in two
flavors: common anode and common cathode. A common anode 7-segment display has
all of the anodes tied together while a common cathode 7-segment display has all the
cathodes tied together as shown in Fig. 9.1.
+3.3V
TU
JN
Common
Anode
g
Common
Cathode
Figure 9.1 A 7-segment display contains seven light emitting diodes (LEDs)
The BASYS and Nexys2 boards have four common-anode 7-segment displays.
This means that all the anodes are tied together and connected through a pnp transistor to
+3.3V. A different FPGA output pin is connected through a 100 current-limiting
resistor to each of the cathodes, a g, plus the decimal point. In the common-anode case,
an output 0 will turn on a segment and an output 1 will turn it off. The table shown in
www.alljntuworld.in
JNTU World
7-Segment Decoder
43
Fig. 9.2 shows output cathode values for each segment a g needed to display all hex
values from 0 F.
b
0
0
0
0
0
1
1
0
0
0
0
1
1
0
1
1
c
0
0
1
0
0
0
0
0
0
0
0
0
1
0
1
1
d
0
1
0
0
1
0
0
1
0
0
1
0
0
0
0
1
e
0
1
0
1
1
1
0
1
0
1
0
0
0
0
0
0
f
0
1
1
1
0
0
0
1
0
0
0
0
0
1
0
0
g
1
1
0
0
0
0
0
1
0
0
0
0
1
0
0
0
1 = off
0 = on
ld
a
0
1
0
0
1
0
0
0
0
0
0
1
0
1
0
0
or
x
0
1
2
3
4
5
6
7
8
9
A
b
C
d
E
F
JN
TU
00
01
~x3 & x2 & ~x1
01
11
10
~x3 & x0
11
10
www.alljntuworld.in
44
JNTU World
Example 9
You can write the Karnaugh maps for the other six segments and then write the
Verilog program for the 7-segment decoder shown in Listing 9.1. A simulation of this
program is shown in Fig. 9.5. Note that the simulation agrees with the truth table in Fig.
9.2.
ld
// b
// c
// d
// e
// f
// g
JN
TU
or
www.alljntuworld.in
JNTU World
7-Segment Decoder
45
ld
We can use a Verilog case statement to design the same 7-segment decoder that
we designed in Section 9.2 using Karnaugh maps. The Verilog program shown in Listing
9.2 is a hex-to-seven-segment decoder that converts a 4-bit input hex digit, 0 F, to the
appropriate 7-segment codes, a g. The case statement in Listing 9.2 directly
implements the truth table in Fig. 9.2. Recall that a typical line in the case statement,
such as
3: a_to_g = 7'b0000110;
JN
TU
or
will assign the 7-bit binary value, 0000110, to the 7-bit array, a_to_g, when the input
hex value x[3:0] is equal to 3 (0011). In the array a_to_g the value a_to_g[6]
corresponds to segment a and the value a_to_g[0] corresponds to segment g. Recall that
in Verilog a string of binary bits is preceded by nb, where n is the number of binary bits
in the string.
In the case statement the value preceding the colon in each line represents the
value of the case parameter, in this case the 4-bit input x. Also note that hex values such
as A are written as hA.
Recall that all case statements should include a default line as shown in Listing
9.2. This is because all cases need to be covered and while it looks as if we covered all
cases in Listing 6.2, Verilog actually defines four possible values for each bit, namely 0
(logic value 0), 1 (logic value 1), Z (high impedance), and X (unkown value).
A simulation of Listing 9.2 will produce the same results as shown in Fig. 9.5. It
should be clear from this example that using the Verilog case statement is often easier
than solving for the logic equations using Karnaugh maps.
To test the 7-segment displays on the FPGA board you could create the design
hex7seg_top.bde shown in Fig. 9.6. This design uses the Verilog program hex7seg.v
from Listing 9.2 and produces a top-level Verilog program hex7seg_top.v equivalent to
Listing 9.3. Each of the four digits on the 7-segment display is enabled by one of the
active low signals an[3:0] and all digits share the same a_to_g[6:0] signals. If an[3:0] =
0000 then all digits are enabled and display the same hex digit. This is what we do in
Fig. 9.6 and Listing 9.3. Making the output dp = 1 will cause the decimal points to be
off. You should be able to display all of the hex digits from 0 F by changing the four
rightmost switches.
www.alljntuworld.in
46
JNTU World
Example 9
or
always @(*)
case(x)
0: a_to_g = 7'b0000001;
1: a_to_g = 7'b1001111;
2: a_to_g = 7'b0010010;
3: a_to_g = 7'b0000110;
4: a_to_g = 7'b1001100;
5: a_to_g = 7'b0100100;
6: a_to_g = 7'b0100000;
7: a_to_g = 7'b0001111;
8: a_to_g = 7'b0000000;
9: a_to_g = 7'b0000100;
'hA: a_to_g = 7'b0001000;
'hb: a_to_g = 7'b1100000;
'hC: a_to_g = 7'b0110001;
'hd: a_to_g = 7'b1000010;
'hE: a_to_g = 7'b0110000;
'hF: a_to_g = 7'b0111000;
default: a_to_g = 7'b0000001;
endcase
endmodule
ld
// 0
TU
// all digits on
// dp off
JN
assign an = 4'b0000;
assign dp = 1;
hex7seg D4 (.x(sw),
.a_to_g(a_to_g)
);
endmodule
www.alljntuworld.in
JNTU World
47
Example 10
ld
7-Segment Displays:
x7seg and x7segb
In this example we will show how to display different hex values on the four 7segment displays.
or
Prerequisite knowledge:
Karnaugh maps Appendix D
case statement Example 7
LEDs Example 1
JN
TU
The two OR gates in Fig. 10.2 will implement these logic equations for s[1:0].
btn[3] btn[2] btn[1] btn[0]
0
0
0
0
0
0
0
1
0
0
1
0
0
1
0
0
1
0
0
0
s[1]
X
0
0
1
1
s[0]
X
0
1
0
1
www.alljntuworld.in
Example 10
or
ld
48
JNTU World
Figure 10.2 BDE circuit mux7seg.bde for multiplexing the four 7-segment displays
TU
JN
assign x = 'h1234;
hex7seg U1
(
.a_to_g(a_to_g), .x(digit));
mux44 U2
(
.s(s), .x(x), .z(digit));
assign s[0] = btn[3] | btn[1];
assign s[1] = btn[3] | btn[2];
assign an = ~btn;
endmodule
www.alljntuworld.in
JNTU World
49
TU
or
ld
We saw in Section 10.1 that to display a 16-bit hex value on the four 7-segment
displays we must multiplex the four hex digits. You can only make it appear that all four
digits are on by multiplexing them fast enough (greater than 30 times per second) so that
your eyes retain the values. This is the same way that your TV works where only a
single picture element (pixel) is on at any one time, but the entire screen is refreshed 30
times per second so that you perceive the entire image. To do this the value of s[1:0] in
Fig. 10.2 must count from 0 to 3 continually at this fast rate. At the same time the value
of the outputs an[3:0] must be synchronized with s[1:0] so as to enable the proper digit at
the proper time. A circuit for doing this is shown in Fig. 10.3. The outputs an[3:0] will
satisfy the truth table in Fig. 10.4. Note that each output an[i] is just the maxterm M[i] of
q[1:0].
JN
Figure 10.3 BDE circuit x7seg.bde for displaying x[15:0] on the four 7-segment displays
q[1]
0
0
1
1
q[0]
0
1
0
1
A simulation of x7seg.bde is shown in Fig. 10.5. Note how the an[3:0] output
selects one digit at a time to display the value 1234 on the 7-segment displays. When
x7seg.bde is compiled it creates a Verilog program that is equivalent to Listing 10.2. The
top-level design shown in Fig. 10.6 can be used to test the x7seg module on the FPGA
board. The Verilog program corresponding to this top-level design is given in Listing
10.3. Note that the x7seg module requires a 190 Hz clock generated by the clock divider
module clkdiv from Example 8.
www.alljntuworld.in
Example 10
or
ld
50
JNTU World
JN
TU
hex7seg U1
(
.a_to_g(a_to_g),.x(digit));
mux44 U2
(
.s({q[1:0]}),.x(x),.z(digit));
counter
#(
.N(2)) U3
(
.clk(cclk),.clr(clr),.q(q[1:0]));
endmodule
www.alljntuworld.in
JNTU World
51
ld
or
TU
assign x = 'h1234;
assign dp = 1;
clkdiv U1
(
.clk190(clk190),
.clr(btn[3]),
.mclk(mclk)
);
JN
x7seg U3
(
.a_to_g(a_to_g),
.an(an),
.cclk(clk190),
.clr(btn[3]),
.x(x)
);
endmodule
www.alljntuworld.in
52
JNTU World
Example 10
assign x = {sw,btn[2:0],5'b01010};
ld
called x7segb, that displays leading zeros as blanks is shown in Listing 10.4. This is
done by writing logic equations for aen[3:0] that depend on the values of x[15:0]. For
example, aen[3] will be 1 (and thus digit 3 will not be blank) if any one of the top four
bits of x[15:0] is 1. Similarly, aen[2] will be 1 if any one of the top eight bits of x[15:0]
is 1, and aen[1] will be 1 if any one of the top twelve bits of x[15:0] is 1. Note that
aen[0] is always 1 so that digit 1 will always be displayed even if it is zero.
To test the module x7segb you can run the top-level design shown in Listing 10.4
that will display the value of x on the 7-segment displays where x is defined by the
following statement:
// digit 0 = A
or
The curly brackets {--,--} are used for concatenation in Verilog. In this case we form the
16-bit value of x by concatenating the eight switches, the three right-most pushbuttons,
and the five bits 01010. Note that if all switches are off an A will be displayed on digit 0
with three leading blanks. Turning on the switches and pushing the three right-most
pushbuttons will display various hex numbers always with leading blanks.
TU
dp = 1;
aen[3:0] for leading blanks
aen[3] = x[15] | x[14] | x[13] | x[12];
aen[2] = x[15] | x[14] | x[13] | x[12]
| x[11] | x[10] | x[9] | x[8];
assign aen[1] = x[15] | x[14] | x[13] | x[12]
| x[11] | x[10] | x[9] | x[8]
| x[7] | x[6] | x[5] | x[4];
assign aen[0] = 1;
// digit 0 always on
JN
assign
// set
assign
assign
www.alljntuworld.in
JNTU World
or
ld
53
// 0
// Digit select
always @(*)
begin
an = 4'b1111;
if(aen[s] == 1)
an[s] = 0;
end
TU
// 2-bit counter
always @(posedge cclk or posedge clr)
begin
if(clr == 1)
s <= 0;
else
s <= s + 1;
end
JN
endmodule
www.alljntuworld.in
54
JNTU World
Example 10
ld
JN
TU
endmodule
x7segb X2 (.x(x),
.clk(clk),
.clr(btn[3]),
.a_to_g(a_to_g),
.an(an),
.dp(dp)
);
or
www.alljntuworld.in
JNTU World
55
Example 11
or
Prerequisite knowledge:
Basic Gates Appendix C
Equality Detector Example 6
Quad 2-to-1 Multiplexer Example 6
7-Segment Displays Example 10
ld
In this example we will design a circuit that converts a 6-bit signed number to a 4bit output that gets saturated at -8 and +7.
JN
TU
Figure 11.1 shows a circuit called sat4bit.bde that was described in the November
2001 issue of NASA Tech Briefs. The circuit will take a 6-bit twos complement number
with a signed value between 32 and +31 and convert it to a 4-bit twos complement
number with a signed value between 8 and +7. Negative input values less than 8 will
be saturated at 8. Positive input values greater than +7 will be saturated at +7.
Note that the two XNOR gates and the AND gate form an equality detector whose
output s is 1 when x[3], x[4], and x[5] are all equal (see Example 4). This will be the case
when the 6-bit input number x[5:0] is between -8 and +7. In this case output y[3:0] of the
quad 2-to-1 MUX will be connected to the input x[3:0]. If the top three bits of x[5:0] are
not equal and x[5] is 1 then the input value will be less than -8 and the output y[3:0] of
the quad 2-to-1 MUX will be saturated at -8. On the other hand if the top three bits of
x[5:0] are not equal and x[5] is 0 then the input value will be greater than +7 and the
output y[3:0] of the quad 2-to-1 MUX will be saturated at +7.
www.alljntuworld.in
JNTU World
Example 11
56
assign
assign
assign
assign
ld
c0;
c1;
s;
xi;
c1 = ~(x[4] ^ x[3]);
xi = ~(x[5]);
c0 = ~(x[5] ^ x[4]);
s = c0 & c1;
endmodule
mux24 U1
(
.a({x[5],xi,xi,xi}),
.b(x[3:0]),
.s(s),
.y(y)
);
or
wire
wire
wire
wire
JN
TU
A top-level design that can be used to test sat4bit is shown in Fig. 11.2. The
module x7segb11 is a modification of Listing 10.4 that will display only values between
-8 and +7 on the 7-segment display. Listing 11.2 shows the Verilog program for the
module x7segb11. The input to x7segb11 is the 4-bit output y[3:0] from sat4bit. Note
that only the two rightmost 7-segment display are enabled. The two leftmost displays are
always blank. The hex7seg always block in Listing 11.2 has been modified to display the
magnitude of the signed value of y[3:0] 0 to 8. The preceding 7-segment display will
either be blank or display a minus sign. The quad 4-to-1 MUX and the new 2-to-1 MUX
are used to display the minus sign when aen[1] is enabled if y[3] is 1; i.e., if y is negative.
www.alljntuworld.in
JNTU World
57
ld
assign
assign
assign
assign
assign
assign
or
reg msel;
reg [6:0] a_g0;
wire [6:0] a_g1;
reg [1:0] s;
reg [3:0] digit;
wire [3:0] aen;
a_g1 = 7'b1111110;
dp = 1;
aen[3] = 0;
//
aen[2] = 0;
//
aen[1] = y[3];
//
aen[0] = 1;
//
// minus sign
digit
digit
digit
digit
3
2
1
0
always off
always off
on if negative
always on
TU
JN
-8
-7
-6
-5
-4
-3
-2
-1
// 0
www.alljntuworld.in
58
JNTU World
Example 11
or
ld
// Digit select
always @(*)
begin
an = 4'b1111;
if(aen[s] == 1)
an[s] = 0;
end
endmodule
// 2-bit counter
always @(posedge cclk or posedge clr)
begin
if(clr == 1)
s <= 0;
else
s <= s + 1;
end
JN
TU
The Verilog program corresponding to the top-level design in Fig. 11.2 is given in
Listing 11.3. Download this top-level design to the FPGA board and observe the output
on the 7-segment display for different 6-bit switch inputs.
www.alljntuworld.in
JNTU World
sat4bit U1
(
.x(sw),
.y(y)
);
x7segb11 U2
(
.a_to_g(a_to_g),
.an(an),
.cclk(clk190),
.clr(btn[3]),
.dp(dp),
.y(y)
);
or
assign ld = sw;
59
ld
TU
clkdiv U3
(
.clk190(clk190),
.clr(btn[3]),
.mclk(mclk)
);
JN
endmodule
www.alljntuworld.in
60
JNTU World
Example 12
Example 12
Full Adder
ld
or
Prerequisite knowledge:
Basic Gates Appendix C
Karnaugh Maps Appendix D
7-Segment Displays Example 10
b
0
1
0
1
s
0
1
1
0
c
0
0
0
1
TU
a
0
0
1
1
The truth table for a half adder is shown in Fig. 12.1. In this table bit a is added
to bit b to produce the sum bit s and the carry bit c. Note that if you add 1 to 1 you get 2,
which in binary is 10 or 0 with a carry bit. The BDE logic diagram, halfadd.bde, for a
half adder is also shown in Fig. 12.1. Note that the sum s is just the exclusive-or of a and
b and the carry c is just a & b. The Verilog program corresponding to the circuit in Fig.
12.1 is shown in Listing 12.1. A simulation of halfadd.bde is shown in Fig. 12.2.
Figure 12.1 Truth table and logic diagram halfadd.bde for a half-adder
JN
www.alljntuworld.in
JNTU World
61
ld
Full Adder
or
TU
When adding binary numbers we need to consider the carry from one bit to the
next. Thus, at any bit position we will be adding three bits: ai, bi and the carry-in ci from
the addition of the two bits to the right of the current bit position. The sum of these three
bits will produce a sum bit, si, and a carry-out, ci+1, which will
be the carry-in to the next bit position to the left. This is called a c a b s c
i
i
i
i
i+1
full adder and its truth table is shown in Fig. 12.3. The results of
0 0
the first seven rows in this truth table can be inferred from the 0 0 0
0 0 1
1 0
truth table for the half adder given in Fig. 12.1. In all of these 0 1 0
1 0
rows only two 1's are ever added together. The last row in Fig. 0 1 1
0 1
12.3 adds three 1's. The result is 3, which in binary is 11, or 1 1 0 0
1 0
plus a carry.
1 0 1
0 1
1
1
0
0 1
From the truth table in Fig. 12.3 we can write a sum of
1
1
1
1 1
products expression for si as
JN
si =
~ci
| ~ci
| ci
| ci
(12.1)
Figure 12.3
Truth table for a full adder
We can use the distributive law to factor out ~ci from the first two product terms and ci
from the last two product terms in Eq. (12.1) to obtain
si =
|
(12.2)
(12.3)
www.alljntuworld.in
62
JNTU World
Example 12
si = ci ^ (ai ^ bi)
(12.4)
Fig. 12.4 shows the K-map for ci+1 from the truth table in Fig. 12.3. The map
shown in Fig. 12.4a leads to the reduced form for ci+1 given by
ci+1 =
(12.5)
ci+1 =
=
=
ld
While this is the reduced form, a more convenient form can be written from Fig. 12.4b as
follows:
ai & bi | ci & ~ai & bi | ci & ai & ~bi
ai & bi | ci & (~ai & bi | ai & ~bi)
ai & bi | ci & (ai ^ bi)
bi
bi
01
11
10
01
11
a ib i
00
ci
10
or
a ib i
00
ci
(12.6)
ci
ci
ai
ai
(a)
(b)
Figure 12.4 K-maps for ci+1 for full adder in Fig. 6.2
TU
From Eqs. (12.4) and (12.6) we can draw the logic diagram for a full adder as shown in
Fig. 12.5. Comparing this diagram to that for a half adder in Fig. 12.1 it is clear that a
full adder can be made from two half adders plus an OR gate as shown in Fig. 12.6.
ci
ai
si
JN
bi
c i+1
ci
ai
bi
half-adder
half-adder
si
c
c i+1
Figure 12.6 A full adder can be made from two half adders plus an OR gate
www.alljntuworld.in
JNTU World
Full Adder
63
or
ld
From Fig. 12.6 we can create a BDE design, fulladd.bde, as shown in Fig. 12.7.
The Verilog program resulting from compiling this design is equivalent to that shown in
Listing 12.2. A simulation of this full adder is shown in Fig. 12.8. Note that the outputs
agree with the truth table in Fig. 12.3.
TU
wire c1;
wire c2;
wire s1;
JN
halfadd U1
(
.a(a),
.b(b),
.c(c1),
.s(s1)
);
halfadd U2
(
.a(s1),
.b(cin),
.c(c2),
.s(s)
);
endmodule
www.alljntuworld.in
Example 12
or
ld
64
JNTU World
JN
TU
Figure 12.8 Simulation of the full adder in Fig. 12.7 and Listing 12.2
www.alljntuworld.in
JNTU World
4-Bit Adder
65
Example 13
4-Bit Adder
or
Prerequisite knowledge:
Basic Gates Appendix C
Karnaugh Maps Appendix D
Full Adder Example 12
ld
JN
TU
Four of the full adders in Fig. 12.7 can be combined to form a 4-bit adder as
shown in Fig. 13.1. Note that the full adder for the least significant bit will have a carryin of zero while the remaining bits get their carry-in from the carry-out of the previous
bit. The final carry-out, is the cout for the 4-bit addition. The Verilog program
corresponding to the 4-bit adder in Fig. 13.1 is given in Listing 13.1.
www.alljntuworld.in
66
JNTU World
Example 13
ld
fulladd U1
(
.a(a[2]),
.b(b[2]),
.cin(c2),
.cout(c3),
.s(s[2])
);
fulladd U2
(
.a(a[3]),
.b(b[3]),
.cin(c3),
.cout(cout),
.s(s[3])
);
or
wire c1;
wire c2;
wire c3;
TU
fulladd U3
(
.a(a[1]),
.b(b[1]),
.cin(c1),
.cout(c2),
.s(s[1])
);
JN
fulladd U4
(
.a(a[0]),
.b(b[0]),
.cin(cin),
.cout(c1),
.s(s[0])
);
endmodule
A simulation of the 4-bit adder in Fig. 13.1 and Listing 13.1 is shown in Fig. 13.2.
The value of a is incremented from 0 to F and is added to the hex value B. The sum s is
always equal to a + b. Note that the carry flag, cout, is equal to 1 when the correct
unsigned answer exceeds 15 (or F).
We can test the adder4 module from Fig. 13.1 and Listing 13.1 on the FPGA
board by combining it with the x7segb module from Listing 10.4 in Example 10 and the
clkdiv module from Listing 8.2 from Example 8 to produce the top-level design shown in
Listing 13.2. The 4-bit number sw[7:4] will be displayed on the first (left-most) 7-
www.alljntuworld.in
JNTU World
4-Bit Adder
67
or
ld
segment display. The 4-bit number sw[3:0] will be displayed on the second 7-segment
display. These two numbers will be added and the 4-bit sum will be displayed on the
fourth (right-most) 7-segment display and the carry bit will be displayed on the third 7segment display. Try it.
Figure 13.2 Simulation of the 4-bit adder in Fig. 13.1 and Listing 13.1
TU
JN
assign
assign
assign
assign
cin = 0;
x = {sw,3'b000,c4,sum};
clr = btn[3];
ld = sw;
adder4 U1 (.cin(cin),.a(sw[7:4]),.b(sw[3:0]),
.cout(c4),.s(sum));
clkdiv U2 (.mclk(mclk),.clr(clr),.clk190(clk190));
x7segb U3 (.x(x),.cclk(clk190),.clr(clr),
.a_to_g(a_to_g),.an(an),.dp(dp));
endmodule
www.alljntuworld.in
68
JNTU World
Example 14
Example 14
N-Bit Adder
Prerequisite knowledge:
4-Bit Adder Example 13
ld
or
It would be convenient to be able to make a 4-bit adder (or any size adder) by just
using a + sign in a Verilog statement. In fact, we can! When you write a + b in a Verilog
program the compiler will produce a full adder of the type we designed in Example 12.
The only question is how to create the output carry bit. The trick is to add a leading 0 to
a and b and then make a 5-bit temporary variable to hold the sum as shown in Listing
14.1. The most-significant bit of this 5-bit sum will be the carry flag.
A simulation of this program is shown in Fig. 14.1. Compare this with Fig. 13.2.
TU
JN
always @(*)
begin
temp = {1'b0,a} + {1'b0,b};
s = temp[3:0];
cf = temp[4];
end
endmodule
www.alljntuworld.in
JNTU World
N-Bit Adder
69
TU
always @(*)
begin
y = a + b;
end
endmodule
or
ld
Listing 14.2 shows an N-bit adder that uses a parameter statement. This is a
convenient adder to use when you dont need the carry flag. An example of using this as
an 8-bit adder is shown in the simulation in Fig. 14.2. Note that when the sum exceeds
FF it simply wraps around and the carry flag is lost.
JN
The top-level design shown in Fig. 14.3 can be used to test this N-bit adder on the
FPGA board. In this case we are adding two 4-bit switch settings and observing the sum
on the 7-segment display. To set the parameter N to 4 right-click on the adder symbol,
select Properties and click on the Parameter tab. Set the actual value of N to 4.
Figure 14.3 Top-level design for testing the N-bit adder on the FPGA board
www.alljntuworld.in
70
JNTU World
Example 15
Example 15
N-Bit Comparator
ld
or
The easiest way to implement a comparator in Verilog is to use the relational and
logical operators shown in Table 15.1. An example of using these to implement an N-bit
comparator is shown in Listing 15.1. A simulation of this program for the default value
of N = 8 is shown in Fig. 15.1.
Note in the always block in Listing 15.1 we set the values of gt, eq, and lt to zero
before the if statements. This is important to make sure that each output has a value
assigned to it. If you dont do this then Verilog will assume you dont want the value to
change and will include a latch in your system. Your circuit will then not be a
combinational circuit.
JN
TU
www.alljntuworld.in
JNTU World
N-Bit Comparator
71
endmodule
or
always @(*)
begin
gt = 0;
eq = 0;
lt = 0;
if(x > y)
gt = 1;
if(x == y)
eq = 1;
if(x < y)
lt = 1;
end
ld
JN
TU
You can test this comparator on the FPGA board by creating the BDE block
diagram comp4_top.bde shown in Fig. 15.2. To make this a 4-bit comparator right-click
on the comp symbol, select Properties, click on the Parameters tab, and set the actual
value of N to 4. You will be comparing the 4-bit number x[3:0] on the left four switches
with the 4-bit number y[3:0] on the right four switches. The three LEDs ld[4:2] will
detect the outputs gt, eq, and lt. We selected these three LEDs because on the BASYS
board they are three different colors. Compile the design comp4_top.bde, implement it,
and download the .bit file to the FPGA board. Test the comparator by changing the
switch settings.
www.alljntuworld.in
Example 15
JN
TU
or
ld
72
JNTU World
www.alljntuworld.in
JNTU World
109
Appendix A
TU
or
ld
JN
Browse to the directory where you want the project saved, type Example1 for the
workspace name and click OK.
www.alljntuworld.in
110
JNTU World
Appendix A
or
ld
Select Create an Empty Design with Design Flow and click Next.
TU
JN
Select Xilinx
ISE/WebPack 8.1 XST VHDL/Verilog
Press Select
www.alljntuworld.in
JNTU World
111
or
ld
Select Implementation
Choose Xilinx
ISE/WebPack 8.1
TU
Press Select
JN
Click Ok
www.alljntuworld.in
112
JNTU World
Appendix A
ld
JN
TU
or
Click Next
Click Finish.
www.alljntuworld.in
JNTU World
113
or
ld
Click on BDE.
TU
Click Next.
JN
Select Verilog
and Click Next
www.alljntuworld.in
114
JNTU World
Appendix A
Type sw
Set array
indexes to 7:0
TU
Click New.
or
ld
Type sw2led
and click Next.
Type ld
Set array
indexes to 7:0
JN
Click New.
Click out.
Click Finish.
www.alljntuworld.in
JNTU World
115
ld
This will generate a block diagram (schematic) template with the input and output ports
displayed.
or
You will need to select the output port by dragging the mouse with
the left mouse button down and move the output port to the left.
TU
Select the bus icon and connect the input sw[7:0] to the output ld[7:0] as shown.
JN
Click Save
www.alljntuworld.in
116
JNTU World
Appendix A
or
ld
Right-click on sw2led.bde
and select Compile
TU
JN
Click
synthesis options
www.alljntuworld.in
JNTU World
117
ld
Check Verilog
or
BASYS Board:
Select 3s100etq144 for Device from pull down list.
Nexys2 Board:
Select 3s500efg320 for Device from pull down list.
JN
TU
Click synthesis
Click Ok.
www.alljntuworld.in
118
JNTU World
Appendix A
JN
TU
or
ld
Click implementation
options
Select
Custom constraint file
www.alljntuworld.in
JNTU World
119
ld
JN
TU
or
www.alljntuworld.in
120
JNTU World
Appendix A
or
ld
Click
implementation
TU
To program the Spartan3E on the BASYS or Nexys-2 boards we will use the
ExPort tool that is part of the the Adept Suite available free from Digilent at
http://www.digilentinc.com/Software/Adept.cfm?Nav1=Software&Nav2=Adept
Double-click the ExPort icon on the desktop.
JN
www.alljntuworld.in
JNTU World
121
or
ld
JN
TU
Your program is now running on the board. Change the switches and watch the LEDs.
www.alljntuworld.in
122
JNTU World
Appendix A
JN
TU
Click Next.
or
ld
Click on BDE.
Select Verilog
and Click Next
www.alljntuworld.in
JNTU World
123
Type a.
TU
Click New.
or
ld
Type gates2
and click Next.
Click New.
JN
Type b.
www.alljntuworld.in
124
JNTU World
Appendix A
Click New.
Type and_.
or
ld
Click out.
JN
TU
Continue to click New and add the outputs nand_, or_, nor_, xor_, and xnor_.
Click Finish.
www.alljntuworld.in
JNTU World
125
or
ld
This will generate a block diagram (schematic) template with the input and output ports
displayed.
Select the output ports by dragging the mouse with the left
mouse button down and move the output ports to the left.
JN
TU
Click + on
Built-in symbols
www.alljntuworld.in
126
JNTU World
Appendix A
or
ld
Grab the and2 symbol with the mouse and drag it to the output port and_
JN
TU
Grab the symbols for nand2, or2, nor2, xor2, and xnor2 and drag them to the
appropriate output port, moving the output ports down as necessary.
www.alljntuworld.in
JNTU World
127
or
TU
Click Save
ld
Select the wire icon and connect the gate inputs to a and b as shown.
JN
Right-click on gates2.bde
and select Compile
www.alljntuworld.in
128
JNTU World
Appendix A
Part 6: Simulation
ld
or
Select gates2.bde
Click > and Click OK
TU
Click Choose, select gates2 as the top-level design, and click Add.
JN
Click OK
www.alljntuworld.in
JNTU World
129
Click OK
JN
TU
or
ld
www.alljntuworld.in
130
JNTU World
Appendix A
or
ld
The waveform window will automatically come up with the simulation already
initialized. Make sure the order is a, b, and_, nand_, or_, nor_, xor_, xnor (grab and
drag if necessary). Right-click on a and select Stimulators.
JN
TU
Click Apply
www.alljntuworld.in
JNTU World
131
Click Close
Click Apply
or
ld
JN
TU
www.alljntuworld.in
132
JNTU World
Appendix A
Click on HDE.
TU
Click Next.
or
ld
Select Verilog
and Click OK.
JN
Type gates2
and click Next.
www.alljntuworld.in
JNTU World
Type a.
Click New.
or
ld
Click New.
133
TU
Type b.
Click New.
JN
Type z.
Click Finish.
www.alljntuworld.in
134
JNTU World
Appendix A
This will generate a Verilog template with the input and output signals filled in. Delete
all the comments and replace them with the single comment
ld
or
Delete these
statements.
Delete these
comments.
JN
TU
Edit the module, input, output, and wire statements to conform to the 2001 Verilog
standard as shown (see Listing 2.1 in Example 1).
www.alljntuworld.in
JNTU World
135
2
Click Save
1
Type in these six
assign statements
(see Listing 2.1 of
Example 1)
ld
or
JN
TU
Select gates2.v,
click > to move
and then Click Ok
www.alljntuworld.in
136
JNTU World
Appendix A
ld
Click Choose, select gates2 as the top-level design, and click Add.
or
Click Ok
JN
TU
Click Ok
www.alljntuworld.in
JNTU World
137
ld
JN
TU
or
The waveform window will automatically come up with the simulation already
initialized. Make sure the order is a, b, z (grab and drag if necessary).
Right-click on a and select Stimulators.
www.alljntuworld.in
138
JNTU World
Appendix A
or
ld
Click Apply
JN
TU
Click Apply
Click Close
www.alljntuworld.in
JNTU World
139
or
ld
JN
TU
www.alljntuworld.in
JNTU World
Appendix E
Verilog Quick Reference Guide
Parameters
Local parameters
Nets and Variables
Types
TU
Module
endmodule
~ (NOT)
& (AND)
| (OR)
~(&) (NAND)
~(|) (NOR)
^ (XOR)
~^ (XNOR
& (AND)
| (OR)
~& (NAND)
~| (NOR)
^ (XOR)
~^ (XNOR
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (mod)
JN
Logic operators
Reduction operators
Arithmetic operators
ld
Numbers
Example
q0
Prime_number
lteflg
35 (default decimal)
4b1001
8a5 = 8b10100101
or
Signal Values
Definition
Can contain any letter, digit, underscore _, or $
Can not begin with a digit or be a keyword
Case sensitive
0 = logic value 0
1 = logic value 1
z or Z = high impedance
x or X = unknown value
d = decimal
b = binary
h = hexadecimal
o = octal
Associates an identifer name with a value that
can be overridden with the defparam statement
Associates an identifer name with a constant that
cannot be directly overridden
wire (used to connect one logic element to
another)
reg (variables assigned values in always block)
integer (useful for loop control variables)
module module_name
[#(parameter_port_list)]
(port_dir_type_name,{ port_dir_type_name }
);
[wire declarations]
[reg declarations]
[assign assignments]
[always blocks]
Category
Identifer Names
#(parameter N = 8)
module register
#(parameter N = 8)
(input wire load ,
input wire clk ,
input wire clr ,
input wire [N-1:0] d ,
output reg [N-1:0] q
);
always @(posedge clk or posedge clr)
if(clr == 1)
q <= 0;
else if(load)
q <= d;
endmodule
assign z
assign c
assign z
assign w
assign r
assign z
assign d
assign c
assign z
assign w
assign r
assign z
assign d
count <=
q <= q
= ~y;
= a & b;
= x | y;
= ~(u & v);
= ~(s | t);
= x ^ y;
= a ~^ b;
= &a;
= |y;
= ~&v;
= ~|t;
= ^y;
= ~^b;
count + 1;
1;
www.alljntuworld.in
JNTU World
Relational operators
Shift operators
ld
always block
or
if statement
case statement
TU
for loop
Assignment operator
Module instantiation
JN
Parameter override