VHDLで10進同期カウンターを作る

pp.121-122
今度は、同期リセットつきの10進同期カウンターを作る。0 → 9のカウントアップを繰り返すカウンターである。

これだと、何進カウンターであろうと自由自在に作れることになる。

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;

entity count10 is
    port(
        CLK, RST : in  std_logic;
        COUNT    : out std_logic_vector(3 downto 0)
    );
end count10;


architecture RTL of count10 is

-- <=の右辺に出力は置けないため、カウント値を保持するための一種の変数として内部信号を宣言しておく。
signal COUNT_TMP : std_logic_vector(3 downto 0);

begin
    process(CLK)
    begin

   -- CLKのrising edgeが検出されたら、
        if(CLK'event and CLK = '1') then
           -- そのときリセット信号がHであったら、
            if(RST ='1') then
               -- カウント値を0にリセットして、
                COUNT_TMP <= x"0";

           -- そのとき、カウントすべき最大値(ここでは9)にすでに達していたら、
            elsif(COUNT_TMP = x"9") then -- else ifではなくelsif
               -- 0にロールオーバーして、
                COUNT_TMP <= x"0";

           -- そのいずれでもなかったら、
            else
               -- カウントアップする。
                COUNT_TMP <= COUNT_TMP + 1;
            end if;
        end if;
        
    end process;
    
   -- 現在のカウント値を出力する。
   -- <=の右辺に出力は置けないため内部信号を出力に接続する。
    COUNT <= COUNT_TMP;
end RTL;

9 (0b1001)までカウントアップしたあと0に戻る、を繰り返す。


f:id:ti-nspire:20201224060510p:plain:w533