Prev Problem
Next Problem

58. Maximum of Two Numbers

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

//==================================================
// Module: max2_func
// Description: Returns the maximum of two 8-bit unsigned inputs
//==================================================
module max2_func (
    input  wire [7:0] a,
    input  wire [7:0] b,
    output wire [7:0] max
);

    // ------------------------------------------------
    // Function definition
    // ------------------------------------------------
    function automatic [7:0] max2(input [7:0] x, input [7:0] y);
        begin
            max2 = (x > y) ? x : y;  // Single comparison line
        end
    endfunction

    // ------------------------------------------------
    // Use the function
    // ------------------------------------------------
    assign max = max2(a, b);

endmodule

 

Was this helpful?
Upvote
Downvote