【问题标题】:VHDL: Converting from floating point to fixed point explanation?VHDL:从浮点转换为定点解释?
【发布时间】:2011-02-03 03:48:42
【问题描述】:

在第 6.2 章的 VHDL 设计指南中,有一个用于从浮点到定点表示的转换器的实体和架构主体。我对此感到困惑

library ieee; use ieee.std_logic_1164 all;
entity to_fp is
   port(vec: in std_u_logic_vector(15 downto 0);
        r: out real);
end entity to_fp;

architecture behavioral of to_fp is
begin 
    behavior : process (vec) is
       variable temp: bit_vector(vec'range);
       variable negative: boolean;
       variable int_result: integer;
    begin
       temp := to_bitvector(vec);
       negative := temp(temp'left) = '1';
       if negative then
          temp := not temp;
       end if;
       int_result := 0;
       for index in vec'range loop
           int_result := int_result*2 + bit'pos(temp(index));
       end loop;
       if negative then
          int_result := (-int_result) -1;
       end if;
       r <= real(int_result) / 2.0**15;
    end process behavior;
end architecture behavioral;

大部分我都明白。我只是不明白 for 循环。这如何为我们提供位向量的整数表示?请尽可能详细地解释,谢谢:)。

【问题讨论】:

  • 请不要将问题的第一行用作标题行。
  • 这不是浮点数。没有指数场。这个过程所做的就是获取一个位向量,将其解释为一个整数,然后除以 2^15。基本上,它是一个 1.15 定点到实际转换器,但设计过度。另请参阅 Hendrik 的回答。

标签: vhdl


【解决方案1】:
for index in vec'range loop

这会在vec 的范围内循环。在这种情况下(从 15 到 0)。

bit'pos(temp(index));

bit 是一个枚举类型(std.standard 中的type BIT is ('0', '1');)。 pos 属性返回给定值的位置编号(作为整数类型)。所以bit'pos(...) 将位转换为整数。

所以循环的作用是将 bit_vector 转换为整数

不过,我建议为此使用to_integer(unsigned(vec))。记得use ieee.numeric_std.all;

最后一行将整数转换(强制转换)为real

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多