Minimum in array
Solution 1:
When you unroll the for
loop, this is what it would look like:
always_ff @(posedge clk, posedge rstN) begin
if(rstN == 1'b1) begin
currRes <= 4'b1111;
end
else begin
if( resArray[0] < currRes) begin
currRes <= resArray[0];
minVal <= resArray[0];
end
if( resArray[1] < currRes) begin
currRes <= resArray[1];
minVal <= resArray[1];
end
if( resArray[2] < currRes) begin
currRes <= resArray[2];
minVal <= resArray[2];
end
end
end
You end up with multiple nonblocking assignments to the same register (currRes
).
On the 1st posedge of the clock after reset, all 3 if
clauses are true, and the last assignment wins:
currRes <= resArray[2];
So, currRes
is assigned the value 3, not 1.
The same is true of minVal
.
You need to sort the 3 input values, then compare the minimum of those values to the current minimum.