Skip to content

Commit 17a95df

Browse files
authored
adder & subtractor
1 parent ecdcfbf commit 17a95df

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

4-BIT ADDER/4bitAdder.vhd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
4+
entity full_adder_4b is
5+
6+
port (
7+
a,b : in std_logic_vector(3 downto 0);
8+
s : out std_logic_vector(3 downto 0);
9+
c : inout std_logic_vector(4 downto 1)
10+
);
11+
12+
end full_adder_4b;
13+
14+
architecture beh of full_adder_4b is
15+
constant c0 : std_logic := '0';
16+
begin
17+
18+
s(0) <= a(0) xor b(0) xor c0;
19+
c(1) <= (a(0) and b(0))or(b(0) and c0)or(a(0) and c0);
20+
s(1) <= a(1) xor b(1) xor c(1);
21+
c(2) <= (a(1) and b(1))or(b(1) and c(1))or(a(1) and c(1));
22+
s(2) <= a(2) xor b(2) xor c(2);
23+
c(3) <= (a(2) and b(2))or(b(2) and c(2))or(a(2) and c(2));
24+
s(3) <= a(3) xor b(3) xor c(3);
25+
c(4) <= (a(3) and b(3))or(b(3) and c(3))or(a(3) and c(3));
26+
27+
end beh;

4-BIT ADDER/tb_4bitAdder.vhd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
4+
entity full_adder_4b_tst is
5+
6+
end full_adder_4b_tst;
7+
architecture beh of full_adder_4b_tst is
8+
component full_adder_4b
9+
port (
10+
a,b: in std_logic_vector(3 downto 0);
11+
c :inout std_logic_vector(4 downto 1);
12+
s: out std_logic_vector(3 downto 0));
13+
14+
end component;
15+
signal a_s,b_s : std_logic_vector(3 downto 0);
16+
signal s_s : std_logic_vector(3 downto 0);
17+
signal c_s : std_logic_vector(4 downto 1);
18+
begin
19+
20+
u1 : full_adder_4b port map (
21+
a => a_s,
22+
b => b_s,
23+
c => c_s,
24+
s => s_s);
25+
26+
tst_p: process
27+
begin
28+
a_s<="0000";
29+
b_s<="0101";
30+
wait for 100 ns;
31+
a_s<="1100";
32+
b_s<="0100";
33+
wait for 100 ns;
34+
a_s<="1111";
35+
b_s<="0000";
36+
wait for 100 ns;
37+
a_s<="0010";
38+
b_s<="1101";
39+
wait for 100 ns;
40+
end process;
41+
end beh;

8-BIT ADDER/8bitAdder.vhd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library IEEE;
2+
use IEEE.std_logic_1164.all;
3+
use IEEE.std_logic_arith.all;
4+
use IEEE.std_logic_unsigned.all;
5+
entity adder_8b is
6+
port (
7+
a : in std_logic_vector(7 downto 0);
8+
b : in std_logic_vector(7 downto 0);
9+
c: out std_logic_vector(7 downto 0);
10+
);
11+
12+
end adder_8b;
13+
14+
architecture myarc of adder_8b is
15+
begin
16+
c(7 downto 0) <= a(7 downto 0) + b(7 downto 0);
17+
end myarc;

8-BIT ADDER/tb_8bitAdder.vhd

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
library IEEE;
2+
use IEEE.std_logic_1164.all;
3+
use IEEE.std_logic_unsigned.all;
4+
use IEEE.numeric_std.all;
5+
6+
entity tb_adder_8b is
7+
end tb_adder_8b;
8+
9+
architecture myarc of tb_adder_8b is
10+
component adder_8b
11+
port (
12+
a : in std_logic_vector(7 downto 0);
13+
b : in std_logic_vector(7 downto 0);
14+
c: out std_logic_vector(7 downto 0);
15+
);
16+
end adder_8b;
17+
18+
signal a: std_logic_vector (7 downto 0) := (others => '0');
19+
signal b: std_logic_vector (7 downto 0) := (others => '0');
20+
signal c: std_logic_vector(7 downto 0);
21+
begin
22+
23+
uut: adder_8b PORT MAP(a=>a,b=>b,c=>c);
24+
tb: process
25+
begin
26+
wait for 10 ns;
27+
a<="00000000";
28+
b<="01010101";
29+
wait for 10 ns;
30+
a<="00000001";
31+
b<="00000011";
32+
wait for 10 ns;
33+
end process;
34+
end myarc;

8-BIT SUBTRACTOR/8bitSubtractor.vhd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library IEEE;
2+
use IEEE.std_logic_1164.all;
3+
use IEEE.std_logic_arith.all;
4+
use IEEE.std_logic_unsigned.all;
5+
entity subtractor_8b is
6+
port (
7+
a : in std_logic_vector(7 downto 0);
8+
b : in std_logic_vector(7 downto 0);
9+
d: out std_logic_vector(7 downto 0);
10+
);
11+
12+
end subtractor_8b;
13+
14+
architecture myarc of subtractor_8b is
15+
begin
16+
d(7 downto 0) <= a(7 downto 0) - b(7 downto 0);
17+
end myarc;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
library IEEE;
2+
use IEEE.std_logic_1164.all;
3+
use IEEE.std_logic_unsigned.all;
4+
use IEEE.numeric_std.all;
5+
6+
entity tb_subtractor_8b is
7+
end tb_subtractor_8b;
8+
9+
architecture myarc of tb_subtractor_8b is
10+
component subtractor_8b
11+
port (
12+
a : in std_logic_vector(7 downto 0);
13+
b : in std_logic_vector(7 downto 0);
14+
d: out std_logic_vector(7 downto 0);
15+
);
16+
end subtractor_8b;
17+
18+
signal a: std_logic_vector (7 downto 0) := (others => '0');
19+
signal b: std_logic_vector (7 downto 0) := (others => '0');
20+
signal d: std_logic_vector(7 downto 0);
21+
begin
22+
23+
uut: subtractor_8b PORT MAP(a=>a,b=>b,d=>d);
24+
tb: process
25+
begin
26+
wait for 10 ns;
27+
a<="00000000";
28+
b<="01010101";
29+
wait for 10 ns;
30+
a<="00000001";
31+
b<="00000011";
32+
wait for 10 ns;
33+
end process;
34+
end myarc;

0 commit comments

Comments
 (0)