pp.298-301
前回と同じことを今度はvariable
を使って実行してみる。
process
内で宣言する。- 常にローカル変数である。
process
の外へは出られない。外へ出すときはsignal
を経由する。- 割り当てには
:=
を使う。 - 値は即座に更新されるため、更新後の値がすぐ次の行で利用できる。
2つあるif
ブロックの順番を前回と逆にした。
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity counter_with_variable 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_variable is begin process(clk) variable i: natural range 0 to MAX; begin if rising_edge(clk) then if i=MAX-1 then is_max <= '1'; else is_max <= '0'; end if; if i /= MAX then i := i + 1; else i := 0; end if; end if; count <= std_logic_vector(to_unsigned(i, BITS)); end process; end architecture;
動作は前回と同じ。