To implement the bitonic sorting network in SystemVerilog, we will need a series of logic blocks that perform the comparisons and swaps required to sort the original_list_i into the sorted_list_o. Here's a step-by-step guide to correct and enhance your module with the necessary logic to perform a bitonic sort on an 8-element list of 32-bit values.
module bitonic(
input logic [7:0][31:0] original_list_i,
output logic [7:0][31:0] sorted_list_o
);
logic [7:0][31:0] buffer_list;
// Helper function to compare and swap
function automatic void compare_and_swap(
inout logic [31:0] a,
inout logic [31:0] b,
input logic ascending
);
logic [31:0] temp;
if (ascending ? (a > b) : (a < b)) begin
temp = a;
a = b;
b = temp;
end
endfunction
// Bitonic sort process
always_comb begin
buffer_list = original_list_i;
// First stage - Create bitonic sequences of size 2
compare_and_swap(buffer_list[0], buffer_list[1], 1'b1);
compare_and_swap(buffer_list[2], buffer_list[3], 1'b0);
compare_and_swap(buffer_list[4], buffer_list[5], 1'b1);
compare_and_swap(buffer_list[6], buffer_list[7], 1'b0);
// Second stage - Create bitonic sequences of size 4
compare_and_swap(buffer_list[0], buffer_list[2], 1'b1);
compare_and_swap(buffer_list[1], buffer_list[3], 1'b1);
compare_and_swap(buffer_list[4], buffer_list[6], 1'b0);
compare_and_swap(buffer_list[5], buffer_list[7], 1'b0);
// Next, merge the bitonic sequence to form fully sorted sequences
compare_and_swap(buffer_list[0], buffer_list[4], 1'b1);
compare_and_swap(buffer_list[1], buffer_list[5], 1'b1);
compare_and_swap(buffer_list[2], buffer_list[6], 1'b1);
compare_and_swap(buffer_list[3], buffer_list[7], 1'b1);
// Final yield of sorted order
compare_and_swap(buffer_list[1], buffer_list[2], 1'b1);
compare_and_swap(buffer_list[3], buffer_list[4], 1'b1);
compare_and_swap(buffer_list[5], buffer_list[6], 1'b1);
sorted_list_o = buffer_list;
end
endmodule
Key Points:
-
Initialization:
buffer_list is initialized with original_list_i for in-place manipulation during bitonic sorting.
-
Compare-and-Swap Function:
- This function performs an in-place swap of two elements based on the desired sorting order.
-
Bitonic Sort Steps:
- The sort starts by creating bitonic sequences of size 2, then 4, and eventually sorts the list through merging.
-
Understanding Bitonic Sort:
- Bitonic sort is a parallel sorting network that consists of a sequence of comparisons to sort the list.
- Each stage combines sequences into larger bitonic sequences and then sorts them.
-
Full Sort:
- Finally, the algorithm fully sorts the array using the established sequences.
By expressing this logic in a clear and organized way, the code above should correctly implement a bitonic sort on original_list_i, providing the sorted result in sorted_list_o. Adjust or expand loops depending on the specific needs of your synthesis environment or the size of the list to sort.