From b02004f62ef50781aca62c76fd7cd62d24935d28 Mon Sep 17 00:00:00 2001 From: LYC Date: Mon, 28 Oct 2024 14:38:06 +0800 Subject: [PATCH] 1 --- cache/Project1/Proj1-1/Proj1-1/src/cache.cc | 67 ++++++++++++++++++ cache/Project1/Proj1-1/Proj1-1/src/cache.h | 69 ++++++++++++++++--- cache/Project1/Proj1-1/Proj1-1/src/main.cc | 25 ++++++- cache/Project1/Proj1-1/Proj1-1/src/public.h | 7 ++ .../Proj1-1/Proj1-1/src/para.h | 25 ------- 5 files changed, 155 insertions(+), 38 deletions(-) create mode 100644 cache/Project1/Proj1-1/Proj1-1/src/public.h diff --git a/cache/Project1/Proj1-1/Proj1-1/src/cache.cc b/cache/Project1/Proj1-1/Proj1-1/src/cache.cc index 05b26b0..dca466e 100644 --- a/cache/Project1/Proj1-1/Proj1-1/src/cache.cc +++ b/cache/Project1/Proj1-1/Proj1-1/src/cache.cc @@ -1 +1,68 @@ #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<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); \ No newline at end of file diff --git a/cache/Project1/Proj1-1/Proj1-1/src/cache.h b/cache/Project1/Proj1-1/Proj1-1/src/cache.h index 885b2d4..a63ddf9 100644 --- a/cache/Project1/Proj1-1/Proj1-1/src/cache.h +++ b/cache/Project1/Proj1-1/Proj1-1/src/cache.h @@ -1,24 +1,71 @@ #pragma once +#include "public.h" #include #include #include +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; // Blocks +}; + class Cache { 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 L1; + + // L2 cache + Cache * L2; public: - Cache(/* args */); + Cache(char *argv[]); ~Cache(); + + uint32_t getMaxIndex(); + uint32_t getAssoc(); + void writeCache(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() -{ -} diff --git a/cache/Project1/Proj1-1/Proj1-1/src/main.cc b/cache/Project1/Proj1-1/Proj1-1/src/main.cc index 17f5db2..e40b366 100644 --- a/cache/Project1/Proj1-1/Proj1-1/src/main.cc +++ b/cache/Project1/Proj1-1/Proj1-1/src/main.cc @@ -1,6 +1,27 @@ #include "world.h" +#include "cache.h" +#include "public.h" -int main(int argc, char *argv[]) { - hello_world(argc, argv); +int main(int argc, char *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 (...) + { + } } diff --git a/cache/Project1/Proj1-1/Proj1-1/src/public.h b/cache/Project1/Proj1-1/Proj1-1/src/public.h new file mode 100644 index 0000000..0df24ae --- /dev/null +++ b/cache/Project1/Proj1-1/Proj1-1/src/public.h @@ -0,0 +1,7 @@ +#pragma once + +#define LRU 0 +#define LFU 1 + +#define WBWA 0 +#define WTNA \ No newline at end of file diff --git a/cache/TJU-2023-Computer-Organization/Proj1-1/Proj1-1/src/para.h b/cache/TJU-2023-Computer-Organization/Proj1-1/Proj1-1/src/para.h index 0c37cab..492018d 100644 --- a/cache/TJU-2023-Computer-Organization/Proj1-1/Proj1-1/src/para.h +++ b/cache/TJU-2023-Computer-Organization/Proj1-1/Proj1-1/src/para.h @@ -71,25 +71,8 @@ public: int WritePolicy() {return wp;} 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[]); - // ===== 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){ os << " ===== Simulator configuration =====\n"; os << " L1_BLOCKSIZE:" << setw(22) << cp.bs << endl; @@ -102,18 +85,10 @@ public: return os; } - /** - * @brief Get HT - * @return 0.25 + 2.5 * (s/ (512.0 * 1024)) + 0.025 * (bs/ 16.0) + 0.025 * as; - */ double GetHT() { 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() { return 20 + 0.5 * (bs / 16.0); }