Laboratory 1 (individual) -- Introduction to Altera DE2 and Quartus

Worth: 10 points
Assigned: 9 January 2009
Due: 16 January 2009

Overview

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.

Altera DE2 development and education board

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.

The DE2 (shown on the right; click to expand) is designed to support a wide range of experiments. It combines a variety of logic and I/O devices onto a single printed-circuit board and allows you to configure and control these devices to create different applications. The logic devices on the DE2 are an FPGA (a programmable logic device) and several memory components (SDRAM, SRAM, and flash RAM). The I/O devices on the DE2 are a small LCD display, numerous LEDs (lights), and switches. In addition, the DE2 has connections to a variety of external I/O devices, including PS/2 (keyboard and mouse), USB, VGA (video), audio (microphone and speaker), TV, Ethernet, RS-232 (serial port), Secure Digital and IrDA (infrared).

In this first lab, we will use the Cyclone II FPGA, LEDs and switches.

Figure 2.1 from the DE2 user manual shows where these components are on the board.

Creating digital circuits with Quartus CAD software

Quartus is the software package we will use in this class to build digital circuits on the FPGA. Quartus is composed of multiple tools, with separate tools for the various steps of building a digital circuit: design entry, synthesis, downloading, and testing.
  1. Your first job as a circuit designer is design entry: expressing the circuit in a format that a machine can understand. In this class, we will express digital circuits as Verilog code (Verilog is a language used to describe hardware, i.e. an HDL).
  2. The next step is to synthesize the Verilog code. Synthesis means to translate the Verilog code into a low-level circuit specification for the FPGA.
  3. When you are ready to try your circuit on hardware, you will download the low-level configuration (that you generated during synthesis) to the FPGA.
  4. Finally, you will test your circuit on the FPGA by varying the inputs to the DE2 and watching its output.

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.

Creating a Quartus project

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.

Design entry

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:

  1. The code describes the circuit for the module top. This module name corresponds to the name of the top-level design entity you specified when creating the Quartus project.
  2. 1'b0 and 1'b1 are how you express numbers in Verilog. The number before the ' indicates the number of bits in the number; in this case, we are expressing a 1-bit number. The letter after the ' indicates which base the number is in; in this case, we are expressing numbers in base 2, or binary, denoted by the letter b. The number after the letter indicates the value of the number; in this case, we are expressing the numbers 0 and 1. Other possible bases for expressing numbers are decimal (denoted by the letter d) and hexadecimal (denoted by the letter h). For example, an 8-bit number with the value 200 could be written as 8'd200 or 8'b11001000 or 8'hc8.
  3. The module takes an input variable (DPDT_SW) and produces an output variable (LED_RED). Each variable is declared as an array of 18 bits (bits 17 through 0). The file top.qsf that you downloaded when you created the project connects the DPDT_SW array to the 18 switches on the DE2. Similarly, top.qsf connects the LED_RED array to the 18 red LEDs on the DE2.
  4. Each variable in Verilog (such as DPDT_SW and LED_RED) is declared as type wire or type reg. Use the following rule to determine which type to use for a variable: if the variable is assigned a value in the module (through a statement such as LED_RED[0] = 1'b1), it should be declared reg. Otherwise it should be declared wire.
  5. The code block starting with always @* specifies the relationship between LED_RED and DPDT_SW. It specifies how the value of LED_RED[0] (the 0th element of the LED_RED array) depends on the values of DPDT_SW[0], DPDT_SW[1], and DPDT_SW[2]. always @* means "continuously drive the wire LED_RED based on the value computed in this block". In this case, the value will depend on DPDT_SW and will implement a "majority vote": the output of the LED will be 1 if and only if two or more of the switches are 1.
  6. The syntax of Verilog is similar to that of C++.

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.

Synthesis

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.

Downloading

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.

Testing

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.

Variations

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.

Pre-lab assignment

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.

In-lab demonstration

Demonstrate to the GSI one of the two variations (comparison or subtraction of two 4-bit numbers). You won't have time to demonstrate your circuit on all 256 possible input values. Instead, carefully select a few input values that demonstrate the correctness of your circuit for a variety of cases. Remember to show interesting boundary cases, i.e. input values that are on the boundary between two different types of behaviors.

After you have demonstrated one of the two variations to your GSI, submit the top.v you demonstrated.