aboutsummaryrefslogtreecommitdiff
path: root/src/example.v
blob: bf90bbe9b87c5ab55d2b3f5308213b31bcbf2ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
`default_nettype none
  
module vga_example(input wire 	    clock_100mhz,
		   input wire 	    reset,
		   
		   output reg 	    h_sync,
		   output reg 	    v_sync,
		   output reg [2:0] vga_red,
		   output reg [2:0] vga_green,
		   output reg [2:0] vga_blue
		   );
   
   wire [9:0] 			    col_next_tick;
   wire [8:0] 			    row_next_tick;
   wire 			    h_sync_next_tick;
   wire 			    v_sync_next_tick;
   wire 			    display_on_next_tick;
   
   vga vga(clock_100mhz, ~reset, h_sync_next_tick, v_sync_next_tick,
	   col_next_tick, row_next_tick, display_on_next_tick);
   
   always @ (posedge clock_100mhz) begin
      h_sync <= h_sync_next_tick;
      v_sync <= v_sync_next_tick;
      
      if (display_on_next_tick) begin
	 vga_red <= 3'b000;
	 vga_green <= row_next_tick < 220 ? 3'b111 : 3'b000;
	 vga_blue <= row_next_tick >= 220 ? 3'b111 : 3'b000;	 
      end
      else begin
	 vga_red <= 0;
	 vga_green <= 0;
	 vga_blue <= 0;
      end
   end // always @ (posedge clock_100mhz)
endmodule // vga_example

module vga(input wire       clock_100mhz,
	   input wire 	    reset,
	   
	   output reg 	    h_sync,
	   output reg 	    v_sync,
	   output reg [9:0] col,
	   output reg [8:0] row,
	   output reg 	    display_on);

   parameter h_pixels = 640;
   parameter v_pixels = 480;
   parameter h_front_porch = 16;
   parameter v_front_porch = 10;
   parameter h_pulse = 96;
   parameter v_pulse = 2;
   parameter h_back_porch = 48;
   parameter v_back_porch = 33;
   parameter h_pol = 1'b0;
   parameter v_pol = 1'b1;

   parameter h_pulse_start = h_front_porch;
   parameter v_pulse_start = v_front_porch;
   parameter h_pulse_end = h_front_porch + h_pulse;
   parameter v_pulse_end = v_front_porch + v_pulse;
   parameter h_active_video_start = h_front_porch + h_pulse + h_back_porch;
   parameter v_active_video_start = v_front_porch + v_pulse + v_back_porch;
   parameter h_frame_end = h_active_video_start + h_pixels;
   parameter v_frame_end = v_active_video_start + v_pixels;

   reg [9:0] 		    h_counter;
   reg [9:0] 		    v_counter;

   reg [1:0] 		    divider; // 25MHz

   always @ (posedge clock_100mhz) begin
      if (reset) begin
	 divider <= 2'b00;
	 h_counter <= 0;
	 v_counter <= 0;

	 h_sync <= ~h_pol;
	 v_sync <= ~v_pol;
	 row <= 0;
	 col <= 0;
	 display_on <= 0;
      end // if (reset)
      else begin
	 divider <= divider + 1;
	 
	 if (divider == 2'b11) begin
	    display_on <= (h_counter < h_frame_end - 1) &&
			  (h_counter >= h_active_video_start - 1) &&
			  (v_counter < v_frame_end) &&
			  (v_counter >= v_active_video_start);

	    if (h_counter < h_frame_end - 1) begin
	       h_counter <= h_counter + 1;

	       h_sync <= h_pol ^ (h_counter < h_pulse_start - 1 || h_counter >= h_pulse_end - 1);
	       
	       if (h_counter >= h_active_video_start)
		 col <= col + 1;
	    end
	    else begin
	       h_counter <= 0;
	       col <= 0;
	       
	       if (v_counter < v_frame_end - 1) begin
		  v_counter <= v_counter + 1;

		  v_sync <= v_pol ^ (v_counter < v_pulse_start - 1 || v_counter >= v_pulse_end - 1);

		  if (v_counter >= v_active_video_start)
		    row <= row + 1;
	       end
	       else begin
		  v_counter <= 0;
		  row <= 0;
	       end // else: !if(v_counter < v_frame_end - 1)
	    end // else: !if(h_counter < h_frame_end - 1)
	 end // if (divider == 2'b11)
      end // else: !if(reset)
   end // always @ (posedge clock_100mhz)
endmodule // vga