-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarry_lookahead_adder.vhd
79 lines (72 loc) · 2.2 KB
/
carry_lookahead_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
library IEEE;
use IEEE.STD_LOGIC_1164.all;
--! @title Carry Look-ahead Adder
--! A N-bit adder using carry look-ahead.
--! This is combinational circuit.
--! If enable != '1' then the output is set to high impedence.
entity carry_lookahead_adder is
generic (
--! The bit wifth of input integers.
bit_width : integer := 4
);
port (
--! Input integer A.
A : in std_logic_vector(bit_width - 1 downto 0);
--! Input integer B.
B : in std_logic_vector(bit_width - 1 downto 0);
--! Output Sum=A+B.
S : out std_logic_vector(bit_width - 1 downto 0);
--! Output Carry.
C_out : out std_logic;
--! If not = '1', output is high impedence.
enable : in std_logic
);
end carry_lookahead_adder;
architecture arc of carry_lookahead_adder is
--! 1-bit Full Adder
component full_adder is
port (
--! Input A.
A : in std_logic;
--! Input B.
B : in std_logic;
--! Input Carry.
C_in : in std_logic;
--! Output Sum.
S : out std_logic;
--! Output Carry.
C_out : out std_logic;
--! If not = '1', output is high impedence and internal state is frozen.
enable : in std_logic
);
end component;
--! Generate signals
signal G : std_logic_vector(bit_width - 1 downto 0);
--! Propagate signals
signal P : std_logic_vector(bit_width - 1 downto 0);
--! Carry buffer
signal C : std_logic_vector(bit_width downto 0);
begin
--! No carry for first FA
C(0) <= '0';
--! Create the Generate (Gi=Ai*Bi), Propagate (Pi=Ai+Bi) and Carry terms
GENERATE_GPC : for j in 0 to bit_width - 1 generate
G(j) <= A(j) and B(j);
P(j) <= A(j) or B(j);
C(j + 1) <= G(j) or (P(j) and C(j));
end generate GENERATE_GPC;
--! Create the Full Adders and connect them to GPC
GENERATE_FULL_ADDERS : for i in 0 to bit_width - 1 generate
FULL_ADDER_INST : full_adder
port map(
A => A(i),
B => B(i),
C_in => C(i),
S => S(i),
C_out => open,
enable => enable
);
end generate GENERATE_FULL_ADDERS;
--! Output results
C_out <= C(bit_width) when enable = '1' else 'Z';
end architecture;