Файл: Digital design with CPLD applications and VHDL (R. Dueck, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 13.06.2025
Просмотров: 8151
Скачиваний: 6
6.6 • Binary Adders and Subtractors |
251 |
||
ENTITY add4gen IS |
|||
PORT ( |
|||
c0 |
: IN |
BIT; |
|
a, b |
: IN |
BIT_VECTOR (4 downto 1); |
|
c4 |
: OUT |
BIT; |
|
sum |
: OUT |
BIT_VECTOR (4 downto 1)); |
|
END add4gen; |
|||
ARCHITECTURE adder OF add4gen IS
——Component declaration COMPONENT full_add
PORT (
a, b, c_in : IN BIT; c_out, sum : OUT BIT);
END COMPONENT;
—— Define a signal for internal carry bits SIGNAL c : BIT_VECTOR (4 downto 0);
BEGIN
c(0) c0; adders:
FOR i IN 1 to 4 GENERATE
adder: full_add PORT MAP (a(i),b(i),c(i-1),c(i),sum(i)); END GENERATE;
c4 c(4); END adder;
The GENERATE statement will create hardware that corresponds to the range of the index variable, i. In this case i goes from 1 to 4, so the statement instantiates four instances of the full adder. Since we have an input carry, an output carry and three internal carries, we must use a 5-bit signal (BIT_VECTOR (4 downto 0)) if we are to include all carry bits in indexed form. The input carry, c0, defined in the entity declaration, is assigned to the vector element c(0). Similarly, the output, c4, is assigned the value of the element c(4).
It is easy to expand the adder width by changing the range of the FOR GENERATE statement. For example, to make an 8-bit adder, we change the vectors to have a width of eight bits. The required VHDL code, shown next, requires the same number of lines of code as the 4-bit adder.
add8gen.vhd |
generate statement |
|||
ENTITY add8gen IS |
||||
PORT ( |
||||
C0 |
: IN |
BIT; |
||
a, b |
: IN |
BIT_VECTOR (8 downto 1); |
||
c8 |
: OUT |
BIT; |
||
sum |
: OUT |
BIT_VECTOR (8 downto 1)); |
||
END add8gen; |
||||
ARCHITECTURE adder OF add8gen IS |
||||
—— Component declaration |
||||
COMPONENT full_add |
||||
PORT ( |
||||
a, b, c_in : IN |
BIT; |
|||
c_out, sum : OUT |
BIT); |
|||
252 |
C H A P T E R 6 • Digital Arithmetic and Arithmetic Circuits |
||
END COMPONENT; |
|||
—— Define a signal for internal carry bits |
|||
SIGNAL c : BIT_VECTOR (8 downto 0); |
|||
BEGIN |
|||
c(0) |
c0; |
||
adders: |
|||
FOR i IN 1 to 8 GENERATE |
|||
adder: full_add PORT MAP (a(i), b(i), c(i-1), c(i), |
|||
sum(i)); |
|||
END GENERATE; |
|||
c8 |
c(8); |
||
END adder; |
|||
2’s Complement Subtractor |
|||
Recall the technique for subtracting binary numbers in 2’s complement notation. For ex- |
|||
ample, to find the difference 0101 0011 by 2’s complement subtraction: |
|||
1. Find the 2’s complement of 0011: |
|||
0011 |
|||
1100 |
(1’s complement) |
||
1 |
|||
1101 |
(2’s complement) |
||
2. Add the 2’s complement of the subtrahend to the minuend:
0101 |
( 5) |
1101 |
( 3) |
1 0010 |
( 2) |
(Discard carry)
We can easily build a circuit to perform 2’s complement subtraction, using a parallel binary adder and an inverter for each bit of one of the operands. The circuit shown in Figure 6.14 performs the operation (A B).
FIGURE 6.14
2’s Complement Subtractor
The four inverters generate the 1’s complement of B. The parallel adder generates the 2’s complement by adding the carry bit (held at logic 1) to the 1’s complement at the B inputs. Algebraically, this is expressed as:
A B A ( B) A B 1
where B is the 1’s complement of B, and (B 1) is the 2’s complement of B.
6.6 • Binary Adders and Subtractors |
253 |
|||
EXAMPLE 6.21 |
Verify the operation of the 2’s complement subtractor in Figure 6.14 by subtracting: |
|||
a. |
1001 0011 |
(unsigned) |
||
b. |
0100 0111 |
(signed) |
||
SOLUTION Let B be the 1’s complement of B.
a. Inverter inputs (B): |
0011 |
|
Inverter outputs (B): |
1100 |
|
Sum (A B 1): |
1001 |
( 9) |
1100 |
( 3) |
|
1 |
1 0110 |
( 6) |
|||||
(Discard carry) |
||||||
b. Inverter inputs (B): |
0111 |
|||||
Inverter outputs (B): |
1000 |
|||||
Sum (A B 1): |
0100 |
( 4) |
||||
1000 |
( 7) |
|||||
1 |
← |
|||||
Negative result: |
1101 |
( 3) |
||||
1’s complement of 1101: |
0010 |
|||||
1 |
||||||
2’s complement of 1101: |
0011 |
( 3) |
||||
Parallel Binary Adder/Subtractor
Figure 6.15 shows a parallel binary adder configured as a programmable adder/subtractor. The Exclusive OR gates work as programmable inverters to pass B to the parallel adder in either true or complement form, as shown in Figure 6.16.
FIGURE 6.15
2’s Complement Adder/Subtractor
254 |
C H A P T E R 6 • Digital Arithmetic and Arithmetic Circuits |
FIGURE 6.16
XOR as a Programmable Inverter
The ADD/SUB input is tied to the XOR inverter/buffers and to the carry input of the parallel adder. When ADD/SUB 1, B is complemented and the 1 from the carry input is added to the complement sum. The effect is to subtract (A B). When ADD/SUB 0, the B inputs are presented to the adder in true form and the carry input is 0. This produces an output equivalent to (A B).
This circuit can add or subtract 4-bit signed or unsigned binary numbers.
6.22Write a VHDL file to implement the 4-bit adder/subtractor shown in Figure 6.15. Also create a simulation file to test a representative selection of addition and subtraction operations.
SOLUTION The VHDL file is as follows:
addsub4g.vhd
sub |
: IN |
BIT; |
|
a, b |
: IN |
BIT_VECTOR (4 downto 1); |
|
c4 |
: OUT |
BIT; |
|
sum |
: OUT |
BIT_VECTOR (4 downto 1)); |
|
END addsub4g; |
|||
ARCHITECTURE adder OF addsub4g IS |
|||
—— Component declaration |
|||
COMPONENT full_add |
|||
PORT ( |
|||
a, b, c_in : IN |
BIT; |
||
c_out, sum : OUT |
BIT); |
||
END COMPONENT; |
|||
—— Define a signal for internal carry bits |
|||
SIGNAL c |
: BIT_VECTOR (4 downto 0); |
||
SIGNAL b_comp : BIT_VECTOR (4 downto 1);
BEGIN
—— add/subtract select to carry input (sub 1 for subtract) c(0) sub;
adders:
FOR i IN 1 to 4 GENERATE
——invert b for subtract (b(i) xor 1),
——do not invert for add (b(i) xor 0) b_comp(i) b(i) xor sub;
adder: full_add PORT MAP (a(i), b_comp(i), c(i-1), c(i),
sum(i));
END GENERATE; c4 c(4);
END adder;