Skip to content

Commit 7470909

Browse files
committed
quest 33: Added HDL, tb & wave for bubble_sort
1 parent 9287c8e commit 7470909

3 files changed

Lines changed: 440 additions & 0 deletions

File tree

ip/33_Bubble_Sort/bubble_sort.vhd

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
onerror {resume}
2+
quietly WaveActivateNextPane {} 0
3+
add wave -noupdate -divider DuT
4+
add wave -noupdate -divider Interface
5+
add wave -noupdate /tb_bubble_sort/clk
6+
add wave -noupdate /tb_bubble_sort/rst_n
7+
add wave -noupdate /tb_bubble_sort/sortit
8+
add wave -noupdate /tb_bubble_sort/din
9+
add wave -noupdate /tb_bubble_sort/dout
10+
add wave -noupdate -divider Internal
11+
add wave -noupdate -radix unsigned /tb_bubble_sort/DuT/bubble_sorter/memory
12+
add wave -noupdate -radix unsigned /tb_bubble_sort/DuT/bubble_sorter/temp
13+
add wave -noupdate -radix unsigned /tb_bubble_sort/DuT/bubble_sorter/counter
14+
add wave -noupdate -radix hexadecimal -childformat {{/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(24) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(23) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(22) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(21) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(20) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(19) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(18) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(17) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(16) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(15) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(14) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(13) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(12) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(11) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(10) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(9) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(8) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(7) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(6) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(5) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(4) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(3) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(2) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(1) -radix unsigned} {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(0) -radix unsigned}} -subitemconfig {/tb_bubble_sort/DuT/bubble_sorter/sorted_vector(24) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(23) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(22) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(21) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(20) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(19) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(18) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(17) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(16) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(15) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(14) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(13) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(12) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(11) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(10) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(9) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(8) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(7) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(6) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(5) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(4) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(3) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(2) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(1) {-height 15 -radix unsigned} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector(0) {-height 15 -radix unsigned}} /tb_bubble_sort/DuT/bubble_sorter/sorted_vector
15+
add wave -noupdate -divider {tb - Internal}
16+
add wave -noupdate /tb_bubble_sort/simulation_done
17+
TreeUpdate [SetDefaultTree]
18+
WaveRestoreCursors {{Cursor 1} {10411234 ps} 0}
19+
quietly wave cursor active 1
20+
configure wave -namecolwidth 150
21+
configure wave -valuecolwidth 100
22+
configure wave -justifyvalue left
23+
configure wave -signalnamewidth 1
24+
configure wave -snapdistance 10
25+
configure wave -datasetprefix 0
26+
configure wave -rowmargin 4
27+
configure wave -childrowmargin 2
28+
configure wave -gridoffset 0
29+
configure wave -gridperiod 1
30+
configure wave -griddelta 40
31+
configure wave -timeline 0
32+
configure wave -timelineunits ns
33+
update
34+
WaveRestoreZoom {0 ps} {10963050 ps}

0 commit comments

Comments
 (0)