forked from lfantoniosi/mce2vga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_level.vhd
236 lines (177 loc) · 4.46 KB
/
sync_level.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
-- Detects the levels of hsync and vsync and if the video is enabled
-- 2017 Luis Antoniosi
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity sync_level is
generic(
constant vsync_level : std_logic := '1';
constant hsync_level : std_logic := '1';
constant hsync_ticks : integer := 7;
constant vsync_ticks : integer := 63;
constant noise_filter: std_logic := '1'
);
port(clk : in std_logic;
hsync : in std_logic;
vsync : in std_logic;
vblank : buffer std_logic;
hblank : buffer std_logic;
enable : in std_logic;
video_enable : buffer std_logic;
no_video : buffer std_logic;
adjust : in unsigned(2 downto 0)
);
end sync_level;
architecture behavioral of sync_level is
signal vsync_lo : integer range 0 to 1024*1024*128;
signal vsync_hi : integer range 0 to 1024*1024*128;
signal vcount : integer range 0 to 65535;
signal hcount : integer range 0 to 65535;
signal vchange : std_logic;
signal video_cnt : integer range 0 to 1024*1024*128 := 0;
signal video_trg : std_logic := '0';
signal hsync_adjust: integer range 0 to 255;
begin
process(clk, adjust)
begin
if (rising_edge(clk)) then
hsync_adjust <= hsync_ticks + to_integer(adjust);
end if;
end process;
--
-- detect hsync level, pulse duration and row interval
process(clk, hsync, hsync_adjust, enable)
variable sync : std_logic;
variable peak: integer range 0 to 65535 := 0;
begin
if (rising_edge(clk)) then
if (enable = '1') then
hblank <= '0';
if (hcount < 65535) then
hcount <= hcount + 1;
end if;
if (hsync /= sync) then
peak := peak + 1;
if (peak > hsync_adjust) then
peak := 0;
sync := hsync;
if (hsync = hsync_level) then
hblank <= '1';
hcount <= 0;
end if;
end if;
elsif noise_filter = '1' then
peak := 0;
end if;
end if;
end if;
end process;
process(clk, hblank, vblank, enable)
begin
if (rising_edge(clk)) then
if (enable = '1') then
if (hblank = '1' and vcount < 65535) then
vcount <= vcount + 1;
end if;
if (vblank = '1') then
vcount <= 0;
end if;
end if;
end if;
end process;
-- detect vsync level, pulse duration and frame interval
process(clk, vsync, enable)
variable sync : std_logic;
variable peak: integer range 0 to 65535 := 0;
begin
if (rising_edge(clk)) then
if (enable = '1') then
vblank <= '0';
vchange <= '0';
if (vsync /= sync) then
peak := peak + 1;
if (peak > vsync_ticks) then
vchange <= '1';
peak := 0;
sync := vsync;
if (vsync = vsync_level) then
vblank <= '1';
end if;
end if;
elsif noise_filter = '1' then
peak := 0;
end if;
end if;
end if;
end process;
process(clk, vsync, vchange, enable)
variable peak_lo: integer range 0 to (1024*1024*16-1);
begin
if (rising_edge(clk)) then
if (enable = '1') then
if(vsync = '0' or vsync = 'Z') then
if (peak_lo < (1024*1024*16-1)) then
peak_lo := peak_lo + 1;
end if;
if(vchange = '1') then
vsync_lo <= peak_lo;
peak_lo := 0;
end if;
end if;
end if;
end if;
end process;
process(clk, vsync, vchange, enable)
variable peak_hi: integer range 0 to (1024*1024*16-1);
begin
if (rising_edge(clk)) then
if (enable = '1') then
if (vsync = '1') then
if (peak_hi < (1024*1024*16-1)) then
peak_hi := peak_hi + 1;
end if;
if(vchange = '1') then
vsync_hi <= peak_hi;
peak_hi := 0;
end if;
end if;
end if;
end if;
end process;
process(clk, enable)
begin
if (rising_edge(clk)) then
if (enable = '1') then
video_trg <= '0';
if (video_cnt > 0) then
video_cnt <= video_cnt - 1;
else
video_cnt <= 1024 * 1024 * 10;
video_trg <= '1';
end if;
end if;
end if;
end process;
process(clk, video_trg, vsync_lo, vsync_hi, enable)
begin
if (rising_edge(clk)) then
if (enable = '1') then
if (video_trg = '1') then
if (hcount = 65535) then
video_enable <= '0';
no_video <= '0';
elsif ((vsync_level = '0' and vsync_lo < vsync_hi) or (vsync_level = '1' and vsync_hi < vsync_lo)) then
video_enable <= '1';
no_video <= '0';
else
video_enable <= '0';
no_video <= '0';
end if;
end if;
else
video_enable <= '0';
no_video <= '0';
end if;
end if;
end process;
end behavioral;