forked from ZipCPU/wb2axip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
axisafety.v
2087 lines (1926 loc) · 52.7 KB
/
axisafety.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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
//
// Filename: axisafety.v
// {{{
// Project: WB2AXIPSP: bus bridges and other odds and ends
//
// Purpose: Given that 1) AXI interfaces can be difficult to write and to
// get right, 2) my experiences with the AXI infrastructure of an
// ARM+FPGA is that the device needs a power cycle following a bus fault,
// and given that I've now found multiple interfaces that had some bug
// or other within them, the following is a bus fault isolator. It acts
// as a bump in the log between the interconnect and the user core. As
// such, it has two interfaces: The first is the slave interface,
// coming from the interconnect. The second interface is the master
// interface, proceeding to a slave that might be faulty.
//
// INTERCONNECT --> (S) AXISAFETY (M) --> POTENTIALLY FAULTY CORE
//
// The slave interface has been formally verified to be a valid AXI
// slave, independent of whatever the potentially faulty core might do.
// If the user core (i.e. the potentially faulty one) responds validly
// to the requests of a master, then this core will turn into a simple
// delay on the bus. If, on the other hand, the user core suffers from
// a protocol error, then this core will set one of two output
// flags--either o_write_fault or o_read_fault indicating whether a write
// or a read fault was detected. Further attempts to access the user
// core will result in bus errors, generated by the AXISAFETY core.
//
// Assuming the bus master can properly detect and deal with a bus error,
// this should then make it possible to properly recover from a bus
// protocol error without later needing to cycle power.
//
// The core does have a couple of limitations. For example, it can only
// handle one burst transaction, and one ID at a time. This matches the
// performances of Xilinx's example AXI full core, from which many user
// cores have have been copied/modified from. Therefore, there will be
// no performance loss for such a core.
//
// Because of this, it is still possible that the user core might have an
// undetected fault when using this core. For example, if the interconnect
// issues more than one bus request before receiving the response from the
// first request, this safety core will stall the second request,
// preventing the downstream core from seeing this second request. If the
// downstream core would suffer from an error while handling the second
// request, by preventing the user core from seeing the second request this
// core eliminates that potential for error.
//
// Usage: The important part of using this core is to connect the slave
// side to the interconnect, and the master side to the user core.
//
// Some options are available:
//
// 1) C_S_AXI_ADDR_WIDTH The number of address bits in the AXI channel
// 1) C_S_AXI_DATA_WIDTH The number of data bits in the AXI channel
// 3) C_S_AXI_ID_WIDTH The number of bits in an AXI ID. As currently
// written, this number cannot be zero. You can, however, set it
// to '1' and connect all of the relevant ID inputs to zero.
//
// These three parameters have currently been abbreviated with AW, DW, and
// IW. I anticipate returning them to their original meanings, I just
// haven't done so (yet).
//
// 4) OPT_TIMEOUT This is the number of clock periods, from
// interconnect request to interconnect response, that the
// slave needs to respond within. (The actual timeout from the
// user slave's perspective will be shorter than this.)
//
// This timeout is part of the check that a slave core will
// always return a response. From the standpoint of this core,
// it can be set arbitrarily large. (From the standpoint of
// formal verification, it needs to be kept short ...)
// Feel free to set this even as large as 1ms if you would like.
//
// 5) OPT_SELF_RESET If set, will send a reset signal to the slave
// following any write or read fault. This will cause the other
// side of the link (write or read) to fault as well. Once the
// channel then becomes inactive, the slave will be released from
// reset and will be able to interact with the rest of the bus
// again.
//
// Performance: As mentioned above, this core can handle one read burst and one
// write burst at a time, no more. Further, the core will delay
// an input path by one clock and the output path by another clock, so that
// the latency involved with using this core is a minimum of two extra
// clocks beyond the latency user slave core.
//
// Maximum write latency: N+3 clocks for a burst of N values
// Maximum read latency: N+3 clocks for a burst of N values
//
// Faults detected:
//
// Write channel:
// 1. Raising BVALID prior to the last write value being sent
// to the user/slave core.
// 2. Raising BVALID prior to accepting the write address
// 3. A BID return ID that doesn't match the request AWID
// 4. Sending too many returns, such as not dropping BVALID
// or raising it when there is no outstanding write
// request
//
// While the following technically isn't a violation of the
// AXI protocol, it is treated as such by this fault isolator.
//
// 5. Accepting write data prior to the write address
//
// The fault isolator will guarantee that AWVALID is raised before
// WVALID, so this shouldn't be a problem.
//
// Read channel:
//
// 1. Raising RVALID before accepting the read address, ARVALID
// 2. A return ID that doesn't match the ID that was sent.
// 3. Raising RVALID after the last return value, and so returning
// too many response values
// 4. Setting the RLAST value on anything but the last value from
// the bus.
//
//
// 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 axisafety #(
// {{{
parameter C_S_AXI_ID_WIDTH = 1,
parameter C_S_AXI_DATA_WIDTH = 32,
parameter C_S_AXI_ADDR_WIDTH = 16,
// OPT_SELF_RESET: Set to true if the downstream slave should be
// reset upon detecting an error and separate from the upstream reset
// domain
parameter [0:0] OPT_SELF_RESET = 0,
//
// I use the following abbreviations, IW, DW, and AW, to simplify
// the code below (and to help it fit within an 80 col terminal)
localparam IW = C_S_AXI_ID_WIDTH,
localparam DW = C_S_AXI_DATA_WIDTH,
localparam AW = C_S_AXI_ADDR_WIDTH,
//
// OPT_TIMEOUT references the number of clock cycles to wait
// between raising the *VALID signal and when the respective
// *VALID return signal must be high. You might wish to set this
// to a very high number, to allow your core to work its magic.
parameter OPT_TIMEOUT = 20
// }}}
) (
// {{{
output reg o_read_fault,
output reg o_write_fault,
// User ports ends
// Do not modify the ports beyond this line
// Global Clock Signal
input wire S_AXI_ACLK,
// Global Reset Signal. This Signal is Active LOW
input wire S_AXI_ARESETN,
output reg M_AXI_ARESETN,
//
// The input side. This is where slave requests come into
// the core.
// {{{
//
// Write address
input wire [IW-1 : 0] S_AXI_AWID,
input wire [AW-1 : 0] S_AXI_AWADDR,
input wire [7 : 0] S_AXI_AWLEN,
input wire [2 : 0] S_AXI_AWSIZE,
input wire [1 : 0] S_AXI_AWBURST,
input wire S_AXI_AWLOCK,
input wire [3 : 0] S_AXI_AWCACHE,
input wire [2 : 0] S_AXI_AWPROT,
input wire [3 : 0] S_AXI_AWQOS,
input wire S_AXI_AWVALID,
output reg S_AXI_AWREADY,
// Write data
input wire [DW-1 : 0] S_AXI_WDATA,
input wire [(DW/8)-1 : 0] S_AXI_WSTRB,
input wire S_AXI_WLAST,
input wire S_AXI_WVALID,
output reg S_AXI_WREADY,
// Write return
output reg [IW-1 : 0] S_AXI_BID,
output reg [1 : 0] S_AXI_BRESP,
output reg S_AXI_BVALID,
input wire S_AXI_BREADY,
// Read address
input wire [IW-1 : 0] S_AXI_ARID,
input wire [AW-1 : 0] S_AXI_ARADDR,
input wire [7 : 0] S_AXI_ARLEN,
input wire [2 : 0] S_AXI_ARSIZE,
input wire [1 : 0] S_AXI_ARBURST,
input wire S_AXI_ARLOCK,
input wire [3 : 0] S_AXI_ARCACHE,
input wire [2 : 0] S_AXI_ARPROT,
input wire [3 : 0] S_AXI_ARQOS,
input wire S_AXI_ARVALID,
output reg S_AXI_ARREADY,
// Read data
output reg [IW-1 : 0] S_AXI_RID,
output reg [DW-1 : 0] S_AXI_RDATA,
output reg [1 : 0] S_AXI_RRESP,
output reg S_AXI_RLAST,
output reg S_AXI_RVALID,
input wire S_AXI_RREADY,
// }}}
//
// The output side, where slave requests are forwarded to the
// actual slave
// {{{
// Write address
// {{{
output reg [IW-1 : 0] M_AXI_AWID,
output reg [AW-1 : 0] M_AXI_AWADDR,
output reg [7 : 0] M_AXI_AWLEN,
output reg [2 : 0] M_AXI_AWSIZE,
output reg [1 : 0] M_AXI_AWBURST,
output reg M_AXI_AWLOCK,
output reg [3 : 0] M_AXI_AWCACHE,
output reg [2 : 0] M_AXI_AWPROT,
output reg [3 : 0] M_AXI_AWQOS,
output reg M_AXI_AWVALID,
input wire M_AXI_AWREADY,
// Write data
output reg [DW-1 : 0] M_AXI_WDATA,
output reg [(DW/8)-1 : 0] M_AXI_WSTRB,
output reg M_AXI_WLAST,
output reg M_AXI_WVALID,
input wire M_AXI_WREADY,
// Write return
input wire [IW-1 : 0] M_AXI_BID,
input wire [1 : 0] M_AXI_BRESP,
input wire M_AXI_BVALID,
output wire M_AXI_BREADY,
// }}}
// Read address
// {{{
output reg [IW-1 : 0] M_AXI_ARID,
output reg [AW-1 : 0] M_AXI_ARADDR,
output reg [7 : 0] M_AXI_ARLEN,
output reg [2 : 0] M_AXI_ARSIZE,
output reg [1 : 0] M_AXI_ARBURST,
output reg M_AXI_ARLOCK,
output reg [3 : 0] M_AXI_ARCACHE,
output reg [2 : 0] M_AXI_ARPROT,
output reg [3 : 0] M_AXI_ARQOS,
output reg M_AXI_ARVALID,
input wire M_AXI_ARREADY,
// Read data
input wire [IW-1 : 0] M_AXI_RID,
input wire [DW-1 : 0] M_AXI_RDATA,
input wire [1 : 0] M_AXI_RRESP,
input wire M_AXI_RLAST,
input wire M_AXI_RVALID,
output wire M_AXI_RREADY
// }}}
// }}}
// }}}
);
localparam LGTIMEOUT = $clog2(OPT_TIMEOUT+1);
localparam EXOKAY = 2'b01;
localparam SLAVE_ERROR = 2'b10;
//
//
// Register declarations
// {{{
reg faulty_write_return, faulty_read_return;
wire clear_fault;
//
// Timer/timeout variables
reg [LGTIMEOUT-1:0] write_timer, read_timer;
reg write_timeout, read_timeout;
//
// Double buffer the write address channel
reg r_awvalid, m_awvalid;
reg [IW-1:0] r_awid, m_awid;
reg [AW-1:0] r_awaddr, m_awaddr;
reg [7:0] r_awlen, m_awlen;
reg [2:0] r_awsize, m_awsize;
reg [1:0] r_awburst, m_awburst;
reg r_awlock, m_awlock;
reg [3:0] r_awcache, m_awcache;
reg [2:0] r_awprot, m_awprot;
reg [3:0] r_awqos, m_awqos;
//
// Double buffer for the write channel
reg r_wvalid,m_wvalid;
reg [DW-1:0] r_wdata, m_wdata;
reg [DW/8-1:0] r_wstrb, m_wstrb;
reg r_wlast, m_wlast;
//
// Double buffer the write response channel
reg m_bvalid; // r_bvalid == 0
reg [IW-1:0] m_bid;
reg [1:0] m_bresp;
//
// Double buffer the read address channel
reg r_arvalid, m_arvalid;
reg [IW-1:0] r_arid, m_arid;
reg [AW-1:0] r_araddr, m_araddr;
reg [7:0] r_arlen, m_arlen;
reg [2:0] r_arsize, m_arsize;
reg [1:0] r_arburst, m_arburst;
reg r_arlock, m_arlock;
reg [3:0] r_arcache, m_arcache;
reg [2:0] r_arprot, m_arprot;
reg [3:0] r_arqos, m_arqos;
//
// Double buffer the read data response channel
reg r_rvalid,m_rvalid;
reg [IW-1:0] m_rid;
reg [1:0] r_rresp, m_rresp;
reg m_rlast;
reg [DW-1:0] r_rdata, m_rdata;
//
// Write FIFO data
reg [IW-1:0] wfifo_id;
reg wfifo_lock;
//
// Read FIFO data
reg [IW-1:0] rfifo_id;
reg rfifo_lock;
reg [8:0] rfifo_counter;
reg rfifo_empty, rfifo_last, rfifo_penultimate;
//
//
reg [0:0] s_wbursts;
reg [8:0] m_wpending;
reg m_wempty, m_wlastctr;
reg waddr_valid, raddr_valid;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Write channel processing
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// Write address processing
// {{{
// S_AXI_AWREADY
// {{{
initial S_AXI_AWREADY = (OPT_SELF_RESET) ? 0:1;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
S_AXI_AWREADY <= !OPT_SELF_RESET;
else if (S_AXI_AWVALID && S_AXI_AWREADY)
S_AXI_AWREADY <= 0;
else if (clear_fault)
S_AXI_AWREADY <= 1;
else if (!S_AXI_AWREADY)
S_AXI_AWREADY <= !S_AXI_AWREADY && S_AXI_BVALID && S_AXI_BREADY;
// }}}
// waddr_valid
// {{{
generate if (OPT_SELF_RESET)
begin
initial waddr_valid = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
waddr_valid <= 0;
else if (S_AXI_AWVALID && S_AXI_AWREADY)
waddr_valid <= 1;
else if (waddr_valid)
waddr_valid <= !S_AXI_BVALID || !S_AXI_BREADY;
end else begin
always @(*)
waddr_valid = !S_AXI_AWREADY;
end endgenerate
// }}}
// r_aw*
// {{{
// Double buffer for the AW* channel
//
initial r_awvalid = 0;
always @(posedge S_AXI_ACLK)
begin
if (S_AXI_AWVALID && S_AXI_AWREADY)
begin
r_awvalid <= 1'b0;
r_awid <= S_AXI_AWID;
r_awaddr <= S_AXI_AWADDR;
r_awlen <= S_AXI_AWLEN;
r_awsize <= S_AXI_AWSIZE;
r_awburst <= S_AXI_AWBURST;
r_awlock <= S_AXI_AWLOCK;
r_awcache <= S_AXI_AWCACHE;
r_awprot <= S_AXI_AWPROT;
r_awqos <= S_AXI_AWQOS;
end else if (M_AXI_AWREADY)
r_awvalid <= 0;
if (!S_AXI_ARESETN)
r_awvalid <= 0;
end
// }}}
// m_aw*
// {{{
// Second half of the AW* double-buffer. The m_* terms reference
// either the value in the double buffer (assuming one is in there),
// or the incoming value (if the double buffer is empty)
//
always @(*)
if (r_awvalid)
begin
m_awvalid = r_awvalid;
m_awid = r_awid;
m_awaddr = r_awaddr;
m_awlen = r_awlen;
m_awsize = r_awsize;
m_awburst = r_awburst;
m_awlock = r_awlock;
m_awcache = r_awcache;
m_awprot = r_awprot;
m_awqos = r_awqos;
end else begin
m_awvalid = S_AXI_AWVALID && S_AXI_AWREADY;
m_awid = S_AXI_AWID;
m_awaddr = S_AXI_AWADDR;
m_awlen = S_AXI_AWLEN;
m_awsize = S_AXI_AWSIZE;
m_awburst = S_AXI_AWBURST;
m_awlock = S_AXI_AWLOCK;
m_awcache = S_AXI_AWCACHE;
m_awprot = S_AXI_AWPROT;
m_awqos = S_AXI_AWQOS;
end
// }}}
// M_AXI_AW*
// {{{
// Set the output AW* channel outputs--but only on no fault
//
initial M_AXI_AWVALID = 0;
always @(posedge S_AXI_ACLK)
begin
if (!M_AXI_AWVALID || M_AXI_AWREADY)
begin
if (o_write_fault)
M_AXI_AWVALID <= 0;
else
M_AXI_AWVALID <= m_awvalid;
M_AXI_AWID <= m_awid;
M_AXI_AWADDR <= m_awaddr;
M_AXI_AWLEN <= m_awlen;
M_AXI_AWSIZE <= m_awsize;
M_AXI_AWBURST <= m_awburst;
M_AXI_AWLOCK <= m_awlock;
M_AXI_AWCACHE <= m_awcache;
M_AXI_AWPROT <= m_awprot;
M_AXI_AWQOS <= m_awqos;
end
if (!M_AXI_ARESETN)
M_AXI_AWVALID <= 0;
end
// }}}
// }}}
// s_wbursts
// {{{
// Count write bursts outstanding from the standpoint of
// the incoming (slave) channel
//
// Notice that, as currently built, the count can only ever be one
// or zero.
//
// We'll use this count in a moment to determine if a response
// has taken too long, or if a response is returned when there's
// no outstanding request
initial s_wbursts = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
s_wbursts <= 0;
else if (S_AXI_WVALID && S_AXI_WREADY && S_AXI_WLAST)
s_wbursts <= 1;
else if (S_AXI_BVALID && S_AXI_BREADY)
s_wbursts <= 0;
// }}}
// wfifo_id, wfifo_lock
// {{{
// Keep track of the ID of the last transaction. Since we only
// ever have one write transaction outstanding, this will need to be
// the ID of the returned value.
//
always @(posedge S_AXI_ACLK)
if (S_AXI_AWVALID && S_AXI_AWREADY)
begin
// {{{
wfifo_id <= S_AXI_AWID;
wfifo_lock <= S_AXI_AWLOCK;
// }}}
end
// }}}
// m_wpending, m_wempty, m_wlastctr
// {{{
// m_wpending counts the number of (remaining) write data values that
// need to be sent to the slave. It counts this number with respect
// to the *SLAVE*, not the master. When m_wpending == 1, WLAST shall
// be true. To make comparisons of (m_wpending == 0) or (m_wpending>0),
// m_wempty is assigned to (m_wpending). Similarly, m_wlastctr is
// assigned to (m_wpending == 1).
//
initial m_wpending = 0;
initial m_wempty = 1;
initial m_wlastctr = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN || o_write_fault)
begin
// {{{
m_wpending <= 0;
m_wempty <= 1;
m_wlastctr <= 0;
// }}}
end else if (M_AXI_AWVALID && M_AXI_AWREADY)
begin
// {{{
if (M_AXI_WVALID && M_AXI_WREADY)
begin
// {{{
// Accepting AW* and W* packets on the same
// clock
if (m_wpending == 0)
begin
// The AW* and W* packets go together
m_wpending <= {1'b0, M_AXI_AWLEN};
m_wempty <= (M_AXI_AWLEN == 0);
m_wlastctr <= (M_AXI_AWLEN == 1);
end else begin
// The W* packet goes with the last
// AW* command, the AW* packet with a
// new one
m_wpending <= M_AXI_AWLEN+1;
m_wempty <= 0;
m_wlastctr <= (M_AXI_AWLEN == 0);
end
// }}}
end else begin
// {{{
m_wpending <= M_AXI_AWLEN+1;
m_wempty <= 0;
m_wlastctr <= (M_AXI_AWLEN == 0);
// }}}
end
// }}}
end else if (M_AXI_WVALID && M_AXI_WREADY && (!m_wempty))
begin
// {{{
// The AW* channel is idle, and we just accepted a value
// on the W* channel
m_wpending <= m_wpending - 1;
m_wempty <= (m_wpending <= 1);
m_wlastctr <= (m_wpending == 2);
// }}}
end
// }}}
// Write data processing
// {{{
// S_AXI_WREADY
// {{{
// The S_AXI_WREADY or write channel stall signal
//
// For this core, we idle at zero (stalled) until an AW* packet
// comes through
//
initial S_AXI_WREADY = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
S_AXI_WREADY <= 0;
else if (S_AXI_WVALID && S_AXI_WREADY)
begin
// {{{
if (S_AXI_WLAST)
S_AXI_WREADY <= 0;
else if (o_write_fault)
S_AXI_WREADY <= 1;
else if (!M_AXI_WVALID || M_AXI_WREADY)
S_AXI_WREADY <= 1;
else
S_AXI_WREADY <= 0;
// }}}
end else if ((s_wbursts == 0)&&(waddr_valid)
&&(o_write_fault || M_AXI_WREADY))
S_AXI_WREADY <= 1;
else if (S_AXI_AWVALID && S_AXI_AWREADY)
S_AXI_WREADY <= 1;
// }}}
// r_w*
// {{{
// Double buffer for the write channel
//
// As before, the r_* values contain the values in the double
// buffer itself
//
initial r_wvalid = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
r_wvalid <= 0;
else if (!r_wvalid)
begin
// {{{
r_wvalid <= (S_AXI_WVALID && S_AXI_WREADY
&& M_AXI_WVALID && !M_AXI_WREADY);
r_wdata <= S_AXI_WDATA;
r_wstrb <= S_AXI_WSTRB;
r_wlast <= S_AXI_WLAST;
// }}}
end else if (o_write_fault || M_AXI_WREADY || !M_AXI_ARESETN)
r_wvalid <= 0;
// }}}
// m_w*
// {{{
// Here's the result of our double buffer
//
// m_* references the value within the double buffer in the case that
// something is in the double buffer. Otherwise, it is the values
// directly from the inputs. In the case of a fault, neither is true.
// Write faults override.
//
always @(*)
if (o_write_fault)
begin
// {{{
m_wvalid = !m_wempty;
m_wlast = m_wlastctr;
m_wstrb = 0;
// }}}
end else if (r_wvalid)
begin
// {{{
m_wvalid = 1;
m_wstrb = r_wstrb;
m_wlast = r_wlast;
// }}}
end else begin
// {{{
m_wvalid = S_AXI_WVALID && S_AXI_WREADY;
m_wstrb = S_AXI_WSTRB;
m_wlast = S_AXI_WLAST;
// }}}
end
// }}}
// m_wdata
// {{{
// The logic for the DATA output of the double buffer doesn't
// matter so much in the case of o_write_fault
always @(*)
if (r_wvalid)
m_wdata = r_wdata;
else
m_wdata = S_AXI_WDATA;
// }}}
// M_AXI_W*
// {{{
// Set the downstream write channel values
//
// As per AXI spec, these values *must* be registered. Note that our
// source here is the m_* double buffer/incoming write data switch.
initial M_AXI_WVALID = 0;
always @(posedge S_AXI_ACLK)
begin
if (!M_AXI_WVALID || M_AXI_WREADY)
begin
M_AXI_WVALID <= m_wvalid;
M_AXI_WDATA <= m_wdata;
M_AXI_WSTRB <= m_wstrb;
M_AXI_WLAST <= m_wlast;
end
// Override the WVALID signal (only) on reset, voiding any
// output we might otherwise send.
if (!M_AXI_ARESETN)
M_AXI_WVALID <= 0;
end
// }}}
// }}}
// Write fault detection
// {{{
// write_timer
// {{{
// The write timer
//
// The counter counts up to saturation. It is reset any time
// the write channel is either clear, or a value is accepted
// at the *MASTER* (not slave) side. Why the master side? Simply
// because it makes the proof below easier. (At one time I checked
// both, but then couldn't prove that the faults wouldn't get hit
// if the slave responded in time.)
initial write_timer = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN || !waddr_valid)
write_timer <= 0;
else if (!o_write_fault && M_AXI_BVALID)
write_timer <= 0;
else if (S_AXI_WREADY)
write_timer <= 0;
else if (!(&write_timer))
write_timer <= write_timer + 1;
// }}}
// write_timeout
// {{{
// Write timeout detector
//
// If the write_timer reaches the OPT_TIMEOUT, then the write_timeout
// will get set true. This will force a fault, taking the write
// channel off line.
initial write_timeout = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN || clear_fault)
write_timeout <= 0;
else if (M_AXI_BVALID)
write_timeout <= write_timeout;
else if (S_AXI_WVALID && S_AXI_WREADY)
write_timeout <= write_timeout;
else if (write_timer >= OPT_TIMEOUT)
write_timeout <= 1;
// }}}
// faulty_write_return
// {{{
// fault_write_return
//
// This combinational logic is used to catch an invalid return, and
// so take the slave off-line before the return can corrupt the master
// channel. Reasons for taking the write return off line are listed
// below.
always @(*)
begin
faulty_write_return = 0;
if (M_AXI_WVALID && M_AXI_WREADY
&& M_AXI_AWVALID && !M_AXI_AWREADY)
// Accepting the write *data* prior to the write
// *address* is a fault
faulty_write_return = 1;
if (M_AXI_BVALID)
begin
if (M_AXI_AWVALID || M_AXI_WVALID)
// Returning a B* acknowledgement while the
// request remains outstanding is also a fault
faulty_write_return = 1;
if (!m_wempty)
// Same as above, but this time the write
// channel is neither complete, nor is *WVALID
// active. Values remain to be written,
// and so a return is a fault.
faulty_write_return = 1;
if (s_wbursts <= (S_AXI_BVALID ? 1:0))
// Too many acknowledgments
//
// Returning more than one BVALID&BREADY for
// every AWVALID & AWREADY is a fault.
faulty_write_return = 1;
if (M_AXI_BID != wfifo_id)
// An attempt to return the wrong ID
faulty_write_return = 1;
if (M_AXI_BRESP == EXOKAY && !wfifo_lock)
// An attempt to return the wrong ID
faulty_write_return = 1;
end
end
// }}}
// o_write_fault
// {{{
// On a write fault, we're going to disconnect the write port from
// the slave, and return errors on each write connect. o_write_fault
// is our signal determining if the write channel is disconnected.
//
// Most of this work is determined within faulty_write_return above.
// Here we do just a bit more:
initial o_write_fault = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN || clear_fault)
o_write_fault <= 0;
else if ((!M_AXI_ARESETN&&o_read_fault) || write_timeout)
o_write_fault <= 1;
else if (M_AXI_BVALID && M_AXI_BREADY)
o_write_fault <= (o_write_fault) || faulty_write_return;
else if (M_AXI_WVALID && M_AXI_WREADY
&& M_AXI_AWVALID && !M_AXI_AWREADY)
// Accepting the write data prior to the write address
// is a fault
o_write_fault <= 1;
// }}}
// }}}
// Write return generation
// {{{
// m_b*
// {{{
// Since we only ever allow a single write burst at a time, we don't
// need to double buffer the return channel. Hence, we'll set our
// return channel based upon the incoming values alone. Note that
// we're overriding the M_AXI_BID below, in order to make certain that
// the return goes to the right source.
//
always @(*)
if (o_write_fault)
begin
m_bvalid = (s_wbursts > (S_AXI_BVALID ? 1:0));
m_bid = wfifo_id;
m_bresp = SLAVE_ERROR;
end else begin
m_bvalid = M_AXI_BVALID;
if (faulty_write_return)
m_bvalid = 0;
m_bid = wfifo_id;
m_bresp = M_AXI_BRESP;
end
// }}}
// S_AXI_B*
// {{{
// We'll *never* stall the slaves BREADY channel
//
// If the slave returns the response we are expecting, then S_AXI_BVALID
// will be low and it can go directly into the S_AXI_BVALID slot. If
// on the other hand the slave returns M_AXI_BVALID at the wrong time,
// then we'll quietly accept it and send the write interface into
// fault detected mode, setting o_write_fault.
//
// Sadly, this will create a warning in Vivado. If/when you see it,
// see this note and then just ignore it.
assign M_AXI_BREADY = 1;
//
// Return a write acknowlegement at the end of every write
// burst--regardless of whether or not the slave does so
//
initial S_AXI_BVALID = 0;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
S_AXI_BVALID <= 0;
else if (!S_AXI_BVALID || S_AXI_BREADY)
S_AXI_BVALID <= m_bvalid;
//
// Set the values associated with the response
//
always @(posedge S_AXI_ACLK)
if (!S_AXI_BVALID || S_AXI_BREADY)
begin
S_AXI_BID <= m_bid;
S_AXI_BRESP <= m_bresp;
end
// }}}
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Read channel processing
// {{{
////////////////////////////////////////////////////////////////////////
//
//
//
// Read address channel
// {{{
// S_AXI_ARREADY
// {{{
initial S_AXI_ARREADY = (OPT_SELF_RESET) ? 0:1;
always @(posedge S_AXI_ACLK)
if (!S_AXI_ARESETN)
S_AXI_ARREADY <= !OPT_SELF_RESET;
else if (S_AXI_ARVALID && S_AXI_ARREADY)
S_AXI_ARREADY <= 0;
else if (clear_fault)
S_AXI_ARREADY <= 1;
else if (!S_AXI_ARREADY)
S_AXI_ARREADY <= (S_AXI_RVALID && S_AXI_RLAST && S_AXI_RREADY);
// }}}
// raddr_valid
// {{{
always @(*)
if (OPT_SELF_RESET)
raddr_valid = !rfifo_empty;
else
raddr_valid = !S_AXI_ARREADY;
// }}}
// r_ar*
// {{{
// Double buffer the values associated with any read address request
//
initial r_arvalid = 0;
always @(posedge S_AXI_ACLK)
begin
if (S_AXI_ARVALID && S_AXI_ARREADY)
begin
r_arvalid <= (M_AXI_ARVALID && !M_AXI_ARREADY);
r_arid <= S_AXI_ARID;
r_araddr <= S_AXI_ARADDR;
r_arlen <= S_AXI_ARLEN;
r_arsize <= S_AXI_ARSIZE;
r_arburst <= S_AXI_ARBURST;
r_arlock <= S_AXI_ARLOCK;
r_arcache <= S_AXI_ARCACHE;
r_arprot <= S_AXI_ARPROT;
r_arqos <= S_AXI_ARQOS;
end else if (M_AXI_ARREADY)
r_arvalid <= 0;
if (!M_AXI_ARESETN)
r_arvalid <= 0;
end
// }}}
// m_ar*
// {{{
always @(*)
if (r_arvalid)
begin
m_arvalid = r_arvalid;
m_arid = r_arid;
m_araddr = r_araddr;
m_arlen = r_arlen;
m_arsize = r_arsize;
m_arburst = r_arburst;
m_arlock = r_arlock;
m_arcache = r_arcache;
m_arprot = r_arprot;
m_arqos = r_arqos;
end else begin
m_arvalid = S_AXI_ARVALID && S_AXI_ARREADY;
m_arid = S_AXI_ARID;
m_araddr = S_AXI_ARADDR;
m_arlen = S_AXI_ARLEN;
m_arsize = S_AXI_ARSIZE;
m_arburst = S_AXI_ARBURST;
m_arlock = S_AXI_ARLOCK;
m_arcache = S_AXI_ARCACHE;
m_arprot = S_AXI_ARPROT;
m_arqos = S_AXI_ARQOS;
end
// }}}
// M_AXI_AR*
// {{{
// Set the downstream values according to the transaction we've just
// received.
//
initial M_AXI_ARVALID = 0;
always @(posedge S_AXI_ACLK)
begin
if (!M_AXI_ARVALID || M_AXI_ARREADY)
begin
if (o_read_fault)
M_AXI_ARVALID <= 0;
else