No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vga_ctrl.vhd 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date: 04/25/2014 02:10:40 PM
  6. -- Design Name:
  7. -- Module Name: vga_ctrl - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool Versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_ARITH.ALL;
  23. use IEEE.std_logic_unsigned.all;
  24. use ieee.math_real.all;
  25. -- Uncomment the following library declaration if using
  26. -- arithmetic functions with Signed or Unsigned values
  27. --use IEEE.NUMERIC_STD.ALL;
  28. -- Uncomment the following library declaration if instantiating
  29. -- any Xilinx leaf cells in this code.
  30. --library UNISIM;
  31. --use UNISIM.VComponents.all;
  32. entity vga_ctrl is
  33. Port ( CLK_I : in STD_LOGIC;
  34. VGA_HS_O : out STD_LOGIC;
  35. VGA_VS_O : out STD_LOGIC;
  36. VGA_RED_O : out STD_LOGIC_VECTOR (3 downto 0);
  37. VGA_BLUE_O : out STD_LOGIC_VECTOR (3 downto 0);
  38. VGA_GREEN_O : out STD_LOGIC_VECTOR (3 downto 0);
  39. PS2_CLK : inout STD_LOGIC;
  40. PS2_DATA : inout STD_LOGIC
  41. );
  42. end vga_ctrl;
  43. architecture Behavioral of vga_ctrl is
  44. COMPONENT MouseCtl
  45. GENERIC
  46. (
  47. SYSCLK_FREQUENCY_HZ : integer := 100000000;
  48. CHECK_PERIOD_MS : integer := 500;
  49. TIMEOUT_PERIOD_MS : integer := 100
  50. );
  51. PORT(
  52. clk : IN std_logic;
  53. rst : IN std_logic;
  54. value : IN std_logic_vector(11 downto 0);
  55. setx : IN std_logic;
  56. sety : IN std_logic;
  57. setmax_x : IN std_logic;
  58. setmax_y : IN std_logic;
  59. ps2_clk : INOUT std_logic;
  60. ps2_data : INOUT std_logic;
  61. xpos : OUT std_logic_vector(11 downto 0);
  62. ypos : OUT std_logic_vector(11 downto 0);
  63. zpos : OUT std_logic_vector(3 downto 0);
  64. left : OUT std_logic;
  65. middle : OUT std_logic;
  66. right : OUT std_logic;
  67. new_event : OUT std_logic
  68. );
  69. END COMPONENT;
  70. COMPONENT MouseDisplay
  71. PORT(
  72. pixel_clk : IN std_logic;
  73. xpos : IN std_logic_vector(11 downto 0);
  74. ypos : IN std_logic_vector(11 downto 0);
  75. hcount : IN std_logic_vector(11 downto 0);
  76. vcount : IN std_logic_vector(11 downto 0);
  77. enable_mouse_display_out : OUT std_logic;
  78. red_out : OUT std_logic_vector(3 downto 0);
  79. green_out : OUT std_logic_vector(3 downto 0);
  80. blue_out : OUT std_logic_vector(3 downto 0)
  81. );
  82. END COMPONENT;
  83. component clk_wiz_0
  84. port
  85. (-- Clock in ports
  86. clk_in1 : in std_logic;
  87. -- Clock out ports
  88. clk_out1 : out std_logic
  89. );
  90. end component;
  91. --***1280x1024@60Hz***--
  92. constant FRAME_WIDTH : natural := 1280;
  93. constant FRAME_HEIGHT : natural := 1024;
  94. constant H_FP : natural := 48; --H front porch width (pixels)
  95. constant H_PW : natural := 112; --H sync pulse width (pixels)
  96. constant H_MAX : natural := 1688; --H total period (pixels)
  97. constant V_FP : natural := 1; --V front porch width (lines)
  98. constant V_PW : natural := 3; --V sync pulse width (lines)
  99. constant V_MAX : natural := 1066; --V total period (lines)
  100. constant H_POL : std_logic := '1';
  101. constant V_POL : std_logic := '1';
  102. -------------------------------------------------------------------------
  103. -- VGA Controller specific signals: Counters, Sync, R, G, B
  104. -------------------------------------------------------------------------
  105. -- Pixel clock, in this case 108 MHz
  106. signal pxl_clk : std_logic;
  107. -- The active signal is used to signal the active region of the screen (when not blank)
  108. signal active : std_logic;
  109. -- Horizontal and Vertical counters
  110. signal h_cntr_reg : std_logic_vector(11 downto 0) := (others =>'0');
  111. signal v_cntr_reg : std_logic_vector(11 downto 0) := (others =>'0');
  112. -- Pipe Horizontal and Vertical Counters
  113. signal h_cntr_reg_dly : std_logic_vector(11 downto 0) := (others => '0');
  114. signal v_cntr_reg_dly : std_logic_vector(11 downto 0) := (others => '0');
  115. -- Horizontal and Vertical Sync
  116. signal h_sync_reg : std_logic := not(H_POL);
  117. signal v_sync_reg : std_logic := not(V_POL);
  118. -- Pipe Horizontal and Vertical Sync
  119. signal h_sync_reg_dly : std_logic := not(H_POL);
  120. signal v_sync_reg_dly : std_logic := not(V_POL);
  121. -- VGA R, G and B signals coming from the main multiplexers
  122. signal vga_red_cmb : std_logic_vector(3 downto 0);
  123. signal vga_green_cmb : std_logic_vector(3 downto 0);
  124. signal vga_blue_cmb : std_logic_vector(3 downto 0);
  125. --The main VGA R, G and B signals, validated by active
  126. signal vga_red : std_logic_vector(3 downto 0);
  127. signal vga_green : std_logic_vector(3 downto 0);
  128. signal vga_blue : std_logic_vector(3 downto 0);
  129. -- Register VGA R, G and B signals
  130. signal vga_red_reg : std_logic_vector(3 downto 0) := (others =>'0');
  131. signal vga_green_reg : std_logic_vector(3 downto 0) := (others =>'0');
  132. signal vga_blue_reg : std_logic_vector(3 downto 0) := (others =>'0');
  133. -------------------------------------------------------------------------
  134. --Mouse pointer signals
  135. -------------------------------------------------------------------------
  136. -- Mouse data signals
  137. signal MOUSE_X_POS: std_logic_vector (11 downto 0);
  138. signal MOUSE_Y_POS: std_logic_vector (11 downto 0);
  139. signal MOUSE_X_POS_REG: std_logic_vector (11 downto 0);
  140. signal MOUSE_Y_POS_REG: std_logic_vector (11 downto 0);
  141. -- Mouse cursor display signals
  142. signal mouse_cursor_red : std_logic_vector (3 downto 0) := (others => '0');
  143. signal mouse_cursor_blue : std_logic_vector (3 downto 0) := (others => '0');
  144. signal mouse_cursor_green : std_logic_vector (3 downto 0) := (others => '0');
  145. -- Mouse cursor enable display signals
  146. signal enable_mouse_display: std_logic;
  147. -- Registered Mouse cursor display signals
  148. signal mouse_cursor_red_dly : std_logic_vector (3 downto 0) := (others => '0');
  149. signal mouse_cursor_blue_dly : std_logic_vector (3 downto 0) := (others => '0');
  150. signal mouse_cursor_green_dly : std_logic_vector (3 downto 0) := (others => '0');
  151. -- Registered Mouse cursor enable display signals
  152. signal enable_mouse_display_dly : std_logic;
  153. -----------------------------------------------------------
  154. -- Signals for generating the background (moving colorbar)
  155. -----------------------------------------------------------
  156. signal cntDyn : integer range 0 to 2**28-1; -- counter for generating the colorbar
  157. signal intHcnt : integer range 0 to H_MAX - 1;
  158. signal intVcnt : integer range 0 to V_MAX - 1;
  159. -- Colorbar red, greeen and blue signals
  160. signal bg_red : std_logic_vector(3 downto 0);
  161. signal bg_blue : std_logic_vector(3 downto 0);
  162. signal bg_green : std_logic_vector(3 downto 0);
  163. -- Pipe the colorbar red, green and blue signals
  164. signal bg_red_dly : std_logic_vector(3 downto 0) := (others => '0');
  165. signal bg_green_dly : std_logic_vector(3 downto 0) := (others => '0');
  166. signal bg_blue_dly : std_logic_vector(3 downto 0) := (others => '0');
  167. begin
  168. clk_wiz_0_inst : clk_wiz_0
  169. port map
  170. (
  171. clk_in1 => CLK_I,
  172. clk_out1 => pxl_clk);
  173. ----------------------------------------------------------------------------------
  174. -- Mouse Controller
  175. ----------------------------------------------------------------------------------
  176. Inst_MouseCtl: MouseCtl
  177. GENERIC MAP
  178. (
  179. SYSCLK_FREQUENCY_HZ => 108000000,
  180. CHECK_PERIOD_MS => 500,
  181. TIMEOUT_PERIOD_MS => 100
  182. )
  183. PORT MAP
  184. (
  185. clk => pxl_clk,
  186. rst => '0',
  187. xpos => MOUSE_X_POS,
  188. ypos => MOUSE_Y_POS,
  189. zpos => open,
  190. left => open,
  191. middle => open,
  192. right => open,
  193. new_event => open,
  194. value => x"000",
  195. setx => '0',
  196. sety => '0',
  197. setmax_x => '0',
  198. setmax_y => '0',
  199. ps2_clk => PS2_CLK,
  200. ps2_data => PS2_DATA
  201. );
  202. ---------------------------------------------------------------
  203. -- Generate Horizontal, Vertical counters and the Sync signals
  204. ---------------------------------------------------------------
  205. -- Horizontal counter
  206. process (pxl_clk)
  207. begin
  208. if (rising_edge(pxl_clk)) then
  209. if (h_cntr_reg = (H_MAX - 1)) then
  210. h_cntr_reg <= (others =>'0');
  211. else
  212. h_cntr_reg <= h_cntr_reg + 1;
  213. end if;
  214. end if;
  215. end process;
  216. -- Vertical counter
  217. process (pxl_clk)
  218. begin
  219. if (rising_edge(pxl_clk)) then
  220. if ((h_cntr_reg = (H_MAX - 1)) and (v_cntr_reg = (V_MAX - 1))) then
  221. v_cntr_reg <= (others =>'0');
  222. elsif (h_cntr_reg = (H_MAX - 1)) then
  223. v_cntr_reg <= v_cntr_reg + 1;
  224. end if;
  225. end if;
  226. end process;
  227. -- Horizontal sync
  228. process (pxl_clk)
  229. begin
  230. if (rising_edge(pxl_clk)) then
  231. if (h_cntr_reg >= (H_FP + FRAME_WIDTH - 1)) and (h_cntr_reg < (H_FP + FRAME_WIDTH + H_PW - 1)) then
  232. h_sync_reg <= H_POL;
  233. else
  234. h_sync_reg <= not(H_POL);
  235. end if;
  236. end if;
  237. end process;
  238. -- Vertical sync
  239. process (pxl_clk)
  240. begin
  241. if (rising_edge(pxl_clk)) then
  242. if (v_cntr_reg >= (V_FP + FRAME_HEIGHT - 1)) and (v_cntr_reg < (V_FP + FRAME_HEIGHT + V_PW - 1)) then
  243. v_sync_reg <= V_POL;
  244. else
  245. v_sync_reg <= not(V_POL);
  246. end if;
  247. end if;
  248. end process;
  249. --------------------
  250. -- The active
  251. --------------------
  252. -- active signal
  253. active <= '1' when h_cntr_reg_dly < FRAME_WIDTH and v_cntr_reg_dly < FRAME_HEIGHT
  254. else '0';
  255. --------------------
  256. -- Register Inputs
  257. --------------------
  258. register_inputs: process (pxl_clk)
  259. begin
  260. if (rising_edge(pxl_clk)) then
  261. if v_sync_reg = V_POL then
  262. MOUSE_X_POS_REG <= MOUSE_X_POS;
  263. MOUSE_Y_POS_REG <= MOUSE_Y_POS;
  264. end if;
  265. end if;
  266. end process register_inputs;
  267. ---------------------------------------
  268. -- Generate moving colorbar background
  269. ---------------------------------------
  270. process(pxl_clk)
  271. begin
  272. if(rising_edge(pxl_clk)) then
  273. cntdyn <= cntdyn + 1;
  274. end if;
  275. end process;
  276. intHcnt <= conv_integer(h_cntr_reg);
  277. intVcnt <= conv_integer(v_cntr_reg);
  278. bg_red <= conv_std_logic_vector((-intvcnt - inthcnt - cntDyn/2**20),8)(7 downto 4);
  279. bg_green <= conv_std_logic_vector((inthcnt - cntDyn/2**20),8)(7 downto 4);
  280. bg_blue <= conv_std_logic_vector((intvcnt - cntDyn/2**20),8)(7 downto 4);
  281. ----------------------------------
  282. -- Mouse Cursor display instance
  283. ----------------------------------
  284. Inst_MouseDisplay: MouseDisplay
  285. PORT MAP
  286. (
  287. pixel_clk => pxl_clk,
  288. xpos => MOUSE_X_POS_REG,
  289. ypos => MOUSE_Y_POS_REG,
  290. hcount => h_cntr_reg,
  291. vcount => v_cntr_reg,
  292. enable_mouse_display_out => enable_mouse_display,
  293. red_out => mouse_cursor_red,
  294. green_out => mouse_cursor_green,
  295. blue_out => mouse_cursor_blue
  296. );
  297. ---------------------------------------------------------------------------------------------------
  298. -- Register Outputs coming from the displaying components and the horizontal and vertical counters
  299. ---------------------------------------------------------------------------------------------------
  300. process (pxl_clk)
  301. begin
  302. if (rising_edge(pxl_clk)) then
  303. bg_red_dly <= bg_red;
  304. bg_green_dly <= bg_green;
  305. bg_blue_dly <= bg_blue;
  306. mouse_cursor_red_dly <= mouse_cursor_red;
  307. mouse_cursor_blue_dly <= mouse_cursor_blue;
  308. mouse_cursor_green_dly <= mouse_cursor_green;
  309. enable_mouse_display_dly <= enable_mouse_display;
  310. h_cntr_reg_dly <= h_cntr_reg;
  311. v_cntr_reg_dly <= v_cntr_reg;
  312. end if;
  313. end process;
  314. ----------------------------------
  315. -- VGA Output Muxing
  316. ----------------------------------
  317. vga_red <= mouse_cursor_red_dly when enable_mouse_display_dly = '1' else
  318. bg_red_dly;
  319. vga_green <= mouse_cursor_green_dly when enable_mouse_display_dly = '1' else
  320. bg_green_dly;
  321. vga_blue <= mouse_cursor_blue_dly when enable_mouse_display_dly = '1' else
  322. bg_blue_dly;
  323. ------------------------------------------------------------
  324. -- Turn Off VGA RBG Signals if outside of the active screen
  325. -- Make a 4-bit AND logic with the R, G and B signals
  326. ------------------------------------------------------------
  327. vga_red_cmb <= (active & active & active & active) and vga_red;
  328. vga_green_cmb <= (active & active & active & active) and vga_green;
  329. vga_blue_cmb <= (active & active & active & active) and vga_blue;
  330. -- Register Outputs
  331. process (pxl_clk)
  332. begin
  333. if (rising_edge(pxl_clk)) then
  334. v_sync_reg_dly <= v_sync_reg;
  335. h_sync_reg_dly <= h_sync_reg;
  336. vga_red_reg <= vga_red_cmb;
  337. vga_green_reg <= vga_green_cmb;
  338. vga_blue_reg <= vga_blue_cmb;
  339. end if;
  340. end process;
  341. -- Assign outputs
  342. VGA_HS_O <= h_sync_reg_dly;
  343. VGA_VS_O <= v_sync_reg_dly;
  344. VGA_RED_O <= vga_red_reg;
  345. VGA_GREEN_O <= vga_green_reg;
  346. VGA_BLUE_O <= vga_blue_reg;
  347. end Behavioral;