library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity rcv is
port(din,clk:in std_logic;dout:out std_logic_vector(7 downto 0);ready:out std_logic);
end entity;
architecture arch_rcv of rcv is
type rcv_state is (idle,start,get,wt,stop);
signal curr_state,next_state:rcv_state;
signal clk_div_en,clk_div1,clk_div2:std_logic:='0';
signal bit_cnt:unsigned(3 downto 0):="0000";
signal clk_cnt1,clk_cnt2:unsigned(15 downto 0):=x"0000";
begin
process(clk)is
begin
if(clk'event and clk='1')then
curr_state<=next_state;
if(clk_div_en='1')then
if(clk_cnt1=2600)then
clk_div1<=not clk_div1;
else
clk_cnt1<=clk_cnt1+1;
end if;
if(clk_cnt2=5200)then
clk_div2<=not clk_div2;
clk_cnt2<=x"0000";
else
clk_cnt2<=clk_cnt2+1;
end if;
elsif(clk_div_en='0')then
clk_cnt2<=x"0000";
end if;
end if;
end process;
process(din,curr_state,clk_div1,clk_div2)is
begin
if(curr_state=idle)then
ready<='1';
clk_div_en<='0';
bit_cnt<="0000";
if(din='0')then
next_state<=start;
end if;
elsif(curr_state=start)then
ready<='0';
clk_div_en<='1';
if(clk_div1='1')then
next_state<=get;
end if;
elsif(curr_state=get)then
if(clk_div2='1')then
if(bit_cnt=8)then
next_state<=stop;
else
dout(to_integer(bit_cnt))<=din;
bit_cnt<=bit_cnt+1;
next_state<=wt;
end if;
end if;
elsif(curr_state=wt)then
if(clk_div2='0')then
next_state<=get;
end if;
elsif(curr_state=stop)then
if(clk_div2='0')then
next_state<=idle;
end if;
end if;
end process;
end arch_rcv;