Draft preview. Lessons are being written and reviewed.

Digital Design / Module 2: Combinational building blocks

11. Barrel shifters and glitches

Know why combinational outputs can pulse before settling.

Lecture coming soon

See it move

Barrel shifters

A barrel shifter moves a word by any amount in one cycle. It would be costly to build a giant mux for each output bit, so we use stages. Each stage shifts by a power of two, or passes the word through, according to one bit of the shift amount. An 8-bit shifter has three stages: shift by 1, 2 and 4. A shift of 5 means stages 1 and 3 are active. That gives log2(N) levels of muxes, with N muxes in each.

Shifting left by k multiplies by 2^k. Logical right shift fills with zeros, whereas arithmetic right shift copies the sign bit, so it divides a signed value by 2^k, rounding towards negative infinity.

Why outputs glitch

Paths through a circuit have different delays, so while inputs change the output may briefly show wrong values. A static hazard is a brief pulse when the output should stay constant. A dynamic hazard is several transitions when it should change just once.

Take F = AB + A'C with B = C = 1. F should be 1 whatever A does. But when A falls, AB turns off immediately, whereas A'C needs an extra inverter delay to turn on. For a moment both terms are 0, and F dips.

Why this usually doesn't matter

A flop samples only at the clock edge. If timing is met, all glitches have died away before the edge, and synchronous design relies on exactly this. The glitch still costs some power, however, since it charges and discharges capacitance.

When it matters a lot

Never use combinational logic as a clock or an asynchronous reset. Any glitch there is seen as a real edge, with immediate effect. Likewise, signals crossing into another should come straight from a flop.

Common mistake

Adding consensus terms to "fix" glitches in ordinary synchronous logic. The synthesis tool may remove them anyway, and they aren't needed. Fix the structure: register the signal before it goes anywhere sensitive.

One more to try

Key ideas

  • A barrel shifter uses log2(N) mux stages.
  • Unequal path delays cause static and dynamic hazards.
  • Registers hide glitches, provided timing is met; never clock from raw combinational logic.

Your exercise

Sketch an 8-bit barrel shifter.

Find the bug

This code has one deliberate bug. Read the symptom, then click the line you think is wrong.

What goes wrong: Positive numbers divide correctly, but -8 shifted right by 1 gives 124 instead of -4.

Interview questions

coreWhy not use combinational output as a clock?

basicIn an 8-bit barrel shifter with stages for shifts of 1, 2 and 4, which stages are active for a shift of 6?

basicWhat does shifting an unsigned value left by 3 do arithmetically, and when does it go wrong?

coreHow many 2:1 muxes and stages does a 32-bit barrel shifter need?

coreWhat do u >>> 2 and s >>> 2 give for these two registers?

reg        [7:0] u = 8'hF0;
reg signed [7:0] s = 8'shF0;
// u >>> 2 ?   s >>> 2 ?

coreA signed 8-bit register holds -7. What is -7 >>> 1, and is that the same as -7 / 2 in Verilog?

coreF = AB + A'C with B = C = 1. Explain the glitch when A falls from 1 to 0, and how a consensus term would remove it.

advancedHow does this code rotate an 8-bit value left by a variable amount?

wire [7:0]  x;
wire [2:0]  amt;
wire [15:0] t   = {x, x} << amt;
wire [7:0]  rot = t[15:8];

advancedThis counter is meant to count 0 to 9. What is wrong with it?

wire clr_n = ~(cnt == 4'd9);
always @(posedge clk or negedge clr_n)
  if (!clr_n) cnt <= 4'd0;
  else        cnt <= cnt + 4'd1;

Practise these 9 questions with animations