Variable shift
Drive the shift amount from an input and trace how its bits select distances through mux stages. For an unsigned logical right shift, test zero, the last in-range distance and a distance at least as large as the data width. Then inspect the structure the tool synthesizes.
The collector lets a separate signal choose the shift distance. Its eight-bit input is hexadecimal AD. Sometimes the whole word should pass through, and sometimes only the top bit should survive. The current amount chooses the distance.
Build the distance with three selectable stages
For eight-bit data, shifts from zero through seven can be composed from shifts of one, two, and four. amount[0] selects a shift of one. amount[1] can add two places to the previous result. amount[2] can add four. Each mux selects the shifted value or bypasses that stage. A distance of three is 0011: AD becomes 56, then 15 after two more places, and the four-place stage passes it through.
If amount[3]=1, the distance is at least eight. This circuit does unsigned logical right shift. Every original bit moves out, so the output is 00. Dropping the high amount bit would make eight look like zero. The wrong circuit would return AD. Test seven and eight together to catch the boundary error.
Synthesizable SystemVerilog
module variable_shift8 (
input logic [7:0] din,
input logic [3:0] amount,
output logic [7:0] y
);
logic [7:0] stage1, stage2, stage4;
assign stage1 = amount[0] ? din >> 1 : din;
assign stage2 = amount[1] ? stage1 >> 2 : stage1;
assign stage4 = amount[2] ? stage2 >> 4 : stage2;
assign y = amount[3] ? 8'b0 : stage4;
endmodule
Check the results against the waveform
The input and output are hexadecimal. The shift distance is decimal.
| Change at | din | amount | y |
|---|---|---|---|
| 0 ns | AD | 0 | AD |
| 10 ns | AD | 1 | 56 |
| 20 ns | AD | 3 | 15 |
| 30 ns | AD | 7 | 01 |
| 40 ns | AD | 8 | 00 |
| 50 ns | AD | 15 | 00 |
Try din=80. Shifting right by seven moves the top bit to bit zero and gives 01. Shifting by eight clears it. The three shift stages are on a combinational path with no registers between them.
Download the runnable SystemVerilog lab. Icarus checks the 6 rows above and 4,096 binary input combinations. It reads each directed result 1 ns after changing the inputs. The wait sets an observation time in the testbench. The waveform is functional evidence. Physical delay needs a timing model.
Interactive lab
Start with AD and try distances 3, 7, and 8. Change the input to 80 and find the last bit that can survive.
- Output (hex)
Show the last eight observations
Teaching film
Run the RTL yourself
Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.
Download Variable shift RTL (.zip)