【发布时间】:2017-10-29 21:43:59
【问题描述】:
我正在用 VHDL 制作一个通用的 N 位 ALU。我无法为加法的进位或减法的借位分配值。我尝试了以下方法:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity alu is
generic(n: integer :=1); --Default to 1
port (
a : in std_logic_vector(n-1 downto 0);
b : in std_logic_vector(n-1 downto 0);
op : in std_logic_vector(1 downto 0);
output : out std_logic_vector(n-1 downto 0);
carryborrow: out std_logic
);
end alu;
architecture Behavioral of alu is
signal result: std_logic_vector(n downto 0);
begin
process( a, b, op )
begin
case op is
when "00" =>
result(n) <= '0';
result(n-1 downto 0) <= a and b; --and gate
output <= result(n-1 downto 0);
carryborrow <= '0';
when "01" =>
result(n) <= '0';
result(n-1 downto 0) <= a or b; --or gate
output <= result(n-1 downto 0);
carryborrow <= '0';
when "10" =>
result(n) <= '0';
result(n-1 downto 0) <= std_logic_vector(signed(a) + signed(b)); --addition
output <= result(n-1 downto 0);
carryborrow <= result(n);
when "11" =>
result(n) <= '0';
result(n-1 downto 0) <= std_logic_vector(signed(a) - signed(b)); --subtraction
output <= result(n-1 downto 0);
carryborrow <= result(n);
when others =>
NULL;
end case;
end process;
end Behavioral;
这似乎将carryborrow 位设置为始终为0。我怎样才能将它分配给它应该是什么而不会出现类型错误?
【问题讨论】:
-
考虑一直阅读The CARRY flag and OVERFLOW flag in binary arithmetic。二进制有符号数 (2'sC) 容易通过符号位进行溢出。你的意思是做无符号算术吗?
-
@user1155120 那么我不应该将我的向量转换为
signed,对吧? -
我缺少你的测试台。测试你的代码通常是一种方法:它会告诉你哪里出了问题。