Docsity

adders and subtractors, Lecture notes of Computer science

Partial preview of the text

Download adders and subtractors and more Lecture notes Computer science in PDF only on Docsity!

ADDERS AND SUBTRACTORS

September 18th^ , 2007 CSC Fall 2007 Prepared by: Steven Medina

PURPOSE

The purpose of this lab is to show you how to implement an adder using Quartus. As the name implies, adders are used to add two sets of values together. Adders are a very common design in digital design. For example, a CPU will use an adder to have its program counter point to its next instruction. This is done by adding a constant value of 4 to the current instructions memory address. You will be using adders both here, and in future labs. You will be shown three different kinds of adders. They are the half-adder, the full-adder. And the ripple carry adder. The purpose is to show you not only what each is, but why they are important. You will learn why each is important as you go through this lab. After creating our adder designs in Quartus, you will test your design on Altera’s DE2 programmable board. The DE2 board will be explained later in this lab.

HALF-ADDER

A half-adder (HA) is an adder that accepts two inputs and gives two outputs. The two inputs are the two single bit binary values that will be added to each other. The two outputs represent the sum. We need two outputs (rather than one output) because the sum may have a carry bit. For example, in binary, 1+0 = 1. This situation has no carry bit in the output. In other words, the output itself is 1 bit. However, if we add 1+1, we get 10. This output is 2 bits long. This is a case where the carry-bit for the output is needed. A half adder consists of two logic gates. These are an AND gate, and an Exclusive OR gate. A diagram of a half adder is shown below.

. Figure 1: Half Adder using XOR and AND gate.

For the design of the half adder, do the following.

  1. Create a new project in Quartus. Where it asks for the family or device you wish to target for compilation, select the Cyclone II board. Under the list of available devices, click EP2C35F672C6.

  2. Design the half-adder shown in figure 1 in a block diagram file (bdf). Remember that the XOR and AND gates are available by clicking the “Symbol Tool” icon.

  3. Compile your design.

ENTITY dec_7seg IS PORT( hex_digit : IN STD_LOGIC; segment_a, segment_b, segment_c, segment_d, segment_e, segment_f, segment_g : OUT std_logic); END dec_7seg;

ARCHITECTURE a OF dec_7seg IS SIGNAL segment_data : STD_LOGIC_VECTOR(6 DOWNTO 0);

BEGIN PROCESS (Hex_digit) -- HEX to 7 Segment Decoder for LED Display BEGIN -- Hex-digit is the four bit binary value to display in hexadecimal CASE Hex_digit IS WHEN '0' => segment_data <= "1111110"; WHEN '1' => segment_data <= "0110000"; WHEN OTHERS => segment_data <= "0111110"; END CASE; END PROCESS;

-- extract segment data bits and invert -- LED driver circuit is inverted

segment_a <= NOT segment_data(6); segment_b <= NOT segment_data(5); segment_c <= NOT segment_data(4); segment_d <= NOT segment_data(3); segment_e <= NOT segment_data(2); segment_f <= NOT segment_data(1); segment_g <= NOT segment_data(0); END a;

  1. Create a symbol block out of this VHDL file. Remember that the symbol block will be automatically named the same name as the entity of your VHDL file, which is dec_7seg. A picture of what the block should look like is shown below.

Figure 3: Image of dec_7seg block.

  1. Add one of the dec_7seg blocks to both the carry output and the sum output.

  2. Add a dec_7seg block to each wire coming out of the input pins. You have three input pins going into three inputs. Simply split the wire coming out of each input pin between your original design and the dec_7seg blocks.

  3. Create output pins for each of the seven outputs of both dec_7seg blocks. Naming them something such as “a1, b1, c1…g1” and “a2, b2, c2…g2” from top to bottom would be a good naming convention.

  4. Simulate your design using a vector waveform file. Just as in the half-adder, set your inputs to have every possible combination of adding before simulating. Of course, since this is a full-adder, you will have more possible combinations of inputs. Do your outputs show that the inputs are being added together?

  5. In your simulation output, you will have outputs for each of the pins from the dec_7seg outputs. These represent a seven-segment display like the one shown below.

Figure 4: Seven-Segment Display

Each of the 7 lines is labeled according to the labels you would have on your dec_7seg block if you used the naming convention I suggested.

  1. The logic values output from the dec_7seg are INVERTED logic values from what would really be displayed on the seven-segment display on the DE2 board. Therefore, if output “a1” in your vector waveform file gives a logic value 1, the seven segment display would actually turn “a” off from figure 3. Using the output of your vector waveform file, make sure that for any given sum of two numbers, the outputs from dec_7seg will display the correct value on a seven-segment display. Again, the logic values coming from the dec_7seg blocks are INVERTED from what will actually appear on the DE2 board.

4-BIT RIPPLE CARRY ADDER

Now that you have created a full-adder, you can begin adding numbers that are larger than 1-bit in length. In this case, you will design a circuit that will add two 4-bit numbers together. This will be done by cascading four full-adders together. For example, if we wanted to add the numbers 1011 and 1110 together, we should get the proper result of 11001 by using nothing but full adders. In other words we need to implement a design that can add numbers such as:

Figure 6: Black box of hexadecimal-to-binary converter.

  1. Create a symbol block for the VHDL code you just wrote. Connect an input pin into your hex-to-binary converter (remember, it is a 4-bit input). One of each output should connect to the first input for each full-adder in your design.

  2. Add another hex-to-binary converter in your diagram. Connect an input pin into the block. Connect one of each output into the other input for each of the full-adders in your design.

  3. Split both input pins that go into your hex-to-binary converter into a dec_7seg_hex block. You should have already created this block in step 4.

  4. The input pin that goes into the “carry” input can remain the same, with no need for converting it into hexadecimal. However, split the wire coming out of this input pin into a regular dec_7seg block. You must import this block into your design if you have not done so already. To import a file:

a) Click project=>Add/remove files in project. The screen that appears is shown below.

Figure 7: Add/Remove project screen.

b) Click on the box with the three dots. A screen will appear asking which file you wish to add.

c) Add the “dec_7seg” file. If it is not in the same directory as your Ripple-Carry Adder project, you must copy and paste it there first, and redo this process.

  1. When the values are output from each full-adder, they must be converted back to hexadecimal. Therefore, you must now write a small VHDL file that will create the exact opposite as figure 6. In other words, you want to convert binary to hexadecimal. You must then create a symbol block out of your VHDL code. A image of what a block like this would look like is shown below.

Figure 8: Binary-to-hexadecimal converter.

  1. Connect one data outputs from each full-adder into one of the inputs of the binary-to- hex converter.

  2. Connect a wire from the carry output of the last full adder into a regular dec_7seg block. Create output pins for all seven outputs.

  3. Output the binary-to-hex converter into a dec_7seg_hex block. Create output pins for all seven outputs.

  4. Compile your design.

  5. Simulate your design in a vector waveform file.

  6. See if you can trace a combination of inputs to see if the two dec_7seg_hex blocks have the right logic values. Remember; invert the logic values to see what the ACTUAL seven-segment decoder on the DE2 board would display.

Shown below in figure 9 is a sample of what the final design might look like.

  1. Create a symbol block out of your two’s compliment VHDL code.

  2. Connect all the inputs and outputs to either a dec_7seg or a dec_7seg_hex block; whichever is appropriate for that input or output.

  3. Compile your design.

  4. Simulate your design in a vector waveform file to see if your design adds and/or subtracts, depending on the value of the input pin going into the two’s compliment block.

A final design of your Adder/Subtractor will look something like the image below.

Figure 11: Image of an Adder/Subtractor using a Ripple-Carry Adder.

The block that is highlighted in red is the symbol block for the ripple-carry adder. The block highlighted in green is the symbol block for the two’s complimentor. All the rest of the blocks are either dec_7seg blocks or input/out pins.