WISC-SP22-5-Stage-Pipelined-Processor / verilog / shifter.v
shifter.v
Raw
/*
    CS/ECE 552 Spring '22
    Homework #2, Problem 1
    
    A barrel shifter module.  It is designed to shift a number via rotate
    left, shift left, shift right arithmetic, or shift right logical based
    on the 'Oper' value that is passed in.  It uses these
    shifts to shift the value any number of bits.
 */
module shifter (In, ShAmt, Oper, Out);

    // declare constant for size of inputs, outputs, and # bits to shift
    parameter OPERAND_WIDTH = 16;
    parameter SHAMT_WIDTH   =  4;
    parameter NUM_OPERATIONS = 2;

    input  [OPERAND_WIDTH -1:0] In   ; // Input operand
    input  [SHAMT_WIDTH   -1:0] ShAmt; // Amount to shift/rotate
    input  [NUM_OPERATIONS-1:0] Oper ; // Operation type
    output [OPERAND_WIDTH -1:0] Out  ; // Result of shift/rotate

   /* YOUR CODE HERE */
    wire [15:0] rot_left;
    wire [15:0] shft_left;
    wire [15:0] rot_right;
    wire [15:0] shft_right_log;
    
    rotate_left iRotate_left(.Out(rot_left), .In(In), .ShAmt(ShAmt));
    shift_left iShft_left(.Out(shft_left), .In(In), .ShAmt(ShAmt));
    rotate_right iRot_right(.Out(rot_right), .In(In), .ShAmt(ShAmt));
    shift_right_logical iShft_right(.Out(shft_right_log), .In(In), .ShAmt(ShAmt));
    
    assign Out = Oper[1] ? (Oper[0] ? shft_right_log : rot_right) : (Oper[0] ? shft_left : rot_left);
   
endmodule