Draft preview. Lessons are being written and reviewed.

Digital Design / Module 4: Sequential logic

26. LFSRs

Generate pseudo-random patterns cheaply.

Lecture coming soon

See it move

The idea

A linear feedback is a shift register whose input is an XOR of some of its own bits. With the right bits, called taps, it steps through a long sequence that looks random, at a cost of just one or a few XOR gates. It is completely repeatable, so it is called pseudo-random.

// 4-bit maximal LFSR: taps at bits 3 and 2
always_ff @(posedge clk)
  if (rst) q <= 4'b0001;
  else     q <= {q[2:0], q[3] ^ q[2]};

Period

With N bits there are 2^N states, and a good choice of taps visits 2^N - 1 of them before repeating. This is a maximal-length sequence. The one state it skips is all zeros: XOR of zeros is zero, so if the register ever reached that state it would stay there.

The choice of taps matters. Most sets give shorter cycles, so people use published tables of maximal taps rather than guessing. You can see the sequences in the animation above.

Common uses

  • Test patterns in built-in self-test
  • Generating random stimulus cheaply
  • Scrambling data so that long runs of the same bit don't appear on a link
  • Very fast counters, where only a sequence is needed, since an LFSR has no carry chain
  • Checksums: CRC uses the same structure

Reset is mandatory

An XOR-based LFSR needs a non-zero seed. Reset it to all zeros and it stays locked there. An XNOR version instead locks up at all ones. If a design might see a bit upset, add logic to detect and leave the lock-up state.

Common mistake

Treating an LFSR as truly random. Its output is deterministic and correlated between adjacent bits, so it suits testing and scrambling, but not security.

Key ideas

  • Feedback XOR taps set the sequence.
  • A maximal N-bit LFSR cycles through 2^N-1 states.
  • An XOR-based LFSR locks up at all-zeros.

Your exercise

Build a 4-bit maximal LFSR and list its states.

Find the bug

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

What goes wrong: After reset the output is 0 and never changes. It should step through 15 values.

Interview questions

coreWhat state must an XOR LFSR avoid?

basicHow many states does a maximal-length 8-bit LFSR step through before repeating, and which state does it never reach?

basicAn LFSR uses XNOR feedback instead of XOR. Which state does it lock up in?

coreThis 4-bit LFSR is reset to 4'b0001. What is its state after three clocks, and what is its period?

always @(posedge clk)
  if (rst) q <= 4'b0001;
  else     q <= {q[2:0], q[3] ^ q[2]};

coreAn LFSR is written without a reset. What do you see in simulation, and what could happen in silicon?

coreWhy is an LFSR useful as a very fast counter, and what do you give up compared with a binary counter?

coreGive three places where LFSRs are used in chips, and say why the property they rely on fits each one.

advancedA 4-bit LFSR is written with taps at bits 3 and 1 instead of 3 and 2. What is wrong with its sequence?

q <= {q[2:0], q[3] ^ q[1]};

advancedWhy is an LFSR unsuitable for generating secret keys, even though its output looks random?

Practise these 9 questions with animations