VHDL:如何将生成块的迭代器转换为 std_logic_vector?

VHDL: How to convert an iterator of generate block to std_logic_vector?

我想从生成器中的整数中获取二进制 std_logic_vector。

例如,

0 -> 0000

1 -> 0001

2 -> 0010

...

15 -> 1111

我可以为每个整数写 16 个 if-else 语句,但我不喜欢这个主意。 (如果我有超过 16 个整数怎么办?)

我试过两种方法,但都不起作用: 地址和命中都是std_logic_vector

G_1 : for i in 0 to 15 generate
    address <= conv_std_logic_vector(i, 4) when hit(i) = '1';
end generate G_1;   

G_2 : for i in 0 to 15 generate
    address <= std_logic_vector(to_unsigned(i, 4)) when hit(i) = '1';
end generate G_2;

我还注意到,如果我使用数字而不是 i,它会起作用。 (示例:当我使用 conv_std_logic_vector(5, 4) 时得到“0101”)

我做错了什么?有什么方法可以使用 vhdl 来实现吗?

首先,不要use ieee.std_logic_arith.all。相反,use ieee.numeric_std.all,并摆脱任何讨厌的 conv_integer 功能;首选样式是使用像 unsigned 这样的类型,然后像在第二个代码示例中那样转换或转换这些类型。

继续你的循环,你正在使用 generate 循环:

G_1 : for i in 0 to 15 generate
  address <= conv_std_logic_vector(i, 4) when hit(i) = '1';
end generate G_1;

这将生成 16 行的形式:

address <= conv_std_logic_vector(0, 4) when hit(0) = '1';
address <= conv_std_logic_vector(1, 4) when hit(1) = '1';
address <= conv_std_logic_vector(2, 4) when hit(2) = '1';

等由于每个并发分配都推断出自己的过程,因此您的设计将在 address 信号上有多个驱动程序,这在符合综合条件的设计中是不允许的。

好像objective是根据hit向量中最低的set('1')位设置address。这称为优先级编码器。像这样会更好:

process (hit)
begin
  for i in 0 to 15 loop
    address <= (others => '0');  -- Default assignment, any later assignment takes priority
    if (hit(i) = '1') then
      address <= std_logic_vector(to_unsigned(i, address`length));
      exit;
    end if;
  end loop;
end process;

由于 address 似乎代表一个无符号数,您可以使用类型 unsigned 作为此信号,并保存类型转换。