思路:
模块一:产生随机序列1作为本地码组
模块二:产生随机序列2作为接收码组
模块三:二输入异或门得到异或序列
模块四:在阀门高电平期间统计异或高电平期间对CLK个数,统计误码个数
模块五:在阀门高电平期间CLK个数
模块六:设置阀门时间
模块七:阀门高电平期间 误码率=误码个数/clk
模块一:产生随机序列1作为本地码组
模块二:产生随机序列2作为接收码组
模块三:二输入异或门得到异或序列
模块四:在阀门高电平期间统计异或高电平期间对CLK个数,统计误码个数
模块五:在阀门高电平期间CLK个数
模块六:设置阀门时间
模块七:阀门高电平期间 误码率=误码个数/clk
话不多说直接上程序,已注释。
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.std_logic_unsigned.ALL;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.std_logic_unsigned.ALL;
ENTITY wsj1 IS --定义实体wsj1
GENERIC(n : positive := 16);
PORT(CLK:IN STD_LOGIC;--时钟信号
CTL:IN STD_LOGIC;--控制信号
BASIC:BUFFER STD_LOGIC;--设置伪随机序列1作为本地码组 反馈系数235(8)
RX:BUFFER STD_LOGIC;--假设伪随机序列2作为接收码组 反馈系数203(8)
YH:BUFFER STD_LOGIC;--异或接收码组和本地码组脉冲
COUNTER_OUT: OUT std_logic_vector(n-1 DOWNTO 0)--记录误码个数
);
END wsj1;
GENERIC(n : positive := 16);
PORT(CLK:IN STD_LOGIC;--时钟信号
CTL:IN STD_LOGIC;--控制信号
BASIC:BUFFER STD_LOGIC;--设置伪随机序列1作为本地码组 反馈系数235(8)
RX:BUFFER STD_LOGIC;--假设伪随机序列2作为接收码组 反馈系数203(8)
YH:BUFFER STD_LOGIC;--异或接收码组和本地码组脉冲
COUNTER_OUT: OUT std_logic_vector(n-1 DOWNTO 0)--记录误码个数
);
END wsj1;
ARCHITECTURE BEHAV OF wsj1 IS
SIGNAL C0,C1,C2,C3,C4,C5,C6,C7:STD_LOGIC;--作为本地码组的随机序列
SIGNAL A0,A1,A2,A3,A4,A5,A6,A7:STD_LOGIC;--发送码组随机序列
SIGNAL COUNTER_T : std_logic_vector(n-1 DOWNTO 0);--统计高电平个数
SIGNAL C0,C1,C2,C3,C4,C5,C6,C7:STD_LOGIC;--作为本地码组的随机序列
SIGNAL A0,A1,A2,A3,A4,A5,A6,A7:STD_LOGIC;--发送码组随机序列
SIGNAL COUNTER_T : std_logic_vector(n-1 DOWNTO 0);--统计高电平个数
BEGIN
------产生随机序列一作为本地码组(235)
PROCESS(CLK, CTL) --设置敏感量
BEGIN
IF CLK'EVENT AND CLK='1' THEN
IF ( CTL='1') THEN
C7<='0';C6<='0';C5<='0';C4<='0';C3<='0';C2<='0';C1<='0';C0<='1';BASIC<=C7;
ELSE
C1<=C0;C2<=C1; C3<=C2;C4<=C3;C5<=C4;C6<=C5;C7<=C6;
C0<=C7 XOR C4 XOR C3 XOR C2 ; --设置反馈方式 235(8)=1001110
BASIC<=C7;
END IF;
END IF;
END PROCESS;
PROCESS(CLK, CTL) --设置敏感量
BEGIN
IF CLK'EVENT AND CLK='1' THEN
IF ( CTL='1') THEN
C7<='0';C6<='0';C5<='0';C4<='0';C3<='0';C2<='0';C1<='0';C0<='1';BASIC<=C7;
ELSE
C1<=C0;C2<=C1; C3<=C2;C4<=C3;C5<=C4;C6<=C5;C7<=C6;
C0<=C7 XOR C4 XOR C3 XOR C2 ; --设置反馈方式 235(8)=1001110
BASIC<=C7;
END IF;
END IF;
END PROCESS;
------产生随机序列二作为本地码组(203)
PROCESS(CLK, CTL) --设置敏感量
BEGIN
IF CLK'EVENT AND CLK='1' THEN
IF ( CTL='1') THEN
A7<='0';A6<='0';A5<='0';A4<='0';A3<='0';A2<='0';A1<='0';A0<='1';RX<=A7;
ELSE
A1<=A0;A2<=A1; A3<=A2;A4<=A3;A5<=A4;A6<=A5;A7<=A6;
A0<=A7 XOR A1; --设置反馈方式 203(8)=1000010
RX<=A7;
END IF;
END IF;
END PROCESS;
------二输入异或门
PROCESS(CLK,CTL)
BEGIN
IF CLK'EVENT AND CLK='0' THEN
YH<=RX xor BASIC;
END IF;
END PROCESS;
------在YH高电平期间对CLK进行计数,统计误码个数
PROCESS(CLK,YH,CTL)
BEGIN
IF rising_edge(CLK) THEN
IF YH='1' THEN
COUNTER_T <= COUNTER_T+1;
ELSE
COUNTER_T <= (OTHERS => '0');
END IF;
END IF;
END PROCESS;
PROCESS(YH)
BEGIN
IF falling_edge(YH) THEN
COUNTER_OUT <=COUNTER_T;
END IF;
END PROCESS;
END BEHAV;
PROCESS(CLK, CTL) --设置敏感量
BEGIN
IF CLK'EVENT AND CLK='1' THEN
IF ( CTL='1') THEN
A7<='0';A6<='0';A5<='0';A4<='0';A3<='0';A2<='0';A1<='0';A0<='1';RX<=A7;
ELSE
A1<=A0;A2<=A1; A3<=A2;A4<=A3;A5<=A4;A6<=A5;A7<=A6;
A0<=A7 XOR A1; --设置反馈方式 203(8)=1000010
RX<=A7;
END IF;
END IF;
END PROCESS;
------二输入异或门
PROCESS(CLK,CTL)
BEGIN
IF CLK'EVENT AND CLK='0' THEN
YH<=RX xor BASIC;
END IF;
END PROCESS;
------在YH高电平期间对CLK进行计数,统计误码个数
PROCESS(CLK,YH,CTL)
BEGIN
IF rising_edge(CLK) THEN
IF YH='1' THEN
COUNTER_T <= COUNTER_T+1;
ELSE
COUNTER_T <= (OTHERS => '0');
END IF;
END IF;
END PROCESS;
PROCESS(YH)
BEGIN
IF falling_edge(YH) THEN
COUNTER_OUT <=COUNTER_T;
END IF;
END PROCESS;
END BEHAV;
testbench写法:
信号初始化CLK和ctl都给'1'
code excute only once:
code excute only once:
CTL<='0';WAIT for 600NS;
code excute always:
WAIT for 50NS;
CLK<=NOT CLK;
temp:=temp+1;
if(temp=1024) then
CLK<='0';
end if;
CLK<=NOT CLK;
temp:=temp+1;
if(temp=1024) then
CLK<='0';
end if;
在modemsim查看波形。
Smartkeke