1、数字系统设计与硬件描述语言期末考试作业题目: 数字式竞赛抢答器设计 学院: 电子信息工程学院专业: 电子信息工程学号: 3009204150姓名: 蒋溯南 2012-06-12一、 选题设计描述1. 基本要求:至少可容纳 3 组参赛者同时抢答,具有抢答成功、提前抢答等警示,具有计分功能,打对计分,打错不计分或者扣分。2. 本设计基本性能介绍:可同时容纳 4 位抢答者,完全满足题设要求。3. 算法简介:分三模块进行编程:计分模块,抢答确认及警告模块,计时模块二、 程序源代码及说明1. 抢答确认模块:library ieee;use ieee.std_logic_1164.all;use ieee
2、.std_logic_unsigned.all;entity stay isport(clk,en,rst:in std_logic;warn:out std_logic;-提前抢答警告back:buffer std_logic;-反馈k1,k2,k3,k4:in std_logic;table:out std_logic_vector(3 downto 0);-台号end stay;architecture one of stay issignal foul:std_logic;- 警告signal cnt:std_logic_vector(2 downto 0);-计数beginp1:pr
3、ocess(k1,rst,k2,k3,k4,back)-抢答确认beginif rst=1 then back=1;table=“0000“;elsif back=1 thenif k1=1 thentable=“0001“;back=0;elsif k2=1 thentable=“0010“;back=0;elsif k3=1 thentable=“0011“;back=0;elsif k4=1 thentable=“0100“;back=0;else back=1;table=“0000“;end if ;end if;end process p1;p3:process(clk,en,ba
4、ck,rst,cnt)-提前抢答警告beginif rst=1 thencnt=“000“;foul=0;elsif clkevent and clk=1 thenif en=0 and back=0 thenif cnt“111“ thenfoul=not foul;cnt=cnt+1;-计数警告取反else foul=0;end if;end if;end if;end process p3;warn=foul;end one;2. 计分模块:library ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;e
5、ntity mark isport(clk,up,dn:in std_logic;-加减分数table:in std_logic_vector(3 downto 0);-台号a1,a2,a3,a4:buffer std_logic_vector(3 downto 0);-得分end mark;architecture one of mark isbeginprocess(clk,table,a1,a2,a3,a4,up,dn)-加减分数beginif clkevent and clk=1 thenif table=“0001“ then if up=1 thenif a1=“1111“ the
6、na1=“0000“;else a1=a1+1;end if;elsif dn=1 then if a1=“0000“ thena1=“0000“;else a1=a1-1;end if;end if;elsif table=“0010“ thenif up=1 thenif a1=“1111“ thena2=“0000“;else a2=a2+1;end if;elsif dn=1 then if a2=“0000“ thena2=“0000“;else a2=a2-1;end if;end if;elsif table=“0011“ thenif up=1 thenif a3=“1111“
7、 thena3=“0000“;else a3=a3+1;end if;elsif dn=1 then if a3=“0000“ thena3=“0000“;else a3=a3-1;end if;end if;elsif table=“0100“ thenif up=1 thenif a4=“1111“ thena4=“0000“;else a4=a4+1;end if;elsif dn=1 then if a4=“0000“ thena4=“0000“;else a4=a4-1;end if;end if;end if;end if;end process;end one;3. 计时模块:l
8、ibrary ieee;use ieee.std_logic_1164.all;use ieee.std_logic_unsigned.all;entity count_dn isport(clk,rst,en,stop:in std_logic;-stop 由反馈信号 BACKwarn20:buffer std_logic;-倒计时警告信号cnt_one:buffer std_logic_vector(3 downto 0);-倒计时个位cnt_ten:buffer std_logic_vector(3 downto 0);-倒计时十位end count_dn;architecture on
9、e of count_dn issignal cout,tmp:std_logic;-警告信号,count 进位signal score:std_logic_vector(3 downto 0);-计数beginp1:process(clk,rst,en,stop,cnt_one)-个位倒计时beginif rst=1 thencnt_one=“0000“;elsif stop=0 thencnt_one=“0000“;elsif clkevent and clk=1 thencout=0;if en=1 thencnt_one=cnt_one-1;if cnt_one=“0000“ then
10、if cnt_ten=“0000“ then cnt_one=“0000“;else cnt_one=“1001“;cout=1;end if;end if;end if;end if;end process p1;p2:process(cout,rst,en,stop,cnt_ten)-十位倒计时beginif rst=1 thencnt_ten=“0010“;elsif stop=0 thencnt_ten=“0010“;elsif coutevent and cout=1 thenif en=1 thenif cnt_ten=“0000“ thencnt_ten=“0000“; else
11、 cnt_ten=cnt_ten-1;end if;end if;end if;end process p2;p3:process(rst,cout,cnt_one,cnt_ten,stop)-计时超时警告beginif rst=1 then score=“0000“;tmp=0;elsif clkevent and clk=1 then if stop=1 and cnt_one=“0000“ and cnt_ten=“0000“ then if score“1010“ then tmp=not tmp;score=score+1;else tmp=0;end if;end if;end if;end process p3;warn20=tmp;end one;三、 仿真结果及分析