Prev Problem
Next Problem

60. Using Task

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

//==================================================
// Module: sat_add8_wrap
// Description: 8-bit unsigned addition with saturation
//==================================================
module sat_add8_wrap (
    input  wire [7:0] a,
    input  wire [7:0] b,
    output reg  [7:0] sum,
    output reg        carry
);

    // ------------------------------------------------
    // Task definition: performs saturated addition
    // ------------------------------------------------
    task automatic sat_add8(
        input  [7:0] x,
        input  [7:0] y,
        output [7:0] s,
        output       c
    );
        reg [8:0] temp;
        begin
            temp = x + y;           // 9-bit temp detects carry
            c = temp[8];            // carry = MSB
            s = c ? 8'hFF : temp[7:0]; // if carry, saturate to FF
        end
    endtask

    // ------------------------------------------------
    // Invoke task combinationally
    // ------------------------------------------------
    always @* begin
        sat_add8(a, b, sum, carry);
    end

endmodule

 

Was this helpful?
Upvote
Downvote