-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_pipeline.vhd
361 lines (300 loc) · 8.11 KB
/
video_pipeline.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.vga_util.all;
use work.glyph_util.all;
--enum __font_config {
-- BLINK = 0x80,
-- BRIGHT = 0x08,
--
-- BG_BLACK = 0x00,
-- BG_BLUE = 0x10,
-- BG_GREEN = 0x20,
-- BG_CYAN = 0x30,
-- BG_RED = 0x40,
-- BG_MAGENTA = 0x50,
-- BG_BROWN = 0x60,
-- BG_LIGHTGRAY = 0x70,
--
-- FG_BLACK = BG_BLACK,
-- FG_BLUE = BG_BLUE >> 4,
-- FG_GREEN = BG_GREEN >> 4,
-- FG_CYAN = BG_CYAN >> 4,
-- FG_RED = BG_RED >> 4,
-- FG_MAGENTA = BG_MAGENTA >> 4,
-- FG_BROWN = BG_BROWN >> 4,
-- FG_LIGHTGRAY = BG_LIGHTGRAY >> 4
--};
entity video_pipeline is
generic (
videomode: vga_videomode := (640, 480, 60)
);
port (
clk, en, reset: in std_logic;
hsync, vsync: out std_logic := '0';
red, green, blue: out std_logic_vector(3 downto 0) := X"0";
addrb: in std_logic_vector(15 downto 0);
dib: in std_logic_vector(7 downto 0);
dob: out std_logic_vector(7 downto 0) := X"00";
enb, regceb, web: in std_logic
);
end entity;
architecture rtl of video_pipeline is
-- Predefined control values
constant CHAR_WIDTH: natural := 8;
constant CHAR_HEIGHT: natural := 8;
-- Helper variable
constant timings: vga_sync_timings := get_timings_from_videomode(videomode);
subtype htimer_T is natural range 0 to get_max_timing(timings.h);
subtype vtimer_T is natural range 0 to get_max_timing(timings.v);
type vga_beam_state is record
htimer: htimer_T;
vtimer: vtimer_T;
hstate: vga_hstate;
vstate: vga_vstate;
end record;
constant VGA_BEAM_STATE_INIT: vga_beam_state := (
htimer => 0, vtimer => 0,
hstate => vga_hstate'left, vstate => vga_vstate'left
);
type vga_text_counters is record
pixel_local_x: natural range 0 to CHAR_WIDTH - 1;
char_x: natural range 0 to (videomode.width / CHAR_WIDTH) - 1;
pixel_local_y: natural range 0 to CHAR_HEIGHT - 1;
char_y: natural range 0 to (videomode.height / CHAR_HEIGHT) - 1;
incr_pixel_x: boolean;
incr_char_x: boolean;
incr_pixel_y: boolean;
incr_char_y: boolean;
incr_frame: boolean;
addr: std_logic_vector(15 downto 0);
end record;
constant VGA_TEXT_COUNTERS_INIT: vga_text_counters := (
pixel_local_x => 0, char_x => 0,
pixel_local_y => 0, char_y => 0,
incr_pixel_x => false,
incr_char_x => false,
incr_pixel_y => false,
incr_char_y => false,
incr_frame => false,
addr => X"0000"
);
type vga_pipeline_stage is record
bs: vga_beam_state;
tc: vga_text_counters;
end record;
constant VGA_PIPELINE_STAGE_INIT: vga_pipeline_stage := (
bs => VGA_BEAM_STATE_INIT,
tc => VGA_TEXT_COUNTERS_INIT
);
type vga_pipeline is array (positive range 1 to 6) of vga_pipeline_stage;
constant VGA_PIPELINE_INIT: vga_pipeline :=
(others => VGA_PIPELINE_STAGE_INIT);
type scratchpad_T is record
ascii_char: std_logic_vector(7 downto 0);
glyph: glyph_bitmap;
row: std_logic_vector(0 to 7);
pixel: std_logic;
end record;
function have_active_video(stage: vga_pipeline_stage) return boolean is
begin
if stage.bs.hstate = HActiveVideo and stage.bs.vstate = VActiveVideo then
return true;
else
return false;
end if;
end function;
function have_new_char(stage: vga_pipeline_stage) return boolean is
begin
if have_active_video(stage) and stage.tc.pixel_local_x = 0 then
return true;
else
return false;
end if;
end function;
-- *** Signals ***
signal pixel_clk: std_logic := '0';
signal new_stage: vga_pipeline_stage := VGA_PIPELINE_STAGE_INIT;
signal pipeline: vga_pipeline := VGA_PIPELINE_INIT;
signal scratchpad: scratchpad_T := (
ascii_char => X"00",
glyph => get_gb(nul),
row => X"00",
pixel => '0'
);
-- Memory interface
signal addra: std_logic_vector(15 downto 0) := X"0000";
signal doa: std_logic_vector(7 downto 0) := X"00";
signal ena, regcea: std_logic := '0';
begin
addra <= pipeline(1).tc.addr;
scratchpad.ascii_char <= doa;
pipeline_event_dispatcher: process (pixel_clk, new_stage, pipeline,
scratchpad)
is
variable char: character;
begin
if have_new_char(pipeline(1)) then
ena <= '1';
else
ena <= '0';
end if;
if have_new_char(pipeline(2)) then
regcea <= '1';
else
regcea <= '0';
end if;
char := character'val( to_integer(unsigned(scratchpad.ascii_char)) );
if rising_edge(pixel_clk) then
if have_new_char(pipeline(3)) then
scratchpad.glyph <= get_gb(char);
end if;
end if;
if rising_edge(pixel_clk) then
if have_active_video(pipeline(4)) then
scratchpad.row <=
scratchpad.glyph(pipeline(4).tc.pixel_local_y);
end if;
end if;
if rising_edge(pixel_clk) then
if have_active_video(pipeline(5)) then
scratchpad.pixel <=
scratchpad.row(pipeline(5).tc.pixel_local_x);
end if;
end if;
end process;
shift_pipeline_forward: process (pixel_clk, en, reset, new_stage, pipeline) is
begin
if rising_edge(pixel_clk) then
if reset = '1' then
pipeline <= VGA_PIPELINE_INIT;
elsif en = '1' then
pipeline(1) <= new_stage;
pipeline(2) <= pipeline(1);
pipeline(3) <= pipeline(2);
pipeline(4) <= pipeline(3);
pipeline(5) <= pipeline(4);
pipeline(6) <= pipeline(5);
end if;
end if;
end process;
drive_output: process (new_stage, pipeline, scratchpad) is
variable ActiveVideo: boolean;
variable addr_slv: std_logic_vector(15 downto 0);
variable output_stage: vga_pipeline_stage;
begin
ActiveVideo := output_stage.bs.hstate = HActiveVideo
and output_stage.bs.vstate = VActiveVideo;
output_stage := pipeline(6);
-- output_stage := new_stage;
addr_slv := output_stage.tc.addr;
if output_stage.bs.hstate = HSyncPulse then
hsync <= '1';
else
hsync <= '0';
end if;
if output_stage.bs.vstate = VSyncPulse then
vsync <= '1';
else
vsync <= '0';
end if;
if ActiveVideo then
if addr_slv /= addrb then
if scratchpad.pixel = '1' then
-- red <= "1111";
green <= "1111";
-- blue <= "1111";
else
-- red <= "0000";
green <= "0000";
-- blue <= "0000";
end if;
else
-- Invert colours
if scratchpad.pixel = '1' then
-- red <= "0000";
green <= "0000";
-- blue <= "0000";
else
-- red <= "1111";
green <= "1111";
-- blue <= "1111";
end if;
end if;
-- green <= addr_slv(3 downto 0);
-- red <= addr_slv(7 downto 4);
-- blue <= addr_slv(11 downto 8);
else
red <= "0000";
green <= "0000";
blue <= "0000";
end if;
end process;
-- *** Components ***
fb0: entity work.ramb64x8(structural)
port map (
clka => pixel_clk,
addra => addra,
dia => X"00",
doa => doa,
ena => ena,
regcea => regcea,
rstrama => '0',
rstrega => '0',
wea => '0',
clkb => clk,
addrb => addrb,
dib => dib,
dob => dob,
enb => enb,
regceb => regceb,
rstramb => '0',
rstregb => '0',
web => web
);
cau_inst: entity work.character_address_unit(rtl)
port map (
clk => pixel_clk, reset => '0',
incr_frame => new_stage.tc.incr_frame,
incr_char_y => new_stage.tc.incr_char_y,
incr_pixel_y => new_stage.tc.incr_pixel_y,
incr_char_x => new_stage.tc.incr_char_x,
offset => X"0000",
load_offset => '0',
addr => new_stage.tc.addr
);
vtfc_inst: entity work.vga_text_frame_counter(rtl)
generic map (
videomode => videomode,
CHAR_WIDTH => CHAR_WIDTH,
CHAR_HEIGHT => CHAR_HEIGHT
) port map (
clk => pixel_clk, en => en, reset => reset,
hstate => new_stage.bs.hstate,
vstate => new_stage.bs.vstate,
pixel_local_x => new_stage.tc.pixel_local_x,
char_x => new_stage.tc.char_x,
pixel_local_y => new_stage.tc.pixel_local_y,
char_y => new_stage.tc.char_y,
incr_pixel_x => new_stage.tc.incr_pixel_x,
incr_char_x => new_stage.tc.incr_char_x,
incr_pixel_y => new_stage.tc.incr_pixel_y,
incr_char_y => new_stage.tc.incr_char_y,
incr_frame => new_stage.tc.incr_frame
);
vs_fsm: entity work.vga_state_fsm(structural)
generic map (
timings => get_timings_from_videomode(videomode)
) port map (
clk => pixel_clk, en => en, reset => reset,
htimer => new_stage.bs.htimer,
hstate => new_stage.bs.hstate,
vtimer => new_stage.bs.vtimer,
vstate => new_stage.bs.vstate
);
vga_pixel_clk_inst: vga_pixel_clock
port map (
clk => clk,
pixel_clk => pixel_clk
);
end architecture;