【正文】
e : pingponggame。 signal a,b: integer range 0 to 1。 signal i: integer range 0 to 8。 begin process(reset,hit1,hit2,hit3,hit4,clk,a,b,i) begin 進程開始 if reset=39。139。 then 異步置位 i=0。count1=00000。count2=00000。a=0。b=0。 elsif clk39。event and clk=39。139。 then 當處于時鐘 inclock 上升沿時 if count1=10101or count2=10101then i=0。count1=00000。count2=00000。 elsif startbutton=39。039。 then i=0。count1=00000。count2=00000。 else case state is when waitserve= 進程處于等待發(fā)球狀態(tài) case serve is when (01) = i=1。state=light1on。 when (10) = i=8。state=light8on。 when others= i=0。 end case。 when light1on= 進程處于第一盞燈亮狀態(tài) i=2。 if (hit2=39。139。) or (hit4=39。139。) then i=0。 count1=count1+1。state=waitserve。 else state=ballmoveto2。 end if。 when light8on= 進程處于第八盞燈亮狀態(tài) i=7。 if (hit1=39。139。) or (hit3=39。139。) then i=0。 count2=count2+1。state=waitserve。 else state=ballmoveto1。 end if。 乒乓球游戲電路設(shè)計 16 when ballmoveto1= if (hit1=39。139。) or ( hit3=39。139。) then i=0。 count2=count2+1。state=waitserve。 elsif i=2 then i=1。 state=allow13hit。 else i=i1。 end if。 when ballmoveto2= if (hit2=39。139。) or (hit4=39。139。) then i=0。 count1=count1+1。state=waitserve。 elsif i=7 then i=8。 state=allow24hit。 else i=i+1。 end if。 when allow13hit= if (hit1=39。139。) or (hit3=39。139。) then i=2。 state=ballmoveto2。 a=1。 elsif (hit3=39。139。) and (a=1) then i=2。 state=ballmoveto2。 a=0。 else count2=count2+1。i=0。 state=waitserve。 end if。 when allow24hit= if (hit2 =39。139。) and (b=0) then i=7。 state=ballmoveto1。 b=1。 elsif (hit4=39。139。) and (b=1) then i=2。 state=ballmoveto2。 b=0。 else count1=count1+1。i=0。 state=waitserve。 end if。 end case。 end if。 end if。 end process。 light=10000000 when i=1 else 01000000 when i=2 else 00100000 when i=3 else 乒乓球游戲電路設(shè)計 17 00010000 when i=4 else 00001000 when i=5 else 00000100 when i=6 else 00000010 when i=7 else 00000001 when i=8 else 00000000。 end one。 數(shù)碼管: library ieee。 use 。 use 。 use 。 entity mydecoder is port(binaryin : in std_logic_vector(1 to 5)。 5 位 二進制碼的輸入端口 bcdout1 : out std_logic_vector(1 to 7)。 七段譯碼器輸出端口 bcdout2 : out std_logic_vector(1 to 7))。 end entity mydecoder。 architecture m of mydecoder is signal tembinaryin:std_logic_vector(1 to 5)。 begin process(binaryin) begin tembinaryin=binaryin。 case tembinaryin is 把 0 到 9 的 5 位二進制碼轉(zhuǎn)換成七段譯碼 when00000=bcdout1=1111110。bcdout2=1111110。 when00001=bcdout1=1111110。bcdout2=0110000。 when00010=bcdout1=1111110。bcdout2=1101101。 when00011=bcdout1=1111110。bcdout2=1111001。 when00100=bcdout1=1111110。bcdout2=0110011。 when00101=bcdout1=1111110。bcdout2=1011011。 when00110=bcdout1=1111110。bcdout2=1011111。 when00111=bcdout1=1111110。bcdout2=1110000。 when01000=bcdout1=1111110。bcdout2=1111111。 when01001=bcdout1=1111110。bcdout2=1111011。 把 10 到 19 的 5 位二進制碼轉(zhuǎn)換成七段譯碼 when01010=bcdout1=0110000。bcdout2=1111110。 when01011=bcdout1=0110000。bcdout2=0110000。 when01100=bcdout1=0110000。bcdout2=1101101。 when01101=bcdout1=0110000。bcdout2=1111001。 when01110=bcdout1=0110000。bcdout2=0110011。 when01111=bcdout1=0110000。bcdout2=1011011。 乒乓球游戲電路設(shè)計 18 when10000=bcdout1=0110000。bcdout2=1011111。 when10001=bcdout1=0110000。bcdout2=1110000。 when10010=bcdout1=0110000。bcdout2=1111111。 when10011=bcdout1=0110000。bcdout2=1111011。 把 20 到 21 的 5 位二進制碼轉(zhuǎn)換成七段譯碼 when10100=bcdout1=1101101。bcdout2=1111110。 when10101=bcdout1=1101101。bcdout2=0110000。 如果 5 位二進制碼 不在 0 到 21 范圍內(nèi),那么兩個七段譯碼器都顯示 0 when others=bcdout1=1101101。bcdout2=1111110。 end case。 end process。 end m。