-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adder.vhd
45 lines (31 loc) · 879 Bytes
/
Adder.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_SIGNED.ALL;
entity Adder is
generic (N : integer := 64);
port (
A : in STD_LOGIC_VECTOR(N-1 downto 0);
B : in STD_LOGIC_VECTOR(N-1 downto 0);
R : out STD_LOGIC_VECTOR(N-1 downto 0);
CARRY_IN : in STD_LOGIC;
OVERFLOW : out STD_LOGIC
);
end Adder;
architecture Behavioral of Adder is
signal result : STD_LOGIC_VECTOR(N-1 downto 0);
begin
ADD : process (A, B, result, CARRY_IN) is
begin
-- Add, add, add, away!
result <= A + B + CARRY_IN;
-- Overflow if the sign bit of A and B is equal, but result differs.
-- OVERFLOW <= ( (not A(N-1)) xor B(N-1)) and (A(N-1) xor result(N-1));
if (A(N-1) = B(N-1)) and (B(N-1) /= result(N-1)) then
OVERFLOW <= '1';
else
OVERFLOW <= '0';
end if;
-- Onwards!
R <= result;
end process;
end Behavioral;