Shift Registers and Serial-Parallel Conversion

Move bits one place per clock edge. Turn a serial stream into a word and back, and pick the shift direction for the bit order.

You Will Be Able To

  • Name the four shift-register forms: SISO, SIPO, PISO and PIPO.
  • Write a shift register that receives a word MSB first or LSB first.
  • Read the mode table of a universal shift register.
  • Use a shift register as a delay line of N clock cycles.
Lesson
G6.2
Time
25 min
Runs on
Your browser
Check
5 questions

A temperature sensor has one data pin. It sends a reading one bit per clock edge. The chip that receives it must rebuild the word. A row of flip-flops that pass bits along does that.

Four Bits Arrive, MSB FirstEach cycle ends at a rising edge. The edge takes sin into bit 0 and moves every other bit one place up.
  • I/O
  • Storage
sinqSensorone data pinsipo4four flip-flopsq0q1q2q3Readerreads q
sin→ sipo4q→ ReaderSensorone data pinsipo4four flip-flopsq0q1q2q3Readerreads q

A slot lights when a received bit sits in it.

012345clkshiftsinq00000001001001011011filled00000001001101111111
Scroll horizontally ↔
  1. Cycle 0

    After reset q is 0000. The first bit, the MSB, is 1.

  2. Cycle 1

    The edge took the 1 into bit 0. The next bit is 0.

  3. Cycle 2

    The 1 moved up to bit 1, and the 0 entered bit 0.

  4. Cycle 4

    After four edges q is 1011. The first bit received sits in bit 3.

  5. Cycle 5

    shift is 0, so the word stays put.

Measured: the values are the lines the lab below prints.

Four Ways to Move Bits

A shift register is a row of flip-flops on one clock. At each edge every bit moves one place, and a new bit enters at one end.

FormBits inBits outTypical use
SISO: serial in, serial outone per edgeone per edgea delay line
SIPO: serial in, parallel outone per edgeall at oncereceive a serial word
PISO: parallel in, serial outall at once (load)one per edgesend a word on one wire
PIPO: parallel in, parallel outall at onceall at oncea register; shifting moves bits sideways

Serial means one bit per clock edge on one wire. Parallel means every bit on its own wire.

In SystemVerilog a shift is one concatenation. {q[2:0], sin} drops the top bit, moves the rest up and puts sin at the bottom.

Serial in, Parallel Out

The lab receives the word 1011, MSB first. After four edges the whole word sits in q, ready to read in parallel.

The first bit received ends at the top, so MSB-first data lands in the right places.
always_ff @(posedge clk)
  if (rst)        q <= 4'b0000;
  else if (shift) q <= {q[2:0], sin};

Parallel in, Serial Out

To send a word, load it in one edge, then shift it out one bit per edge. The serial output is the bit at the end that leaves first.

A transmitter and a receiver must agree on the bit order. Here the MSB went first, so the receiver of the lab rebuilds the same word.

The Universal Shift Register

A universal shift register can hold, shift either way or load. A 2-bit mode input picks one action per edge. The 74x194 chip works this way.

modeAction at the edgeCode (4 bits, q[3] on the left)
00holdq <= q;
01shift right: sr_in enters at the leftq <= {sr_in, q[3:1]};
10shift left: sl_in enters at the rightq <= {q[2:0], sl_in};
11load d in parallelq <= d;

With one mode input there is never a question of which action wins.

MSB First or LSB First

The order on the wire decides the shift direction. Send 1011 LSB first and the bits arrive as 1, 1, 0, 1.

MSB first: shift up

q <= {q[2:0], sin};   // new bit in q[0]

The first bit climbs to q[3], the top. Right for MSB-first data.

LSB first: shift down

q <= {sin, q[3:1]};   // new bit in q[3]

The first bit sinks to q[0], the bottom. Right for LSB-first data.

Ethernet sends each byte of a frame LSB first, except the frame check sequence. Its MDIO management bus sends fields MSB first. Read the bit order in the standard before you write the shift.

A Shift Register as a Delay Line

A SISO register of N stages gives back its input N clock edges later. Pipelines use this to keep a control bit in step with data.

Write it as one concatenation: dly <= {dly[1:0], d}; makes three stages, and dly[2] is d from three edges ago.

Asked in interviews
  1. How do you convert serial data to parallel?

    Shift each bit into a register at its clock edge. After N edges the register holds the N-bit word. A counter marks when the word is complete.

  2. What is a universal shift register?

    A register that can hold, shift left, shift right or load in parallel, chosen by a mode input at each clock edge. The 74x194 is the classic example.

  3. How many cycles does a shift register delay a signal?

    One clock edge per stage. A chain of N flip-flops gives the input back N cycles later.

Try It

Each line shows the inputs one edge sampled and the q it produced. The 1011 climbs in one bit per edge.

After the fourth edge q is 1011, the word that was sent. With shift at 0 the register holds.

Change the shift to q <= {sin, q[3:1]}; and run again. The same bits now land as 1101: the order is reversed.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: shift=1 sin=1 q=0001 shift=1 sin=0 q=0010 shift=1 sin=1 q=0101 shift=1 sin=1 q=1011 shift=0 sin=0 q=1011

Original design · 4 cells · 4 flip-flops · 0 latches · 0 logic levels. Synthesis uses Yosys; counts depend on the toolchain.

Read included file: tb.sv
// tb.sv: sends 1011, MSB first, one bit per rising edge, then holds (read-only).
`timescale 1ns/1ns
module tb;
  logic       clk = 0, rst = 1, shift = 0, sin = 0;
  logic [3:0] q;
  sipo4 dut (.clk(clk), .rst(rst), .shift(shift), .sin(sin), .q(q));
  task automatic tick(input logic s, b);
    shift = s; sin = b;
    #5 clk = 1; #5 clk = 0;
    $display("shift=%b sin=%b  q=%b", shift, sin, q);
  endtask
  initial begin
    #5 clk = 1; #5 clk = 0; rst = 0;     // reset edge: q = 0000
    tick(1, 1);                          // bit 3 (the MSB)
    tick(1, 0);                          // bit 2
    tick(1, 1);                          // bit 1
    tick(1, 1);                          // bit 0 (the LSB): q holds the whole word
    tick(0, 0);                          // shift = 0: hold
  end
endmodule

Check Yourself

Spot the bugEach option is the clocked block of an 8-bit receiver. The bits arrive LSB first, one per edge. After eight edges q must equal the word sent. Which block fails?
Predict the outputA three-stage delay line starts at 000. The input is 1, 1, 0, 1, 0, 0, one bit per edge. The program records the last stage after each edge. Predict the exact line.
module tb;
  logic       clk = 0, d;
  logic [2:0] dly = 3'b000;
  string      s = "";
  always_ff @(posedge clk) dly <= {dly[1:0], d};   // three stages
  initial begin
    for (int k = 0; k < 6; k++) begin
      d = (6'b110100 >> (5 - k)) & 1;               // d = 1, 1, 0, 1, 0, 0
      #1 clk = 1; #1 clk = 0;
      s = {s, dly[2] ? "1" : "0"};
    end
    $display("%s", s);
  end
endmodule
Read the waveformA 4-bit SIPO register starts at 0000 and shifts up, new bit in bit 0. In which cycle does q first show 0101?
A SIPO register
0123clksinq0000000100100101
Scroll horizontally ↔

Measured in the lab above: cycles 0 to 3 of its trace.

ConceptA universal shift register has mode 11 at an edge. What does it do?
ConceptA control bit must reach the end of a datapath in the same cycle as data that passes through 5 pipeline stages. What do you put on the control path?

Common Mistakes

Wrong Shift toward the MSB when the bits arrive LSB first.

The first bit then ends in the top place, and the word comes out with its bits reversed.

Fix MSB first: shift up, new bit into bit 0. LSB first: shift down, new bit into the top bit.

Wrong Let load and shift both be 1 in one cycle with no rule for which wins.

Two if statements that both assign the register give "the last one wins", which nobody chose on purpose.

Fix Write one if/else if chain, or one mode input, so the priority is in the code.

Wrong Count to 8 when a word has 8 bits and the counter starts at 0.

Values 0 to 7 already make eight edges. Waiting for 8 takes one bit too many.

Fix Mark the word complete on the edge that takes the bit with count 7.

Wrong Write each stage of a chain with = in its own line of one block.

The later lines read values that changed a moment earlier, and the chain collapses (G6.1).

Fix Use <=, or one concatenation such as q <= {q[2:0], sin};.

Remember

  • A shift register moves every bit one place per edge; one concatenation writes it.
  • SIPO receives a serial word; PISO sends one; SISO is a delay line; PIPO is a plain register.
  • A universal shift register picks hold, shift left, shift right or load with a mode input.
  • The bit order on the wire decides the shift direction: MSB first shifts up, LSB first shifts down.

Practice

Report a problem with this page