当 std_logic_vector 的大小可参数化时,如何计算其所需的位数?
How to calculate the number of bits needed for an std_logic_vector when its size is parametrable?
我有一个实例化深度为通用的 FIFO 的实体:
DEPTH_FIFO : natural range 2 to 64 := 3; -- number of vectors in the FIFO
我必须声明一个可以存储 FIFO 索引的计数器,但我需要知道计数器的大小。
signal cnt_FIFO : unsigned(length_cnt_FIFO-1 downto 0);
我的问题是找到一种方法来计算常量length_cnt_FIFO。
我已经试过了:
constant length_cnt_FIFO : natural := CEIL(LOG(2, DEPTH_FIFO));
与图书馆 use ieee.MATH_REAL.all;
但是我遇到了类型转换的问题。
任何人有使这项工作或任何其他解决方案可行的想法吗?
提前致谢,
SLP
- 你的
DEPTH_FIFO
是自然数,但你必须将它转换成实数才能传递给对数函数。
- CEIL 函数 returns 实数:您必须将其转换回自然数以将其存储在自然数类型的常量中。
- LOG 函数的参数是实数,因此传递文字 2 会导致问题,因为它是作为整数处理的。使用例如
2.0
或 LOG2 函数。
这对我有用:
constant length_cnt_FIFO : natural := natural(CEIL(LOG2(real(DEPTH_FIFO))));
我在手边的实用程序包中定义了以下函数:
function ilog2(val : integer) return integer
is
constant vec : std_logic_vector(31 downto 0) :=
std_logic_vector(to_unsigned(val, 32));
begin
for i in vec'left downto vec'right loop
if (vec(i) = '1') then
return i;
end if;
end loop;
return -1; -- The number is '0'.
end function ilog2;
我有一个实例化深度为通用的 FIFO 的实体:
DEPTH_FIFO : natural range 2 to 64 := 3; -- number of vectors in the FIFO
我必须声明一个可以存储 FIFO 索引的计数器,但我需要知道计数器的大小。
signal cnt_FIFO : unsigned(length_cnt_FIFO-1 downto 0);
我的问题是找到一种方法来计算常量length_cnt_FIFO。
我已经试过了:
constant length_cnt_FIFO : natural := CEIL(LOG(2, DEPTH_FIFO));
与图书馆 use ieee.MATH_REAL.all;
但是我遇到了类型转换的问题。
任何人有使这项工作或任何其他解决方案可行的想法吗?
提前致谢,
SLP
- 你的
DEPTH_FIFO
是自然数,但你必须将它转换成实数才能传递给对数函数。 - CEIL 函数 returns 实数:您必须将其转换回自然数以将其存储在自然数类型的常量中。
- LOG 函数的参数是实数,因此传递文字 2 会导致问题,因为它是作为整数处理的。使用例如
2.0
或 LOG2 函数。
这对我有用:
constant length_cnt_FIFO : natural := natural(CEIL(LOG2(real(DEPTH_FIFO))));
我在手边的实用程序包中定义了以下函数:
function ilog2(val : integer) return integer
is
constant vec : std_logic_vector(31 downto 0) :=
std_logic_vector(to_unsigned(val, 32));
begin
for i in vec'left downto vec'right loop
if (vec(i) = '1') then
return i;
end if;
end loop;
return -1; -- The number is '0'.
end function ilog2;