Verilog LFSR(2)

#学习记录#

数字设计-LFSR (1)-CSDN博客

1  Fibonacci LFSR

Fibonacci LFSR的反馈多项式为:x^3+x^2+1,电路图如下图1所示,schematic如图2所示。输出序列为111-110-100-001-010-101-011。

图1  斐波那契LFSR电路图

图2  斐波那契LFSR schematic

1.1  Verilog描述Fibonacci LFSR

1.1.1  代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/01 08:00:45
// Design Name: 
// Module Name: LFSR_fibonacci
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module LFSR_fibonacci(
    input clk,
    input rst_n,
    output reg [2:0] q
    );
always @(posedge clk or negedge  rst_n) begin
    if(!rst_n) 
        q<=3'b111;
    else
    q<={q[1],q[0],q[2]^q[1]};
end           
endmodule

1.1.2  testbench

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/01 08:15:30
// Design Name: 
// Module Name: LFSR_fibonaccitb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module LFSR_fibonaccitb(  );
    reg clk;
    reg rst_n;
    wire [2:0] q;
LFSR_fibonacci tb(
    .clk(clk),
    .rst_n(rst_n),
    .q(q)
);
initial begin
    rst_n=0;clk=0;
    #5
    rst_n=1;
    #1000
    $stop;
end
always #5 clk=~clk;
endmodule

1.2  仿真波形

图3  斐波那契LFSR仿真波形

2  Galois LFSR

Galois LFSR的反馈多项式为x^3+x^2+1,电路如图4所示,schematic如图5所示。输出序列为111-101-100-010-001-110-011。

图4  伽罗瓦LFSR电路图

图5  伽罗瓦LFSR schematic

2.1  Verolog描述Galois LFSR

2.1.1  代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/01 08:47:10
// Design Name: 
// Module Name: LFSR_galois
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module LFSR_galois(
    input clk,
    input rst_n,
    output reg [2:0] q
 );
always @(posedge clk or negedge rst_n) begin
    if(!rst_n)
        q<=3'b111;
    else
        q<={q[0],q[2]^q[0],q[1]};
end
endmodule

2.1.2  testbench

`timescale 1ns / 1ps
//
// Company: 
// Engineer: Mr-pn-junction
// 
// Create Date: 2023/11/01 09:03:07
// Design Name: 
// Module Name: LFSR_galoistb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module LFSR_galoistb(  );
    reg    clk;
    reg  rst_n;
    wire [2:0] q;
LFSR_galois tb(
    .clk(clk),
    .rst_n(rst_n),
    .q(q)
);

initial begin
    clk=0; rst_n=0;
    #5
    rst_n=1;
    #1000
    $stop;
end
always #5 clk=~clk;
endmodule

2.2  仿真波形

图6  伽罗瓦LFSR仿真波形

参考文献

[1] Verilog高级数字系统设计技术与实例分析. Kishore Mishra. 电子工业出版社.

### Verilog 实现 LFSR 的基本原理 在硬件描述语言中,线性反馈移位寄存器(LFSR)可以通过组合逻辑电路来实现。对于斐波那契型 LFSR,在每次时钟脉冲到来时,当前状态会通过特定的反馈多项式计算新状态并更新移位寄存器的内容[^2]。 具体到 Verilog 中,可以定义一个模块用于表示 n 位宽的 LFSR 寄存器,并利用异或门完成必要的反馈连接: ```verilog module lfsr_fibonacci ( input wire clk, input wire rst_n, // active low reset output reg [WIDTH-1:0] q ); parameter WIDTH = 8; // Initial seed value must not be all zeros. reg [WIDTH-1:0] state; always @(posedge clk or negedge rst_n) begin : proc_state if (!rst_n) state <= {WIDTH{1'b1}}; // Reset to non-zero pattern else state <= next_state(); end function automatic [WIDTH-1:0] next_state(); integer i; logic feedback_bit; // Calculate XOR of taps based on polynomial coefficients feedback_bit = ^state[TAPS]; // Example tap positions for (i = 0; i < WIDTH - 1; ++i) next_state[i] = state[i + 1]; next_state[WIDTH - 1] = feedback_bit; endfunction assign q = state; endmodule ``` 上述代码展示了如何创建一个基于 Fibonacci 结构的简单 LFSR 模块。`TAPS` 参数应设置为目标多项式的抽头位置数组;例如 `[7,5,3,1]` 对于宽度为 8 的 Galois 字段 GF(2)。 为了确保正常工作,初始种子值不应全为零,因为这会导致后续所有输出均为零的状态循环。通常会在复位信号 `rst_n` 下载入非零模式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值