pingpang.txt

来自「乒乓球游戏机实验报告实验人: 大火虎设计课题: 用VHDL设计一个乒乓球游戏机,」· 文本 代码 · 共 125 行

TXT
125
字号
library IEEE;
USE IEEE.STD_LOGIC_1164.all;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;


ENTITY statemachine IS
	port(
		CLK	: in std_logic;
		reset	: in std_logic;
		startA,hitA,startB,hitB	:in std_logic;
		scoreA,scoreB	:in integer range 0 to 21;
		clear,increaseA,increaseB	:out std_logic;
		tablelight	:out std_logic_vector(0 to 8);
		Awin,Bwin	:out std_logic
	    );
	end;
	
		
	ARCHITECTURE behavior OF statemachine IS 
	type state_type is						

	(waitstate,AtoB,BtoA,Ascore,Bscore,finalresult);
	signal state:state_type;
	signal tablestate:integer range 0 to 8;
	begin
		process(clk,reset)
	begin
		if reset='1' then
		state<=waitstate;
		clear<='1';
		Awin<='0';
		Bwin<='0';
	elsif rising_edge(clk) then
	case state is
		when waitstate=>
		clear<='0';
		increaseA<='0';
		increaseB<='0';
		if ((scoreA=21) or (scoreB=21)) then
		
		 state<=finalresult;
		else
		 if startA='1' then
		    state<=AtoB;
		    tablestate<=0;
		 else
			if startB='1' then
			   state<=BtoA;
			   tablestate<=8;
			else
			    state<=waitstate;
			end if;
		 end if;
		end if;
		when AtoB=>
			if hitB='1' then
				if tablestate<=4 then
				 state<=Ascore;
				else
				 state<=BtoA;
				end if;
			else
				if tablestate=8 then
				 state<=Ascore;
				else
				 tablestate<=tablestate+1;
				end if;
			end if;
		when BtoA=>
			if hitA='1' then
				if tablestate>=4 then
				 state<=Bscore;
				else
				 state<=AtoB;
				end if;
			else
				if tablestate=0 then
				 state<=Bscore;
				else
				 tablestate<=tablestate-1;
				end if;
			end if;
		when Ascore=>
			increaseA<='1';
			state<=waitstate;
		when Bscore=>
			increaseB<='1';
			state<=waitstate;
		when finalresult=>
			if (scoreA=21) then
				Awin<='1';
			else
				Bwin<='1';
			end if;
		when others=>
			state<=waitstate;
		end case;
		end if;
	end process;


	process(clk)
	begin
		if falling_edge(clk) then
			if ( (state=AtoB) or (state=BtoA) ) then
			 case tablestate is
			  when 0=>tablelight<="100000000"; 	
			  when 1=>tablelight<="010000000";
			  when 2=>tablelight<="001000000";
			  when 3=>tablelight<="000100000";
			  when 4=>tablelight<="000010000";
			  when 5=>tablelight<="000001000";
			  when 6=>tablelight<="000000100";
			  when 7=>tablelight<="000000010";
			  when 8=>tablelight<="000000001";
			  when others=>tablelight<="00000000";
			 end case;
			else
			 tablelight<="000000000";
			end if;
		end if;
	end process;
	end;		

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?