Файл: Digital design with CPLD applications and VHDL (R. Dueck, 2000).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 13.06.2025

Просмотров: 8109

Скачиваний: 6

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

9.4 • Programming Binary Counters in VHDL

385

9.4 Programming Binary Counters in VHDL

K E Y T E R M S

If statement A VHDL construct in which statements within the IF statement are executed only when a specified Boolean condition is satisfied.

Attribute A property associated with a named identifier in VHDL. (For example, the attribute EVENT, when associated with the identifier clk (written clk’EVENT), indicates, when true, that a transition has occurred on the input called clk.)

When using VHDL to create a counter, we can take several approaches. We can encode the Boolean equations of the counter directly with concurrent signal assignment statements; we can use VHDL code to describe the behavior of the counter; we can use a CASE statement to implement the state diagram of the counter; or we can use a predefined counter, such as those found in the MAX PLUS II Library of Parameterized Modules (LPM) and map its ports to the ports of a VHDL design entity.

If we chose to use concurrent signal assignments to encode the Boolean equations of a counter, we could derive the following equations for a 4-bit counter with D flip-flops.

d(3)<= q(3)xor(q(2)and q(1)and q(0));,

d(2)<= q(2)xor(q(1)and q(0));

d(1)<= q(1)xor q(1);,

d(0)<= not q(0);,

In Chapter 5, we saw that using concurrent signal assignment statements is an inefficient way to code many digital functions. (For one thing, if we use this procedure, we must know what the equations are. Getting to that point requires a lot of work that can be done by the VHDL compiler.) While acknowledging this as a possible option, we will not examine this method any further for the count logic of binary counters.

In this section, we will design a counter using a behavioral description and using an LPM counter. The design of a counter as a state machine will be examined in the next chapter.

Behavioral Description of Counters

The following VHDL code shows the behavioral description of a simple 8-bit counter (ct_simp.vhd) with asynchronous clear.

ct_simp.vhd

clear

: IN

BIT;

q

: OUT

INTEGER RANGE 0 TO 255);

END ct_simp;

ARCHITECTURE a OF ct_simp IS

BEGIN

PROCESS (clk, clear)

VARIABLE count : INTEGER RANGE 0 TO 255;

BEGIN

If (clear = ‘0’) THEN count := 0;

ELSE

IF (clk’EVENT AND clk = ‘1’) THEN count := count + 1;

END IF;


386 C H A P T E R 9 • Counters and Shift Registers

END IF;

q <= count;

END PROCESS;

END a;

Recall that the PROCESS statement has the following syntax:

PROCESS (sensitivity list)

[VARIABLE variable name :type [range]; ]

BEGIN

Process statements

END PROCESS;

Square brackets [ ] indicate an optional part of the code.

When there is a change in an item in the sensitivity list, the process statements are executed. For a synchronous counter, the list would often only include clock, since any action in a synchronous circuit depends on a clock transition. Since the clear function in this counter is asynchronous, the clear input must also be monitored for any changes.

To hold the accumulating output value of the counter, we define a variable called count, presumed to have an initial value of 0, but defined for the range of 0 to 255. (This 8- bit value rolls over to 0 when the count exceeds 255.) The variable (any variable) is local to the process in which it is defined. We update the value of count by an IF statement, with the form:

IF (condition) THEN

Statement[s];

[ELSIF (condition) THEN

statement[s];]

[ELSE statement[s];]

END IF;

The clause (IF (clear=‘0’) THEN) monitors the asynchronous clear function independently of the clock and executes the variable assignment that sets the output to 0 if the Boolean condition (clear=‘0’) is true. Otherwise, the clock is monitored for a positive edge by the condition (clk’EVENT AND clk = ‘1’). The clause clk’EVENT (pronounced “clock tick event”) is a predefined attribute of the clock signal and is true if there has just been a change on clock. The combination of this and the condition clk = ‘1’ indicates that a positive edge has just occurred. If this is true, the count is incremented.

As a final step, the accumulated count must be assigned to an output port. This is done in the concurrent signal assignment q <= count at the end of the process.

Note the difference in types of assignments. A variable is assigned by the : operator (e.g., count := count + 1;). A signal is assigned by the <= operator (eg., q <= count).

LPM Counters in VHDL

We can use a component (lpm_counter) from the Library of Parameterized Modules (LPM) to instantiate a counter in VHDL. When using an LPM counter, we don’t need to describe the behavior of the counter, as this has been done for us in the module itself. All component to the ports of the

the parameters we needlpm_simp.vhd external port or an internal

signal. The VHDL code below shows the VHDL implementation (lpm_simp.vhd) of the same 8-bit counter as in the previous behavioral example.

——lpm_simp.vhd

——Eight-bit binary counter based on a component


9.4 • Programming Binary Counters in VHDL

387

——from the Library of Parameterized Modules (LPM)

——Counter has an active-LOW asynchronous clear.

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

LIBRARY lpm;

USE lpm.lpm_components.ALL;

ENTITY lpm_simp IS

PORT(

clk, clear : IN

STD_LOGIC;

q

: OUT

STD_LOGIC_VECTOR (7 downto 0));

END lpm_simp;

ARCHITECTURE count OF lpm_simp IS

SIGNAL clrn : STD_LOGIC;

BEGIN

count8: lpm_counter

GENERIC MAP (LPM_WIDTH => 8)

PORT MAP ( clock

=> clk,

aclr

=> clrn,

q

=> q(7 downto 0));

clrn <= not clear;

END count;

LPM components require us to use two packages: the std_logic_1164 package in the ieee library to define STD_LOGIC types used in the LPM components and the lpm_components package in the lpm library to define the components themselves. Since LPM components are defined using STD_LOGIC and STD_LOGIC_VECTOR types, we should use these types for our other identifiers as well.

The entity declaration defines the inputs and outputs of our counter and need not correspond to the port names for the LPM counter. That correspondence is defined in the architecture body, where we instantiate the counter module. The counter is defined in a component instantiation statement, which takes 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);

The component name is the name of the LPM component. Parameter names are those defined in the LPM component, such as LPM_WIDTH. Parameter values are those values assigned in the instance of the component. Component ports are the LPM port names. Connect ports are the names of identifiers declared in the entity or as signals or variables.

If we want to invert the active level of an LPM input port, we must use a signal assignment statement. (e.g., clrn <= not clear;) We need to do this because a VHDL input port cannot be “updated” (modified); only an output can be assigned a new value as a result of a Boolean expression. Thus, we create a signal called clrn that maps to the aclr (asynchronous clear) port of the LPM counter. This is connected to the clear input of the counter circuit via an inverter. Figure 9.20 shows the graphic equivalent of this mapping.

SECTION 9.4 REVIEW PROBLEM

9.4Write a VHDL code segment that increments a variable called count upon detection of a negative edge of an input called clock.


388 C H A P T E R 9 • Counters and Shift Registers

FIGURE 9.20

Graphic Equivalent of an LPM Counter with Active-Low Clear

LPM_AVALUE=

LPM_DIRECTION=

LPM_MODULUS=

LPM_SVALUE=

LPM_WIDTH=8

LPM_COUNTER

q[]

OUTPUT

qd[7..0]

INPUT

clock

aclr

NOT

INPUT

clrn

clear

9.5 Control Options for Synchronous Counters

K E Y T E R M S

Parallel load A function that allows simultaneous loading of binary values into all flip-flops of a synchronous circuit. Parallel loading can be synchronous or asynchronous.

Presettable counter A counter with a parallel load function.

Clear Reset (synchronous or asynchronous).

Count enable A control function that allows a counter to progress through its count sequence when active and disables the counter when inactive.

Bidirectional counter A counter that can count up or down, depending on the state of a control input.

Terminal count The last state in a count sequence before the sequence repeats (e.g., 1111 is the terminal count of a 4-bit binary UP counter; 0000 is the terminal count of a 4-bit binary DOWN counter).

Ripple carry out or ripple clock out (RCO) An output that produces one pulse with the same period as the clock upon terminal count.

Synchronous counters can be designed with a number of features other than just straight counting. Some of the most common features include:

Synchronous or asynchronous parallel load, which allows the count to be set to any value whenever a LOAD input is asserted

Synchronous or asynchronous clear (reset), which sets all of the counter outputs to zero

Count enable, which allows the count sequence to progress when asserted and inhibits the count when deasserted

Bidirectional control, which determines whether the counter counts up or down

Output decoding, which activates one or more outputs when detecting particular states on the counter outputs

Ripple carry out or ripple clock out (RCO), a special case of output decoding that produces a pulse upon detecting the terminal count, or last state, of a count sequence.


9.5 • Control Options for Synchronous Counters

389

We will examine the implementation of these functions, first as Graphic Design Files in MAX PLUS II, and then, in the next section, in VHDL, both as behavioral descriptions and as functions of LPM counters.

Parallel Loading

Figure 9.21 shows the symbol of a 4-bit presettable counter (i.e., a counter with a parallel load function). The parallel inputs, P3 to P0, have direct access to the flip-flops of the counter. When the LOAD input is asserted, the values at the P inputs are loaded directly into the counter and appear at the Q outputs.

N O T E

Parallel loading requires at least two sets of inputs: the load data (P3 to P0) and the load command (LOAD). If the load function is synchronous, as described below, it also requires a clock input.

FIGURE 9.21

4-bit Counter with Parallel Load

P3 P2 P1 P0

LOAD

CTR DIV 16

CLOCK Q3 Q2 Q1 Q0

MSB LSB

4b_al_sl.scf

counters have the same clock, load, and P inputs. The count is already in progress at the beginning of the simulation window and shows both counters advancing with each clock pulse: 4, 5, 6.

When LOAD goes HIGH at 500 ns, the value of P[3..0] ( AH) is loaded into the asynchronously loading counter (QA[3..0]) immediately after a short propagation delay (12.5 ns). The counter with synchronous load (QS[3..0]) is not loaded until the next positive clock edge, shown at 560 ns.

FIGURE 9.22

Synchronous vs. Asynchronous Load

Synchronous Load

The logic diagram of Figure 9.23 shows the concept of synchronous parallel load. Depending on the status of the LOAD input, the flip-flop will either count according to its