-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cc
568 lines (497 loc) · 14.1 KB
/
cache.cc
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
/* Write back cache for more practical simulation
Copyright (c) 2021 Amano laboratory, Keio University.
Author: Takuya Kojima
This file is part of CubeSim, a cycle accurate simulator for 3-D stacked system.
CubeSim is free software: 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 2 of the License, or
(at your option) any later version.
CubeSim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY 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 CubeSim. If not, see <https://www.gnu.org/licenses/>.
*/
#include "cache.h"
#include "excnames.h"
#include "mapper.h"
//#define CACHE_DEBUG
Cache::Cache(Mapper* mem, unsigned int block_count_, unsigned int block_size_, unsigned int way_size_) :
physmem(mem),
block_count(block_count_),
block_size(block_size_),
way_size(way_size_)
{
//block_size: byte size
blocks = new Entry*[way_size];
word_size = block_size / 4;
for (int i = 0; i < way_size; i++) {
blocks[i] = new Entry[block_count];
for (int j = 0; j < block_count; j++) {
blocks[i][j].data = new uint32[word_size];
blocks[i][j].dirty = false;
blocks[i][j].valid = false;
blocks[i][j].last_access = machine->num_cycles;
}
}
// analyze bit format
offset_len = int(std::log2(block_size));
index_len = int(std::log2(block_count));
// init cache profile
cache_hit_counts = 0;
cache_miss_counts = 0;
cache_wb_counts = 0;
isisolated = false;
// init cache status
status = next_status = CACHE_IDLE;
last_state_update_time = 0;
}
Cache::~Cache()
{
delete [] blocks;
}
void Cache::step()
{
if (machine->num_cycles > last_state_update_time) {
status = next_status;
last_state_update_time = machine->num_cycles;
}
if (status == CACHE_WB || status == CACHE_OP_WB) {
cache_wb();
} else if (status == CACHE_FETCH) {
cache_fetch();
}
}
void Cache::reset_stat()
{
// If exception occurs while cache working, cache must be reset
if (status != CACHE_IDLE) {
status = CACHE_IDLE;
physmem->release_bus(cache_op_state->client);
delete cache_op_state;
}
}
void Cache::addr_separete(uint32 addr, uint32 &tag, uint32 &index, uint32 &offset)
{
tag = addr >> (offset_len + index_len);
index = (addr & ((1 << (index_len + offset_len)) - 1)) >> offset_len;
offset = (addr & ((1 << offset_len) - 1));
}
uint32 Cache::calc_addr(uint32 way, uint32 index)
{
return (blocks[way][index].tag << (offset_len + index_len)) + (index << offset_len);
}
bool Cache::ready(uint32 addr)
{
// just return whether the cache block is available
uint32 index, way, offset;
return cache_hit(addr, index, way, offset);
}
bool Cache::cache_hit(uint32 addr, uint32 &index, uint32 &way, uint32 &offset)
{
// in case of cache hit index, way, offset will be set
uint32 tag;
addr_separete(addr, tag, index, offset);
for (way = 0; way < way_size; way++) {
if (blocks[way][index].tag == tag && blocks[way][index].valid) {
#if defined(CACHE_DEBUG)
printf("Cache Hit addr(0x%x)\n", addr);
#endif
return true;
}
}
#if defined(CACHE_DEBUG)
printf("Cache Miss addr(0x%x)\n", addr);
#endif
return false;
}
void Cache::request_block(uint32 addr, int mode, DeviceExc* client)
{
int least_recent_used_way = 0;
bool find = false;
uint32 tag, index, way, offset;
addr_separete(addr, tag, index, offset);
//if cache is working or bus is busy, request is ignored
if (status == CACHE_IDLE && physmem->acquire_bus(client)) {
//find free block or LRU block
for (way = 0; way < way_size; way++) {
if (blocks[least_recent_used_way][index].last_access >
blocks[way][index].last_access) {
//update
least_recent_used_way = way;
}
if (!blocks[way][index].valid) {
find = true;
break;
}
}
if (!find) { // in case of no free block
way = least_recent_used_way;
//check if WB is needed
if (!isisolated && mode != INSTFETCH && blocks[way][index].dirty) {
next_status = CACHE_WB;
#if defined(CACHE_DEBUG)
fprintf(stderr, "WB cache block way(%d) index(%d) is used for addr(0x%x)\n", way, index, addr);
#endif
} else {
next_status = CACHE_FETCH;
}
} else {
next_status = CACHE_FETCH;
}
#if defined(CACHE_DEBUG)
if (next_status == CACHE_FETCH) {
fprintf(stderr, "cache block way(%d) index(%d) is used for addr(0x%x)\n", way, index, addr);
}
#endif
// regist replaced cache block & status
cache_op_state = new CacheOpState{word_size, addr, way, index, mode, false, client};
cache_miss_counts++;
}
}
void Cache::cache_wb()
{
uint32 wb_addr = calc_addr(cache_op_state->way, cache_op_state->index);
wb_addr += (word_size - cache_op_state->counter) * 4;
unsigned int way = cache_op_state->way;
unsigned int index = cache_op_state->index;
// request for access at first
if (cache_op_state->counter == word_size) {
for (int i = 0; i < word_size; i++) {
physmem->request_word(wb_addr + (4 * i), DATASTORE, cache_op_state->client);
}
}
// if access is ready, write back the data
if (physmem->ready(wb_addr, DATASTORE, cache_op_state->client)) {
physmem->store_word(wb_addr,
physmem->host_to_mips_word(blocks[way][index].data[word_size - cache_op_state->counter]),
cache_op_state->client);
if (--cache_op_state->counter == 0) {
//finish write back
if (status == CACHE_OP_WB) {
//no need to fetch block
blocks[way][index].dirty = false;
if (cache_op_state->last_invalidate) {
blocks[way][index].valid = false;
}
next_status = CACHE_IDLE;
delete cache_op_state;
physmem->release_bus(cache_op_state->client);
} else {
next_status = CACHE_FETCH;
cache_op_state->counter = word_size;
}
cache_wb_counts++;
}
}
}
void Cache::cache_fetch()
{
unsigned int tag, way, index, offset;
uint32 fetch_addr = (cache_op_state->requested_addr & ~((1 << offset_len) - 1))
+ ((word_size - cache_op_state->counter) * 4);
way = cache_op_state->way;
index = cache_op_state->index;
int mode = cache_op_state->mode == INSTFETCH ? INSTFETCH : DATALOAD;
// request for access at first
if (cache_op_state->counter == word_size) {
for (int i = 0; i < word_size; i++) {
physmem->request_word(fetch_addr + (4 * i), mode, cache_op_state->client);
}
}
// if access is ready, fetch the data
if (physmem->ready(fetch_addr, mode, cache_op_state->client)) {
blocks[way][index].data[word_size - cache_op_state->counter] =
physmem->host_to_mips_word(physmem->fetch_word(fetch_addr, mode, cache_op_state->client));
if (--cache_op_state->counter == 0) {
// finish cache fetch
next_status = CACHE_IDLE;
addr_separete(fetch_addr, tag, index, offset);
blocks[way][index].tag = tag;
blocks[way][index].valid = true;
blocks[way][index].dirty = false;
delete cache_op_state;
physmem->release_bus(cache_op_state->client);
}
}
}
bool Cache::exec_cache_op(uint16 opcode, uint32 addr, DeviceExc* client)
{
uint32 tag, index, way, offset;
uint32 least_recent_used_way = 0;
int mode = DATALOAD;
bool last_invalidate = false;
bool stall = false;
bool find;
//if it causes cpu stall, return false
if (status == CACHE_IDLE && physmem->acquire_bus(client)) {
switch (opcode) {
//invalidate by index
case ICACHE_OP_IDX_INV:
mode = INSTFETCH;
case DCACHE_OP_IDX_INV:
fprintf(stderr, "cache index invalidate not implemented\n");
break;
//write back & invalidate by index
case DCACHE_OP_IDX_WB:
fprintf(stderr, "cache index writeback not implemented\n");
break;
case DCACHE_OP_IDX_WBINV:
fprintf(stderr, "cache index writeback, invalidate not implemented\n");
break;
//load tag by index
case ICACHE_OP_IDX_LTAG:
mode = INSTFETCH;
case DCACHE_OP_IDX_LTAG:
fprintf(stderr, "cache tag load not implemented\n");
break;
//store tag by inedx
case ICACHE_OP_IDX_STAG:
mode = INSTFETCH;
case DCACHE_OP_IDX_STAG:
fprintf(stderr, "cache tag store not implemented\n");
break;
//force write back by index
case DCACHE_OP_IDX_FWB:
fprintf(stderr, "cache index force writeback not implemented\n");
break;
//force write back & invalidate by index
case DCACHE_OP_IDX_FWBINV:
fprintf(stderr, "cache index force writeback, invalidate not implemented\n");
break;
//setline
case DCACHE_OP_SETLINE:
if (!cache_hit(addr, index, way, offset)) {
addr_separete(addr, tag, index, offset);
//find free block or oldest block
for (way = 0, find = false; way < way_size; way++) {
if (!blocks[way][index].valid) {
find = true;
break;
}
if (blocks[least_recent_used_way][index].last_access >
blocks[way][index].last_access) {
//update
least_recent_used_way = way;
}
}
if (!find) {
// block replace
way = least_recent_used_way;
}
if (blocks[way][index].dirty) {
next_status = CACHE_OP_WB;
} else {
blocks[way][index].valid = true;
//overwrite tag
blocks[way][index].tag = tag;
blocks[way][index].dirty = false;
}
}
break;
//invalidate by cache hit
case ICACHE_OP_HIT_INV:
mode = INSTFETCH;
case DCACHE_OP_HIT_INV:
if (cache_hit(addr, index, way, offset)) {
blocks[way][index].valid = false;
}
break;
//(force) write back (&invalidate) by cache hit
case DCACHE_OP_HIT_WBINV:
case DCACHE_OP_HIT_FWBINV:
last_invalidate = true;
case DCACHE_OP_HIT_WB:
case DCACHE_OP_HIT_FWB:
if (cache_hit(addr, index, way, offset)) {
if (blocks[way][index].dirty) {
next_status = CACHE_OP_WB;
} else if (opcode == DCACHE_OP_HIT_FWB || opcode == DCACHE_OP_HIT_INV) {
next_status = CACHE_OP_WB;
}
}
break;
//change
case DCACHE_OP_CHANGE:
fprintf(stderr, "cache change not implemented\n");
break;
//reverse change
case DCACHE_OP_RCHANGE:
fprintf(stderr, "cache reverse change not implemented\n");
break;
}
if (next_status == CACHE_OP_WB) {
cache_op_state = new CacheOpState{word_size, addr, way, index, mode, last_invalidate, client};
stall = true;
}
} else {
//cache is working
stall = true;
}
return !stall;
}
uint32 Cache::fetch_word(uint32 addr, int32 mode, DeviceExc *client)
{
uint32 offset;
uint32 way, index;
Cache::Entry *entry = NULL;
if (addr % 4 != 0) {
client->exception(AdEL,mode);
return 0xffffffff;
}
if (!cache_hit(addr, index, way, offset)) {
return 0xffffffff;
} else {
cache_hit_counts++;
}
//get data
entry = &blocks[way][index];
if (!entry->valid) {
return 0xffffffff;
}
if (isisolated) {
printf("Isolated word read returned 0x%x\n", entry->data[offset>>2]);
}
entry->last_access = machine->num_cycles;
return entry->data[offset>>2];
}
uint16 Cache::fetch_halfword(uint32 addr, DeviceExc *client)
{
uint32 offset, index, way;
Cache::Entry *entry = NULL;
if (addr % 2 != 0) {
client->exception(AdEL,DATALOAD);
return 0xffff;
}
if (!cache_hit(addr, index, way, offset)) {
return 0xffff;
} else {
cache_hit_counts++;
}
entry = &blocks[way][index];
if (!entry->valid) {
return 0xffff;
}
//addr check
uint32 n;
n = (offset >> 1) & 0x1;
if (physmem->byteswapped)
n = 1 - n;
if (isisolated) {
printf("Isolated word read returned 0x%x\n", ((uint16 *)(&entry->data[offset>>2]))[n]);
}
entry->last_access = machine->num_cycles;
return ((uint16 *)(&entry->data[offset>>2]))[n];
}
uint8 Cache::fetch_byte(uint32 addr, DeviceExc *client)
{
uint32 offset, index, way;
Cache::Entry *entry = NULL;
if (!cache_hit(addr, index, way, offset)) {
return 0xff;
} else {
cache_hit_counts++;
}
entry = &blocks[way][index];
if (!entry->valid) {
return 0xff;
}
uint32 n;
n = (offset & 0x3);
if (physmem->byteswapped)
n = 3 - n;
if (isisolated) {
printf("Isolated word read returned 0x%x\n", ((uint8 *)(&entry->data[offset>>2]))[n]);
}
entry->last_access = machine->num_cycles;
return ((uint8 *)(&entry->data[offset>>2]))[n];
}
void Cache::store_word(uint32 addr, uint32 data, DeviceExc *client)
{
uint32 offset, way, index;
Cache::Entry *entry = NULL;
if (addr % 4 != 0) {
client->exception(AdES,DATASTORE);
return;
}
if (!cache_hit(addr, index, way, offset)) {
return;
} else {
cache_hit_counts++;
}
entry = &blocks[way][index];
if (!entry->valid) {
return;
}
if (isisolated) {
printf("Write(%d) w/isolated cache 0x%x -> 0x%x\n", 4, data, addr);
}
entry->data[offset>>2] = data;
entry->dirty = true;
entry->last_access = machine->num_cycles;
return;
}
void Cache::store_halfword(uint32 addr, uint16 data, DeviceExc *client)
{
uint32 offset, way, index;
Cache::Entry *entry = NULL;
if (addr % 2 != 0) {
client->exception(AdES,DATASTORE);
return;
}
if (!cache_hit(addr, index, way, offset)) {
return ;
} else {
cache_hit_counts++;
}
entry = &blocks[way][index];
if (!entry->valid) {
return;
}
if (isisolated) {
printf("Write(%d) w/isolated cache 0x%x -> 0x%x\n", 4, data, addr);
}
uint32 n;
n = (offset >> 1) & 0x1;
if (physmem->byteswapped)
n = 1 - n;
((uint16 *)(&entry->data[offset>>2]))[n] = (uint16)data;
entry->dirty = true;
entry->last_access = machine->num_cycles;
return;
}
void Cache::store_byte(uint32 addr, uint8 data, DeviceExc *client)
{
uint32 offset, way, index;
Cache::Entry *entry = NULL;
if (!cache_hit(addr, index, way, offset)) {
return ;
} else {
cache_hit_counts++;
}
entry = &blocks[way][index];
if (!entry->valid) {
return;
}
if (isisolated) {
printf("Write(%d) w/isolated cache 0x%x -> 0x%x\n", 4, data, addr);
}
uint32 n;
n = offset & 0x3;
if (physmem->byteswapped)
n = 3 - n;
((uint8 *)(&entry->data[offset>>2]))[n] = (uint8)data;
entry->dirty = true;
entry->last_access = machine->num_cycles;
return;
}
void Cache::report_prof()
{
uint32 cache_access = cache_miss_counts + cache_hit_counts;
fprintf(stderr, "\tAccess Count %d\n", cache_access);
fprintf(stderr, "\tCache Miss Ratio %.5f%%\n",
(double)cache_miss_counts / (double)cache_access * 100.0);
fprintf(stderr, "\twrite back ratio %.5f%%\n",
(double)cache_wb_counts / (double)cache_miss_counts * 100.0);
}