当我使用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:错误:运算符“+”没有函数声明 alu.vhdl:35:19:错误:没有操作符“ - ”的函数声明 alu.vhdl:37:29:错误:运算符“*”没有函数声明 alu.vhdl:39:28:错误:运算符“/”没有函数声明 alu.vhdl:78:17:错误:没有运算符“+”的函数声明
使用无符号算术时,如何使这些运算符可用?
欢迎使用Stackoverflow。你显然对键入的语言不是很熟悉。 VHDL是一种类型语言,其中变量,信号,常量具有类型,如bit
,integer
,std_logic_vector(3 downto 0)
或unsigned(3 downto 0)
。这些类型定义了可以做什么和什么做不到的。
std_logic_vector(3 downto 0)
并得到一个也是std_logic_vector(3 downto 0)
的结果。这是你尝试用rslt<= a + b;
做的。编译器只是告诉你没有这样的"+"
操作符可见。rslt<= a - b;
与"-"
算子相同。x<= (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)
。我建议您再次阅读您的VHDL书籍并了解哪些类型,默认情况下在std_logic_vector(...)
和unsigned(...)
类型上定义了哪种操作,以及您声明的两个包(ieee.std_logic_1164
和ieee.numeric_std
)在相同类型上定义了哪些额外操作。