首页 > 解决方案 > 如何在 vhdl 中使用“+”操作?

问题描述

我写了一些代码,其中包含一个执行加法运算的过程。我使用“+”符号,编译器无法识别它。我知道 vhdl 不支持这个符号,但是我们的教授要求我们在代码中使用它。有什么方法可以使用“+”而不会出错?

我使用了所有我知道的库,但没有结果。这是我的代码:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
use ieee.std_logic_signed.all;
use ieee.std_logic_arith.all;

package arith_operation is
  constant size: integer :=8;
  constant select_bits: integer :=3;
  procedure add( x,y: in bit_vector(size-1 downto 0);
                 sum: out bit_vector(size-1 downto 0); c_out: out bit );
end arith_operation;

package body arith_operation is
  procedure add
         ( x,y: in bit_vector(size-1 downto 0);
           sum: out bit_vector(size-1 downto 0);
           c_out: out bit) is
  variable s: bit_vector(size downto 0);
  begin
    s:= ("0" & x) + ("0" & y);
    sum:= s(size-1 downto 0);
    c_out:= s(size);
  end procedure add;
end arith_operation;

这是出现的错误:

Error (10327): VHDL error at arith_operation.vhd(22): can't determine definition of operator ""+"" -- found 0 possible definitions

标签: compiler-errorsvhdlquartus

解决方案


这是因为您的代码没有包含任何使用位向量执行算术的包。在 VHDL 2008 之前,还没有用于此的软件包。VHDL 2008 引入了 numeric_bit_unsigned 以允许使用 bit_vectors 进行无符号运算,以及使用 bit 定义无符号和有符号类型的 numeric_bit。(这与使用 std_logic 作为基本元素类型定义的相同类型的 numeric_std 形成对比)。

您还有冲突和非标准库的问题。非标准的 std_logic_arith 应该被删除,因为它与 numierc_std 冲突。std_logic_unsigned 和 std_logic_signed 都是非标准的,并且使用 std_logic_vectors 执行算术,并且还定义了相同的函数。理想情况下,您不会使用任何一个库,因为它们是非标准 VHDL,但如果您必须使用它们,使用它们的最简单方法是只包含一个或另一个,而不是两者。(您可以同时使用两者,但是您需要使用具有完整路径的函数)。您可以改为使用 VHDL 2008 中添加的 numeric_std_unsigned 用于 SLV 的无符号算术。


推荐阅读