m 序列是一种伪随机码,或伪噪声码(PN 码),其输出在一个周期内具有随机性。本文讨论 m 序列生成电路的 FPGA 实现方法。
目录
1 基本概念
在 n 级移位寄存器的基础上,增加一个反馈环节,如果反馈函数 是现态 的线性组合,即
就得到一个线性反馈移位寄存器(Linear Feedback Shift Register, LFSR)。
对于 n 级 LFSR,除去 “0” 态,最多有 个状态,这 个状态可以产生一个周期为 的序列,称之为 m 序列。m 序列是 n 级 LFSR 所有能产生的序列中,周期最长的序列。
2 设计实现
关于 m 序列的理论研究已经很成熟了,m 序列 LFSR 的反馈系数由本原多项式给出。
使用 constant 存储本原多项式的系数,由于本原多项式最后一项固定为 1,只需要存储 ~ 前面的系数即可。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity lfsr is
generic(
N_VALUE : integer := 4;
LFSR_INIT : integer := 1
);
port(
rst : in std_logic;
clk : in std_logic;
qout : out std_logic
);
end lfsr;
architecture behav of lfsr is
-- internal signal declarations
type array_1x15 is array(0 to 14) of std_logic_vector(15 downto 0);
constant PolyCoeff: array_1x15 := (X"0003", X"0005", X"0009", X"0012", X"0021",
X"0044", X"008E", X"0108", X"0204", X"0402",
X"0829", X"100D", X"2221", X"4001", X"8805");
signal coeff: std_logic_vector(15 downto 0);
signal q: std_logic_vector(N_VALUE-1 downto 0);
signal q0: std_logic;
begin
-- todo
coeff <= PolyCoeff(N_VALUE-2);
qout <= q(N_VALUE-1);
process(q,coeff)
variable tmp: std_logic;
begin
tmp := '0';
for i in 0 to N_VALUE-1 loop
if coeff(i) = '1' then
tmp := tmp xor q(i);
end if;
end loop;
q0 <= tmp;
end process;
process(rst,clk)
begin
if rst = '1' then
q <= conv_std_logic_vector(LFSR_INIT,q'LENGTH);
elsif rising_edge(clk) then
q <= q(q'HIGH-1 downto 0) & q0;
end if;
end process;
end;
3 仿真验证
搭建仿真环境,使用 Modelsim 仿真验证逻辑功能,其中 LFSR 的初始状态设为 1。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity lfsr_tb is
end entity;
architecture behav of lfsr_tb is
-- internal signal declarations
signal rst: std_logic := '1';
signal clk: std_logic := '1';
begin
-- todo
rst <= '1', '0' after 196 ns;
clk <= not clk after 4000 ps;
lfsr_inst: entity work.lfsr
generic map(
N_VALUE => 4,
LFSR_INIT => 1
)
port map(
rst => rst,
clk => clk,
qout => open
);
end architecture;
可以看到,LFSR 有 15 个状态,输出序列 qout 也是以 15 为周期。