-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.cpp
More file actions
80 lines (63 loc) · 2.48 KB
/
cache.cpp
File metadata and controls
80 lines (63 loc) · 2.48 KB
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
#include<bits/stdc++.h>
using namespace std;
int MEM[1024]={0};
int R[32]={0};
const int DEFAULT_CACHE_SIZE=64; // Default cache size
const int BLOCK_SIZE=4; // Block size (4 bytes)
const int BLOCK_COUNT=DEFAULT_CACHE_SIZE/BLOCK_SIZE; // Number of blocks in cache
struct CacheLine {
int tag;
int data[BLOCK_SIZE]; // Array to hold all data for the block
bool valid;
CacheLine() : tag(-1), valid(false) {
fill_n(data, BLOCK_SIZE, 0); // Initialize data to zero
}
};
class Cache {
private:
vector<CacheLine> cache;
public:
Cache() {
cache.resize(BLOCK_COUNT);
}
void getTagIndexOffset(int address, int &tag, int &index, int &offset) {
int offsetBits = log2(BLOCK_SIZE); // 2 bits for offset
int indexBits = log2(BLOCK_COUNT); // 4 bits for index
// Calculate offset
offset = address & (BLOCK_SIZE - 1); // Last 2 bits for offset
// Calculate index
index = (address / BLOCK_SIZE) % BLOCK_COUNT; // Index into cache
// Calculate tag
tag = address >> (offsetBits + indexBits); // Higher bits for tag
}
int read(int address) {
int tag, index, offset;
getTagIndexOffset(address, tag, index, offset);
if (cache[index].valid && cache[index].tag == tag) {
return cache[index].data[offset]; // Return the specific byte in the block
} else {
// Load block from main memory into cache
cache[index].tag = tag;
for (int i = 0; i < BLOCK_SIZE; ++i) {
cache[index].data[i] = MEM[(address / BLOCK_SIZE) * BLOCK_SIZE + i]; // Load entire block
}
cache[index].valid = true;
return cache[index].data[offset]; // Return the specific byte in the block
}
}
void write(int address, int data) {
int tag, index, offset;
getTagIndexOffset(address, tag, index, offset);
// Load block if it's not valid
if (!cache[index].valid || cache[index].tag != tag) {
cache[index].tag = tag;
for (int i = 0; i < BLOCK_SIZE; ++i) {
cache[index].data[i] = MEM[(address / BLOCK_SIZE) * BLOCK_SIZE + i]; // Load entire block
}
cache[index].valid = true;
}
// Write data to the correct offset in the block
cache[index].data[offset] = data;
MEM[address] = data; // Also write back to main memory
}
};