My First Fpga
My First Fpga
My First Fpga
DE10-Nano i www.terasic.com
My First FPGA March 7, 2017
Chapter 1
Introduction
This tutorial provides comprehensive information that will help you understand how to create a
FPGA design and run it on your DE10-Nano development board. The following sections provide a
quick overview of the design flow, explain what you need to get started, and describe what you will
learn.
The standard FPGA design flow starts with design entry using schematics or a hardware description
language (HDL), such as Verilog HDL or VHDL. In this step, you can create a digital circuit that is
implemented inside the FPGA. The flow then proceeds through compilation, simulation,
programming, and verification in the FPGA hardware.
This tutorial guides you through all of the steps except for simulation. Although it is not covered in
this document, simulation is very important to learn, and there are entire applications devoted to
simulating hardware designs. There are two types of simulation, Functional and Timing Functional
simulation allows you to verify that your code is manipulating the inputs and outputs appropriately.
Timing (or post place-and-route) simulation verifies that the design meets timing and functions
appropriately in the device.
DE10-Nano 1 www.terasic.com
My First FPGA March 7, 2017
1.2 Before You Begin
■ You generally know what a FPGA is. This tutorial does not explain the basic concepts of
programmable logic.
■ You are somewhat familiar with digital circuit design and electronic design automation (EDA)
tools.
■ You have installed the Altera Quartus II 16.0 software on your computer. If you do not have
the Quartus II software, you can download it from the Altera web site at www.altera.com/download.
■ You have a DE10-Nano Development Board on which you will test your project. Using a
development board helps you to verify whether your design is really working.
■ You have gone through the quick start guide and/or the getting started user guide for your
development kit. These documents ensure that you have:
Next step you should installed the USB-BlasterII driver, Plug in the 5-volt adapter to provide power
to the board. Use the USB cable to connect the leftmost USB connector on the DE10-Nano board to
a USB port on a computer that runs the Quartus II software. Turn on the power switch on the
DE10-Nano board.
The computer will recognize the new hardware connected to its USB port and Power on the board
as shown in Figure 1-2, but it will be unable to proceed if it does not have the required driver
already installed. The DE10-Nano board is programmed by using Altera USB-Blaster II mechanism.
If the USB-BlasterII driver is not already installed, the Driver Software Installation in Figure 1-3
will appear.Click close.
DE10-Nano 2 www.terasic.com
My First FPGA March 7, 2017
Figure 1-2 Connection Setup
Since the desired driver is not available on the Windows Update Web site, open the Computer
Management and select the Device Manager. This leads to the window in Figure 1-4.
DE10-Nano 3 www.terasic.com
My First FPGA March 7, 2017
Figure 1-4 Device Manager
Right click Other devices>Unknown device and select Update Driver Software…This leads to the window in
Figure 1-5.
DE10-Nano 4 www.terasic.com
My First FPGA March 7, 2017
The driver is available within the Quartus II software. Hence, click Browse my computer for device software to
get to Figure 1-6.
.
Now, click Browse to get to the pop-up box in Figure 1-7 Find the desired driver, which is at
location C:\altera\16.0\quartus\drivers\usb-blaster-ii. Click OK and then upon returning to Figure
1-6.click Next.
DE10-Nano 5 www.terasic.com
My First FPGA March 7, 2017
Figure 1-7 Browse to find the location
The driver will now be installed as indicated in Figure 1-8 Click close and you can start using the
DE10-Nano board.
DE10-Nano 6 www.terasic.com
My First FPGA March 7, 2017
1.3 W hat You W ill Lear n
Create a design that causes LEDs on the development board to blink at a speed that is controlled by
an input key — This design is easy to create and gives you visual feedback that the design works.
Of course, you can use your board to run other designs as well. For the LED design, you will write
Verilog HDL code for a simple 32-bit counter, add a phase-locked loop (PLL) megafunction as the
clock source, and add a 2-input multiplexer megafunction. When the design is running on the board,
you can press an input switch to multiplex the counter bits that drive the output LEDs.
Becoming familiar with Quartus II design tools—This tutorial will not make you an expert, but at
the end, you will understand basic concepts about Quartus II projects, such as entering a design
using a schematic editor and HDL, compiling your design, and downloading it into the FPGA on
your DE10-Nano development board.
Develop a foundation to learn more about FPGAs — For example, you can create and download
digital signal processing (DSP) functions onto a single chip, or build a multi-processor system, or
create anything else you can imagine all on the same chip. You don’t have to scour data books to
find the perfect logic device or create your own ASIC. All you need is your computer, your
imagination, and an DE10-Nano FPGA development board.
DE10-Nano 7 www.terasic.com
My First FPGA March 7, 2017
Chapter 2
You begin this tutorial by creating a new Quartus II project. A project is a set of files that maintain
information about your FPGA design. The Quartus II Settings File (.qsf) and Quartus II Project File
(.qpf) files are the primary files in a Quartus II project. To compile a design or make pin
assignments, you must first create a project.
1. In the Quartus II software, select File > New Project Wizard. The Introduction page opens. See
Figure 2-1.
DE10-Nano 8 www.terasic.com
My First FPGA March 7, 2017
2. Click Next.
a. What is the working directory for this project? Enter a directory in which you will store your
Quartus II project files for this design.
c. File names, project names, and directories in the Quartus II software cannot contain spaces.
e. What is the name of the top-level design entity for this project? Type my_first_fpga. See
Figure 2-2.
f. Click Next.
DE10-Nano 9 www.terasic.com
My First FPGA March 7, 2017
g. You will assign a specific FPGA device to the design and make pin assignments. See Figure
2-3.
h. Click Finish.
4. When prompted, choose Yes to create the my_first_fpga project directory. You just created
your first Quartus II FPGA project. See Figure 2-4.
DE10-Nano 10 www.terasic.com
My First FPGA March 7, 2017
Figure 2-4 my_first_fpga project
DE10-Nano 11 www.terasic.com
My First FPGA March 7, 2017
Chapter 3
Design Entry
In the design entry step you create a schematic or Block Design File (.bdf) that is the top-level
design. You will add library of parameterized modules (LPM) functions and use Verilog HDL code
to add a logic block. When creating your own designs, you can choose any of these methods or a
combination of them.
1. Choose File > New > Block Diagram/Schematic File (see Figure 3-1 to create a new file,
Block1.bdf, which you will save as the top-level design.
DE10-Nano 12 www.terasic.com
My First FPGA March 7, 2017
2. Click OK.
4. Click Save. The new design file appears in the Block Editor (see Figure 3-2).
5. Add HDL code to the blank block diagram by choosing File > New > Verilog HDL File.
6. Click OK to create a new file Verilog1.v, which you will save as simple_counter.v.
7. Select File > Save As and enter the following information (see Figure 3-3).
DE10-Nano 13 www.terasic.com
My First FPGA March 7, 2017
Figure 3-3 Saving the Verilog HDL file
The resulting empty file is ready for you to enter the Verilog HDL code.
8. Type the following Verilog HDL code into the blank simple_counter.v file (see Figure 3-4 The
Verilog File of simple_counter.v).
module simple_counter (
CLOCK_50,
counter_out
);
input CLOCK_50 ;
DE10-Nano 14 www.terasic.com
My First FPGA March 7, 2017
begin
end
9. Save the file by choosing File > Save, pressing Ctrl + s, or by clicking the floppy disk icon.
10. Choose File > Create/Update > Create Symbol Files for Current File to convert the
simple_counter.v file to a Symbol File (.sym).You use this Symbol File to add the HDL code to
your BDF schematic.
The Quartus II software creates a Symbol File and displays a message (see Figure 3-5).
DE10-Nano 15 www.terasic.com
My First FPGA March 7, 2017
Figure 3-5 Create Symbol File was Successful
11. To add the simple_counter.v symbol to the top-level design, click the my_first_fpga.bdf tab.
14. Select the newly created simple_counter symbol by clicking it’s icon.
You can also double-click in a blank area of the BDF to open the Symbol dialog box (see Figure
3-6).
17. Move the cursor to the BDF grid; the symbol image moves with the cursor. Click to place the
simple_counter symbol onto the BDF. You can move the block after placing it by simply clicking
and dragging it to where you want it and releasing the mouse button to place it. See Figure 3-7.
DE10-Nano 16 www.terasic.com
My First FPGA March 7, 2017
Figure 3-7 Placing the simple_counter symbol
18. Press the Esc key or click an empty place on the schematic grid to cancel placing further
instances of this symbol.
Megafunctions, such as the ones available in the LPM, are pre-designed modules that you can use in
FPGA designs. These Altera-provided megafunctions are optimized for speed, area, and device
family. You can increase efficiency by using a megafunction instead of writing the function yourself.
Altera also provides more complex functions, called MegaCore functions, which you can evaluate
for free but require a license file for use in production designs. This tutorial design uses a PLL clock
source to drive a simple counter. A PLL uses the on-board oscillator (DE10-Nano Board is 50 MHz)
to create a constant clock frequency as the input to the counter. To create the clock source, you will
add a pre-built LPM megafunction named Altera PLL.
1. Choose Edit > Insert Symbol or click Add Symbol on the toolbar---
DE10-Nano 17 www.terasic.com
My First FPGA March 7, 2017
Figure 3-8 Tools Icon
3. Selecting IP Catalog.
4. Selection Altera PLL in the Library/Clocks/PLL , click "+Add" bar. See Figure 3-9
b. Under IP variation file, Type pll.v at the end of the already create directory name.
c. Click OK.
DE10-Nano 18 www.terasic.com
My First FPGA March 7, 2017
Figure 3-9 IP Catalog Selections
5. In the Altera PLL windows, make the following selections (see Figure 3-10).
DE10-Nano 19 www.terasic.com
My First FPGA March 7, 2017
Figure 3-10 Selections of Altera PLL windows
6. Turn off the Enable locked output port option on IP windows. As you turn it off, pin disappear
from the PLL block’s graphical preview. See Figure 3-11 for an example.
DE10-Nano 20 www.terasic.com
My First FPGA March 7, 2017
Figure 3-11 Selections of Altera PLL windows
DE10-Nano 21 www.terasic.com
My First FPGA March 7, 2017
Figure 3-12 Selections of Altera PLL windows
DE10-Nano 22 www.terasic.com
My First FPGA March 7, 2017
9. The Symbol window opens, showing the newly created PLL megafunction. See Figure 3-14.
10. Click OK and place the pll symbol onto the BDF to the left of the simple_counter symbol. You
can move the symbols around by holding down the left mouse button, helping you ensure that they
line up properly. See Figure 3-15.
DE10-Nano 23 www.terasic.com
My First FPGA March 7, 2017
11. Move the mouse so that the cursor (also called the selection tool) is over the pll symbol’s
outclk_0 output pin. The orthogonal node tool (cross-hair) icon appears.
12. Click and drag a bus line from the outclk_0 output to the simple_counter clock input. This
action ties the pll output to the simple_counter input (see Figure 3-16).
Figure 3-16 Draw a Bus Line connect pll outclk_0 port to simple_counter CLOCK_50 port
13. Add an input pin and an output bus with the following steps:
b. Under Libraries, select quartus/libraries > primitives > pin >input. See Figure 3-17
c. Click OK
If you need more room to place symbols, you can use the vertical and horizontal scroll bars at the
edges of the BDF window to view more drawing space.
DE10-Nano 24 www.terasic.com
My First FPGA March 7, 2017
Figure 3-17 Input pin symbol
d. Place the new pin onto the BDF so that it is touching the input to the pll symbol.
e. Use the mouse to click and drag the new input pin to the left; notice that the ports remain
connected as shown in Figure 3-18.
f. Change the pin name by double-clicking pin_name and typing CLOCK_50 (see Figure 3-19).
This name correlates to the oscillator clock that is connected to the FPGA.
g. Using the Orthogonal Bus tool, draw a bus line connected on one side to the simple_counter
output port, and leave the other end unconnected at about 4 to 8 grid spaces to the right of the
simple_counter.
DE10-Nano 25 www.terasic.com
My First FPGA March 7, 2017
Figure 3-19 Change the input port name
i. Using the Orthogonal Bus tool , draw a bus line connected on one side to the
simple_counter output port, and leave the other end unconnected at about 6 to 8 grid spaces to the
right of the simple_counter.
j. Type counter [31..0] as the bus name (see Figure 3-20). The notation [X..Y] is the Quartus II
method for specifying the bus width in BDF schematics, where X is the most significant bit (MSB)
and Y is the least significant bit (LSB).
DE10-Nano 26 www.terasic.com
My First FPGA March 7, 2017
Figure 3-20 Change the output BUS name
DE10-Nano 27 www.terasic.com
My First FPGA March 7, 2017
3.1 Add a Multiplexer
This design uses a multiplexer to route the simple_counter output to the LED pins on the
DE10-Nano development board. You will use the MegaWizard Plug-In Manager to add the
multiplexer, lpm_mux. The design multiplexes two variations of the counter bus to four LEDs on
the DE10-Nano development board.
2. Click Library.
3. Click Miscellaneous.
4. Choose LPM_MUX.
5. Choose the device family that corresponds to the device on the development board you are
using, choose Verilog HDL as the output file type, and name the output file counter_bus_mux.v (see
Figure 3-22).
6. Click OK.
DE10-Nano 28 www.terasic.com
My First FPGA March 7, 2017
7. Under How many ‘data’ inputs do you want? Select 2 inputs (default).
8. Under how wide should the 'data' input and 'result' output be? Select 4 (see Figure 3-23).
9. Click Next.
DE10-Nano 29 www.terasic.com
My First FPGA March 7, 2017
10. Click Finish twice. The Symbol window appears (see Figure 3-24 for an example).
11. Click OK
DE10-Nano 30 www.terasic.com
My First FPGA March 7, 2017
12. Place the counter_bus_mux symbol below the existing symbols on the BDF. See Figure 3-25
13. Add input buses and output pins to the counter_bus_mux symbol as follows:
a. Using the Orthogonal Bus tool, draw bus lines from the data1x[3..0] and data0x[3..0].
b. Draw a bus line from the result [3..0] output port to about 4 to 8 grid spaces to the right of
counter_bus_mux.
d. Name the bus counter[26..23], which selects only those counter output bits to connect to
Because the input busses to counter_bus_mux have the same names as the output bus from
simple_counter, (counter[x .. y]) the Quartus II software knows to connect these busses.
e. Click OK.
g. Name the bus counter [24..21], which selects only those counter output bits to connect to the
four bits of the data1x input.
DE10-Nano 31 www.terasic.com
My First FPGA March 7, 2017
Figure 3-26 Renamed counter_bus_mux Bus Lines
If you have not done so yet, have your project file before continuing.
15. Under Libraries, double-click quartus/libraries/ > primitives > pin > output (see Figure 3-27).
17. Place this output pin so that it connects to the counter_bus_mux result [3..0] bus output line.
18. Rename the output pin as LED [3..0] as described in steps 13 c and d. (see Figure 3-28).
DE10-Nano 32 www.terasic.com
My First FPGA March 7, 2017
Figure 3-28 Rename the output pin
19. Attach an input pin to the multiplexer select line using an input pin:
b. Under Libraries, double-click quartus/libraries/ > primitives > pin > input.
c. Click OK.
23. Place another input pin at the left of the pll and rename it as KEY[1] (see Figure 3-29) .
25. Under Libraries, double-click quartus/libraries/ > primitives >logic > not (see Figure 3-30).
27. Place this not pin so that it connects to KEY[1] and the pll rst (see Figure 3-31).
DE10-Nano 33 www.terasic.com
My First FPGA March 7, 2017
Figure 3-29 Adding the KEY [1:0] Input Pin
DE10-Nano 34 www.terasic.com
My First FPGA March 7, 2017
Figure 3-31 Place the not pin
You have finished adding symbols to your design. You can add notes or information to the project
as text using the Text tool on the toolbar (indicated with the A symbol). For example, you can add
the label “OFF = SLOW, ON = FAST” to the KEY [0] input pin and add a project description, such
as “My First FPGA Project.”
In this section, you will make pin assignments. Before making pin assignments, perform the
following steps:
1. Choose Processing > Start > Start Analysis & Elaboration in preparation for assigning pin
locations.
2. Click OK in the message window that appears after analysis and elaboration completes.
To make pin assignments that correlate to the KEY[1:0] and CLOCK_50 input pins and LED[3..0]
output pin, perform the following steps:
1. Choose Assignments > Pin Planner,which opens the Pin Planner, a spreadsheet-like table of
specific pin assignments. The Pin Planner shows the design’s six pins. See Figure 3-32.
DE10-Nano 35 www.terasic.com
My First FPGA March 7, 2017
Figure 3-32 Pin Planner Example
2. In the Location column next to each of the seven node names, add the coordinates (pin
numbers) as shown in Table 3-1 for the actual values to use with your DE10-Nano board.
Double-click in the Location column for any of the seven pins to open a drop-down list and type the
location shown in the table alternatively, you can select the pin from a drop-down list. For example,
if you type F1 and press the Enter key, the Quartus II software fills in the full PIN_F1 location name
for you. The software also keeps track of corresponding FPGA data such as the I/O bank and VREF
Group. Each bank has a distinct color, which corresponds to the top-view wire bond drawing in the
upper right window. See Figure 3-33.
DE10-Nano 36 www.terasic.com
My First FPGA March 7, 2017
Figure 3-33 Completed Pin Planning Example
Timing settings are critically important for a successful design. For this tutorial you will create a
basic Synopsys Design Constraints File (.sdc) that the Quartus II TimeQuest Timing Analyzer uses
during design compilation. For more complex designs, you will need to consider the timing
requirements more carefully.
DE10-Nano 37 www.terasic.com
My First FPGA March 7, 2017
To create an SDC, perform the following steps:
1. Open the TimeQuest Timing Analyzer by choosing Tools > TimeQuest Timing Analyzer.
2. Choose File > New SDC file. The SDC editor opens.
derive_pll_clocks
derive_clock_uncertainty
Naming the SDC with the same name as the top-level file except for the .sdc extension causes the
Quartus II software to using this timing analysis file automatically by default. If you used another
name, you would need to add the SDC to the assignments file list.
DE10-Nano 38 www.terasic.com
My First FPGA March 7, 2017
Chapter 4
After creating your design you must compile it. Compilation converts the design into a bitstream
that can be downloaded into the FPGA. The most important output of compilation is an SRAM
Object File (.sof), which you use to program the device. The software also generates other report
files that provide information about your code as it compiles.
If you want to store .SOF in memory device (such as flash or EEPROMs), you must first convert
the SOF to a file type specifically for the targeted memory device.
Now that you have created a complete Quartus II project and entered all assignments, you can
compile the design.
In the Processing menu, choose Start Compilation or click the Play button on the toolbar.
While compiling your design, the Quartus II software provides useful information about the
compilation (see Figure 4-1).
DE10-Nano 39 www.terasic.com
My First FPGA March 7, 2017
Figure 4-1 Compilation Message for project
When compilation is complete, the Quartus II software displays a message. Click OK to close the
message box.
The Quartus II Messages window displays many messages during compilation. It should not display
any critical warnings; it may display a few warnings that indicate that the device timing information
is preliminary or that some parameters on the I/O pins used for the LEDs were not set. The software
provides the compilation results in the Compilation Report tab as shown in Figure 4-2.
DE10-Nano 40 www.terasic.com
My First FPGA March 7, 2017
Figure 4-2 Compilation Report Example
After compiling and verifying your design you are ready to program the FPGA on the development
board. You download the SOF you just created into the FPGA using the USB-BlasterII circuitry on
the board. Set up your hardware for programming using the following steps:
a) Connect the power supply cable to your board and to a power outlet.
b) For the DE10-Nano board, connect the USB-BlasterII (included in your development kit) to
J13 and the USB cable to the USB-BlasterII. Connect the other end of the USB cable to the host
computer.
Refer to the getting started user guide for detailed instructions on how to connect the cables.
DE10-Nano 41 www.terasic.com
My First FPGA March 7, 2017
Program the FPGA using the following steps.
1. Choose Tools > Programmer. The Programmer window opens. See Figure 4-3.
3. If it is not already turned on, turn on the DE-SoC [USB-1] option under currently selected
hardware. See Figure 4-4.
DE10-Nano 42 www.terasic.com
My First FPGA March 7, 2017
Figure 4-4 Hardware Setting
4. Click Close.
5. Click Auto Detect to detect all the devices on the JTAG chain.
DE10-Nano 43 www.terasic.com
My First FPGA March 7, 2017
7. Click Yes to match device list.
8. Both HPS and FPGA will be list on the programmer. Select FPGA device and click Change
File a .sof file.
DE10-Nano 44 www.terasic.com
My First FPGA March 7, 2017
9. Selecting the my_first_fpga.sof file from the project directory (see Figure 4-8), select to
Program/Configure and click Start to program .sof file into FPGA. Please see Figure 4-9.
DE10-Nano 45 www.terasic.com
My First FPGA March 7, 2017
Figure 4-9 Downloading Complete
Congratulations, you have created, compiled, and programmed your first FPGA design! The
compiled SRAM Object File (.sof) is loaded onto the FPGA on the development board and the
design should be running.
When you verify the design in hardware, you observe the runtime behavior of the FPGA hardware
design and ensure that it is functioning appropriately.
1. Observe that the four development board LEDs appear to be advancing slowly in a binary
count pattern, which is driven by the simple_counter bits [26..23].
The LEDs are active high, therefore, when counting begins all LEDs are turned off (the 0000 state).
DE10-Nano 46 www.terasic.com
My First FPGA March 7, 2017
2. Press and hold KEY [0] on the development board and observe that the LEDs advance more
quickly. Pressing this KEY causes the design to multiplex using the faster advancing part of the
counter (bits [24..21]).
3. If other LEDs emit faintness light, Choose Assignments > Device. Click Device and Pin
Options. See Figure 4-10.
Choose Unused Pins. Reserve all unused pins: Choose the As input tri-stated option. See Figure
4-11.
DE10-Nano 47 www.terasic.com
My First FPGA March 7, 2017
Figure 4-11 Setting unused pins
4. In the Processing menu, choose Start Compilation. After the compile, Choose Tools >
Programmer. Select the my_first_fpga.sof file from the project directory. Click Start. At this time
you could find the other LEDs are unlighted.
DE10-Nano 48 www.terasic.com
My First FPGA March 7, 2017
Chapter 5
Appendix
Tel :+886-3-575-0880
Fax :+886-3-572-6690
Add:9F, No.176, Sec.2, Gongdao 5th Rd, East Dist, Hsinchu City, Taiwan 300-70
Email:sales@terasic.com / support@terasic.com
DE10-Nano 49 www.terasic.com
My First FPGA March 7, 2017