4-bit Adder - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

202


69
 
1
-- Alternative testbench
2
library IEEE;
3
use IEEE.std_logic_1164.all;
4
5
entity testbench2 is
6
end testbench2;
7
8
architecture tb of testbench2 is
9
10
signal  x, y, z, sHA, cHA, sFA, cFA: std_logic;
11
signal A, B, S: std_logic_vector (3 downto 0);
12
signal C0, C4: std_logic;
13
14
begin
15
16
    DUT_HA: entity work.half_adder port map( x, y, sHA, cHA);
17
    DUT_FA: entity work.full_adder port map( x, y, z, sFA, cFA);
18
    DUT_Add4: entity work.adder_4bit(adder4_struct) port map( A, B, C0, S, C4);
19
20
    process begin
21
22
        x <= '0';
23
        y <= '0';
24
        z <= '0';
25
        A <= "0000";
26
        B <= "0000";
27
        C0 <= '0';
28
        wait for 10 ns;
29
        assert (sHA='0' and cHA='0') report "ERROR Half adder on (0,0)";
30
        assert (sFA='0' and cFA='0') report "ERROR Full adder on (0,0,0)";
31
        assert (S="0000" and C4='0') report "ERROR  Adder 4 bit on (0000,0000,0)";
32
        
33
        x <= '0';
34
        y <= '1';
35
        z <= '0';
36
        A <= "0101";
37
        B <= "1010";
38
        C0 <= '0';
39
        wait for 10 ns;
40
        assert (sHA='1' and cHA='0') report "ERROR Half adder on (0,1)";
41
        assert (sFA='1' and cFA='0') report "ERROR Full adder on (0,1,0)";
42
        assert (S="1111" and C4='0') report "ERROR Adder 4 bit on (0101,1010,0)";
43
44
        x <= '1';
45
        y <= '1';
46
        z <= '0';
47
        A <= "1100";
48
        B <= "1001";
49
        C0 <= '0';
50
        wait for 10 ns;
51
        assert (sHA='0' and cHA='1') report "ERROR Half adder on (1,1)";
52
        assert (sFA='0' and cFA='1') report "ERROR Full adder on (1,1,0)";
53
        assert (S="0101" and C4='1') report "ERROR Adder 4 bit on (1100,1001,0)";
54
55
        x <= '1';
56
        y <= '1';
57
        z <= '1';
58
        A <= "1111";
59
        B <= "0001";
60
        C0 <= '0';
61
        wait for 10 ns;
62
        assert (sHA='0' and cHA='1') report "ERROR Half adder on (1,1)";
63
        assert (sFA='1' and cFA='1') report "ERROR Full adder on (1,1,1)";
64
        assert (S="0000" and C4='1') report "ERROR Adder 4 bit on (1111,0001,0)";
65
          
66
    wait;
67
    end process;
68
69
end tb; 
xxxxxxxxxx
1
40
 
1
-- 4-bit ripple carry adder with gerarchic dataflow-structural architecture and behavioral architecture
2
3
library IEEE;
4
use IEEE.std_logic_1164.all;
5
use IEEE.std_logic_unsigned.all;
6
7
entity adder_4bit is
8
    port ( A, B: in std_logic_vector(3 downto 0);   
9
             C0: in std_logic;                      
10
              S: out std_logic_vector(3 downto 0);  
11
             C4: out std_logic);                                        
12
end adder_4bit;
13
14
architecture adder4_struct of adder_4bit is
15
16
    signal C: std_logic_vector(4 downto 0);
17
18
begin
19
    bit0: entity work.full_adder port map (A(0), B(0), C(0), S(0), C(1));
20
    bit1: entity work.full_adder port map (A(1), B(1), C(1), S(1), C(2));
21
    bit2: entity work.full_adder port map (A(2), B(2), C(2), S(2), C(3));
22
    bit3: entity work.full_adder port map (A(3), B(3), C(3), S(3), C(4));
23
    C(0) <= C0;
24
    C4 <= C(4);
25
   
26
end adder4_struct;
27
28
architecture adder4_beh of adder_4bit is
29
30
signal 
31
32
    sum: std_logic_vector(4 downto 0);
33
34
begin
35
    
36
    sum <= ('0' & A) + ('0' & B) + ("0000" & C0);
37
    C4 <= sum(4);
38
    S <= sum(3 downto 0);
39
40
end adder4_beh;
806 views and 0 likes     
 
4-bit ripple carry adder with two alternative testbenches

4-bit ripple carry adder with two alternative testbenches

190:0