Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6) Bonus: Write a module containing a function that does a bubble or a shell sor

ID: 3348425 • Letter: 6

Question

6) Bonus: Write a module containing a function that does a bubble or a shell sort. Pick only one. Sort in ascending order. Assume the array you are sorting is N (which will be a parameter) values long and each value is 8 bits wide. This is an array of N 8bit numbers. Another way to put this is that it is an unpacked, packed array. The unpacked length is N, the packed length is 8. An example of such an array is logic [7:0] UnSrtArr [N-1:0]: You have some freedom in the way you write this code. Note, that pseudocodes are given in the cheat sheet. State your assumptions and define any variables or parameters used. Use data structures if it makes it easier. Use good organization and indent. Partial credit on bonus questions will be limited. module sort2 #(parameter N=16) (input [701 UnsrtArr [N-101, output logic [701 SrtArr [N-1:0));

Explanation / Answer

module sort2 #(parameter N = 16) (
input [7:0] UnSrtArr[N-1:0],
output logic [7:0] SrtArr[N-1:0]
);


// Internal variables
logic [7:0] tempArr[N-1:0], tempVar;
integer i, j;


always_comb
begin // begin always

for (i = 0; i < N ; i = i + 1)
tempArr[i] = UnSrtArr[i];


for (i = 0; i < N ; i = i + 1) begin // loop1
for (j = 1; j < N; j = j + 1) begin // loop2
if (tempArr[j-1] > tempArr[j]) begin // start if
tempVar = tempArr[j-1];
tempArr[j-1] = tempArr[j];
tempArr[j] = tempVar;
end // end if
end // end loop2
end // end loop1

for (i = 0; i < N ; i = i + 1)
SrtArr[i] = tempArr[i];

end //end always
  
endmodule

module testbench ;
reg [7:0] UnSrtArr[15:0];
logic [7:0] SrtArr[15:0];

sort2 #(.N(16)) DUT (UnSrtArr, SrtArr);
integer i;

initial begin
$write ("UnSorted Array Input = ");
for (i = 0; i < 16 ; i = i + 1) begin
UnSrtArr[i] = $urandom;
$write("%4d ", UnSrtArr[i]);
end
$display("");

#10;

// $display("UnSorted Array Input = ");
// for (i = 0; i < N ; i = i + 1)
// $display("%d ", UnSrtArr[i]);

$write ("Sorted Array Output = ");
for (i = 0; i < 16 ; i = i + 1)
$write("%4d ", SrtArr[i]);
$display("");

end
endmodule

/*************** OUTPUT OF PROGRAM **************
UnSorted Array Input = 180 170 214 100 186 0 41 116 179 132 232 184 28 52 96 13
Sorted Array Output = 0 13 28 41 52 96 100 116 132 170 179 180 184 186 214 232
**************************************************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote