|
| 1 | +--! ---------------------------------------------------------------------------- |
| 2 | +--! @author N. Selvarajah |
| 3 | +--! @brief Based on chipdev.io question 33 |
| 4 | +--! @details VHDL module for Bubble Sort |
| 5 | +--! @details Basically, repeatedly steps through the list, compares adjacent elements |
| 6 | +--! and swaps them if they are in the wrong order. The pass through the list |
| 7 | +--! is repeated until the list is sorted. |
| 8 | +--! @note This means in worst case, it has a time complexity of O(n^2) |
| 9 | +--! ---------------------------------------------------------------------------- |
| 10 | + |
| 11 | +library ieee; |
| 12 | +use ieee.std_logic_1164.all; |
| 13 | +use ieee.numeric_std.all; |
| 14 | + |
| 15 | +use work.utils_pkg.all; |
| 16 | + |
| 17 | +entity bubble_sort is |
| 18 | + generic ( |
| 19 | + BITWIDTH: positive := 3; |
| 20 | + VECTOR_SIZE: positive range 1 to to_bits(positive'high) := 8 |
| 21 | + ); |
| 22 | + port ( |
| 23 | + clk: in std_ulogic; |
| 24 | + rst_n: in std_ulogic; |
| 25 | + din: in std_ulogic_vector(BITWIDTH - 1 downto 0); |
| 26 | + sortit: in std_ulogic; |
| 27 | + dout: out std_ulogic_vector(VECTOR_SIZE * BITWIDTH downto 0) |
| 28 | + ); |
| 29 | +end entity; |
| 30 | + |
| 31 | +architecture behavioural of bubble_sort is |
| 32 | +begin |
| 33 | + bubble_sorter: process(clk) |
| 34 | + type memory_array_t is array (0 to VECTOR_SIZE - 1) of din'subtype; |
| 35 | + variable memory: memory_array_t; |
| 36 | + variable temp: din'subtype; |
| 37 | + variable counter: natural range 0 to VECTOR_SIZE - 1 := 0; |
| 38 | + variable sorted_vector: dout'subtype; |
| 39 | + |
| 40 | + impure function bubble_sorter return memory_array_t is |
| 41 | + variable swap: boolean; |
| 42 | + begin |
| 43 | + for i in VECTOR_SIZE - 1 downto 0 loop |
| 44 | + inner_loop: for mem_idx in 1 to VECTOR_SIZE - 1 loop |
| 45 | + swap := unsigned(memory(mem_idx - 1)) > unsigned(memory(mem_idx)); |
| 46 | + if swap then |
| 47 | + temp := memory(mem_idx - 1); |
| 48 | + memory(mem_idx - 1) := memory(mem_idx); |
| 49 | + memory(mem_idx) := temp; |
| 50 | + end if; |
| 51 | + exit inner_loop when mem_idx >= i; |
| 52 | + end loop; |
| 53 | + end loop; |
| 54 | + |
| 55 | + return memory; |
| 56 | + end function; |
| 57 | + |
| 58 | + impure function get_sorted_vector return dout'subtype is |
| 59 | + variable result: dout'subtype := (others => '0'); |
| 60 | + begin |
| 61 | + -- Map the MSB to the lowest index |
| 62 | + for idx in 0 to VECTOR_SIZE - 1 loop |
| 63 | + result((idx + 1) * BITWIDTH - 1 downto idx * BITWIDTH) := memory(VECTOR_SIZE - 1 - idx); |
| 64 | + end loop; |
| 65 | + return result; |
| 66 | + end function; |
| 67 | + begin |
| 68 | + -- NOTE: Order of operations is important here |
| 69 | + if rising_edge(clk) then |
| 70 | + if rst_n = '0' then |
| 71 | + memory := (others => (others => '0')); |
| 72 | + counter := 0; |
| 73 | + sorted_vector := (others => '0'); |
| 74 | + elsif not sortit then |
| 75 | + memory(counter) := din; |
| 76 | + counter := counter + 1 when counter < VECTOR_SIZE - 1 else 0; |
| 77 | + else |
| 78 | + memory := bubble_sorter; |
| 79 | + sorted_vector := get_sorted_vector; |
| 80 | + end if; |
| 81 | + end if; |
| 82 | + |
| 83 | + dout <= sorted_vector; |
| 84 | + end process; |
| 85 | +end architecture; |
0 commit comments