-
Notifications
You must be signed in to change notification settings - Fork 35
/
sdtxframe.v
1940 lines (1786 loc) · 46.5 KB
/
sdtxframe.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/sdtxframe.v
// {{{
// Project: SD-Card controller
//
// Purpose: Given a frame of data from memory, formats it for sending to
// the front end.
//
// Comments:
// This implementation really needs a means of stopping the SD clock if
// the incoming data isn't yet ready. Although the command controller
// doesn't (yet) use such a feature, it could be quite valuable in the
// future when working with a stream-based DMA. (Perhaps it needs to be
// made as a parameter?)
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2024, 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
//
////////////////////////////////////////////////////////////////////////////////
//
`timescale 1ns/1ps
`default_nettype none
// }}}
module sdtxframe #(
// {{{
parameter NCRC = 16,
// OPT_SERDES=1 delays the invocation of tristate by a clock
// cycle. This is in an attempt to match Xilinx's 8x SERDES.
parameter [0:0] OPT_SERDES = 1'b0,
parameter [0:0] OPT_CRCTOKEN = 1'b0,
parameter [NCRC-1:0] CRC_POLYNOMIAL = 16'h1021
// }}}
) (
// {{{
input wire i_clk, i_reset,
//
input wire [7:0] i_cfg_spd,
input wire [1:0] i_cfg_width,
input wire i_cfg_ddr,
input wire i_cfg_pp,
input wire i_cfg_expect_ack,
//
input wire i_cfg_clk90,
input wire [7:0] i_ckwide,
//
input wire i_en, i_ckstb, i_hlfck,
//
input wire S_VALID,
output wire S_READY,
input wire [31:0] S_DATA,
input wire S_LAST,
//
output wire tx_valid,
// input wire tx_ready,
output wire [31:0] tx_data,
output wire tx_tristate,
//
input wire i_crcack,
input wire i_crcnak,
output wire o_done,
output wire o_err,
output wire o_ercode
// }}}
);
// Local declarations
// {{{
localparam [1:0] P_IDLE = 2'b00,
P_DATA = 2'b01,
P_CRC = 2'b10,
P_LAST = 2'b11;
localparam [1:0] WIDTH_1W = 2'b00,
WIDTH_4W = 2'b01,
WIDTH_8W = 2'b10;
localparam [1:0] P_1D = 2'b00,
P_2D = 2'b01,
P_4D = 2'b10;
reg cfg_ddr, cfg_pp;
reg [1:0] cfg_width, cfg_period;
wire start_packet;
reg pre_valid;
reg [1:0] pstate;
wire pre_ready;
reg [31:0] pre_data;
reg [3:0] pre_count;
integer ik, jk;
reg [NCRC- 1:0] crc_1w_reg;
reg [NCRC* 2-1:0] di_crc_2w, nxt_crc_2w, new_crc_2w, crc_2w_reg;
reg [NCRC* 4-1:0] di_crc_4w, nxt_crc_4w, new_crc_4w, crc_4w_reg;
reg [NCRC* 8-1:0] di_crc_8w, nxt_crc_8w, new_crc_8w, crc_8w_reg;
reg [NCRC*16-1:0] di_crc_8d, nxt_crc_8d, new_crc_8d, crc_8d_reg;
reg ck_valid, ck_tristate, ck_stop_bit;
reg r_tristate;
reg [4:0] ck_counts;
reg [31:0] ck_data, ck_sreg;
reg r_done;
reg [3:0] r_timeout;
// }}}
// Steps: #1, Packetizer: breaks incoming signal into wires
// #2, add CRC
// #3, split across clocks
//
////////////////////////////////////////////////////////////////////////
//
// Configuration
// {{{
initial cfg_period = 2'b00;
always @(posedge i_clk)
if (i_reset || !OPT_SERDES)
cfg_period <= P_1D;
else if (pstate == P_IDLE)
begin
if (i_cfg_ddr && i_cfg_spd == 0)
cfg_period <= P_4D; // Four data periods / clk
else if ((i_cfg_ddr && i_cfg_spd == 1)
||(!i_cfg_ddr && i_cfg_spd == 0))
cfg_period <= P_2D; // Two data periods / clk
else
cfg_period <= P_1D; // One data period / clk
end
always @(posedge i_clk)
if (i_reset)
cfg_width <= WIDTH_1W;
else if (pstate == P_IDLE)
cfg_width <= i_cfg_width;
always @(posedge i_clk)
if (i_reset)
cfg_pp <= 1'b0;
else if (pstate == P_IDLE)
cfg_pp <= i_cfg_pp;
always @(posedge i_clk)
if (i_reset)
cfg_ddr <= 1'b0;
else if (pstate == P_IDLE)
cfg_ddr <= i_cfg_ddr;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Packetizer: Add CRC(s) to the packet
// {{{
assign start_packet = i_en && S_VALID && i_ckstb && !tx_valid && !ck_valid;
initial pstate = P_IDLE;
initial pre_valid = 1'b0;
initial pre_data = {(32){1'b1}};
always @(posedge i_clk)
if (i_reset)
begin
// {{{
pstate <= P_IDLE;
pre_valid <= 1'b0;
pre_data <= {(32){1'b1}};
// }}}
end else case(pstate)
P_IDLE: begin
// {{{
pstate <= P_IDLE;
pre_valid <= 0;
pre_data <= (S_VALID) ? S_DATA : {(32){1'b1}};
if (start_packet)
begin
pstate <= (S_LAST) ? P_CRC : P_DATA;
pre_valid <= 1;
end end
// }}}
P_DATA: if (S_VALID && S_READY)
// {{{
begin
pstate <= P_DATA;
pre_valid <= 1;
pre_data <= S_DATA;
if (S_LAST)
pstate <= P_CRC;
end
// }}}
P_CRC: if (pre_ready)
// {{{
begin
pre_valid <= 1'b1;
if (pre_count == 0)
pstate <= P_LAST;
case(cfg_width)
WIDTH_1W: if (cfg_ddr)
pre_data <= crc_2w_reg[NCRC*2-1:0];
else
pre_data <= { crc_1w_reg[NCRC-1:0], 16'hffff };
WIDTH_4W: if (cfg_ddr)
pre_data <= crc_8w_reg[8*NCRC-1:8*NCRC-32];
else
pre_data <= crc_4w_reg[4*NCRC-1:4*NCRC-32];
WIDTH_8W: if (cfg_ddr)
pre_data <= crc_8d_reg[16*NCRC-1:16*NCRC-32];
else
pre_data <= crc_8w_reg[8*NCRC-1:8*NCRC-32];
default: pre_data <= crc_8w_reg[8*NCRC-1:8*NCRC-32];
endcase
end
// }}}
P_LAST: begin
if (pre_ready)
begin
pre_valid <= 0;
pre_data <= {(32){1'b1}};
end
if (!tx_valid)
pstate <= P_IDLE;
end
endcase
`ifdef FORMAL
always @(*)
if (!i_reset && !pre_valid && pstate != P_IDLE)
assert(&pre_data);
`endif
initial pre_count = 0;
always @(posedge i_clk)
if (i_reset)
pre_count <= 0;
else if (pstate == P_DATA || pstate == P_IDLE)
begin
// pre_count =
// (SDR) 16bits / wire / 32
// (DDR) 32bits / wire / 32
case(cfg_width)
WIDTH_1W: pre_count <= 0;
WIDTH_4W: pre_count <= (cfg_ddr) ? 3 : 1;
default: // WIDTH_8W
pre_count <= (cfg_ddr) ? 7:3;
endcase
end else if (pre_ready && pre_count != 0)
pre_count <= pre_count - 1;
assign S_READY = pre_ready && (pstate == P_DATA || pstate == P_IDLE);
// }}}
////////////////////////////////////////////////////////////////////////
//
// CRC calculation
// {{{
// di_crc*, new_crc*, next_crc*: Advance CRC calculations combinatorialy
// {{{
always @(*)
begin
// Notes:
// {{{
// Why the following monstrosity? So we can read data out
// directly into the shift register(s). However, the shift
// register(s) are not sorted by rail, hence all the work
// below to first sort them by rail, adjust the CRC, then
// sort them back, etc.
//
// The good news is that, inspite of all the tortured pain
// below, it's primarily just wire assignments and
// rearrangements. The logic itself is either a shift, or
// (for 1-4 bits per CRC per step), a XOR reduction of up
// to 20 incoming bits.
// }}}
// Pre-load the CRC fills
// {{{
for(ik=0; ik<NCRC; ik=ik+1)
begin
for(jk=0; jk<2; jk=jk+1)
di_crc_2w[jk*NCRC+ik] = crc_2w_reg[ik*2+jk];
for(jk=0; jk<4; jk=jk+1)
di_crc_4w[jk*NCRC+ik] = crc_4w_reg[ik*4+jk];
for(jk=0; jk<8; jk=jk+1)
di_crc_8w[jk*NCRC+ik] = crc_8w_reg[ik*8+jk];
for(jk=0; jk<16; jk=jk+1)
di_crc_8d[jk*NCRC+ik] = crc_8d_reg[ik*16+jk];
end
// }}}
// Advance the CRCs based on S_DATA
// {{{
for(ik=0; ik<2; ik=ik+1)
begin
new_crc_2w[ik*NCRC +: NCRC] =
APPLYCRC16(di_crc_2w[ik*NCRC +: NCRC],
{ S_DATA[30+ik],S_DATA[28+ik],
S_DATA[26+ik],S_DATA[24+ik],
S_DATA[22+ik],S_DATA[20+ik],
S_DATA[18+ik],S_DATA[16+ik],
S_DATA[14+ik],S_DATA[12+ik],
S_DATA[10+ik],S_DATA[ 8+ik],
S_DATA[ 6+ik],S_DATA[ 4+ik],
S_DATA[ 2+ik],S_DATA[ ik] });
end
for(ik=0; ik<4; ik=ik+1)
begin
new_crc_4w[ik*NCRC +: NCRC] =
APPLYCRC8(di_crc_4w[ik*NCRC +: NCRC],
{ S_DATA[28+ik],S_DATA[24+ik],
S_DATA[20+ik],S_DATA[16+ik],
S_DATA[12+ik],S_DATA[ 8+ik],
S_DATA[ 4+ik],S_DATA[ ik] });
end
for(ik=0; ik<8; ik=ik+1)
begin
new_crc_8w[ik*NCRC +: NCRC] =
APPLYCRC4(di_crc_8w[ik*NCRC +: NCRC],
{ S_DATA[24+ik], S_DATA[16+ik],
S_DATA[8+ik], S_DATA[ik] });
end
for(ik=0; ik<16; ik=ik+1)
begin
new_crc_8d[ik*NCRC +: NCRC] =
APPLYCRC2(di_crc_8d[ik*NCRC +: NCRC],
{ S_DATA[16+ik], S_DATA[ik] });
end
// }}}
// Order the bits to place the results back in line
// {{{
for(ik=0; ik<NCRC; ik=ik+1)
begin
for(jk=0; jk<2; jk=jk+1)
nxt_crc_2w[ik*2+jk] = new_crc_2w[jk*NCRC+ik];
for(jk=0; jk<4; jk=jk+1)
nxt_crc_4w[ik*4+jk] = new_crc_4w[jk*NCRC+ik];
for(jk=0; jk<8; jk=jk+1)
nxt_crc_8w[ik*8+jk] = new_crc_8w[jk*NCRC+ik];
for(jk=0; jk<16; jk=jk+1)
nxt_crc_8d[ik*16+jk] = new_crc_8d[jk*NCRC+ik];
end
// }}}
end
// }}}
// crc_*_reg: Advance CRC calculations sequentially w/ new data
// {{{
always @(posedge i_clk)
if (i_reset || (!i_en && !tx_valid))
begin
crc_1w_reg <= 0;
crc_2w_reg <= 0;
crc_4w_reg <= 0;
crc_8w_reg <= 0;
crc_8d_reg <= 0;
end else if (S_VALID && S_READY)
begin
crc_1w_reg <= {(NCRC ){1'b1}};
crc_2w_reg <= {(NCRC* 2){1'b1}};
crc_4w_reg <= {(NCRC* 4){1'b1}};
crc_8w_reg <= {(NCRC* 8){1'b1}};
crc_8d_reg <= {(NCRC*16){1'b1}};
case(cfg_width)
WIDTH_1W: if (cfg_ddr)
crc_2w_reg <= nxt_crc_2w;
else
crc_1w_reg <= APPLYCRC32(crc_1w_reg, S_DATA);
WIDTH_4W: if (cfg_ddr)
crc_8w_reg <= nxt_crc_8w;
else
crc_4w_reg <= nxt_crc_4w;
WIDTH_8W: if (cfg_ddr)
crc_8d_reg <= nxt_crc_8d;
else
crc_8w_reg <= nxt_crc_8w;
default: begin end
endcase
end else if (i_ckstb && pstate != P_IDLE && pstate != P_DATA && ck_counts == 0)
begin
crc_1w_reg <= {(NCRC){1'b1}};
crc_2w_reg <= {(2*NCRC){1'b1}};
crc_4w_reg <= { crc_4w_reg[ 4*NCRC-32-1:0], 32'hffff_ffff };
crc_8w_reg <= { crc_8w_reg[ 8*NCRC-32-1:0], 32'hffff_ffff };
crc_8d_reg <= { crc_8d_reg[16*NCRC-32-1:0], 32'hffff_ffff };
end
// }}}
function automatic [NCRC-1:0] STEPCRC(input [NCRC-1:0] prior,
// {{{
input i_bit);
begin
if (prior[NCRC-1] ^ i_bit)
STEPCRC = { prior[NCRC-2:0], 1'b0 } ^ CRC_POLYNOMIAL;
else
STEPCRC = { prior[NCRC-2:0], 1'b0 };
end endfunction
// }}}
function automatic [NCRC-1:0] APPLYCRC32(input [NCRC-1:0] prior,
// {{{
input [31:0] i_crc_data);
integer ck;
reg [NCRC-1:0] fill;
begin
fill = prior;
for(ck=0; ck<32; ck=ck+1)
fill = STEPCRC(fill, i_crc_data[31-ck]);
APPLYCRC32 = fill;
end endfunction
// }}}
function automatic [NCRC-1:0] APPLYCRC16(input [NCRC-1:0] prior,
// {{{
input [15:0] i_crc_data);
integer ck;
reg [NCRC-1:0] fill;
begin
fill = prior;
for(ck=0; ck<16; ck=ck+1)
fill = STEPCRC(fill, i_crc_data[15-ck]);
APPLYCRC16 = fill;
end endfunction
// }}}
function automatic [NCRC-1:0] APPLYCRC8(input [NCRC-1:0] prior,
// {{{
input [7:0] i_crc_data);
integer ck;
reg [NCRC-1:0] fill;
begin
fill = prior;
for(ck=0; ck<8; ck=ck+1)
fill = STEPCRC(fill, i_crc_data[7-ck]);
APPLYCRC8 = fill;
end endfunction
// }}}
function automatic [NCRC-1:0] APPLYCRC4(input [NCRC-1:0] prior,
// {{{
input [3:0] i_crc_data);
integer ck;
reg [NCRC-1:0] fill;
begin
fill = prior;
for(ck=0; ck<4; ck=ck+1)
fill = STEPCRC(fill, i_crc_data[3-ck]);
APPLYCRC4 = fill;
end endfunction
// }}}
function automatic [NCRC-1:0] APPLYCRC2(input [NCRC-1:0] prior,
// {{{
input [1:0] i_crc_data);
integer ck;
reg [NCRC-1:0] fill;
begin
fill = prior;
for(ck=0; ck<2; ck=ck+1)
fill = STEPCRC(fill, i_crc_data[1-ck]);
APPLYCRC2 = fill;
end endfunction
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Clock divider, data shift register
// {{{
// ck_valid
// {{{
initial ck_valid = 0;
initial ck_stop_bit = 0;
always @(posedge i_clk)
if (i_reset)
begin
ck_valid <= 0;
ck_stop_bit <= 1'b0;
end else if (start_packet)
begin
// Start us one clock cycle early with a single start bit
ck_valid <= 1;
ck_stop_bit <= 1'b1; // Need a stop bit
end else if (i_ckstb && ck_counts == 0)
begin
ck_valid <= pre_valid || ck_stop_bit;
ck_stop_bit <= pre_valid;
end
`ifdef FORMAL
always @(*)
if (!i_reset && (ck_stop_bit || pre_valid))
assert(ck_valid);
always @(*)
if (!i_reset && ck_valid && pre_valid)
assert(ck_stop_bit);
always @(*)
if (!i_reset && !ck_stop_bit && !pre_valid)
assert(!ck_valid || (&tx_data));
`endif
// }}}
initial ck_counts = 0;
initial ck_sreg = -1;
initial ck_data = -1;
always @(posedge i_clk)
if (i_reset) // pstate == P_IDLE)
begin
// {{{
ck_sreg <= {(32){1'b1}};
ck_data <= {(32){1'b1}};
ck_counts<= 0;
// }}}
end else if (i_ckstb && pre_valid && ck_counts == 0) // && tx_ready
begin // Load the shift registers
case(cfg_period)
P_1D: begin // One clock period of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { pre_data[30: 0], 1'b1 };
ck_data <= { (4){7'h7f, pre_data[31] } };
// In the case of the 1b CRC, we only need
// 16 clock cycles, not 32.
ck_counts <= (!cfg_ddr && pstate == P_LAST) ? 15: 31;
end
WIDTH_4W: begin
ck_sreg <= { pre_data[27: 0], 4'hf };
ck_data <= { (4){ 4'hf, pre_data[31:28] } };
ck_counts <= 7;
end
default: begin // WIDTH_8W
ck_sreg <= { pre_data[23: 0], 8'hff };
ck_data <= { (4){pre_data[31:24] } };
ck_counts <= 3;
end
endcase end
// }}}
P_2D: begin // Two clock periods of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { pre_data[29: 0], 2'b11 };
ck_data <= { {(2){7'h7f, pre_data[31] }},
{(2){7'h7f, pre_data[30] }} };
ck_counts <= 15;
end
WIDTH_4W: begin
ck_sreg <= { pre_data[23: 0], 8'hff };
ck_data <= { {(2){4'hf, pre_data[31:28] }},
{(2){4'hf, pre_data[27:24] }} };
ck_counts <= 3;
end
default: begin // WIDTH_8W
ck_sreg <= { pre_data[15: 0], 16'hffff };
ck_data <= { {(2){pre_data[31:24] }},
{(2){pre_data[23:16]}} };
ck_counts <= 1;
end
endcase end
// }}}
default: begin // Four clock periods of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { pre_data[27: 0], 4'hf };
ck_data <= { 7'h7f, pre_data[31],
7'h7f, pre_data[30],
7'h7f, pre_data[29],
7'h7f, pre_data[28] };
ck_counts <= 7;
end
WIDTH_4W: begin
ck_sreg <= { pre_data[15: 0], 16'hffff };
ck_data <= { 4'hf, pre_data[31:28],
4'hf, pre_data[27:24],
4'hf, pre_data[23:20],
4'hf, pre_data[19:16] };
ck_counts <= 1;
end
default: begin // WIDTH_8W
ck_sreg <= {(32){1'b1}};
ck_data <= pre_data;
ck_counts <= 0;
end
endcase end
// }}}
endcase
end else if ((i_ckstb || (i_hlfck && cfg_ddr)) && ck_counts > 0)
begin // Drain the shift registers
ck_counts <= ck_counts - 1;
case(cfg_period)
P_1D: begin // One clock period of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { ck_sreg[30: 0], 1'b1 };
ck_data <= { (4){7'h7f, ck_sreg[31] }};
end
WIDTH_4W: begin
ck_sreg <= { ck_sreg[27: 0], 4'hf };
ck_data <= { (4){4'hf, ck_sreg[31:28] } };
end
default: begin // WIDTH_8W
ck_sreg <= { ck_sreg[23: 0], 8'hff };
ck_data <= { (4){ck_sreg[31:24] } };
end
endcase end
// }}}
P_2D: begin // Two clock periods of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { ck_sreg[29: 0], 2'b11 };
ck_data <= { {(2){7'h7f, ck_sreg[31] }},
{(2){7'h7f, ck_sreg[30] }} };
end
WIDTH_4W: begin
ck_sreg <= { ck_sreg[23: 0], 8'hff };
ck_data <= { {(2){4'hf, ck_sreg[31:28] }},
{(2){4'hf, ck_sreg[27:24] }} };
end
default: begin // WIDTH_8W
ck_sreg <= { ck_sreg[15: 0], 16'hffff };
ck_data <= { {(2){ck_sreg[31:24] }},
{(2){ck_sreg[23:16]}} };
end
endcase end
// }}}
default: begin // Four clock periods of data
// {{{
case(cfg_width)
WIDTH_1W: begin
ck_sreg <= { ck_sreg[27: 0], 4'hf };
ck_data <= { 7'h7f, ck_sreg[31],
7'h7f, ck_sreg[30],
7'h7f, ck_sreg[29],
7'h7f, ck_sreg[28] };
end
WIDTH_4W: begin
ck_sreg <= { ck_sreg[15: 0], 16'hffff };
ck_data <= { 4'hf, ck_sreg[31:28],
4'hf, ck_sreg[27:24],
4'hf, ck_sreg[23:20],
4'hf, ck_sreg[19:16] };
end
default: begin // WIDTH_8W
ck_sreg <= {(32){1'b1}};
ck_data <= ck_sreg;
end
endcase end
// }}}
endcase
end else if (i_ckstb && ck_counts == 0)
begin
// Fully idle
ck_data <= -1;
ck_sreg <= -1;
ck_counts <= (start_packet && i_cfg_ddr && !i_hlfck) ? 1:0;
if (start_packet) // Implies i_ckstb
case(cfg_period)
2'b00: begin // One data period / clock
case(cfg_width)
WIDTH_1W: ck_data <= {(4){ 8'hfe }};
WIDTH_4W: ck_data <= {(4){ 8'hf0 }};
default: ck_data <= {(4){ 8'h00 }};
endcase end
2'b01: begin // Two data periods / clock
// {{{
if (cfg_ddr)
begin
// One pedge, one negedge
case(cfg_width)
WIDTH_1W: ck_data <= {(4){ 8'hfe }};
WIDTH_4W: ck_data <= {(4){ 8'hf0 }};
default: ck_data <= {(4){ 8'h00 }};
endcase
end else begin
// Two posedges, so only the second gets the
// start bits
case(cfg_width)
WIDTH_1W: ck_data <= { {(2){8'hff}}, {(2){8'hfe}} };
WIDTH_4W: ck_data <= { {(2){8'hff}}, {(2){8'hf0}} };
default: ck_data <= { {(2){8'hff}}, {(2){8'h00}} };
endcase
end end
// }}}
default: begin // 4 data periods / clock, DDR
// pedge, nedge, pedge, nedge, so the last two
// get the start bit
case(cfg_width)
WIDTH_1W: ck_data <= { {(2){8'hff}}, {(2){8'hfe}} };
WIDTH_4W: ck_data <= { {(2){8'hff}}, {(2){8'hf0}} };
default: ck_data <= { {(2){8'hff}}, {(2){8'h00}} };
endcase end
endcase
end
`ifdef FORMAL
always @(*)
if (!i_reset && ck_valid && !pre_valid && !ck_stop_bit)
assert(&ck_sreg);
`endif
initial {r_tristate, ck_tristate } = 2'h3;
always @(posedge i_clk)
if (i_reset) // pstate == P_IDLE)
begin
ck_tristate <= 1'b1;
r_tristate <= 1'b1;
end else if (i_ckstb && pre_valid && ck_counts == 0) // && tx_ready
begin
if (cfg_pp || cfg_period != P_1D)
begin
ck_tristate <= 1'b0;
r_tristate <= 1'b0;
end else case(cfg_width) // One clock period of data
WIDTH_1W: begin
ck_tristate <= pre_data[31];
r_tristate <= pre_data[31] && (!OPT_SERDES || ck_tristate);
end
WIDTH_4W: begin
ck_tristate <= (&pre_data[31:28]);
r_tristate <= (&pre_data[31:28]) && (!OPT_SERDES || ck_tristate);
end
default: begin
ck_tristate <= (&pre_data[31:24]);
r_tristate <= (&pre_data[31:24]) && (!OPT_SERDES || ck_tristate);
end
endcase
end else if ((i_ckstb || (i_hlfck && cfg_ddr)) && ck_counts > 0)
begin
if (cfg_pp || cfg_period != P_1D)
begin
ck_tristate <= 1'b0;
r_tristate <= 1'b0;
end else case(cfg_width) // One clock period of data
WIDTH_1W: begin
ck_tristate <= ck_sreg[31];
r_tristate <= ck_sreg[31] && (!OPT_SERDES || ck_tristate);
end
WIDTH_4W: begin
ck_tristate <= (&ck_sreg[31:28]);
r_tristate <= (&ck_sreg[31:28]) && (!OPT_SERDES || ck_tristate);
end
default: begin
ck_tristate <= (&ck_sreg[31:24]);
r_tristate <= (&ck_sreg[31:24]) && (!OPT_SERDES || ck_tristate);
end
endcase
end else if (i_ckstb && ck_counts == 0)
begin
ck_tristate <= 1'b1;
r_tristate <= (!OPT_SERDES || ck_tristate);
if (start_packet)
begin
ck_tristate <= 1'b0;
r_tristate <= 1'b0;
end
end else
r_tristate <= ck_tristate;
assign pre_ready = (ck_counts == 0) && i_ckstb; // && tx_ready;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Final outputs
// {{{
assign tx_valid = ck_valid;
// assign ck_ready = (i_ckstb || (i_hlfck && cfg_ddr)); // && tx_ready;
assign tx_data = ck_data;
assign tx_tristate = r_tristate;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Status feedback
// {{{
// r_timeout
// {{{
// Insist on at least 10 clocks (2 + CRC) after any transmission before
// declaring ourselves done. The eMMC specification requires at least
// 2 such clocks, together with the number of clocks required for an
// ACK/NACK sequence (5). Here, we round that number up to 15 for good
// measure.
initial r_timeout = (OPT_CRCTOKEN) ? 4'd15 : 4'h0;
always @(posedge i_clk)
if (!OPT_CRCTOKEN)
r_timeout <= 0;
else if (i_reset || S_VALID || tx_valid || !i_en)
begin
r_timeout <= 15;
end else if (i_ckstb && (r_timeout != 0))
r_timeout <= r_timeout - 1;
// }}}
// o_done
// {{{
initial r_done = 1'b0;
always @(posedge i_clk)
if (i_reset || S_VALID || tx_valid || !i_en)
r_done <= 1'b0;
else if (!r_done && ((i_ckstb && (!OPT_CRCTOKEN || r_timeout <= 1))
|| i_crcack || i_crcnak))
// Once set, r_done will stay set until i_en drops
r_done <= 1'b1;
assign o_done = r_done;
// }}}
// o_err, o_ercode
// {{{
generate if (OPT_CRCTOKEN)
begin : GEN_CRCERR
reg r_ackd, r_err, r_ercode;
initial r_ackd = 1'b0;
always @(posedge i_clk)
if (i_reset || (i_en && S_VALID) || tx_valid || !i_en)
r_ackd <= 1'b0;
else if (!r_done && i_crcack)
r_ackd <= 1'b1;
initial { r_err, r_ercode } = 2'b00;
always @(posedge i_clk)
if (i_reset || (i_en && S_VALID) || tx_valid || !i_en)
begin
{ r_err, r_ercode } <= 2'b00;
end else if (i_en && !r_done && !o_err && !r_ackd)
begin
if (r_timeout <= 1 && i_cfg_expect_ack)
{ r_err, r_ercode } <= 2'b10;
if (i_crcnak && !i_crcack)
{ r_err, r_ercode } <= 2'b11;
end
assign { o_err, o_ercode } = { r_err, r_ercode };
end else begin : NO_CRCTOKEN
assign { o_err, o_ercode } = 2'b00;
// Verilator lint_off UNUSED
wire unused_token;
assign unused_token = &{ 1'b0, i_cfg_expect_ack };
// Verilator lint_on UNUSED
end endgenerate
// }}}
// }}}
//
// Make verilator happy
// {{{
// verilator coverage_off
// verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, i_ckwide, i_cfg_clk90 };
// verilator lint_on UNUSED
// verilator coverage_on
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
// These first sets of properties may be used in simulation as well
// as formal proofs. They are useful for verifying assumptions.
// {{{
// Verilator lint_off UNUSED
wire f_pending_half, f_pending_reset;
reg f_past_tx_valid, f_past_tick, f_ck_started;
reg f_ckstb, f_hlfck;
(* keep *) reg [9+5:0] fb_count, fd_offset, fd_count,
f_loaded_count;
(* keep *) reg [9:0] fs_count;
reg [10:0] fcrc_count;
(* keep *) reg fs_last;
wire f_step, fload_xtra;
// Verilator lint_on UNUSED
assign f_step = f_ckstb || (cfg_ddr && f_hlfck);
assign fload_xtra = tx_valid && f_step; // || fb_count == fd_offset);
always @(*)
begin
// Verilator lint_off WIDTH
if (fb_count < fd_offset)
f_loaded_count = 0;
else if (!tx_valid)
f_loaded_count = fb_count - fd_offset;
else case(cfg_period)
P_1D: case(cfg_width)
// {{{
WIDTH_1W: f_loaded_count = fd_count + ck_counts + (fload_xtra ? 1:0);
WIDTH_4W: f_loaded_count = fd_count + (ck_counts*4) + (fload_xtra ? 4:0);
WIDTH_8W: f_loaded_count = fd_count + (ck_counts*8) + (fload_xtra ? 8:0);
default: begin end
endcase
// }}}
P_2D: case(cfg_width)
// {{{
WIDTH_1W: f_loaded_count = fd_count + ck_counts*2 + (fload_xtra ? 2:0);
WIDTH_4W: f_loaded_count = fd_count + ck_counts*8 + (fload_xtra ? 8:0);
WIDTH_8W: f_loaded_count = fd_count + ck_counts*16 + (fload_xtra ? 16:0);
default: begin end
endcase
// }}}
P_4D: case(cfg_width)
// {{{
WIDTH_1W: f_loaded_count = fd_count + ck_counts*4 + (fload_xtra ? 4:0);
WIDTH_4W: f_loaded_count = fd_count + ck_counts*16 + (fload_xtra ? 16:0);
WIDTH_8W: f_loaded_count = fd_count + ck_counts*32 + (fload_xtra ? 32:0);
default: begin end
endcase
// }}}
default: begin end
endcase
// Verilator lint_on WIDTH
end
always @(posedge i_clk)
if (i_reset)
{ f_ckstb, f_hlfck } <= 2'b00;
else
{ f_ckstb, f_hlfck } <= { i_ckstb, i_hlfck };
// fb_count -- count bits sent, including starting word
// {{{
always @(posedge i_clk)
if (i_reset || !tx_valid)
fb_count <= 0;
else if (f_ckstb || (cfg_ddr && f_hlfck)) case(cfg_width)
WIDTH_1W: begin
case(cfg_period)
P_1D: fb_count <= fb_count + 1;
P_2D: fb_count <= fb_count + 2;
default: fb_count <= fb_count + 4;
endcase end
WIDTH_4W: begin
case(cfg_period)
P_1D: fb_count <= fb_count + 4;
P_2D: fb_count <= fb_count + 8;
default: fb_count <= fb_count + 16;
endcase end
default: begin
case(cfg_period)
P_1D: fb_count <= fb_count + 8;
P_2D: fb_count <= fb_count + 16;
default: fb_count <= fb_count + 32;
endcase end
endcase
always @(*)
if (!i_reset && (tx_valid || fb_count > 0))
begin
assert(i_en);
// assert(i_ckstb || f_ck_started);
end
// }}}
initial fs_last = 0;
always @(posedge i_clk)