Laboratory 2 (individual) -- Combinational logic and multiple modules

Worth: 20 points
Assigned: 16 January 2009
Due: 23 January 2009

Overview

This lab will give you experience implementing a combinational circuit that spans multiple Verilog modules. Your circuit will compute and display hexadecimal values on the DE2's 7-segment LEDs.

Displaying numbers in hexadecimal

Hexadecimal (base 16) is a common way to represent numbers in digital circuits. Each hex digit shows the value of 4 bits of the number (value 0-15):
Value 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Representation in hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Your first task is to implement a circuit that displays the value of a 4-bit number as a hex digit. The hex digit will be displayed on the 7-segment LEDs.

Create a new Quartus project called lab2. As always, the top-level design entity for the project will be a module named top. Remember to download top.qsf into your project directory after you create the project but before you add any files.

After you create the project and download top.qsf, download top.v and hexdigit.v into the project directory. Add these files into the Quartus project via Project -> Add/Remove Files in Project... (screenshot). This will bring up the following window. Click the ... button to choose a file, which will bring up the following window. Select each file (top.v and hexdigit.v) and click Add to add it to the project. Alternatively, you can click the Add All button to add all Verilog files in the directory to the project.

After you're done adding files to the project, you should see the following screenshot, and you can click OK. When you return to the main Quartus window, I suggest you click the Files tab of the Project Navigator window (upper left). This will show you all the files in the current project (screenshot), and you can then double-click any of the files to edit it.

Next, complete the Verilog module hexdigit. hexdigit takes as input a 4-bit number (in) and produces a 7-bit array of values (out) based on the input value. The bits of out are meant to control the segments of a 7-segment LED, as per the following diagram:

              out[0]
             -------- 
            |        |
     out[5] |        | out[1]
            | out[6] |
             --------
            |        |
     out[4] |        | out[2]
            |        |
             --------
              out[3]

For each bit of out, the value 0 causes that segment of the LED to be lit, and the value 1 causes that segment of the LED to be off. This is opposite from the LED_RED devices you used in Lab 1. Complete the code so that the value of out returned by hexdigit generates a human-readable picture of the input value on the 7-segment LED.

Remember that each output value must be defined for each combination of input values (usually by having an unconditional else clause in each if statement). Also remember that multi-bit values are specified with the most-significant bit on the left. E.g., out = 7'b0110000; is shorthand for:

	out[6] = 1'b0;
	out[5] = 1'b1;
	out[4] = 1'b1;
	out[3] = 1'b0;
	out[2] = 1'b0;
	out[1] = 1'b0;
	out[0] = 1'b0;

Combining multiple modules in Verilog

After you've completed hexdigit.v, look at the module top. Just as in Lab 1, top lists the inputs and outputs for the circuit (DPDT_SW, HEX0, HEX1). Unlike Lab 1, the outputs (in this case, HEX0 and HEX1) are declared wire instead of reg--this follows the rule given in Lab 1 (declare a variable reg if it's assigned a value in the module; otherwise declare it wire).

Note the following two lines in top:

    hexdigit u1 (DPDT_SW[3:0], HEX0);
    hexdigit u2 (DPDT_SW[7:4], HEX1);

These lines are not executable statements. Rather, they specify that the circuit includes two instances of your hexdigit module (arbitrarily named u1 and u2), with both instances of hexdigit operating continuously and at the same time. Each instance of hexdigit continuously receives as input a set of values from the switches (DPDT_SW). The output from each instance of hexdigit is continuously fed to one of the 7-segment LEDs (HEX0 or HEX1).

Compile, download, and test your circuit. You should see two of the 7-segment LEDs display a picture corresponding to the values of the switches. If the wrong picture is displayed, fix your code in hexdigit and try again.

Compute and display the absolute value of the difference of two numbers

After you've implemented a circuit to display numbers in hexadecimal on the 7-segment LEDs, extend your circuit to compute the absolute value of the difference of two numbers and display the result on another 7-segment LED. Create a new Verilog file called difference.v and add it to your project. difference.v should contain a module difference that computes the absolute value of the difference of two 4-bit input values and produces a 4-bit output. Your code will be similar to the variations you wrote in Lab 1. Instead of taking DPDT_SW[7:4] and DPDT_SW[3:0] as inputs, however, difference will get its inputs from two input parameters (similar to hexdigit's input parameter). Similarly, instead of outputting directly to LED_RED, difference will output the result to a parameter.

Modify the code in the module top to include an instance of your module difference. The inputs to difference should be DPDT_SW[7:4] and DPDT_SW[3:0], and the outputs should be a new variable called diff. Declare diff in top after the declaration of the top module with the following code:

	wire [3:0] diff;

Add code in top to display the output of difference (i.e. diff) onto HEX2.

Pre-lab assignment

Before you arrive in lab, you should:

Remember that you can create and edit Verilog files with any text editor, i.e. you need not use Quartus's text editor. If you'd like to use Quartus on your personal PC, you can download it for free from Altera.

In-lab demonstration

Demonstrate your final circuit to the GSI. It should display DPDT_SW[7:0] in hexadecimal on two 7-segment LEDs, and it should display the absolute value of the difference of DPDT_SW[7:4] and DPDT_SW[3:0] on a third 7-segment LED. After you've demonstrated your circuit to the GSI, submit your final version of top.v, difference.v, and hexdigit.v.