我正在尝试使用完整的加法器来创建N位加法器/减法器。 输入是N位A,N位B,结果应为2N的长度(它输出ALU,其中2条总线分别为N位的高位和低位,因此我尝试扩展有符号位)。
问题出现在减法进行中。例如,在执行3-2(假设N = 3,所以它是011-010,并且有两个补码的情况是011 + 110)时,我得到001带有进位1。但在其他情况下,则有必要。例如,当尝试执行(-3)+(-3)(101 + 101,又是N = 3)时,我得到010,进位为1。这次,进位确实指示符号,因此我想扩展一下它。
这是我的代码:
entity FullAdder is
Port (
A : in std_logic;
B : in std_logic;
Cin : in std_logic;
sum : out std_logic;
Cout : out std_logic
);
end FullAdder;
architecture gate of FullAdder is
begin
sum <= A xor B xor Cin ;
Cout <= (A and B) OR (Cin and A) OR (Cin and B) ;
end gate;
这是N位加法器
entity NbitsAdder is
generic(N: integer := 8);
Port(
A : in std_logic_vector((N-1) downto 0);
B : in std_logic_vector((N-1) downto 0);
Cin: in std_logic;
SUM : out std_logic_vector((N-1) downto 0);
Cout : out std_logic
);
end NbitsAdder;
architecture NbitsAdderGate of NbitsAdder is
...
signal temp : std_logic_vector (N downto 0);
begin
temp (0) <= Cin;
arrrayOfFullAdders : for i in 0 to N-1 generate
adder_i: FullAdder port map ( A(i), B(i), temp(i), SUM(i), temp (i+1) );
end generate;
Cout <= temp(N); --which will be extend
end NbitsAdderGate;
这是ADDER或SUBTRACTOR
entity NbitsAddOrSub is
generic(N: integer := 8);
port(
A : in std_logic_vector ((N-1) downto 0);
B : in std_logic_vector ((N-1) downto 0);
addOrSub : in std_logic;
sumLo : out std_logic_vector ((N-1) downto 0);
sumHi : out std_logic_vector ((N-1) downto 0)
);
end NbitsAddOrSub;
architecture NbitsAddOrSubGate of NbitsAddOrSub is
signal tempB: std_logic_vector ( (N-1) downto 0);
signal CoutTemp: std_logic;
begin
loop1 : for i in 0 to N-1 generate
xor_i: xorGate port map ( B(i), addOrSub, tempB(i));
end generate;
theOperation : NbitsAdder generic map (N)
port map ( A => A, B => tempB, Cin => addOrSub, sum => sumLo, Cout => CoutTemp);
sumHi <= (N-1 downto 0 => CoutTemp); -- tring to extend the sign bit
end NbitsAddOrSubGate;
答案 0 :(得分:1)
在带符号的加法中,进位没有任何意义。您从总和的最高有效位而不是从进位获得符号位。在第二个示例中,存在下溢,因为-3 + -3小于2 ^((N = 3)-1),因此结果不正确。 要对结果进行符号扩展,您应该首先检查溢出/下溢条件以进行符号加法。如果没有发生上溢/下溢,则查看总和的最高有效位并将其扩展