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.
sin into bit 0 and moves every other bit one place up.- I/O
- Storage
A slot lights when a received bit sits in it.
- Cycle 0
After reset
qis 0000. The first bit, the MSB, is 1. - Cycle 1
The edge took the 1 into bit 0. The next bit is 0.
- Cycle 2
The 1 moved up to bit 1, and the 0 entered bit 0.
- Cycle 4
After four edges
qis 1011. The first bit received sits in bit 3. - Cycle 5
shiftis 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.
| Form | Bits in | Bits out | Typical use |
|---|---|---|---|
| SISO: serial in, serial out | one per edge | one per edge | a delay line |
| SIPO: serial in, parallel out | one per edge | all at once | receive a serial word |
| PISO: parallel in, serial out | all at once (load) | one per edge | send a word on one wire |
| PIPO: parallel in, parallel out | all at once | all at once | a 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.
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.
| mode | Action at the edge | Code (4 bits, q[3] on the left) |
|---|---|---|
| 00 | hold | q <= q; |
| 01 | shift right: sr_in enters at the left | q <= {sr_in, q[3:1]}; |
| 10 | shift left: sl_in enters at the right | q <= {q[2:0], sl_in}; |
| 11 | load d in parallel | q <= 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
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.
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.
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.
Synthesize the design to measure its cells and inspect the mapped gates.
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=1011Original 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
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.