Chapter 25 of 32

Registers & Shift Registers

One flip-flop stores one bit. But a processor works with whole words: an 8-bit character, a 32-bit integer, a 64-bit address. To hold a word we put several flip-flops side by side on a common clock, and we get a register. Connect the flip-flops in a chain so that each passes its bit to the next on every clock edge, and we get a shift register, the circuit that turns parallel data into serial data and back again. Every USB, UART, SPI and I²C link, and every keyboard and LED driver chip, depends on shift registers.

A register is a group of n flip-flops sharing a common clock (and usually a common reset), used to store an n-bit word. A shift register is a register whose flip-flops are connected in a chain so that the stored bits move one position on each clock edge.

This chapter uses the D flip-flop and asynchronous clear from Flip-Flops: SR, JK, D & T and the 2-to-1 and 4-to-1 multiplexers from Multiplexers. We cover registers with parallel load, the four shift-register types (SISO, SIPO, PISO, PIPO), bidirectional and universal shift registers, and the shift-register counters (ring and Johnson). Every timing table in this chapter is real output from SystemVerilog simulations run with Icarus Verilog.


The Basic Register

The simplest register is n D flip-flops with a common clock. On every rising edge each flip-flop copies its own input:

   I3          I2          I1          I0        (parallel inputs)
   |           |           |           |
 +-v-+       +-v-+       +-v-+       +-v-+
 | D |       | D |       | D |       | D |
 |   |       |   |       |   |       |   |
 |>  |       |>  |       |>  |       |>  |
 +-+-+       +-+-+       +-+-+       +-+-+
   |  ^        |  ^        |  ^        |  ^
   Q3 |        Q2 |        Q1 |        Q0 |      (parallel outputs)
      |           |           |           |
CLK --+-----------+-----------+-----------+
CLR′ --- to the asynchronous clear of all four (not drawn)

This is a PIPO (parallel-in, parallel-out) register, also called a buffer register. Its weakness: it loads new data on every clock edge. Usually we want it to load only when told to.

Register with Parallel Load

Add a LOAD control. Do not gate the clock (an AND gate on the clock line adds skew and can produce glitches, the problem described in Hazards in Combinational Circuits). Instead, put a 2-to-1 multiplexer in front of each D input that chooses between the new data and the flip-flop's own output:

D_i = LOAD · I_i  +  LOAD′ · Q_i

LOAD = 1 → D_i = I_i  → new data loaded at the clock edge
LOAD = 0 → D_i = Q_i  → each flip-flop reloads its own value (hold)
        I_i ---->|1  2:1 |
                 |  MUX  |----> D_i --->[ D FF ]---+---> Q_i
     +---------->|0      |              (CLK)      |
     |           +---+---+                         |
     |              LOAD                           |
     +---------------------------------------------+

All flip-flops still see every clock edge, so the timing stays clean. This is exactly what an HDL synthesis tool produces for:

always_ff @(posedge clk)
  if (load) q <= d;      // no else: hold, implemented with the feedback MUX (or a flip-flop enable)

The registers you met in a CPU (accumulator, program counter, instruction register, MAR and MDR) are registers of this kind, with extra controls such as increment or clear.


Shift Registers

Connect the output of each flip-flop to the D input of the next. On each clock edge every bit moves one place, a new bit enters at one end, and the bit at the other end leaves.

Serial in                                              Serial out
   ---->[D FF3]--Q3-->[D FF2]--Q2-->[D FF1]--Q1-->[D FF0]--Q0---->
           ^             ^              ^              ^
CLK -------+-------------+--------------+--------------+

At each rising edge:  Q3 ← Serial in,  Q2 ← Q3,  Q1 ← Q2,  Q0 ← Q1

Why does this work without the bits "racing" through the whole chain in one edge? Because these are edge-triggered flip-flops: each one samples its input at the edge, and the new outputs appear only tcq later, after the sampling is over. (With transparent latches, the bit would race straight through, the problem we saw at the end of Latches.) For this to work the flip-flop's hold time must be smaller than its clock-to-Q delay, which standard flip-flops guarantee.

Four Types by Input and Output

Type

Data in

Data out

Clocks to move an n-bit word

Typical use

SISO

Serial (1 bit per clock)

Serial

n to load; 2n − 1 to load and read out completely

Digital delay line

SIPO

Serial

Parallel (all bits at once)

n

Serial receivers (UART, SPI), LED drivers (74HC595, 74HC164)

PISO

Parallel

Serial

1 to load, then n − 1 shifts

Serial transmitters, reading many buttons with few pins (74HC165)

PIPO

Parallel

Parallel

1

Storage register, pipeline register


SISO and SIPO: Serial In

Here is a 4-bit serial-in shift register. Data enters at Q3 and moves right; Q0 is the serial output, and Q3 to Q0 together are the parallel output. We send the word 1101 LSB first (bits 1, 0, 1, 1) and then feed 0s:

`timescale 1ns/1ns
// 4-bit serial-in shift register (SISO / SIPO): data enters at Q3 and moves right
module sipo4(input logic CLK, SIN, output logic [3:0] Q);
  always_ff @(posedge CLK) Q <= {SIN, Q[3:1]};
endmodule

module tb;
  logic CLK = 0, SIN = 0;
  logic [3:0] Q;
  logic [3:0] word = 4'b1101;        // sent LSB first: 1, 0, 1, 1
  sipo4 u(CLK, SIN, Q);
  always #5 CLK = ~CLK;
  initial begin
    u.Q = 4'b0000;
    $display("clock  SIN  Q3 Q2 Q1 Q0   serial out (Q0)");
    $display("  0     -    %b  %b  %b  %b      %b", Q[3], Q[2], Q[1], Q[0], Q[0]);
    for (int i = 0; i < 7; i++) begin
      @(negedge CLK) SIN = (i < 4) ? word[i] : 1'b0;
      @(posedge CLK) #1 $display("  %0d     %b    %b  %b  %b  %b      %b", i+1, SIN, Q[3], Q[2], Q[1], Q[0], Q[0]);
    end
    $finish;
  end
endmodule

Output:

clock  SIN  Q3 Q2 Q1 Q0   serial out (Q0)
  0     -    0  0  0  0      0
  1     1    1  0  0  0      0
  2     0    0  1  0  0      0
  3     1    1  0  1  0      0
  4     1    1  1  0  1      1
  5     0    0  1  1  0      0
  6     0    0  0  1  1      1
  7     0    0  0  0  1      1

Follow the first bit (a 1): it enters Q3 at clock 1 and reaches Q0 at clock 4. After 4 clocks the parallel outputs read Q3 Q2 Q1 Q0 = 1101, the complete word: that is the SIPO use, serial-to-parallel conversion in n clocks.

Used as a SISO, we read only Q0. The first bit appears at Q0 after clock 4, and the remaining three bits come out at clocks 5, 6 and 7 (0, 1, 1). So the serial output sequence is 1, 0, 1, 1, the same order the bits went in, delayed by 4 clocks. To push a whole n-bit word in and get it all out serially takes n + (n − 1) = 2n − 1 clocks (7 here). A SISO register is therefore a delay line: an n-stage SISO delays a serial stream by n clock periods.


PISO: Parallel In, Serial Out

A PISO register needs two modes: load all bits at once, then shift them out one per clock. A 2-to-1 MUX in front of each flip-flop selects between the parallel input and the neighbour's output:

D3 = LOAD·P3 + LOAD′·0        (0 shifted into the leftmost stage)
D2 = LOAD·P2 + LOAD′·Q3
D1 = LOAD·P1 + LOAD′·Q2
D0 = LOAD·P0 + LOAD′·Q1       serial output = Q0
`timescale 1ns/1ns
// 4-bit parallel-in serial-out shift register
// LOAD = 1: Q <= P (parallel load);  LOAD = 0: shift right, serial output is Q[0]
module piso4(input logic CLK, LOAD, input logic [3:0] P, output logic SOUT);
  logic [3:0] Q;
  always_ff @(posedge CLK)
    if (LOAD) Q <= P;
    else      Q <= {1'b0, Q[3:1]};
  assign SOUT = Q[0];
endmodule

module tb;
  logic CLK = 0, LOAD = 1, SOUT;
  logic [3:0] P = 4'b0110;
  piso4 u(CLK, LOAD, P, SOUT);
  always #5 CLK = ~CLK;
  initial begin
    @(posedge CLK) #1 $display("after load:   Q=%b  SOUT=%b", u.Q, SOUT);
    LOAD = 0;
    repeat (3) @(posedge CLK) #1 $display("after shift:  Q=%b  SOUT=%b", u.Q, SOUT);
    $finish;
  end
endmodule

Output:

after load:   Q=0110  SOUT=0
after shift:  Q=0011  SOUT=1
after shift:  Q=0001  SOUT=1
after shift:  Q=0000  SOUT=0

The serial output reads 0, 1, 1, 0, which is P0, P1, P2, P3: the word 0110 sent LSB first. One clock to load, then n − 1 = 3 shifts to present the remaining bits. This is how a UART transmitter turns a byte into a stream of bits on one wire (it also adds start and stop bits).


Shifting as Arithmetic

Shifting a binary number one place left multiplies it by 2 (if no 1 falls off the left end); shifting it right divides it by 2, discarding the remainder.

0110 (6)  shift left, 0 in  → 1100 (12)
0110 (6)  shift right, 0 in → 0011 (3)
0111 (7)  shift right, 0 in → 0011 (3)    remainder 1 is lost

For signed (2's complement) numbers, an arithmetic right shift copies the sign bit into the leftmost position instead of a 0: 1010 (−6) → 1101 (−3). A shift register therefore doubles as a simple multiplier and divider, which is used in the shift-and-add multiplication methods you meet in computer organisation courses.


Bidirectional and Universal Shift Registers

A bidirectional shift register can shift left or right, chosen by a control input. Each stage gets a 2-to-1 MUX that picks its left neighbour (for shift right) or its right neighbour (for shift left).

A universal shift register goes further and can hold, shift right, shift left or load in parallel. Each stage has a 4-to-1 MUX controlled by two select lines S1 S0. The classic IC is the 74HC194 (4-bit bidirectional universal shift register), whose mode table is:

S1

S0

Mode

Next state of stage i (stage 3 is leftmost here)

0

0

Hold

Qi

0

1

Shift right

Qi+1 (stage 3 takes the shift-right serial input)

1

0

Shift left

Qi−1 (stage 0 takes the shift-left serial input)

1

1

Parallel load

Pi

One stage of a universal shift register:

           Q_i      -->| 0          |
           Q_(i+1)  -->| 1  4-to-1  |
           Q_(i-1)  -->| 2   MUX    |----> D_i --->[ D FF ]---> Q_i
           P_i      -->| 3          |
                       +-----+------+
                          S1 S0

(In the data sheet the outputs are named QA to QD and "shift right" moves data from QA towards QD; the idea is the same, only the labels differ.)

`timescale 1ns/1ns
// 4-bit universal shift register, mode select like the 74HC194
// S1 S0 = 00 hold, 01 shift right, 10 shift left, 11 parallel load
module univ_shift4(input  logic       CLK, CLR_n,
                   input  logic [1:0] S,
                   input  logic       SR_in, SL_in,   // serial inputs
                   input  logic [3:0] P,              // parallel inputs P[3]..P[0]
                   output logic [3:0] Q);             // Q[3] is the leftmost bit
  always_ff @(posedge CLK, negedge CLR_n)
    if (!CLR_n) Q <= 4'b0000;
    else case (S)
      2'b00: Q <= Q;                      // hold
      2'b01: Q <= {SR_in, Q[3:1]};        // shift right: new bit enters on the left
      2'b10: Q <= {Q[2:0], SL_in};        // shift left: new bit enters on the right
      2'b11: Q <= P;                      // parallel load
    endcase
endmodule

module tb;
  logic CLK = 0, CLR_n = 1, SR_in = 0, SL_in = 0;
  logic [1:0] S = 0;
  logic [3:0] P = 0, Q;
  univ_shift4 u(CLK, CLR_n, S, SR_in, SL_in, P, Q);
  always #5 CLK = ~CLK;
  task automatic step(input logic [1:0] s, input logic sr, sl, input logic [3:0] p, input string what);
    @(negedge CLK) begin S = s; SR_in = sr; SL_in = sl; P = p; end
    @(posedge CLK) #1 $display("%-24s S1S0=%b  Q=%b", what, S, Q);
  endtask
  initial begin
    #1 CLR_n = 0;      // pulse the asynchronous clear
    #1 CLR_n = 1;
    #1;
    $display("after clear              Q=%b", Q);
    step(2'b11, 0, 0, 4'b1011, "parallel load 1011");
    step(2'b00, 0, 0, 4'b0000, "hold");
    step(2'b01, 0, 0, 4'b0000, "shift right, in 0");
    step(2'b01, 1, 0, 4'b0000, "shift right, in 1");
    step(2'b10, 0, 0, 4'b0000, "shift left, in 0");
    step(2'b10, 0, 1, 4'b0000, "shift left, in 1");
    $finish;
  end
endmodule

Output:

after clear              Q=0000
parallel load 1011       S1S0=11  Q=1011
hold                     S1S0=00  Q=1011
shift right, in 0        S1S0=01  Q=0101
shift right, in 1        S1S0=01  Q=1010
shift left, in 0         S1S0=10  Q=0100
shift left, in 1         S1S0=10  Q=1001

Trace the shifts by hand to check: 1011 shifted right with 0 entering on the left gives 0101; shifted right again with 1 entering gives 1010; shifted left with 0 entering on the right gives 0100; shifted left with 1 entering gives 1001. Because the universal register can do all four operations, it can work as any of SISO, SIPO, PISO or PIPO.


Shift-Register Counters: Ring and Johnson

Feed the serial output back to the serial input and a shift register becomes a counter that cycles through a fixed pattern. These are studied together with other counters in Synchronous Counters; here is the essential idea.

  • Ring counter: Q0 is fed back to the first stage. Start it with a single 1 (1000) and the 1 circulates. An n-bit ring counter has n states, and each state has exactly one 1, so no decoder is needed ("one-hot").

  • Johnson (twisted-ring) counter: Q0′ (the complement) is fed back. Starting from 0000 it fills with 1s and then empties. An n-bit Johnson counter has 2n states, and each state can be decoded with a single 2-input gate.

clock  ring  johnson
  0    1000  0000
  1    0100  1000
  2    0010  1100
  3    0001  1110
  4    1000  1111
  5    0100  0111
  6    0010  0011
  7    0001  0001
  8    1000  0000

(Real simulation output of 4-bit ring and Johnson counters with the initial states shown.) The ring counter repeats after 4 clocks; the Johnson counter after 8. Both must be started in a valid state (with preset/clear), because an n-bit register has 2n possible states and the unused ones form other cycles.


Real-World Uses

  • Serial communication: a UART or SPI transmitter is a PISO; the receiver is a SIPO. One wire carries a whole byte, one bit per clock.

  • Saving microcontroller pins: a 74HC595 (8-bit SIPO with an output storage register) lets three pins (data, shift clock, latch clock) drive eight LEDs, and several 595s can be chained for more. A 74HC165 (8-bit PISO) does the reverse for reading eight buttons.

  • Pipelines: each pipeline stage in a processor ends in a PIPO register.

  • Sequence generation and CRC/scrambling: shift registers with XOR feedback (linear-feedback shift registers) generate pseudo-random sequences and compute checksums.

  • Delay lines: an n-stage SISO delays a bit stream by n clock periods.


Common Mistakes

  • Gating the clock to implement LOAD. Use a MUX (or the flip-flop's enable) on the D input; keep the clock clean.

  • Miscounting serial clocks. Loading n bits serially takes n clocks; getting all of them out of a SISO after that takes n − 1 more (the first bit is already at the output), so 2n − 1 in total.

  • Mixing up shift directions. "Right" and "left" depend on how the stages are drawn and labelled. Always state which end is the MSB and where the serial input enters.

  • Forgetting the sign bit in an arithmetic right shift. Shifting 1010 (−6) right with a 0 entering gives 0101 (+5), not −3.

  • Saying an n-bit Johnson counter has n states. It has 2n; the ring counter has n.


Interview and GATE-Style Questions

How many clock pulses are needed to enter an 8-bit word into an 8-bit serial-in shift register, and how many more to read it all out serially?

8 to enter it. After that the first bit is already at the serial output, so 7 more clock pulses bring out the remaining bits: 15 in total (2n − 1).

A 4-bit shift register (serial input on the left, shifting right) contains 1100. The serial input sequence is 1, 0, 1 (first bit first). What are the contents after three clocks?

Clock 1: 1110. Clock 2: 0111. Clock 3: 1011.

What is the difference between a ring counter and a Johnson counter? How many states does each have with 5 flip-flops?

A ring counter feeds Q of the last stage back to the first; a Johnson counter feeds Q′ of the last stage back. With 5 flip-flops: ring counter 5 states, Johnson counter 10 states.

Why is a register with LOAD control built with a multiplexer rather than by ANDing the clock with LOAD?

Gating the clock delays it (clock skew) and a glitch on LOAD can create an extra clock edge, corrupting the register. With a MUX on the D input, every flip-flop sees the same clean clock and simply reloads its own value when LOAD = 0.

Which shift-register type is used in a UART receiver, and which in a UART transmitter?

Receiver: SIPO (bits arrive one at a time and the byte is read out in parallel). Transmitter: PISO (the byte is loaded in parallel and shifted out one bit at a time).

An 8-bit register holds 00010110 (22). What does it hold after one logical left shift and after two logical right shifts of the original value?

Left shift: 00101100 (44). Two right shifts: 00000101 (5), which is 22 divided by 4 with the remainder discarded.


Summary

  • A register is n flip-flops on a common clock storing an n-bit word. A per-bit 2-to-1 MUX gives it a LOAD/HOLD control without gating the clock.

  • A shift register chains flip-flops so bits move one place per clock. By how data enters and leaves: SISO (delay line), SIPO (serial-to-parallel), PISO (parallel-to-serial), PIPO (storage).

  • Serial loading takes n clocks; SISO in-and-out takes 2n − 1.

  • A universal shift register (like the 74HC194) uses a 4-to-1 MUX per stage to hold (00), shift right (01), shift left (10) or load (11).

  • Shifting left/right multiplies/divides by 2; feeding the output back gives ring (n states) and Johnson (2n states) counters.

Next, in Asynchronous Counters, we chain toggle flip-flops so that each one clocks the next, and build circuits that count clock pulses.