-
Notifications
You must be signed in to change notification settings - Fork 12
/
vga_sync.v
170 lines (152 loc) · 4.25 KB
/
vga_sync.v
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//------------------------------------------------------------------------------
// Felipe Machado Sanchez
// Area de Tecnologia Electronica
// Universidad Rey Juan Carlos
// https://github.com/felipe-m
// using a 50 MHz clk
module vga_sync
#(parameter
c_pxl_visible = 640,
c_pxl_fporch = 16,
// from 0 to front porch
c_pxl_2_fporch = c_pxl_visible + c_pxl_fporch, // 656
c_pxl_synch = 96,
// from 0 to synch
c_pxl_2_synch = c_pxl_2_fporch + c_pxl_synch, // 752
// total horizontal pixels
c_pxl_total = 800,
// number of pixels of the backporch
c_pxl_bporch = c_pxl_total - c_pxl_2_synch, // 48
// --------------- Number of rows (Vertical) ----------
c_line_visible = 480,
c_line_fporch = 9,
// from 0 to front porch (vertical)
c_line_2_fporch = c_line_visible + c_line_fporch, // 489
c_line_synch = 2,
// from init to synchro (vertical)
c_line_2_synch = c_line_2_fporch + c_line_synch, // 491
// total number of lines
c_line_total = 520,
// number of lines of the back porch
c_line_bporch = c_line_total - c_line_2_synch, // 29
// number of bits for each count
c_nb_pxls = $clog2(c_pxl_total), // 10, //c_pxl_total : 800,
c_nb_lines = $clog2(c_line_total), // 10, //c_line_total : 520,
// VGA frequency
c_freq_vga = 25*10**6, // VGA 25MHz
// active level of synchronization
c_synch_act = 0
)
(
input rst,
input clk,
output visible,
output new_pxl,
output reg hsync,
output reg vsync,
output [10-1:0] col,
output [10-1:0] row
);
reg cnt_clk; // count 0 to 1: 2 clk cycles, from 50MHz to 25MHz
reg [10-1:0] cnt_pxl;
reg [10-1:0] cnt_line;
wire end_cnt_pxl;
wire end_cnt_line;
wire new_line;
reg visible_pxl;
reg visible_line;
// count 2 clock cycles to get a pixel cycle
always @ (posedge rst, posedge clk)
begin
if (rst)
cnt_clk <= 1'b0;
else
cnt_clk <= ~cnt_clk;
end
assign new_pxl = cnt_clk;
assign col = cnt_pxl;
assign row = cnt_line;
assign visible = visible_pxl && visible_line;
// counting 800 pixels (columns)
always @ (posedge rst, posedge clk)
begin
if (rst)
cnt_pxl <= 10'd0;
else begin
if (new_pxl) begin
if (end_cnt_pxl)
cnt_pxl <= 10'd0;
else
cnt_pxl <= cnt_pxl + 1;
end
end
end
// end of pixel count
assign end_cnt_pxl = (cnt_pxl == c_pxl_total-1) ? 1'b1 : 1'b0;
// new line: when in the end of the count and there is a new pixel
assign new_line = end_cnt_pxl && new_pxl;
// combinational outputs of pixel count (horizontal)
always @ (rst or cnt_pxl)
begin
if (rst) begin
visible_pxl = 1'b0;
hsync = ~c_synch_act;
end
else if (cnt_pxl < c_pxl_visible) begin
visible_pxl = 1'b1;
hsync = ~c_synch_act;
end
else if (cnt_pxl < c_pxl_2_fporch) begin
visible_pxl = 1'b0;
hsync = ~c_synch_act;
end
else if (cnt_pxl < c_pxl_2_synch) begin
visible_pxl = 1'b0;
hsync = c_synch_act; // synch active
end
else begin
visible_pxl = 1'b0;
hsync = ~c_synch_act;
end
end
// counting 520 lines (rows)
always @ (posedge rst, posedge clk)
begin
if (rst)
cnt_line <= 10'd0;
else begin
if (new_line) begin
if (end_cnt_line)
cnt_line <= 10'd0;
else
cnt_line <= cnt_line + 1;
end
end
end
// end of pixel count
assign end_cnt_line = (cnt_line == c_line_total-1) ? 1'b1 : 1'b0;
// combinational outputs of line count (vertical)
always @ (rst or cnt_line)
begin
if (rst) begin
visible_line = 1'b0;
vsync = ~c_synch_act;
end
else if (cnt_line < c_line_visible) begin
visible_line = 1'b1;
vsync = ~c_synch_act;
end
else if (cnt_line < c_line_2_fporch) begin
visible_line = 1'b0;
vsync = ~c_synch_act;
end
else if (cnt_line < c_line_2_synch) begin
visible_line = 1'b0;
vsync = c_synch_act; // synch active
end
else begin
visible_line = 1'b0;
vsync = ~c_synch_act;
end
end
endmodule