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

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

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

290 C H A P T E R 7 • Introduction to Sequential Logic

FIGURE 7.27

Gated SR Latch

Figure 7.27c shows the ENABLE input as a synchronizing signal. A periodic pulse waveform is present on the ENABLE line. The S and R inputs are free to change at random, but the latch outputs will change only when the ENABLE input is active. Since the ENABLE pulses are equally spaced in time, changes to the latch output can occur only at fixed intervals. The outputs can change out of synchronization if S or R change when ENABLE is HIGH. We can minimize this possibility by making the ENABLE pulses as short as possible.

Table 7.4 represents the function table for a gated SR latch.

Table 7.4 Gated SR Latch Function Table

EN

S

R

Qt 1

Qt 1

Function

1

0

0

Qt

Qt

No change

1

0

1

0

1

Reset

1

1

0

1

0

Set

1

1

1

0

0

Forbidden

0

X

X

Qt

Qt

Inhibited

 

 

 

 

 

 

 

7.3 • Gated Latches

291

 

 

EXAMPLE 7.4

Figure 7.28 shows two gated latches with the same S and R input waveforms but different

 

ENABLE waveforms. EN1 has a 50% duty cycle. EN2 has a duty cycle of 16.67%.

 

Draw the output waveforms, Q1 and Q2. Describe how the length of the ENABLE pulse affects the output of each latch, assuming that the intent of each circuit is to synchronize the output changes to the beginning of the ENABLE pulse.

FIGURE 7.28

Example 7.4

Effect of ENABLE Pulse Width

SOLUTION Figure 7.28b shows the completed timing diagram. The longer ENABLE pulse at latch 1 allows the output to switch too soon during pulses 1 and 4. (“Too soon” means before the beginning of the next ENABLE pulse.) In each of these cases, the S and R inputs change while the ENABLE input is HIGH. This premature switching is eliminated in latch 2 because the S and R inputs change after the shorter ENABLE pulse is finished. A shorter pulse gives less chance for synchronization error, since the time for possible output

changes is minimized.

Transparent Latch (Gated D Latch)

Figure 7.29 shows the equivalent circuit of a gated D (“data”) latch, or transparent latch. This circuit has two modes. When the ENABLE input is HIGH, the latch is transparent because the output Q goes to the level of the data input, D. (We say, “Q follows D.”) When the ENABLE input is LOW, the latch stores the data that was present at D when ENABLE was last HIGH. In this way, the latch acts as a simple memory circuit.

292 C H A P T E R 7 • Introduction to Sequential Logic

FIGURE 7.29

Transparent Latch

The latch in Figure 7.29 is a modification of the gated SR latch, configured so that the S and R inputs are always opposite. Under these conditions, the states S R 0 (no change) and S R 1 (forbidden) can never occur. However, the equivalent of the no change state happens when the ENABLE input is LOW, when the latch steering gates are inhibited.

Figure 7.30 shows the operation of the transparent latch in the inhibit (no change), set, and reset states. When the latch is inhibited, the steering gates block any LOW pulses to the latch gates; the latch does not change states, regardless of the logic level at D.

FIGURE 7.30

Operation of Transparent Latch

If EN 1, Q follows D. When D 1, the upper steering gate transmits a LOW to the SET input of the latch and Q 1. When D 0, the lower steering gate transmits a LOW to the RESET input of the output latch and Q 0.

Table 7.5 shows the function table for a transparent latch.

Table 7.5

Function Table of a Transparent Latch

 

 

 

 

 

 

EN

D

Qt 1

Qt 1

Function

Comment

0

X

Qt

Qt

No Change

Store

 

 

 

 

 

 

1

0

0

1

Reset

Transparent

1

1

1

0

Set

 

 

 

 

 

 

 

d_latch.gdf d_latch.scf

d_lch.vhd d_lch.scf

7.3 • Gated Latches

293

Implementing D Latches in MAX PLUS II

A D latch can be implemented in MAX PLUS II as a primitive in a Graphic Design File or in a VHDL design entity. It can also be created with a behavioral or structural description in a VHDL file.

Figure 7.31 shows a D latch primitive in a MAX PLUS II Graphic Design File. Figure 7.32 shows a simulation of the latch. From 0 to 500 ns, ENABLE is HIGH and the latch is in the transparent mode (Q follows D). When ENABLE goes LOW, the last value of D (0) is stored until ENABLE goes high again, just before 800 ns. When ENABLE goes LOW again, a new value of D (1) is stored until the end of the simulation.

LATCH

 

D

OUTPUT

Q

 

 

ENA Q

 

 

 

 

 

 

 

 

FIGURE 7.31

D-Latch in a MAX PLUS II Graphic Design File

FIGURE 7.32

Simulation for a D Latch

InVHDL, a PROCESS statement is concurrent, but the statements inside the PROCESS are sequential. In other words, anything described by a PROCESS acts like a separate component in a design entity. However, the interior of the component so described acts as a sequential circuit. Since the behavior of a D latch is sequential, its description can be created inside a PROCESS. (You can pull a latch out of a bin of parts and connect it in a circuit, but the inside of the part is sequential.) The following VHDL code describes a D latch.

lch.vhd

latch with active-HIGH level-sensitive enable

d_lch IS

 

PORT(

 

 

d, ena : IN

BIT;

q

: OUT

BIT);

END d_lch;

ARCHITECTURE a OF d_lch IS

BEGIN

PROCESS (d, ena) BEGIN

IF (ena ´1´) THEN q d;

END IF;

END PROCESS;

END a;

294 C H A P T E R 7 • Introduction to Sequential Logic

Another method, recommended by the MAX PLUS II documentation, is to instantiate a LATCH primitive in a VHDL file. The primitive is contained in the altera library, in a package called maxplus2. The component declaration for this primitive is:

COMPONENT LATCH

PORT (d

: IN

STD_LOGIC;

ena

: IN

STD_LOGIC;

q

: OUT STD_LOGIC);

END COMPONENT;

Since the component declaration is in the maxplus2 package, you do not have to declare it in the file in which you are using it. A VHDL file that uses the latch primitive is listed next. The component declaration uses STD LOGIC types, so we must include the type definitions in the ieee library (std_logic_1164 package).

lch_prim.vhd

USE ieee.std_logic_1164.ALL;

LIBRARY altera;

USE altera.maxplus2.ALL;

 

ENTITY lch_prim IS

 

 

 

PORT(

 

 

 

d_in, enable

: IN

STD_LOGIC;

 

q_out

: OUT

STD_LOGIC);

 

END lch_prim;

 

 

 

ARCHITECTURE a OF lch_prim IS

 

BEGIN

 

 

 

—— Instantiate a latch from a MAX PLUS II primitive

 

latch_primitive: latch

 

 

PORT MAP (d

d_in,

 

ena

enable,

 

q

q_out);

 

END a;

 

 

 

More information about MAX PLUS II primitives can be found in MAX PLUS II

 

Help. In the Help menu, select Primitives. By clicking on the name of a particular primi-

 

tive, you can determine whether it can be instantiated in a VHDL file and what its compo-

 

nent declaration is, if available.

 

 

 

7.5

A system for monitoring automobile traffic is set up at an intersection, with four sensors,

 

placed as shown in Figure 7.33. Each sensor monitors traffic for a particular direction.

 

When a car travels over a sensor, it produces a logic HIGH. The status of the sensor system

FIGURE 7.33

Example 7.5

Sensor Placement in a Traffic

Intersection

FIGURE 7.34

Sensors

Example 7.5

 

1

D Latch Collection of Data

2

3

4

Timing

Pulse

 

 

 

7.3 • Gated Latches

295

 

 

 

Data Logging System

 

 

 

 

 

 

 

 

 

D1

Q1

 

Q1

 

 

 

 

 

 

ENA

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D2

Q2

 

Q2

 

 

 

 

 

 

ENA

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D3

Q3

 

Q3

 

 

 

 

 

 

ENA

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

D4

Q4

 

Q4

 

 

 

 

 

 

ENA

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

is captured for later analysis by a set of D latches, as shown in Figure 7.34. A timing pulse enables the latches once every five seconds and thus stores the system status as a “snapshot” of the traffic pattern.

Figure 7.35 shows the timing diagram of a typical traffic pattern at the intersection. The D inputs show the cars passing through the intersection in the various lanes. Complete this timing diagram by drawing the Q outputs of the latches.

How should we interpret the Q output waveforms?

FIGURE 7.35

Example 7.5

Latch Configuration and Timing

Diagram

296 C H A P T E R 7 • Introduction to Sequential Logic

SOLUTION Figure 7.35 shows the completed timing diagram. The ENABLE input synchronizes the random sensor pattern to a 5-second standard interval. A HIGH on any Q output indicates a car over a sensor at the beginning of the interval. For example, at the beginning of the first interval, there is a car in the northbound lane (Q1) and one in the

southbound lane (Q2). Similar interpretations can be made for each interval.

Multi-bit Latches in VHDL

K E Y T E R M S

Library of Parameterized Modules (LPM) A standardized set of components for which certain properties can be specified when the component is instantiated.

Parameter (in an LPM component) A property of a component that can be specified when the component is instantiated.

Generic map A VHDL construct that maps one or more parameters of a component to a value for that instance of the component.

Port map A VHDL construct that maps the name of a port in a component to the name of a port, variable, or signal in a design entity that uses the component.

We can easily use VHDL to implement latches with multiple D inputs and Q outputs, but with a common ENABLE line, as in Figure 7.34. Three approaches are:

1.Use a behavioral description, as we did earlier for a single latch (d_lch.vhd). Use STD_LOGIC_VECTOR types for D and Q, rather than STD_LOGIC.

2.Altera recommends using a latch primitive or predefined component, rather than creating your own latch structures. We can use multiple LATCH primitives, instantiated by a GENERATE statement, as we did for multiple instances of a full adder in Chapter 6.

3.Use a latch component from the Library of Parameterized Modules (LPM). These components are specified in the lpm_components package in the lpm library.

Certain properties of an LPM component, such as the number of inputs or outputs, can be specified when the component is instantiated. These properties are referred to as parameters, and are listed in a generic map. For example, to make the latch output and input four bits wide, we set the parameter called LPM_WIDTH to a value of 4. The various parameters of an LPM component can be found in the LPM Quick Reference on the CD that accompanies this book or in the MAX PLUS II Help menu under

Megafunctions/LPM.

An input or output of an LPM component is called a port. A port map is used to make a correspondence between the port names in the component declaration and the port names used in the file containing the component. Since LPM components are declared in a separate package, we must refer to the MAX PLUS II Help or the LPM Quick Reference to determine the port names for a component. LPM components are instantiated the same as any other component.

The three VHDL files that follow each specify a 4-bit latch with common enable, each using one of the above methods.

Behavioral Description:

——ltch4bhv.vhd

——D latch with active-HIGH level-sensitive enable

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ltch4bhv.vhd

ltch4prm.vhd

ltch4lpm.vhd

ltch4lpm.scf

 

 

 

7.3 • Gated Latches

297

ENTITY ltch4bhv

IS

 

 

PORT(d

:

IN

STD_LOGIC_VECTOR (3 downto 0);

 

enable

:

IN

STD_LOGIC;

 

q

:

OUT

STD_LOGIC_VECTOR (3 downto 0));

 

END ltch4bhv;

a OF ltch4bhv IS

(enable, d)

BEGIN

IF (enable ´1´) THEN q d;

END IF;

END PROCESS;

END a;

4 LATCH Primitives and a GENERATE Statement:

——ltch4prm.vhd

——D latch with active-HIGH level-sensitive enable

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

LIBRARY altera;

USE altera.maxplus2.ALL;

ENTITY ltch4prm

IS

 

PORT(d_in : IN

STD_LOGIC_VECTOR (3 downto 0);

enable

: IN

STD_LOGIC;

q_out

: OUT

STD_LOGIC_VECTOR (3 downto 0));

END ltch4prm;

 

 

 

ARCHITECTURE a OF ltch4prm IS BEGIN

—— Instantiate a latch from a MAX PLUS II primitive latch4:

FOR i IN 3 downto 0 GENERATE latch_primitive: latch

PORT MAP (d d_in (i), ena enable, q q_out (i)); END GENERATE;

END a;

LPM Latch:

——ltch4lpm.vhd

——4-BIT D latch with active-HIGH level-sensitive enable

——Uses a latch component from the Library of Parameterized

——Modules (LPM)

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

LIBRARY lpm;

USE lpm.lpm_components.ALL;

ENTITY ltch4lpm

IS

 

 

PORT(d_in

: IN

STD_LOGIC_VECTOR (3 downto

0);

enable

: IN

STD_LOGIC;

 

q_out

: OUT

STD_LOGIC_VECTOR (3 downto

0) );

END ltch4lpm;

 

 

 

 

q_out);

298 C H A P T E R 7 • Introduction to Sequential Logic

ARCHITECTURE a OF ltch4lpm IS

BEGIN

—— Instantiate latch from an LPM component latch4: lpm_latch

GENERIC MAP (LPM_WIDTH 4) PORT MAP (data d_in,

gate enable, q

END a;

All three files can be tested with the same simulation, shown in Figure 7.36. The inputs, d_in, represent a 4-bit group of signals, as do the outputs, q_out. An increasing count, from 5 to C (0101 to 1100) is applied to d_in. This count contains both states (0 and 1) for each input bit. For each applied input state, the output bus, q_out, does not change until the enable line goes HIGH.

FIGURE 7.36

Simulation of a 4-bit D Latch

SECTION 7.3 REVIEW PROBLEM

7.3Write the VHDL code for a 16-bit latch with common active-HIGH enable, using MAX PLUS II latch primitives.

7.4Edge-Triggered D Flip-Flops

K E Y T E R M S

Edge The HIGH-to-LOW (negative edge) or LOW-to-HIGH (positive edge) transition of a pulse waveform.

CLOCK An enabling input to a sequential circuit that is sensitive to the positiveor negative-going edge of a waveform.

Edge-triggered Enabled by the positive or negative edge of a digital waveform.

Edge-sensitive Edge-triggered.

Level-sensitive Enabled by a logic HIGH or LOW level.

Flip-flop A sequential circuit based on a latch whose output changes when its

CLOCK input receives an edge.

In Example 7.4, we saw how a shorter pulse width at the ENABLE input of a gated latch increased the chance of the output being synchronized to the ENABLE pulse waveform. This is because a shorter ENABLE pulse gives less chance for the SET and RESET inputs to change during the time the latch is enabled.

A logical extension of this idea is to enable the latch for such a small time that the width of the ENABLE pulse is almost zero. The best approximation we can make to this is to allow changes to the circuit output only when an enabling, or CLOCK, input receives the edge of an input waveform. An edge is the part of a waveform that is in transition from

7.4 • Edge-Triggered D Flip-Flops

299

LOW to HIGH (positive edge) or HIGH to LOW (negative edge), as shown in Figure 7.37.

We can say that a device enabled by an edge is edge-triggered or edge-sensitive.

FIGURE 7.37

Edges of a CLOCK Waveform

Since the CLOCK input enables a circuit only while in transition, we can refer to it as a “dynamic” input. This is in contrast to the ENABLE input of a gated latch, which is levelsensitive or “static,” and will enable a circuit for the entire time it is at its active level.

FIGURE 7.38

D Flip-Flop Logic Symbol

FIGURE 7.39

D Flip-Flop Equivalent Circuit

Latches vs. Flip-Flops

K E Y T E R M

Edge detector A circuit in an edge-triggered flip-flop that converts the active edge of a CLOCK input to an active-level pulse at the internal latch’s SET and RESET inputs.

A gated latch with a clock input is called a flip-flop. Although the distinction is not always understood, we will define a latch as a circuit with a level-sensitive enable (e.g., gated D latch) or no enable (e.g., NAND latch) and a flip-flop as a circuit with an edge-triggered clock (e.g., D flip-flop). A NAND or NOR latch is sometimes called an SR flip-flop. By our definition this is not correct, since neither of these circuits has a clock input. (An SR flip-flop would be like the gated SR latch of Figure 7.27 with a clock instead of an enable input.)

The symbol for the D, or data, flip-flop is shown in Figure 7.38. The D flip-flop has the same behavior as a gated D latch, except that the outputs change only on the positive edge of the clock waveform, as opposed to the HIGH state of the enable input. The triangle on the CLK (clock) input of the flip-flop indicates that the device is edge-triggered.

Table 7.6 shows the function table of a positive edge-triggered D flip-flop.

Figure 7.39 shows the equivalent circuit of a positive edge-triggered D flip-flop. The circuit is the same as the transparent latch of Figure 7.29, except that the enable input (called CLK in the flip-flop) passes through an edge detector, a circuit that converts a positive edge to a brief positive-going pulse. (A negative edge detector converts a negative edge to a positive-going pulse.)

Table 7.6 Function Table for a Positive

Edge-Triggered D Flip-Flop

CLK

D

Qt 1

Qt 1

Function

0

0

1

Reset

1

1

0

Set

0

X

Qt

Qt

Inhibited

1

X

Qt

Qt

Inhibited

X

Qt

Qt

Inhibited