-----------------------640*480@60Hz(clk = 25MHz)-----------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all;
--use ieee.std_logic_signed.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
ENTITY VGA IS
PORT (
hsync,vsync : OUT std_logic;
rgb : OUT std_logic_vector(11 DOWNTO 0);
en_mouse,en_A,en_B,en_C : OUT std_logic;
rst,clk : IN std_logic;
number,sum0,sum1,sum2,coin0,coin1,coin2,change0,change1,change2 : IN integer range 0 to 9;
sell_goods : IN std_logic_vector(1 DOWNTO 0);
good_select : IN std_logic_vector(2 DOWNTO 0);
mousex: inout std_logic_vector(9 downto 0);
mousey: inout std_logic_vector(9 downto 0)
);
END VGA;
ARCHITECTURE behavior OF VGA IS
-----------------------
-- signal wire --
-----------------------
--douta1
SIGNAL addra1 : STD_LOGIC_VECTOR(16 DOWNTO 0);
SIGNAL douta1 : STD_LOGIC_VECTOR(7 DOWNTO 0);
--douta3
SIGNAL addra3 : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL douta3 : STD_LOGIC_VECTOR(7 DOWNTO 0);
--VGA
SIGNAL rgb_reg : std_logic_vector(11 DOWNTO 0) := "ZZZZZZZZZZZZ";
SIGNAL hs,vs : std_logic := '1';
signal rgb1,rgb2 : std_logic_vector(11 DOWNTO 0);
--devider clk_1s
signal clk_1s : std_logic;
-----------------------
-- component --
-----------------------
--douta1.coe
component blk_mem_gen_0
PORT (
clka : IN STD_LOGIC;
rsta : IN STD_LOGIC;
addra : IN STD_LOGIC_VECTOR(16 DOWNTO 0);
douta : OUT STD_LOGIC_VECTOR(7 DOWNTO 0)
);
END component;
--douta3.coe
component blk_mem_gen_1
Port (
clka : in STD_LOGIC;
rsta : in STD_LOGIC;
addra : in STD_LOGIC_VECTOR ( 15 downto 0 );
douta : out STD_LOGIC_VECTOR ( 7 downto 0 )
);
end component;
BEGIN
-----------------------
-- component --
-- instantiation --
-----------------------
--douta1
mydouta1 : blk_mem_gen_0 PORT map(
clka => clk,
rsta => rst,
addra =>addra1,
douta =>douta1
);
--douta3
mydouta3 : blk_mem_gen_1 Port map(
clka => clk,
rsta => rst,
addra =>addra3,
douta => douta3
);
-----------------------
-- process --
-----------------------
VGA:PROCESS(rst,clk)
variable hcount : integer range 0 to 800 := 0;
variable vcount : integer range 0 to 525 := 0;
variable cnt_1s : integer range 0 to 12500000 := 12500000;
BEGIN
if(rst = '1') then
rgb_reg <= "ZZZZZZZZZZZZ";
hs <= '1';
vs <= '1';
clk_1s <= '0';
en_mouse <= '0';
en_A <= '0';
en_B <= '0';
en_C <= '0';
hcount := 0;
vcount := 0;
cnt_1s := 12500000;
elsif(clk'EVENT AND clk = '1') then
if(hcount = 800) then
hcount := 0;
if(vcount = 525) then
vcount := 0;
else
vcount := vcount+1;
end if;
else
hcount := hcount+1;
end if;
--this is hs pulse
if((hcount >= 0) and (hcount< 96 )) then
hs <= '0';
else
hs <= '1';
end if;
--this is vs pulse
if((vcount >= 0) and (vcount < 2)) then
vs <= '0';
else
vs <= '1';
end if;
--devider clk_1s
if(cnt_1s = 0) then
cnt_1s := 12500000;
clk_1s <= not clk_1s;
else
cnt_1s := cnt_1s-1;
end if;
--mouse : 购买数量显示区域
if( mousex>=697 and mousex<724 and mousey>=238 and mousey<269 ) then
en_mouse <= '1';
else
en_mouse <= '0';
end if;
if( mousex >= 414 and mousex < 484 and mousey >= 100 and mousey < 210 ) then
en_A <= '1';
else
en_A <= '0';
end if;
if((mousex >= 539) and (mousex < 609) and (mousey >= 100) and (mousey < 210)) then
en_B <= '1';
else
en_B <= '0';
end if;
if((mousex >= 664) and (mousex < 734) and (mousey >= 100) and (mousey < 210)) then
en_C <= '1';
else
en_C <= '0';
end if;
--------------------------------------------------------------------------------------------------------------------------------------------
if((hcount >= 144) and (hcount < 784) and (vcount >= 35) and (vcount < 515)) then
--------------------------------------------------------------------------------------------------------------------------------------------
--mouse
if( (hcount >= mousex) and (hcount < (mousex+4) ) and (vcount >= mousey) and ( vcount < (mousey+4) ) ) then
rgb_reg <= "000011110000"; --green
--douta1 自动售货机字样 210*40
elsif((hcount >= 464) and (hcount < 674) and (vcount >= 40) and (vcount < 80)) then
rgb1 <= "011001100011";
addra1 <= conv_std_logic_vector(( (hcount-464) + (vcount-40)*210 ),17);
rgb2 <= '0'&douta1(7)&douta1(6)&douta1(5)&'0'&douta1(4)&douta1(3)&douta1(2)&'0'&'0'&douta1(1)&douta1(0);
rgb_reg <= rgb1 and rgb2;
--douta1 自动售货机图片(brom) 210*380
elsif((hcount >= 149) and (hcount < 369) and (vcount >= 85) and (vcount < 465)) then
addra1 <= conv_std_logic_vector(( (hcount-149) + (vcount-85)*210 + 210*40 ),17);
rgb_reg <= '0'&douta1(7)&douta1(6)&douta1(5)&'0'&douta1(4)&douta1(3)&douta1(2)&'0'&'0'&douta1(1)&douta1(0);
---------------------------------------------------------------------------------------------------------------------------------------------
--douta1 商品A图片 70*110
elsif((h