Skip to content

Instantly share code, notes, and snippets.

@yunsu3042
Created March 26, 2018 10:55
Show Gist options
  • Save yunsu3042/f348d7a548521696b5ba06cb01449e3c to your computer and use it in GitHub Desktop.
Save yunsu3042/f348d7a548521696b5ba06cb01449e3c to your computer and use it in GitHub Desktop.
import Multiplexer::*;
interface BarrelShifterRight;
method ActionValue#(Bit#(64)) rightShift(Bit#(64) val, Bit#(6) shiftAmt, Bit#(1) shiftValue);
endinterface
module mkBarrelShifterRight(BarrelShifterRight);
method ActionValue#(Bit#(64)) rightShift(Bit#(64) val, Bit#(6) shiftAmt, Bit#(1) shiftValue);
/* TODO: Implement right barrel shifter using six multiplexer */
Bit#(64) shiftedVal;
for(Integer i = 0; i < 6; i = i + 1)
begin
for(Integer j = 0; j < 64 - 2 ** i; j = j + 1)
begin
shiftedVal[j] = val[j + 2 ** i];
end
for(Integer j = 0; j < 2 ** i; j = j + 1)
begin
shiftedVal[63 - j] = shiftValue;
end
val = multiplexer_n(shiftAmt[i], val, shiftedVal);
end
return val;
endmethod
endmodule
interface BarrelShifterRightLogical;
method ActionValue#(Bit#(64)) rightShift(Bit#(64) val, Bit#(6) shiftAmt);
endinterface
module mkBarrelShifterRightLogical(BarrelShifterRightLogical);
let bsr <- mkBarrelShifterRight;
method ActionValue#(Bit#(64)) rightShift(Bit#(64) val, Bit#(6) shiftAmt);
/* TODO: Implement logical right shifter using the right shifter */
let ans = bsr.rightShift(val, shiftAmt, 0);
let result <- ans;
return result;
endmethod
endmodule
typedef BarrelShifterRightLogical BarrelShifterRightArithmetic;
module mkBarrelShifterRightArithmetic(BarrelShifterRightArithmetic);
let bsr <- mkBarrelShifterRight;
method ActionValue#(Bit#(64)) rightShift(Bit#(64) val, Bit#(6) shiftAmt);
/* TODO: Implement arithmetic right shifter using the right shifter */
let ans = bsr.rightShift(val, shiftAmt, val[63]);
let result <- ans;
return result;
endmethod
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment