最大値到達フラグつきカウンター

pp.289-291

=は代入ではなくequal。
/=は割って代入ではなくnot equal。

クロックエッジの検出については下のどちらでも同じことだが、event属性で直接検出するよりもrising_edge()函数falling_edge()函数のほうが望ましいとの由。

  • if clk'event and clk='1'
  • if rising_edge(clk)
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity counter_with_is_max_flag is
    generic(
        MAX : natural := 6;
        BITS: natural := 3 -- ceil(log2(6))
    );
    port(
        clk   : in  std_logic;
        count : out std_logic_vector(BITS-1 downto 0);
        is_max: out std_logic
    );
end entity;

architecture rtl of counter_with_is_max_flag is
    signal i: natural range 0 to MAX; -- これをカウント値として使う。
begin
    process(clk)
    begin
        if rising_edge(clk) then

            if i /= MAX then -- カウント値が最大値に達していなかったらこのタイミングで
                i <= i + 1;  -- カウント値をインクリメントするが、
            else             -- カウント値が最大値に達していたらこのタイミングで
                i <= 0;      -- カウント値をリセットする。
            end if;

            if i=MAX-1 then    -- カウント値が最大値の1つ手前に達していたらこのタイミングで
                is_max <= '1'; -- フラグを立てるが、
            else               -- カウント値が最大値の1つ手前に達していなかったらこのタイミングで
                is_max <= '0'; -- フラグを立てない。
            end if;

        end if;
    end process;
    count <= std_logic_vector(to_unsigned(i, BITS));
end architecture;

f:id:ti-nspire:20210419091425p:plain
f:id:ti-nspire:20210419091846j:plain:w500