An LFSR is a simple circuit that generates a sequence of bits that looks random. It is used to create patterns for testing or encryption.
Linear Feedback Shift Register (LFSR) in Verilog
module lfsr(
input clk,
input reset,
output reg [3:0] q
);
wire feedback = q[3] ^ q[2];
always @(posedge clk or posedge reset) begin
if (reset)
q <= 4'b0001;
else
q <= {q[2:0], feedback};
end
endmoduleThe LFSR uses XOR of selected bits as feedback.
The register shifts bits on each clock cycle, adding the feedback bit at the end.
wire feedback = q[3] ^ q[1]; q <= {q[2:0], feedback};
if (reset) q <= 4'b1000; else q <= {q[2:0], feedback};
This program creates a 4-bit LFSR and a testbench that runs it. The output shows the LFSR state changing every clock cycle.
module lfsr(
input clk,
input reset,
output reg [3:0] q
);
wire feedback = q[3] ^ q[2];
always @(posedge clk or posedge reset) begin
if (reset)
q <= 4'b0001;
else
q <= {q[2:0], feedback};
end
endmodule
// Testbench
module testbench();
reg clk = 0;
reg reset = 1;
wire [3:0] q;
lfsr uut(.clk(clk), .reset(reset), .q(q));
always #5 clk = ~clk; // Clock toggles every 5 time units
initial begin
$monitor($time, " q = %b", q);
#10 reset = 0; // Release reset after 10 time units
#100 $finish;
end
endmoduleMake sure the initial value (seed) is not zero; otherwise, the LFSR will stay at zero.
The choice of bits for feedback affects the length of the sequence before it repeats.
An LFSR generates a sequence of bits using shift and XOR feedback.
It is useful for pseudo-random number generation and testing hardware.
Reset sets the starting value, and feedback taps control the sequence.
Practice
What is the main purpose of a Linear Feedback Shift Register (LFSR) in digital circuits?
Solution
Step 1: Understand LFSR function
An LFSR uses shift and XOR feedback to produce a sequence of bits that appears random.Step 2: Identify main use
This sequence is used for pseudo-random number generation and testing, not arithmetic or storage.Final Answer:
To generate a pseudo-random sequence of bits -> Option DQuick Check:
LFSR = pseudo-random bit generator [OK]
- Confusing LFSR with memory storage
- Thinking LFSR does arithmetic
- Assuming LFSR converts analog signals
Which of the following is the correct Verilog syntax to declare a 4-bit register named lfsr?
Solution
Step 1: Recall Verilog register declaration
Registers are declared withreg [msb:lsb] name;syntax.Step 2: Check each option
reg [3:0] lfsr; uses correct syntaxreg [3:0] lfsr;. reg lfsr[3:0]; uses wrong array style. wire [4:1] lfsr; uses wire, not reg. reg (3:0) lfsr; has invalid parentheses.Final Answer:
reg [3:0] lfsr; -> Option AQuick Check:
Correct reg syntax = reg [3:0] lfsr; [OK]
- Using wire instead of reg for registers
- Incorrect bit range order or syntax
- Using parentheses instead of brackets
Given this Verilog snippet for a 3-bit LFSR with taps at bits 3 and 2:
reg [2:0] lfsr = 3'b001;
always @(posedge clk) begin
lfsr <= {lfsr[1:0], lfsr[2] ^ lfsr[1]};
endWhat is the value of
lfsr after 1 clock cycle?Solution
Step 1: Calculate first clock cycle
Initial lfsr = 001. Feedback = lfsr[2]^lfsr[1] = 0^0=0.
New lfsr = {lfsr[1:0], feedback} = {0,1,0} = 010.Step 2: Confirm value
After 1 clock cycle, lfsr = 3'b010, which matches 3'b010.Final Answer:
3'b010 -> Option BQuick Check:
Shift and XOR feedback = 010 after 1 cycle [OK]
- Mixing bit order in concatenation
- Calculating feedback incorrectly
- Confusing initial and next states
Identify the error in this Verilog LFSR code snippet:
reg [3:0] lfsr = 4'b0000;
always @(posedge clk) begin
lfsr <= {lfsr[2:0], lfsr[3] ^ lfsr[0]};
endSolution
Step 1: Check initial value
LFSRs must not start with all zeros because they will stay stuck at zero forever (no feedback).Step 2: Verify other parts
Feedback taps and concatenation are valid for a 4-bit LFSR. Register size is correctly declared as 4 bits.Final Answer:
Initial value can't be all zeros in LFSR -> Option CQuick Check:
All-zero start = stuck LFSR [OK]
- Using zero initial value
- Misunderstanding feedback tap positions
- Incorrect bit concatenation order
You want to design an 8-bit LFSR in Verilog that cycles through all 255 non-zero states before repeating. Which of the following tap combinations is a valid maximal-length feedback polynomial?
Solution
Step 1: Recall maximal-length tap sets for 8-bit LFSR
Common maximal-length taps for 8-bit LFSR are bits 8 and 5 (1-based indexing).Step 2: Match options with known polynomials
Taps at bits 8 and 5 (lfsr[7] and lfsr[4]) matches a valid maximal-length set. Taps at bits 8 and 7 (lfsr[7] and lfsr[6]), 8 and 6 (lfsr[7] and lfsr[5]), and 8 and 4 (lfsr[7] and lfsr[3]) are not maximal-length taps.Step 3: Check indexing in Verilog (0-based)
Bits 8 and 5 correspond to lfsr[7] and lfsr[4], which is Taps at bits 8 and 5 (lfsr[7] and lfsr[4]).Final Answer:
Taps at bits 8 and 5 (lfsr[7] and lfsr[4]) -> Option AQuick Check:
Maximal taps for 8-bit LFSR = bits 8 and 5 [OK]
- Choosing non-maximal tap positions
- Confusing bit indexing (1-based vs 0-based)
- Assuming any two taps produce maximal length
Ready to prove it?
Pass the test and this pattern moves unknown
