-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsddma_mm2s.v
1706 lines (1572 loc) · 42.4 KB
/
sddma_mm2s.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: rtl/sddma_mm2s.v
// {{{
// Project: SD-Card controller
//
// Purpose: ZipDMA -- Read values from memory
//
// This is the first component of the DMA sequence. It reads values from
// memory, and aligns them with an outgoing data stream.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2025, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
`default_nettype none
`timescale 1ns/1ps
// }}}
module sddma_mm2s #(
// {{{
parameter ADDRESS_WIDTH = 30,
parameter BUS_WIDTH = 64,
parameter LGLENGTH = 10,
parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0,
parameter [0:0] OPT_LOWPOWER = 1'b0,
parameter [0:0] OPT_FIRSTBEAT_TRIM = 1'b0,
// Abbreviations
localparam DW = BUS_WIDTH,
localparam AW = ADDRESS_WIDTH-$clog2(DW/8)
// }}}
) (
// {{{
input wire i_clk, i_reset,
// Configuration
// {{{
input wire i_request,
output reg o_busy, o_err,
input wire i_inc,
// # of transferred byte per beat
input wire [1:0] i_size,
input wire [LGLENGTH:0] i_transferlen,
input wire [ADDRESS_WIDTH-1:0] i_addr, // Byte address
// }}}
// Wishbone master interface
// {{{
output reg o_rd_cyc, o_rd_stb,
// Verilator coverage_off
output wire o_rd_we,
// Verilator coverage_on
output reg [AW-1:0] o_rd_addr,
// Verilator coverage_off
output wire [DW-1:0] o_rd_data,
// Verilator coverage_on
output reg [DW/8-1:0] o_rd_sel,
input wire i_rd_stall,
input wire i_rd_ack,
input wire [DW-1:0] i_rd_data,
input wire i_rd_err,
// }}}
// Outgoing Stream interface
// {{{
output wire M_VALID,
input wire M_READY, // *MUST* be 1
output wire [DW-1:0] M_DATA,
// How many bytes are valid?
output wire [$clog2(DW/8):0] M_BYTES,
output wire M_LAST
// }}}
// }}}
);
// Local declarations
// {{{
// size prefix is # of valid bytes in the beat (one clk cycle)
// difference between _size and _len is that "size" references the
// current beat, whereas _len references the whole transfer. _sel
// references which data byte lanes are valid (like WSTRB from AXI
// interface)
//
localparam [1:0] SZ_BYTE = 2'b11,
SZ_16B = 2'b10,
SZ_32B = 2'b01,
SZ_BUS = 2'b00;
localparam WBLSB = $clog2(DW/8);
reg [WBLSB:0] nxtstb_size, rdstb_size, rdack_size,
first_size, last_size;
reg r_wrap;
reg [ADDRESS_WIDTH:0] next_addr;
reg [ADDRESS_WIDTH-1:0] last_request_addr;
reg [WBLSB-1:0] subaddr, rdack_subaddr;
reg [DW/8-1:0] nxtstb_sel, first_sel, first_sel_no_shift,
base_sel, ibase_sel;
reg [LGLENGTH:0] wb_outstanding;
reg [WBLSB+1:0] fill, next_fill;
reg m_valid, m_last;
reg [DW-1:0] sreg;
reg [WBLSB:0] m_bytes;
reg [LGLENGTH:0] rdstb_len, rdack_len;
reg [WBLSB-1:0] pre_shift;
reg [DW-1:0] pre_shifted_data;
reg r_inc;
reg [1:0] r_size;
reg [LGLENGTH:0] r_transferlen;
reg [ADDRESS_WIDTH-1:0] r_addr;
// }}}
assign o_rd_we = 1'b0;
assign o_rd_data = {(DW){1'b0}};
// Copy the configuration whenever i_request && !o_busy
// {{{
always @(posedge i_clk)
if (!o_busy && (!OPT_LOWPOWER || i_request))
begin
r_inc <= i_inc;
r_size <= i_size;
r_transferlen <= i_transferlen;
r_addr <= i_addr;
end
// }}}
// nxtstb_size
// {{{
generate if (BUS_WIDTH > 32)
begin : GEN_NXTSTB_SIZE
// {{{
always @(*)
begin
first_size = 0;
case(i_size)
SZ_BYTE: first_size = 1;
SZ_16B: first_size = (i_addr[0]) ? 1 : 2;
// Verilator lint_off WIDTH
SZ_32B: first_size = 4 - i_addr[1:0];
SZ_BUS: first_size = (DW/8)-i_addr[WBLSB-1:0];
// Verilator lint_on WIDTH
endcase
if ({{ (LGLENGTH-WBLSB){1'b0}}, first_size } > i_transferlen)
first_size = i_transferlen[WBLSB:0];
end
always @(*)
begin
nxtstb_size = rdstb_size;
last_size = r_addr[WBLSB-1:0]+ r_transferlen[WBLSB-1:0];
case(r_size)
SZ_BYTE: nxtstb_size = 1;
// Verilator lint_off WIDTH
SZ_16B: if (r_transferlen == 2)
nxtstb_size = 2 - r_addr[0];
else if (r_transferlen == 3)
nxtstb_size = r_addr[0] + 1;
else
nxtstb_size = (rdstb_len == 3) ? 1 : 2;
SZ_32B: begin
last_size[WBLSB:2] = 0;
if (r_transferlen < 8)
begin
if (r_transferlen[1:0] + r_addr[1:0] == 0)
nxtstb_size = 4;
else
nxtstb_size = (4 > rdstb_len - rdstb_size) ? last_size : 4;
end else
nxtstb_size = (rdstb_len >= 4 && rdstb_len < 8)
? (rdstb_len - 4) : 4;
end
SZ_BUS: begin
nxtstb_size = (DW/8);
if (DW/8 > rdstb_len - rdstb_size)
nxtstb_size= { 1'b0,rdstb_len[WBLSB:0] }
- { 1'b0, rdstb_size[WBLSB:0]};
end
// Verilator lint_on WIDTH
endcase
end
// }}}
end else begin : STD_NXTSTB_SIZE
// {{{
always @(*)
begin
first_size = 0;
case(i_size)
SZ_BYTE: first_size = 1;
SZ_16B: first_size = (i_addr[0]) ? 1:2;
// Verilator lint_off WIDTH
default:
first_size = (DW/8)-i_addr[WBLSB-1:0];
endcase
if (first_size > i_transferlen)
first_size = i_transferlen;
// Verilator lint_on WIDTH
end
always @(*)
begin
nxtstb_size = rdstb_size;
last_size = r_addr[WBLSB-1:0]+r_transferlen[WBLSB-1:0];
casez(r_size)
SZ_BYTE: nxtstb_size = 1;
// Verilator lint_off WIDTH
SZ_16B: if (r_transferlen == 2)
nxtstb_size = 2 - r_addr[0];
else if (r_transferlen == 3)
nxtstb_size = r_addr[0] + 1;
else
nxtstb_size = (rdstb_len == 3) ? 1 : 2;
default: begin
last_size[WBLSB:2] = 0;
if (r_transferlen < 8)
begin
if (r_transferlen[1:0] + r_addr[1:0] == 0)
nxtstb_size = 4;
else
nxtstb_size = (4 > rdstb_len - rdstb_size) ? last_size : 4;
end else
nxtstb_size = (rdstb_len >= 4
&& rdstb_len < 8)
? (rdstb_len - 4) : 4;
end
// Verilator lint_on WIDTH
endcase
end
// }}}
end endgenerate
// }}}
// next_addr
// {{{
always @(*)
begin
next_addr = { 1'b0, o_rd_addr, subaddr };
if (o_rd_stb && !i_rd_stall && r_inc)
next_addr = next_addr
+ { {(ADDRESS_WIDTH-WBLSB-1){1'b0}}, rdstb_size };
end
// }}}
// o_rd_cyc, o_rd_stb, o_busy, o_err, rdstb_len, rdstb_size
// {{{
initial { o_rd_cyc, o_rd_stb } = 2'b00;
initial { o_busy, o_err } = 2'b00;
always @(posedge i_clk)
if (i_reset)
begin
// {{{
o_rd_cyc <= 1'b0;
o_rd_stb <= 1'b0;
{ o_rd_addr, subaddr } <= 0;
rdstb_size <= 0;
rdstb_len <= 0;
o_busy <= 0;
o_err <= 0;
// }}}
end else if ((o_rd_cyc && i_rd_err) || o_err)
begin
// {{{
o_rd_cyc <= 1'b0;
o_rd_stb <= 1'b0;
{ o_rd_addr, subaddr } <= 0;
rdstb_size <= 0;
rdstb_len <= 0;
o_busy <= o_rd_cyc && i_rd_err;
o_err <= o_rd_cyc && i_rd_err;
// }}}
end else if (!o_busy)
begin
// {{{
o_rd_cyc <= i_request;
o_rd_stb <= i_request;
o_busy <= i_request;
o_err <= 0;
rdstb_size <= 0;
rdstb_len <= 0;
if (!OPT_LOWPOWER || i_request)
begin
{ o_rd_addr, subaddr } <= i_addr;
// rdstb_size
rdstb_size <= first_size;
// rdstb_len
rdstb_len <= i_transferlen;
end
// }}}
end else begin
if (!i_rd_stall)
o_rd_stb <= 1'b0;
if (rdstb_len > { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size })
begin
if (r_wrap || next_addr[ADDRESS_WIDTH])
{ o_err, o_rd_cyc, o_rd_stb } <= 3'b100;
else
o_rd_stb <= 1'b1;
end
if (o_rd_stb && !i_rd_stall)
begin
// {{{
if (rdstb_len <= { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size })
begin
rdstb_len <= 0;
end else begin
rdstb_len <= rdstb_len
- { {(LGLENGTH-WBLSB){1'b0}}, rdstb_size };
end
// rdstb_size
rdstb_size <= nxtstb_size;
{ o_rd_addr, subaddr } <= next_addr[ADDRESS_WIDTH-1:0];
// }}}
end
if (wb_outstanding == (i_rd_ack ? 1:0) && !o_rd_stb)
o_rd_cyc <= 1'b0;
if (m_valid && m_last)
o_busy <= 0;
end
initial r_wrap = 1'b0;
always @(posedge i_clk)
if (i_reset || (o_rd_cyc && i_rd_err) || o_err || !o_busy)
r_wrap <= 1'b0;
else if (o_rd_stb && !i_rd_stall)
r_wrap <= next_addr[ADDRESS_WIDTH];
`ifdef FORMAL
always @(*)
if (o_busy && m_valid && m_last)
begin
assert(rdack_len == 0);
assert(fill == m_bytes);
end
`endif
// }}}
// o_rd_sel
// {{{
// ibase_sel
generate if (BUS_WIDTH > 32)
begin : GEN_STRB
// {{{
always @(*)
begin
ibase_sel = 0;
if (OPT_LITTLE_ENDIAN)
begin
// {{{
// Verilator coverage_off
case(i_size)
SZ_BYTE: ibase_sel = {{(DW/8-1){1'b0}}, 1'b1} << i_addr[WBLSB-1:0];
SZ_16B: ibase_sel = {{(DW/8-2){1'b0}}, 2'h3} << {i_addr[WBLSB-1:1], 1'b0 };
SZ_32B: ibase_sel = {{(DW/8-4){1'b0}}, 4'b1111} << {i_addr[WBLSB-1:2], 2'b0 };
SZ_BUS: ibase_sel = {(DW/8){1'b1}};
endcase
// Verilator coverage_on
// }}}
end else begin
// {{{
case(i_size)
SZ_BYTE: ibase_sel = {1'h1, {(DW/8-1){1'b0}} } >> i_addr[WBLSB-1:0];
SZ_16B: ibase_sel = {2'h3, {(DW/8-2){1'b0}} } >> {i_addr[WBLSB-1:1], 1'b0 };
SZ_32B: ibase_sel = {4'hf, {(DW/8-4){1'b0}} } >> {i_addr[WBLSB-1:2], 2'b0 };
SZ_BUS: ibase_sel = {(DW/8){1'b1}};
endcase
// }}}
end
end
// }}}
end else begin : MIN_STRB
// {{{
always @(*)
if (OPT_LITTLE_ENDIAN)
begin
// {{{
// Verilator coverage_off
case(i_size)
SZ_BYTE: ibase_sel = {{(DW/8-1){1'b0}}, 1'b1} << i_addr[WBLSB-1:0];
SZ_16B: ibase_sel = {{(DW/8-2){1'b0}}, 2'h3} << {i_addr[WBLSB-1:1], 1'b0 };
default: ibase_sel = {(DW/8){1'b1}};
endcase
// Verilator coverage_on
// }}}
end else begin
// {{{
case(i_size)
SZ_BYTE: ibase_sel= {1'h1, {(DW/8-1){1'b0}} } >> i_addr[WBLSB-1:0];
SZ_16B: ibase_sel = {2'h3, {(DW/8-2){1'b0}} } >> {i_addr[WBLSB-1:1], 1'b0 };
default: ibase_sel = {(DW/8){1'b1}};
endcase
// }}}
end
// }}}
end endgenerate
always @(posedge i_clk)
if (i_reset || (o_rd_cyc && i_rd_err))
begin
base_sel <= 0;
end else if (!o_busy)
begin
base_sel <= 0;
if (i_request || !OPT_LOWPOWER)
base_sel <= ibase_sel;
end else if (o_rd_stb && !i_rd_stall)
base_sel <= nxtstb_sel;
// nxtstb_sel
// {{{
generate if (DW == 32)
begin : GEN_NXTSTB_SEL
// {{{
always @(*)
if (OPT_LITTLE_ENDIAN)
begin
// Verilator coverage_off
case(r_size)
SZ_BYTE: nxtstb_sel = { base_sel[DW/8-2:0], base_sel[DW/8-1] };
SZ_16B: nxtstb_sel = { base_sel[DW/8-3:0], base_sel[DW/8-1:DW/8-2] };
default: nxtstb_sel = {(DW/8){1'b1}};
endcase
if (!r_inc)
nxtstb_sel = base_sel;
// Verilator coverage_on
end else begin
case(r_size)
SZ_BYTE: nxtstb_sel = { base_sel[0:0], base_sel[DW/8-1:1] };
SZ_16B: nxtstb_sel = { base_sel[1:0], base_sel[DW/8-1:2] };
default:
nxtstb_sel = {(DW/8){1'b1}};
endcase
if (!r_inc)
nxtstb_sel = base_sel;
end
// }}}
end else begin : GEN_WIDE_NXTSTB_SEL
always @(*)
if (OPT_LITTLE_ENDIAN)
begin
// Verilator coverage_off
case(r_size)
SZ_BYTE: nxtstb_sel = { base_sel[DW/8-2:0], base_sel[DW/8-1] };
SZ_16B: nxtstb_sel = { base_sel[DW/8-3:0], base_sel[DW/8-1:DW/8-2] };
SZ_32B: nxtstb_sel = { base_sel[DW/8-5:0], base_sel[DW/8-1:DW/8-4] };
SZ_BUS: nxtstb_sel = {(DW/8){1'b1}};
endcase
if (!r_inc)
nxtstb_sel = base_sel;
// Verilator coverage_on
end else begin
case(r_size)
SZ_BYTE: nxtstb_sel = { base_sel[0:0], base_sel[DW/8-1:1] };
SZ_16B: nxtstb_sel = { base_sel[1:0], base_sel[DW/8-1:2] };
SZ_32B: nxtstb_sel = { base_sel[3:0], base_sel[DW/8-1:4] };
SZ_BUS: nxtstb_sel = {(DW/8){1'b1}};
endcase
if (!r_inc)
nxtstb_sel = base_sel;
end
end endgenerate
// }}}
// first_sel
generate if (BUS_WIDTH > 32)
begin : GEN_FIRST_SEL
// {{{
always @(*)
begin
first_sel_no_shift = 0;
first_sel = 0;
// Verilator lint_off WIDTH
if (!OPT_FIRSTBEAT_TRIM || i_transferlen >= DW/8)
first_sel_no_shift = -1;
else if (OPT_LITTLE_ENDIAN)
first_sel_no_shift = (1 << i_transferlen) - 1;
else
first_sel_no_shift = ({(DW/8){1'b1}} << (DW/8 - i_transferlen));
// Verilator lint_on WIDTH
if (OPT_LITTLE_ENDIAN)
begin
// {{{
// Verilator coverage_off
case(i_size)
SZ_BYTE: first_sel = {{(DW/8-1){1'b0}}, 1'b1} << i_addr[WBLSB-1:0];
SZ_16B: begin
first_sel_no_shift = first_sel_no_shift << i_addr[0];
first_sel_no_shift[DW/8-1:2] = 0;
first_sel = first_sel_no_shift << {i_addr[WBLSB-1:1], 1'b0 };
end
SZ_32B: begin
first_sel_no_shift = first_sel_no_shift << i_addr[1:0];
first_sel_no_shift[DW/8-1:4] = 0;
first_sel = first_sel_no_shift << {i_addr[WBLSB-1:2], 2'b00 };
end
SZ_BUS: first_sel = first_sel_no_shift << i_addr[WBLSB-1:0];
endcase
// Verilator coverage_on
// }}}
end else begin
// {{{
case(i_size)
SZ_BYTE: first_sel = {1'b1, {(DW/8-1){1'b0}} } >> i_addr[WBLSB-1:0];
SZ_16B: begin
first_sel_no_shift = first_sel_no_shift >> i_addr[0];
first_sel_no_shift[DW/8-3:0] = 0;
first_sel = first_sel_no_shift >> {i_addr[WBLSB-1:1], 1'b0 };
end
SZ_32B: begin
first_sel_no_shift = first_sel_no_shift >> i_addr[1:0];
first_sel_no_shift[DW/8-5:0] = 0;
first_sel = first_sel_no_shift >> {i_addr[WBLSB-1:2], 2'b00 };
end
SZ_BUS: first_sel = first_sel_no_shift >> i_addr[WBLSB-1:0];
endcase
// }}}
end
end
// }}}
end else begin : MIN_FIRST_SEL
// {{{
always @(*)
begin
first_sel_no_shift = 0;
first_sel = 0;
if (!OPT_FIRSTBEAT_TRIM || i_transferlen >= DW/8)
first_sel_no_shift = -1;
else if (OPT_LITTLE_ENDIAN)
first_sel_no_shift = (1 << i_transferlen) - 1;
else
first_sel_no_shift = ({(DW/8){1'b1}} << (DW/8 - i_transferlen));
if (OPT_LITTLE_ENDIAN)
begin
// {{{
// Verilator coverage_off
case(i_size)
SZ_BYTE: first_sel = {{(DW/8-1){1'b0}}, 1'b1} << i_addr[WBLSB-1:0];
SZ_16B: begin
first_sel_no_shift = first_sel_no_shift << i_addr[0];
first_sel_no_shift[DW/8-1:2] = 0;
first_sel = first_sel_no_shift << {i_addr[WBLSB-1:1], 1'b0 };
end
default: first_sel = first_sel_no_shift << i_addr[WBLSB-1:0];
endcase
// Verilator coverage_on
// }}}
end else begin
// {{{
case(i_size)
SZ_BYTE: first_sel = {1'b1, {(DW/8-1){1'b0}} } >> i_addr[WBLSB-1:0];
SZ_16B: begin
first_sel_no_shift = first_sel_no_shift >> i_addr[0];
first_sel_no_shift[DW/8-3:0] = 0;
first_sel = first_sel_no_shift >> {i_addr[WBLSB-1:1], 1'b0 };
end
default: first_sel = first_sel_no_shift >> i_addr[WBLSB-1:0];
endcase
// }}}
end
end
// }}}
end endgenerate
// o_rd_sel
always @(posedge i_clk)
if (i_reset || (o_rd_cyc && i_rd_err))
begin
o_rd_sel <= 0;
end else if (!o_busy)
begin
// {{{
o_rd_sel <= {(DW/8){1'b0}};
if (!OPT_LOWPOWER || i_request)
o_rd_sel <= first_sel;
// }}}
end else if (o_rd_stb && !i_rd_stall)
o_rd_sel <= nxtstb_sel;
// }}}
// wb_outstanding
// {{{
initial wb_outstanding = 0;
always @(posedge i_clk)
if (i_reset || !o_rd_cyc || i_rd_err)
wb_outstanding <= 0;
// wb_pipeline_full <= 1'b0;
else case({ (o_rd_stb && !i_rd_stall), i_rd_ack })
2'b10: wb_outstanding <= wb_outstanding + 1;
2'b01: wb_outstanding <= wb_outstanding - 1;
default: begin end
endcase
// }}}
// rdack_subaddr
// {{{
always @(posedge i_clk)
if (!o_busy)
begin
if (!OPT_LOWPOWER || i_request)
rdack_subaddr <= i_addr[WBLSB-1:0];
end else if (i_rd_ack)
begin
// Verilator lint_off WIDTH
if (r_inc)
rdack_subaddr <= rdack_subaddr + rdack_size;
else case(r_size)
SZ_BYTE: begin end
SZ_16B: rdack_subaddr[ 0] <= 1'b0;
SZ_32B: rdack_subaddr[1:0] <= 2'b0;
SZ_BUS: rdack_subaddr[WBLSB-1:0] <= {(WBLSB){1'b0}};
endcase
// Verilator lint_on WIDTH
end
// }}}
// rdack_len
// {{{
// Total length remaining, from the perspective of the bus return.
// Hence, on any bus return, we drop by the number of bytes valid
// in that return, or minus rdack_size.
always @(posedge i_clk)
if (!o_busy)
begin
if (!OPT_LOWPOWER || i_request)
rdack_len <= i_transferlen;
end else if (i_rd_ack)
begin
rdack_len <= rdack_len-{ {(LGLENGTH-WBLSB){1'b0}}, rdack_size };
if (rdack_len <= { {(LGLENGTH-WBLSB){1'b0}}, rdack_size })
rdack_len <= 0;
end
// }}}
// rdack_size
// {{{
always @(posedge i_clk)
if (!o_busy)
begin
if (!OPT_LOWPOWER || i_request)
rdack_size <= first_size;
end else if (i_rd_ack)
begin
case(r_size)
SZ_BYTE:rdack_size <= 1;
// Verilator lint_off WIDTH
SZ_16B: if (rdack_len > 2 + rdack_size)
rdack_size <= 2;
else
rdack_size <= rdack_len - rdack_size;
SZ_32B: if (rdack_len > 4 + rdack_size)
rdack_size <= 4;
else
rdack_size <= rdack_len - rdack_size;
SZ_BUS: if (rdack_len > DW/8 + rdack_size)
rdack_size <= DW/8;
else
rdack_size <= rdack_len - rdack_size;
// Verilator lint_on WIDTH
endcase
end
// }}}
// fill, next_fill (depends on rdack_size)
// {{{
always @(*)
begin
next_fill = (M_VALID) ? 0 : fill;
if (i_rd_ack)
next_fill = next_fill + { 1'b0, rdack_size };
end
always @(posedge i_clk)
if (!o_busy)
fill <= 0;
else
fill <= next_fill;
// }}}
// m_valid
// {{{
initial m_valid = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
m_valid <= 1'b0;
else begin
m_valid <= 0;
if ((!m_valid || !m_last) && rdack_len == 0 && fill > 0)
m_valid <= 1;
else if (o_rd_cyc && i_rd_ack)
m_valid <= 1'b1;
end
// }}}
// sreg
// {{{
initial pre_shift = 0;
always @(posedge i_clk)
if (!o_busy)
begin
pre_shift <= 0;
if (!OPT_LOWPOWER || i_request)
pre_shift <= i_addr[WBLSB-1:0];
end else if (o_rd_cyc && i_rd_ack)
begin
case(r_size)
SZ_BYTE: pre_shift <= pre_shift + (r_inc ? 1 : 0);
SZ_16B: begin
// {{{
pre_shift <= pre_shift + (r_inc ? 2 : 0);
pre_shift[0] <= 1'b0;
end
// }}}
SZ_32B: begin
// {{{
// Verilator lint_off WIDTH
pre_shift <= pre_shift + (r_inc ? 4 : 0);
// Verilator lint_on WIDTH
pre_shift[1:0] <= 2'b0;
end
// }}}
SZ_BUS: pre_shift <= 0;
endcase
end
always @(*)
if (OPT_LITTLE_ENDIAN)
pre_shifted_data = i_rd_data >> (8*pre_shift);
else
pre_shifted_data = i_rd_data << (8*pre_shift);
initial sreg = 0;
always @(posedge i_clk)
if (!o_busy)
sreg <= 0;
else if (o_rd_cyc && i_rd_ack)
begin
// {{{
// Verilator lint_off WIDTH
sreg <= pre_shifted_data;
// Verilator lint_on WIDTH
// }}}
end else if (m_valid)
begin
// {{{
sreg <= {(DW){1'b0}};
// }}}
end
// }}}
// m_bytes
// {{{
initial m_bytes = 0;
always @(posedge i_clk)
if (!o_busy)
begin
m_bytes <= 0;
end else if (i_rd_ack)
begin
if (|next_fill[WBLSB+1:WBLSB]) // if next_fill >= DW/8)
// Verilator lint_off WIDTH
m_bytes <= DW/8;
// Verilator lint_on WIDTH
else
m_bytes <= { 1'b0, next_fill[WBLSB-1:0] };
end else if (rdack_len == 0)
m_bytes <= next_fill[WBLSB:0];
// }}}
// m_last
// {{{
always @(*)
begin
last_request_addr = i_addr;
if (r_inc)
// Verilator lint_off WIDTH
last_request_addr = i_addr + i_transferlen - 1;
// Verilator lint_on WIDTH
end
initial m_last = 0;
always @(posedge i_clk)
if (i_reset)
m_last <= 1'b0;
else if (!o_busy)
begin
m_last <= 1'b0;
if (!OPT_LOWPOWER || i_request)
case(i_size)
SZ_BYTE: m_last <= (i_transferlen <= 1);
SZ_16B: m_last <= (last_request_addr[ADDRESS_WIDTH-1:1] != i_addr[ADDRESS_WIDTH-1:1]);
SZ_32B: m_last <= (last_request_addr[ADDRESS_WIDTH-1:2] != i_addr[ADDRESS_WIDTH-1:2]);
SZ_BUS: m_last <= (last_request_addr[ADDRESS_WIDTH-1:WBLSB] != i_addr[ADDRESS_WIDTH-1:WBLSB]);
endcase
end else if (i_rd_ack)
begin
// Verilator lint_off WIDTH
m_last <= (rdack_len <= rdack_size) && (next_fill <= DW/8);
// Verilator lint_on WIDTH
end else if (rdack_len == 0)
m_last <= 1;
// }}}
assign M_VALID = m_valid;
assign M_DATA = sreg;
assign M_BYTES= m_bytes;
assign M_LAST = m_last;
// Keep Verilator happy
// {{{
// Verilator coverage_off
// Verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, M_READY, last_request_addr[0],
r_addr[ADDRESS_WIDTH-1:WBLSB] };
// Verilator lint_on UNUSED
// Verilator coverage_on
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
// localparam [0:0] CONTRACT = 1'b1;
localparam F_LGDEPTH = LGLENGTH+1-WBLSB;
localparam F_LGCOUNT = LGLENGTH+1;
reg f_past_valid;
wire [F_LGDEPTH-1:0] fwb_nreqs, fwb_nacks, fwb_outstanding;
(* anyconst *) reg f_cfg_inc;
(* anyconst *) reg [1:0] f_cfg_size;
(* anyconst *) reg [ADDRESS_WIDTH-1:0] f_cfg_addr;
(* anyconst *) reg [LGLENGTH:0] f_cfg_len;
reg [DW/8-1:0] f_base_sel;
reg [F_LGCOUNT-1:0] f_rcvd, f_sent;
reg [WBLSB:0] f_ack_size, f_stb_size;
reg [F_LGCOUNT-1:0] f_outstanding_bytes;
reg f_stb_first, f_stb_last,
f_ack_first, f_ack_last;
(* keep *) reg [WBLSB-1:0] f_excess_last_return, lower_len_bits;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
always @(*)
if (!f_past_valid)
assume(i_reset);
////////////////////////////////////////////////////////////////////////
//
// Configuration properties
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// Handshake property
always @(posedge i_clk)
if (!f_past_valid || $past(i_reset) || $past(o_err))
begin
assume(!i_request);
end else if ($past(o_busy || i_request))
begin
assume(i_request);
assume($stable(i_inc));
assume($stable(i_size));
assume($stable(i_addr));
assume($stable(i_transferlen));
end
// Assume the DMA request is for an arbitrary value we can track
always @(*)
begin
assume(f_cfg_len > 0);
if (i_request && !o_busy)
begin
assume(i_inc == f_cfg_inc);
assume(i_size == f_cfg_size);
assume(i_addr == f_cfg_addr);
assume(i_transferlen == f_cfg_len);
end else if (o_busy)
begin
assert(r_inc == f_cfg_inc);
assert(r_size == f_cfg_size);
assert(r_addr == f_cfg_addr);
assert(r_transferlen == f_cfg_len);
end
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// f_stb_first, f_stb_last, f_ack_first, f_ack_last
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
f_stb_first = (rdstb_len == f_cfg_len);
always @(*)
if (rdstb_len == 0)
f_stb_last <= 1'b0;
else case(r_size)
SZ_BYTE: f_stb_last <= (rdstb_len == 1);
SZ_16B: f_stb_last <= (rdstb_len + f_excess_last_return[0] == 2);
SZ_32B: f_stb_last <= (rdstb_len + f_excess_last_return[1:0] == 4);
SZ_BUS: f_stb_last <= (rdstb_len + f_excess_last_return[WBLSB-1:0] == DW/8);
endcase
always @(*)
f_ack_first = (f_rcvd == 0);
always @(*)
if (rdack_len == 0)
f_ack_last <= 1'b0;
else case(r_size)
SZ_BYTE: f_ack_last <= (rdack_len == 1);
SZ_16B: f_ack_last <= (rdack_len + f_excess_last_return[0] == 2);
SZ_32B: f_ack_last <= (rdack_len + f_excess_last_return[1:0] == 4);
SZ_BUS: f_ack_last <= (rdack_len + f_excess_last_return[WBLSB-1:0] == DW/8);
endcase
// }}}
////////////////////////////////////////////////////////////////////////
//
// f_excess_last_return
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
begin
f_excess_last_return = f_cfg_addr + f_cfg_len;
case(r_size)
SZ_BYTE: f_excess_last_return = 0;
SZ_16B: begin
f_excess_last_return = 2 - f_excess_last_return[0];
f_excess_last_return[WBLSB-1:1] = 0;
end
SZ_32B: begin
f_excess_last_return = 4 - f_excess_last_return[1:0];
f_excess_last_return[WBLSB-1:2] = 0;
end
SZ_BUS: f_excess_last_return = (DW/8) - f_excess_last_return[WBLSB-1:0];
endcase
end