Prev Problem
Next Problem

57. Using Functions

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

//==================================================
// Module: parity_and_reverse
// Description: Computes parity (XOR of all bits)
//              and bit-reversed version of input.
//==================================================
module parity_and_reverse (
    input  wire [7:0] a,
    output wire       parity,
    output wire [7:0] rev
);

    // ------------------------------------------------
    // Predefined functions (as per template)
    // ------------------------------------------------
    function automatic parity8(input [7:0] x);
        parity8 = ^x;  // Reduction XOR
    endfunction

    function automatic [7:0] reverse8(input [7:0] x);
        integer i;
        begin
            for (i = 0; i < 8; i = i + 1)
                reverse8[i] = x[7 - i];
        end
    endfunction

    // ------------------------------------------------
    // Use the functions
    // ------------------------------------------------
    assign parity = parity8(a);
    assign rev    = reverse8(a);

endmodule

 

Was this helpful?
Upvote
Downvote