Prev Problem
Next Problem

61. Vector Min/Max Pair

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

module minmax4_task (
    input  [7:0] v0,
    input  [7:0] v1,
    input  [7:0] v2,
    input  [7:0] v3,
    output reg [7:0] min_val,
    output reg [7:0] max_val
);

    // Task to compute min and max of 4 values
    task minmax4;
        input  [7:0] a, b, c, d;
        output [7:0] min_o;
        output [7:0] max_o;

        reg [7:0] min01, max01;
        reg [7:0] min23, max23;
        begin
            // Pairwise compare (a,b)
            max01 = (a > b) ? a : b;
            min01 = (a < b) ? a : b;

            // Pairwise compare (c,d)
            max23 = (c > d) ? c : d;
            min23 = (c < d) ? c : d;

            // Final reduce across pairs
            max_o = (max01 > max23) ? max01 : max23;
            min_o = (min01 < min23) ? min01 : min23;
        end
    endtask

    // Always block calls the task
    always @* begin
        minmax4(v0, v1, v2, v3, min_val, max_val);
    end

endmodule

 

Was this helpful?
Upvote
Downvote