Draft preview. Lessons are being written and reviewed.

Digital Design / Module 4: Sequential logic

25. Shift registers

Move and convert serial and parallel data.

Lecture coming soon

See it move

The idea

Chain flops so that each one's output feeds the next. On each clock, every bit moves along one position. It's simple, and it is used everywhere.

always_ff @(posedge clk)
  sr <= {sr[N-2:0], din};

Because the update is non-blocking, all the flops capture their old neighbour's value at the same instant, which gives a clean shift.

Common uses

  • Delay line: N stages delay a signal by N clocks, often used to align a control signal with a pipelined data path.
  • Serial in, parallel out (SIPO): shift in N bits, then read the whole word. A receiver works this way.
  • Parallel in, serial out (PISO): load a word, then shift it out one bit at a time. A transmitter works this way.
  • Pattern matching: compare the last N bits against a constant.

A parameterised SIPO

module sipo #(parameter N = 8) (
  input              clk, rst, din, en,
  output reg [N-1:0] q
);
  always_ff @(posedge clk)
    if (rst)     q <= '0;
    else if (en) q <= {q[N-2:0], din};
endmodule

The first bit in ends up at the top, so this shifts MSB first. A UART sends LSB first, so it would shift the other way: q <= {din, q[N-1:1]};

Hold timing

Shift registers have almost no logic between flops, so they are the classic location for hold problems. Clock between neighbouring flops can let data race through two stages at once. Physical design tools insert delay buffers to fix this.

Common mistake

Using = in the shift. As covered in the blocking lesson, this turns the whole chain into one flop in simulation, or sends the data through in a single clock.

Key ideas

  • Chain flops to delay by N cycles.
  • SIPO and PISO bridge serial links and parallel buses.
  • Non-blocking assignments keep ordering correct.

Your exercise

Write a parameterised SIPO.

Find the bug

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

What goes wrong: Only the lowest bit ever changes. The upper bits never pick up any data.

Interview questions

coreHow would you delay a signal by 8 cycles?

basicWhat are serial-in parallel-out and parallel-in serial-out shift registers used for?

basicThis register starts at 0 and the bits of 8'hA5 are shifted in MSB first, one per clock. What is q after four clocks?

always @(posedge clk)
  q <= {q[6:0], din};

coreThis three-stage shift register uses blocking assignments. After one clock with din = 1, what are q1, q2 and q3, and why is that wrong?

always @(posedge clk) begin
  q1 = din;
  q2 = q1;
  q3 = q2;
end

coreThis PISO is loaded with 8'hC1 and then shifts. Which bit order appears on tx, and how would you change it to send MSB first?

always @(posedge clk)
  if (load) p <= data;
  else      p <= {1'b0, p[7:1]};

assign tx = p[0];

coreWhy are shift registers a classic place for hold violations, and how are they normally fixed?

coreA control signal must stay aligned with data passing through a 4-stage pipeline. How do you do this, and what goes wrong if you get the count wrong?

advancedYou need to delay an 8-bit data stream by 1,000 cycles. Would you build 8,000 flops? What are the alternatives?

advancedTo detect the pattern 1101 in a serial stream, a designer keeps the last four bits in a shift register and compares them with 4'b1101. How does this compare with a state machine, including overlapping matches?

Practise these 9 questions with animations