bubble.vhd


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
6
entity Bubblesort is
7
end Bubblesort;
8
9
architecture Behavioral of Bubblesort is
10
11
-- Signals RAM
12
signal addra      : std_logic_vector(5 downto 0)  := (others => '0');    
13
signal addrb      : std_logic_vector(5 downto 0)  := (others => '0');  
14
signal addrb_d1      : std_logic_vector(5 downto 0)  := (others => '0');  
15
signal addrb_d2      : std_logic_vector(5 downto 0)  := (others => '0');  
16
signal addrb_d3      : std_logic_vector(5 downto 0)  := (others => '0');  
17
signal addrb_d4      : std_logic_vector(5 downto 0)  := (others => '0');  
18
signal dout_a      : std_logic_vector(15 downto 0)  := (others => '0');
19
signal din_a      : std_logic_vector(15 downto 0)  := (others => '0');  
20
signal din_b      : std_logic_vector(15 downto 0)  := (others => '0');  
21
signal we_a        : std_logic_vector(0 downto 0)  := (others => '0');
22
signal we_b        : std_logic_vector(0 downto 0)  := (others => '0');
23
24
-- Signals Bubble
25
signal busy         : std_logic           := '0';
26
signal value1       : std_logic_vector(15 downto 0)  := (others => '0');
27
signal value2       : std_logic_vector(15 downto 0)  := (others => '0');
28
signal temp         : std_logic_vector(15 downto 0)  := (others => '0');
29
signal n            : integer range 0 to 64;
30
signal start      : std_logic           := '0';
31
signal clk        : std_logic           := '0';
32
signal enable         : std_logic           := '0';     
33
signal done         : std_logic           := '0';       
34
signal reset         : std_logic           := '0';       
35
signal reset_count    : std_logic_vector(1 downto 0)  := (others => '0'); 
36
signal i        : std_logic_vector(5 downto 0)  := (others => '0');  
37
38
39
begin
40
41
42
-------------------------------------------------------------------
43
-- TESTBENCH
44
-------------------------------------------------------------------
45
clk    <= not clk  after 20 ns;  -- 25 MHz Taktfrequenz
46
enable  <= '1';
47
start   <= '1';
48
49
-------------------------------------------------------------------
50
-- INSTANTIATE BUBBLE_RAM
51
-------------------------------------------------------------------
52
BUBBLE : entity work.BUBBLE
53
port map
54
(
55
    clka   => clk,
56
    wea   => "0",
57
    addra   => i,
58
    dina   => din_a,
59
    douta   => value2,
60
    clkb   => clk,
61
    web   => we_b,
62
    addrb   => addrb_d4,
63
    dinb   => din_b,
64
    doutb   => open
65
  );
66
  
67
-------------------------------------------------------------------
68
-- DELAYS
69
-------------------------------------------------------------------
70
p_del: process(clk) is
71
begin
72
  if rising_edge(clk) then
73
    value1 <= value2;
74
  end if;
75
end process;
76
77
p_adrb_del1: process(clk) is 
78
begin
79
  if rising_edge(clk) then
80
    addrb_d1 <= addrb;
81
  end if;
82
end process;
83
84
p_adrb_del2: process(clk) is 
85
begin
86
  if rising_edge(clk) then
87
    addrb_d2 <= addrb_d1;
88
  end if;
89
end process;
90
91
p_adrb_del3: process(clk) is 
92
begin
93
  if rising_edge(clk) then
94
    addrb_d3 <= addrb_d2;
95
  end if;
96
end process;
97
98
99
p_adrb_del4: process(clk) is 
100
begin
101
  if rising_edge(clk) then
102
    addrb_d4 <= addrb_d3;
103
  end if;
104
end process;
105
 
106
-------------------------------------------------------------------
107
-- GENERATE RESET
108
-------------------------------------------------------------------
109
p_reset_count: process(clk) is
110
begin
111
  if rising_edge(clk) then
112
    if (reset_count < "10") then
113
      reset <= '1';
114
      reset_count <= reset_count + 1;
115
    else
116
      reset <= '0';
117
      reset_count <= "11";
118
    end if;
119
  end if;
120
end process;
121
122
-------------------------------------------------------------------
123
-- BUBBLE ALGORITHM
124
-------------------------------------------------------------------
125
p_bubble: process(clk) is 
126
begin
127
    if rising_edge(clk) then
128
    if (busy = '0') then
129
      if (start = '1') then
130
            busy <= '1';
131
            n    <= 0;
132
            i    <= "000000";
133
      end if;
134
    else
135
      if (n < 64) then
136
        if (i < "111111") then  -- (n -1) = 63
137
          if value1 > value2 then
138
            TEMP <= value2;
139
140
            we_b <= "1";
141
            addrb <= addra + 1;
142
            din_b <= TEMP;
143
          else
144
            we_b <= "0";
145
            addrb <= addra + 1;
146
          end if;
147
          i <= i+1; -- addra
148
        else
149
          i <= (others => '0');
150
          n <= n + 1;
151
        end if;
152
      else
153
        busy <= '0';       
154
        --do   <= din;
155
      end if;
156
    end if;
157
  end if;
158
end process;
159
   
160
   done <= '1' when start = '0' and busy = '0' else '0';
161
162
end Behavioral;