シフトレジスタを作る

pp.311-312

4ビット幅、4段のシフトレジスタを作る。クロックのたびに入力値が次段へ順送りされる。

library ieee;
use ieee.std_logic_1164.all;

entity shift_register is
    generic(
        NUM_BITS  : natural := 4;
        NUM_STAGES: natural := 4
    );
    port(
        clk : in  std_logic;
        din : in  std_logic_vector(NUM_BITS-1 downto 0);
        dout: out std_logic_vector(NUM_BITS-1 downto 0)
    );
end entity;

architecture rtl of shift_register is
    type slv_array is array (0 to NUM_STAGES-1) of std_logic_vector(NUM_BITS-1 downto 0);
    signal q: slv_array;
begin
    process(clk)
    begin
       --if rising_edge(clk) then
        if falling_edge(clk) then
            q <= din & q(0 to NUM_STAGES-2); -- 最後尾のレジスタの値を捨て、dinを最前列に追加する。
        end if;
        dout <= q(NUM_STAGES-1); -- 最後尾のレジスタの値を出力する
    end process;
end architecture;

f:id:ti-nspire:20210421134706j:plain:w500
RTLビュー:
f:id:ti-nspire:20210421134411p:plain