forked from ZipCPU/wb2axip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aximm2s.v
1985 lines (1771 loc) · 48.7 KB
/
aximm2s.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: aximm2s
// {{{
// Project: WB2AXIPSP: bus bridges and other odds and ends
//
// Purpose: Converts an AXI (full) memory port to an AXI-stream
// interface.
// // {{{
// While I am aware that other vendors sell similar components, if you
// look under the hood you'll find no relation to anything but my own
// work here.
//
// Registers:
//
// 0: CMD_CONTROL
// Controls the transaction via either starting or aborting an
// ongoing transaction, provides feedback regarding the current
// status.
//
// [31] r_busy
// True if the core is in the middle of a transaction
//
// [30] r_err
// True if the core has detected an error, a bus error
// while the FIFO is reading.
//
// Writing a '1' to this bit while the core is idle will clear it.
// New transfers will not start until this bit is cleared.
//
// [29] r_complete
// True if the transaction has completed, whether normally or
// abnormally (error or abort).
//
// Any write to the CMD_CONTROL register will clear this flag.
//
// [28] r_continuous
// Normally the FIFO gets cleared and reset between operations.
// However, if you set r_continuous, the core will then expect
// a second operation to take place following the first one.
// In this case, the operation will complete but the FIFO won't
// get cleared. During this time, the FIFO will not fill further.
//
// Any write to the CMD_CONTROL register while the core is not
// busy will adjust this bit.
//
// [27] !r_increment
//
// If clear, the core reads from subsequent and incrementing
// addresses. If set, the core reads from the same address
// throughout a transaction.
//
// Writes to CMD_CONTROL while the core is idle will adjust this
// bit.
//
// [20:16] LGFIFO
// These are read-only bits, returning the size of the FIFO.
//
// ABORT
// If the core is busy, and ABORT_KEY (currently set to 8'h6d
// below) is written to the top 8-bits of this register,
// the current transfer will be aborted. Any pending reads
// will be completed, but nothing more will be written to the
// stream.
//
// Alternatively, the core will enter into an abort state
// following any returned bus error indications.
//
// 4: (Unused and reserved)
//
// 8-c: CMD_ADDRLO, CMD_ADDR_HI
// [C_AXI_ADDR_WIDTH-1:($clog2(C_AXI_DATA_WIDTH)-3)]
// If idle, the address the core will read from when it starts.
// If busy, the address the core is currently reading from.
// Upon completion, the address either returns to the starting
// address (if r_continuous is clear), or otherwise becomes the
// address where the core left off. In the case of an abort or an
// error, this will be (near) the address that was last read.
//
// Why "near"? Because this address records the reads that have
// been issued while no error is pending. If a bus error return
// comes back, there may have been several more reads issued before
// that error address.
//
// 10-14: (Unused and reserved)
//
// 18-1c: CMD_LENLO, CMD_LENHI
// [LGLEN-1:0]
// The size of the transfer in bytes. Only accepts aligned
// addresses, therefore bits [($clog2(C_AXI_DATA_WIDTH)-3):0]
// will always be forced to zero. To find out what size bus
// this core is conencted to, or the maximum transfer length,
// write a -1 to this value and read the returning result.
// Only the active bits will be set.
//
// While the core is busy, reads from this address will return
// the number of items still to be read from the bus.
//
// I hope to eventually add support for unaligned bursts. Such
// support is not currently part of this core.
//
// }}}
//
// Status:
// {{{
// 1. The core passes both cover checks and formal property (assertion)
// based checks. It has not (yet) been tested in real hardware.
//
// 2. I'd like to support unaligned addresses and lengths. This will
// require aligning the data coming out of the FIFO as well.
// As written, the core doesn't yet support these.
//
// }}}
// 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 aximm2s #(
// {{{
parameter C_AXI_ADDR_WIDTH = 32,
parameter C_AXI_DATA_WIDTH = 32,
parameter C_AXI_ID_WIDTH = 1,
//
// We support five 32-bit AXI-lite registers, requiring 5-bits
// of AXI-lite addressing
localparam C_AXIL_ADDR_WIDTH = 5,
localparam C_AXIL_DATA_WIDTH = 32,
//
// The bottom ADDRLSB bits of any AXI address are subword bits
localparam ADDRLSB = $clog2(C_AXI_DATA_WIDTH)-3,
localparam AXILLSB = $clog2(C_AXIL_DATA_WIDTH)-3,
//
// OPT_UNALIGNED: Allow unaligned accesses, address requests
// and sizes which may or may not match the underlying data
// width. If set, the core will quietly align these requests.
parameter [0:0] OPT_UNALIGNED = 1'b0,
//
// OPT_TKEEP [Future]: If set, will also add TKEEP signals to
// the outgoing slave interface. This is necessary if ever you
// wish to output partial stream words, such as might happen if
// the length were ever something other than a full number of
// words. (Not yet implemented)
// parameter [0:0] OPT_TKEEP = 1'b0,
//
// OPT_TLAST: If enabled, will embed TLAST=1 at the end of every
// commanded burst. If disabled, TLAST will be set to a
// constant 1'b1.
parameter [0:0] OPT_TLAST = 1'b0,
//
// ABORT_KEY is the value that, when written to the top 8-bits
// of the control word, will abort any ongoing operation.
parameter [7:0] ABORT_KEY = 8'h6d,
//
// The size of the FIFO
parameter LGFIFO = 9,
//
// Maximum number of bytes that can ever be transferred, in
// log-base 2
parameter LGLEN = 20,
//
// AXI_ID is the ID we will use for all of our AXI transactions
parameter AXI_ID = 0
// }}}
) (
// {{{
input wire S_AXI_ACLK,
input wire S_AXI_ARESETN,
//
// The stream interface
// {{{
output wire M_AXIS_TVALID,
input wire M_AXIS_TREADY,
output wire [C_AXI_DATA_WIDTH-1:0] M_AXIS_TDATA,
output wire M_AXIS_TLAST,
// }}}
//
// The control interface
// {{{
input wire S_AXIL_AWVALID,
output wire S_AXIL_AWREADY,
input wire [C_AXIL_ADDR_WIDTH-1:0] S_AXIL_AWADDR,
input wire [2:0] S_AXIL_AWPROT,
//
input wire S_AXIL_WVALID,
output wire S_AXIL_WREADY,
input wire [C_AXIL_DATA_WIDTH-1:0] S_AXIL_WDATA,
input wire [C_AXIL_DATA_WIDTH/8-1:0] S_AXIL_WSTRB,
//
output wire S_AXIL_BVALID,
input wire S_AXIL_BREADY,
output wire [1:0] S_AXIL_BRESP,
//
input wire S_AXIL_ARVALID,
output wire S_AXIL_ARREADY,
input wire [C_AXIL_ADDR_WIDTH-1:0] S_AXIL_ARADDR,
input wire [2:0] S_AXIL_ARPROT,
//
output wire S_AXIL_RVALID,
input wire S_AXIL_RREADY,
output wire [C_AXIL_DATA_WIDTH-1:0] S_AXIL_RDATA,
output wire [1:0] S_AXIL_RRESP,
// }}}
//
//
// The AXI (full) read interface
// {{{
output wire M_AXI_ARVALID,
input wire M_AXI_ARREADY,
output wire [C_AXI_ID_WIDTH-1:0] M_AXI_ARID,
output wire [C_AXI_ADDR_WIDTH-1:0] M_AXI_ARADDR,
output wire [7:0] M_AXI_ARLEN,
output wire [2:0] M_AXI_ARSIZE,
output wire [1:0] M_AXI_ARBURST,
output wire M_AXI_ARLOCK,
output wire [3:0] M_AXI_ARCACHE,
output wire [2:0] M_AXI_ARPROT,
output wire [3:0] M_AXI_ARQOS,
//
input wire M_AXI_RVALID,
output wire M_AXI_RREADY,
input wire [C_AXI_DATA_WIDTH-1:0] M_AXI_RDATA,
input wire M_AXI_RLAST,
input wire [C_AXI_ID_WIDTH-1:0] M_AXI_RID,
input wire [1:0] M_AXI_RRESP,
// }}}
//
//
// Create an output signal to indicate that we've finished
output reg o_int
// }}}
);
// Local parameter declarations
// {{{
localparam [2:0] CMD_CONTROL = 3'b000,
// CMD_UNUSED_1 = 3'b001,
CMD_ADDRLO = 3'b010,
CMD_ADDRHI = 3'b011,
// CMD_UNUSED_2 = 3'b100,
// CMD_UNUSED_3 = 3'b101,
CMD_LENLO = 3'b110,
CMD_LENHI = 3'b111;
localparam CBIT_BUSY = 31,
CBIT_ERR = 30,
CBIT_COMPLETE = 29,
CBIT_CONTINUOUS = 28,
CBIT_INCREMENT = 27;
localparam LGMAXBURST=(LGFIFO > 8) ? 8 : LGFIFO-1;
localparam LGMAX_FIXED_BURST = (LGMAXBURST < 4) ? LGMAXBURST : 4,
MAX_FIXED_BURST = (1<<LGMAX_FIXED_BURST);
localparam LGLENW = LGLEN - ($clog2(C_AXI_DATA_WIDTH)-3),
LGLENWA = LGLENW + (OPT_UNALIGNED ? 1:0);
// localparam LGFIFOB = LGFIFO + ($clog2(C_AXI_DATA_WIDTH)-3);
// localparam [ADDRLSB-1:0] LSBZEROS = 0;
// }}}
wire i_clk = S_AXI_ACLK;
wire i_reset = !S_AXI_ARESETN;
// Signal declarations
// {{{
reg r_busy, r_err, r_complete, r_continuous, r_increment,
cmd_abort, zero_length,
w_cmd_start, w_complete, w_cmd_abort, r_pre_start;
// reg cmd_start;
reg axi_abort_pending;
reg [LGLENWA-1:0] ar_requests_remaining,
ar_bursts_outstanding,
ar_next_remaining;
reg [LGMAXBURST:0] r_max_burst;
reg [C_AXI_ADDR_WIDTH-1:0] axi_raddr;
reg [C_AXI_ADDR_WIDTH-1:0] cmd_addr;
reg [LGLENW-1:0] cmd_length_w;
reg [LGLENWA-1:0] cmd_length_aligned_w;
reg unaligned_cmd_addr;
// FIFO signals
wire reset_fifo, write_to_fifo,
read_from_fifo;
wire [C_AXI_DATA_WIDTH-1:0] write_data;
wire [LGFIFO:0] fifo_fill;
wire fifo_full, fifo_empty;
wire awskd_valid, axil_write_ready;
wire [C_AXIL_ADDR_WIDTH-AXILLSB-1:0] awskd_addr;
//
wire wskd_valid;
wire [C_AXIL_DATA_WIDTH-1:0] wskd_data;
wire [C_AXIL_DATA_WIDTH/8-1:0] wskd_strb;
reg axil_bvalid;
//
wire arskd_valid, axil_read_ready;
wire [C_AXIL_ADDR_WIDTH-AXILLSB-1:0] arskd_addr;
reg [C_AXIL_DATA_WIDTH-1:0] axil_read_data;
reg axil_read_valid;
reg [C_AXIL_DATA_WIDTH-1:0] w_status_word;
reg [2*C_AXIL_DATA_WIDTH-1:0] wide_address, wide_length,
new_wideaddr, new_widelen;
wire [C_AXIL_DATA_WIDTH-1:0] new_cmdaddrlo, new_cmdaddrhi,
new_lengthlo, new_lengthhi;
reg axi_arvalid;
reg [C_AXI_ADDR_WIDTH-1:0] axi_araddr;
reg [7:0] axi_arlen;
// Speed up checking for zeros
reg ar_none_remaining,
ar_none_outstanding,
phantom_start, start_burst;
reg ar_multiple_full_bursts,
ar_multiple_fixed_bursts,
ar_multiple_bursts_remaining,
ar_needs_alignment;
wire partial_burst_requested;
reg [LGMAXBURST-1:0] addralign;
reg [LGFIFO:0] rd_uncommitted;
reg [LGMAXBURST:0] initial_burstlen;
reg [LGLENWA-1:0] rd_reads_remaining;
reg rd_none_remaining,
rd_last_remaining;
wire realign_last_valid;
/*
wr_none_pending, r_none_remaining;
reg w_phantom_start, phantom_start;
reg [LGFIFO:0] next_fill;
*/
// }}}
////////////////////////////////////////////////////////////////////////
//
// AXI-lite signaling
//
////////////////////////////////////////////////////////////////////////
//
// This is mostly the skidbuffer logic, and handling of the VALID
// and READY signals for the AXI-lite control logic in the next
// section.
// {{{
//
// Write signaling
//
// {{{
skidbuffer #(.OPT_OUTREG(0), .DW(C_AXIL_ADDR_WIDTH-AXILLSB))
axilawskid(//
.i_clk(S_AXI_ACLK), .i_reset(i_reset),
.i_valid(S_AXIL_AWVALID), .o_ready(S_AXIL_AWREADY),
.i_data(S_AXIL_AWADDR[C_AXIL_ADDR_WIDTH-1:AXILLSB]),
.o_valid(awskd_valid), .i_ready(axil_write_ready),
.o_data(awskd_addr));
skidbuffer #(.OPT_OUTREG(0), .DW(C_AXIL_DATA_WIDTH+C_AXIL_DATA_WIDTH/8))
axilwskid(//
.i_clk(S_AXI_ACLK), .i_reset(i_reset),
.i_valid(S_AXIL_WVALID), .o_ready(S_AXIL_WREADY),
.i_data({ S_AXIL_WDATA, S_AXIL_WSTRB }),
.o_valid(wskd_valid), .i_ready(axil_write_ready),
.o_data({ wskd_data, wskd_strb }));
assign axil_write_ready = awskd_valid && wskd_valid
&& (!S_AXIL_BVALID || S_AXIL_BREADY);
initial axil_bvalid = 0;
always @(posedge i_clk)
if (i_reset)
axil_bvalid <= 0;
else if (axil_write_ready)
axil_bvalid <= 1;
else if (S_AXIL_BREADY)
axil_bvalid <= 0;
assign S_AXIL_BVALID = axil_bvalid;
assign S_AXIL_BRESP = 2'b00;
// }}}
//
// Read signaling
//
// {{{
skidbuffer #(.OPT_OUTREG(0), .DW(C_AXIL_ADDR_WIDTH-AXILLSB))
axilarskid(//
.i_clk(S_AXI_ACLK), .i_reset(i_reset),
.i_valid(S_AXIL_ARVALID), .o_ready(S_AXIL_ARREADY),
.i_data(S_AXIL_ARADDR[C_AXIL_ADDR_WIDTH-1:AXILLSB]),
.o_valid(arskd_valid), .i_ready(axil_read_ready),
.o_data(arskd_addr));
assign axil_read_ready = arskd_valid
&& (!axil_read_valid || S_AXIL_RREADY);
initial axil_read_valid = 1'b0;
always @(posedge i_clk)
if (i_reset)
axil_read_valid <= 1'b0;
else if (axil_read_ready)
axil_read_valid <= 1'b1;
else if (S_AXIL_RREADY)
axil_read_valid <= 1'b0;
assign S_AXIL_RVALID = axil_read_valid;
assign S_AXIL_RDATA = axil_read_data;
assign S_AXIL_RRESP = 2'b00;
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// AXI-lite controlled logic
//
////////////////////////////////////////////////////////////////////////
//
// {{{
//
// Abort transaction
//
always @(*)
begin
w_cmd_abort = 0;
w_cmd_abort = (axil_write_ready && awskd_addr == CMD_CONTROL)
&& (wskd_strb[3] && wskd_data[31:24] == ABORT_KEY);
if (!r_busy)
w_cmd_abort = 0;
end
initial cmd_abort = 0;
always @(posedge i_clk)
if (i_reset)
cmd_abort <= 0;
else
cmd_abort <= (cmd_abort && r_busy)||w_cmd_abort;
//
// Start command
//
always @(*)
if (r_busy)
w_cmd_start = 0;
else begin
w_cmd_start = 0;
if ((axil_write_ready && awskd_addr == CMD_CONTROL)
&& (wskd_strb[3] && wskd_data[CBIT_BUSY]))
w_cmd_start = 1;
if (r_err && !wskd_data[CBIT_ERR])
w_cmd_start = 0;
if (zero_length)
w_cmd_start = 0;
if (OPT_UNALIGNED && unaligned_cmd_addr
&& wskd_data[CBIT_INCREMENT])
w_cmd_start = 0;
end
//
// Calculate busy or complete flags
//
initial r_busy = 0;
initial r_complete = 0;
always @(posedge i_clk)
if (i_reset)
begin
r_busy <= 0;
r_complete <= 0;
end else if (!r_busy)
begin
if (w_cmd_start)
r_busy <= 1'b1;
if (axil_write_ready && awskd_addr == CMD_CONTROL)
// Any write to the control register will clear the
// completion flag
r_complete <= 1'b0;
end else if (r_busy)
begin
if (w_complete)
begin
r_complete <= 1;
r_busy <= 1'b0;
end
end
//
// Interrupts
//
initial o_int = 0;
always @(posedge i_clk)
if (i_reset)
o_int <= 0;
else
o_int <= (r_busy && w_complete);
//
// Error conditions
//
always @(posedge i_clk)
if (i_reset)
r_err <= 0;
else if (!r_busy)
begin
if (axil_write_ready && awskd_addr == CMD_CONTROL)
begin
if (!w_cmd_abort)
r_err <= r_err && (!wskd_strb[3] || !wskd_data[CBIT_ERR]);
// On any request to start a transfer with an unaligned
// address, that's not incrementing--declare an
// immediate error
if (wskd_strb[3] && wskd_data[CBIT_BUSY]
&& wskd_data[CBIT_INCREMENT]
&& (OPT_UNALIGNED && unaligned_cmd_addr))
r_err <= 1'b1;
end
end else // if (r_busy)
begin
if (M_AXI_RVALID && M_AXI_RREADY && M_AXI_RRESP[1])
r_err <= 1'b1;
end
initial r_continuous = 0;
always @(posedge i_clk)
if (i_reset)
r_continuous <= 0;
else begin
if (!r_busy && axil_write_ready && awskd_addr == CMD_CONTROL
&& !w_cmd_abort)
r_continuous <= wskd_strb[3] && wskd_data[CBIT_CONTINUOUS];
end
always @(*)
begin
wide_address = 0;
wide_address[C_AXI_ADDR_WIDTH-1:0] = cmd_addr;
if (!OPT_UNALIGNED)
wide_address[ADDRLSB-1:0] = 0;
wide_length = 0;
wide_length[ADDRLSB +: LGLENW] = cmd_length_w;
end
assign new_cmdaddrlo = apply_wstrb(
wide_address[C_AXIL_DATA_WIDTH-1:0],
wskd_data, wskd_strb);
assign new_cmdaddrhi = apply_wstrb(
wide_address[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH],
wskd_data, wskd_strb);
assign new_lengthlo = apply_wstrb(
wide_length[C_AXIL_DATA_WIDTH-1:0],
wskd_data, wskd_strb);
assign new_lengthhi = apply_wstrb(
wide_length[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH],
wskd_data, wskd_strb);
always @(*)
begin
new_wideaddr = wide_address;
if (awskd_addr == CMD_ADDRLO)
new_wideaddr[C_AXIL_DATA_WIDTH-1:0] = new_cmdaddrlo;
if (awskd_addr == CMD_ADDRHI)
new_wideaddr[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH] = new_cmdaddrhi;
if (!OPT_UNALIGNED)
new_wideaddr[ADDRLSB-1:0] = 0;
new_wideaddr[2*C_AXIL_DATA_WIDTH-1:C_AXI_ADDR_WIDTH] = 0;
new_widelen = wide_length;
if (awskd_addr == CMD_LENLO)
new_widelen[C_AXIL_DATA_WIDTH-1:0] = new_lengthlo;
if (awskd_addr == CMD_LENHI)
new_widelen[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH] = new_lengthhi;
new_widelen[ADDRLSB-1:0] = 0;
new_widelen[2*C_AXIL_DATA_WIDTH-1:ADDRLSB+LGLENW] = 0;
end
initial r_increment = 1'b1;
initial cmd_addr = 0;
initial cmd_length_w = 0; // Counts in bytes
initial zero_length = 1;
initial cmd_length_aligned_w = 0;
initial unaligned_cmd_addr = 1'b0;
initial ar_multiple_full_bursts = 0;
initial ar_multiple_fixed_bursts = 0;
always @(posedge i_clk)
begin
if (axil_write_ready && !r_busy)
begin
case(awskd_addr)
CMD_CONTROL:
r_increment <= !wskd_data[CBIT_INCREMENT];
CMD_ADDRLO: begin
cmd_addr <= new_wideaddr[C_AXI_ADDR_WIDTH-1:0];
unaligned_cmd_addr <= |new_wideaddr[ADDRLSB-1:0];
if (OPT_UNALIGNED)
cmd_length_aligned_w <= cmd_length_w
+ (|new_wideaddr[ADDRLSB-1:0] ? 1:0);
// ERR: What if !r_increment? In that case, we can't
// support unaligned addressing
end
CMD_ADDRHI: if (C_AXI_ADDR_WIDTH > C_AXIL_DATA_WIDTH)
begin
cmd_addr <= new_wideaddr[C_AXI_ADDR_WIDTH-1:0];
end
CMD_LENLO: begin
cmd_length_w <= new_widelen[ADDRLSB +: LGLENW];
zero_length <= (new_widelen[ADDRLSB +: LGLENW] == 0);
cmd_length_aligned_w <= new_widelen[ADDRLSB +: LGLENW]
+ (unaligned_cmd_addr ? 1:0);
ar_multiple_full_bursts <= |new_widelen[LGLEN-1:(ADDRLSB+LGMAXBURST)];
ar_multiple_fixed_bursts <= |new_widelen[LGLEN-1:(ADDRLSB+LGMAX_FIXED_BURST)];
end
CMD_LENHI: begin
cmd_length_w <= new_widelen[ADDRLSB +: LGLENW];
zero_length <= (new_widelen[ADDRLSB +: LGLENW] == 0);
cmd_length_aligned_w <= new_widelen[ADDRLSB +: LGLENW]
+ (unaligned_cmd_addr ? 1:0);
ar_multiple_full_bursts <= |new_widelen[LGLEN-1:(ADDRLSB+LGMAXBURST)];
ar_multiple_fixed_bursts <= |new_widelen[LGLEN-1:(ADDRLSB+LGMAX_FIXED_BURST)];
end
default: begin end
endcase
end else if(r_busy && r_continuous && !axi_abort_pending
&& r_increment)
cmd_addr <= axi_raddr
+ ((M_AXI_RVALID && !M_AXI_RRESP[1]
&& (!unaligned_cmd_addr || realign_last_valid))
? (1<<ADDRLSB) : 0);
if (i_reset)
begin
r_increment <= 1'b1;
cmd_addr <= 0;
cmd_length_w <= 0;
zero_length <= 1;
unaligned_cmd_addr <= 0;
cmd_length_aligned_w <= 0;
ar_multiple_full_bursts <= 0;
ar_multiple_fixed_bursts <= 0;
end
if (!OPT_UNALIGNED)
unaligned_cmd_addr <= 0;
end
always @(*)
begin
w_status_word = 0;
w_status_word[CBIT_BUSY] = r_busy;
w_status_word[CBIT_ERR] = r_err;
w_status_word[CBIT_COMPLETE] = r_complete;
w_status_word[CBIT_CONTINUOUS] = r_continuous;
w_status_word[CBIT_INCREMENT] = !r_increment;
w_status_word[20:16] = LGFIFO;
end
always @(posedge i_clk)
if (!axil_read_valid || S_AXIL_RREADY)
begin
axil_read_data <= 0;
case(arskd_addr)
CMD_CONTROL: axil_read_data <= w_status_word;
CMD_ADDRLO: axil_read_data <= wide_address[C_AXIL_DATA_WIDTH-1:0];
CMD_ADDRHI: axil_read_data <= wide_address[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH];
CMD_LENLO: axil_read_data <= wide_length[C_AXIL_DATA_WIDTH-1:0];
CMD_LENHI: axil_read_data <= wide_length[2*C_AXIL_DATA_WIDTH-1:C_AXIL_DATA_WIDTH];
default axil_read_data <= 0;
endcase
end
function [C_AXIL_DATA_WIDTH-1:0] apply_wstrb;
input [C_AXIL_DATA_WIDTH-1:0] prior_data;
input [C_AXIL_DATA_WIDTH-1:0] new_data;
input [C_AXIL_DATA_WIDTH/8-1:0] wstrb;
integer k;
for(k=0; k<C_AXIL_DATA_WIDTH/8; k=k+1)
begin
apply_wstrb[k*8 +: 8]
= wstrb[k] ? new_data[k*8 +: 8] : prior_data[k*8 +: 8];
end
endfunction
// }}}
////////////////////////////////////////////////////////////////////////
//
// The data FIFO section
//
////////////////////////////////////////////////////////////////////////
//
// {{{
assign reset_fifo = i_reset || (!r_busy && (!r_continuous || r_err));
// Realign the data (if OPT_UNALIGN) before sending it to the FIFO
// {{{
// This allows us to handle unaligned addresses.
generate if (OPT_UNALIGNED)
begin : REALIGN_DATA
reg r_write_to_fifo;
reg [C_AXI_DATA_WIDTH-1:0] last_data,
r_write_data;
reg [ADDRLSB-1:0] corollary_shift;
reg last_valid;
reg [ADDRLSB-1:0] realignment;
always @(*)
realignment = cmd_addr[ADDRLSB-1:0];
initial last_data = 0;
always @(posedge S_AXI_ACLK)
if (reset_fifo || !unaligned_cmd_addr)
last_data <= 0;
else if (M_AXI_RVALID && M_AXI_RREADY)
last_data <= M_AXI_RDATA >> (realignment * 8);
initial last_valid = 1'b0;
always @(posedge S_AXI_ACLK)
if (reset_fifo)
last_valid <= 0;
else if (M_AXI_RVALID && M_AXI_RREADY)
last_valid <= 1'b1;
else if (!r_busy)
last_valid <= 1'b0;
assign realign_last_valid = last_valid;
always @(*)
corollary_shift = -realignment;
always @(posedge S_AXI_ACLK)
if (M_AXI_RVALID && M_AXI_RREADY)
r_write_data <= (M_AXI_RDATA << (corollary_shift*8))
| last_data;
else if (!fifo_full)
r_write_data <= last_data;
initial r_write_to_fifo = 1'b0;
always @(posedge S_AXI_ACLK)
if (reset_fifo)
r_write_to_fifo <= 1'b0;
else if (M_AXI_RVALID && M_AXI_RREADY)
r_write_to_fifo <= last_valid || !unaligned_cmd_addr;
else if (!fifo_full)
r_write_to_fifo <= 1'b0;
assign write_to_fifo = r_write_to_fifo;
assign write_data = r_write_data;
end else begin : ALIGNED_DATA
assign write_to_fifo = M_AXI_RVALID && M_AXI_RREADY;
assign write_data = M_AXI_RDATA;
assign realign_last_valid = 0;
end endgenerate
// }}}
assign read_from_fifo = M_AXIS_TVALID && M_AXIS_TREADY;
assign M_AXIS_TVALID = !fifo_empty;
// Write the results to the FIFO
// {{{
generate if (OPT_TLAST)
begin : FIFO_W_TLAST
// FIFO section--used if we have to add a TLAST signal to the
// data stream
// {{{
reg pre_tlast;
wire tlast;
// tlast will be set on the last data word of any commanded
// burst.
// Appropriately, pre_tlast = (something) && M_AXI_RVALID
// && M_AXI_RREADY && M_AXI_RLAST
// We can simplify this greatly, since any time M_AXI_RVALID is
// true, we also know that M_AXI_RREADY will be true. This
// allows us to get rid of the RREADY condition. Next, we can
// simplify the RVALID condition since we'll never write to the
// FIFO if RVALID isn't also true. Finally, we can get rid of
// M_AXI_RLAST since this is captured by rd_last_remaining.
always @(*)
pre_tlast = rd_last_remaining;
if (OPT_UNALIGNED)
begin
reg r_tlast;
// REALIGN delays the data by one clock period. We'll
// also need to delay the last as well.
// Note that no one cares what tlast is if write_to_fifo
// is zero, allowing us to massively simplify this.
always @(posedge i_clk)
r_tlast <= pre_tlast;
assign tlast = r_tlast;
end else begin
assign tlast = pre_tlast;
end
sfifo #(.BW(C_AXI_DATA_WIDTH+1), .LGFLEN(LGFIFO))
sfifo(i_clk, reset_fifo,
write_to_fifo, { tlast, write_data }, fifo_full, fifo_fill,
read_from_fifo, { M_AXIS_TLAST, M_AXIS_TDATA }, fifo_empty);
// }}}
end else begin : NO_TLAST_FIFO
// FIFO section, where TLAST is held at 1'b1
// {{{
sfifo #(.BW(C_AXI_DATA_WIDTH), .LGFLEN(LGFIFO))
sfifo(i_clk, reset_fifo,
write_to_fifo, write_data, fifo_full, fifo_fill,
read_from_fifo, M_AXIS_TDATA, fifo_empty);
assign M_AXIS_TLAST = 1'b1;
// }}}
end endgenerate
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// The incoming AXI (full) protocol section
//
////////////////////////////////////////////////////////////////////////
//
//
// {{{
// Some counters to keep track of our state
// {{{
// Count the number of word writes left to be requested, starting
// with the overall command length and then reduced by M_AWLEN on
// each address write
// {{{
always @(*)
begin
ar_next_remaining = ar_requests_remaining;
ar_next_remaining = ar_requests_remaining
+ { {(LGLENWA-8){phantom_start}},
(phantom_start) ? ~M_AXI_ARLEN : 8'h00};
end
always @(posedge i_clk)
if (!r_busy)
r_pre_start <= 1;
else
r_pre_start <= 0;
always @(posedge i_clk)
if (!r_busy)
begin
ar_needs_alignment <= 0;
if (|new_wideaddr[ADDRLSB +: LGMAXBURST])
begin
if (|new_widelen[LGLEN-1:(LGMAXBURST+ADDRLSB)])
ar_needs_alignment <= 1;
if (~new_wideaddr[ADDRLSB +: LGMAXBURST]
< new_widelen[ADDRLSB +: LGMAXBURST])
ar_needs_alignment <= 1;
end
end
initial ar_none_remaining = 1;
initial ar_requests_remaining = 0;
always @(posedge i_clk)
if (!r_busy)
begin
ar_requests_remaining <= cmd_length_aligned_w;
ar_none_remaining <= zero_length;
ar_multiple_bursts_remaining
<= |cmd_length_aligned_w[LGLENWA-1:LGMAXBURST+1];
end else if (cmd_abort || axi_abort_pending)
begin
ar_requests_remaining <= 0;
ar_none_remaining <= 1;
ar_multiple_bursts_remaining <= 0;
end else if (phantom_start)
begin
// Verilator lint_off WIDTH
ar_requests_remaining
<= ar_next_remaining;
ar_none_remaining <= (ar_next_remaining == 0);
ar_multiple_bursts_remaining
<= |ar_next_remaining[LGLENWA-1:LGMAXBURST+1];
// Verilator lint_on WIDTH
end
// }}}
// Calculate the maximum possible burst length, ignoring 4kB boundaries
// {{{
always @(*)
addralign = 1+(~cmd_addr[ADDRLSB +: LGMAXBURST]);
always @(*)
begin
initial_burstlen = (1<<LGMAXBURST);
if (!r_increment)
begin
initial_burstlen = MAX_FIXED_BURST;
if (!ar_multiple_fixed_bursts
&& !cmd_length_aligned_w[LGMAX_FIXED_BURST])
initial_burstlen = { 5'b0, cmd_length_aligned_w[
LGMAX_FIXED_BURST-1:0] };
end else if (ar_needs_alignment)
initial_burstlen = { 1'b0, addralign };
else if (!ar_multiple_full_bursts
&& !cmd_length_aligned_w[LGMAXBURST])
initial_burstlen = { 1'b0, cmd_length_aligned_w[
LGMAXBURST-1:0] };
end
initial r_max_burst = 0;
always @(posedge i_clk)
if (!r_busy || r_pre_start)
begin
// Force us to align ourself early
// That way we don't need to check for
// alignment (again) later
r_max_burst <= initial_burstlen;
end else if (phantom_start)
begin
// Verilator lint_off WIDTH
if (r_increment || LGMAXBURST <= LGMAX_FIXED_BURST)
begin : LIMIT_BY_LGMAXBURST
r_max_burst <= (1<<LGMAXBURST);
if (!ar_multiple_bursts_remaining
&& ar_next_remaining[LGMAXBURST:0] < (1<<LGMAXBURST))
r_max_burst <= { 1'b0, ar_next_remaining[8:0] };
end else begin : LIMIT_BY_SIXTEEN
r_max_burst <= MAX_FIXED_BURST;
if (!ar_multiple_bursts_remaining
&& ar_next_remaining[LGMAXBURST:0] < MAX_FIXED_BURST)
r_max_burst <= { 1'b0, ar_next_remaining[7:0] };
end
// Verilator lint_on WIDTH
end
// }}}
// Count the number of bursts outstanding--these are the number of
// ARVALIDs that have been accepted, but for which the RVALID && RLAST
// has not (yet) been returned.
// {{{
initial ar_none_outstanding = 1;
initial ar_bursts_outstanding = 0;
always @(posedge i_clk)
if (i_reset)
begin
ar_bursts_outstanding <= 0;
ar_none_outstanding <= 1;
end else case ({ phantom_start,
M_AXI_RVALID && M_AXI_RREADY && M_AXI_RLAST })
2'b01: begin
ar_bursts_outstanding <= ar_bursts_outstanding - 1;
ar_none_outstanding <= (ar_bursts_outstanding == 1);
end