Barrel Shifter

Medium Verilog Optimization Asked at Apple
Solve this problem

Implement a combinational 8-bit barrel shifter that can shift left or right by 0 to 7 positions in a single cycle, with a logical or arithmetic right shift.

Constraints

  • Purely combinational - no clock.
  • Direction and arithmetic-mode select inputs.
  • Shift amount is 3 bits.
  • Arithmetic right shift must replicate the sign bit.
  • Implement as a multiplexer tree, not a shift loop.
  • No for-loop-based sequential shifting.
Starter codeVerilog
1module barrel_shifter (2    input wire [7:0] din,3    input wire [2:0] amount,4    input wire       right,5    input wire       arith,6    output wire [7:0] dout7);8// TODO9endmodule