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
// Description: Compute min and max among 4 unsigned 8-bit inputs
//==================================================
module minmax4_task (
    input  wire [7:0] v0,
    input  wire [7:0] v1,
    input  wire [7:0] v2,
    input  wire [7:0] v3,
    output reg  [7:0] min_val,
    output reg  [7:0] max_val
);

    // ------------------------------------------------
    // Task to compute min and max among 4 values
    // ------------------------------------------------
    task automatic find_minmax4(
        input  [7:0] a,
        input  [7:0] b,
        input  [7:0] c,
        input  [7:0] d,
        output [7:0] min_out,
        output [7:0] max_out
    );
        reg [7:0] min_temp, max_temp;
        begin
            // Start with first value
            min_temp = a;
            max_temp = a;

            // Compare with others
            if (b < min_temp) min_temp = b;
            if (c < min_temp) min_temp = c;
            if (d < min_temp) min_temp = d;

            if (b > max_temp) max_temp = b;
            if (c > max_temp) max_temp = c;
            if (d > max_temp) max_temp = d;

            // Assign results
            min_out = min_temp;
            max_out = max_temp;
        end
    endtask

    // ------------------------------------------------
    // Combinational call
    // ------------------------------------------------
    always @* begin
        find_minmax4(v0, v1, v2, v3, min_val, max_val);
    end

endmodule

 

Was this helpful?
Upvote
Downvote