⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 target.vhd

📁 基于spartan3火龙刀系列FPGA开发板制作的VGA实验例程
💻 VHD
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity target is
    Port ( clk : in std_logic;
           reset : in std_logic;
           hcnt : in std_logic_vector(9 downto 0);
           vcnt : in std_logic_vector(9 downto 0);
           mouse_x : in std_logic_vector(9 downto 0);
           ballx : in std_logic_vector(9 downto 0);
           bally : in std_logic_vector(9 downto 0);
           hit_target : out std_logic;
           targetrgb : out std_logic_vector(7 downto 0));
end target;

architecture Behavioral of target is

CONSTANT FRAME_WIDTH : INTEGER :=20;
CONSTANT TAR_WIDTH : INTEGER :=10;
CONSTANT TAR_LEN : INTEGER :=20;
CONSTANT BALL_R : INTEGER :=5;

signal movclk,sampclk : std_logic;
signal sampcnt : std_logic_vector(5 downto 0);
signal hit : std_logic;
signal rgbout : std_logic_vector(7 downto 0);
signal loc,mov_x,tempmov : std_logic_vector(9 downto 0);

begin

movclk <= vcnt(9);
sampclk <= sampcnt(5);

sampcount: process(movclk,reset)
begin
  if reset='0' then
    sampcnt <= "000000";
  elsif (movclk'event and movclk='1') then
    sampcnt <= sampcnt + '1';
  end if;
end process;

drawtarget: process(clk)
begin
  if (clk'event and clk='1') then
    if ((hcnt>(loc-TAR_LEN)) and (hcnt<(loc+TAR_LEN))) and ((vcnt>=FRAME_WIDTH) and (vcnt<=(FRAME_WIDTH + TAR_WIDTH-1))) then
      rgbout <= "00111111";
  	 else
	   rgbout <= "00000000";
    end if;
  end if;
end process;

movable: process(movclk,reset)
begin
  if reset='0' then
    loc <= (others=>'0');
  elsif (movclk'event and movclk='1') then
    loc <= loc + mov_x;
	 --if loc>=770 then
	  -- loc <= "1100000001";
	 --elsif loc<=29 then
	  -- loc <= "0000011100";
	 --end if;
  end if;
end process;

sampspeed: process(sampclk,reset)
begin
  if reset='0' then
    tempmov <= "0000000001";
  elsif (sampclk'event and sampclk='1') then
    tempmov <= mouse_x;
  end if;
end process;

detectmov: process(movclk,reset)
begin
  if reset='0' then
    mov_x <= "0000000001";
  elsif (movclk'event and movclk='1') then
    if (loc>=(640- FRAME_WIDTH - TAR_LEN)) and (mov_x(9)='0') then
	   mov_x <= not mov_x + "0000000001";
	 elsif (loc<=(FRAME_WIDTH + TAR_LEN-1)) and (mov_x(9)='1') then
	   mov_x <= not mov_x + "0000000001";
 	 else 
	   mov_x <= tempmov;
	 end if;
  end if;
end process;

detecthit: process(movclk,reset)
begin
  if reset='0' then
    hit <= '0';
  elsif (movclk'event and movclk='1') then
    if (bally<=(FRAME_WIDTH + TAR_WIDTH + BALL_R)) then
 	   if (ballx>=(loc-TAR_LEN+1)) and (ballx<=(loc+TAR_LEN-1)) then
		  hit <= '1';
		else
		  hit <= '0';
		end if;
	 else
	   hit <= '0';
	 end if;
  end if;
end process;

hit_target <= hit;
targetrgb <= rgbout;

end Behavioral;

⌨️ 快捷键说明

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