ceil(log2())を求める函数を作る / パッケージ内に

pp.355-356
パッケージ内に函数を作る。

subprograms_pkg.vhd

package subprograms_pkg is
   -- 函数を宣言する。
   -- function 函数名 (引数: 型) return 型;
    function ceil_log2 (input: positive) return natural;
end package;

package body subprograms_pkg is
   -- 函数の中身を記述する。
    function ceil_log2 (input: positive) return natural is
        variable result: natural := 0;
    begin
        while 2**result < input loop
            result := result + 1;
        end loop;
        return result;
    end function;
end package body;
-- 作ったパッケージを利用する。
-- use work.パッケージ名.all;
use work.subprograms_pkg.all;

entity test_circuit is
    generic(
        BITS: natural := 8
    );
    port(
        inp : in  positive range 1 to 2**BITS-1;
        outp: out natural  range 0 to BITS
    );
end entity;

architecture rtl of test_circuit is
begin
    outp <= ceil_log2(inp);
end architecture;

今ceil(log2(0x85)) = ceil(log2(133)) = 8 = 0b1000を計算している。
f:id:ti-nspire:20210426093551j:plain