This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
forked from hany606/Real-time-edge-detection-on-FPGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGA.v.bak
139 lines (103 loc) · 3.16 KB
/
VGA.v.bak
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module VGA(
input clk, // Clock -50 MHz
input reset, // reset pin
input [2:0] pixel_rgb, // pixel value
output hsync, vsync, // sync signals
output red, green, blue, // output RGB data to the monitor
output active, // Active when pixel inside the 640 x 480 area
output ptick, // Pixel clock
output [9:0] xpos, ypos // Current pixel position
);
// Horizontal -> pixels
// Vertical-> lines
localparam
HPIXELS = 640,
HA = HPIXELS - 1, // Hor. active area (0 to 639 = 640 pixels)
HFP = HA + 16, // Hor. front porch end position
HSYNC = HFP + 96, // Hor. sync end position
HBP = HSYNC + 48, // Hor. back porch end position
VPIXELS = 480,
VA = VPIXELS - 1, // Vert. active area (0 to 479 = 480 pixels)
VFP = VA + 11, // Vert. front porch end position
VSYNC = VFP + 2, // Vert. sync end position
VBP = VSYNC + 31; // Vert. back porch end position
// Mod-2 counter
reg mod2_r;
wire mod2_next;
wire ptick_w;
// Sync counters
reg [9:0] hcount, hcount_next;
reg [9:0] vcount, vcount_next;
// Sync output buffers
reg vsync_r, hsync_r;
wire vsync_next, hsync_next;
// RGB signal buffer
reg red_r, green_r, blue_r;
// Status signals
wire h_end, v_end;
always @ (posedge clk or posedge reset) begin
if (reset) begin
mod2_r <= 1'b0;
vcount <= 0;
hcount <= 0;
vsync_r <= 1'b0;
hsync_r <= 1'b0;
red_r <= 1'b0;
green_r <= 1'b0;
blue_r <= 1'b0;
end
else begin
mod2_r <= mod2_next;
vcount <= vcount_next;
hcount <= hcount_next;
vsync_r <= vsync_next;
hsync_r <= hsync_next;
red_r <= pixel_rgb[0];
green_r <= pixel_rgb[1];
blue_r <= pixel_rgb[2];
end
end
// Mod-2 circuit to generate the 25 MHz tick
assign mod2_next = ~mod2_r;
assign ptick_w = mod2_r;
// End of horizontal line counter (799)
assign h_end = (hcount == HBP);
// End of vertical (524)
assign v_end = (vcount == VBP);
// Next-state logic of mod-800 horizontal sync counter
always @(*) begin
if (ptick_w) // 25 MHz pixel tick
if (h_end) // End of line ?
hcount_next = 0;
else
hcount_next = hcount + 10'd1;
else
hcount_next = hcount;
end
// Next-state logic of mod-525 vertical sync counter
always @(*) begin
if (ptick_w & h_end) // 25 MHz pixel tick and end of line
if (v_end) // End of screen ?
vcount_next = 0;
else
vcount_next = vcount + 10'd1;
else
vcount_next = vcount;
end
// Horizontal and vertical sync, and f_tick, buffered to avoid glitches
// hsync_next reset between 656 and 752
assign hsync_next = ~((hcount > HFP) && (hcount <= HSYNC));
// vsync_next reset between 491 and 493
assign vsync_next = ~((vcount > VFP) && (vcount <= VSYNC));
// active when the current position is inside the visible area
assign active = (hcount <= HA) && (vcount <= VA);
// Outputs
assign hsync = hsync_r;
assign vsync = vsync_r;
assign xpos = hcount;
assign ypos = vcount;
assign ptick = ptick_w;
assign red = active ? red_r : 1'b0;
assign green = active ? green_r : 1'b0;
assign blue = active ? blue_r : 1'b0;
endmodule