Prev Problem
Next Problem

62. Nibble Swap

module nibble_swap_task (
    input  [7:0] x,
    output reg [7:0] y
);
    task swap_nibbles;
        input  [7:0] xin;
        output [3:0] hi;
        output [3:0] lo;
        begin
            hi = xin[7:4];
            lo = xin[3:0];
        end
    endtask

    reg [3:0] hi_n, lo_n;

    always @* begin
        swap_nibbles(x, hi_n, lo_n);
        y = {lo_n, hi_n};
    end
endmodule

💡Remember

  • Tasks can return multiple outputs (here, hi and lo)—that’s why tasks are handy for small unpack/repack jobs.
  • Keep the logic combinational: use always @*, no #/@/wait.
  • Bit slicing: x[7:4] is the high nibble, x[3:0] is the low nibble.