16-bit Arithmetic Barrel Shifter
A barrel shifter shifts by any amount in one cycle, which is how DSPs multiply and divide by powers of two. This module accepts a 16-bit input and shifts it left or right by 0 to 15 positions based on a 4-bit shift amount.
When shifting right, the operation can be either logical or arithmetic. A logical right shift pads the most significant bits with zeros. An arithmetic right shift pads the most significant bits with the original sign bit (the most significant bit of the input) to preserve the sign of two's complement numbers. Left shifts always pad the least significant bits with zeros, regardless of the arithmetic flag. The design must compute the result combinationally.
| Signal | Direction | Width | Description |
|---|---|---|---|
data_in |
input | 16 | Input data word to be shifted. |
shift_amt |
input | 4 | Number of bit positions to shift (0 to 15). |
dir |
input | 1 | Shift direction; 0 indicates left shift, 1 indicates right shift. |
arith_flag |
input | 1 | Shift type; 0 indicates logical shift, 1 indicates arithmetic shift. Applies to right shifts only. |
data_out |
output | 16 | Shifted result. Purely combinational output. |
Constraints
- The design must be purely combinational. No clocks, resets, or sequential logic are permitted.
- Do not use sequential loops that require multiple clock cycles to evaluate.
- When
diris 0 (left shift), thearith_flagmust be ignored, and the least significant bits must be padded with zeros. - When
diris 1 (right shift) andarith_flagis 1, the replicated sign bit must matchdata_in[15]. - Shift amounts range from 0 to 15; no saturation logic is required for over-shifting.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
Free to solve. A Codiode account keeps your progress.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Datapath Design
Problem 4 of 27 in the Datapath Design track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks