【问题标题】:4-bit ALU using VHDL showing error: no function declarations for operator "+" ("-", "*",and "/")使用 VHDL 的 4 位 ALU 显示错误:运算符“+”(“-”、“*”和“/”)没有函数声明
【发布时间】:2018-09-10 13:39:39
【问题描述】:

当我使用 ghdl 编译这段代码时,它会产生错误。

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;


entity alu is

 generic ( constant N: natural := 1 );     

       port( a,b : in std_logic_vector(3 downto 0);
             sel : in std_logic_vector(3 downto 0);
             y : out std_logic_vector(3 downto 0);
             x: out std_logic_vector(7 downto 0);
             cout : out std_logic);
end alu;


architecture behavioral of alu is
signal rslt : std_logic_vector(3 downto 0);
signal tmp :  std_logic_vector(4 downto 0);
begin


process(a,b,sel)


begin

case sel is

        when "0000"=>

         rslt<= a + b;        -- Line 33
         when "0001"=>
         rslt<= a - b;        -- Line 35
          when "0010"=>
          x<= (unsigned(a)) * (unsigned(b)); -- Line 37
          when "0011"=>
          x<=(unsigned(a)) / (unsigned(b));   -- Line 39
          when "0100"=>
         rslt<=std_logic_vector(unsigned(a) sll N);
          when "0101"=>
         rslt<=std_logic_vector(unsigned(a) srl N);
         when "0110"=>
         rslt<=std_logic_vector(unsigned(a) rol N);
          when "0111"=>
         rslt<=std_logic_vector(unsigned(a) ror N);
          when "1000"=>
         rslt<= a and b;
          when "1001"=>
         rslt<= a or b;
           when "1010"=>
         rslt<= a xor b;
           when "1011"=>
         rslt<= a xnor b;
           when "1100"=>
         rslt<= a nand b;
           when "1101"=>
         rslt<= a nor b;
            when "1110"=>
               if (a > b) then
                     rslt<= "0001";
                else
                    rslt<="0000";
                end if;
          when "1111"=>
               if (a = b)then
                    rslt<="0001";
               else
                    rslt<="0000";
               end if;
          when others=> 
                 rslt<= "0000";
       end case;
   end process;

y<=rslt;
tmp<= ('0' & a) + ('0' & b);     -- Line 78
cout<=tmp(4);
end behavioral;

ghdl -a alu.vhdl
alu.vhdl:33:19:error: 运算符“+”没有函数声明
alu.vhdl:35:19:error: 没有操作符“-”的函数声明
alu.vhdl:37:29:error: 没有操作符“*”的函数声明
alu.vhdl:39:28:error: 运算符“/”没有函数声明
alu.vhdl:78:17:error: no function declarations for operator "+"

在使用无符号算术时,如何使这些运算符可用?

【问题讨论】:

  • 欢迎来到 StackOverflow。我建议更好地格式化您的错误并进一步解释您要达到的目标。无论如何,看看这个post,因为您似乎在理解 VHDL 如何支持向量运算方面遇到了问题。
  • -2008 修订包 numeric_std_unsigned 提供对 std_logic_vector 值的算术运算(被视为无符号,由 delirium 的链接 Synopsys std_logic_unsigned 和 std_logic_arith 提供的功能的超集,既不是标准的一部分)。还有对相关运算符的操作数的类型转换以及分配给 std_logic_vector 的返回值的类型转换。您可以使用无符号类型的端口和中间信号来表示二进制数,而无需更改或添加 use 子句并删除类型转换。
  • 您似乎在不知道代码的作用的情况下复制了代码。我这么说是因为您的代码包含您自己问题的答案。

标签: vhdl alu ghdl


【解决方案1】:

欢迎使用 Stackoverflow。您显然对类型语言不是很熟悉。 VHDL 是一种类型化语言,其中变量、信号、常量具有类型,如bitintegerstd_logic_vector(3 downto 0)unsigned(3 downto 0)。这些类型定义了什么可以做,什么不能做。

  1. 默认情况下,您不能添加两个std_logic_vector(3 downto 0) 并得到同样是std_logic_vector(3 downto 0) 的结果。这就是您尝试对rslt&lt;= a + b; 执行的操作。编译器只是告诉您没有这样的"+" 运算符可见。
  2. rslt&lt;= a - b;"-" 运算符相同。
  3. x&lt;= (unsigned(a)) * (unsigned(b)); 稍微好一点,因为您没有尝试将两个 std_logic_vector(3 downto 0) 相乘。您改为将它们转换为 unsigned(3 downto 0)。不错的选择,因为ieee.numeric_std 包为unsigned(...) 类型重载了"*" 运算符。不幸的是,您尝试将结果分配给std_logic_vector(7 downto 0),而ieee.numeric_std."*" 运算符返回unsigned(7 downto 0)。因此,编译器再次抱怨它没有找到合适的"*" 运算符。注意:括号不是必需的。你可以直接写unsigned(a) * unsigned(b)
  4. 其他错误作为练习没有解释。

我建议您再次阅读您的 VHDL 书籍并了解什么是类型,std_logic_vector(...)unsigned(...) 类型默认定义了哪些类型的操作,以及您的两个包在相同类型上定义了哪些额外操作声明(ieee.std_logic_1164ieee.numeric_std)。

【讨论】:

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