Using_Library_Modules_Verilog
Using_Library_Modules_Verilog
in Verilog Designs
1 Introduction
This tutorial explains how Intel’s library modules can be included in Verilog-based designs, which are implemented
by using the Quartus® Prime software.
Contents:
• Example Circuit
FPGAcademy.org 1
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
2 Background
Practical designs often include commonly used circuit blocks such as adders, subtractors, multipliers, decoders,
counters, and shifters. Intel provides efficient implementations of such blocks in the form of library modules that
can be instantiated in Verilog designs. The compiler may recognize that a standard function specified in Verilog
code can be realized using a library module, in which case it may automatically infer this module. However, many
library modules provide functionality that is too complex to be recognized automatically by the compiler. These
modules have to be instantiated in the design explicitly by the user. Quartus® Prime software includes a library of
parameterized modules (LPM). The modules are general in structure and they are tailored to a specific application
by specifying the values of general parameters.
The detailed examples in the tutorial were obtained using the Quartus Prime version 21.1, but other versions of the
software can also be used. When selecting a device within Quartus Prime, use the device names associated with
FPGA chip on the DE-series board by referring to Table 1.
3 Example Circuit
As an example, we will use the adder/subtractor circuit shown in Figure 1. It can add, subtract, and accumulate n -bit
numbers using the 2’s complement number representation. The two primary inputs are numbers A = an−1 an−2 · · · a0
and B = bn−1 bn−2 · · · b0 , and the primary output is Z = z n−1 z n−2 · · · z 0 . Another input is the AddSub control signal
which causes Z = A +B to be performed when AddSub = 0 and Z = A −B when AddSub = 1. A second control input,
Sel, is used to select the accumulator mode of operation. If Sel = 0, the operation Z = A ± B is performed, but if Sel
= 1, then B is added to or subtracted from the current value of Z . If the addition or subtraction operations result in
arithmetic overflow, an output signal, Overflow, is asserted.
2 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
To make it easier to deal with asynchronous input signals, they are loaded into flip-flops on a positive edge of the
clock. Thus, inputs A and B will be loaded into registers Areg and Breg, while Sel and AddSub will be loaded into
flip-flops SelR and AddSubR, respectively. The adder/subtractor circuit places the result into register Zreg.
The required circuit is described by the Verilog code in Figure 2. For our example, we use a 16-bit circuit as specified
by n = 16. Implement this circuit as follows:
• Select the FPGA chip that is on the DE-series board. A list of device names on DE-series boards can be found
in Table 1.
FPGAcademy.org 3
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
// Top-level module
module addersubtractor (A, B, Clock, Reset, Sel, AddSub, Z, Overflow);
parameter n = 16;
input [n-1:0] A, B;
input Clock, Reset, Sel, AddSub;
output [n-1:0] Z;
output Overflow;
reg SelR, AddSubR, Overflow;
reg [n-1:0] Areg, Breg, Zreg;
wire [n-1:0] G, H, M, Z;
wire carry_out, over_flow;
4 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
// k-bit adder
module adderk (carryin, X, Y, S, carryout);
parameter k = 8;
input [k-1:0] X, Y;
input carryin;
output [k-1:0] S;
output carryout;
reg [k-1:0] S;
reg carryout;
To implement this adder/subtractor circuit, create a new directory named tutorial_lpm, and then create a project
addersubtractor2. Choose the same device as we previously selected (Refer to Table 1) to allow a direct comparison
of implemented designs.
FPGAcademy.org 5
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
The new design will include the desired LPM subcircuit specified as a Verilog module that will be instantiated in
the top-level Verilog design module. The Verilog module for the LPM subcircuit is generated by using a wizard as
follows:
1. Select Tools > IP Catalog, which opens the IP Catalog window in Figure 4.
2. In the IP Catalog panel, expand Library > Basic Functions > Arithmetic and double-click on LPM_ADD_SUB
6 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
3. In the pop-up box shown in Figure 5, choose Verilog as the type of output file that should be created. The
output file must be given a name; choose the name megaddsub.v and indicate that the file should be placed in
the directory tutorial_lpm as shown in the figure. Press OK.
FPGAcademy.org 7
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
4. In the box in Figure 6 specify that the width of the data inputs is 16 bits. Also, specify the operating mode
in which one of the ports allows performing both addition and subtraction of the input operand, under the
control of the add_sub input. A symbol for the resulting LPM is shown in the top left corner. Note that if
add_sub = 1 then result = A + B ; otherwise, result = A − B . This interpretation of the control input and the
operation performed is different from our original design in Figures 1 and 2, which we have to account for in
the modified design. Observe that we have included this change in the circuit in Figure 3. Click Next.
8 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
5. In the box in Figure 7, specify that the values of both inputs may vary and select Signed for the type of
addition/subtraction. Click Next.
FPGAcademy.org 9
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
6. The box in Figure 8 allows the designer to indicate optional inputs and outputs that may be specified. Since
we need the overflow signal, make the Create an overflow output choice and press Next.
10 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
7. In the box in Figure 9 say No to the pipelining option and click Next.
8. Figure 10 shows the simulation model files needed to simulate the generated design. Press Next to proceed to
the final page.
FPGAcademy.org 11
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
9. Figure 11 gives a summary which shows the files that the wizard will create. Press Finish to complete the
process.
12 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
10. The box in Figure 12 may pop up. If it does, press Yes to add the newly generated files to the project.
FPGAcademy.org 13
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
input add_sub;
input [15:0] dataa;
input [15:0] datab;
output overflow;
output [15:0] result;
wire sub_wire0;
wire [15:0] sub_wire1;
wire overflow = sub_wire0;
wire [15:0] result = sub_wire1[15:0];
lpm_add_sub lpm_add_sub_component (
.dataa (dataa),
.add_sub (add_sub),
.datab (datab),
.overflow (sub_wire0),
.result (sub_wire1));
defparam
lpm_add_sub_component.lpm_direction = "UNUSED",
lpm_add_sub_component.lpm_hint = "ONE_INPUT_IS_CONSTANT=NO,CIN_USED=NO",
lpm_add_sub_component.lpm_representation = "SIGNED",
lpm_add_sub_component.lpm_type = "LPM_ADD_SUB",
lpm_add_sub_component.lpm_width = 16;
endmodule
14 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
The modified Verilog code for the adder/subtractor design is given in Figure 14. Put this code into a file addersub-
tractor2.v under the directory tutorial_lpm. Also make sure to change the top level entity by selecting Project -> set
as top level entity. The differences between this code and Figure 2 are:
• The assign statements that define the over_flow signal and the XOR gates (along with the signal defined as
wire H) are no longer needed.
• The adderk instance of the adder circuit is replaced by megaddsub. Note that the dataa and datab inputs shown
in Figure 6 are driven by the G and Breg vectors, respectively. Also, the inverted version of the AddSubR signal
is specified to conform with the usage of this control signal in the LPM.
• The adderk module is deleted from the code.
// Top-level module
module addersubtractor2 (A, B, Clock, Reset, Sel, AddSub, Z, Overflow);
parameter n = 16;
input [n-1:0] A, B;
input Clock, Reset, Sel, AddSub;
output [n-1:0] Z;
output Overflow;
reg SelR, AddSubR, Overflow;
reg [n-1:0] Areg, Breg, Zreg;
wire [n-1:0] G, M, Z;
wire over_flow;
FPGAcademy.org 15
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
Figure 14. Verilog code for the circuit in Figure 3 (Part b).
If the megaddsub.qip file has not been included in the project (e.g. if you answered No in the box in Figure 12, or
possibly if the box did not show up at all), you need to include it manually. To include the megaddsub.v file in the
project, select Project > Add/Remove Files in Project to reach the window in Figure 15. The file addersubtrac-
tor2.v should already be listed as being included in the project. Browse for the other files by clicking the button ...
to reach the window in Figure 16. Select the file megaddsub.qip and click Open, which returns to the window in
Figure 15. Click Add to include the file and then click OK. Now, the modified design can be compiled and simulated
in the usual way.
16 FPGAcademy.org
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
FPGAcademy.org 17
Mar 2022
U SING L IBRARY M ODULES IN V ERILOG D ESIGNS For Quartus® Prime 21.1
Copyright © FPGAcademy.org. All rights reserved. FPGAcademy and the FPGAcademy logo are trademarks of
FPGAcademy.org. This document is being provided on an “as-is” basis and as an accommodation and therefore
all warranties, representations or guarantees of any kind (whether express, implied or statutory) including, with-
out limitation, warranties of merchantability, non-infringement, or fitness for a particular purpose, are specifically
disclaimed.
18 FPGAcademy.org
Mar 2022