This lab will introduce you to the hardware prototyping board (DE2) and computer-aided design software (Quartus) you will use this semester. The lab will walk you through the steps needed to implement, download, and test a simple digital circuit.
You will build several digital circuits this semester, ranging in complexity from a simple function of inputs to a general-purpose computer. Altera's DE2 board is the vehicle you will use to implement these circuits.
In this first lab, we will use the Cyclone II FPGA, LEDs and switches.
This lab will walk you through each step of the design process. Altera provides a similar tutorial, but you don't need to read this unless you're curious.
Quartus calls a digital circuit a project. All files for a Quartus project will be stored in one directory. To create a new digital circuit, first create a new directory to hold all information for the digital circuit. The directory may be named arbitrarily; these instructions assume your directory is named lab1. Create this directory somewhere under your ITCS AFS home directory.
Next, start Quartus by opening a terminal window and typing quartus (see Using computers in ENGR 100 for how to open a terminal window). When you run Quartus for the first time, you will get a dialogue box asking you to specify the look and feel of the Quartus interface (screenshot). Choose Quartus II.
When Quartus starts, it will offer you the choice to create a new project or open an existing project (screenshot). Click Create a New Project (New Project Wizard). If Quartus does not ask you this automatically, you can invoke the New Project Wizard through the File menu (File -> New Project Wizard) (screenshot). (We use the convention Menu1 -> Menu2 -> Item to describe how to click through a series of menus. All mouse clicks are assumed to be with the left mouse button, unless otherwise specified.) Here is a screenshot of the first window displayed by the New Project Wizard. Click Next to skip this introduction.
Fill in the dialogue boxes on the next window of the New Project Wizard (screenshot). The working directory for this project is the directory you created, e.g., lab1. You can specify the directory by clicking the ... icon, navigating to the directory, and clicking Open. We suggest you name your project top. The top-level design entity for all projects in this class must be top. Click Finish to end the New Project Wizard (there are subsequent dialogue boxes, but we won't need them for this lab).
After the New Project Wizard completes, copy the file top.qsf into your project directory (your copy should also be named top.qsf). This file contains configuration data for the hardware platform (i.e. the DE2) that will implement your circuit. You'll have to overwrite the initial top.qsf file that Quartus' New Project Wizard created. Here is a screenshot of the main Quartus display after you finish the New Project Wizard. The name of the FPGA at the top left part of the screen should be Cyclone II: EP2C35F672C6. If you see Stratix: AUTO instead, it means you downloaded top.qsf before running the New Project Wizard.
At this point, you've created a Quartus project file top.qpf in the lab1 directory. You can open this project later within Quartus via File -> Open Project, navigating to the project directory, and selecting top.qpf (screenshot 1) (screenshot 2).
Warning: Quartus hangs for several minutes if you navigate to a directory that contains a PDF file.
Now that you've created a Quartus project, it's time to enter your first digital circuit. All circuits in the class will be expressed in Verilog code. You can use Quartus's built-in text editor (or any other text editor) to edit Verilog code. Do not use a word processor such as Microsoft Word or OpenOffice, since word processors include extraneous formatting information in their files.
Quartus' text editor can be invoked through Quartus' File menu (File -> New -> Design Files -> Verilog HDL File) (screenshot 1) (screenshot 2).
Use the text editor to enter the following Verilog code (screenshot).
module top(
input wire [17:0] DPDT_SW,
output reg [17:0] LED_RED);
always @* begin
if (DPDT_SW[0] == 1'b1 && DPDT_SW[1] == 1'b1) begin
LED_RED[0] = 1'b1;
end else if (DPDT_SW[0] == 1'b1 && DPDT_SW[2] == 1'b1) begin
LED_RED[0] = 1'b1;
end else if (DPDT_SW[1] == 1'b1 && DPDT_SW[2] == 1'b1) begin
LED_RED[0] = 1'b1;
end else begin
LED_RED[0] = 1'b0; // default value is 0
end
end
endmodule
Save the code as top.v (File -> Save As) (screenshot 1) (screenshot 2). Quartus adds the ".v" extension automatically, so you need only type top as the file name. Make sure the box is checked to add this file automatically to the current project.
A few points about the Verilog code above:
We'll learn more about Verilog as the semester progresses. Surprisingly, there are only a couple more Verilog constructs you'll need to describe a complete CPU. The labs will teach all the Verilog you'll need, but if you want more comprehensive information on Verilog, you can check out Deepak Kumar Tala's tutorial or Stuart Sutherland's reference manual.
The next step is to translate the high-level hardware description written in Verilog to a low-level circuit specification for the FPGA. This step is called synthesis (Quartus calls it compilation). You can invoke Quartus' compiler through the Processing menu (Processing -> Start Compilation) (screenshot) or a keyboard shortcut (Ctrl-L). Later in the semester, we will describe how Verilog constructs are synthesized to digital logic.
Quartus displays the status of the compilation as it runs (screenshot). The small Status window on the left shows the progress of each phase of the compilation. The wide window at the bottom displays compilation messages, including warnings and errors. Quartus will notify you with a confirmation box after it finishes compiling your circuit (it will issue numerous warnings about unused signals and I/O assignments; don't worry about these). After you click OK, you'll get the following screenshot.
If the compiler encounters an error (e.g., you had a typo), the bottom window will show an error message that is usually helpful for diagnosing the cause of the error (screenshot). Double-clicking on the error message will usually open the file with the offending code (screenshot). You can then fix the error and re-compile.
After you've compiled the Verilog code, you're ready to download it onto the Cyclone II FPGA. Quartus downloads the synthesized circuit to the FPGA via the USB cable that connects the DE2 to the computer that is running Quartus.
First, turn on the DE2 (if it's not already on) by pressing the power button (big red button on the upper left). This should light up the blue power LED (upper left).
You will download the synthesized circuit via Quartus' In-System Memory Content Editor (which we call the "memory editor" for short). Start the memory editor from Quartus' Tools menu (Tools -> In-System Memory Content Editor) (screenshot). You will see the following screenshot.
The only part of the memory editor we will use for this lab is the top, right-hand corner of the memory editor (JTAG Chain Configuration). Drag the vertical divider (just to the left of "JTAG Chain Configuration") to the left so you can see more of that part of the memory editor. Click the Hardware pulldown menu and select USB-Blaster (screenshot).
Now click the ... button, which will bring up a window to select the programming file (screenshot). Navigate to the project directory and select top.sof; this file contains the low-level circuit specification you created when you compiled the Verilog code.
Finally, click the blue arrow icon. This downloads the circuit onto the FPGA. Quartus may pause for about 30 seconds the first time you download a circuit; subsequent times will be much faster.
Now that you've entered, compiled and downloaded your circuit, it's time to test your circuit's behavior. The switches used in this circuit are DPDT_SW[1] and DPDT_SW[0] (labeled SW1 and SW0 on the board). Move the levers of these switches between 0 (closer to the front edge of the DE2) and 1 (away from the front edge of the DE2) and observe the output of LED_RED[0] (the LED directly above DPDT_SW[0]).
Does your circuit behave as expected? If so, congratulations! You've implemented your first digital circuit. If not, go back and edit top.v and fix any mistakes you made when entering the code.
Now that you've implemented a digital circuit, experiment a little to see if you can implement some other simple functions. You can do this within the same Quartus project or in another Quartus project (which would be stored in a separate directory).
Try implementing a circuit that compares two unsigned 4-bit numbers. Each number will be represented in binary by the values of 4 switches. Multi-bit numbers can be referred to using Verilog's array notation VARIABLE[msb:lsb], where msb is the most-significant bit of the number and lsb is the least-significant bit of the number. Refer to the first number as DPDT_SW[7:4] and the second number as DPDT_SW[3:0]. Verilog operators understand multi-bit values, so you can express this circuit in only a few lines of Verilog. Write, enter, download, and test Verilog code to light an LED when the value of DPDT_SW[7:4] is greater than the value of DPDT_SW[3:0].
Now try implementing a circuit that subtracts one 4-bit number from another, producing a 4-bit output. The numbers can again be input through the switches, i.e. DPDT_SW[7:4] and DPDT_SW[3:0]. Just as you input a 4-bit number using Verilog's array notation (DPDT_SW[3:0]), you can output a 4-bit number using the same notation (LED_RED[3:0] = some value). Note that this circuit won't have an if statement. Again, you can use Verilog operators on multi-bit values, so your code should be very short. Don't worry about inputs that produce negative results.
Each lab will include tasks for you to do to prepare for your lab section. You should complete these tasks before your lab meets. It will be difficult for you to complete the lab during your lab section if you do not complete the pre-lab assignment before your lab meets.
Your pre-lab assignment for this lab is to read through this handout.
After you have demonstrated one of the two variations to your GSI, submit the top.v you demonstrated.