-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.v
758 lines (638 loc) · 18.9 KB
/
cache.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
//Carson, Castro, Kempis
//Project 3
module cache(clock, reset_n, data_cpu, data_mem, addr_cpu, addr_mem,
rd_cpu, wr_cpu, rd_mem, wr_mem, stall_cpu, ready_mem);
// Parameters
parameter AWIDTH = 27; //Address Bus width
parameter DWIDTH = 10; //Data Bus Width
parameter CACHELINES = 16;
parameter BLOCKSIZE = 10;
parameter NUMOFSETS = 11;
parameter VALIDBIT = 1;
parameter DIRTYBIT = 1;
parameter USEDBIT = 1;
parameter TAGWIDTH = 6;
input clock;
input reset_n; //Active Low Reset Signal Input
inout [DWIDTH-1:0] data_cpu; //Data bus from CPU
inout [DWIDTH-1:0] data_mem; //Data bus to Main Memory
input [AWIDTH-1:0] addr_cpu; //Address bus from CPU
output reg [AWIDTH-1:0] addr_mem; //Address bus to Main Memory
input rd_cpu; //Active High Read signal from CPU
input wr_cpu; //Active High Write signal from CPU
output reg rd_mem; //Active High Read signal to Main Memory
output reg wr_mem; //Active High Write signal to Main Memory
output reg stall_cpu; //Active High Stall Signal to CPU
input ready_mem; //Active High Ready signal from Main memory
// State Machine States
localparam IDLE = 3'd0,
READ = 3'd1,
WRITE = 3'd2,
READMM = 3'd3,
WAITFORMM = 3'd4,
UPDATEMM = 3'd5,
UPDATECACHE = 3'd6;
wire [TAGWIDTH-1:0] tagdata;
wire [2:0] index;
wire [1:0] bytsel;
reg [DWIDTH-1:0] rdata_byte;
reg [DWIDTH-1:0] wdata_byte;
reg [DWIDTH-1:0] wmem_byte;
reg [(DWIDTH*BLOCKSIZE)-1:0] rmem_10bits;
reg [(DWIDTH*BLOCKSIZE)-1:0] wmem_10bits;
reg [3:0] count; //To count byte transfer between Cache and memory during read and write memory operation, used as shift register.
reg rdwr; // If read then '1', if write the '0'
reg we0; //Active High Write Enable for DATA RAM 0
reg we1; //Active High Write Enable for DATA RAM 1
reg wet0; //Active High Write Enable for TAG RAM 0
reg wet1; //Active High Write Enable for TAG RAM 1
reg update_flag;
wire hit, hit_w0, hit_w1;
wire valid, vw0, vw1;
wire uw0, uw1;
wire dirty, dw0, dw1;
wire [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] rtag0; //14-bits
wire [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] rtag1;
wire [(DWIDTH*BLOCKSIZE)-1:0] rdata0;
wire [(DWIDTH*BLOCKSIZE)-1:0] rdata1;
wire [DWIDTH-1:0] bytew0;
wire [DWIDTH-1:0] bytew1;
reg [(DWIDTH*BLOCKSIZE)-1:0] rdata;
reg [(DWIDTH*BLOCKSIZE)-1:0] wdata;
reg [(DWIDTH*BLOCKSIZE)-1:0] strdata0;
reg [(DWIDTH*BLOCKSIZE)-1:0] strdata1;
reg [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] wtag0;
reg [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] wtag1;
reg [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] strtag0;
reg [(VALIDBIT+USEDBIT+DIRTYBIT+TAGWIDTH-1):0] strtag1;
reg [AWIDTH-1:0] addrlatch;
// State Variables
reg [2:0] state;
// Combinational Logic
assign tagdata = (state == IDLE) ? addr_cpu[15:5] : addrlatch[15:5];
assign index = (state == IDLE) ? addr_cpu[4:2] : addrlatch[4:2];
assign bytsel = (state == IDLE) ? addr_cpu[1:0] : addrlatch[1:0];
assign vw0 = rtag0[13];
assign vw1 = rtag1[13];
assign valid = vw0 & vw1;
assign uw0 =rtag0[12];
assign uw1 =rtag1[12];
assign dw0 = rtag0[11];
assign dw1 = rtag1[11];
assign dirty = dw0 | dw1;
assign hit_w0 = vw0 & (tagdata == rtag0[10:0]);
assign hit_w1 = vw1 & (tagdata == rtag1[10:0]);
assign hit = hit_w0 | hit_w1;
assign bytew0 = (bytsel == 2'd0) ? rdata0[7:0] :((bytsel == 2'd1) ? rdata0[15:8] :((bytsel == 2'd2) ? rdata0[23:16] : rdata0[31:24]));
assign bytew1 = (bytsel == 2'd0) ? rdata1[7:0] :((bytsel == 2'd1) ? rdata1[15:8] :((bytsel == 2'd2) ? rdata1[23:16] : rdata1[31:24]));
assign data_cpu = (!wr_cpu) ? rdata_byte : 8'dZ;
assign data_mem = wr_mem ? wmem_byte : 8'dZ;
// Cache Controller State Machine and Logic
always@(posedge clock or negedge reset_n)
begin
if(!reset_n)
begin
addrlatch <= 'd0;
addr_mem <= 'd0;
rd_mem <= 'd0;
wr_mem <= 'd0;
stall_cpu <= 'd0;
state <= IDLE;
rdata_byte<= 'd0;
wdata_byte<= 'd0;
wmem_byte <= 'd0;
rmem_10bits<= 'd0;
wmem_10bits<= 'd0;
wdata <= 'd0;
wtag0 <= 'd0;
wtag1 <= 'd0;
we0 <= 1'd0;
we1 <= 1'd0;
wet0 <= 1'd0;
wet1 <= 1'd0;
rdwr <= 1'd1;
strdata0 <= 'd0;
strdata1 <= 'd0;
strtag0 <= 'd0;
strtag1 <= 'd0;
rdata <= 'd0;
count <= 4'd0;
update_flag<= 1'd0;
end
else
begin
case(state)
IDLE : begin
addrlatch <= addr_cpu;
we0 <= 1'd0;
we1 <= 1'd0;
wet0 <= 1'd0;
wet1 <= 1'd0;
stall_cpu <= 1'd0;
rd_mem <= 1'd0;
wr_mem <= 1'd0;
rdata_byte<= 8'd0;
wmem_byte <= 8'd0;
rmem_10bits<= 'd0;
wdata <= 'd0;
wtag0 <= 'd0;
wtag1 <= 'd0;
update_flag<= 1'd0;
count <= 4'd0;
if(rd_cpu)
begin
state <= READ;
rdwr <= 1'd1;
end
else if(wr_cpu)
begin
state <= WRITE;
wdata_byte <= data_cpu;
rdwr <= 1'd0;
end
else state <= state;
end
READ : begin
we0 <= 1'd0;
we1 <= 1'd0;
case(hit)
1'd0: begin
strtag0 <= rtag0;
strtag1 <= rtag1;
strdata0 <= rdata0;
strdata1 <= rdata1;
stall_cpu <= 1'd1;
wet0 <= 1'd0;
wet1 <= 1'd0;
if(ready_mem)
if(valid & dirty)
state <= UPDATEMM;
else
state <= READMM;
else
state <= state;
end
1'd1: begin
state <= IDLE;
wet0 <= 1'd1;
wet1 <= 1'd1;
stall_cpu <= 1'd0;
if(hit_w0)
begin
rdata_byte <= bytew0;
if(uw0)
wtag0 <= rtag0;
else
wtag0 <= {rtag0[13],1'd1,rtag0[11:0]};
if(uw1)
wtag1 <= rtag1;
else
wtag1 <= {rtag1[13],1'd1,rtag1[11:0]};
end
else
begin
rdata_byte <= bytew1;
if(uw1)
wtag1 <= {rtag1[13],1'd0,rtag1[11:0]};
else
wtag1 <= rtag1;
if(uw0)
wtag0 <= {rtag0[13],1'd0,rtag0[11:0]};
else
wtag0 <= rtag0;
end
end
endcase
end
WRITE : begin
case(hit)
1'd0: begin
strtag0 <= rtag0;
strtag1 <= rtag1;
strdata0 <= rdata0;
strdata1 <= rdata1;
stall_cpu <= 1'd1;
if(ready_mem)
if(valid & dirty)
state <= UPDATEMM;
else
state <= READMM;
else
state <= state;
end
1'd1: begin
state <= IDLE;
wet0 <= 1'd1;
wet1 <= 1'd1;
stall_cpu <= 1'd0;
if(hit_w0)
begin
we0 <= 1'd1;
case(bytsel)
2'd0: wdata <= {rdata0[31:8],wdata_byte};
2'd1: wdata <= {rdata0[31:16],wdata_byte,rdata0[7:0]};
2'd2: wdata <= {rdata0[31:24],wdata_byte,rdata0[15:0]};
2'd3: wdata <= {wdata_byte,rdata0[23:0]};
endcase
if(uw0)
wtag0 <= {rtag0[13:12],1'd1,rtag0[10:0]};
else
wtag0 <= {rtag0[13],1'd1,1'd1,rtag0[10:0]};
if(uw1)
wtag1 <= rtag1;
else
wtag1 <= {rtag1[13],1'd1,rtag1[11:0]};
end
else
begin
we1 <= 1'd1;
case(bytsel)
2'd0: wdata <= {rdata1[31:8],wdata_byte};
2'd1: wdata <= {rdata1[31:16],wdata_byte,rdata1[7:0]};
2'd2: wdata <= {rdata1[31:24],wdata_byte,rdata1[15:0]};
2'd3: wdata <= {wdata_byte,rdata1[23:0]};
endcase
if(uw1)
wtag1 <= {rtag1[13],1'd0,1'd1,rtag1[10:0]};
else
wtag1 <= {rtag1[13:12],1'd1,rtag1[10:0]};
if(uw0)
wtag0 <= {rtag0[13],1'd0,rtag0[11:0]};
else
wtag0 <= rtag0;
end
end
endcase
end
READMM : begin
addr_mem <= {addrlatch[15:2],2'd0};
update_flag<= 1'd0;
if(ready_mem)
begin
rd_mem <= 1'd1;
state <= WAITFORMM;
end
else
begin
rd_mem <= 1'd0;
state <= state;
end
end
WAITFORMM : begin
if(ready_mem)
begin
// if(rdwr)
// state <= UPDATECACHE;
// else
// begin
if(update_flag)
state <= READMM;
else
state <= UPDATECACHE;
// end
rd_mem <= 1'd0;
wr_mem <= 1'd0;
end
else
begin
if(!rdwr)
begin
wmem_byte <= wmem_10bits[7:0];
wmem_10bits<= {8'd0,wmem_10bits[31:8]};
end
state <= state;
end
end
UPDATEMM : begin
update_flag<= 1'd1;
if(uw0)
begin
addr_mem <= {strtag1[10:0],addrlatch[4:2],2'd0};
wmem_10bits <= strdata1;
end
else
begin
addr_mem <= {strtag0[10:0],addrlatch[4:2],2'd0};
wmem_10bits <= strdata0;
end
if(ready_mem)
begin
wr_mem <= 1'd1;
state <= WAITFORMM;
end
else
begin
wr_mem <= 1'd0;
state <= state;
end
end
UPDATECACHE: begin
update_flag<= 1'd0;
if(count!=4'b1111)
begin
rmem_10bits <= {data_mem,rmem_10bits[31:8]};
count <= {1'd1,count[3:1]};
end
else
begin
wdata <= rmem_10bits;
state <= IDLE;
/* if(rdwr)
state <= READ;
else
state <= WRITE; */
if(uw0)
begin
wtag1 <= {1'd1,1'd0,1'd0,addrlatch[15:5]};
wtag0 <= {strtag0[13],1'd0,strtag0[11:0]};
we1 <= 1'd1;
we0 <= 1'd0;
wet0 <= 1'd1;
wet1 <= 1'd1;
end
else
begin
wtag0 <= {1'd1,1'd1,1'd0,addrlatch[15:5]};
wtag1 <= {strtag1[13],1'd1,strtag1[11:0]};
we0 <= 1'd1;
we1 <= 1'd0;
wet0 <= 1'd1;
wet1 <= 1'd1;
end
end
end
default: begin
addrlatch <= 'd0;
addr_mem <= 'd0;
rd_mem <= 'd0;
wr_mem <= 'd0;
stall_cpu <= 'd0;
state <= IDLE;
rdata_byte<= 'd0;
wdata_byte<= 'd0;
wmem_byte <= 'd0;
rmem_10bits<= 'd0;
wmem_10bits<= 'd0;
wdata <= 'd0;
wtag0 <= 'd0;
wtag1 <= 'd0;
we0 <= 1'd0;
we1 <= 1'd0;
wet0 <= 1'd0;
wet1 <= 1'd0;
rdwr <= 1'd1;
strdata0 <= 'd0;
strdata1 <= 'd0;
strtag0 <= 'd0;
strtag1 <= 'd0;
rdata <= 'd0;
count <= 4'd0;
end
endcase
end
end
endmodule
module ram_sync_read_d0(clock, addr, din, we, dout);
parameter AWIDTH = 3;
parameter DWIDTH = 32;
localparam DEPTH = 1 << AWIDTH;
// Memory Array Decaration
input clock; //Input signal for clock
input [AWIDTH-1:0] addr; //Parameterized Address bus input
input [DWIDTH-1:0] din; //Parameterized Data bus input
input we; //Write Enable (input HIGH = write, LOW = read)
output [DWIDTH-1:0] dout; //Parameterized Data bus output
reg [DWIDTH-1:0] mem_array [0:DEPTH-1];
// Memory initialization
initial
begin
$readmemb("dram0.txt", mem_array);
end
reg [AWIDTH-1:0] rd_addr;
always@(posedge clock)
begin
if(we) mem_array[addr] <= din;
rd_addr <= addr;
end
assign dout = mem_array[rd_addr];
endmodule
module ram_sync_read_d1(clock, addr, din, we, dout);
parameter AWIDTH = 3;
parameter DWIDTH = 32;
localparam DEPTH = 1 << AWIDTH;
// Memory Array Decaration
input clock; //Input signal for clock
input [AWIDTH-1:0] addr; //Parameterized Address bus input
input [DWIDTH-1:0] din; //Parameterized Data bus input
input we; //Write Enable (input HIGH = write, LOW = read)
output [DWIDTH-1:0] dout; //Parameterized Data bus output
reg [DWIDTH-1:0] mem_array [0:DEPTH-1];
// Memory initialization
initial
begin
$readmemb("dram1.txt", mem_array);
end
reg [AWIDTH-1:0] rd_addr;
always@(posedge clock)
begin
if(we) mem_array[addr] <= din;
rd_addr <= addr;
end
assign dout = mem_array[rd_addr];
endmodule
module ram_sync_read_t0(clock, addr, din, we, dout);
parameter AWIDTH = 3;
parameter DWIDTH = 32;
localparam DEPTH = 1 << AWIDTH;
input clock; //Input signal for clock
input [AWIDTH-1:0] addr; //Parameterized Address bus input
input [DWIDTH-1:0] din; //Parameterized Data bus input
input we; //Write Enable (input HIGH = write, LOW = read)
output [DWIDTH-1:0] dout; //Parameterized Data bus output
reg [DWIDTH-1:0] mem_array [0:DEPTH-1];
// Memory initialization
initial
begin
$readmemb("tagRam0.txt", mem_array);
end
reg [AWIDTH-1:0] rd_addr;
always@(posedge clock)
begin
if(we) mem_array[addr] <= din;
rd_addr <= addr;
end
assign dout = mem_array[rd_addr];
endmodule
module ram_sync_read_t1(clock, addr, din, we, dout);
parameter AWIDTH = 3;
parameter DWIDTH = 32;
localparam DEPTH = 1 << AWIDTH;
input clock; //Input signal for clock
input [AWIDTH-1:0] addr; //Parameterized Address bus input
input [DWIDTH-1:0] din; //Parameterized Data bus input
input we; //Write Enable (input HIGH = write, LOW = read)
output [DWIDTH-1:0] dout; //Parameterized Data bus output
reg [DWIDTH-1:0] mem_array [0:DEPTH-1];
// Memory initialization
initial
begin
$readmemb("tagRam1.txt", mem_array);
end
reg [AWIDTH-1:0] rd_addr;
always@(posedge clock)
begin
if(we) mem_array[addr] <= din;
rd_addr <= addr;
end
assign dout = mem_array[rd_addr];
endmodule
//Testbench
module cache_tb;
reg clock;
reg reset_n;
reg [15:0] addr_cpu;
reg rd_cpu;
reg wr_cpu;
reg ready_mem;
wire [15:0] addr_mem;
wire rd_mem;
wire wr_mem;
wire stall_cpu;
wire [7:0] data_cpu;
wire [7:0] data_mem;
reg [7:0] dcpu;
reg [7:0] wcpu;
reg [7:0] dmem;
reg [7:0] wmem;
cache uut (
.clock(clock),
.reset_n(reset_n),
.data_cpu(data_cpu),
.data_mem(data_mem),
.addr_cpu(addr_cpu),
.addr_mem(addr_mem),
.rd_cpu(rd_cpu),
.wr_cpu(wr_cpu),
.rd_mem(rd_mem),
.wr_mem(wr_mem),
.stall_cpu(stall_cpu),
.ready_mem(ready_mem)
);
//Connecting the modules
ram_sync_read_d0 dr0 (
.clock(clock),
.addr(index),
.din(wdata),
.we(we0),
.dout(rdata0)
);
ram_sync_read_d1 dr1 (
.clock(clock),
.addr(index),
.din(wdata),
.we(we1),
.dout(rdata1)
);
ram_sync_read_t0 tr0 (
.clock(clock),
.addr(index),
.din(wtag0),
.we(wet0),
.dout(rtag0)
);
ram_sync_read_t0 tr1 (
.clock(clock),
.addr(index),
.din(wtag0),
.we(wet0),
.dout(rtag1)
);
assign data_cpu = wr_cpu? wcpu : 8'dZ;
assign data_mem = !wr_mem? dmem : 8'dZ;
//Clock generator block
initial begin
clock = 1'd0;
forever
#10 clock = ~clock;
end
task delay;
begin
@(negedge clock);
end
endtask
initial begin
// Initialize Inputs
reset_n = 0; addr_cpu = 0; rd_cpu = 0;
wr_cpu = 0; ready_mem = 1; wcpu = 0;
repeat(4)
delay;
reset_n = 1;
delay; delay;
rd_cpu = 1'd1; addr_cpu = 16'b0000_0000_1001_0011;
dcpu = data_cpu;
delay;
rd_cpu = 1'd1; dcpu = data_cpu;
delay;
rd_cpu = 1'd0;
delay; delay;
wr_cpu = 1'd1; wcpu = 8'h23;
addr_cpu = 16'b0000_0000_1001_0011;
delay; delay;
wr_cpu = 1'd0;
delay; delay;
rd_cpu = 1'd1; addr_cpu = 16'b0000_0000_1001_0011;
dcpu = data_cpu;
delay;
rd_cpu = 1'd1; dcpu = data_cpu;
delay;
rd_cpu = 1'd0;
delay; delay;
rd_cpu = 1'd1; addr_cpu = 16'b1100_0000_1000_1011;
dcpu = data_cpu;
delay;
rd_cpu = 1'd1; dcpu = data_cpu;
@(posedge rd_mem);
ready_mem = 0;
repeat(4)
delay;
ready_mem = 1;
delay;
dmem = 8'h11;
delay;
dmem = 8'h22;
delay;
dmem = 8'h33;
delay;
dmem = 8'h44;
delay; delay; delay; delay;
rd_cpu = 1'd0;
delay;
rd_cpu = 1'd1; addr_cpu = 16'b1100_0001_0001_1010;
dcpu = data_cpu;
delay;
rd_cpu = 1'd1; dcpu = data_cpu;
@(posedge wr_mem);
ready_mem = 0;
repeat(4)
delay;
ready_mem = 1;
@(posedge rd_mem);
ready_mem = 0;
repeat(4)
delay;
ready_mem = 1;
delay;
dmem = 8'hAA;
delay;
dmem = 8'hBB;
delay;
dmem = 8'hCC;
delay;
dmem = 8'hDD;
delay; delay; delay; delay;
rd_cpu = 1'd0;
repeat(10)
delay;
#100 $finish;
end
endmodule