-
Notifications
You must be signed in to change notification settings - Fork 0
/
vga_util.vhd
289 lines (253 loc) · 9.19 KB
/
vga_util.vhd
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
package vga_util is
type vga_videomode is record
width: positive;
height: positive;
refresh_rate: positive;
end record;
type vga_hsync_timings is record
frontporch: positive;
syncpulse: positive;
backporch: positive;
activevideo: positive;
end record;
type vga_vsync_timings is record
frontporch: positive;
syncpulse: positive;
backporch: positive;
activevideo: positive;
end record;
type vga_sync_timings is record
h: vga_hsync_timings;
v: vga_vsync_timings;
end record;
type vga_hstate is (HFrontPorch, HSyncPulse, HBackPorch, HActiveVideo);
type vga_vstate is (VFrontPorch, VSyncPulse, VBackPorch, VActiveVideo);
function get_max_timing(timings: vga_hsync_timings) return positive;
function get_max_timing(timings: vga_vsync_timings) return positive;
function get_timings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_sync_timings;
function get_timings_from_videomode(mode: vga_videomode)
return vga_sync_timings;
function get_htimings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_hsync_timings;
function get_htimings_from_videomode(mode: vga_videomode)
return vga_hsync_timings;
function get_timer_limit(timings: vga_hsync_timings; cur_state: vga_hstate)
return positive;
function get_vtimings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_vsync_timings;
function get_vtimings_from_videomode(mode: vga_videomode)
return vga_vsync_timings;
function get_timer_limit(timings: vga_vsync_timings; cur_state: vga_vstate)
return positive;
function get_next_vga_state(state: vga_hstate) return vga_hstate;
function get_next_vga_state(state: vga_vstate) return vga_vstate;
component vga_hsync_ctrl is
generic (
timings: vga_hsync_timings
);
port (
clk, en, reset: in std_logic;
hsync: out std_logic := '0';
timer: out natural range 0 to get_max_timing(timings) - 1 := 0;
state: out vga_hstate := vga_hstate'left
);
end component;
component vga_vsync_ctrl is
generic (
timings: vga_vsync_timings
);
port (
clk, en, reset: in std_logic;
vsync: out std_logic := '0';
timer: out natural range 0 to get_max_timing(timings) - 1 := 0;
state: out vga_vstate := vga_vstate'left
);
end component;
component vga_sync_ctrl is
generic (
timings: vga_sync_timings
);
port (
clk, en, reset: in std_logic;
hsync, vsync: out std_logic;
htimer: out natural range 0 to get_max_timing(timings.h);
vtimer: out natural range 0 to get_max_timing(timings.v);
hstate: out vga_hstate;
vstate: out vga_vstate
);
end component;
component vga_pixel_clock is
port (
clk: in std_logic;
pixel_clk: out std_logic
);
end component;
end package;
package body vga_util is
function get_max_timing(timings: vga_hsync_timings)
return positive
is
variable a1: positive := timings.frontporch;
variable a2: positive := timings.syncpulse;
variable a3: positive := timings.backporch;
variable a4: positive := timings.activevideo;
variable max12: positive := a1;
variable max34: positive := a3;
begin
if a1 > a2 then max12 := a1; else max12 := a2; end if;
if a3 > a4 then max34 := a3; else max34 := a4; end if;
if max12 > max34 then return max12; else return max34; end if;
end function;
function get_max_timing(timings: vga_vsync_timings)
return positive
is
variable a1: positive := timings.frontporch;
variable a2: positive := timings.syncpulse;
variable a3: positive := timings.backporch;
variable a4: positive := timings.activevideo;
variable max12: positive := a1;
variable max34: positive := a3;
begin
if a1 > a2 then max12 := a1; else max12 := a2; end if;
if a3 > a4 then max34 := a3; else max34 := a4; end if;
if max12 > max34 then return max12; else return max34; end if;
end function;
function get_timings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_sync_timings
is
variable ret: vga_sync_timings;
begin
ret.h := get_htimings_from_videomode(width, height, refresh_rate);
ret.v := get_vtimings_from_videomode(width, height, refresh_rate);
return ret;
end function;
function get_timings_from_videomode(mode: vga_videomode)
return vga_sync_timings
is
variable ret: vga_sync_timings;
begin
ret.h := get_htimings_from_videomode(mode);
ret.v := get_vtimings_from_videomode(mode);
return ret;
end function;
function get_htimings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_hsync_timings
is
type triplet is array (natural range 0 to 2) of positive;
variable videomode: triplet := (width, height, refresh_rate);
begin
if videomode = (640, 480, 60) then
return vga_hsync_timings'(
frontporch => 16,
syncpulse => 96,
backporch => 48,
activevideo => 640
);
else
report "Invalid videomode submitted to 'get_htimings_from_mode'"
& ", namely " & positive'image(width)
& " * " & positive'image(height)
& " @ " & positive'image(refresh_rate)
& " - resorting to default 640 * 480 @ 60"
severity error;
return vga_hsync_timings'(
frontporch => 16,
syncpulse => 96,
backporch => 48,
activevideo => 640
);
end if;
end function;
function get_htimings_from_videomode(mode: vga_videomode)
return vga_hsync_timings is
begin
return get_htimings_from_videomode(mode.width, mode.height,
mode.refresh_rate);
end function;
function get_timer_limit(timings: vga_hsync_timings; cur_state: vga_hstate)
return positive
is
begin
case cur_state is
when HFrontPorch => return timings.frontporch - 1;
when HSyncPulse => return timings.syncpulse - 1;
when HBackPorch => return timings.backporch - 1;
when HActiveVideo => return timings.activevideo - 1;
end case;
end function;
function get_vtimings_from_videomode(width: positive; height: positive;
refresh_rate: positive) return vga_vsync_timings
is
type triplet is array (natural range 0 to 2) of positive;
variable videomode: triplet := (width, height, refresh_rate);
begin
if videomode = (640, 480, 60) then
return vga_vsync_timings'(
frontporch => 10 * 800,
syncpulse => 2 * 800,
backporch => 33 * 800,
activevideo => 480 * 800
);
else
report "Invalid videomode submitted to 'get_vtimings_from_mode'"
& ", namely " & positive'image(width)
& " * " & positive'image(height)
& " @ " & positive'image(refresh_rate)
& " - resorting to default 640 * 480 @ 60"
severity error;
return vga_vsync_timings'(
frontporch => 10 * 800,
syncpulse => 2 * 800,
backporch => 33 * 800,
activevideo => 480 * 800
);
end if;
end function;
function get_vtimings_from_videomode(mode: vga_videomode)
return vga_vsync_timings is
begin
return get_vtimings_from_videomode(mode.width, mode.height,
mode.refresh_rate);
end function;
function get_timer_limit(timings: vga_vsync_timings; cur_state: vga_vstate)
return positive
is
begin
case cur_state is
when VFrontPorch => return timings.frontporch - 1;
when VSyncPulse => return timings.syncpulse - 1;
when VBackPorch => return timings.backporch - 1;
when VActiveVideo => return timings.activevideo - 1;
end case;
end function;
function get_next_vga_state(state: vga_hstate) return vga_hstate is
begin
case state is
when HFrontPorch =>
return HSyncPulse;
when HSyncPulse =>
return HBackPorch;
when HBackPorch =>
return HActiveVideo;
when HActiveVideo =>
return HFrontPorch;
end case;
end function;
function get_next_vga_state(state: vga_vstate) return vga_vstate is
begin
case state is
when VFrontPorch =>
return VSyncPulse;
when VSyncPulse =>
return VBackPorch;
when VBackPorch =>
return VActiveVideo;
when VActiveVideo =>
return VFrontPorch;
end case;
end function;
end package body;