-
Notifications
You must be signed in to change notification settings - Fork 37
/
spirxdata.v
696 lines (626 loc) · 14.7 KB
/
spirxdata.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
////////////////////////////////////////////////////////////////////////////////
//
// Filename: rtl/spirxdata.v
// {{{
// Project: SD-Card controller
//
// Purpose: To handle all of the processing associated with receiving data
// from an SD card via the lower-level SPI processor, and then
// issuing write commands to our internal memory store (external to this
// module).
//
// 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 spirxdata #(
// {{{
parameter DW = 32, AW = 8,
parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0,
localparam CRC_POLYNOMIAL = 16'h1021
// }}}
) (
// {{{
input wire i_clk, i_reset,
//
input wire i_start,
input wire [3:0] i_lgblksz,
input wire i_fifo,
output reg o_busy,
//
input wire i_ll_stb,
input wire [7:0] i_ll_byte,
//
output reg o_write,
output reg [AW-1:0] o_addr,
output reg [DW-1:0] o_data,
//
output reg o_rxvalid,
output reg [7:0] o_response
// }}}
);
// Signal declarations
// {{{
reg error_token, start_token, token, received_token, done,
lastaddr;
reg all_mem_written, lastdata;
reg [1:0] crc_byte;
reg [2:0] r_lgblksz_m3;
reg new_data_byte;
reg [3:0] crc_fill;
reg [7:0] crc_gearbox;
reg [15:0] next_crc_data;
reg [15:0] crc_data;
reg crc_err, crc_active;
reg [2:0] fill;
reg [23:0] gearbox;
reg [15:0] first_crc_data;
// }}}
// error_token
// {{{
always @(*)
begin
error_token = 0;
if (i_ll_byte[7:4] == 0)
error_token = 1;
if (!i_ll_stb || received_token)
error_token = 0;
end
// }}}
// start_token
// {{{
always @(*)
begin
start_token = 0;
if (!i_ll_byte[0])
start_token = 1;
if (!i_ll_stb || received_token)
start_token = 0;
end
// }}}
// token
// {{{
always @(*)
token = (start_token || error_token);
// }}}
// done
// {{{
always @(*)
done = (i_ll_stb && (crc_byte>1));
// }}}
// received_token
// {{{
initial received_token = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
received_token <= 0;
else if (token)
received_token <= 1;
// }}}
// o_busy
// {{{
initial o_busy = 0;
always @(posedge i_clk)
if (i_reset)
o_busy <= 0;
else if (!o_busy)
o_busy <= i_start;
else if (error_token || done)
o_busy <= 0;
// }}}
// o_rxvalid
// {{{
initial o_rxvalid = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
o_rxvalid <= 0;
else if (error_token || done)
o_rxvalid <= 1;
// }}}
// o_response
// {{{
initial o_response = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
o_response <= 0;
else if (error_token)
o_response <= i_ll_byte;
else if (done)
o_response <= (crc_err || (crc_data[7:0] != i_ll_byte)) ? 8'h10 : 0;
// }}}
// o_write
// {{{
initial o_write = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
o_write <= 0;
else if (received_token && !all_mem_written)
o_write <= (&fill) && i_ll_stb;
else
o_write <= 0;
// }}}
// o_data
// {{{
initial o_data = 0;
always @(posedge i_clk)
if (received_token && !all_mem_written)
begin
if (OPT_LITTLE_ENDIAN)
o_data <= { i_ll_byte, gearbox };
else
o_data <= { gearbox, i_ll_byte };
end
// }}}
// o_addr
// {{{
always @(posedge i_clk)
if (!o_busy)
o_addr <= { i_fifo, {(AW-1){1'b0}} };
else if (o_write && !lastaddr)
o_addr <= o_addr + 1;
// }}}
// fill
// {{{
initial fill = 0;
always @(posedge i_clk)
begin
if (i_ll_stb)
begin
if (OPT_LITTLE_ENDIAN)
gearbox <= { i_ll_byte, gearbox[23:8] };
else
gearbox <= { gearbox[15:0], i_ll_byte };
end
if (!o_busy || !received_token)
fill <= 0;
else if ((&fill) && i_ll_stb)
fill <= 0;
else if (i_ll_stb)
fill <= { fill[1:0], 1'b1 };
end
// }}}
// lastdata
// {{{
always @(posedge i_clk)
if (!o_busy)
lastdata <= 0;
else if (!lastdata)
lastdata <= (lastaddr && (&fill));
// }}}
// all_mem_written
// {{{
initial all_mem_written = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
all_mem_written <= 0;
else if (o_write && lastaddr)
all_mem_written <= 1;
// }}}
// crc_byte
// {{{
initial crc_byte = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
crc_byte <= 0;
else if (i_ll_stb && lastaddr && lastdata)
crc_byte <= crc_byte + 1;
// }}}
// lastaddr, r_lgblksz_m3
// {{{
initial r_lgblksz_m3 = 0;
initial lastaddr = 0;
always @(posedge i_clk)
if (!o_busy)
begin
lastaddr <= (i_lgblksz < 4);
// Verilator lint_off WIDTH
r_lgblksz_m3 <= i_lgblksz-3;
// Verilator lint_on WIDTH
end else if (o_write && !lastaddr)
begin
case(r_lgblksz_m3)
0: lastaddr <= 1; // 8 bytes
1: lastaddr <= (&o_addr[1:1]); // 16 bytes
2: lastaddr <= (&o_addr[2:1]); // 32 bytes
3: lastaddr <= (&o_addr[3:1]); // 64 bytes
4: lastaddr <= (&o_addr[4:1]); // 128 bytes
5: lastaddr <= (&o_addr[5:1]); // 256 bytes
default: lastaddr <= (&o_addr[6:1]); // 512 bytes
endcase
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// CRC calculation
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// new_data_byte
// {{{
always @(*)
new_data_byte = (i_ll_stb && !all_mem_written);
// }}}
// crc_fill, crc_active
// {{{
initial crc_fill = 0;
initial crc_active = 0;
always @(posedge i_clk)
if (i_reset || !o_busy || !received_token)
begin
crc_fill <= 0;
crc_active <= 0;
end else if (crc_active || new_data_byte)
begin
// Verilator lint_off WIDTH
crc_fill <= crc_fill - (crc_active ? 1:0)
+ (new_data_byte ? 4:0);
// Verilator lint_on WIDTH
if (new_data_byte)
crc_active <= 1;
else
crc_active <= (crc_fill > 1);
end
// }}}
// crc_gearbox
// {{{
always @(posedge i_clk)
if (!crc_active)
crc_gearbox <= i_ll_byte;
else
crc_gearbox <= { crc_gearbox[8-3:0], 2'b00 };
// }}}
// first_crc_data, next_crc_data
// {{{
always @(*)
begin
first_crc_data = crc_data << 1;
if (crc_data[15] ^ crc_gearbox[7])
first_crc_data = first_crc_data ^ CRC_POLYNOMIAL;
if (first_crc_data[15] ^ crc_gearbox[6])
next_crc_data = (first_crc_data << 1) ^ CRC_POLYNOMIAL;
else
next_crc_data = (first_crc_data << 1);
end
// }}}
// crc_data
// {{{
initial crc_data = 0;
always @(posedge i_clk)
if (!o_busy)
crc_data <= 0;
else if (crc_active)
crc_data <= next_crc_data;
// }}}
// crc_err
// {{{
initial crc_err = 0;
always @(posedge i_clk)
if (i_reset || !o_busy)
crc_err <= 0;
else if (i_ll_stb && (crc_byte == 1))
crc_err <= (crc_data[15:8] != i_ll_byte);
// else if (i_ll_stb && (crc_byte == 2)
// crc_err <= (crc_data[7:0] != i_ll_byte);
// }}}
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
`ifdef SPIRXDATA
`define ASSUME assume
`else
`define ASSUME assert
`endif
reg f_past_valid;
reg [3:0] f_lgblksz;
wire [3:0] f_lgblksz_m3;
reg f_fifo;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
// Reset check(s)
always @(posedge i_clk)
if (!f_past_valid || $past(i_reset))
begin
assert(!o_busy);
assert(o_write == 0);
end
always @(posedge i_clk)
if (!f_past_valid || !$past(o_busy))
begin
assert(!o_rxvalid);
assert(crc_fill == 0);
end
always @(*)
if (!o_busy)
assert(!o_write);
////////////////////////////////////////////////////////////////////////
//
// Data assumptions
// {{{
////////////////////////////////////////////////////////////////////////
//
//
reg [6:0] f_last_read;
initial f_last_read = 0;
always @(posedge i_clk)
begin
f_last_read <= f_last_read << 1;
if (i_ll_stb)
f_last_read[0] <= 1;
end
`ifdef VERIFIC
always @(*)
`ASSUME($onehot0({f_last_read, i_ll_stb}));
`else
always @(*)
case({f_last_read, i_ll_stb})
8'h00: begin end
8'h01: begin end
8'h02: begin end
8'h04: begin end
8'h08: begin end
8'h10: begin end
8'h20: begin end
8'h40: begin end
8'h80: begin end
default: `ASSUME(0);
endcase
`endif
always @(*)
if (!o_busy && i_start)
begin
`ASSUME(i_lgblksz <= 9);
`ASSUME(i_lgblksz >= 3);
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// Induction assertions
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
if (o_busy)
assert(crc_active == (crc_fill > 0));
always @(*)
if (o_write)
assert(fill == 0);
always @(*)
if (o_busy && crc_byte > 0)
begin
assert(lastaddr);
assert(all_mem_written || o_write);
assert(lastdata);
end
always @(*)
if (o_busy && all_mem_written)
begin
assert(lastaddr);
assert(lastdata);
end
always @(*)
if (o_busy && lastdata)
begin
assert(lastaddr);
assert((&fill) || o_write || all_mem_written);
end
always @(posedge i_clk)
if (!o_busy)
begin
f_lgblksz <= i_lgblksz;
f_fifo <= i_fifo;
end
assign f_lgblksz_m3 = f_lgblksz - 3;
always @(*)
if (o_busy)
begin
assert(f_lgblksz >= 3);
assert(f_lgblksz <= 9);
assert(o_addr[AW-1] == f_fifo);
if (!received_token)
begin
assert((f_lgblksz == 3) || !lastaddr);
assert(o_addr[AW-2:0] == 0);
assert(fill == 0);
end
end
always @(*)
if (o_busy)
assert(f_lgblksz_m3[2:0] == r_lgblksz_m3);
always @(*)
if (o_busy)
case(r_lgblksz_m3)
3'h0: assert(lastaddr); // 8 bytes
3'h1: assert(lastaddr == (&o_addr[1:0])); // 16 bytes
3'h2: assert(lastaddr == (&o_addr[2:0])); // 32 bytes
3'h3: assert(lastaddr == (&o_addr[3:0])); // 64 bytes
3'h4: assert(lastaddr == (&o_addr[4:0])); // 128 bytes
3'h5: assert(lastaddr == (&o_addr[5:0])); // 256 bytes
default: assert(lastaddr == (&o_addr[AW-2:0]));
endcase
always @(*)
if (o_busy)
case(r_lgblksz_m3)
3'h0: assert(o_addr[AW-2:1] == 0); // 8 bytes
3'h1: assert(o_addr[AW-2:2] == 0); // 16 bytes
3'h2: assert(o_addr[AW-2:3] == 0); // 32 bytes
3'h3: assert(o_addr[AW-2:4] == 0); // 64 bytes
3'h4: assert(o_addr[AW-2:5] == 0); // 128 bytes
3'h5: assert(o_addr[AW-2:6] == 0); // 256 bytes
default: begin end // assert(lastaddr == (&o_addr[AW-2:0]));
endcase
always @(*)
if (fill != 0)
begin
assert(fill[0]);
if (!fill[1])
assert(fill[2] == 0);
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// CRC checks and properties
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
assert(crc_fill <= 4);
always @(*)
if (o_busy && !received_token)
begin
assert(!crc_active);
assert(crc_fill == 0);
assert(crc_data == 0);
end
always @(*)
if (crc_active)
begin
if (crc_data == 0 && crc_gearbox[7:6] == 0)
assert(next_crc_data == 0);
if ((crc_data[15] ^ crc_gearbox[7])
&&((crc_data[14] ^ crc_gearbox[6])==0))
assert(next_crc_data == ({ crc_data[13:0], 2'b00 } ^ { CRC_POLYNOMIAL[14:0], 1'b0 }));
if (((crc_data[15] ^ crc_gearbox[7])==0)
&&(crc_data[14] ^ crc_gearbox[6]))
assert(next_crc_data == ({ crc_data[13:0], 2'b00 } ^ CRC_POLYNOMIAL[14:0]));
end
/*
reg [5:0] f_read_seq;
(* anyseq *) reg f_read_check;
reg [DW-1:0] f_read_data;
always @(posedge i_clk)
if (rdvalid[RDDELAY-1])
f_read_data <= i_data;
always @(*)
if (f_read_seq != 0)
assume(!f_read_check);
initial f_read_seq = 0;
always @(posedge i_clk)
if (!o_busy)
f_read_seq <= 0;
else if (f_read_check && o_read)
f_read_seq <= 1;
else if (rdvalid != 0)
begin
if (rdvalid[RDDELAY-1])
f_read_seq <= 2;
end else if (!i_ll_busy)
f_read_seq <= (f_read_seq << 1);
always @(*)
if (i_reset || !o_busy || (f_read_seq == 0))
begin end
else case(f_read_seq)
6'h01: begin
assert(rdvalid != 0);
assert(fill == 5'h10);
end
6'h02: begin
assert(rdvalid == 0);
assert(fill == 5'h1f);
assert(gearbox[DW-1:0] == f_read_data);
end
6'h04: begin
assert(rdvalid == 0);
assert(fill == 5'h1e);
assert(gearbox[8+DW-1:8] == f_read_data);
end
6'h08: begin
assert(rdvalid == 0);
assert(fill == 5'h1c);
assert(gearbox[8+DW-1:16] == f_read_data[23:0]);
end
6'h10: begin
assert(rdvalid == 0);
assert(fill == 5'h18);
assert(gearbox[8+DW-1:24] == f_read_data[15:0]);
end
6'h20: begin
assert(fill[4]);
assert(gearbox[8+DW-1:DW] == f_read_data[7:0]);
end
default: assert($onehot0(f_read_seq));
endcase
*/
// }}}
`ifdef SPIRXDATA
////////////////////////////////////////////////////////////////////////
//
// Cover properties
// {{{
////////////////////////////////////////////////////////////////////////
//
//
//
reg cvr_packet_received;
always @(*)
if (!received_token)
begin
cover(error_token);
cover(start_token);
end
initial cvr_packet_received = 0;
always @(posedge i_clk)
if (i_reset || error_token)
cvr_packet_received <= 0;
else if (o_rxvalid)
cvr_packet_received <= 1;
always @(posedge i_clk)
begin
cover(o_rxvalid);
cover(o_rxvalid && all_mem_written);
cover(o_rxvalid && f_lgblksz == 4 && all_mem_written);
cover(o_rxvalid && f_lgblksz == 4 && o_response == 0);
cover(o_rxvalid && f_lgblksz == 4 && o_response == 0 && all_mem_written);
cover(o_busy && f_lgblksz == 4 && o_addr == 8'h01);
cover(o_busy && f_lgblksz == 4 && o_addr == 8'h02);
cover(o_busy && f_lgblksz == 4 && o_addr == 8'h03);
cover(o_busy && crc_byte == 0);
cover(o_busy && crc_byte == 1);
cover(o_busy && crc_byte == 2);
cover(o_busy && crc_byte == 1 && i_ll_stb);
cover(o_busy && crc_byte == 2 && i_ll_stb);
cover(cvr_packet_received && !o_busy);
end
// }}}
`endif // SPIRXDATA
`endif // FORMAL
// }}}
endmodule