C731 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 31 · Shift registers, LFSRs and CRCs

Moving bits in a line, and the sequences a little feedback makes

Chain flip-flops so each one's input is the previous one's output, and on every clock edge the bits march one place along: a shift register. That's how bits cross a serial link one at a time and turn back into bytes at the other end. Feed a couple of the bits back through an XOR gate and the register starts producing a long sequence that looks random but repeats exactly: a linear-feedback shift register. LFSRs generate test patterns and scramble data on every high-speed link, and the same circuit computes the CRC that checks every Ethernet frame and file you download.

  • A 4-bit LFSR: on each clock the bits shift from Q0 toward Q3, and the new Q0 is the XNOR of Q3 and Q2.
  • Read the four bits as a number on each clock: 0000, 0001, 0011, 0111, 1110, ... It visits 15 different states before repeating, every state except 1111.
  • Four flip-flops and one gate produce a 15-step pseudo-random sequence; 32 flip-flops would give over four billion steps.

Predict

A 16-bit LFSR with the right feedback taps cycles through how many states before repeating?

Name it

Shift registers

  • Serial in, parallel out (SIPO): bits arrive one per clock and, after n clocks, the n-bit word is available at once: a serial receiver.
  • Parallel in, serial out (PISO): load a word, then shift it out one bit per clock: a transmitter.
  • At a clock of f, n bits take n/f to cross.

Counters from shift registers

  • Ring counter: the last output fed back to the first, with a single 1 circulating: n states from n flip-flops, but each state decodes with no logic.
  • Johnson counter: the last output inverted and fed back: 2n states from n flip-flops (0000, 1000, 1100, 1110, 1111, 0111, ...).

Linear-feedback shift registers

The feedback is the XOR of selected bits, the taps. With taps matching a primitive polynomial (for 4 bits, x4+x3+1: taps 4 and 3), the sequence has the maximal length 2n−1. Each output bit looks random (as many 1s as 0s, give or take one), but the sequence is completely predictable from the state. Uses: pseudo-random test patterns (built-in self-test), scramblers that keep serial links from sending long runs of 0s, and spread-spectrum codes.

CRCs

A cyclic redundancy check treats a message as a polynomial over bits and divides it by a fixed generator polynomial, using XOR in place of subtraction; the remainder, n bits for a degree-n generator, is appended. The receiver divides again and finds a remainder of zero unless the message was corrupted. In hardware the division is an LFSR whose taps are the generator's terms: one bit of message per clock. A CRC with an n-bit remainder catches every burst error up to n bits long.

Worked example

Compute the 3-bit CRC of the message 1101 with generator 1011 (x3+x+1).

Show the solution
  1. Append three zeros. The message becomes 1101000.
  2. Divide by 1011 with XOR. The first step is 1101⊕1011=0110 then bring down bits and repeat until the message is used up.
  3. Read the remainder. The remainder is 001, so 1101001 is sent.
  4. Check. Dividing 1101001 by 1011 leaves 000.

Answer the CRC is 001, and 1101001 is sent.

Worked example

A serial link runs at 115,200 bits per second with a start and a stop bit around each 8-bit byte. How many bytes per second does it carry?

Show the solution
  1. Bit times per byte. Each byte takes 10 bit times: a start bit, 8 data bits and a stop bit.
  2. Divide. 11520010=11520

Answer 11,520 bytes per second.

Worked example

A 4-bit LFSR shifts left and feeds back the XOR of its top two bits, which implements x4+x3+1. Starting at 0001, how many clocks until it returns?

Show the solution
  1. Run the sequence. 0001, 0010, 0100, 1001, 0011, 0110, 1101, 1010, 0101, 1011, 0111, 1111, 1110, 1100, 1000, then back to 0001.
  2. Count the states. That's 15 states, every nonzero pattern once.
  3. Why 15. A primitive polynomial of degree n gives the maximum period 2n−1; only the all-zero state is missing, and it must be avoided because it feeds back zeros forever.
  4. Uses. Such sequences make cheap pseudorandom test patterns and scramblers.

Answer 15 clocks.

Check

How many states does a maximal-length 8-bit LFSR cycle through?

Check

How many states does a Johnson counter built from 4 flip-flops have?

Check

A shift register clocked at 1 MHz receives 32 bits serially. How long does it take?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

A 4-bit LFSR shifts left, dropping bit 3, and puts Q3⊕Q2 into bit 0. Its state is 6 (in decimal). What is the next state?

Check

A serial link sends 10 bit times per byte (start, data, parity and stop bits) at 115200 bits per second. How many bytes per second?

Check

What is the 3-bit CRC of the message 1101 with generator 1011?

Check

A maximal-length LFSR has n = 7 bits; a serial link runs at 9600 bits per second with start and stop bits.

(a)

What's the LFSR's period?

(b)

How many bytes per second does the link carry?

Check

A maximal-length n-bit LFSR runs from a clock at f.

Step 1

What's its period, in clocks?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does a maximal-length LFSR visit every state except one?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.