Prev Problem
Next Problem

62. Nibble Swap

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

 

Code

//==================================================
// Module: nibble_swap_task
// Description: Swap high and low nibbles of 8-bit input
//==================================================
module nibble_swap_task (
    input  wire [7:0] x,
    output reg  [7:0] y
);

    // ------------------------------------------------
    // Task to extract high and low nibbles
    // ------------------------------------------------
    task automatic swap_nibbles(
        input  [7:0] xin,
        output [3:0] hi,
        output [3:0] lo
    );
        begin
            hi = xin[7:4];   // Extract high nibble
            lo = xin[3:0];   // Extract low nibble
        end
    endtask

    // ------------------------------------------------
    // Combinational logic using the task
    // ------------------------------------------------
    reg [3:0] hi_nib, lo_nib;

    always @* begin
        swap_nibbles(x, hi_nib, lo_nib);
        y = {lo_nib, hi_nib};   // Swap nibbles
    end

endmodule

 

Was this helpful?
Upvote
Downvote