VHDL:从浮点转换为定点解释?

JC2*_*JC2 1 vhdl

在第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;
Run Code Online (Sandbox Code Playgroud)

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

Hen*_*rik 9

for index in vec'range loop
Run Code Online (Sandbox Code Playgroud)

这循环超出范围vec.在这种情况下(15 downto 0).

bit'pos(temp(index));
Run Code Online (Sandbox Code Playgroud)

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

那么循环的作用是将bit_vector转换为整数.

不过,我建议to_integer(unsigned(vec))用于此目的.记住use ieee.numeric_std.all;.

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