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.

Each amount bit controls a stage. AD shifted by 3 uses stages 1 and 2, bypasses 4, and gives 15
AD shifted by 3 uses stages 1 and 2, bypasses 4, and gives 15

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 atdinamounty
0 nsAD0AD
10 nsAD156
20 nsAD315
30 nsAD701
40 nsAD800
50 nsAD1500
Eight-bit data can keep one bit at shift 7. Shift 8 or more clears it.
The plot uses the VCD generated by the Icarus run.

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

    Read the transcript

    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)

    Check your understanding

    1. Which two fixed-shift stages are active for a distance of three?
    2. What is hexadecimal AD logically shifted right by three?
    3. What are the results of shifting eight-bit AD right by seven and by eight?
    4. If only the low three amount bits are wired, what happens for amount=8 and din=AD?
    5. Which two routes does each one-, two-, or four-place stage need?