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

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

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

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

Добавлен: 13.06.2025

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

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

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

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

——srg4behv.vhd

——Behavioral description of a 4-bit serial shift register LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

srg4behv.vhd

srg4behv.scf

ENTITY srg4behv IS

PORT (

serial_in, clk : IN

STD_LOGIC;

q

: BUFFER

STD_LOGIC_VECTOR(3 downto 0) );

END srg4behv;

ARCHITECTURE right_shift of srg4behv IS

BEGIN

PROCESS (clk)

BEGIN

IF (clk’EVENT and clk = ‘1’) THEN q <= serial_in & q(3 downto 1);

END IF;

END PROCESS;

END right_shift;

In the behavioral design, we are not concerned with the flip-flop inputs or other internal connections; the behavioral description is sufficient for the VHDL compiler to synthesize the required hardware. Compare this to the dataflow description, where we created a set of flip-flops, then assigned Boolean functions to the D inputs. In this case, the behavioral design method combines these two steps into one.

9.15Write the code for a VHDL design entity that implements a 4-bit bidirectional shift register with asynchronous clear. Create a simulation that verifies the design function.

Solution The VHDL code for the bidirectional shift register, srg4bidi.vhd, follows. A CASE statement monitors the directional control of the shift register. We require the others clause of the CASE statement since the identifier direction is of type STD_LOGIC; the cases ‘0’ and ‘1’ do not cover all possible values of STD_LOGIC. Since we want no action to be taken in the default case, we use the keyword NULL.

.electronictech.com

srg4bidi.vhd

srg4bidi.scf

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY srg4bidi IS

PORT (

clk, clear

: IN STD_LOGIC;

rsi, lsi

: IN STD_LOGIC;

direction

: IN STD_LOGIC;

q

: BUFFER STD_LOGIC_VECTOR(3 downto 0) );

END srg4bidi;

ARCHITECTURE bidirectional_shift of srg4bidi IS BEGIN

PROCESS (clk, clear)

BEGIN

IF clear = ‘0’ THEN

q <= “0000”; —— asynchronous clear ELSIF (clk‘EVENT and clk = ‘1’) THEN

CASE direction IS

WHEN ‘0’ =>

q <= q(2 downto 0) & lsi; —— left shift


9.8 • Programming Shift Registers in VHDL

431

WHEN ‘1’ =>

q <= rsi & q(3 downto 1); —— right shift WHEN others =>

NULL;

END CASE;

END IF;

END PROCESS;

END bidirectional_shift;

Figure 9.73 shows the simulation of the shift register, with the left shift function in the first half of the simulation and the right shift function in the second half.

FIGURE 9.73

Example 9.15

4-bit Bidirectional Shift Register

Shift Registers of Generic Width

K E Y T E R M

GENERIC A clause in the entity declaration of a VHDL component that lists the

parameters that can be specified when the component is instantiated.

All multibit VHDL components we have examined until now have been of a specified width (e.g., 2-to-4 decoder, 8-bit MUX, 8-bit adder, 4-bit counter). VHDL allows us to create components having a generic, or unspecified, width or other parameter which is specified when the component is instantiated. In the entity declaration of such a component, we indicate an unspecified parameter (such as width) in a GENERIC clause. The unspecified parameter must be given a default value in the GENERIC clause, indicated by : value.

When we instantiate the component, we specify the parameter value in a generic map, as we have done with components from the Library of Parameterized Modules. The design entity srt_bhv.vhd below behaviorally defines an n-bit right-shift register, with a default width of four bits given by the statement ( GENERIC (width : POSITIVE := 4);).

The entity srt8_bhv.vhd instantiates the n-bit register as an 8-bit circuit by specifying the bit width in a generic map. If no value is specified, the component is presumed to have a default width of four, as defined in the component’s entity declaration.

—— srt_bhv.vhd

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

——Behavioral description of an n-bit shift register LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

srt_bhv.vhd srt8_bhv.vhd srt8_bhv.scf

ENTITY srt_bhv IS

GENERIC (width : POSITIVE := 4);

PORT (

serial_in, clk : IN

STD_LOGIC;

q

: BUFFER

STD_LOGIC_VECTOR(width-1 downto 0) );

END srt_bhv;

of srt_bhv IS

PROCESS (clk)

BEGIN

IF (clk‘EVENT and clk = ‘1’) THEN

q(width-1 downto 0) <= serial_in & q(width-1 downto 1);

END IF;

END PROCESS;

END right_shift;

——srt8_bhv.vhd

——8-bit shift register that instantiates srt_bhv LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY srt8_bhv IS

PORT(

data_in, clock : IN

STD_LOGIC;

qo

: BUFFER

STD_LOGIC_VECTOR(7 downto 0) );

END srt8_bhv;

ARCHITECTURE right_shift of srt8_bhv IS

COMPONENT srt_bhv

GENERIC (width : POSITIVE);

PORT (

serial_in, clk

: IN

STD_LOGIC;

q

: OUT

STD_LOGIC_VECTOR(7 downto 0) );

END COMPONENT;

BEGIN

Shift_right_8: srt_bhv

GENERIC MAP (width=> 8)

PORT MAP (serial_in =>

data_in,

clk

=>

clock,

q

=>

qo);

END right_shift;

9.16Write the code for a VHDL design entity that defines a universal shift register with a generic width. (The default width is eight bits.) Instantiate this entity as a component in a file for a 16-bit universal shift register.

Solution

——srg_univ.vhd

——Universal shift register with generic width

——Default width = 8 bits


srg_univ.vhd srg16uni.vhd

9.8 • Programming Shift Registers in VHDL

433

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

USE ieee.std_logic_arith.ALL;

ENTITY srg_univ IS

GENERIC (width : POSITIVE := 8);

PORT (

clk, clear

: IN STD_LOGIC;

rsi, lsi

: IN STD_LOGIC;

function_select

: IN STD_LOGIC_VECTOR(1 downto 0);

p

: IN STD_LOGIC_VECTOR(width-1 downto 0);

q

: BUFFER STD_LOGIC_VECTOR(width-1 downto 0) );

END srg_univ;

univ IS

PROCESS (clk, clear)

BEGIN

IF clear = ‘0’ THEN

--Conversion function to convert integer 0 to vector

--of any width. Requires ieee.std_logic_arith package. q <= CONV_STD_LOGIC_VECTOR(0, width);

ELSIF (clk’EVENT and clk = ‘1’) THEN

CASE function_select IS

WHEN “00” =>

q <= q; —— Hold WHEN “01” =>

q <= rsi & q(width-1 downto 1); -- Shift right WHEN “10” =>

q <= q(width-2 downto 0) & lsi; -- Shift left WHEN “11” =>

q <= p; —— Load WHEN OTHERS =>

NULL;

END CASE;

END IF;

END PROCESS;

END universal_shift;

——srg16uni.vhd

——16-bit universal shift register (instantiates srg_univ) LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY srg16uni IS

PORT (

clock, clr

: IN

STD_LOGIC;

rsi, lsi

: IN

STD_LOGIC;

s

: IN

STD_LOGIC_VECTOR(1 downto 0);

parallel_in : IN

STD_LOGIC_VECTOR(15 downto 0);

qo

: BUFFER

STD_LOGIC_VECTOR(15 downto 0) );

END srg16uni;

ARCHITECTURE universal_shift of srg16uni IS

COMPONENT srg_univ

GENERIC (width : POSITIVE);

PORT (

clk, clear

: IN STD_LOGIC;


434

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

rsi, lsi

: IN STD_LOGIC;

function_select

: IN STD_LOGIC_VECTOR(1 downto 0);

p

: IN STD_LOGIC_VECTOR(width-1 downto 0);

q

: BUFFER STD_LOGIC_VECTOR(width-1 downto 0));

END COMPONENT;

BEGIN

Shift_universal_16: srg_univ

GENERIC MAP (width=> 16)

PORT MAP (clk

=> clock,

clear

=>

clr,

rsi

=>

rsi,

lsi

=>

lsi,

function_select =>

s,

p

=>

parallel_in,

q

=>

qo);

END universal_shift;

When we are designing the clear function in srg_univ.vhd, we must account for the fact that we must set all bits of a vector of unknown width to ‘0’. To get around this problem, we use a conversion function that changes an INTEGER value of 0 to a STD_LOGIC_VECTOR of width bits and assigns the value to the output. The required conversion function, CONV_STD_LOGIC_VECTOR(value, number_of_bits), is found in the std_logic_arith package in the ieee library. We could also use the construct

q <= (others => ‘0’);

which states that the default case is to set all bits of q to 0 when clear is 0. Since there is no other case specified, all bits of q are cleared.

LPM Shift Registers

The Library of Parameterized Modules contains a shift register component, lpm_shiftreg, that we can instantiate in a VHDL design entity. The various functions of lpm_shiftreg are listed in Table 9.16.

The following VHDL code instantiates lpm_shiftreg as an 8-bit shift register with serial input and serial output. In this case, the LPM component is declared explicitly, with the component declaration statement listing only the ports and parameters used by the design entity. The component instantiation statement lists the port names from the design entity in the same order as the corresponding component port names. By default the register direction is LEFT (i.e., toward the MSB).

——srg8_lpm.vhd

——8-bit serial shift register (shift left by default) LIBRARY ieee;

USE ieee.std_logic_1164.ALL; LIBRARY lpm;

USE lpm.lpm_components.ALL;