Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Digital design with CPLD applications and VHDL (R. Dueck, 2000)

.pdf
Скачиваний:
259
Добавлен:
12.08.2013
Размер:
9 Mб
Скачать

250

C H A P T E R 6 • Digital Arithmetic and Arithmetic Circuits

The component declaration statement defines the ports of the component with the same names as in the full_add.vhd. Note that the form of the component declaration statement is almost the same as that of the component’s entity declaration. In effect, we are redefining the component entity in the top-level file of the design hierarchy.

The component instantiation statement is of the following form:

__instance_name: __component_name

GENERIC MAP (__parameter_name __parameter_value , __parameter_name __parameter_value)

PORT MAP (__component_port __connect_port, __component_port __connect_port);

In the generic map, a generalized parameter name can be mapped to a specific value when the component is instantiated. For example, a parameter name can be given a value that specifies the number of component output bits. We will not use this feature in our present examples.

In the port map, component ports are the names of the ports used in the component file and connect ports are the names of the ports, variables, or signals used in the higher-level design entity. For example, the component ports of the full adder component are a, b, c in, c_out, and sum. The connect ports for the instance adder1 are a(1), b(1), c0, c(1), and sum(1). The ripple carry from adder1 to adder2 is achieved by mapping the port c_in of adder2 to c(1), which is also mapped to the port c_out of adder1.

We can write the component instantiation statements more efficiently if we decide to use all ports of the component in the order they are defined. In this case, we can simply list the connect ports in the port map in the correct order, as follows:

adder1: full_add PORT MAP (a(1),b(1),c0, c(1),sum(1)); adder2: full_add PORT MAP (a(2),b(2),c(1),c(2),sum(2)); adder3: full_add PORT MAP (a(3),b(3),c(2),c(3),sum(3)); adder4: full_add PORT MAP (a(4),b(4),c(3),c4, sum(4));

If we only wish to use some of the component ports or use them in a different order than the order in which theywere originally defined, we must use the previous form of port map (i.e., a a(1), etc.).

GENERATE Statements

K E Y T E R M

GENERATE statement A VHDL construct that is used to create repetitive por-

tions of hardware.

The four component instantiation statements shown previously can be written in a more general form:

adder(i): full_add PORT MAP (a(i), b(i), c(i-1), c(i), sum(i));

A statement that can be written in this indexed form can be implemented using a

GENERATE statement, which has the form:

label:

FOR index IN range GENERATE

statements;

END GENERATE;

The VHDL code that follows shows how to use the statement to create a 4-bit adder.

generate statement

add4gen.vhd

 

 

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;

6.6 • Binary Adders and Subtractors

255

FIGURE 6.17

Example 6.21 Simulation of a

4-bit Adder/Subtractor

addSub4g.scf

The VHDL code for the adder/subtractor is the same as that for the 4-bit adder created using a GENERATE statement, except that there is an input to select the add or subtract function. This input (sub) is tied to c(0) and to a set of XOR functions that invert b for subtraction. Input b is transferred through the XOR functions without inversion for the add function.

the adder/subtractor. Table 6.8 shows the operahexadecimal and binary form. Note that the sums the differences are interpreted as signed opera-

tions. Any sum or difference can be interpreted either way, but this will sometimes result in a sign bit overflow. (e.g., the sums 8 8 10 and F 1 10 both indicate an overflow if they are interpreted as signed additions.)

Table 6.8

Add/Subtract Results

 

 

 

 

 

 

 

Hexadecimal Sum/Difference

Binary Equivalent

 

 

 

 

 

 

 

7

1

 

8

0111

0001 0 1000 (Unsigned)

8

8

 

10

1000

1000 1 0000 (Unsigned)

A 1 B

1010

0001 0 1011 (Unsigned)

F 0 F

1111

0000 0 1111 (Unsigned)

F 1 10

1111

0001 1 0000 (Unsigned)

0

1

F

0000

0001

1111 (Signed: 1)

0

8

 

8

0000

1000

1000

(Signed: 8)

0

A 6

0000

1010

0110

(Signed: 0 ( 6) 6)

0

F 1

0000

1111

0001

(Signed: 0 ( 1) 1)

 

 

 

 

 

 

 

 

EXAMPLE 6.23

Note that the simulation in Figure 6.17 shows some intermediate states on the sum wave-

 

form in between steady state values. Examine the transition from the sum F 0 F to the

 

sum F 1 10 by using the Zoom In function in the Simulator window. Briefly explain

 

how the intermediate states arise in this transition.

 

SOLUTION Figures 6.18 and 6.19 show the transition from F 0 F to F 1 10.

 

The transition on the sum waveform is from F to E to 0 or in binary from 1111 to 1110 to

FIGURE 6.18

 

Example 6.22

 

Interval from F to E

 

256

C H A P T E R 6 • Digital Arithmetic and Arithmetic Circuits

FIGURE 6.19

Example 6.22

Interval from F to 0

0000. This transition is the result of a change from 0 to 1 on the b1 input of the adder/ subtractor.

Figure 6.18 shows the interval from F to E (the time difference between the vertical line marking 36 ns and the arrow cursor, shown in the box labeled Interval) as 7.4 ns. This is the delay from b1 to sum1.

Figure 6.19 shows the interval from F to 0 on the sum waveform, given as 12.6 ns. This interval represents the time required for sum2, sum3, and sum4 to change after a

change on b1.

 

 

Table 6.9 Overflow Detector

Truth Table

SA

SB

S

V

 

 

 

 

0

0

0

0

0

0

1

1

0

1

0

0

0

1

1

0

1

0

0

0

1

0

1

0

1

1

0

1

1

1

1

0

 

 

 

 

Overflow Detection

We will examine two methods for detecting overflow in a binary adder/subtractor: one that requires access to the sign bits of the operands and result and another that requires access to the internal carry bits of the circuit.

Recall from Example 6.12 the condition for detecting a sign bit overflow in a sum of two binary numbers.

N O T E

If the sign bits of both operands are the same and the sign bit of the sum is different from the operand sign bits, an overflow has occurred.

This implies that overflow is not possible if the sign bits of the operands are different from each other. This is true because the sum of two opposite-sign numbers will always be smaller in magnitude than the larger of the two operands.

Here are two examples:

1.( 15) ( 7) ( 8); 8 has a smaller magnitude than 15.

2.( 13) ( 9) ( 4); 4 has a smaller magnitude than 13.

No carry into the sign bit will be generated in either case.

An 8-bit parallel binary adder will add two signed binary numbers as follows:

SA A7

A6 A5 A4 A3 A2 A1

(SA Sign bit of A)

SB B7 B6 B5 B4 B3 B2 B1

 

(SB Sign bit of B)

S 7

6 5 4 3 2 1

(S Sign bit of sum)

From our condition for overflow detection, we can make a truth table for an overflow variable, V, in terms of SA, SB, and S . Let us specify that V 1 when there is an overflow condition. This condition occurs when (SA SB) S . Table 6.9 shows the truth table for the overflow detector function.

6.6 • Binary Adders and Subtractors

257

The SOP Boolean expression for the overflow detector is:

V SA SB S SA SB S

Figure 6.20 shows a logic circuit that will detect a sign bit overflow in a parallel binary adder. The inputs SA, SB, and S are the MSBs of the adder A and B inputs and outputs, respectively.

 

FIGURE 6.20

 

Overflow Detector

 

 

EXAMPLE 6.24

Combine two instances of the 4-bit counter shown in Figure 6.15 and other logic to make

 

an 8-bit adder/subtractor that includes a circuit to detect sign bit overflow.

 

SOLUTION Figure 6.21 represents the 8-bit adder/subtractor with an overflow detector

 

of the type shown in Figure 6.20.

FIGURE 6.21

Example 6.24 8-Bit Adder With Overflow Detector

258

C H A P T E R 6 • Digital Arithmetic and Arithmetic Circuits

A second method of overflow detection generates an overflow indication by examining the carry bits into and out of the MSB of a 2’s complement adder/subtractor.

Consider the following 8-bit 2’s complement sums. We will use our previous knowledge of overflow to see whether overflow occurs and then compare the carry bits into and out of the MSB.

a. 80H 80H

 

 

b. 7FH 01H

 

 

c. 7FH 80H

 

 

d. 7FH C0H

 

 

a. 80H 10000000

10000000

 

 

10000000

 

 

1 00000000

(Sign bit overflow; V 1)

Carry into MSB 0

 

 

Carry out of MSB 1

 

 

b. 7FH 01111111

01111111

 

01H 00000001

00000001

 

 

0 10000000

(Sign bit overflow; V 1)

Carry into MSB 1

 

 

Carry out of MSB 0

 

 

c. 7FH 01111111

01111111

 

80H 10000000

10000000

 

 

0 11111111 (No sign bit overflow; V 0)

Carry into MSB 0

 

 

Carry out of MSB 0

 

 

d. 7FH 01111111

01111111

 

C0H 11000000

11000000

 

 

1 00111111 (No sign bit overflow; V 0)

Carry into MSB 1

Carry out of MSB 1

The above examples suggest that a 2’s complement sum has overflowed if there is a carry into or out of the MSB, but not both. For an 8-bit adder/subtractor, we can write the Boolean equation for this condition as V C8 C7. More generally, for an n-bit

adder/subtractor, V Cn Cn 1.

Figure 6.22 shows a circuit that can implement the overflow detection from the carry into and out of the MSB of an 8-bit adder.

8 A8

B8

C8 C7

7 A7

B7

C7 C6

V

FIGURE 6.22

6.7 • BCD Adders

259

SECTION 6.6B REVIEW PROBLEM

6.10What is the permissible range of values of a sum or difference, x, in a 12-bit parallel binary adder if it is written as:

a.A signed binary number?

b.An unsigned binary number?

6.7BCD Adders

(This section may be omitted without loss of continuity.)

K E Y T E R M

BCD adder A parallel adder whose output is in groups of 4 bits, each group rep-

resenting a BCD digit.

It is sometimes convenient to have the output of an adder circuit available as a BCD number, particularly if the result is to be displayed numerically. The problem is that most parallel adders have binary outputs, and 6 of the 16 possible 4-bit binary sums—1010 to 1111—are not within the range of the BCD code.

BCD numbers range from 0000 to 1001, or 0 to 9 in decimal. The unsigned binary sum of any two BCD numbers plus an input carry can range from 00000 ( 0000 0000

0) to 10011 ( 1001 1001 1 1910).

For any sum up to 1001, the BCD and binary values are the same. Any sum greater than 1001 must be modified, since it requires a second BCD digit. For example, the binary value of 1910 is 100112. The BCD value of 1910 is 0001 1001BCD. (The most significant digit of a sum of two BCD digits and a carry will never be larger than 1, since the largest

such sum is 1910.)

Table 6.10 shows the complete list of possible binary sums of two BCD digits (A and B) and a carry (C), their decimal equivalents, and their corrected BCD values. The MSD of the BCD sum is shown only as a carry bit, with leading zeros suppressed.

Table 6.10 Binary Sums of Two BCD Digits

and a Carry Bit

BinarySum

 

Corrected BCD

(A B C)

Decimal

(Carry BCD)

 

 

 

00000

0

0 0000

00001

1

0 0001

00010

2

0 0010

00011

3

0 0011

00100

4

0 0100

00101

5

0 0101

00110

6

0 0110

00111

7

0 0111

01000

8

0 1000

01001

9

0 1001

01010

10

1 0000

01011

11

1 0001

01100

12

1 0010

01101

13

1 0011

01110

14

1 0100

01111

15

1 0101

10000

16

1 0110

10001

17

1 0111

10010

18

1 1000

10011

19

1 1001