Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Linear-feedback shift register (LFSR) design in vhdl

By EG Projects May 12, 2019

LFSR stands for linear feedback shift register. Although they are widely used in random electronics projects but they are quiet often neglected by the engineers community. LFSR is comprised of a series of D-flip flops, depending on the size of the LFSR. Some of the states and especially the last one is feed back to the system by going through logical XOR.  
LFSR circuit diagram

LFSR circuit diagram

whats the purpose of LFSR? and how it works?

The soul purpose of lfsr is to generate random numbers/logic. How ever the big advantage is next state can be know if the inputs are known.
This control means every thing to hardware engineer. By knowing the states lfsr can be utilized to generate test patterns for a given circuit. lfsr can be molded to act as a counter or event generator. Ends of lfsr can be brought together to form a cascaded loop.

So a linear feed-back shift register (LFSR) is a shift register whose input bit is a linear function of its previous state. This is a rotating register, in which one of the Flip-Flops has a XOR as its input, an XOR among two or more outputs of the remaining Flip-Flops. The outputs connected to the XOR Gate are called TAP. There are two TAPs in the below figure.

Linear feed back register with two taps

Linear feed back register with two taps

The initial value of the LFSR is called “seed”, and because the operation of the register is deterministic, the stream of values produced by the register is completely determined by its current (or previous) state. Likewise, because the register has a finite number of possible states, it must eventually enter a repeating cycle. However, an LFSR with a well-chosen feedback function can produce a sequence of bits which appears random and which has a very long cycle.

The circuit can be initialized with a different seed from Null vector. In the upper Figure two of the three Flip-Flops are connected to the XOR, which is a input to the other Flip-Flop. Let’s suppose that all bits are initialized to ’1’ after the reset. At each clock cycle the rotation continues and runs a sequence of pseudo random bits on the Flip Flop’s outputs, which will be repeated at a given frequency. In this case the sequence will have a length of 7 as shown in the table.

It can be demonstrated that the length of sequence is 2n − 1. The sequence is often associated to a polynomial where the terms different from zero are those with a position corresponding to the TAP.In this case P = 1 + x2 + x3.

Applications of LFSRs include generating pseudo-random numbers, pseudo-noise sequences, fast digital counters, and whitening sequences. Both hardware and software implementations of LFSRs
are common.

LFSR output truth table

LFSR output truth table

LFSR with Polynomial function

LFSR with Polynomial function

8-bit lfsr in vhdl

Lets build an 8-bit lfsr in vhdl. I will design the lfsr using FSM(finite state machine). I aspect that you know what is finite state machine and how it works.

​ The top level entity is comprised of 2 input and 1 output ports. Input ports are clock and reset. Output port is an 8-bit number. Top level entity looks like the the one given in below figure. The picture is taken from top level RTL design in xilinx inc. 

8-bit lfsr top level entity

8-bit lfsr top level entity

The input to lfsr is states coming back as input to XOR. The states which are taped for XOR are 0,2,3 and 4. The lfsr diagram is shown below.   
8-bit lfsr in vhdl

8-bit lfsr in vhdl

Simulation result of the linear feed back register is below. On initial reset the input to system is “00000001“. If you now calculate manually the output by using equation Z=0(bit) XOR 2(bit) XOR 3(bit) XOR 4(bit). You will get same result as in the simulation below.  
8-bit lfsr simulation output

8-bit lfsr simulation output

LFSR vhdl code

First the necessary vhdl libraries are included in the project. After libraries top level lfsr entity is defined in the code. In the code below top level entity name is LFSR8. After entity input output ports declaration its time to define the internal architecture of the linear feed back register.

In architecture i first defined the two signals(currstate and nextstate) of 8-bit length. These signals are playing a vital role in lfsr working. Their purpose is to hold the current state and next state. In the process part first the reset port is defined. If reset is ‘1’ high then current state is initialized “00000001“. Else if its a rising edge of clock then next state is assigned to current state. Note that the reset is asynchronous and process is sensitive to input clock and reset.   

 After the process block the XOR between the tap states or bits is done. The result of XOR is saved in feedback signal. On the bases of the feedback signal nextstate is calculated. The statement Nextstate <= feedback & Currstate(7 downto 1) is concatenating the new lsb(least significant bit) which is feedback with the rest of the number. The & operator in vhdl is used for concatenation purposes. The new state is assigned to the next state. While the current state is assigned to output in the next statement. 

LFSR VHDL test bench

Test bench of the project is below. The main lfsr entity is instantiated in the test bench. After instantiation it is mapped with test bench signals. Two process are defined in test bench. The first process is about clock input. Clock period is 20 ns. The second process is about resetting the system and initializing the current state with initial value.
8-bit lfsr project files


Filed Under: Microcontroller Projects, VHDL

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions.

  • Why is military EMC so strict?
  • Cannot prototype a power supply
  • Phased array antenna never on full power for long?
  • TPS63070 Power Supply
  • Question about resistors values impact on lead reading

RSS Electro-Tech-Online.com Discussions

  • need help in photodetection TIA circuit
  • Need to solder a really delicate ribbon for an electric reel need advice
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS
  • Measuring controller current output with a meter
  • Anyone In The US Ordered From AliExpress Recently?

Featured Tutorials

Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
    Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
    Practical application of hardware filters with real-life examples
  • A filter design example
    A filter design example
  • Types of filter responses
    Types of filter responses
  • What are the two types of hardware filters?
    What are the two types of hardware filters?
  • What are hardware filters and their types?
    What are hardware filters and their types?
More Tutorials >

Recent Articles

  • Microchip extends single pair Ethernet lineup with software-less endpoint
  • Infineon MCU targets high-voltage BMS in electric vehicles
  • SemiQ releases expanded SiC MOSFET lineup with detailed thermal and switching data
  • TDK adds 1000 W models to dc-dc converter series
  • LEMO introduces resin-free IP68 connectors for compact equipment

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
  • Guest Post Guidelines
  • Advertise
  • Subscribe