【问题标题】:VHDL: How to convert an iterator of generate block to std_logic_vector?VHDL:如何将生成块的迭代器转换为 std_logic_vector?
【发布时间】:2017-12-05 14:00:00
【问题描述】:

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

例如,

0 -> 0000

1 -> 0001

2 -> 0010

...

15 -> 1111

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

我尝试使用两种方法,但它们都不起作用: address 和 hit 都是 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 的方法?

【问题讨论】:

    标签: vhdl


    【解决方案1】:

    首先,不要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 信号上具有多个驱动程序,这在符合综合条件的设计中是不允许的。

    似乎目标是根据hit 向量中的最低设置 ('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 类型,并保存一个类型转换。

    【讨论】:

    • 如果您使用的是 VHDL 2008 或更高版本,您甚至可以使用 u_unsigned(未解决)而不是 unsigned(已解决)。这样,您将在多个驱动器情况下收到错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2020-05-18
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多