用c++实现全相联组相联和直接映射的cache模拟器完整代码
时间: 2023-11-23 22:05:41 浏览: 71
基于C语言进行Cache模拟器实验
以下是一个简单的 C++ 编写的 cache 模拟器,支持全相联、组相联和直接映射三种映射方式:
```cpp
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
#define MAX_SIZE 1024
enum Mapping {
DIRECT,
SET_ASSOCIATIVE,
FULLY_ASSOCIATIVE
};
class Cache {
public:
Cache(int size, int block_size, int mapping) {
this->size = size;
this->block_size = block_size;
this->mapping = mapping;
this->num_blocks = size / block_size;
this->num_sets = (mapping == SET_ASSOCIATIVE) ? num_blocks / SET_SIZE : 1;
this->num_ways = (mapping == SET_ASSOCIATIVE) ? SET_SIZE : num_blocks;
this->data = new char[size];
this->valid = new bool[num_blocks];
this->tag = new int[num_blocks];
this->lru = new int[num_sets][SET_SIZE];
memset(this->valid, false, num_blocks * sizeof(bool));
memset(this->tag, 0, num_blocks * sizeof(int));
memset(this->lru, -1, num_sets * SET_SIZE * sizeof(int));
}
~Cache() {
delete[] data;
delete[] valid;
delete[] tag;
delete[] lru;
}
void read(int address) {
int block_index = address / block_size;
int set_index = (mapping == SET_ASSOCIATIVE) ? block_index % num_sets : 0;
int tag_index = block_index / num_sets;
bool hit = false;
int way_index = -1;
for (int i = 0; i < num_ways; i++) {
int index = (mapping == SET_ASSOCIATIVE) ? set_index * SET_SIZE + i : i;
if (valid[index] && tag[index] == tag_index) {
hit = true;
way_index = i;
if (mapping == SET_ASSOCIATIVE) {
update_lru(set_index, i);
}
break;
}
}
if (hit) {
cout << "Cache hit. Block " << block_index << " is in way " << way_index << "." << endl;
} else {
cout << "Cache miss. Block " << block_index << " is not in the cache." << endl;
int evict_index = -1;
if (mapping == DIRECT) {
evict_index = block_index % num_blocks;
} else if (mapping == SET_ASSOCIATIVE) {
int lru_index = lru[set_index][0];
evict_index = set_index * SET_SIZE + lru_index;
} else if (mapping == FULLY_ASSOCIATIVE) {
evict_index = find_lru();
}
if (valid[evict_index]) {
cout << "Evicting block " << evict_index * block_size << "." << endl;
}
read_block(block_index);
valid[evict_index] = true;
tag[evict_index] = tag_index;
if (mapping == SET_ASSOCIATIVE) {
update_lru(set_index, way_index);
}
}
}
private:
int size;
int block_size;
int mapping;
int num_blocks;
int num_sets;
int num_ways;
char *data;
bool *valid;
int *tag;
int (*lru)[SET_SIZE];
const int SET_SIZE = 4;
void read_block(int block_index) {
int start_address = block_index * block_size;
int end_address = start_address + block_size;
for (int i = start_address; i < end_address; i++) {
data[i] = '0' + (i % 10);
}
}
void update_lru(int set_index, int way_index) {
int way_lru = lru[set_index][way_index];
for (int i = 0; i < SET_SIZE; i++) {
if (lru[set_index][i] < way_lru) {
lru[set_index][i]++;
}
}
lru[set_index][way_index] = 0;
}
int find_lru() {
int lru_index = 0;
int max_lru = -1;
for (int i = 0; i < num_blocks; i++) {
if (valid[i] && lru[0][i] > max_lru) {
lru_index = i;
max_lru = lru[0][i];
}
}
return lru_index;
}
};
int main() {
int size, block_size, mapping;
cout << "Enter cache size (in bytes): ";
cin >> size;
cout << "Enter block size (in bytes): ";
cin >> block_size;
cout << "Enter mapping (0: direct, 1: set-associative, 2: fully-associative): ";
cin >> mapping;
Cache cache(size, block_size, mapping);
int address;
while (true) {
cout << "Enter memory address (or -1 to quit): ";
cin >> address;
if (address < 0) {
break;
}
cache.read(address);
}
return 0;
}
```
在使用时,用户需要输入 cache 的参数,然后输入内存地址进行模拟。例如,以下是使用该程序模拟一个大小为 8KB、块大小为 16B、组相联映射的 cache:
```
Enter cache size (in bytes): 8192
Enter block size (in bytes): 16
Enter mapping (0: direct, 1: set-associative, 2: fully-associative): 1
Enter memory address (or -1 to quit): 0
Cache miss. Block 0 is not in the cache.
Reading block 0.
Enter memory address (or -1 to quit): 16
Cache miss. Block 1 is not in the cache.
Reading block 1.
Enter memory address (or -1 to quit): 32
Cache miss. Block 2 is not in the cache.
Reading block 2.
Enter memory address (or -1 to quit): 48
Cache miss. Block 3 is not in the cache.
Reading block 3.
Enter memory address (or -1 to quit): 0
Cache hit. Block 0 is in way 0.
Enter memory address (or -1 to quit): -1
```
阅读全文