/
dcache.v
2725 lines (2422 loc) · 66.5 KB
/
dcache.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: dcache.v
// {{{
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
//
// Purpose: To provide a simple data cache for the ZipCPU. The cache is
// designed to be a drop in replacement for the pipememm memory
// unit currently existing within the ZipCPU. The goal of this unit is
// to achieve single cycle read access to any memory in the last cache line
// used, or two cycle access to any memory currently in the cache.
//
// The cache separates between four types of accesses, one write and three
// read access types. The read accesses are split between those that are
// not cacheable, those that are in the cache, and those that are not.
//
// 1. Write accesses always create writes to the bus. For these reasons,
// these may always be considered cache misses.
//
// Writes to memory locations within the cache must also update
// cache memory immediately, to keep the cache in synch.
//
// It is our goal to be able to maintain single cycle write
// accesses for memory bursts.
//
// 2. Read access to non-cacheable memory locations will also immediately
// go to the bus, just as all write accesses go to the bus.
//
// 3. Read accesses to cacheable memory locations will immediately read
// from the appropriate cache line. However, since thee valid
// line will take a second clock to read, it may take up to two
// clocks to know if the memory was in cache. For this reason,
// we bypass the test for the last validly accessed cache line.
//
// We shall design these read accesses so that reads to the cache
// may take place concurrently with other writes to the bus.
//
// Errors in cache reads will void the entire cache line. For this reason,
// cache lines must always be of a smaller in size than any associated
// virtual page size--lest in the middle of reading a page a TLB miss
// take place referencing only a part of the cacheable page.
//
//
//
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2023, 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.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
//
`default_nettype none
//
//
`ifdef FORMAL
`define ASSERT assert
`ifdef DCACHE
`define ASSUME assume
`else
`define ASSUME assert
`endif
`endif
// }}}
module dcache #(
// {{{
parameter LGCACHELEN = 8,
BUS_WIDTH=32,
ADDRESS_WIDTH=32-$clog2(BUS_WIDTH/8),
LGNLINES=(LGCACHELEN-3), // Log of the number of separate cache lines
NAUX=5, // # of aux d-wires to keep aligned w/memops
parameter DATA_WIDTH=32, // CPU's register width
parameter [0:0] OPT_LOCAL_BUS=1'b1,
parameter [0:0] OPT_PIPE=1'b1,
parameter [0:0] OPT_LOCK=1'b1,
parameter [0:0] OPT_DUAL_READ_PORT=1'b1,
parameter OPT_FIFO_DEPTH = 4,
localparam AW = ADDRESS_WIDTH, // Just for ease of notation below
localparam CS = LGCACHELEN, // Number of bits in a cache address
localparam LS = CS-LGNLINES, // Bits to spec position w/in cline
`ifdef FORMAL
parameter F_LGDEPTH=1 + (((!OPT_PIPE)||(LS > OPT_FIFO_DEPTH))
? LS : OPT_FIFO_DEPTH),
`endif
parameter [0:0] OPT_LOWPOWER = 1'b0,
// localparam DW = 32, // Bus data width
localparam DP = OPT_FIFO_DEPTH,
localparam WBLSB = $clog2(BUS_WIDTH/8),
// localparam DLSB = $clog2(DATA_WIDTH/8),
//
localparam [1:0] DC_IDLE = 2'b00, // Bus is idle
localparam [1:0] DC_WRITE = 2'b01, // Write
localparam [1:0] DC_READS = 2'b10, // Read a single value(!cachd)
localparam [1:0] DC_READC = 2'b11 // Read a whole cache line
// }}}
) (
// {{{
input wire i_clk, i_reset, i_clear,
// Interface from the CPU
// {{{
input wire i_pipe_stb, i_lock,
input wire [2:0] i_op,
input wire [DATA_WIDTH-1:0] i_addr,
input wire [DATA_WIDTH-1:0] i_data,
input wire [(NAUX-1):0] i_oreg, // Aux data, such as reg to write to
// Outputs, going back to the CPU
output reg o_busy, o_rdbusy,
output reg o_pipe_stalled,
output reg o_valid, o_err,
output reg [(NAUX-1):0] o_wreg,
output reg [DATA_WIDTH-1:0] o_data,
// }}}
// Wishbone bus master outputs
// {{{
output wire o_wb_cyc_gbl, o_wb_cyc_lcl,
output reg o_wb_stb_gbl, o_wb_stb_lcl,
output reg o_wb_we,
output reg [(AW-1):0] o_wb_addr,
output reg [BUS_WIDTH-1:0] o_wb_data,
output wire [BUS_WIDTH/8-1:0] o_wb_sel,
// Wishbone bus slave response inputs
input wire i_wb_stall, i_wb_ack, i_wb_err,
input wire [BUS_WIDTH-1:0] i_wb_data
// }}}
// }}}
);
// Declarations
// {{{
localparam FIF_WIDTH = (NAUX-1)+2+WBLSB;
integer ik;
`ifdef FORMAL
wire [(F_LGDEPTH-1):0] f_nreqs, f_nacks, f_outstanding;
wire f_pc, f_gie, f_read_cycle;
reg f_past_valid;
`endif
//
// output reg [31:0] o_debug;
reg cyc, stb, last_ack, end_of_line, last_line_stb;
reg r_wb_cyc_gbl, r_wb_cyc_lcl;
// npending is the number of pending non-cached operations, counted
// from the i_pipe_stb to the o_wb_ack
reg [DP:0] npending;
reg [((1<<LGNLINES)-1):0] c_v; // One bit per cache line, is it valid?
reg [(AW-LS-1):0] c_vtags [0:((1<<LGNLINES)-1)];
reg [BUS_WIDTH-1:0] c_mem [0:((1<<CS)-1)];
reg set_vflag;
reg [1:0] state;
reg [(CS-1):0] wr_addr;
reg [BUS_WIDTH-1:0] cached_iword, cached_rword;
reg lock_gbl, lock_lcl;
// To simplify writing to the cache, and the job of the synthesizer to
// recognize that a cache write needs to take place, we'll take an extra
// clock to get there, and use these c_w... registers to capture the
// data in the meantime.
reg c_wr;
reg [BUS_WIDTH-1:0] c_wdata;
reg [BUS_WIDTH/8-1:0] c_wsel;
reg [(CS-1):0] c_waddr;
reg [(AW-LS-1):0] last_tag;
reg last_tag_valid;
wire [(LGNLINES-1):0] i_cline;
wire [(CS-1):0] i_caddr;
`ifdef FORMAL
reg [F_LGDEPTH-1:0] f_fill;
reg [AW:0] f_return_address;
reg [AW:0] f_pending_addr;
reg f_pc_pending;
wire [4:0] f_last_reg, f_addr_reg;
// Verilator lint_off UNDRIVEN
(* anyseq *) reg [4:0] f_areg;
// Verilator lint_on UNDRIVEN
`endif
wire cache_miss_inow, w_cachable;
wire raw_cachable_address;
reg r_cachable, r_svalid, r_dvalid, r_rd, r_cache_miss,
r_rd_pending;
reg [AW-1:0] r_addr;
wire [(LGNLINES-1):0] r_cline;
wire [(CS-1):0] r_caddr;
wire [(AW-LS-1):0] r_ctag;
reg wr_cstb, r_iv, in_cache;
reg [(AW-LS-1):0] r_itag;
reg [FIF_WIDTH:0] req_data;
reg gie;
reg [BUS_WIDTH-1:0] pre_data, pre_shifted;
// }}}
// Convenience assignments
// {{{
assign i_cline = i_addr[WBLSB +LS +: (CS-LS)];
assign i_caddr = i_addr[WBLSB +: CS];
assign cache_miss_inow = (!last_tag_valid)
||(last_tag != i_addr[WBLSB+LS +: (AW-LS)])
||(!c_v[i_cline]);
assign w_cachable = ((!OPT_LOCAL_BUS)
||(i_addr[DATA_WIDTH-1:DATA_WIDTH-8]!=8'hff))
&&((!i_lock)||(!OPT_LOCK))&&(raw_cachable_address);
assign r_cline = r_addr[(CS-1):LS];
assign r_caddr = r_addr[(CS-1):0];
assign r_ctag = r_addr[(AW-1):LS];
// }}}
// Cachability checking
// {{{
iscachable chkaddress(i_addr[0 +: AW+WBLSB], raw_cachable_address);
// }}}
// r_* values
// {{{
// The one-clock delayed read values from the cache.
//
initial r_rd = 1'b0;
initial r_cachable = 1'b0;
initial r_svalid = 1'b0;
initial r_dvalid = 1'b0;
initial r_cache_miss = 1'b0;
initial r_addr = 0;
initial last_tag_valid = 0;
initial r_rd_pending = 0;
always @(posedge i_clk)
begin
// The single clock path
// The valid for the single clock path
// Only ... we need to wait if we are currently writing
// to our cache.
r_svalid<= (i_pipe_stb)&&(!i_op[0])&&(w_cachable)
&&(!cache_miss_inow)&&(!c_wr)&&(!wr_cstb);
//
// The two clock in-cache path
//
// Some preliminaries that needed to be calculated on the first
// clock
if ((!o_pipe_stalled)&&(!r_rd_pending))
r_addr <= i_addr[WBLSB +: AW];
if ((!o_pipe_stalled)&&(!r_rd_pending))
begin
r_iv <= c_v[i_cline];
// r_itag <= c_vtags[i_cline];
r_cachable <= (!i_op[0])&&(w_cachable)&&(i_pipe_stb);
r_rd_pending <= (i_pipe_stb)&&(!i_op[0])&&(w_cachable)
&&((cache_miss_inow)||(c_wr)||(wr_cstb));
// &&((!c_wr)||(!wr_cstb));
end else begin
r_iv <= c_v[r_cline];
// r_itag <= c_vtags[r_cline];
r_rd_pending <= (r_rd_pending)
&&((!cyc)||(!i_wb_err))
&&((r_itag != r_ctag)||(!r_iv));
end
r_rd <= (i_pipe_stb)&&(!i_op[0]);
// r_itag contains the tag we didn't have available to us on the
// last clock, r_ctag is a bit select from r_addr containing a
// one clock delayed address.
r_dvalid <= (!r_svalid)&&(!r_dvalid)&&(r_itag == r_ctag)&&(r_iv)
&&(r_cachable)&&(r_rd_pending);
if ((r_itag == r_ctag)&&(r_iv)&&(r_cachable)&&(r_rd_pending))
begin
last_tag_valid <= 1'b1;
last_tag <= r_ctag;
end else if ((state == DC_READC)
&&(last_tag[CS-LS-1:0]==r_addr[CS-1:LS])
&&((i_wb_ack)||(i_wb_err)))
last_tag_valid <= 1'b0;
// r_cache miss takes a clock cycle. It is only ever true for
// something that should be cachable, but isn't in the cache.
// A cache miss is only true _if_
// 1. A read was requested
// 2. It is for a cachable address, AND
// 3. It isn't in the cache on the first read
// or the second read
// 4. The read hasn't yet started to get this address
r_cache_miss <= ((!cyc)||(o_wb_we))&&(r_cachable)
// One clock path -- miss
&&(!r_svalid)
// Two clock path -- misses as well
&&(r_rd)&&(!r_svalid)
&&((r_itag != r_ctag)||(!r_iv));
if (i_clear)
last_tag_valid <= 0;
if (i_reset)
begin
// r_rd <= 1'b0;
r_cachable <= 1'b0;
r_svalid <= 1'b0;
r_dvalid <= 1'b0;
r_cache_miss <= 1'b0;
// r_addr <= 0;
r_rd_pending <= 0;
last_tag_valid <= 0;
end
end
always @(posedge i_clk)
r_itag <= c_vtags[(!o_pipe_stalled && !r_rd_pending) ? i_cline : r_cline];
// }}}
// o_wb_sel, r_sel
// {{{
generate if (DATA_WIDTH == BUS_WIDTH)
begin : COPY_SEL
// {{{
reg [BUS_WIDTH/8-1:0] r_sel;
initial r_sel = 4'hf;
always @(posedge i_clk)
if (i_reset)
r_sel <= 4'hf;
else if (!o_pipe_stalled && (!OPT_LOWPOWER || i_pipe_stb))
begin
casez({i_op[2:1], i_addr[1:0]})
4'b0???: r_sel <= 4'b1111;
4'b100?: r_sel <= 4'b1100;
4'b101?: r_sel <= 4'b0011;
4'b1100: r_sel <= 4'b1000;
4'b1101: r_sel <= 4'b0100;
4'b1110: r_sel <= 4'b0010;
4'b1111: r_sel <= 4'b0001;
endcase
end else if (OPT_LOWPOWER && !i_wb_stall)
r_sel <= 4'h0;
assign o_wb_sel = (state == DC_READC) ? 4'hf : r_sel;
// }}}
end else begin : GEN_SEL
// {{{
reg [DATA_WIDTH/8-1:0] pre_sel;
reg [BUS_WIDTH/8-1:0] full_sel, r_wb_sel;
always @(*)
casez(i_op[2:1])
2'b0?: pre_sel = {(DATA_WIDTH/8){1'b1}};
2'b10: pre_sel = { 2'b11, {(DATA_WIDTH/8-2){1'b0}} };
2'b11: pre_sel = { 1'b1, {(DATA_WIDTH/8-1){1'b0}} };
endcase
always @(*)
if (OPT_LOCAL_BUS && (&i_addr[31:24]))
full_sel = { {(BUS_WIDTH/8-4){1'b0}}, pre_sel }
>> (i_addr[1:0]);
else
full_sel = { pre_sel, {(BUS_WIDTH/8-4){1'b0}} }
>> (i_addr[WBLSB-1:0]);
initial r_wb_sel = -1;
always @(posedge i_clk)
if (i_reset)
r_wb_sel <= -1;
else if (i_pipe_stb && (i_op[0] || !w_cachable))
r_wb_sel <= full_sel;
else if (!i_wb_stall)
r_wb_sel <= -1;
assign o_wb_sel = r_wb_sel;
// }}}
end endgenerate
// }}}
// o_wb_data
// {{{
generate if (DATA_WIDTH == BUS_WIDTH)
begin
// {{{
initial o_wb_data = 0;
always @(posedge i_clk)
if (i_reset)
o_wb_data <= 0;
else if ((!o_busy || !i_wb_stall) && (!OPT_LOWPOWER || i_pipe_stb))
begin
if (DATA_WIDTH == 32)
begin
if (OPT_LOWPOWER)
begin : ZERO_UNUSED_DATA_BITS
casez({ i_op[2:1], i_addr[1:0] })
4'b0???: o_wb_data <= i_data;
4'b100?: o_wb_data <= { i_data[15:0], 16'h0 };
4'b101?: o_wb_data <= { 16'h0, i_data[15:0] };
4'b1100: o_wb_data <= { i_data[7:0], 24'h0 };
4'b1101: o_wb_data <= { 8'h0, i_data[7:0], 16'h0 };
4'b1110: o_wb_data <= { 16'h0, i_data[7:0], 8'h0 };
4'b1111: o_wb_data <= { 24'h0, i_data[7:0] };
endcase
end else begin : DUPLICATE_UNUSED_DATA_BITS
casez(i_op[2:1])
2'b0?: o_wb_data <= i_data;
2'b10: o_wb_data <= { (2){i_data[15:0]} };
2'b11: o_wb_data <= { (4){i_data[ 7:0]} };
endcase
end
end else begin
// Verilator coverage_off
casez(i_op[2:1])
2'b0?: o_wb_data <= i_data << (8*i_addr[$clog2(DATA_WIDTH)-1:0]);
2'b10: o_wb_data <= { 16'h0, i_data[15:0] } << (8*i_addr[$clog2(DATA_WIDTH)-1:0]);
2'b11: o_wb_data <= { 24'h0, i_data[7:0] } << (8*i_addr[$clog2(DATA_WIDTH)-1:0]);
endcase
// Verilator coverage_on
end
end else if (OPT_LOWPOWER && !i_wb_stall)
o_wb_data <= 0;
// }}}
end else begin
// {{{
reg [DATA_WIDTH-1:0] pre_shift;
reg [BUS_WIDTH-1:0] wide_preshift, shifted_data;
always @(*)
begin
casez(i_op[2:1])
2'b0?: pre_shift = i_data;
2'b10: pre_shift = { i_data[15:0],
{(DATA_WIDTH-16){1'b0}} };
2'b11: pre_shift = { i_data[ 7:0],
{(DATA_WIDTH- 8){1'b0}} };
endcase
if (OPT_LOCAL_BUS && (&i_addr[DATA_WIDTH-1:DATA_WIDTH-8]))
begin
wide_preshift = { {(BUS_WIDTH-DATA_WIDTH){1'b0}}, pre_shift };
shifted_data = wide_preshift >> (8*i_addr[2-1:0]);
end else begin
wide_preshift = { pre_shift,
{(BUS_WIDTH-DATA_WIDTH){1'b0}} };
shifted_data = wide_preshift >> (8*i_addr[WBLSB-1:0]);
end
end
initial o_wb_data = 0;
always @(posedge i_clk)
if (OPT_LOWPOWER && i_reset)
o_wb_data <= 0;
else if ((!o_busy || !i_wb_stall)
&& (!OPT_LOWPOWER || (i_pipe_stb && i_op[0])))
begin
if (!OPT_LOWPOWER)
begin
casez(i_op[2:1])
2'b0?: o_wb_data <= {(BUS_WIDTH/DATA_WIDTH){i_data}};
2'b10: o_wb_data <= {(BUS_WIDTH/16){i_data[15:0]}};
2'b11: o_wb_data <= {(BUS_WIDTH/ 8){i_data[ 7:0]}};
endcase
end else begin
o_wb_data <= shifted_data;
end
end else if (OPT_LOWPOWER && !i_wb_stall)
o_wb_data <= 0;
// }}}
end endgenerate
// }}}
// Register return FIFO
// {{{
generate if (OPT_PIPE)
begin : OPT_PIPE_FIFO
// {{{
reg [FIF_WIDTH-1:0] fifo_data [0:((1<<OPT_FIFO_DEPTH)-1)];
reg [DP:0] wraddr, rdaddr;
always @(posedge i_clk)
if (i_pipe_stb)
fifo_data[wraddr[DP-1:0]]
<= { i_oreg[NAUX-2:0], i_op[2:1], i_addr[WBLSB-1:0] };
always @(posedge i_clk)
if (i_pipe_stb)
gie <= i_oreg[NAUX-1];
`ifdef NO_BKRAM
reg [FIF_WIDTH-1:0] r_req_data, r_last_data;
reg single_write;
always @(posedge i_clk)
r_req_data <= fifo_data[rdaddr[DP-1:0]];
always @(posedge i_clk)
single_write <= (rdaddr == wraddr)&&(i_pipe_stb);
always @(posedge i_clk)
if (i_pipe_stb)
r_last_data <= { i_oreg[NAUX-2:0],
i_op[2:1], i_addr[WBLSB-1:0] };
always @(*)
begin
req_data[NAUX+4-1] = gie;
// if ((r_svalid)||(state == DC_READ))
if (single_write)
req_data[FIF_WIDTH-1:0] = r_last_data;
else
req_data[FIF_WIDTH-1:0] = r_req_data;
end
always @(*)
`ASSERT(req_data == fifo_data[rdaddr[DP-1:0]]);
`else
always @(*)
req_data[FIF_WIDTH-1:0] = fifo_data[rdaddr[DP-1:0]];
always @(*)
req_data[FIF_WIDTH] = gie;
`endif
initial wraddr = 0;
always @(posedge i_clk)
if ((i_reset)||((cyc)&&(i_wb_err)))
wraddr <= 0;
else if (i_pipe_stb)
wraddr <= wraddr + 1'b1;
initial rdaddr = 0;
always @(posedge i_clk)
if ((i_reset)||((cyc)&&(i_wb_err)))
rdaddr <= 0;
else if ((r_dvalid)||(r_svalid))
rdaddr <= rdaddr + 1'b1;
else if ((state == DC_WRITE)&&(i_wb_ack))
rdaddr <= rdaddr + 1'b1;
else if ((state == DC_READS)&&(i_wb_ack))
rdaddr <= rdaddr + 1'b1;
`ifdef FORMAL
reg [AW-1:0] f_fifo_addr [0:((1<<OPT_FIFO_DEPTH)-1)];
reg [F_LGDEPTH-1:0] f_last_wraddr;
reg [FIF_WIDTH:0] f_last_data;
always @(*)
begin
f_fill = 0;
f_fill[DP:0] = wraddr - rdaddr;
end
always @(*)
`ASSERT(f_fill <= { 1'b1, {(DP){1'b0}} });
always @(*)
if ((r_dvalid)||(r_svalid))
begin
if (r_svalid)
begin
`ASSERT(f_fill == 1);
end else if (r_dvalid)
begin
`ASSERT(f_fill == 1);
end else
`ASSERT(f_fill == 0);
end else if (r_rd_pending)
begin
`ASSERT(f_fill == 1);
end else
`ASSERT(f_fill == npending);
initial f_pc_pending = 0;
always @(posedge i_clk)
if (i_reset)
f_pc_pending <= 1'b0;
else if (i_pipe_stb)
f_pc_pending <= (!i_op[0])&&(i_oreg[3:1] == 3'h7);
else if (f_fill == 0)
f_pc_pending <= 1'b0;
//else if ((o_valid)&&(o_wreg[3:1] == 3'h7)&&(f_fill == 0))
// f_pc_pending <= 1'b0;
always @(posedge i_clk)
if (f_pc_pending)
begin
`ASSUME(!i_pipe_stb);
end
always @(posedge i_clk)
if (state == DC_WRITE)
begin
`ASSERT(!f_pc_pending);
end
always @(*)
begin
f_last_wraddr = 0;
f_last_wraddr[DP:0] = wraddr - 1'b1;
end
assign f_last_data = fifo_data[f_last_wraddr];
always @(posedge i_clk)
if (r_rd_pending)
begin
`ASSERT(f_pc_pending == (f_last_data[1+WBLSB+2 +: 3] == 3'h7));
`ASSERT({ gie, f_last_data[2+WBLSB +: 4] } == f_last_reg);
end
`define INSPECT_FIFO
reg [((1<<(DP+1))-1):0] f_valid_fifo_entry;
genvar gk;
for(gk=0; gk<(1<<(DP+1)); gk=gk+1)
begin
always @(*)
begin
f_valid_fifo_entry[gk] = 1'b0;
/*
if ((rdaddr[DP] != wraddr[DP])
&&(rdaddr[DP-1:0] == wraddr[DP-1:0]))
f_valid_fifo_entry[k] = 1'b1;
else */
if ((rdaddr < wraddr)&&(gk < wraddr)
&&(gk >= rdaddr))
f_valid_fifo_entry[gk] = 1'b1;
else if ((rdaddr > wraddr)&&(gk >= rdaddr))
f_valid_fifo_entry[gk] = 1'b1;
else if ((rdaddr > wraddr)&&(gk < wraddr))
f_valid_fifo_entry[gk] = 1'b1;
end
`ifdef INSPECT_FIFO
wire [FIF_WIDTH-1:0] fifo_data_k;
assign fifo_data_k = fifo_data[gk[DP-1:0]];
always @(*)
if (f_valid_fifo_entry[gk])
begin
if (!f_pc_pending)
begin
`ASSERT((o_wb_we)||(fifo_data_k[1+2+WBLSB +: 3] != 3'h7));
end else if (gk != f_last_wraddr)
`ASSERT(fifo_data_k[1+2+WBLSB +: 3] != 3'h7);
end
`endif // INSPECT_FIFO
end
`ifndef INSPECT_FIFO
always @(posedge i_clk)
if ((r_rd_pending)&&(rdaddr[DP:0] != f_last_wraddr[DP-1]))
assume(req_data[1+2+WBLSB +: 3] != 3'h7);
`endif // INSPECT_FIFO
//
//
//
always @(*)
begin
f_pending_addr[AW-1:0] = f_fifo_addr[rdaddr];
f_pending_addr[AW] = r_wb_cyc_lcl;
end
//
//
//
always @(posedge i_clk)
if (i_pipe_stb)
begin
if (OPT_LOCAL_BUS && (&i_addr[DATA_WIDTH-1:DATA_WIDTH-8]))
f_fifo_addr[wraddr[DP-1:0]] <= { 1'b1, i_addr[2 +: AW] };
else
f_fifo_addr[wraddr[DP-1:0]] <= { 1'b0, i_addr[WBLSB +: AW] };
end
always @(*)
begin
f_return_address[AW] = (o_wb_cyc_lcl);
f_return_address[AW-1:0] = f_fifo_addr[rdaddr];
if (state == DC_READC)
f_return_address[LS-1:0]
= (o_wb_addr[LS-1:0] - f_outstanding[LS-1:0]);
end
`define TWIN_WRITE_TEST
`ifdef TWIN_WRITE_TEST
reg [DP:0] f_twin_next;
// Verilator lint_off UNDRIVEN
(* anyconst *) reg [DP:0] f_twin_base;
(* anyconst *) reg [AW+FIF_WIDTH-1:0] f_twin_first,
f_twin_second;
// Verilator lint_on UNDRIVEN
reg f_twin_none, f_twin_single, f_twin_double, f_twin_last;
reg f_twin_valid_one, f_twin_valid_two;
always @(*)
f_twin_next = f_twin_base+1;
always @(*)
begin
f_twin_valid_one = ((f_valid_fifo_entry[f_twin_base])
&&(f_twin_first == { f_fifo_addr[f_twin_base[DP-1:0]],
fifo_data[f_twin_base[DP-1:0]] }));
f_twin_valid_two = ((f_valid_fifo_entry[f_twin_next])
&&(f_twin_second == { f_fifo_addr[f_twin_next[DP-1:0]],
fifo_data[f_twin_next[DP-1:0]] }));
end
always @(*)
begin
f_twin_none =(!f_twin_valid_one)&&(!f_twin_valid_two);
f_twin_single =( f_twin_valid_one)&&(!f_twin_valid_two);
f_twin_double =( f_twin_valid_one)&&( f_twin_valid_two);
f_twin_last =(!f_twin_valid_one)&&( f_twin_valid_two);
end
always @(posedge i_clk)
if ((!f_past_valid)||($past(i_reset))||($past(cyc && i_wb_err)))
begin
`ASSERT(f_twin_none);
end else if ($past(f_twin_none))
begin
`ASSERT(f_twin_none || f_twin_single || f_twin_last);
end else if ($past(f_twin_single))
begin
`ASSERT(f_twin_none || f_twin_single || f_twin_double || f_twin_last);
end else if ($past(f_twin_double))
begin
`ASSERT(f_twin_double || f_twin_last);
end else if ($past(f_twin_last))
`ASSERT(f_twin_none || f_twin_single || f_twin_last);
// f_addr_reg test
// {{{
always @(*)
if (o_rdbusy)
begin
if (f_twin_valid_one && f_twin_base != f_last_wraddr)
`ASSERT({ gie, f_twin_first[2+WBLSB +: 4] } != f_addr_reg);
if (f_twin_valid_two && f_twin_next != f_last_wraddr)
`ASSERT({ gie, f_twin_second[2+WBLSB +: 4] } != f_addr_reg);
if ((rdaddr != f_last_wraddr)&&(rdaddr != f_twin_base)
&&(rdaddr != f_twin_next))
assume({ gie, req_data[2+WBLSB +: 4] } != f_addr_reg);
end
// }}}
`endif // TWIN_WRITE_TEST
always @(*)
`ASSERT(req_data == { gie, fifo_data[rdaddr[DP-1:0]] });
always @(posedge i_clk)
if (r_svalid||r_dvalid || r_rd_pending)
begin
`ASSERT(f_fill == 1);
end else if (f_fill > 0)
begin
`ASSERT(cyc);
end
always @(posedge i_clk)
if (state != 0)
begin
`ASSERT(f_fill > 0);
end else if (!r_svalid && !r_dvalid && !r_rd_pending)
`ASSERT(f_fill == 0);
`endif // FORMAL
always @(posedge i_clk)
o_wreg <= req_data[2+WBLSB +: NAUX];
// }}}
end else begin : NO_FIFO
// {{{
reg [AW-1:0] fr_last_addr;
always @(posedge i_clk)
if (i_pipe_stb)
req_data <= { i_oreg, i_op[2:1], i_addr[WBLSB-1:0] };
always @(*)
o_wreg = req_data[2+WBLSB +: NAUX];
always @(*)
gie = o_wreg[NAUX-1];
`ifdef FORMAL
// f_pc_pending
// {{{
always @(*)
begin
f_pc_pending = 0;
if ((r_rd_pending || state == DC_READS)||(o_valid))
f_pc_pending = (o_wreg[3:1] == 3'h7);
end
// }}}
// f_pending_addr
// {{{
initial f_pending_addr = 0;
always @(posedge i_clk)
if (i_reset)
f_pending_addr <= 0;
else if (i_pipe_stb)
begin
if ((OPT_LOCAL_BUS)&&(&i_addr[DATA_WIDTH-1:DATA_WIDTH-8]))
f_pending_addr <= { 1'b1, i_addr[2 +: AW] };
else
f_pending_addr <= { 1'b0, i_addr[WBLSB +: AW] };
end
// }}}
// f_return_address
// {{{
always @(posedge i_clk)
if (stb)
fr_last_addr <= o_wb_addr;
always @(*)
begin
f_return_address[AW] = o_wb_cyc_lcl;
f_return_address[AW-1:LS] = o_wb_addr[AW-1:LS];
if (OPT_LOWPOWER && !stb)
f_return_address[AW-1:LS] = fr_last_addr[AW-1:LS];
end
always @(*)
if (state == DC_READS)
begin
f_return_address[LS-1:0] = o_wb_addr[LS-1:0];
if (OPT_LOWPOWER && !stb)
f_return_address[LS-1:0] = fr_last_addr[LS-1:0];
end else begin
f_return_address[LS-1:0]
= (o_wb_addr[LS-1:0] - f_outstanding[LS-1:0]);
if (OPT_LOWPOWER && !stb)
f_return_address[LS-1:0] = (fr_last_addr[LS-1:0] - f_outstanding[LS-1:0]);
end
// }}}
// f_last_reg
// {{{
always @(*)
if (o_rdbusy)
assert(o_wreg == f_last_reg);
// }}}
// verilator lint_off UNUSED
initial f_fill = 0;
wire unused_no_fifo_formal;
assign unused_no_fifo_formal = &{ 1'b0, f_return_address,
f_addr_reg, f_fill };
`endif
wire unused_no_fifo;
assign unused_no_fifo = &{ 1'b0, gie };
// verilator lint_on UNUSED
// }}}
end endgenerate
// }}}
// BIG STATE machine: CYC, STB, c_v, state, etc
// {{{
initial o_wb_addr = 0;
initial r_wb_cyc_gbl = 0;
initial r_wb_cyc_lcl = 0;
initial o_wb_stb_gbl = 0;
initial o_wb_stb_lcl = 0;
initial c_v = 0;
initial cyc = 0;
initial stb = 0;
initial c_wr = 0;
initial wr_cstb = 0;
initial state = DC_IDLE;
initial set_vflag = 1'b0;
always @(posedge i_clk)
if (i_reset)
begin
// {{{
c_v <= 0;
c_wr <= 1'b0;
c_wsel <= {(BUS_WIDTH/8){1'b1}};
r_wb_cyc_gbl <= 1'b0;
r_wb_cyc_lcl <= 1'b0;
o_wb_stb_gbl <= 0;
o_wb_stb_lcl <= 0;
o_wb_addr <= 0;
wr_cstb <= 1'b0;
last_line_stb <= 1'b0;
end_of_line <= 1'b0;
state <= DC_IDLE;
cyc <= 1'b0;
stb <= 1'b0;
state <= DC_IDLE;
set_vflag <= 1'b0;
// }}}
end else begin
// By default, update the cache from the write 1-clock ago
// c_wr <= (wr_cstb)&&(wr_wtag == wr_vtag);
// c_waddr <= wr_addr[(CS-1):0];
c_wr <= 0;
set_vflag <= 1'b0;
if (!cyc && set_vflag)
c_v[c_waddr[(CS-1):LS]] <= 1'b1;
wr_cstb <= 1'b0;
// end_of_line
// {{{
// Verilator coverage_off
if (LS <= 0)
end_of_line <= 1'b1;
// Verilator coverage_on
else if (!cyc)
end_of_line <= 1'b0;
else if (!end_of_line)
begin
if (i_wb_ack)
end_of_line
<= (c_waddr[(LS-1):0] == {{(LS-2){1'b1}},2'b01});
else
end_of_line
<= (c_waddr[(LS-1):0]=={{(LS-1){1'b1}}, 1'b0});
end
// }}}
// last_line_stb
// {{{
if (!cyc || !stb || (OPT_LOWPOWER && state != DC_READC))
last_line_stb <= (LS <= 0);
// Verilator coverage_off
else if (!i_wb_stall && (LS <= 1))
last_line_stb <= 1'b1;
// Verilator coverage_on
else if (!i_wb_stall)
last_line_stb <= (o_wb_addr[(LS-1):1]=={(LS-1){1'b1}});
else
last_line_stb <= (o_wb_addr[(LS-1):0]=={(LS){1'b1}});
// }}}
//
//
case(state)
DC_IDLE: begin
// {{{
o_wb_we <= 1'b0;
cyc <= 1'b0;
stb <= 1'b0;
r_wb_cyc_gbl <= 1'b0;
r_wb_cyc_lcl <= 1'b0;
o_wb_stb_gbl <= 1'b0;
o_wb_stb_lcl <= 1'b0;
in_cache <= (i_op[0])&&(w_cachable);
if ((i_pipe_stb)&&(i_op[0]))
begin // Write operation
// {{{
state <= DC_WRITE;
if (OPT_LOCAL_BUS && (&i_addr[DATA_WIDTH-1:DATA_WIDTH-8]))
o_wb_addr <= i_addr[2 +: AW];
else
o_wb_addr <= i_addr[WBLSB +: AW];
o_wb_we <= 1'b1;
cyc <= 1'b1;
stb <= 1'b1;
if (OPT_LOCAL_BUS)
begin
r_wb_cyc_gbl <= (i_addr[DATA_WIDTH-1:DATA_WIDTH-8]!=8'hff);
r_wb_cyc_lcl <= (i_addr[DATA_WIDTH-1:DATA_WIDTH-8]==8'hff);
o_wb_stb_gbl <= (i_addr[DATA_WIDTH-1:DATA_WIDTH-8]!=8'hff);
o_wb_stb_lcl <= (i_addr[DATA_WIDTH-1:DATA_WIDTH-8]==8'hff);
end else begin
r_wb_cyc_gbl <= 1'b1;
o_wb_stb_gbl <= 1'b1;