Skip to content

Instantly share code, notes, and snippets.

@Kenta11
Created May 9, 2020 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kenta11/eac55636205832478118f051398c4167 to your computer and use it in GitHub Desktop.
Save Kenta11/eac55636205832478118f051398c4167 to your computer and use it in GitHub Desktop.
VGA controler
// Copyright (c) 2020 Kenta Arai
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
`timescale 1ns / 1ps
module controler(
input clk,
input rst,
output [16:0] address,
input [11:0] data,
output [3:0] vgaRed,
output [3:0] vgaBlue,
output [3:0] vgaGreen,
output hsync,
output vsync
);
localparam [9:0] WIDTH = 640;
localparam [9:0] HEIGHT = 480;
localparam [9:0] BEGINNING_OF_HORIZONTAL_SYNC = 16;
localparam [9:0] END_OF_HORIZONTAL_SYNC = BEGINNING_OF_HORIZONTAL_SYNC + 96;
localparam [9:0] BEGINNING_OF_DISPLAY_PIXELS_H = END_OF_HORIZONTAL_SYNC + 48;
localparam [9:0] END_OF_ROW = BEGINNING_OF_DISPLAY_PIXELS_H + WIDTH;
localparam [9:0] BEGINNING_OF_VERTICAL_SYNC = 10;
localparam [9:0] END_OF_VERTICAL_SYNC = BEGINNING_OF_VERTICAL_SYNC + 2;
localparam [9:0] BEGINNING_OF_DISPLAY_PIXELS_V = END_OF_VERTICAL_SYNC + 33;
localparam [9:0] END_OF_COLUMN = BEGINNING_OF_DISPLAY_PIXELS_V + HEIGHT;
reg [9:0] h_count;
always@(posedge clk)
begin
if (rst)
h_count <= 0;
else
h_count <= (h_count == END_OF_ROW - 1) ? 0 : h_count + 1;
end
reg [9:0] v_count;
always@(posedge clk)
begin
if (rst)
v_count <= 0;
else if (h_count == END_OF_ROW - 1)
v_count <= (v_count == END_OF_COLUMN - 1) ? 0 : v_count + 1;
end
reg [16:0] base_address;
always@(posedge clk)
begin
if (rst)
base_address <= 0;
else
if (v_count < BEGINNING_OF_DISPLAY_PIXELS_V)
base_address <= 0;
else if ((h_count == END_OF_ROW - 1) & (v_count[0] == 0))
base_address <= base_address + WIDTH / 2;
end
reg [16:0] offset;
always@(posedge clk)
begin
if (rst)
offset <= 0;
else
if (h_count < BEGINNING_OF_DISPLAY_PIXELS_H)
offset <= 0;
else if (h_count[0] == 1)
offset <= offset + 1;
end
assign address = base_address + offset;
assign {vgaRed, vgaBlue, vgaGreen} = data;
assign hsync = ~((BEGINNING_OF_HORIZONTAL_SYNC <= h_count) & (h_count < END_OF_HORIZONTAL_SYNC));
assign vsync = ~((BEGINNING_OF_VERTICAL_SYNC <= v_count) & (v_count < END_OF_VERTICAL_SYNC));
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment