在 vhdl 中将固定转换为 std_logic_vector 的正确方法是什么?

two*_*two 2 vhdl

我正在尝试将 sfixed(从 ieee.fixed_pkg)转换为 std_logic_vector,我想知道正确的语法是什么以及为什么以下是(显然是错误的)。我尝试编译以下 3 种架构:

library ieee;
    use ieee.std_logic_1164.all;
    use ieee.fixed_pkg.all;

entity test is
    port (input: in sfixed(0 downto -7) := x"00";
    output: out std_logic_vector(7 downto 0) := x"00");
end;    
Run Code Online (Sandbox Code Playgroud)

架构一:

architecture a of test is begin 
    output <= std_logic_vector(input);
end;
Run Code Online (Sandbox Code Playgroud)

架构b:

architecture b of test is begin 
    proc: process (input) begin 
        output <= std_logic_vector(input);
    end process;
end;
Run Code Online (Sandbox Code Playgroud)

架构 c:

architecture c of test is begin 
    proc: process (input) begin 
        if ('1' and '1') then
            output <= std_logic_vector(input);
        end if;
    end process;
end;
Run Code Online (Sandbox Code Playgroud)

我使用的编译器是“ModelSim ALTERA vcom 10.3d Compiler 2014.10 Oct 7 2014”。架构 a 和 b 不编译并显示错误消息:

Error: [...] Index value -7 (of type std.STANDARD.NATURAL) is out of range 0 to 2147483647.
Run Code Online (Sandbox Code Playgroud)

但是架构 c 编译,同时仍然给我警告信息:

Warning: [...] Index value -7 (of type std.STANDARD.NATURAL) is out of range 0 to 2147483647.
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:投射这个的正确方法是什么,为什么上面发布的三种架构之间有什么区别?

Jim*_*wis 5

@BrianDrmmond 讨论的将具有负索引的 sfixed 类型转换为 std_logic_vector 所导致的范围问题是在标准开发过程中发现的一个问题。对于 GHDL 以外的模拟器来说,这也是一个真正的问题。

因此,该包提供了类型转换函数来处理这个问题。要从 sfixed 或 ufixed 转换为 std_logic_vector,请使用 to_slv 和 to_std_logic_vector:

  output <= to_slv(input);
Run Code Online (Sandbox Code Playgroud)

要将 std_logic_vector 转换为 sfixed / ufixed,请使用 to_sfixed/to_ufixed 的其中一种风格。有一个将索引作为参数,另一个将对象作为参数。

  signal a_sfixed : sfixed(0 downto -7) := x"00";
  signal a_slv    : std_logic_vector(7 downto 0) := x"00";

  a_sfixed <= to_sfixed(a_slv, 0, -7);
  . . . 
  a_sfixed <= to_sfixed(a_slv, a_sfixed);
Run Code Online (Sandbox Code Playgroud)

是的,您可以使用类型转换(也称为强制转换)进行赋值而不是上述,但是,如果您想在表达式中使用转换后的值,结果的范围将是不正确的,因为它是由范围决定的的输入。

  signal a_sfixed : sfixed(0 downto -7) := x"00";
  signal a_slv    : std_logic_vector(7 downto 0) := x"00";
  signal y_sfixed : sfixed(1 downto -7) := x"00";

  y_sfixed <= a_sfixed + to_sfixed(a_slv, 0, -7);
Run Code Online (Sandbox Code Playgroud)