This commit is contained in:
LYC 2024-10-30 14:06:32 +08:00
parent dacff5ba53
commit 3e95c6dcd6
4 changed files with 10 additions and 22 deletions

View File

@ -39,7 +39,7 @@ double CacheConfig::getMP()
return 20 + 0.5 * (blockSize / 16.0); return 20 + 0.5 * (blockSize / 16.0);
} }
int CacheConfig::log2_floor(uint32_t x) int CacheConfig::log2(uint32_t x)
{ {
return x == 0 ? -1 : 31 - __builtin_clz(x); return x == 0 ? -1 : 31 - __builtin_clz(x);
} }
@ -62,8 +62,8 @@ uint32_t CacheConfig::getAs()
void CacheConfig::setupTIO() void CacheConfig::setupTIO()
{ {
// setup t, i, b // setup t, i, b
o = log2_floor(blockSize); o = log2(blockSize);
i = log2_floor(s / (blockSize * assoc)); i = log2(s / (blockSize * assoc));
t = 32 - o - i; t = 32 - o - i;
om = (1 << o) - 1; om = (1 << o) - 1;
@ -72,7 +72,7 @@ void CacheConfig::setupTIO()
if (!victimSize) if (!victimSize)
return; return;
vo = log2_floor(blockSize); vo = log2(blockSize);
vi = 0; vi = 0;
vt = 32 - vo - vi; vt = 32 - vo - vi;
victimAsso = victimSize / blockSize; victimAsso = victimSize / blockSize;
@ -81,7 +81,7 @@ void CacheConfig::setupTIO()
// vim = ((1<<vi)-1) << vo; // vim = ((1<<vi)-1) << vo;
} }
CacheIndex CacheConfig::VictimAddrParser(uint32_t addr) CacheIndex CacheConfig::parserVictimAddr(uint32_t addr)
{ {
uint32_t offset = addr & vom; uint32_t offset = addr & vom;
// uint32_t index = (addr & vim) >> vo; // uint32_t index = (addr & vim) >> vo;
@ -129,7 +129,6 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
auto addr = config->AddrParser(rawAddr); auto addr = config->AddrParser(rawAddr);
auto s = &cache[addr.index]; auto s = &cache[addr.index];
// Find in Cache
auto cl = find_if( auto cl = find_if(
s->begin(), s->begin(),
s->end(), s->end(),
@ -140,7 +139,6 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
if (cl != s->end()) if (cl != s->end())
{ {
// Cache Hit!
for (auto &b : (*s)) for (auto &b : (*s))
b.lru += (b.lru < cl->lru); b.lru += (b.lru < cl->lru);
cl->lru = 0; cl->lru = 0;
@ -192,11 +190,8 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
} }
else else
{ {
// Has Victim Cache: Replace with Victim
// Miss Write or Read Miss, Go to Next Level auto vaddr = config->parserVictimAddr(rawAddr);
// 1. Find it from Victim Cache
auto vaddr = config->VictimAddrParser(rawAddr);
auto vcp = &victimCache; auto vcp = &victimCache;
auto vic = find_if( auto vic = find_if(
@ -209,13 +204,12 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
if (vic != vcp->end()) if (vic != vcp->end())
{ {
// V. Cache Hit from Victim!
auto tmp = *vic; auto tmp = *vic;
if (cl->valid) if (cl->valid)
{ {
for (auto &b : *vcp) for (auto &b : *vcp)
b.lru += (b.lru < tmp.lru); b.lru += (b.lru < tmp.lru);
*vic = {config->VictimAddrParser(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true}; *vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true};
} }
for (auto &b : (*s)) for (auto &b : (*s))
@ -225,17 +219,14 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
return; return;
} }
// X. Victim Cache Miss!
isWrite ? writeMiss++ : readMiss++; isWrite ? writeMiss++ : readMiss++;
if (!cl->valid) if (!cl->valid)
{ {
if (nextCache != nullptr) if (nextCache != nullptr)
{ {
// 2. Find it from Next Level
nextCache->useCache(rawAddr, false); nextCache->useCache(rawAddr, false);
} }
// Empty Level 1
for (auto &b : (*s)) for (auto &b : (*s))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite, true}; *cl = {addr.tag, 0, isWrite, true};
@ -252,7 +243,6 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
if (vic == vcp->end()) if (vic == vcp->end())
{ {
// Vic Full, find one to deprecate
vic = max_element( vic = max_element(
vcp->begin(), vcp->begin(),
vcp->end()); vcp->end());
@ -265,17 +255,15 @@ void Cache::useCache(uint32_t rawAddr, bool isWrite)
} }
} }
} }
// New Victim Cache
for (auto &b : *vcp) for (auto &b : *vcp)
b.lru++; b.lru++;
*vic = {config->VictimAddrParser(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true}; *vic = {config->parserVictimAddr(config->UnParser(cl->tag, addr)).tag, 0, cl->dirty, true};
for (auto &b : (*s)) for (auto &b : (*s))
b.lru++; b.lru++;
*cl = {addr.tag, 0, isWrite, true}; *cl = {addr.tag, 0, isWrite, true};
if (nextCache != nullptr) if (nextCache != nullptr)
{ {
// 2. Find it from Next Level
nextCache->useCache(rawAddr, false); nextCache->useCache(rawAddr, false);
} }
} }

View File

@ -47,12 +47,12 @@ public:
double getHT(); double getHT();
double getHT2(); double getHT2();
double getMP(); double getMP();
int log2_floor(uint32_t x); int log2(uint32_t x);
uint32_t getMaxIndex(); uint32_t getMaxIndex();
uint32_t getVictimAs(); uint32_t getVictimAs();
uint32_t getAs(); uint32_t getAs();
void setupTIO(); void setupTIO();
CacheIndex VictimAddrParser(uint32_t addr); CacheIndex parserVictimAddr(uint32_t addr);
CacheIndex AddrParser(uint32_t addr); CacheIndex AddrParser(uint32_t addr);
uint32_t UnParser(uint32_t tag, CacheIndex tio); uint32_t UnParser(uint32_t tag, CacheIndex tio);
uint32_t UnParserVic(uint32_t tag, CacheIndex tio); uint32_t UnParserVic(uint32_t tag, CacheIndex tio);

Binary file not shown.

Binary file not shown.