|
| 1 | +library ieee; |
| 2 | +use ieee.std_logic_1164.all; |
| 3 | +use ieee.numeric_std.all; |
| 4 | + |
| 5 | +entity vga is |
| 6 | + port( |
| 7 | + clk : in std_logic; -- Pixel clock |
| 8 | + rst : in std_logic; |
| 9 | + |
| 10 | + hsync : out std_logic; |
| 11 | + vsync : out std_logic; |
| 12 | + |
| 13 | + -- Input colors |
| 14 | + i_red : in std_logic_vector(1 downto 0); |
| 15 | + i_blue : in std_logic_vector(1 downto 0); |
| 16 | + i_green : in std_logic_vector(1 downto 0); |
| 17 | + |
| 18 | + -- Output colors |
| 19 | + -- 4 x 4 x 4 yields 64 different colors |
| 20 | + o_red : out std_logic_vector(1 downto 0); |
| 21 | + o_blue : out std_logic_vector(1 downto 0); |
| 22 | + o_green : out std_logic_vector(1 downto 0) |
| 23 | + ); |
| 24 | +end entity vga; |
| 25 | + |
| 26 | +architecture RTL of vga is |
| 27 | + |
| 28 | + -- Timing - 1024 x 768 @ 60Hz |
| 29 | + -- 65 MHz Pixel Clock |
| 30 | + |
| 31 | + constant H_VISIBLE_AREA : natural := 1024; |
| 32 | + constant H_FRONT_PORCH : natural := H_VISIBLE_AREA + 0; |
| 33 | + constant H_SYNC_PULSE : natural := H_FRONT_PORCH + 24; |
| 34 | + constant H_BACK_PORCH : natural := H_SYNC_PULSE + 136; |
| 35 | + constant H_WHOLE_LINE : natural := H_BACK_PORCH + 160; |
| 36 | + |
| 37 | + constant V_VISIBLE_AREA : natural := 768; |
| 38 | + constant V_FRONT_PORCH : natural := V_VISIBLE_AREA + 0; |
| 39 | + constant V_SYNC_PULSE : natural := V_FRONT_PORCH + 3; |
| 40 | + constant V_BACK_PORCH : natural := V_SYNC_PULSE + 6; |
| 41 | + constant V_WHOLE_LINE : natural := V_BACK_PORCH + 29; |
| 42 | + |
| 43 | + -- Data for test |
| 44 | + constant WHITE_PIXEL : std_logic_vector(5 downto 0) := (others => '1'); |
| 45 | + |
| 46 | + alias R : std_logic_vector(1 downto 0) is WHITE_PIXEL(5 downto 4); |
| 47 | + alias G : std_logic_vector(1 downto 0) is WHITE_PIXEL(3 downto 2); |
| 48 | + alias B : std_logic_vector(1 downto 0) is WHITE_PIXEL(1 downto 0); |
| 49 | + |
| 50 | + procedure SyncCount(signal count : inout natural; |
| 51 | + constant wrap : in natural; |
| 52 | + constant enable : in boolean; |
| 53 | + variable wrapped : out boolean) is |
| 54 | + begin |
| 55 | + if enable then |
| 56 | + if count < wrap then |
| 57 | + count <= count + 1; |
| 58 | + wrapped := false; |
| 59 | + else |
| 60 | + count <= 0; |
| 61 | + wrapped := true; |
| 62 | + end if; |
| 63 | + end if; |
| 64 | + end procedure; |
| 65 | + |
| 66 | + signal h_count : natural := 0; |
| 67 | + signal v_count : natural := 0; |
| 68 | + |
| 69 | + |
| 70 | +begin |
| 71 | + |
| 72 | + process(clk) |
| 73 | + |
| 74 | + variable wrapped : boolean; |
| 75 | + |
| 76 | + begin |
| 77 | + |
| 78 | + if rising_edge(clk) then |
| 79 | + |
| 80 | + if rst='1' then |
| 81 | + |
| 82 | + o_red <= (others => '0'); |
| 83 | + o_blue <= (others => '0'); |
| 84 | + o_green <= (others => '0'); |
| 85 | + |
| 86 | + else |
| 87 | + |
| 88 | + SyncCount(h_count, H_WHOLE_LINE, true, wrapped); |
| 89 | + SyncCount(v_count, V_WHOLE_LINE, wrapped, wrapped); |
| 90 | + |
| 91 | + -- Sync Pulses |
| 92 | + if h_count = H_SYNC_PULSE then |
| 93 | + hsync <= '0'; |
| 94 | + elsif h_count = H_BACK_PORCH then |
| 95 | + hsync <= '1'; |
| 96 | + end if; |
| 97 | + |
| 98 | + if v_count = V_SYNC_PULSE then |
| 99 | + vsync <= '0'; |
| 100 | + elsif v_count = V_BACK_PORCH then |
| 101 | + vsync <= '1'; |
| 102 | + end if; |
| 103 | + |
| 104 | + -- To display or not to display |
| 105 | + if h_count < H_VISIBLE_AREA and v_count < V_VISIBLE_AREA then |
| 106 | + |
| 107 | + o_red <= R; |
| 108 | + o_green <= G; |
| 109 | + o_blue <= B; |
| 110 | + |
| 111 | + else |
| 112 | + |
| 113 | + o_red <= (others => '0'); |
| 114 | + o_green <= (others => '0'); |
| 115 | + o_blue <= (others =>'0'); |
| 116 | + |
| 117 | + end if; |
| 118 | + |
| 119 | + end if; |
| 120 | + |
| 121 | + end if; |
| 122 | + |
| 123 | + end process; |
| 124 | +end architecture RTL; |
0 commit comments