4
entity ripple_carry_adder is
generic(width : positive := 4);
Port ( A, B : in STD_LOGIC_VECTOR (width-1 downto 0);
Sum : out STD_LOGIC_VECTOR (width-1 downto 0);
carry : out STD_LOGIC);
end ripple_carry_adder;
architecture Behavioral of ripple_carry_adder is
component half_adder is
Port ( a, b : in STD_LOGIC;
sum : out STD_LOGIC;
carry : out STD_LOGIC);
end component half_adder;
component full_adder is
Port ( a, b, c : in STD_LOGIC;
sum : out STD_LOGIC;
carry : out STD_LOGIC);
end component full_adder;
signal C : std_logic_vector(width-1 downto 0);
begin
RCA : for i in 0 to width-1 generate
LSB : if i = 0 generate
ADD0 : half_adder port map (a => A(0), b => B(0), sum => SUM(0), carry => C(0));
end generate LSB;
UPPER_BITS : if i > 0 generate
ADDX : full_adder port map (a => A(i), b => B(i), c => C(i-1), sum => SUM(i), carry => C(i));
end generate UPPER_BITS;
end generate RCA;
carry <= C(width-1);
end Behavioral;
entity if_generate is
generic(sel : natural := 0);
Port ( a, b, c, d : in STD_LOGIC;
y : out STD_LOGIC);
end if_generate;
architecture Behavioral of if_generate is
begin
AND_DATA : if sel = 0 generate
y <= (((a and b) and c) and d);
end generate;
OR_DATA : if sel = 1 generate
y <= (((a or b) or c) or d);
end generate;
NAND_DATA : if sel = 2 generate
y <= (((a nand b) nand c) nand d);
end generate;
NOR_DATA : if sel = 3 generate
y <= (((a nor b) nor c) nor d);
end generate;
XOR_DATA : if sel = 4 generate
y <= (((a xor b) xor c) xor d);
end generate;
XNOR_DATA : if sel >= 5 generate
y <= (((a xnor b) xnor c) xnor d);
end generate;
end Behavioral;
entity parity_generator is
--
generic(sel : natural := 0; -- 0 = even parity, 1 = odd parity
width : natural := 8 -- data input (word) size
);
Port ( D : in STD_LOGIC_VECTOR (width-1 downto 0); -- data input
parity : out STD_LOGIC -- parity bit
);
end parity_generator;
architecture Behavioral of parity_generator is
begin
--Use for-generate statement to design the "parity_generator" module
EVEN : if sel = 0 generate
--set the "parity" bit
end generate;
ODD : if sel = 1 generate
-- set the "parity" bit
end generate;
end Behavioral;