【问题标题】:Error adding std_logic_vectors添加 std_logic_vectors 时出错
【发布时间】:2011-05-01 20:47:31
【问题描述】:

我想要一个添加两个 std_logic_vector 的简单模块。但是,在使用代码时 下面的 + 运算符不会合成。

library IEEE; 
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;

entity add_module is
        port(
  pr_in1   : in std_logic_vector(31 downto 0);
  pr_in2   : in std_logic_vector(31 downto 0);
  pr_out   : out std_logic_vector(31 downto 0)  
        );
end add_module;

architecture Behavior of add_module is

begin

    pr_out <= pr_in1 + pr_in2;

end architecture Behavior;

我从 XST 得到的错误信息

第 17 行。+ 在此上下文中不能有这样的操作数。

我想念图书馆吗?如果可能,我不想将输入转换为自然数。

非常感谢

【问题讨论】:

    标签: vhdl fpga


    【解决方案1】:

    解决此错误的简单方法是:
    添加unsign库,
    之后,您的代码开始工作。

    使用

    ieee.std_logic_unsigned.all;
    pr_out <= pr_in1 + pr_in2;
    

    【讨论】:

    • 正如@Martin Thompson 所说,不推荐使用这个库。
    • 你能解释一下吗?
    • 一切都在 Martin Thompson 答案的链接中。 std_logic_arith``std_logic_unsignedstd_logic_signed 是由 synopsis 开发的非标准库。 numeric_std 是标准库。
    • 另外,我在主题上的 2 美分:使用 numeric_std 可以防止在代码编写过程中出现几个简单的错误,因为在处理数字时使用 signedunsigned 类型而不是通用 std_logic_vector
    【解决方案2】:

    不要使用std_logic_arith - 我有written about this(有点长:)。

    不要使用 numeric_std - 并在您的实体端口上使用正确的类型。如果您正在做算术,请使用数字类型(整数或(无)符号向量,视情况而定)。它们会很好地合成。

    std_logic_vectors 适合

    • 当您不关心数值时(一组控制位、一些随机数据位)
    • 当您不知道输入的类型时(例如,可以根据控制标志对有符号和无符号数字进行运算的加法器)。

    【讨论】:

    • 我建议查看 Martin 的链接。
    【解决方案3】:

    来自@Aurelien 的关于使用 numeric_std 的好建议。

    请记住,将两个 32 位值相加可能会产生一个 33 位值,并决定您要如何处理溢出。

    【讨论】:

      【解决方案4】:

      您希望编译器如何知道您的 std_logic_vectors 是有符号还是无符号?这两种情况下加法器的实现是不一样的,所以你需要明确地告诉编译器你想让它做什么;-)

      注意:StackOverflow 中的 VHDL 语法高亮显示很糟糕。将此代码复制/粘贴到您首选的 VHDL 编辑器中,以便更轻松地阅读。

      library IEEE; 
      use IEEE.std_logic_1164.all;
      -- use IEEE.std_logic_arith.all; -- don't use this
      use IEEE.numeric_std.all; -- use that, it's a better coding guideline
      
      -- Also, never ever use IEEE.std_unsigned.all or IEEE.std_signed.all, these
      -- are the worst libraries ever. They automatically cast all your vectors
      -- to signed or unsigned. Talk about maintainability and strong typed language...
      
      entity add_module is
        port(
          pr_in1   : in std_logic_vector(31 downto 0);
          pr_in2   : in std_logic_vector(31 downto 0);
          pr_out   : out std_logic_vector(31 downto 0)  
        );
      end add_module;
      
      architecture Behavior of add_module is
      begin
      
        -- Here, you first need to cast your input vectors to signed or unsigned 
        -- (according to your needs). Then, you will be allowed to add them.
        -- The result will be a signed or unsigned vector, so you won't be able
        -- to assign it directly to your output vector. You first need to cast
        -- the result to std_logic_vector.
      
        -- This is the safest and best way to do a computation in VHDL.
      
        pr_out <= std_logic_vector(unsigned(pr_in1) + unsigned(pr_in2));
      
      end architecture Behavior;
      

      【讨论】:

        猜你喜欢
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-12
        • 2016-10-09
        • 2014-02-12
        • 2016-09-11
        相关资源
        最近更新 更多