Prev Problem
Next Problem

70. Universal Barrel Shifter

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

 

Code

module univ_barrel8 (
    input  [7:0] x,
    input  [2:0] sh,    // 0..7
    input  [2:0] mode,  // see table below
    output reg [7:0] y
);
    // mode encodings
    localparam [2:0] 
                        PASS = 3'b000,
                        LSL  = 3'b001,
                        LSR  = 3'b010,
                        ASR  = 3'b011,
                        ROL  = 3'b100,
                        ROR  = 3'b101;

    always @* begin
        case (mode)
            PASS: y = x;                                        // pass-through
            LSL : y = (x << sh) & 8'hFF;                        // logical left
            LSR : y = (x >> sh);                                // logical right
            ASR : y = $signed(x) >>> sh;                        // arithmetic right
            ROL : y = ((x << sh) | (x >> (8 - sh))) & 8'hFF;    // rotate left
            ROR : y = ((x >> sh) | (x << (8 - sh))) & 8'hFF;    // rotate right
            default: y = x;                                       // reserved → PASS
        endcase
    end
endmodule

 

Was this helpful?
Upvote
Downvote