/
p66brxgears.v
475 lines (412 loc) · 11 KB
/
p66brxgears.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
////////////////////////////////////////////////////////////////////////////////
//
// Filename: rtl/net/p66brxgears.v
// {{{
// Project: 10Gb Ethernet switch
//
// Purpose:
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2023-2024, Gisselquist Technology, LLC
// {{{
// This file is part of the ETH10G project.
//
// The ETH10G project contains free software and gateware, licensed under the
// terms of the 3rd version of the GNU General Public License as published by
// the Free Software Foundation.
//
// This project 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 p66brxgears (
// {{{
input wire i_clk, i_reset,
input wire [31:0] i_data,
output wire M_VALID,
output wire [65:0] M_DATA
// }}}
);
// Local declarations
// {{{
localparam LOCKMSB = 5;
reg rx_valid;
reg [6:0] rx_count;
reg [95:0] rx_gears;
reg [65:0] al_last, al_data;
reg [65:0] ign_al_msb;
reg [6:0] al_shift;
reg al_slip;
reg [LOCKMSB:0] lock_count;
reg [128-1:0] full_set;
reg [31:0] r_data;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Step one: the gearbox
// {{{
// pre-state: register the data -- for timing's sake
always @(posedge i_clk)
r_data <= i_data;
// { rx_count } = # of bits in our shift register
// rx_valid = (66 or more bits in the shift register)
always @(posedge i_clk)
if (i_reset)
begin
rx_count <= 0;
rx_valid <= 0;
end else if (rx_valid)
begin
rx_count <= rx_count + 32 - 66;
rx_valid <= 1'b0; // i.e. if (rx_count + 32 - 66 >= 66)
end else begin
rx_count <= rx_count + 32; // Always add 32
rx_valid <= (rx_count >= 34);
end
always @(*)
begin
full_set = { 32'h0, rx_gears }
| ({ 96'h0, r_data } << rx_count);
if (rx_valid)
full_set = full_set >> 66;
end
always @(posedge i_clk)
if (i_reset)
rx_gears <= 0;
else
rx_gears <= full_set[95:0];
// }}}
////////////////////////////////////////////////////////////////////////
//
// Step two: alignment
// {{{
always @(posedge i_clk)
if (i_reset)
al_last <= 0;
else if (rx_valid)
al_last <= rx_gears[65:0];
always @(posedge i_clk)
if (rx_valid)
{ign_al_msb, al_data } <= { rx_gears[65:0],al_last } >>al_shift;
always @(posedge i_clk)
if (i_reset)
begin
lock_count <= 0;
al_slip <= 0;
al_shift <= 0;
end else if (rx_valid)
begin
if (al_slip)
al_slip <= 0;
else if (al_data[0] != al_data[1])
begin
if (!lock_count[LOCKMSB])
lock_count <= lock_count + 1;
end else if (lock_count > 3)
lock_count <= lock_count - 3;
else begin
lock_count <= 0;
al_slip <= 1;
if (al_shift >= 65)
al_shift <= 0;
else
al_shift <= al_shift + 1;
end
end
`ifdef FORMAL
always @(*)
if (!i_reset)
assert(al_shift < 66);
always @(*)
if (!i_reset && lock_count[LOCKMSB])
assert(lock_count[LOCKMSB-1:0] == 0);
`endif
// }}}
////////////////////////////////////////////////////////////////////////
//
// Step three: output
// {{{
assign M_VALID = rx_valid && lock_count[LOCKMSB];
assign M_DATA = al_data;
// }}}
// Keep Verilator happy
// {{{
// Verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, ign_al_msb };
// Verilator lint_on UNUSED
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
// The following isn't a *full* formal proof, but it was enough
// to get me past some bugs I just couldn't figure my way through.
reg f_past_valid;
integer ik;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
always @(*) if (!f_past_valid) assume(i_reset);
// always @(*) assume(i_reset != f_past_valid);
////////////////////////////////////////////////////////////////////////
//
// Ad-hoc properties
// {{{
always @(*)
if (!i_reset)
begin
assert(rx_count <= 96);
assert(rx_count[0] == 1'b0);
assert(rx_valid == (rx_count >= 66));
end
always @(*)
if (!i_reset)
assert(full_set[127:96] == 0);
always @(*)
if (!i_reset)
begin
for(ik=0; ik<96; ik=ik+1)
if (rx_count <= ik)
assert(rx_gears[ik] == 1'b0);
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// Contract
// {{{
////////////////////////////////////////////////////////////////////////
//
//
reg [4:0] f_pipe;
reg [6:0] f_align_posn;
reg [31:0] f_shift_idata, f_data;
reg [63:0] f_align_idata;
reg [6:0] fr_align_posn;
reg [31:0] fr_data;
reg [63:0] fr_shift_data;
reg [6:0] frx_align_posn, frx_alignment;
reg [95:0] frx_shift_data, frx_dblshift_data;
reg [6:0] fal_align_posn;
reg [31:0] fal_shift;
////////////////////////////////////////
//
// Alignment contract
// {{{
//
// Given alignment data arrives every 66 bits, prove that once we
// align we keep alignment
// f_pipe -- to know when each stage is valid
// {{{
always @(posedge i_clk)
if (i_reset)
f_pipe <= 0;
else begin
f_pipe[2:0] <= { f_pipe[1:0], 1'b1 };
if (rx_valid)
f_pipe[4:3] <= f_pipe[3:2];
end
always @(*)
if (!i_reset)
case(f_pipe)
5'h00: begin end
5'h01: begin end
5'h03: begin end
5'h07: begin end
5'h0f: begin end
5'h1f: begin end
default: assert(0);
endcase
// }}}
// Step 1. Assume two alignment bits every 66 on input
// {{{
initial f_align_posn = 0;
always @(posedge i_clk)
if (f_align_posn < 32)
f_align_posn <= f_align_posn + 66-32;
else
f_align_posn <= f_align_posn - 32;
always @(*)
assert(f_align_posn < 66+66-32);
always @(posedge i_clk)
f_data <= i_data;
always @(*)
f_align_idata = { i_data, f_data } >> f_align_posn;
always @(*)
if (f_align_posn < 63)
assume(^f_align_idata[1:0]);
// }}}
// Step 2. Track from i_data to r_data
// {{{
initial fr_align_posn = 0;
always @(posedge i_clk)
if (f_align_posn < 66)
fr_align_posn <= f_align_posn;
else
fr_align_posn <= f_align_posn - 66;
always @(posedge i_clk)
fr_data <= r_data;
always @(posedge i_clk)
if (!i_reset)
assert(f_data == r_data);
always @(*)
fr_shift_data = { r_data, fr_data } >> fr_align_posn;
always @(*)
if (!i_reset && f_pipe[1] && fr_align_posn < 63)
assert(^fr_shift_data[1:0]);
// }}}
// Step 3. Track from r_data to rx_gears
// {{{
initial frx_align_posn = 0;
always @(posedge i_clk)
if (rx_valid && (fr_align_posn + rx_count > 66 + 32))
frx_align_posn <= fr_align_posn + rx_count - 66 - 32;
else
frx_align_posn <= fr_align_posn + rx_count - 32;
always @(*)
frx_shift_data = rx_gears >> frx_align_posn;
always @(*)
if (frx_align_posn > 66)
frx_dblshift_data = rx_gears >> (frx_align_posn - 66);
else
frx_dblshift_data = rx_gears >> (66 + frx_align_posn);
always @(posedge i_clk)
if (!i_reset && f_pipe[2] && (frx_align_posn + 1 < rx_count))
assert(^frx_shift_data[1:0]);
always @(*)
if (frx_align_posn >= 66)
frx_alignment = frx_align_posn - 66;
else
frx_alignment = frx_align_posn;
// always @(*)
// if (f_pipe[3] && frx_align_posn > 66 && frx_align_posn - 66 < rx_count)
// assert(^frx_dblshift_data[1:0]);
always @(posedge i_clk)
if (!i_reset && f_pipe[3])
assert($stable(frx_alignment));
// }}}
// Step 3. Track from rx_gears to al_data
// Step 4. Now *PROVE*: Once locked, always locked
// {{{
always @(posedge i_clk)
if (rx_valid)
fal_align_posn <= frx_alignment;
always @(*)
fal_shift = { rx_gears[65:0], al_last } >> fal_align_posn;
always @(posedge i_clk)
if (!i_reset && f_pipe[4] && (rx_valid || fal_align_posn < 65))
assert(^fal_shift[1:0]);
always @(posedge i_clk)
if (!i_reset && !$past(i_reset) && $past(f_pipe[4]) && $past(al_shift) == fal_align_posn)
begin
// assert(f_locked);
assert(!$rose(al_slip));
assert($stable(al_shift));
end
// }}}
// }}}
////////////////////////////////////////
//
// Data contract
// {{{
//
// Given arbitrary input data at an arbitrary point in time, prove
// that this data ends up in the output at the same point in time
(* anyconst *) reg [15:0] fc_posn;
(* anyconst *) reg [1:0] fc_dblet;
reg [15:0] fc0_posn;
reg [63:0] fc0_shift;
reg [15:0] fc1_posn;
reg [63:0] fc1_shift;
reg [15:0] fc2_posn;
reg [95:0] fc2_shift;
reg [15:0] fc3_posn;
reg [65:0] fc3_shift;
reg [15:0] falc_posn;
reg [65:0] falc_shift;
initial fc0_posn = 0;
always @(posedge i_clk)
fc0_posn <= fc0_posn + 32;
always @(*)
fc0_shift = { i_data, f_data } >> (fc_posn-fc0_posn);
always @(*)
if (fc0_posn <= fc_posn && fc_posn <= fc0_posn + 32)
assume(fc0_shift[1:0] == fc_dblet);
initial fc1_posn = -32;
always @(posedge i_clk)
fc1_posn <= fc0_posn;
always @(*)
fc1_shift = { r_data, fr_data } >> (fc_posn-fc1_posn);
always @(*)
if (!i_reset && f_pipe[0] && fc1_posn <= fc_posn && fc_posn + 1 < fc1_posn + 32)
assert(fc1_shift[1:0] == fc_dblet);
// fc2_posn is the position of the first bit of the rx_gears register
initial fc2_posn = 0;
always @(posedge i_clk)
if (rx_valid)
fc2_posn <= fc0_posn - rx_count + 66;
else
fc2_posn <= fc0_posn - rx_count;
always @(*)
fc2_shift = rx_gears >> (fc_posn-fc2_posn);
always @(*)
if (!i_reset && f_pipe[2] && fc2_posn <= fc_posn && fc_posn + 1 < fc2_posn + rx_count)
assert(fc2_shift[1:0] == fc_dblet);
initial fc3_posn = 0;
always @(posedge i_clk)
if (rx_valid)
fc3_posn <= fc2_posn;
always @(*)
fc3_shift = al_last >> (fc_posn-fc3_posn);
always @(*)
if (!i_reset && f_pipe[3] && fc3_posn <= fc_posn && fc_posn + 1 < fc3_posn + 66)
assert(fc3_shift[1:0] == fc_dblet);
always @(posedge i_clk)
if (rx_valid)
falc_posn <= fc3_posn + al_shift;
always @(*)
falc_shift = { ign_al_msb, al_data } >> (fc_posn-falc_posn);
always @(*)
if (!i_reset && rx_valid && f_pipe[4] && falc_posn <= fc_posn && fc_posn + 1 < falc_posn + 66)
assert(falc_shift[1:0] == fc_dblet);
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// "Careless" assumptions
// {{{
////////////////////////////////////////////////////////////////////////
//
always @(*)
begin
assume(fc_posn[15] == 1'b0);
assume(fc_posn > 16'h80);
end
// }}}
`endif
// }}}
endmodule