HDLBits 系列(21)LFSR(线性反馈移位寄存器)

目录

5 bit LFSR

3 bit LFSR

32 bit LFSR


5 bit LFSR

A linear feedback shift register is a shift register usually with a few XOR gates to produce the next state of the shift register. A Galois LFSR is one particular arrangement where bit positions with a "tap" are XORed with the output bit to produce its next value, while bit positions without a tap shift. If the taps positions are carefully chosen, the LFSR can be made to be "maximum-length". A maximum-length LFSR of n bits cycles through 2n-1 states before repeating (the all-zero state is never reached).

The following diagram shows a 5-bit maximal-length Galois LFSR with taps at bit positions 5 and 3. (Tap positions are usually numbered starting from 1). Note that I drew the XOR gate at position 5 for consistency, but one of the XOR gate inputs is 0.

Module Declaration

module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 5'h1
    output [4:0] q
); 

线性反馈移位寄存器是通常带有几个XOR门的移位寄存器,用于产生移位寄存器的下一个状态。 Galois LFSR是一种特殊的移位寄存器,其中将带有“抽头”的位位置与输出位进行异或运算以产生其下一个值。 如果仔细选择抽头位置,则可以将LFSR设为“最大长度”。 n位的最大长度LFSR在重复之前循环经过2n-1个状态(永远不会达到全零状态)。

下图显示了一个5位最大长度的Galois LFSR,在位置5和3处有抽头(抽头位置通常从1开始编号)。 请注意,为保持一致性,我在位置5处绘制了XOR门,但XOR门输入之一为0。

说到底,就是一个移位寄存器,只不过某些输入不是上一级触发器的输出,而是有可能和其他级的输出进行了异或作为当前的输入;

给出设计(与0异或等于本身):

module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 5'h1
    output reg [4:0] q
); 
    always@(posedge clk) begin
        if(reset) q <= 5'h1;
        else begin
           // q[4] <= q[0];
           // q[3] <= q[4];
           // q[2] <= q[3]^q[0];
            //q[1] <= q[2];
            //q[0] <= q[1];
            q <= {q[0],q[4],q[3]^q[0],q[2:1]};
        end
        
    end
    

endmodule

3 bit LFSR

Write the Verilog code for this sequential circuit (Submodules are ok, but the top-level must be named top_module). Assume that you are going to implement the circuit on the DE1-SoC board. Connect the R inputs to the SW switches, connect Clock to KEY[0], and L to KEY[1]. Connect the Q outputs to the red lights LEDR.

Module Declaration

module top_module (
	input [2:0] SW,      // R
	input [1:0] KEY,     // L and clk
	output [2:0] LEDR);  // Q

下面对上面的电路进行设计:

首先,可以看出有三个类似的模块可以调用,那就描述出来这个子模块如下:


module sequential_com(
	input clk,
	input in1,
	input in0,
	input sel,
	output q
	);
	
	wire in_sel;
	assign in_sel = sel ? in1 : in0;

	reg q_mid = 0;
	always@(posedge clk) begin
		q_mid <= in_sel;
	end
	assign q = q_mid;

endmodule

例化该子模块得到顶层设计:

module top_module (
	input [2:0] SW,      // R
	input [1:0] KEY,     // L and clk
	output [2:0] LEDR);  // Q
    
    sequential_com isnt0(
        .clk(KEY[0]),
        .in1(SW[0]),
        .in0(LEDR[2]),
        .sel(KEY[1]),
        .q(LEDR[0])
    );
    
    sequential_com isnt1(
        .clk(KEY[0]),
        .in1(SW[1]),
        .in0(LEDR[0]),
        .sel(KEY[1]),
        .q(LEDR[1])
    );
    
    sequential_com isnt2(
        .clk(KEY[0]),
        .in1(SW[2]),
        .in0(LEDR[1]^LEDR[2]),
        .sel(KEY[1]),
        .q(LEDR[2])
    );
    
    


endmodule

32 bit LFSR

Build a 32-bit Galois LFSR with taps at bit positions 32, 22, 2, and 1.

第一题就写了5 bit的LFSR,这一题只不过多了几位,且告诉你抽头在32, 22, 2, and 1,这几个位置,请设计电路?

如何做这个题目呢?

其实很简单,先写一个位数少的,找下规律,就可以了:

module top_module(
    input clk,
    input reset,    // Active-high synchronous reset to 32'h1
    output [31:0] q
); 
    
    reg [31:0] q1;
        always@(posedge clk) begin
            if(reset) q1 <= 32'h1;
        else begin
            q1 <= {q1[0],q1[31:23],q1[0]^q1[22],q1[21:3],q1[0]^q1[2],q1[0]^q1[1]};
        end
        
    end
    assign q = q1;

endmodule

 

 

 

 

 

### 线性反馈移位寄存器LFSR)的概念 线性反馈移位寄存器(Linear-Feedback Shift Register,LFSR),是在其反馈函数为线性函数的情况下工作的移位寄存器。这种类型的移位寄存器广泛应用于各种领域,尤其是通信系统中的编码和解码过程以及硬件测试模式生成等方面[^1]。 ### 工作原理 LFSR的核心在于利用一个或多个特定位置上的比特值作为输入,通过线性的组合方式——通常是异或(XOR)操作来计算新加入到最左侧的新比特值。每当发生一次时钟脉冲触发事件时,整个寄存器的内容会向右移动一位,并且根据预先定义好的规则,在左端填入一个新的比特值。对于长度为\( n \)LFSR来说: \[ k+1\text{阶段}:\begin{cases} R_{n-1}=R_{n-2}\\ ...\\ R_0=f(R_1,R_2,\ldots,R_{n-1})=(R_{n-1}\cdot g_n)\oplus(R_{n-2}\cdot g_{n-1})\oplus...\oplus(R_0\cdot g_1)+g_0 \end{cases} \] 这里 \( f() \) 表达的是基于当前状态下的线性反馈逻辑运算结果,而 \( g_i \in {0, 1} \) 则决定了哪些位参与到了最终的反馈过程中去[^4]。 ### 应用场景 #### 数据校验与错误检测 在线传输数据包之前,发送方可以使用LFSR生成一段固定长度的数据序列附加于原始消息之后一起发出;接收方收到完整的报文后再重新执行相同的算法得到同样的检验序列并与实际接收到的部分对比验证完整性。这种方法被广泛应用在文件传输协议中以确保数据准确性[^2]。 #### 加密技术 由于LFSR能够产生看似随机但实际上遵循一定规律变化的二进制数列特性,因此也被用来构建流密码体制的一部分组件,比如GSM标准里的A5/1加密算法就是采用了改进版多级联结构形式的LFSRs实现密钥扩展功能[^3]。 #### 测试图案生成 集成电路设计完成后需要经过严格的功能性和性能指标评估环节才能投入量产,此时便可以通过编程控制内部集成有LFSR单元的芯片自动生成一系列具有代表性的激励信号施加给待测对象完成自动化测试流程优化工作效率的同时也提高了覆盖率水平[^5]。 ```verilog // Verilog 实现 EE 类型 LFSR 示例代码片段 module lfsr_ee ( input wire clk, output reg [WIDTH-1:0] q ); parameter WIDTH = 8; integer i; always @(posedge clk) begin for(i=0; i<WIDTH-1; i=i+1) q[i] <= q[i+1]; // 计算新的最低有效位 q[WIDTH-1] <= ^q & feedback_enable; end endmodule ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李锐博恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值