顶层文件:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity test is
port(
clk:in std_logic; --50mHz
c,d,m:in std_logic;
e,r:in std_logic;
boat:out std_logic_vector(7 downto 0);
win,fail:out std_logic;
cnt:out std_logic_vector(3 downto 0);
state:out std_logic_vector(3 downto 0)
);
end test;
architecture behave of test is
signal clk1,clk2,clk3:std_logic;
component frq
port(
clk:in std_logic;
clk1,clk2,clk3:out std_logic
);
end component;
component game
port(
clk1,clk2,clk3:in std_logic;
c,d,m:in std_logic;--animals
e,r:in std_logic;--enter and reser
cnt:out std_logic_vector(3 downto 0);--jishu
state:out std_logic_vector(3 downto 0);--zhuangtai
win,fail:out std_logic;--shu or ying
boat:out std_logic_vector(7 downto 0)--chuan yidong
);
end component;
begin
u0:frq port map (clk,clk1,clk2,clk3);
u1:game port map(clk1,clk2,clk3,c,d,m,e,r,cnt,state,win,fail,boat);
end behave;
game文件:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity game is
port(
clk1,clk2,clk3:in std_logic; --clk1:1KHz;clk2:2.5;clk3:500Hz
c,d,m:in std_logic;--animals1
e,r:in std_logic;--enter and reser
cnt:out std_logic_vector(3 downto 0);--jishu
state:out std_logic_vector(3 downto 0);--zhuangtai
win,fail:out std_logic;--shu or ying
boat:out std_logic_vector(7 downto 0)--chuan yidong
);
end game;
architecture behave of game is
signal c_t,d_t,m_t:std_logic;--animals
signal fail_t,win_t:std_logic;
signal boat_t:std_logic_vector(7 downto 0);
signal state_t:std_logic_vector(3 downto 0);
signal cnt_t:std_logic_vector(3 downto 0);
begin
p1:process(clk1,clk2)
begin
if(clk1'event and clk1='1')then
state_t<="0111";
boat_t<="00000001";
cnt_t<="0000";
win_t<='0';
fail_t<='0';
if(r='1')then --r='1'
boat_t<="00000001";
cnt_t<="0000";
win<='0';
fail<='0';
state_t<="0111";
else
if(state_t="0010" or state_t="0001" or state_t="1110" or state_t="1101")then
fail<='1';win<='0';
elsif(cnt_t>="1111")then
fail<='1';win<='0';
else
if(fail_t='0' and win_t='0' and r='0')then --start
if(c='1' and state_t(2)='1')then
if(e='1')then
state(2)<='0'; ----ok????
end if;
elsif(d='1' and state_t(1)='1')then
if(e='1')then
state_t(1)<='0';
end if;
elsif(m='1' and state_t(0)='1')then
if(e='1')then
state_t(0)<='0';
end if;
else
state_t<=state_t;
end if;
state_t(3)<=not state_t(3);
cnt_t<=cnt_t+1;
else null;
end if;
end if;
end if;
end if;
p3:process(clk3) ---用数码管显示过河 船行驶方向
begin
if(state_t(3)'event and state_t(3)='1)then ---去往目的地
for i in 0 to 7 loop
if(clk3'event and clk3='1')then
boat_t(i)<='1';
end if;
end loop;
elsif(state_t(3)'event and state_t(3)='0')then
for i in 7 downto 0 loop
if(clk3'event and clk3='1')then
boat_t(i)<='1';
end if;
end loop;
else null;
end if;
end process p3;
end behave;
分频段:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity frq is
port(
clk:in std_logic;
clk1,clk2,clk3:out std_logic
);
end frq;
architecture behave of frq is
signal cnt1:integer range 0 to 24999;--1khz,系统时钟
signal cnt2:integer range 0 to 999999;--2.5hz,摁键时钟
signal cnt3:integer range 0 to 49999;--500hz,数码管扫描时钟
signal tmp_1,tmp_2,tmp_3:std_logic;
begin
p1:process(clk)
begin
if(clk'event and clk='1')then
if cnt1=24999 then
cnt1<=0;
tmp_1<=not tmp_1;
else
cnt1<=cnt1+1;
end if;
end if;
end process p1;
clk1<=tmp_1;
p2:process(clk)
begin
if(clk'event and clk='1')then
if cnt2=999999 then
cnt2<=0;
tmp_2<=not tmp_2;
else
cnt2<=cnt2+1;
end if;
else null;
end if;
end process p2;
clk2<=tmp_2;
p3:process(clk)
begin
if(clk'event and clk='1')then
if cnt3=49999 then
cnt3<=0;
tmp_3<=not tmp_3;
else
cnt3<=cnt3+1;
end if;
else null;
end if;
end process p3;
clk3<=tmp_3;
end behave;