【问题标题】:Illegal type conversion VHDL非法类型转换 VHDL
【发布时间】:2014-06-20 01:05:59
【问题描述】:

我试图通过 vhdl 中的类型转换返回类型 std_logic_vector。 这是我的代码:

 function mul(num1,num2 : in std_logic_vector(7 DOWNTO 0)) return std_logic_vector is
    variable v_TEST_VARIABLE1 : integer;
    variable v_TEST_VARIABLE2 : integer;
    variable n_times: integer:=1;
    variable product: integer:=0;
    begin 
      for n_times in 1 to v_TEST_VARIABLE2 loop
        product:=product + v_TEST_VARIABLE1;
      end loop;
    return std_logic_vector(product);
  end mul;

它在编译时给出"Illegal type conversion from std.standard.integer to ieee.std_logic_1164.std_logic_vector (numeric to array)."。 如何在这样的代码中返回 std_logic_vector?

【问题讨论】:

    标签: type-conversion vhdl


    【解决方案1】:

    请先查看 Russell 的帖子。如果你使用 VHDL-2008, numeric_std_unsigned 包,那么你可以只使用一次转换:

    use ieee.numeric_std_unsigned.all ; 
    ...
    return to_std_logic_vector(product, length) ;  -- long form
    
    -- alternate short form
    return to_slv(product, length) ; 
    

    使用警告: 对于综合,我认为 VHDL-2008 项目处于支持的前沿。因此,虽然我在我的测试平台中经常使用 VHDL-2008,但我尝试将它在我的 RTL 代码中的使用限制为使用其他方法无法完成的事情。但是,如果您想使用这样的代码,重要的是在您的综合工具中试用它并在它不起作用时提交错误报告 - 这是改变发生的唯一方式。

    【讨论】:

      【解决方案2】:

      您必须先将其转换为无符号。我希望你使用 numeric_std?如果是这样,您的代码如下所示。请注意,要使用 to_unsigned,您必须知道输出 std_logic_vector 的长度。所以这要么需要成为你的函数的输入,要么你可以绑定返回向量:

      return std_logic_vector(to_unsigned(product, length));
      

      更多关于如何convert std_logic_vector to integer的信息。

      【讨论】:

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