Advertisement
NovaYoshi

VGA module

Jan 27th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns / 1ps
  2.  
  3. module vga2(
  4.     input clk,
  5.     input rst_n,
  6.     output hsyncOut,
  7.     output vsyncOut,
  8.     output [2:0] Red,
  9.     output [2:0] Green,
  10.     output [1:0] Blue,
  11.     output [9:0] hCountOut,
  12.     output [9:0] vCountOut,
  13.     input [2:0] RedIn,
  14.     input [2:0] GreenIn,
  15.     input [1:0] BlueIn
  16.     );
  17.    
  18.     reg divide_by_2;
  19.     reg clock2;
  20.     reg [9:0] hCount;
  21.     reg [9:0] vCount;
  22.     reg [9:0] nextHCount;
  23.     reg [9:0] nextVCount;
  24.     reg hsync;
  25.     reg vsync;
  26.     assign hsyncOut = hsync;
  27.     assign vsyncOut = vsync;
  28.     assign hCountOut = hCount;
  29.     assign vCountOut = vCount;
  30.     wire inRange = hCount < 639 && vCount < 479;
  31.    
  32.     reg [2:0] RedOut;
  33.     reg [2:0] GreenOut;
  34.     reg [1:0] BlueOut;
  35.     assign Red = RedOut;
  36.     assign Green = GreenOut;
  37.     assign Blue = BlueOut;
  38.  
  39.     // 100% --> 50%
  40.     always @(posedge clk or negedge rst_n) begin
  41.         if (rst_n == 0) begin
  42.             clock2 <= 0;
  43.         end else begin
  44.             clock2 <= !clock2;
  45.         end
  46.     end
  47.  
  48.     // 50% --> 25%
  49.     always @(posedge clock2 or negedge rst_n) begin
  50.         if (rst_n == 0) begin
  51.             divide_by_2 = 0;
  52.         end else begin
  53.             divide_by_2 = !divide_by_2;
  54.         end
  55.     end
  56.  
  57.     always @(posedge divide_by_2 or negedge rst_n) begin
  58.         if (rst_n == 0) begin
  59.             hCount <= 640;
  60.             vCount <= 480;
  61.             nextHCount <= 641;
  62.             nextVCount <= 481;
  63.         end else begin
  64.             hCount <= (hCount >= 799) ? 0 : hCount + 1;
  65.             vCount <= (hCount >= 799) ? ((vCount >= 524) ? 0 : vCount + 1) : vCount;
  66.  
  67.             nextHCount <= (nextHCount >= 799) ? 0 : hCount + 1;
  68.             nextVCount <= (nextHCount >= 799) ? ((nextVCount >= 524) ? 0 : vCount + 1) : nextVCount;
  69.            
  70.             vsync <= (vCount >= 490 && vCount < 492);
  71.             hsync <= (hCount >= 656 && hCount < 752);
  72.        
  73.             RedOut <= inRange ? RedIn : 0;
  74.             GreenOut <= inRange ? GreenIn : 0;
  75.             BlueOut <= inRange ? BlueIn : 0;
  76.         end
  77.     end
  78.  
  79. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement