VHDL的CRC校验——数字电路课程设计

  • 格式:pdf
  • 大小:285.68 KB
  • 文档页数:17

数字电路课程设计题目CRC校验班级实验二班学号姓名时间第十五、十六周地点科A-304指导陈学英李尚泽【摘要】:根据CRC校验原理,在发送端对原始数据生成CRC校验位,并按照RS-232的传输协议组帧,在接收端对RS-232传输帧格式进行解析,并完成CRC校验。

整个过程在Modelsim上仿真实现。

【目录】:第一章、实验任务及原理第二章、设计思路方法及方案第三章、FPGA模块程序设计与仿真第四章、结束语【正文】【第一章】:实验任务及原理、任务指标、功能需求、原理阐述任务:发送端对原始数据生成CRC 校验位,并按照RS-232的传输协议组帧,在接收端对RS-232传输帧格式进行解析,并完成CRC 校验。

整个过程在Modelsim 上仿真实现。

CRC 校验原理:生成CRC 校验码的基本原理:任意一个由二进制位串组成的代码都可以和一个系数仅为‘0’或‘1’的多项式一一对应,例如‘1010111’对应的多项式为6421x x x x ++++。

CRC 码集选择的原则:若设码字长度为N ,信息字段长度为K ,校验字段长度为R ,则N=K+R ;对于CRC 码集中的任一码字,存在且仅存在一个R 次多项式g(x)使得:()()()()()R V x A x g x x m x r x ==+其中m(x)为K 次信息多项式,r(x)为R-1次校验多项式,g(x)称为生成多项式。

V(x)为发送的信息加码字多项式。

2012()...RR g x g g x g x g x =++++发送方通过指定的g(x)产生CRC 码字,接收方则通过g(x)来验证CRC 码字,若传输码字多项式V(x)能除尽g(x),则传输正确。

【第二章】:设计思路方法及方案、系统功能需求分析、方案确定及框图结构说明本实验采用如图1所示的结构实现CRC 的编解码。

本实验使用CRC-CCITT 标准进行仿真,其生成多项式为:16125()1g x x x x =+++1CRC 编码采用如图2所示的结构实现。

d图2CRC校验采用多项式除法实现,若V(x)能除尽g(x),则传输正确,否则错误。

实现结构上,在发送端进行CRC-CCITT标准编码,在接收端将信源数据与编码字串行输入与发送端相同的CRC编码器,若编码字为全零,则无传输错误,否则,传输出错。

接收端CRC校验时序与发送端相同,区别只在于无需顺序移出CRC 编码字,只需判断是否为全零即可。

【第三章】:FPGA模块程序设计与仿真、VHDL程序,仿真程序,仿真波形及数据分析本次实验,程序主要写成两个大模块,第一个模块命名code,包含编码与组帧,第二个模块命名decode,包含帧解析和CRC校验模块一:code程序(包含编码和组帧模块)library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;----Uncomment the following library declaration if instantiating----any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all;entity code isport(clk:in std_logic;reset:in std_logic;din:in std_logic;din_dv:in std_logic;dout:out std_logic);end entity;-------------------------------crc code partarchitecture one of code issignal d:std_logic_vector(1to16);signal cnt_en:std_logic;signal cnt_en1:std_logic;signal cnt:std_logic_vector(6downto0);signal cnt1:std_logic_vector(6downto0);signal din_new:std_logic;signal din_dv_new:std_logic;signal dout0:std_logic;begin------------------------------------counter part1:process(clk,reset)beginif(reset='0')thencnt<=(others=>'0');elsif(clk'event and clk='1')thenif(cnt_en='1')thenif(cnt="1001111")thencnt<=(others=>'0');elsecnt<=cnt+'1';end if;end if;end if;end process;part2:process(clk,reset)beginif(reset='0')thencnt_en<='0';elsif(clk'event and clk='1')thenif(din_dv='1')thencnt_en<='1';elsif(cnt="1001111")thencnt_en<='0';end if;end if;end process;-------------------------CRC code partpart3:process(clk,reset,din_dv)begind<=(others=>'0');elsif clk'event and clk='1'thenif din_dv='1'thend(1)<=d(16)xor din;d(2)<=d(1);d(3)<=d(2);d(4)<=d(3);d(5)<=d(4);d(6)<=d(5)xor(d(16)xor din);d(7)<=d(6);d(8)<=d(7);d(9)<=d(8);d(10)<=d(9);d(11)<=d(10);d(12)<=d(11);d(13)<=d(12)xor(d(16)xor din);d(14)<=d(13);d(15)<=d(14);d(16)<=d(15);elsed(1)<=d(16);d(2)<=d(1);d(3)<=d(2);d(4)<=d(3);d(5)<=d(4);d(6)<=d(5);d(7)<=d(6);d(8)<=d(7);d(9)<=d(8);d(10)<=d(9);d(11)<=d(10);d(12)<=d(11);d(13)<=d(12);d(14)<=d(13);d(15)<=d(14);d(16)<=d(15);end if;end if;end process;-----------------------------part4:process(clk,reset)begindout0<='0';elsif clk'event and clk='1'thenif din_dv='1'thendout0<=din;elsedout0<=d(16);end if;end if;end process;--------------------------CRC output and frame part5:process(reset,clk)beginif reset='0'thendin_new<='0';din_dv_new<='0';elsif clk'event and clk='1'thendin_new<=dout0;din_dv_new<=cnt_en;end if;end process;part6:process(clk,reset)beginif(reset='0')thencnt_en1<='0';elsif(clk'event and clk='1')thenif(cnt_en='1')thencnt_en1<='1';elsif(cnt1="1010000")thencnt_en1<='0';end if;end if;end process;part7:process(clk,reset)beginif(reset='0')thencnt1<=(others=>'0');elsif(clk'event and clk='1')thenif(cnt_en='1')thenif(cnt1="1010000")thencnt1<=(others=>'0');elsecnt1<=cnt1+'1';end if;end if;end if;end process;part8:process(clk,reset)beginif(reset='0')thendout<='1';elsif(clk'event and clk='1')thenif(din_dv_new='1')thendout<=din_new;elsif(cnt_en='1')thendout<='0';elsedout<='1';end if;end if;end process;end architecture;模块二:decode程序(包含帧解析和除法模块)library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;----Uncomment the following library declaration if instantiating ----any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all;entity decode isport(clk:in std_logic;reset:in std_logic;din:in std_logic;din_dv:in std_logic;output:out std_logic);end entity;architecture two of decode iscomponent codeport(clk:in std_logic;reset:in std_logic;din:in std_logic;din_dv:in std_logic;dout:out std_logic);end component;signal d:std_logic_vector(1to16);signal din1:std_logic;signal cnt:std_logic_vector(6downto0);signal data_dv:std_logic;signal cnt1:std_logic_vector(6downto0);signal cnt_en1:std_logic;signal clr:std_logic;signal dout2:std_logic;signal dout2_dv:std_logic;beginu1:code port map(clk=>clk,reset=>reset,din=>din,din_dv=>din_dv,dout=>din1);----------------------------------receive part1:process(clk,reset)beginif(reset='0')thencnt<=(others=>'0');elsif(clk'event and clk='1')thenif(data_dv='1')thenif(cnt="1001111")thencnt<=(others=>'0');elsecnt<=cnt+1;end if;end if;end if;end process;part2:process(clk,reset)beginif(reset='0')thendata_dv<='0';elsif(clk'event and clk='1')thenif(cnt="1001111")thendata_dv<='0';elsif(din1='0')thendata_dv<='1';end if;end if;end process;----------------------------part3:process(din1,data_dv,clk,reset)beginif reset='0'thendout2<='0';dout2_dv<='0';elsif clk'event and clk='1'thendout2_dv<=data_dv;dout2<=din1;end if;end process;------------------------------------part4:process(clk,reset)beginif(reset='0')thencnt1<=(others=>'0');elsif(clk'event and clk='1')thenif(cnt_en1='1')thenif(cnt1="1010000")thencnt1<=(others=>'0');elsecnt1<=cnt1+1;end if;end if;end if;end process;part5:process(clk,reset)beginif(reset='0')thencnt_en1<='0';elsif(clk'event and clk='1')thenif(dout2_dv='1')thencnt_en1<='1';elsif(cnt1="1010000")thencnt_en1<='0';end if;end if;end process;--------------------------------------part6:process(clk,reset)beginif(reset='0')thenclr<='0';elsif(clk'event and clk='1')thenif(cnt1="1001111")thenclr<='1';elsif(cnt1="1010000")thenclr<='0';end if;end if;end process;part7:process(clk,reset)beginif(reset='0')thenoutput<='0';elsif(clk'event and clk='1')thenif(cnt1="1001111")thenif(d="0000000000000000")thenoutput<='0';elseoutput<='1';end if;elsif(cnt1="1010000")thenoutput<='0';end if;end if;end process;--------------------------------------part8:process(dout2,dout2_dv,reset,clk,clr) beginif reset<='0'or clr='1'thend<=(others=>'0');elsif clk'event and clk='0'thenif dout2_dv='1'thend(1)<=d(16)xor dout2;d(2)<=d(1);d(3)<=d(2);d(4)<=d(3);d(5)<=d(4);d(6)<=d(5)xor(d(16)xor dout2);d(7)<=d(6);d(8)<=d(7);d(9)<=d(8);d(10)<=d(9);d(11)<=d(10);d(12)<=d(11);d(13)<=d(12)xor(d(16)xor dout2);d(14)<=d(13);d(15)<=d(14);d(16)<=d(15);elsed(1)<=d(16);d(2)<=d(1);d(3)<=d(2);d(4)<=d(3);d(5)<=d(4);d(6)<=d(5);d(7)<=d(6);d(8)<=d(7);d(9)<=d(8);d(10)<=d(9);d(11)<=d(10);d(12)<=d(11);d(13)<=d(12);d(14)<=d(13);d(15)<=d(14);d(16)<=d(15);end if;end if;end process;end architecture;仿真程序模块:signal clk:std_logic:='0';signal reset:std_logic:='0';signal din:std_logic:='0';signal din_dv:std_logic:='0';--signal aa:std_logic:='1';--Outputssignal output:std_logic;--Clock period definitionssignal cnt:std_logic_vector(5downto0);Constant dinall:std_logic_vector(63downto 0):=b"111111111111111111111111111111111111111111111111111111111111111 1";BEGIN-----give the dinall value--Instantiate the Unit Under Test(UUT)uut:decode PORT MAP(clk=>clk,reset=>reset,din=>din,din_dv=>din_dv,output=>output);--Clock process definitionsclk_process:processbeginclk<='0';wait for5us;clk<='1';wait for5us;end process;--Stimulus processstim_proc:processbegin--hold reset state for10ns.reset<='0';wait for10ns;reset<='1';wait;--insert stimulus hereend process;--------------------dv produceprocessbegindin_dv<='0';wait for6us;din_dv<='1';wait for643us;din_dv<='0';wait;end process;---------------------couner to outtestcrc:process(clk,reset)beginif reset='0'thencnt<="000000";elsif clk'event and clk='1'thenif cnt<"1000000"and din_dv='1'thencnt<=cnt+'1';end if;end if;end process;testcrc1:process(reset,clk,cnt)beginif reset<='0'thendin<='0';elsif clk'event and clk='1'thencase cnt iswhen"000000"=>din<=dinall(0); when"000001"=>din<=dinall(1);when"000010"=>din<=dinall(2);when"000011"=>din<=dinall(3);when"000100"=>din<=dinall(4);when"000101"=>din<=dinall(5);when"000110"=>din<=dinall(6);when"000111"=>din<=dinall(7);when"001000"=>din<=dinall(8);when"001001"=>din<=dinall(9); when"001010"=>din<=dinall(10);when"001011"=>din<=dinall(11);when"001100"=>din<=dinall(12);when"001101"=>din<=dinall(13);when"001110"=>din<=dinall(14);when"001111"=>din<=dinall(15);when"010000"=>din<=dinall(16);when"010001"=>din<=dinall(17);when"010010"=>din<=dinall(18);when"010011"=>din<=dinall(19);when"010100"=>din<=dinall(20);when"010101"=>din<=dinall(21);when"010110"=>din<=dinall(22);when"010111"=>din<=dinall(23);when"011000"=>din<=dinall(24);when"011001"=>din<=dinall(25);when"011010"=>din<=dinall(26);when"011011"=>din<=dinall(27);when"011100"=>din<=dinall(28);when"011101"=>din<=dinall(29);when"011110"=>din<=dinall(30);when"011111"=>din<=dinall(31);when"100000"=>din<=dinall(32);when"100001"=>din<=dinall(33);when"100010"=>din<=dinall(34);when"100011"=>din<=dinall(35);when"100100"=>din<=dinall(36);when"100101"=>din<=dinall(37);when"100110"=>din<=dinall(38);when"100111"=>din<=dinall(39);when"101000"=>din<=dinall(40);when"101001"=>din<=dinall(41);when"101010"=>din<=dinall(42);when"101011"=>din<=dinall(43);when"101100"=>din<=dinall(44);when"101101"=>din<=dinall(45);when"101110"=>din<=dinall(46);when"101111"=>din<=dinall(47);when"110000"=>din<=dinall(48);when"110001"=>din<=dinall(49);when"110010"=>din<=dinall(50);when"110011"=>din<=dinall(51);when"110100"=>din<=dinall(52);when"110101"=>din<=dinall(53);when"110110"=>din<=dinall(50);when"110111"=>din<=dinall(55);when"111000"=>din<=dinall(56);when"111001"=>din<=dinall(57);when"111010"=>din<=dinall(58);when"111011"=>din<=dinall(59);when"111100"=>din<=dinall(60);when"111101"=>din<=dinall(61);when"111110"=>din<=dinall(62);when"111111"=>din<=dinall(63);when others=>NULL;end case;end if;end process;END;仿真波形及分析output是未加错误的输出结果,未加error时,输出结果一直为零,d(16)是校验部分的最后一个触发器输出结果,在经过80个时钟之后输出结果为零,dout是组帧之后的输出结果,dout2是帧解析后的输出结果。