我有以下内容(未合成):
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
library work;
use work.bus_pkg;
entity gpio_controller is
generic (
gpio_count : natural range 1 to natural'high
);
port (
clk : in std_logic;
gpio : inout std_logic_vector(gpio_count - 1 downto 0)
);
end entity;
architecture behavioral of gpio_controller is
type inout_type is (
DISABLED,
OUTPUT,
WEAK_PULLUP,
WEAK_PULLDOWN);
constant inout_type_bitlength : natural := integer(ceil(log2(real(inout_type'high))));
begin
gpio <= (others => 'Z');
end architecture;
我想要的是确定
inout_type_bitlength
,枚举inout_type所需的最小位数,在我的例子中应该是2(因为枚举4个状态需要2位)。
现在我选择的方法不起作用,但是有没有办法让它起作用?
额外的问题,我可以以某种方式创建一些东西,自动将整数 0 到 n 映射到一个状态,无论有多少个状态?
您可以使用 POS 属性将类型值转换为 int。因此,我们得到:
inout_type'pos(inout_type'high)
获取类型中最高元素的整数值。放在一起:
constant inout_type_bitlength : natural := integer(ceil(log2(real(inout_type'pos(inout_type'high)))));