-
Notifications
You must be signed in to change notification settings - Fork 19
/
imgfifo.v
450 lines (407 loc) · 11.2 KB
/
imgfifo.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
////////////////////////////////////////////////////////////////////////////////
//
// Filename: rtl/imgfifo.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 imgfifo #(
// {{{
parameter ADDRESS_WIDTH=24, LGFLEN = 11,
BUSW=32, LW=11,
localparam AW=ADDRESS_WIDTH,
FIFO_ADDRESS_WIDTH=LGFLEN,
FAW = FIFO_ADDRESS_WIDTH
// }}}
) (
// {{{
input wire i_clk, i_pixclk,
input wire i_reset,
// verilator lint_off SYNCASYNCNET
input wire i_newframe,
// verilator lint_on SYNCASYNCNET
// Frame buffer control info
// {{{
input wire [(AW-1):0] i_baseaddr,
input wire [LGFLEN:0] i_linewords,
input wire [(LW-1):0] i_nlines,
// }}}
// Wishbone bus signaling
// {{{
output reg o_wb_cyc, o_wb_stb,
output reg [(AW-1):0] o_wb_addr,
// Return values on the Wishbone
input wire i_wb_stall,
input wire i_wb_ack,
input wire [(BUSW-1):0] i_wb_data,
input wire i_wb_err,
// }}}
// Now for the pixel interface to the reader on the other end
// {{{
input wire i_rd,
output wire o_valid,
output wire [(BUSW-1):0] o_word,
output wire o_err
// }}}
// }}}
);
// Local declarations
// {{{
reg last_ack, last_stb;
reg room_for_another_line_in_fifo, end_of_frame;
reg [LW-1:0] vpos;
wire [FAW:0] fifo_availability, fifo_fill;
reg [2:0] wb_reset_pipe;
wire wb_reset, pix_reset;
reg [2:0] pix_reset_pipe;
reg [(FAW-1):0] stb_count;
reg [(FAW-1):0] ack_count;
wire fifo_empty, fifo_full;
wire wb_reset_n = !wb_reset;
wire pix_reset_n = !pix_reset;
// }}}
// wb_reset, wb_reset_pipe
// {{{
initial wb_reset_pipe = -1;
always @(posedge i_clk or posedge i_reset or posedge i_newframe)
if (i_reset)
wb_reset_pipe <= -1;
else if (i_newframe)
wb_reset_pipe <= -1;
else
wb_reset_pipe <= { wb_reset_pipe[1:0], 1'b0 };
assign wb_reset = wb_reset_pipe[2];
// }}}
// pix_reset, pix_reset_pipe
// {{{
initial pix_reset_pipe = -1;
always @(posedge i_pixclk or posedge i_reset)
if (i_reset)
pix_reset_pipe <= -1;
else if (i_newframe)
pix_reset_pipe <= -1;
else
pix_reset_pipe <= { pix_reset_pipe[1:0], 1'b0 };
assign pix_reset = (pix_reset_pipe[2]) || (i_newframe);
// }}}
// Wishbone request: CYC, STB, ADDR
// {{{
// We only request a line at a time, and then wait until there's room
// for the next line in the FIFO before requesting it. Ideally, any
// supporting FIFO should have room for at least two lines within it.
initial o_wb_cyc = 1'b0;
initial o_wb_stb = 1'b0;
initial o_wb_addr = 0;
always @(posedge i_clk)
if ((wb_reset)||(i_wb_err))
begin
// {{{
o_wb_cyc <= 1'b0;
o_wb_stb <= 1'b0;
o_wb_addr <= i_baseaddr;
// }}}
end else if (o_wb_cyc)
begin
// {{{
if (!i_wb_stall)
// Stop requesting at the end of the line
o_wb_stb <= (o_wb_stb)&&(!last_stb);
if ((o_wb_stb)&&(!i_wb_stall))
// Increment the address with every item requested
o_wb_addr <= o_wb_addr + 1'b1;
if ((i_wb_ack)&&(last_ack))
// On the last acknowledgment, close up shop
o_wb_cyc <= 1'b0;
// }}}
end else if (room_for_another_line_in_fifo)
begin
// Start reading a new line
o_wb_cyc <= 1'b1;
o_wb_stb <= 1'b1;
end
// }}}
// stb_count
// {{{
initial stb_count= 0;
always @(posedge i_clk)
if ((wb_reset)||(!o_wb_cyc))
stb_count <= 0;
else if ((o_wb_stb)&&(!i_wb_stall))
stb_count <= stb_count + 1'b1;
// }}}
// last_stb
// {{{
initial last_stb = 1'b0;
always @(posedge i_clk)
if ((wb_reset)||(!o_wb_cyc))
last_stb <= 1'b0;
else if ((o_wb_stb)&&(!i_wb_stall))
last_stb <= ({ 2'b0, stb_count } >= { 1'b0, i_linewords}- 2);
else
last_stb <= ({ 2'b0, stb_count } >= { 1'b0, i_linewords}- 1'b1);
// }}}
// ack_count
// {{{
initial ack_count= 0;
always @(posedge i_clk)
if ((wb_reset)||(!o_wb_cyc))
ack_count <= 0;
else if (i_wb_ack)
ack_count <= ack_count + 1'b1;
// }}}
// last_ack
// {{{
initial last_ack = 1'b0;
always @(posedge i_clk)
if ((wb_reset)||(!o_wb_cyc))
last_ack <= 1'b0;
else if (i_wb_ack)
last_ack <= ({2'b00, ack_count} >= {1'b0, i_linewords} - 2);
else
last_ack <= ({2'b00, ack_count} >= {1'b0, i_linewords} - 1'b1);
// }}}
// vpos, end_of_frame
// {{{
initial vpos = 0;
initial end_of_frame = 0;
always @(posedge i_clk)
if (wb_reset)
begin
vpos <= 0;
end_of_frame <= (i_nlines == 0);
end else if ((o_wb_cyc)&&(i_wb_ack)&&(last_ack))
begin
if (vpos < i_nlines)
vpos <= vpos + 1'b1;
end_of_frame <= (i_nlines==0)||(vpos >= i_nlines-1'b1);
end
// }}}
assign fifo_availability = ({1'b1,{(FAW){1'b0}}} - fifo_fill);
// room_for_another_line_in_fifo?
// {{{
initial room_for_another_line_in_fifo = 1'b0;
always @(posedge i_clk)
if ((wb_reset))
room_for_another_line_in_fifo <= 1'b0;
else if (o_wb_cyc)
room_for_another_line_in_fifo <= 1'b0;
else if (!end_of_frame)
room_for_another_line_in_fifo
<= (fifo_availability > i_linewords + 1);
// }}}
// Asynchronous FIFO
// {{{
atxfifo #(
.DSIZE(BUSW),.ASIZE(FAW)
) fifo(
// {{{
i_clk, wb_reset_n,
// Incoming (write) data
(o_wb_cyc)&&(i_wb_ack)&&(!wb_reset), i_wb_data,
fifo_full, fifo_fill,
// Outgoing (read) data
i_pixclk, pix_reset_n, (i_rd)&&(!pix_reset), o_word, fifo_empty
// }}}
);
// }}}
assign o_err = (o_wb_cyc)&&(i_wb_ack)&&(fifo_full);
assign o_valid = (!fifo_empty);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
`ifdef IMGFIFO
`define ASSUME assume
`define ASSERT assert
`else
`define ASSUME assert
`define ASSERT assume
`endif
initial assume(i_reset);
always @(*)
begin
assume(i_linewords == 1280);
assume(i_nlines == 1080);
end
//
// Set up the f_past_valid registers. We'll need one for each of
// the three clock domains: write, read, and the global simulation
// clock.
//
reg f_past_valid_pix, f_past_valid_clk, f_past_valid_gbl;
(* gclk *) reg gbl_clk;
initial f_past_valid_pix = 0;
initial f_past_valid_clk = 0;
initial f_past_valid_gbl = 0;
always @(posedge gbl_clk)
f_past_valid_gbl <= 1'b1;
always @(posedge i_clk)
f_past_valid_clk <= 1'b1;
always @(posedge i_pixclk)
f_past_valid_pix <= 1'b1;
always @(*)
if (!f_past_valid_gbl)
assert((!f_past_valid_clk)&&(!f_past_valid_pix));
////////////////////////////////
//
// Setup the two clocks themselves. We'll assert nothing regarding
// their relative phases or speeds.
//
////////////////////////////////
//
localparam F_CLKBITS=3;
wire [F_CLKBITS-1:0] f_clk_step, f_pclk_step;
assign f_clk_step = $anyconst;
assign f_pclk_step = $anyconst;
always @(*)
assume(f_clk_step != 0);
always @(*)
assume(f_pclk_step != 0);
reg [F_CLKBITS-1:0] f_clk_count, f_pclk_count;
always @(posedge gbl_clk)
f_clk_count <= f_clk_count + f_clk_step;
always @(posedge gbl_clk)
f_pclk_count <= f_pclk_count + f_pclk_step;
always @(*)
begin
assume(i_clk == f_clk_count[F_CLKBITS-1]);
assume(i_pixclk == f_pclk_count[F_CLKBITS-1]);
end
// Insist that items synchronous to the wishbone clock only
// change on a clock edge
always @(posedge gbl_clk)
if ((f_past_valid_gbl)&&(!$rose(i_clk)))
begin
assume($stable(i_reset));
assume($stable(i_baseaddr));
assume((i_reset)||($stable(i_linewords)));
assume((i_reset)||($stable(i_nlines)));
//
assume($stable(i_wb_ack));
assume($stable(i_wb_err));
assume($stable(i_wb_stall));
assume($stable(i_wb_data));
end
always @(*)
if (!f_past_valid_clk)
assume(i_reset);
always @(posedge gbl_clk)
if ((!f_past_valid_gbl)||(!$rose(i_pixclk)))
begin
assume($stable(i_rd));
assume($stable(i_newframe));
end
always @(posedge i_clk)
if ((!f_past_valid_clk)||(!$past(i_reset)))
begin
`ASSUME($stable(i_baseaddr));
`ASSUME($stable(i_linewords));
`ASSUME($stable(i_nlines));
end
always @(posedge i_clk)
if ((f_past_valid_clk)||(!$past(i_reset)))
begin
assume($stable(i_linewords));
assume($stable(i_nlines));
end
//////////////////////////////////////////////
//////////////////////////////////////////////
wire [FAW-1:0] f_nreqs, f_nacks, f_outstanding;
fwb_master #(.AW(AW), .DW(BUSW), .F_MAX_STALL(3),
.F_MAX_ACK_DELAY(5),
.F_LGDEPTH(FAW),
.F_OPT_RMW_BUS_OPTION(1'b0),
.F_OPT_SOURCE(1'b1),
.F_OPT_DISCONTINUOUS(1'b0)
) f_bus(i_clk, i_reset,
o_wb_cyc, o_wb_stb, 1'b0, o_wb_addr, 32'h0, 4'h0,
i_wb_ack, i_wb_stall, i_wb_data, i_wb_err,
f_nreqs, f_nacks, f_outstanding);
//////////////////////////////////////////////
//////////////////////////////////////////////
initial assume(!i_rd);
always @(*)
begin
assert(stb_count <= i_linewords);
if (stb_count == i_linewords)
assert(!o_wb_stb);
if ((o_wb_cyc)&&(!o_wb_stb))
assert(stb_count == i_linewords);
end
always @(*)
begin
assert(ack_count <= i_linewords);
assert(ack_count <= stb_count);
if (ack_count == i_linewords)
assert(!o_wb_cyc);
end
always @(*)
if (o_wb_cyc)
assert(f_nreqs == stb_count);
always @(*)
if (o_wb_cyc)
assert(f_nacks == ack_count);
always @(*)
if ((!end_of_frame)||(o_wb_cyc))
assume(!i_newframe);
always @(posedge i_pixclk)
if (($past(i_newframe))||($past(i_newframe,2)))
assume(!i_newframe);
//
//
// Let's prove that we'll never overflow our FIFO
always @(*)
if ((o_wb_cyc)&&(i_wb_ack))
assert(!fifo_full);
// This is going to depend upon some other assertions just to make
// sure we are always in a state that will never eventually overflow
reg [FAW:0] f_remaining;
always @(posedge i_clk)
if (!o_wb_cyc)
f_remaining <= i_linewords;
else
f_remaining <= i_linewords-ack_count;
/// - (((o_wb_stb)&&(!i_wb_stall))?1:0);
always @(posedge i_clk)
if (o_wb_cyc)
assert({ 1'b0, fifo_availability} > {1'b0,f_remaining});
`endif
// }}}
endmodule