산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> verilog

VHDL 및 Verilog를 사용하여 FPGA에서 효율적인 LFSR 설계

FPGA의 LFSR – VHDL 및 Verilog 코드

FPGA 내부에서 선형 피드백 시프트 레지스터가 작동하는 방식

LFSR은 Linear Feedback Shift Register의 약자로 FPGA 내부에서 유용한 설계입니다. LFSR은 합성이 간단합니다. 즉, 상대적으로 적은 리소스를 사용하며 FPGA 내부에서 매우 높은 클럭 속도로 실행될 수 있습니다. 다음을 포함하여 LFSR을 사용하면 이점을 얻을 수 있는 응용 프로그램이 많이 있습니다.

선형 피드백 시프트 레지스터는 시프트 레지스터로 함께 연결된 FPGA 내부의 일련의 플립플롭으로 구현됩니다. 시프트 레지스터 체인의 여러 탭은 XOR에 대한 입력으로 사용됩니다. 또는 XNOR 문. 이 게이트의 출력은 피드백으로 사용됩니다. 시프트 레지스터 체인의 시작 부분으로 이동하므로 LFSR의 피드백이 됩니다.

XNOR 게이트를 사용한 5비트 LFSR

LFSR이 실행 중일 때 개별 플립플롭에 의해 생성되는 패턴은 의사 무작위입니다. 즉, 무작위에 가깝습니다. LFSR 패턴의 모든 상태에서 다음 상태를 예측할 수 있으므로 완전히 무작위는 아닙니다. 주목해야 할 중요한 시프트 레지스터의 몇 가지 속성이 있습니다:

LFSR이 길수록 모든 반복을 실행하는 데 더 오랜 시간이 걸립니다. N비트의 LFSR에 대해 가능한 가장 긴 반복 횟수는 2N-1입니다. 생각해 보면 N비트 길이의 모든 가능한 패턴은 2N입니다. 따라서 LFSR을 사용하여 표현할 수 없는 패턴은 단 하나뿐입니다. 해당 패턴은 XOR 게이트를 사용할 때 모두 0이고 XNOR 게이트를 피드백 게이트로 사용할 때 모두 1입니다.

VHDL 및 Verilog 코드는 원하는 N비트 폭의 LFSR을 생성합니다. 이는 각 비트 폭에 대해 가능한 최대 LFSR 길이를 생성하기 위해 다항식(LFSR의 수학)을 사용합니다. 따라서 3비트의 경우 가능한 모든 조합(4비트의 경우:24-1=15, 5비트의 경우:25-1=31 등)을 실행하는 데 23-1=7 클럭이 필요합니다. 저는 이를 XNOR 구현을 기반으로 하여 FPGA가 LFSR에서 모두 0인 상태에서 시작할 수 있도록 했습니다. 다음은 Xilinx에서 게시한 모든 LFSR 패턴의 전체 표입니다.

VHDL 구현:

LFSR.vhd

-------------------------------------------------------------------------------
-- File downloaded from http://www.nandland.com
-------------------------------------------------------------------------------
-- Description:
-- A LFSR or Linear Feedback Shift Register is a quick and easy
-- way to generate pseudo-random data inside of an FPGA. The LFSR can be used
-- for things like counters, test patterns, scrambling of data, and others.
-- This module creates an LFSR whose width gets set by a generic. The
-- o_LFSR_Done will pulse once all combinations of the LFSR are complete. The
-- number of clock cycles that it takes o_LFSR_Done to pulse is equal to
-- 2^g_Num_Bits-1. For example, setting g_Num_Bits to 5 means that o_LFSR_Done
-- will pulse every 2^5-1 = 31 clock cycles. o_LFSR_Data will change on each
-- clock cycle that the module is enabled, which can be used if desired.
--
-- Generics:
-- g_Num_Bits - Set to the integer number of bits wide to create your LFSR.
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity LFSR is
 generic (
 g_Num_Bits : integer := 5
 );
 port (
 i_Clk : in std_logic;
 i_Enable : in std_logic;
 -- Optional Seed Value
 i_Seed_DV : in std_logic;
 i_Seed_Data : in std_logic_vector(g_Num_Bits-1 downto 0);
 
 o_LFSR_Data : out std_logic_vector(g_Num_Bits-1 downto 0);
 o_LFSR_Done : out std_logic
 );
end entity LFSR;
architecture RTL of LFSR is
 signal r_LFSR : std_logic_vector(g_Num_Bits downto 1) := (others => '0');
 signal w_XNOR : std_logic;
 
begin
 -- Purpose: Load up LFSR with Seed if Data Valid (DV) pulse is detected.
 -- Othewise just run LFSR when enabled.
 p_LFSR : process (i_Clk) is
 begin
 if rising_edge(i_Clk) then
 if i_Enable = '1' then
 if i_Seed_DV = '1' then
 r_LFSR 

테스트벤치(LFSR_TB.vhd)

-------------------------------------------------------------------------------
-- File downloaded from http://www.nandland.com
-------------------------------------------------------------------------------
-- Description: Simple Testbench for LFSR.vhd. Set c_NUM_BITS to different
-- values to verify operation of LFSR
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
entity LFSR_TB is
end entity LFSR_TB;
architecture behave of LFSR_TB is
 constant c_NUM_BITS : integer := 5;
 constant c_CLK_PERIOD : time := 40 ns; -- 25 MHz
 
 signal r_Clk : std_logic := '0';
 signal w_LFSR_Data : std_logic_vector(c_NUM_BITS-1 downto 0);
 signal w_LFSR_Done : std_logic;
 
begin
 r_Clk c_NUM_BITS)
 port map (
 i_Clk => r_Clk,
 i_Enable => '1',
 i_Seed_DV => '0',
 i_Seed_Data => (others => '0'),
 o_LFSR_Data => w_LFSR_Data,
 o_LFSR_Done => w_LFSR_Done
 );
 
end architecture behave;

Verilog 구현:

LFSR.v

///////////////////////////////////////////////////////////////////////////////
// File downloaded from http://www.nandland.com
///////////////////////////////////////////////////////////////////////////////
// Description: 
// A LFSR or Linear Feedback Shift Register is a quick and easy way to generate
// pseudo-random data inside of an FPGA. The LFSR can be used for things like
// counters, test patterns, scrambling of data, and others. This module
// creates an LFSR whose width gets set by a parameter. The o_LFSR_Done will
// pulse once all combinations of the LFSR are complete. The number of clock
// cycles that it takes o_LFSR_Done to pulse is equal to 2^g_Num_Bits-1. For
// example setting g_Num_Bits to 5 means that o_LFSR_Done will pulse every
// 2^5-1 = 31 clock cycles. o_LFSR_Data will change on each clock cycle that
// the module is enabled, which can be used if desired.
//
// Parameters:
// NUM_BITS - Set to the integer number of bits wide to create your LFSR.
///////////////////////////////////////////////////////////////////////////////
module LFSR #(parameter NUM_BITS)
 (
 input i_Clk,
 input i_Enable,
 // Optional Seed Value
 input i_Seed_DV,
 input [NUM_BITS-1:0] i_Seed_Data,
 output [NUM_BITS-1:0] o_LFSR_Data,
 output o_LFSR_Done
 );
 reg [NUM_BITS:1] r_LFSR = 0;
 reg r_XNOR;
 // Purpose: Load up LFSR with Seed if Data Valid (DV) pulse is detected.
 // Othewise just run LFSR when enabled.
 always @(posedge i_Clk)
 begin
 if (i_Enable == 1'b1)
 begin
 if (i_Seed_DV == 1'b1)
 r_LFSR 

테스트벤치(LFSR_TB.v)

///////////////////////////////////////////////////////////////////////////////
// File downloaded from http://www.nandland.com
///////////////////////////////////////////////////////////////////////////////
// Description: Simple Testbench for LFSR.v. Set c_NUM_BITS to different
// values to verify operation of LFSR
///////////////////////////////////////////////////////////////////////////////
module LFSR_TB ();
 parameter c_NUM_BITS = 4;
 
 reg r_Clk = 1'b0;
 
 wire [c_NUM_BITS-1:0] w_LFSR_Data;
 wire w_LFSR_Done;
 
 LFSR #(.NUM_BITS(c_NUM_BITS)) LFSR_inst
 (.i_Clk(r_Clk),
 .i_Enable(1'b1),
 .i_Seed_DV(1'b0),
 .i_Seed_Data(}), // Replication
 .o_LFSR_Data(w_LFSR_Data),
 .o_LFSR_Done(w_LFSR_Done)
 );
 
 always @(*)
 #10 r_Clk 

verilog

  1. Verilog 포트
  2. Verilog Inter 및 Intra 할당 지연
  3. Verilog 디스플레이 작업
  4. Verilog 작업
  5. Verilog 소개
  6. Verilog 초기 블록
  7. Verilog 수학 함수
  8. Verilog 게이트 지연
  9. 루프용 Verilog
  10. Verilog 단일 포트 RAM