Prev Problem
Next Problem

68. 4-bit Comparator

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

//==================================================
// Module: maxmin4
// Description: Compute max and min of four 4-bit unsigned inputs
//==================================================

module maxmin4 (
    input  wire [3:0] a,
    input  wire [3:0] b,
    input  wire [3:0] c,
    input  wire [3:0] d,
    output reg  [3:0] max,
    output reg  [3:0] min
);

    // Intermediate results
    reg [3:0] max1, max2;
    reg [3:0] min1, min2;

    always @* begin
        // -----------------------------
        // Step 1: Pairwise comparison
        // -----------------------------
        max1 = (a > b) ? a : b;
        min1 = (a < b) ? a : b;

        max2 = (c > d) ? c : d;
        min2 = (c < d) ? c : d;

        // -----------------------------
        // Step 2: Compare between pairs
        // -----------------------------
        max = (max1 > max2) ? max1 : max2;
        min = (min1 < min2) ? min1 : min2;
    end

endmodule

 

Was this helpful?
Upvote
Downvote