forked from ZipCPU/wb2axip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axissafety.v
677 lines (615 loc) · 16.9 KB
/
axissafety.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
////////////////////////////////////////////////////////////////////////////////
//
// Filename: axissafety.v
// {{{
// Project: WB2AXIPSP: bus bridges and other odds and ends
//
// Purpose: A firewall for the AXI-stream protocol. Only handles TVALID,
// TREADY, TDATA, TLAST and TUSER signals.
//
// This module is designed to be a "bump in the stream" of the stream
// protocol--a bump that can be used to detect AXI stream errors.
// Specifically, this firewall can detect one of three errors:
//
// 1. CHANGE The stream is stalled and something changes. This
// could easily be caused by a stream overflow.
// 2. PACKET LENGTH If OPT_PACKET_LENGTH != 0, then all stream
// packets are assumed to be OPT_PACKET_LENGTH in length.
// Packets that are not OPT_PACKET_LENGTH in length will
// generate a fault.
//
// This core cannot handle dynamic packet lengths, but
// should do just fine with detecting errors in fixed
// length packets--such as an FFT might use.
// 3. TOO MANY STALLS
// If OPT_MAX_STALL != 0, and the master stalls an input more than
// OPT_MAX_STALL cycles, then a fault will be declared.
//
// 4. (Sorry--I haven't implemented a video firewall. Video images
// having TUSER set to start of frame will not be guaranteed
// video stream compliant, since TUSER might still be set to the
// wrong number.)
//
// If a fault is detected, o_fault will be set true. This is designed so
// that you can trigger an ILA off of a protocol error, and then see what
// caused the fault.
//
// If OPT_SELF_RESET is set, then o_fault will be self clearing. Once
// a fault is detected, the current packet will be flushed, and the
// firewall will become idle (holding TREADY high) until S_AXIS_TVALID
// and S_AXIS_TLAST--marking the end of a broken packet. This will
// resynchronize the core, following which packets may pass through again.
//
// If you aren't using TLAST, set it to one.
//
// If you aren't using TUSER, set the width to 1 and the input to a
// constant zero.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2019-2021, Gisselquist Technology, LLC
// {{{
// This file is part of the WB2AXIP project.
//
// The WB2AXIP project contains free software and gateware, licensed under the
// Apache License, Version 2.0 (the "License"). You may not use this project,
// or this file, except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
////////////////////////////////////////////////////////////////////////////////
//
`default_nettype none
//
// }}}
module axissafety #(
// {{{
`ifdef FORMAL
parameter [0:0] F_OPT_FAULTLESS = 0,
`endif
parameter C_AXIS_DATA_WIDTH = 8,
parameter C_AXIS_USER_WIDTH = 1,
parameter OPT_MAX_STALL = 0,
parameter OPT_PACKET_LENGTH = 0,
parameter [0:0] OPT_SELF_RESET = 0
// }}}
) (
// {{{
output reg o_fault,
input wire S_AXI_ACLK,
input wire S_AXI_ARESETN,
// AXI-stream slave (incoming) port
// {{{
input wire S_AXIS_TVALID,
output wire S_AXIS_TREADY,
input wire [C_AXIS_DATA_WIDTH-1:0] S_AXIS_TDATA,
input wire S_AXIS_TLAST, // = 1,
input wire [C_AXIS_USER_WIDTH-1:0] S_AXIS_TUSER, // = 0,
// }}}
// AXI-stream master (outgoing) port
// {{{
output reg M_AXIS_TVALID,
input wire M_AXIS_TREADY,
output reg [C_AXIS_DATA_WIDTH-1:0] M_AXIS_TDATA,
output reg M_AXIS_TLAST,
output reg [C_AXIS_USER_WIDTH-1:0] M_AXIS_TUSER
// }}}
// }}}
);
// Parameter/register declarations
// {{{
localparam LGPKTLEN = $clog2(OPT_PACKET_LENGTH+1);
localparam LGSTALLCOUNT = $clog2(OPT_MAX_STALL+1);
reg skdr_valid, skd_valid, skd_ready;
reg [C_AXIS_DATA_WIDTH+1+C_AXIS_USER_WIDTH-1:0] idata,skdr_data;
reg [C_AXIS_DATA_WIDTH-1:0] skd_data;
reg [C_AXIS_USER_WIDTH-1:0] skd_user;
reg skd_last;
reg r_stalled;
reg packet_fault, change_fault, stall_fault;
reg [C_AXIS_DATA_WIDTH+1+C_AXIS_USER_WIDTH-1:0] past_data;
reg [LGSTALLCOUNT-1:0] stall_count;
reg [LGPKTLEN-1:0] s_packet_counter;
reg [LGPKTLEN-1:0] m_packet_count;
reg m_end_of_packet, m_idle;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Implement a skid buffer with no assumptions
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
idata = { S_AXIS_TUSER, S_AXIS_TLAST, S_AXIS_TDATA };
initial skdr_valid = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
skdr_valid <= 0;
else if (S_AXIS_TVALID && S_AXIS_TREADY && (skd_valid && !skd_ready))
skdr_valid <= 1;
else if (skd_ready)
skdr_valid <= 0;
initial skdr_data = 0; // Allow data to be optimized away if always clr
always @(posedge S_AXI_ACLK)
if (S_AXIS_TVALID && S_AXIS_TREADY)
skdr_data <= idata;
always @(*)
if (S_AXIS_TREADY)
{ skd_user, skd_last, skd_data } = idata;
else
{ skd_user, skd_last, skd_data } = skdr_data;
always @(*)
skd_valid = S_AXIS_TVALID || skdr_valid;
assign S_AXIS_TREADY = !skdr_valid;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Fault detection
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// r_stalled
// {{{
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
r_stalled <= 0;
else
r_stalled <= (S_AXIS_TVALID && !S_AXIS_TREADY);
// }}}
// past_data
// {{{
always @(posedge S_AXI_ACLK)
past_data <= idata;
// }}}
always @(*)
change_fault = (r_stalled && (past_data != idata));
// packet_fault
// {{{
generate if (OPT_PACKET_LENGTH != 0)
begin : S_PACKET_COUNT
// {{{
// s_packet_counter
// {{{
initial s_packet_counter = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
s_packet_counter <= 0;
else if (o_fault)
s_packet_counter <= 0;
else if (S_AXIS_TVALID && S_AXIS_TREADY)
begin
s_packet_counter <= s_packet_counter + 1;
if (S_AXIS_TLAST)
s_packet_counter <= 0;
end
// }}}
// packet_fault
// {{{
always @(*)
begin
packet_fault = (S_AXIS_TLAST
!= (s_packet_counter == OPT_PACKET_LENGTH-1));
if (!S_AXIS_TVALID)
packet_fault = 0;
end
// }}}
// }}}
end else begin
// {{{
always @(*)
packet_fault = 1'b0;
always @(*)
s_packet_counter = 0;
// Verilator lint_off UNUSED
wire unused_pkt_counter;
assign unused_pkt_counter = &{ 1'b0, s_packet_counter };
// Verilator lint_on UNUSED
// }}}
end endgenerate
// }}}
// stall_fault
// {{{
generate if (OPT_MAX_STALL != 0)
begin : S_STALL_COUNT
// {{{
initial stall_count = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
stall_count <= 0;
else if (!S_AXIS_TVALID || S_AXIS_TREADY)
stall_count <= 0;
else
stall_count <= stall_count + 1;
always @(*)
stall_fault = (stall_count > OPT_MAX_STALL);
// }}}
end else begin
// {{{
always @(*)
stall_fault = 0;
always @(*)
stall_count = 0;
// }}}
end endgenerate
// }}}
// o_fault
// {{{
initial o_fault = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
o_fault <= 0;
else if (!o_fault)
o_fault <= change_fault || stall_fault || packet_fault;
else if (OPT_SELF_RESET)
begin
if (skd_last && skd_ready && m_idle)
o_fault <= 1'b0;
end
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// The outgoing AXI-stream port
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// skd_ready
// {{{
always @(*)
if (!o_fault && !change_fault && !stall_fault && !packet_fault)
skd_ready = !M_AXIS_TVALID || M_AXIS_TREADY;
else if (!o_fault)
skd_ready = 1'b0;
else
skd_ready = 1'b1;
// }}}
// m_end_of_packet, m_packet_count
// {{{
generate if (OPT_PACKET_LENGTH != 0)
begin
// {{{
initial m_packet_count = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
m_packet_count <= 0;
else if (M_AXIS_TVALID && M_AXIS_TREADY)
begin
m_packet_count <= m_packet_count + 1;
if (M_AXIS_TLAST)
m_packet_count <= 0;
end
// initial m_end_of_packet = 0;
// always @(posedge S_AXI_ACLK)
// if (!S_AXI_ARESETN)
// m_end_of_packet <= 0;
// else if (M_AXIS_TVALID && M_AXIS_TREADY)
// m_end_of_packet <= (m_packet_count >= (OPT_PACKET_LENGTH-3));
always @(*)
begin
m_end_of_packet = (m_packet_count == (OPT_PACKET_LENGTH-1));
if (M_AXIS_TVALID)
m_end_of_packet = (m_packet_count == (OPT_PACKET_LENGTH-2));
end
// }}}
end else begin
// {{{
always @(*)
m_end_of_packet = 1'b1;
always @(*)
m_packet_count = 0;
// Verilator lint_off UNUSED
wire unused_mpkt_counter;
assign unused_mpkt_counter = &{ 1'b0, m_packet_count };
// Verilator lint_on UNUSED
// }}}
end endgenerate
initial m_idle = 1;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
m_idle <= 1;
else if (!M_AXIS_TVALID || M_AXIS_TREADY)
begin
if (M_AXIS_TVALID && M_AXIS_TLAST)
m_idle <= o_fault || (!skd_valid || !skd_ready);
else if (m_idle && !o_fault)
m_idle <= (!skd_valid || !skd_ready);
end
// }}}
// M_AXIS_TVALID
// {{{
initial M_AXIS_TVALID = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
M_AXIS_TVALID <= 1'b0;
else if (!M_AXIS_TVALID || M_AXIS_TREADY)
begin
M_AXIS_TVALID <= 1'b0;
if (!o_fault && skd_ready)
M_AXIS_TVALID <= skd_valid;
else if (o_fault)
begin
if (m_idle)
M_AXIS_TVALID <= 1'b0;
else if (!M_AXIS_TVALID || M_AXIS_TLAST)
M_AXIS_TVALID <= 1'b0;
else
M_AXIS_TVALID <= 1'b1;
end
end
// }}}
// M_AXIS_TUSER, M_AXIS_TLAST, M_AXIS_TDATA
// {{{
initial { M_AXIS_TUSER, M_AXIS_TLAST, M_AXIS_TDATA } <= 0;
always @(posedge S_AXI_ACLK)
begin
if (!M_AXIS_TVALID || M_AXIS_TREADY)
begin
if (o_fault || change_fault || packet_fault)
begin
{ M_AXIS_TUSER, M_AXIS_TLAST, M_AXIS_TDATA } <= 0;
// if (!M_AXIS_TLAST)
// M_AXIS_TLAST <= m_end_of_packet;
end else
{ M_AXIS_TUSER, M_AXIS_TLAST, M_AXIS_TDATA }
<= { skd_user, skd_last, skd_data };
if (OPT_PACKET_LENGTH != 0)
M_AXIS_TLAST <= m_end_of_packet;
else if (o_fault && !m_idle)
M_AXIS_TLAST <= 1'b1;
end
if (!S_AXI_ARESETN)
M_AXIS_TLAST <= 0;
end
// }}}
// }}}
// Make Verilator happy
// {{{
// Verilator lint_off UNUSED
wire unused = &{ 1'b0 };
// Verilator lint_on UNUSED
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
reg f_past_valid;
reg [LGPKTLEN-1:0] fm_packet_counter;
reg [LGSTALLCOUNT-1:0] fm_stall_count;
initial f_past_valid = 1'b0;
always @(posedge S_AXI_ACLK)
f_past_valid <= 1'b1;
always @(*)
if (!f_past_valid)
assume(!S_AXI_ARESETN);
// Stability
// {{{
always @(posedge S_AXI_ACLK)
if (!f_past_valid || !$past(S_AXI_ARESETN))
assert(!M_AXIS_TVALID);
else if ($past(M_AXIS_TVALID && !M_AXIS_TREADY))
begin
assert(M_AXIS_TVALID);
assert($stable(M_AXIS_TDATA));
assert($stable(M_AXIS_TLAST));
assert($stable(M_AXIS_TUSER));
end
// }}}
// Packet counter, assertions on the output
// {{{
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
fm_packet_counter <= 0;
else if (M_AXIS_TVALID && M_AXIS_TREADY)
begin
fm_packet_counter <= fm_packet_counter + 1;
if (M_AXIS_TLAST)
fm_packet_counter <= 0;
end
always @(*)
if (S_AXI_ARESETN && OPT_PACKET_LENGTH != 0)
begin
assert(fm_packet_counter < OPT_PACKET_LENGTH);
assert(M_AXIS_TLAST == (fm_packet_counter == OPT_PACKET_LENGTH-1));
assert(m_packet_count == fm_packet_counter);
if (!o_fault)
begin
if (skdr_valid && skd_last)
begin
assert(s_packet_counter == 0);
assert(m_packet_count == OPT_PACKET_LENGTH-2);
end else if (M_AXIS_TVALID && M_AXIS_TLAST)
begin
assert(s_packet_counter == (skdr_valid ? 1:0));
assert(m_packet_count == OPT_PACKET_LENGTH-1);
end else
assert(s_packet_counter == m_packet_count
+ (skdr_valid ? 1:0)
+ (M_AXIS_TVALID ? 1:0));
end
end
always @(*)
if (S_AXI_ARESETN && !o_fault && OPT_PACKET_LENGTH > 0)
assert(s_packet_counter < OPT_PACKET_LENGTH);
always @(*)
if (S_AXI_ARESETN && OPT_PACKET_LENGTH > 0)
assert(m_idle == (m_packet_count == 0 && !M_AXIS_TVALID));
// }}}
// Input stall counting
// {{{
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
fm_stall_count <= 0;
else if (!S_AXIS_TVALID || S_AXIS_TREADY)
fm_stall_count <= 0;
always @(*)
if (S_AXI_ARESETN && OPT_MAX_STALL > 0)
assert(fm_stall_count < OPT_MAX_STALL);
// }}}
generate if (F_OPT_FAULTLESS)
begin
// {{{
// Stall and stability properties
// {{{
always @(posedge S_AXI_ACLK)
if (!f_past_valid || !$past(S_AXI_ARESETN))
assume(!S_AXIS_TVALID);
else if ($past(S_AXIS_TVALID && !S_AXIS_TREADY))
begin
assume(S_AXIS_TVALID);
assume($stable(S_AXIS_TDATA));
assume($stable(S_AXIS_TLAST));
assume($stable(S_AXIS_TUSER));
end
// }}}
// f_packet_counter
// {{{
if (OPT_PACKET_LENGTH != 0)
begin
// {{{
reg [LGPKTLEN-1:0] fs_packet_counter;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
fs_packet_counter <= 0;
else if (S_AXIS_TVALID && S_AXIS_TREADY)
begin
fs_packet_counter <= fs_packet_counter + 1;
if (S_AXIS_TLAST)
fs_packet_counter <= 0;
end
always @(*)
if (S_AXI_ARESETN)
begin
assert(s_packet_counter == fs_packet_counter);
if (skdr_valid && skd_last)
assert(s_packet_counter == 0);
else if (M_AXIS_TVALID && M_AXIS_TLAST)
assert(s_packet_counter == (skdr_valid ? 1:0));
else // if (!M_AXIS_TVALID && !M_AXIS_TLAST)
assert(s_packet_counter
== fm_packet_counter
+ (skdr_valid ? 1:0)
+ (M_AXIS_TVALID ? 1:0));
end
always @(*)
if (S_AXI_ARESETN && S_AXIS_TVALID)
assume(S_AXIS_TLAST == (fs_packet_counter == OPT_PACKET_LENGTH-1));
// }}}
end
// }}}
// f_stall_count
// {{{
if (OPT_MAX_STALL != 0)
begin
// {{{
reg [LGSTALLCOUNT-1:0] f_stall_count;
initial stall_count = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
f_stall_count <= 0;
else if (!S_AXIS_TVALID || S_AXIS_TREADY)
f_stall_count <= 0;
else
f_stall_count <= f_stall_count + 1;
always @(*)
if (S_AXI_ARESETN)
assert(stall_count == f_stall_count);
always @(*)
assume(f_stall_count <= OPT_MAX_STALL);
// }}}
end
// }}}
always @(*)
assert(!o_fault);
// }}}
end endgenerate
////////////////////////////////////////////////////////////////////////
//
// Cover checks
// {{{
////////////////////////////////////////////////////////////////////////
//
generate if (F_OPT_FAULTLESS)
begin
end else begin
reg cvr_stall_fault, cvr_packet_fault, cvr_change_fault,
cvr_last;
initial cvr_last = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
cvr_last <= 1'b0;
else if (o_fault && $stable(o_fault)
&& $rose(M_AXIS_TVALID && M_AXIS_TLAST))
cvr_last <= 1'b1;
initial cvr_change_fault = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
cvr_change_fault <= 1'b0;
else if (!o_fault && change_fault && !packet_fault && !stall_fault)
cvr_change_fault <= 1'b1;
else if (!o_fault)
cvr_change_fault <= 1'b0;
if (OPT_PACKET_LENGTH > 0)
begin
initial cvr_packet_fault = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
cvr_packet_fault <= 1'b0;
else if (!o_fault && !change_fault && packet_fault && !stall_fault)
cvr_packet_fault <= 1'b1;
else if (!o_fault)
cvr_packet_fault <= 1'b0;
always @(posedge S_AXI_ACLK)
if (S_AXI_ARESETN && $past(S_AXI_ARESETN))
begin
cover($fell(o_fault) && cvr_packet_fault);
cover($fell(o_fault) && cvr_packet_fault && cvr_last);
end
end
if (OPT_MAX_STALL > 0)
begin
initial cvr_stall_fault = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
cvr_stall_fault <= 1'b0;
else if (!o_fault && !change_fault && !packet_fault && stall_fault)
cvr_stall_fault <= 1'b1;
else if (!o_fault)
cvr_stall_fault <= 1'b0;
always @(posedge S_AXI_ACLK)
if (S_AXI_ARESETN && $past(S_AXI_ARESETN))
begin
cover($fell(o_fault) && cvr_stall_fault);
cover($fell(o_fault) && cvr_stall_fault && cvr_last);
end
end
always @(posedge S_AXI_ACLK)
if (S_AXI_ARESETN && $past(S_AXI_ARESETN))
begin
cover($fell(o_fault));
cover($fell(o_fault) && cvr_change_fault);
cover($fell(o_fault) && cvr_change_fault && cvr_last);
end
end endgenerate
// }}}
`endif
// }}}
endmodule