CSE 140L Lab 4 Megafunction Tutorial

In this lab, you will use many blocks with multiple bit-width inputs/outputs, such as 4-bit adder, 4-bit multiplier, 4-bit multiplexer. Fortunately, Quartus II has provided us these modules, which are called as "megafunctions". This tutorial will demonstrate how to construct a 4-bit adder using megafunctions step by step. In the end, some hints on how to build other modules will be given.

Build a 4-bit Adder using Megafunctions

1. In your schematic diagram, double click in any blank space. In the popup symbol dialog window, go to the directory megafunctions/arithmetic/, select the lpm_add_sub module.

2. Click OK. In the popup "MegaWizard Plug-in Manager", Choose VHDL to be the output file type. Click Next.

3. Change the device family to be Cyclone II if it is not so. Change the input buses bit-width of "dataa" and "datab" to 4 bits, since we are building a 4-bit adder. For operation mode, keep the "addition only" selection. You can always refer to the schematic diagram in the left side to check your design.

4. Click Next. Next step asks whether we want to set one input to be constant. Keep the default "No" choice. Then click Next.

5. Next step asks whether we need additional inputs and outputs. Because we do not consider carry-in, carry-out or overflow in this lab, we keep all these options unchecked. Click Next.

6. Next step asks whether we need pipeline. Keep the "No" option. Click Next.

7. Next step shows the simulation library. Just click next to continue.

8. The final step displays the files that will be created. Click Finish. A new adder component will then be created and you can use your mouse to place it in your schematic window. If you get the diagram like follows, you are done!

9. Note that in the created component, both inputs and outputs should be 4-bit buses. Just connect your data lines to them using buses, as what you did for segment display in Lab 2.


Hints on Building Other Modules

- Multiplier: Use the megafunction lpm_mult in megafunctions/arithmetic/. In the first step of the wizard, set both input to be 4 bits and the output to be 8 bits; in the second step, set the type of multiplication as "unsigned"; the rest steps will be the same as the adder. In order to obtain the least significant 4 bits, you need to use bus to specify in your schematic as the following figure:

- Comparator: Use the megafunction lpm_compare in megafunctions/arithmetic/. In the first step, set the bit width to be 4 and choose the compare type to be a <= b; In the second step, set the type of comparison as "unsigned"; the rest steps will be the same.

- Multiplexer: Use the megafunction lpm_mux in megafunctions/gates/. In the first step, set the number of data inputs to be 2 if you are building a 2-to-1 MUX, 4 if you are building a 4-to-1 MUX, and change the bit width to be 4. Note that in the created MUX symbol, data0 is in the bottom, which is the reverse of our convention. So please connect the input buses carefully.

- D Flip-Flop: Use the megafunction lpm_dff in megafunctions/storage/. In the first step, set the number of flip-flops to be 4, and add clock enable signal; in the second step, add synchronous clear signal. Then you are done.