I have tried a few ways using for loops to find the least number in array, but having a hard time in updating the pointer whenever a new least value is encountered. I am following the below textbook approach :
- Start from the beginning of the array and sweep through the entire range
- If first element is lesser than a number at any index, store this number in a temporary register and log the index
- The logged index becomes the new index
- Repeat this until the array is exhausted
I tried to implement this as follows :
module min_in_array (
input logic clk,
input logic rstn,
input logic [3:0][7:0] data_in,
output logic [7:0] data_min
);
logic [7:0] data_min_temp;
logic [3:0] new_min_pointer;
integer i,j;
logic [3:0] new_min_pointer_ff;
always_ff @ (posedge clk or negedge rstn) begin
if (~rstn) begin
data_min_temp <= 0;
new_min_pointer <= 0;
end
else begin
for (j = 0 ; j < 4 ; j = j+1) begin
for (i = new_min_pointer_ff; i < 4 ; i = i+1) begin
if (data_in[i] < data_in[j])
data_min_temp <= data_in[i] ;
new_min_pointer <= i;
end
end
end
end
always_ff @ (posedge clk or negedge rstn) begin
if(~rstn) begin
data_min <= 'b0;
new_min_pointer_ff <= 'b0;
end
else begin
data_min <= data_min_temp;
new_min_pointer_ff <= new_min_pointer;
end
end
endmodule
Where am I going wrong?
The comparison happens in combo logic and pointer update in sequential. Based on the testbench that I have written, this code stops executing after finding the first least element. For example, if array = {17,26,15,14} , the code gives output 15.
