数字电路综合
实验报告
信息与通信工程学院
班级:2009211120班
姓名: 李 川
学号: 09210591
题目:经典数学游戏
一.任务要求
实验内容:一个人要将1只狗,1只猫,1只老鼠渡过河,独木舟一次只
能装载和一只动物,但猫和狗不能单独在一起,而猫和老鼠也不能友好相处,试模拟这个人将三只动物安全渡过河的过程。 用发光二极管亮点的移动模拟独木舟渡河的过程,选中渡河的动物及两岸的动物都应有显示,若选错应有报警显示,且游戏失败,按复位键游戏重新开始。当三只动物均安全度过河时,游戏成功,并显示此次游戏独木舟往返渡河的次数。提高要求:游戏难度可设置,选错动物允许有一次修改机会。
二.系统设计
设计思路:用4个按钮分别用来选择人,狗,猫,鼠,8个LED来表示
人和动物的状态,左边4个代表左岸状态,右边4个代表右岸状态,船用点阵上的LED灯表示,灯右移代表船从左岸划到右岸,左移代表船从右岸划到左岸,失败和成功另外用点阵上的2个点表示。 总体框图及分块设计:
系统结构总框
系统结构图(模块划分和传递关系)
逻辑划分框图
MDS图
控制逻辑图(画在预习报告中,无法复制,截屏)
三.仿真波形及仿真分析
2ms时钟分频仿真图,clk1周期为1ms,是自己所需的,证明计算的分频的数
字正确,4Hz的clk2周期太长,就不进行仿真
数码管的仿真图,in:num,out:
step
模拟渡河的一步(错误的一步,报错fail=1)
复位,reset=1,pl=‘1111’,pr=‘0000’,各自复位
仿真图后面随便乱加的,因为我觉得此实验仿真过程较为复杂,还不如直接下载到板子上来找问题方便直接,故仿真图很粗糙。
四.源程序
--过河游戏--
--顶层模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; use ieee.std_logic_arith.all; entity gh1 is
port( clock: in std_logic; --时钟信号,50MHz mank,dogk,catk,ratk: in std_logic; --人,狗,猫,老鼠的开关信号 enterk: in std_logic; --确认开关信号 resetk: in std_logic; --复位开关信号 pl,pr : out std_logic_vector(3 downto 0); --河两岸动物的状态 boat : out std_logic_vector(13 downto 0); --船灯信号 lose,win : out std_logic; --失败、成功的信号 steph,stepl : out std_logic_vector(6 downto 0));--数码管的驱动信号 end;
architecture jg of gh1 is component fenpin --调用分频器模块 port( cp: in std_logic; cp1,cp2:out std_logic ) ; end component; component fangdou --调用防抖动模块。 port( clk,key : in std_logic; q : out std_logic); end component; component shumaguan --调用译码显示模块。
port( num : in std_logic_vector(3 downto 0); step : out std_logic_vector(6 downto 0)); end component; component guohe --调用过河控制模块。 port( clk,clk1 : in std_logic; man,dog,cat,rat : in std_logic; enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0); counth,countl : out std_logic_vector(3 downto 0); boats : out std_logic_vector(13 downto 0); fail,success : out std_logic); end component;
signal temp1,temp2,temp3,c1,c2 : std_logic; signal temp4,temp5,temp6 : std_logic;
signal coh,col : std_logic_vector(3 downto 0);
begin u1 : fenpin port map(clock,c1,c2); u2 : fangdou port map(c1,mank,temp1); u3 : fangdou port map(c1,dogk,temp2); u4 : fangdou port map(c1,catk,temp3); u5 : fangdou port map(c1,ratk,temp4); u6 : fangdou port map(c1,enterk,temp5); u7 : fangdou port map(c1,resetk,temp6); u8 : guohe map(c1,c2,temp1,temp2,temp3,temp4,temp5,temp6,pl,pr,coh,col,boat,lose,win); u9 : shumaguan port map(coh,steph); u10: shumaguan port map(col,stepl); end jg; --防抖模块 library ieee;
use ieee.std_logic_1164.all; entity fangdou is
port(clk : in std_logic; --时钟信号,1 kHz。 key : in std_logic; --有抖动的开关信号。
q : out std_logic); --防抖动电路输出的无抖动信号。 end;
architecture fangdou1 of fangdou is signal cp : std_logic;
signal jsp : integer range 0 to 3; begin
port
process(clk,cp) begin
if(clk'event and clk='0')then if key='1'then if jsp=3 then jsp
jsp
if jsp=1 then cp
end process; end;
--分频模块 library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity fenpin is port(cp : in std_logic; --时钟信号,50MHz cp1,cp2 : out std_logic); --1 kHz、2.5 Hz 的信号 end fenpin; architecture struct of fenpin is signal clk1,clk2,clk3 : std_logic; signal temp : std_logic_vector(6 downto 0); signal temp1 : std_logic_vector(8 downto 0); signal temp2 : std_logic_vector(8 downto 0); begin fp1:process(cp) begin
if rising_edge(cp) then if(temp="1100011") then clk1
else temp
fp2:process(clk1) begin
if rising_edge(clk1) then if(temp1="111101111") then clk2
end process fp2;
fp3:process(clk2) begin
if rising_edge(clk2) then if(temp2="110001111") then clk3
end process fp3;
cp1
--过河控制模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity guohe is
port( clk,clk1:in std_logic; man,dogman,dog,cat,rat : in std_logic; 关动作信号 enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0);
--人、狗、猫和鼠的开--确认开关信号。 --复位开关信号。
--河两岸动物的状态。
counth,countl : out std_logic_vector(3 downto 0); --过河次数。 boats : out std_logic_vector(13 downto 0); --船灯信号。 fail,success : out std_logic); end guohe;
architecture struct of guohe is
signal ch,cl : std_logic_vector(3 downto 0); signal state : std_logic_vector(3 downto 0); signal enable : std_logic; signal river,river1 : std_logic; signal side : std_logic;
signal boat : std_logic_vector(13 downto 0); signal first : std_logic;
signal ms,ds,cs,rs : std_logic; begin
p1 :process(clk) begin
if(rising_edge(clk)) then if(reset='1') then state
if( river='0' and river1='0' ) then if(enable='0') then if(man='1') then state(3)
if(dog='1') then
state(2)
--成功、警告信号。 --复位信号有效。 --右边状态全为'0',同时--若不处于运输过程。 --按下表示人的开关。 --人的状态改变。
end if;
if(cat='1') then
state(1)
if(rat='1') then
state(0)
if(enter='1') then --对过河次数的处理。 if(cl="1001") then if(ch="1001") then
ch
--若过河次数的高位也计到9,则判为失败。 else
ch
cl
side
--失败和成功的判断。 if (ms='0') then
fail
elsif(ds='1' and state(3)='0' and state(2)='1')then fail
elsif(cs='1' and state(3)='1' and state(1)='0')then fail
elsif(cs='1' and state(3)='0' and state(1)='1')then fail
elsif(rs='1' and state(3)='1' and state(0)='0')then fail
elsif(rs='1' and state(3)='0' and state(0)='1')then fail
fail
--有确认信号。
--人没有上船,过河失败。
fail
fail
elsif(state="1001" or state="1100" or state="0011" or state="0110" or state="0111" or state="1000") then fail
if(state="1111" ) then success
ms
--运输过程。 else
if(side='1' and boat(0)='1')then river1
elsif(side='0' and boat(2)='1')then river1
end process p1;
p2 :process(clk1,cl,ch) begin
if(cl
if (rising_edge(clk1)) then if (river1='0') then if (enable='0') then if (side='0') then boat
elsif(side='1')then
boat
boat
--若从左往右运输且右边的船灯已--若从右往左运输且左边的船灯已 --第一次运输开始前。 --左边的船灯 --人在左边。 --左边的船灯
if (side='1') then --从左往右运输。 boat(0)
boat(2)
boat(1)
end process p2;
p3 :process(clk1,first) begin
if(first='0')then --初始状态。 for i in 0 to 3 loop
pl(i)
if(rising_edge(clk1))then
if(side='1' and boat(2)= '1')then --从左往右运输且左边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
elsif(side='1' and boat(0)= '1')then
--从左往右运输且右边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(0)= '1')then
--从右往左运输且右边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(2)= '1')then
--从右往左运输且左边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
end process p3;
counth
--数码显示模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity guohe is
port( clk,clk1:in std_logic; man,dogman,dog,cat,rat : in std_logic; 关动作信号 enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0); counth,countl : out std_logic_vector(3 downto 0); boats : out std_logic_vector(13 downto 0); fail,success : out std_logic); end guohe;
architecture struct of guohe is
signal ch,cl : std_logic_vector(3 downto 0); signal state : std_logic_vector(3 downto 0); signal enable : std_logic; signal river,river1 : std_logic; signal side : std_logic;
--人、狗、猫和鼠的开--确认开关信号。 --复位开关信号。
--河两岸动物的状态。 --过河次数。 --船灯信号。 --成功、警告信号。
signal boat : std_logic_vector(13 downto 0); signal first : std_logic;
signal ms,ds,cs,rs : std_logic; begin
p1 :process(clk) begin
if(rising_edge(clk)) then if(reset='1') then state
if( river='0' and river1='0' ) then if(enable='0') then if(man='1') then
state(3)
ms
if(dog='1') then
state(2)
if(cat='1') then
state(1)
if(rat='1') then
state(0)
--复位信号有效。 --右边状态全为'0',同时
--若不处于运输过程。 --按下表示人的开关。
--人的状态改变。
if(enter='1') then --对过河次数的处理。 if(cl="1001") then if(ch="1001") then
ch
--若过河次数的高位也计到9,则判为失败。 else
ch
cl
side
--失败和成功的判断。 if (ms='0') then
fail
elsif(ds='1' and state(3)='0' and state(2)='1')then fail
elsif(cs='1' and state(3)='1' and state(1)='0')then fail
elsif(cs='1' and state(3)='0' and state(1)='1')then fail
elsif(rs='1' and state(3)='1' and state(0)='0')then fail
elsif(rs='1' and state(3)='0' and state(0)='1')then fail
fail
fail
fail
elsif(state="1001" or state="1100" or state="0011" or state="0110" or state="0111" or state="1000") then fail
if(state="1111" ) then success
--有确认信号。 --人没有上船,过河失败。
ms
--运输过程。 else
if(side='1' and boat(0)='1')then river1
elsif(side='0' and boat(2)='1')then river1
end process p1;
p2 :process(clk1,cl,ch) begin
if(cl
if (rising_edge(clk1)) then if (river1='0') then if (enable='0') then if (side='0') then boat
elsif(side='1')then
boat
boat
if (side='1') then boat(0)
boat(2)
boat(1)
--第一次运输开始前。 --左边的船灯 --人在左边。 --左边的船灯 --从左往右运输。 -- 船灯从左往右依次亮。
end if; end if; end if; end if;
end process p2;
p3 :process(clk1,first) begin
if(first='0')then --初始状态。 for i in 0 to 3 loop
pl(i)
if(rising_edge(clk1))then
if(side='1' and boat(2)= '1')then --从左往右运输且左边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
elsif(side='1' and boat(0)= '1')then
--从左往右运输且右边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(0)= '1')then
--从右往左运输且右边的船灯亮,即运输刚开始。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(2)= '1')then
--从右往左运输且左边的船灯亮,即运输结束。 for i in 0 to 3 loop
if(state(i)= '0')then pl(i)
pl(i)
end process p3;
counth
五.功能说明
六.元器件清单及资源利用情况 元器件清单:
LED灯:8个 按钮: 6个 七段数码管 点阵
资源利用情况:
左右岸动物均有表示,选中的动物灯熄灭
点阵点的移动代表船的渡河动作 成功或失败均有提示
船每过河一次,数码管显示+1
任何时候按下复位键,游戏重新开始
七.故障及问题分析
编译时,考虑最多的是过河的逻辑,没有考虑太多的与硬件的结合编程,到第三次检查时最重要的过河逻辑也没有编出来,当然无仿真图可言,后面是自己在网上找的一段代码,虽然起先编译未能通过,但问题不大,只用改改些许地方就行了,他采用的是if嵌套,套了7,8层if,起先看这段代码看不懂,后面把每个if和end if对应起来打印出来,再仔细想了几个小时才弄明白。
最初仿真时,因为自己过河动作的频率较低,故所需仿真的时间较长。得1S左右吧,仿真时发现总是到49%时总是不动了,后来才想到由于自己输入的是50MHz的频率,而自己需要的频率又很小,计算机进行此仿真会有大量的计算,导致仿真过慢,这个问题老师第一节课可能也说过,自己没注意。后来又以为如果要仿真就得改代码,故进行各个模块的仿真时都是另建工程再改代码进行仿真,而且忘保存了,不知道报告需要仿真图,后来才发现模块之间的信号是可以进行仿真的,可为时已晚。
进行下载时,有一次下载完后按按钮总没有反应,后面经同学提醒才知道有个按钮得按下,至于这个按钮的作用,不甚了解。 八.总结和结论 此次数电综合实验虽然不是自己完全自己独立完成(上网找了代码)的,但自己仍然学到了不少东西,一是熟悉了VHDL语言,对VHDL进行逻辑电路的结构方式也了解更深,特别是对process的理解和component的应用。另外,也因为此次实验只有自己选这一个题目,很多具体逻辑问题和实际问题都只有靠自己解决,问同学只能解决一部分,真正要完全解决只有靠自己,因此自己也得到了不少锻炼。但是自己也注意到自己编程还是没有考虑很多关于硬件的问题,更多的只是注意软件,这个自己后面还得好好锻炼。另外,对于状态机的认识也不够,希望老师在以后授课时可以让同学们真正学到怎么设计状态机,怎么利用状态机来进行编译等。
数字电路综合
实验报告
信息与通信工程学院
班级:2009211120班
姓名: 李 川
学号: 09210591
题目:经典数学游戏
一.任务要求
实验内容:一个人要将1只狗,1只猫,1只老鼠渡过河,独木舟一次只
能装载和一只动物,但猫和狗不能单独在一起,而猫和老鼠也不能友好相处,试模拟这个人将三只动物安全渡过河的过程。 用发光二极管亮点的移动模拟独木舟渡河的过程,选中渡河的动物及两岸的动物都应有显示,若选错应有报警显示,且游戏失败,按复位键游戏重新开始。当三只动物均安全度过河时,游戏成功,并显示此次游戏独木舟往返渡河的次数。提高要求:游戏难度可设置,选错动物允许有一次修改机会。
二.系统设计
设计思路:用4个按钮分别用来选择人,狗,猫,鼠,8个LED来表示
人和动物的状态,左边4个代表左岸状态,右边4个代表右岸状态,船用点阵上的LED灯表示,灯右移代表船从左岸划到右岸,左移代表船从右岸划到左岸,失败和成功另外用点阵上的2个点表示。 总体框图及分块设计:
系统结构总框
系统结构图(模块划分和传递关系)
逻辑划分框图
MDS图
控制逻辑图(画在预习报告中,无法复制,截屏)
三.仿真波形及仿真分析
2ms时钟分频仿真图,clk1周期为1ms,是自己所需的,证明计算的分频的数
字正确,4Hz的clk2周期太长,就不进行仿真
数码管的仿真图,in:num,out:
step
模拟渡河的一步(错误的一步,报错fail=1)
复位,reset=1,pl=‘1111’,pr=‘0000’,各自复位
仿真图后面随便乱加的,因为我觉得此实验仿真过程较为复杂,还不如直接下载到板子上来找问题方便直接,故仿真图很粗糙。
四.源程序
--过河游戏--
--顶层模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; use ieee.std_logic_arith.all; entity gh1 is
port( clock: in std_logic; --时钟信号,50MHz mank,dogk,catk,ratk: in std_logic; --人,狗,猫,老鼠的开关信号 enterk: in std_logic; --确认开关信号 resetk: in std_logic; --复位开关信号 pl,pr : out std_logic_vector(3 downto 0); --河两岸动物的状态 boat : out std_logic_vector(13 downto 0); --船灯信号 lose,win : out std_logic; --失败、成功的信号 steph,stepl : out std_logic_vector(6 downto 0));--数码管的驱动信号 end;
architecture jg of gh1 is component fenpin --调用分频器模块 port( cp: in std_logic; cp1,cp2:out std_logic ) ; end component; component fangdou --调用防抖动模块。 port( clk,key : in std_logic; q : out std_logic); end component; component shumaguan --调用译码显示模块。
port( num : in std_logic_vector(3 downto 0); step : out std_logic_vector(6 downto 0)); end component; component guohe --调用过河控制模块。 port( clk,clk1 : in std_logic; man,dog,cat,rat : in std_logic; enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0); counth,countl : out std_logic_vector(3 downto 0); boats : out std_logic_vector(13 downto 0); fail,success : out std_logic); end component;
signal temp1,temp2,temp3,c1,c2 : std_logic; signal temp4,temp5,temp6 : std_logic;
signal coh,col : std_logic_vector(3 downto 0);
begin u1 : fenpin port map(clock,c1,c2); u2 : fangdou port map(c1,mank,temp1); u3 : fangdou port map(c1,dogk,temp2); u4 : fangdou port map(c1,catk,temp3); u5 : fangdou port map(c1,ratk,temp4); u6 : fangdou port map(c1,enterk,temp5); u7 : fangdou port map(c1,resetk,temp6); u8 : guohe map(c1,c2,temp1,temp2,temp3,temp4,temp5,temp6,pl,pr,coh,col,boat,lose,win); u9 : shumaguan port map(coh,steph); u10: shumaguan port map(col,stepl); end jg; --防抖模块 library ieee;
use ieee.std_logic_1164.all; entity fangdou is
port(clk : in std_logic; --时钟信号,1 kHz。 key : in std_logic; --有抖动的开关信号。
q : out std_logic); --防抖动电路输出的无抖动信号。 end;
architecture fangdou1 of fangdou is signal cp : std_logic;
signal jsp : integer range 0 to 3; begin
port
process(clk,cp) begin
if(clk'event and clk='0')then if key='1'then if jsp=3 then jsp
jsp
if jsp=1 then cp
end process; end;
--分频模块 library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity fenpin is port(cp : in std_logic; --时钟信号,50MHz cp1,cp2 : out std_logic); --1 kHz、2.5 Hz 的信号 end fenpin; architecture struct of fenpin is signal clk1,clk2,clk3 : std_logic; signal temp : std_logic_vector(6 downto 0); signal temp1 : std_logic_vector(8 downto 0); signal temp2 : std_logic_vector(8 downto 0); begin fp1:process(cp) begin
if rising_edge(cp) then if(temp="1100011") then clk1
else temp
fp2:process(clk1) begin
if rising_edge(clk1) then if(temp1="111101111") then clk2
end process fp2;
fp3:process(clk2) begin
if rising_edge(clk2) then if(temp2="110001111") then clk3
end process fp3;
cp1
--过河控制模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity guohe is
port( clk,clk1:in std_logic; man,dogman,dog,cat,rat : in std_logic; 关动作信号 enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0);
--人、狗、猫和鼠的开--确认开关信号。 --复位开关信号。
--河两岸动物的状态。
counth,countl : out std_logic_vector(3 downto 0); --过河次数。 boats : out std_logic_vector(13 downto 0); --船灯信号。 fail,success : out std_logic); end guohe;
architecture struct of guohe is
signal ch,cl : std_logic_vector(3 downto 0); signal state : std_logic_vector(3 downto 0); signal enable : std_logic; signal river,river1 : std_logic; signal side : std_logic;
signal boat : std_logic_vector(13 downto 0); signal first : std_logic;
signal ms,ds,cs,rs : std_logic; begin
p1 :process(clk) begin
if(rising_edge(clk)) then if(reset='1') then state
if( river='0' and river1='0' ) then if(enable='0') then if(man='1') then state(3)
if(dog='1') then
state(2)
--成功、警告信号。 --复位信号有效。 --右边状态全为'0',同时--若不处于运输过程。 --按下表示人的开关。 --人的状态改变。
end if;
if(cat='1') then
state(1)
if(rat='1') then
state(0)
if(enter='1') then --对过河次数的处理。 if(cl="1001") then if(ch="1001") then
ch
--若过河次数的高位也计到9,则判为失败。 else
ch
cl
side
--失败和成功的判断。 if (ms='0') then
fail
elsif(ds='1' and state(3)='0' and state(2)='1')then fail
elsif(cs='1' and state(3)='1' and state(1)='0')then fail
elsif(cs='1' and state(3)='0' and state(1)='1')then fail
elsif(rs='1' and state(3)='1' and state(0)='0')then fail
elsif(rs='1' and state(3)='0' and state(0)='1')then fail
fail
--有确认信号。
--人没有上船,过河失败。
fail
fail
elsif(state="1001" or state="1100" or state="0011" or state="0110" or state="0111" or state="1000") then fail
if(state="1111" ) then success
ms
--运输过程。 else
if(side='1' and boat(0)='1')then river1
elsif(side='0' and boat(2)='1')then river1
end process p1;
p2 :process(clk1,cl,ch) begin
if(cl
if (rising_edge(clk1)) then if (river1='0') then if (enable='0') then if (side='0') then boat
elsif(side='1')then
boat
boat
--若从左往右运输且右边的船灯已--若从右往左运输且左边的船灯已 --第一次运输开始前。 --左边的船灯 --人在左边。 --左边的船灯
if (side='1') then --从左往右运输。 boat(0)
boat(2)
boat(1)
end process p2;
p3 :process(clk1,first) begin
if(first='0')then --初始状态。 for i in 0 to 3 loop
pl(i)
if(rising_edge(clk1))then
if(side='1' and boat(2)= '1')then --从左往右运输且左边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
elsif(side='1' and boat(0)= '1')then
--从左往右运输且右边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(0)= '1')then
--从右往左运输且右边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(2)= '1')then
--从右往左运输且左边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
end process p3;
counth
--数码显示模块 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity guohe is
port( clk,clk1:in std_logic; man,dogman,dog,cat,rat : in std_logic; 关动作信号 enter : in std_logic; reset : in std_logic; pl,pr : out std_logic_vector(3 downto 0); counth,countl : out std_logic_vector(3 downto 0); boats : out std_logic_vector(13 downto 0); fail,success : out std_logic); end guohe;
architecture struct of guohe is
signal ch,cl : std_logic_vector(3 downto 0); signal state : std_logic_vector(3 downto 0); signal enable : std_logic; signal river,river1 : std_logic; signal side : std_logic;
--人、狗、猫和鼠的开--确认开关信号。 --复位开关信号。
--河两岸动物的状态。 --过河次数。 --船灯信号。 --成功、警告信号。
signal boat : std_logic_vector(13 downto 0); signal first : std_logic;
signal ms,ds,cs,rs : std_logic; begin
p1 :process(clk) begin
if(rising_edge(clk)) then if(reset='1') then state
if( river='0' and river1='0' ) then if(enable='0') then if(man='1') then
state(3)
ms
if(dog='1') then
state(2)
if(cat='1') then
state(1)
if(rat='1') then
state(0)
--复位信号有效。 --右边状态全为'0',同时
--若不处于运输过程。 --按下表示人的开关。
--人的状态改变。
if(enter='1') then --对过河次数的处理。 if(cl="1001") then if(ch="1001") then
ch
--若过河次数的高位也计到9,则判为失败。 else
ch
cl
side
--失败和成功的判断。 if (ms='0') then
fail
elsif(ds='1' and state(3)='0' and state(2)='1')then fail
elsif(cs='1' and state(3)='1' and state(1)='0')then fail
elsif(cs='1' and state(3)='0' and state(1)='1')then fail
elsif(rs='1' and state(3)='1' and state(0)='0')then fail
elsif(rs='1' and state(3)='0' and state(0)='1')then fail
fail
fail
fail
elsif(state="1001" or state="1100" or state="0011" or state="0110" or state="0111" or state="1000") then fail
if(state="1111" ) then success
--有确认信号。 --人没有上船,过河失败。
ms
--运输过程。 else
if(side='1' and boat(0)='1')then river1
elsif(side='0' and boat(2)='1')then river1
end process p1;
p2 :process(clk1,cl,ch) begin
if(cl
if (rising_edge(clk1)) then if (river1='0') then if (enable='0') then if (side='0') then boat
elsif(side='1')then
boat
boat
if (side='1') then boat(0)
boat(2)
boat(1)
--第一次运输开始前。 --左边的船灯 --人在左边。 --左边的船灯 --从左往右运输。 -- 船灯从左往右依次亮。
end if; end if; end if; end if;
end process p2;
p3 :process(clk1,first) begin
if(first='0')then --初始状态。 for i in 0 to 3 loop
pl(i)
if(rising_edge(clk1))then
if(side='1' and boat(2)= '1')then --从左往右运输且左边的船灯亮,即运输刚开始。
for i in 0 to 3 loop if(state(i)= '0')then pl(i)
pl(i)
elsif(side='1' and boat(0)= '1')then
--从左往右运输且右边的船灯亮,即运输结束。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(0)= '1')then
--从右往左运输且右边的船灯亮,即运输刚开始。 for i in 0 to 3 loop if(state(i)= '0')then pr(i)
pr(i)
elsif(side='0' and boat(2)= '1')then
--从右往左运输且左边的船灯亮,即运输结束。 for i in 0 to 3 loop
if(state(i)= '0')then pl(i)
pl(i)
end process p3;
counth
五.功能说明
六.元器件清单及资源利用情况 元器件清单:
LED灯:8个 按钮: 6个 七段数码管 点阵
资源利用情况:
左右岸动物均有表示,选中的动物灯熄灭
点阵点的移动代表船的渡河动作 成功或失败均有提示
船每过河一次,数码管显示+1
任何时候按下复位键,游戏重新开始
七.故障及问题分析
编译时,考虑最多的是过河的逻辑,没有考虑太多的与硬件的结合编程,到第三次检查时最重要的过河逻辑也没有编出来,当然无仿真图可言,后面是自己在网上找的一段代码,虽然起先编译未能通过,但问题不大,只用改改些许地方就行了,他采用的是if嵌套,套了7,8层if,起先看这段代码看不懂,后面把每个if和end if对应起来打印出来,再仔细想了几个小时才弄明白。
最初仿真时,因为自己过河动作的频率较低,故所需仿真的时间较长。得1S左右吧,仿真时发现总是到49%时总是不动了,后来才想到由于自己输入的是50MHz的频率,而自己需要的频率又很小,计算机进行此仿真会有大量的计算,导致仿真过慢,这个问题老师第一节课可能也说过,自己没注意。后来又以为如果要仿真就得改代码,故进行各个模块的仿真时都是另建工程再改代码进行仿真,而且忘保存了,不知道报告需要仿真图,后来才发现模块之间的信号是可以进行仿真的,可为时已晚。
进行下载时,有一次下载完后按按钮总没有反应,后面经同学提醒才知道有个按钮得按下,至于这个按钮的作用,不甚了解。 八.总结和结论 此次数电综合实验虽然不是自己完全自己独立完成(上网找了代码)的,但自己仍然学到了不少东西,一是熟悉了VHDL语言,对VHDL进行逻辑电路的结构方式也了解更深,特别是对process的理解和component的应用。另外,也因为此次实验只有自己选这一个题目,很多具体逻辑问题和实际问题都只有靠自己解决,问同学只能解决一部分,真正要完全解决只有靠自己,因此自己也得到了不少锻炼。但是自己也注意到自己编程还是没有考虑很多关于硬件的问题,更多的只是注意软件,这个自己后面还得好好锻炼。另外,对于状态机的认识也不够,希望老师在以后授课时可以让同学们真正学到怎么设计状态机,怎么利用状态机来进行编译等。