This commit is contained in:
LYC 2024-10-28 14:38:06 +08:00
parent 1978f1277c
commit b02004f62e
5 changed files with 155 additions and 38 deletions

View File

@ -1 +1,68 @@
#include "cache.h" #include "cache.h"
Cache::Cache(char *argv[])
{
this->blockSize = atoi(argv[1]);
this->size = atoi(argv[2]);
this->Assoc = atoi(argv[3]);
this->replicementPolicy = atoi(argv[4]);
this->writePolicy = atoi(argv[5]);
if ((this->blockSize < 0) || !(this->blockSize ^ (this->blockSize - 1)))
{
throw "Para blockSize Err.";
}
if (this->size < 0)
{
throw "Para blockSize Err.";
}
if (this->Assoc < 1)
{
throw "Para blockSize Err.";
}
if (this->replicementPolicy != LRU && this->replicementPolicy != LFU)
{
throw "Para blockSize Err.";
}
if (this->writePolicy != WBWA && this->writePolicy != WTNA)
{
throw "Para blockSize Err.";
}
this->offset = log2_floor(this->blockSize);
this->index = log2_floor(this->size / (this->blockSize * this->assoc));
this->tagBit = 32 - this->offset - this->index;
this->offsetMask = (1 << this->offset) - 1;
this->indexMask = ((1 << this->index) - 1) << this->offset;
this->L1.resize(this->getMaxIndex());
for (auto &s : L1 )
s.block.resize(this->GetAssoc());
}
Cache::~Cache()
{
}
uint32_t Cache::getMaxIndex(){
return (1<<this->index);
}
uint32_t Cache::GetAssoc() {
return this->assoc;
}
CacheTIO Cache::Address2TIO(uint32_t addr)
{
uint32_t offset = addr & om;
uint32_t index = (addr & im) >> o;
uint32_t tag = addr >> (o + i);
return {tag, index, offset};
}
void Cache::writeCache(uint32_t address){
}
void Cache::readCache(uint32_t address);
void Cache::useCache(uint32_t address, int method);

View File

@ -1,24 +1,71 @@
#pragma once #pragma once
#include "public.h"
#include <iostream> #include <iostream>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
struct CacheTIO {
uint32_t tag, index, offset;
};
struct Block{
uint32_t tag = 0; // tag
uint32_t cb = 0; // count block
uint32_t lu = 0; // Last Used
bool d = false; // dirty
bool v = false; // valid
bool operator<(const Block &t) {return cb < t.cb;}
friend ostream& operator<<(ostream& os, Block&b) {
os << "{tag:" << b.tag << ", Count-Block:" << b.cb << ", Last-Used:" << b.lu << ", Dirty:" << b.d << ", Valid:" << b.v << "}";
return os;
}
};
struct Set {
uint32_t countSet = 0; // count set
vector<Block> block; // Blocks
};
class Cache class Cache
{ {
private: private:
/* data */ //Param
int blockSize; // Block Size
int size; // Size
int assoc; // Assoc
int replicementPolicy; // Replicement Policy
int writePolicy; // Write Policy: 1 -> WTNA, 0 ->
int tagBit; // tag bit
int index; // index
int offset; // offset
uint32_t offsetMask; // offset mask
uint32_t indexMask; // index mask
//Count
int readCount = 0; // Read Count
int readMiss = 0; // Read Miss
int writeCount = 0; // Write Count
int writeMiss = 0; // Write Miss
int writeBack = 0; // Write Back
uint32_t lru = 0; // LRU Global Counter
// main cache
vector<Set> L1;
// L2 cache
Cache * L2;
public: public:
Cache(/* args */); Cache(char *argv[]);
~Cache(); ~Cache();
uint32_t getMaxIndex();
uint32_t getAssoc();
void writeCache(uint32_t address); void writeCache(uint32_t address);
void readCache(uint32_t address); void readCache(uint32_t address);
void useCache(uint32_t address, int method);
CacheTIO Address2TIO(uint32_t addr);
}; };
Cache::Cache(/* args */)
{
}
Cache::~Cache()
{
}

View File

@ -1,6 +1,27 @@
#include "world.h" #include "world.h"
#include "cache.h"
#include "public.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
hello_world(argc, argv); {
try
{
if (argc != 7)
{
throw "Para Err."
}
auto fileName = std::string(argv[6]);
struct stat buffer;
if (stat(("../traces/" + tf).c_str(), &buffer))
{
throw "traces file Err.";
}
auto cache = new Cache(char *argv[]);
}
catch (...)
{
}
} }

View File

@ -0,0 +1,7 @@
#pragma once
#define LRU 0
#define LFU 1
#define WBWA 0
#define WTNA

View File

@ -71,25 +71,8 @@ public:
int WritePolicy() {return wp;} int WritePolicy() {return wp;}
string TraceFile() {return tf;} string TraceFile() {return tf;}
/**
* @brief Extract Cache Parameter p
*
* @param argc
* @param argv
* @return
* 1: Error (throw)
* 0: Success
*/
int Extract(int argc, char* argv[]); int Extract(int argc, char* argv[]);
// ===== Simulator configuration =====
// L1_BLOCKSIZE: 16
// L1_SIZE: 16384
// L1_ASSOC: 1
// L1_REPLACEMENT_POLICY: 0
// L1_WRITE_POLICY: 0
// trace_file: gcc_trace.txt
// ===================================
friend ostream& operator<<(ostream &os, const CacheParam& cp){ friend ostream& operator<<(ostream &os, const CacheParam& cp){
os << " ===== Simulator configuration =====\n"; os << " ===== Simulator configuration =====\n";
os << " L1_BLOCKSIZE:" << setw(22) << cp.bs << endl; os << " L1_BLOCKSIZE:" << setw(22) << cp.bs << endl;
@ -102,18 +85,10 @@ public:
return os; return os;
} }
/**
* @brief Get HT
* @return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as;
*/
double GetHT() { double GetHT() {
return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as; return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as;
} }
/**
* @brief Get Miss Penalty
* @return 20 + 0.5 * (bs / 16.0);
*/
double GetMP() { double GetMP() {
return 20 + 0.5 * (bs / 16.0); return 20 + 0.5 * (bs / 16.0);
} }