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
);
    // --------- Provided task (complete the TODOs) ----------
    // Returns both min and max among four 8-bit unsigned inputs.
    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)
        if (a > b) begin
        max01 = a;
        min01 = b;
        end else begin
        max01 = b;
        min01 = a;
        end

        // Pairwise compare (c, d)
        if (c > d) begin
        max23 = c;
        min23 = d;
        end else begin
        max23 = d;
        min23 = c;
        end

        // Final reduce across pairs
        if (max01 > max23)
            max_o = max01;
        else
            max_o = max23;

        if (min01 < min23)
            min_o = min01;
        else
            min_o = min23;
    
            // Pairwise compare (a,b)
            // TODO: set max01 as the larger of a and b
            // TODO: set min01 as the smaller of a and b

            // Pairwise compare (c,d)
            // TODO: set max23 as the larger of c and d
            // TODO: set min23 as the smaller of c and d

            // Final reduce across pairs
            // TODO: set max_o as the larger of max01 and max23
            // TODO: set min_o as the smaller of min01 and min23
        end
    endtask
    // -------------------------------------------------------

    
    // TODO: Call the task and assign outputs
    reg [7:0] tmin, tmax;
    always @* begin
        minmax4(v0, v1, v2, v3,min_val,max_val);
    end

endmodule

 

Was this helpful?
Upvote
Downvote