|
--sdata:12位待发送信息;
--datald:sdata的装载信号;
--datafini:数据接收校验完成;
--error:误码警告信号;
--rdata:接收模块接收的12位有效信息数据;
--clk:时钟信号;
--datacrc:附加5位CRC校验码的17位CRC码,在生成模块被发送,在接收模块被接收;
--hsend,hrecv:生成、检验模块的握手信号,协调相互之间关系。
LIBRARY ieee;
USE ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity crcm is
port
(
clk,hrecv,datald :in std_logic;
sdata :in std_logic_vector(11 downto 0);
datacrci:in std_logic_vector(16 downto 0);
datacrco:out std_logic_vector(16 downto 0);
rdata :out std_logic_vector(11 downto 0);
datafini:out std_logic;
erroro,hsend:out std_logic
);
end crcm;
architecture comm of crcm is
CONSTANT multi_coef:std_logic_vector(5 downto 0) :="110101";
SIGNAL dtemp,sdatam,rdtemp:std_logic_vector(11 downto 0);
SIGNAL cnt,rcnt:std_logic_vector(4 downto 0);
SIGNAL rdatacrc:std_logic_vector(16 downto 0);
SIGNAL st,rt:std_logic;
begin
process(clk)
variable crcvar:std_logic_vector(5 downto 0);
begin
if clk'event and clk='1' then
if st='0' and datald='1' then--存储待发送数据
dtemp <= sdata;
sdatam <= sdata;
cnt <= (others=>'0');
hsend <='0';
st <= '1';
elsif st='1' and cnt < 7 then--计算CRC码
cnt <= cnt+1;
if dtemp(11)='1' then
crcvar := dtemp(11 downto 6)xor multi_coef;
dtemp <= crcvar(4 downto 0)& dtemp(5 downto 0) & '0';
else
dtemp <= dtemp(10 downto 0)& '0';
end if;
elsif st='1' and cnt=7 then
datacrco <= sdatam & dtemp(11 downto 7);--信息码+CRC码
hsend <= '1';
cnt <= cnt+1;
elsif st='1' and cnt=8 then--发送数据:信息码+CRC码
hsend <= '0' ;
st <= '0';
end if;
end if;
end process;
process(hrecv,clk)
variable rcrvar :std_logic_vector(5 downto 0);
begin
if clk'event and clk='1' then--save data+crc dode
if rt='0' and hrecv='1' then
rdtemp <= datacrci(16 downto 5);
rdatacrc <= datacrci;
rcnt <= (others => '0');
elsif rt='1' and rcnt < 7 then--check CRC code
datafini <= '0';
rcnt <= rcnt+1;
rcrvar := rdtemp(11 downto 6) xor multi_coef;
if dtemp(11)='1' then
rdtemp <= rcrvar(4 downto 0)& rdtemp(5 downto 0)& '0';
else
rdtemp <= rdtemp(10 downto 0) & '0';
end if;
elsif rt='1' and rcnt =7 then --output data and judge weather error
rdata <= rdatacrc(16 downto 5);
rt <= '0';
if rdatacrc(4 downto 0) /= rdtemp(11 downto 7) then
erroro <='1';
end if;
end if;
end if;
end process;
end comm;