-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathllvga.v
371 lines (340 loc) · 9.07 KB
/
llvga.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
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
361
362
363
364
365
366
367
368
369
370
////////////////////////////////////////////////////////////////////////////////
//
// Filename: rtl/llvga.v
// {{{
// Project: vgasim, a Verilator based VGA simulator demonstration
//
// Purpose:
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2017-2024, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
`default_nettype none
// }}}
module llvga #(
// {{{
parameter BITS_PER_COLOR = 4,
HW=12,VW=12,
localparam BPC = BITS_PER_COLOR,
BITS_PER_PIXEL = 3 * BPC,
BPP = BITS_PER_PIXEL
// }}}
) (
// {{{
input wire i_pixclk, i_reset, i_test,
// External connections
input wire [BPP-1:0] i_rgb_pix,
// Video mode information
// {{{
input wire [HW-1:0] i_hm_width, i_hm_porch,
i_hm_synch, i_hm_raw,
input wire [VW-1:0] i_vm_height, i_vm_porch,
i_vm_synch, i_vm_raw,
// }}}
// Outgoing signal generation
// {{{
output reg o_rd, o_newline, o_newframe,
// }}}
// VGA connections
// {{{
output reg o_vsync, o_hsync,
output reg [BPC-1:0] o_red, o_grn, o_blu
// }}}
// }}}
);
// Local declarations
// {{{
wire [BPC-1:0] i_red, i_grn, i_blu;
assign i_red = i_rgb_pix[3*BPC-1:2*BPC];
assign i_grn = i_rgb_pix[2*BPC-1: BPC];
assign i_blu = i_rgb_pix[ BPC-1:0];
reg [HW-1:0] hpos;
reg [VW-1:0] vpos;
reg hrd, vrd;
reg first_frame;
wire w_rd;
wire [(BPC-1):0] tst_red, tst_grn, tst_blu;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Horizontal framing
// {{{
////////////////////////////////////////////////////////////////////////
//
//
initial hpos = 0;
initial o_newline = 0;
initial o_hsync = 1;
initial hrd = 1;
always @(posedge i_pixclk)
if (i_reset)
begin
hpos <= 0;
o_newline <= 1'b0;
o_hsync <= 1'b1;
hrd <= 1;
end else
begin
hrd <= (hpos < i_hm_width-2)
||(hpos >= i_hm_raw-2);
if (hpos < i_hm_raw-1'b1)
hpos <= hpos + 1'b1;
else
hpos <= 0;
o_newline <= (hpos == i_hm_width-2);
o_hsync <= (hpos < i_hm_porch-1'b1)||(hpos>=i_hm_synch-1'b1);
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// Vertical framing
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// o_newframe
// {{{
always @(posedge i_pixclk)
if (i_reset)
o_newframe <= 1'b0;
else if ((hpos == i_hm_width - 2)&&(vpos == i_vm_height-1))
o_newframe <= 1'b1;
else
o_newframe <= 1'b0;
// }}}
// vpos, o_vsync
// {{{
initial vpos = 0;
initial o_vsync = 1'b1;
always @(posedge i_pixclk)
if (i_reset)
begin
vpos <= 0;
o_vsync <= 1'b1;
end else if (hpos == i_hm_porch-1'b1)
begin
if (vpos < i_vm_raw-1'b1)
vpos <= vpos + 1'b1;
else
vpos <= 0;
// Realistically, the new frame begins at the top
// of the next frame. Here, we define it as the end
// last valid row. That gives any software depending
// upon this the entire time of the front and back
// porches, together with the synch pulse width time,
// to prepare to actually draw on this new frame before
// the first pixel clock is valid.
o_vsync <= (vpos < i_vm_porch-1'b1)||(vpos>=i_vm_synch-1'b1);
end
// }}}
// vrd
// {{{
initial vrd = 1'b1;
always @(posedge i_pixclk)
vrd <= (vpos < i_vm_height)&&(!i_reset);
// }}}
// first_frame
// {{{
initial first_frame = 1'b1;
always @(posedge i_pixclk)
if (i_reset)
first_frame <= 1'b1;
else if (o_newframe)
first_frame <= 1'b0;
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// (Optionally) generate a test image containing color bars
// {{{
////////////////////////////////////////////////////////////////////////
//
//
vgatestsrc #(
// {{{
.BITS_PER_COLOR(BPC),.HW(HW),.VW(VW)
// }}}
) vgatsrc(
// {{{
.i_pixclk(i_pixclk), .i_reset(i_reset),
.i_width(i_hm_width), .i_height(i_vm_height),
.i_rd(o_rd), .i_newline(o_newline), .i_newframe(o_newframe),
.o_pixel({ tst_red, tst_grn, tst_blu })
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Stream read (ready) generation
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// w_rd
// {{{
assign w_rd = (hrd)&&(vrd)&&(!first_frame);
// }}}
// o_rd
// {{{
initial o_rd = 1'b0;
always @(posedge i_pixclk)
if (i_reset)
o_rd <= 1'b0;
else
o_rd <= w_rd;
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Outgoing pixel generation
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(posedge i_pixclk)
if (w_rd)
begin
o_red <= (i_test) ? tst_red : i_red;
o_grn <= (i_test) ? tst_grn : i_grn;
o_blu <= (i_test) ? tst_blu : i_blu;
end else begin
o_red <= 0;
o_grn <= 0;
o_blu <= 0;
end
//}}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties for verification purposes
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
reg f_past_valid;
reg [47:0] f_last_vmode, f_last_hmode;
wire [47:0] f_vmode, f_hmode;
reg f_stable_mode;
initial f_past_valid = 1'b0;
always @(posedge i_pixclk)
f_past_valid <= 1'b1;
always @(*)
if (!f_past_valid)
assume(i_reset);
always @(*)
begin
assume(12'h10 < i_hm_width);
assume(i_hm_width < i_hm_porch);
assume(i_hm_porch < i_hm_synch);
assume(i_hm_synch < i_hm_raw);
assume(12'h10 < i_vm_height);
assume(i_vm_height < i_vm_porch);
assume(i_vm_porch < i_vm_synch);
assume(i_vm_synch < i_vm_raw);
end
assign f_hmode = { i_hm_width, i_hm_porch, i_hm_synch, i_hm_raw };
assign f_vmode = { i_vm_height, i_vm_porch, i_vm_synch, i_vm_raw };
always @(posedge i_pixclk)
f_last_vmode <= f_vmode;
always @(posedge i_pixclk)
f_last_hmode <= f_hmode;
always @(*)
f_stable_mode = (f_last_vmode == f_vmode)&&(f_last_hmode == f_hmode);
always @(*)
if (!i_reset)
assume(f_stable_mode);
always @(posedge i_pixclk)
if ((!f_past_valid)||($past(i_reset)))
begin
assert(hpos == 0);
assert(vpos == 0);
end
always @(posedge i_pixclk)
if ((f_past_valid)&&(!$past(i_reset))&&(!i_reset)
&&(f_stable_mode)&&($past(f_stable_mode)))
begin
// The horizontal position counter should increment
if ($past(hpos >= i_hm_raw-1'b1))
assert(hpos == 0);
else
assert(hpos == $past(hpos)+1'b1);
// The vertical position counter should increment
if (hpos == i_hm_porch)
begin
if ($past(vpos >= i_vm_raw-1'b1))
assert(vpos == 0);
else
assert(vpos == $past(vpos)+1'b1);
end else
assert(vpos == $past(vpos));
// For induction purposes, we need to insist that both
// horizontal and vertical counters stay within their
// required ranges
assert(hpos < i_hm_raw);
assert(vpos < i_vm_raw);
// If we are less than the data width for both horizontal
// and vertical, then we should be asserting we are in a
// valid data cycle
if ((hpos < i_hm_width)&&(vpos < i_vm_height)
&&(!first_frame))
assert(o_rd);
//
// The horizontal sync should only be valid between positions
// i_hm_porch <= hpos < i_hm_sync, invalid at all other times
//
if (hpos < i_hm_porch)
assert(o_hsync);
else if (hpos < i_hm_synch)
assert(!o_hsync);
else
assert(o_hsync);
// Same thing for vertical
if (vpos < i_vm_porch)
assert(o_vsync);
else if (vpos < i_vm_synch)
assert(!o_vsync);
else
assert(o_vsync);
// At the end of every horizontal line cycle, we assert
// a new line
if (hpos == i_hm_width-1'b1)
assert(o_newline);
else
assert(!o_newline);
// At the end of every vertical frame cycle, we assert
// a new frame, but only on the newline measure
if ((vpos == i_vm_height-1'b1)&&(o_newline))
assert(o_newframe);
else
assert(!o_newframe);
end
`endif
// }}}
endmodule