#include #include #include #include #include #define HIT 1 #define MISS 0 #define READ 0 #define WRITE 1 using namespace std; int LRUCtr=0; class Memory{ public: int readblock(int Address){return 1;} int writeblock(int Address){return 1;} }; Memory M; class Cache { private: int set, asso, LS; int hit_counter,miss_counter; int **TAG, **LRU; bool **Dirty; public: int get_hit(void){return hit_counter;} int get_miss(void){return miss_counter;} Cache(){} void CacheInit( int sets, int associativity, int LineSize ) { int i, j; TAG = new int*[sets]; LRU = new int*[sets]; Dirty = new bool*[sets]; for(i = 0; i < sets; i++){ TAG[i] = new int [associativity]; LRU[i] = new int [associativity]; Dirty[i] = new bool [associativity]; } /* Initialize tag to be -1 */ for(i=0;i0) M.writeblock((OTag*LS*set+index*LS)); } TAG[index][lru_index] = Tag; return MISS; } }; int main() { int hit; unsigned int Address; int j, CPUCycle, i =0; bool RW; int MaxAddress=1<<30; Cache L; ofstream TF("tracefile.txt"); L.CacheInit(128, 4, 64); srand(time(NULL)); Address = rand()%MaxAddress; for(CPUCycle = 0; CPUCycle < 5000; CPUCycle=CPUCycle+4) { if ((rand()%100) <20) Address = rand()%MaxAddress; //20% time it jump to new address else Address = Address+4; // 80% time it goes to next word if ((rand()%100)<20) RW=WRITE; else RW=READ; hit = L.Access(Address,RW); TF<