-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathl1cache.c
69 lines (53 loc) · 1.7 KB
/
l1cache.c
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
#include <stdio.h>
#include <stdlib.h>
#include "hardware.h"
#include "l1cache.h"
#include "victimcache.h"
int64_t fetchL1Cache(int64_t pa, struct Hardware *hardware, int selector) {
int64_t index = (pa >> 5) & (0x7f);
int64_t tag = (pa >> 12) & (0x1fff);
struct L1Cache *l1;
if (!selector)
l1 = hardware->l1d;
else
l1 = hardware->l1i;
if (l1->tags[index] == tag && l1->valid[index])
return 0; // hit
return 1; //miss
}
void updateL1Cache(int64_t pa, struct Hardware *hardware, int fromVictim, int selector) {
//fromVictim is 1 if hit in victim (==MRU replacement), else 0(==LRU replacement)
int64_t index = (pa >> 5) & (0b1111111);
int64_t tag = (pa >> 12) & (0b1111111111111);
struct L1Cache *l1;
if (!selector)
l1 = hardware->l1d;
else
l1 = hardware->l1i;
int64_t oldpa = 0;
//reconstructing oldpa but without offset bits
if (l1->tags[index] != -1) //-1 => empty => placement => no need to update victim cache
{
oldpa = oldpa | (l1->tags[index] << 12) | (index << 5);
updateVictimCache(oldpa, hardware, !fromVictim);
}
//set tag and valid bits
l1->tags[index] = tag;
l1->valid[index] = 1;
}
void invalidateL1Line(int64_t pa, struct Hardware *hardware) {
int64_t index = (pa >> 5) & (0x7f);
int64_t tag = (pa >> 12) & (0x1fff);
struct L1Cache *l1;
l1 = hardware->l1d;
if (l1->valid[index] && l1->tags[index] == tag) {
l1->valid[index] = 0;
printf("Invalidating in L1\n");
}
l1 = hardware->l1i;
if (l1->valid[index] && l1->tags[index] == tag) {
l1->valid[index] = 0;
printf("Invalidating in L1\n");
}
return;
}