#include #include #include #include #include #define HIT 1 #define MISS 0 #define READ 0 #define WRITE 1 #define PageHitDelay 50 #define PageMissDelay 600 #define DEBUG 1 using namespace std; int LRUCtr=0; ofstream MTF("dram_tracefile.txt"); class Bank { int Address; int RowBufferAddess; int PageSize, NumRowPerBank, NumColumnPerRow; int PageHit=0, PageMiss=0; int ReadCtr=0, WriteCtr=0; public: Bank(){} void init(int RowPerBank, int ColPerRow){ NumRowPerBank=RowPerBank; NumColumnPerRow=ColPerRow; PageSize=NumColumnPerRow; } int Access(int Addr26, bool RW){ int Addr18=Addr26 %(NumRowPerBank*NumColumnPerRow); int Row=Addr18/NumColumnPerRow; if (RW) WriteCtr++; else ReadCtr++; if (RowBufferAddess==Row) { PageHit++; return PageHitDelay; } PageMiss++; RowBufferAddess=Row; return PageMissDelay; } int getNumPageHits(){return PageHit;} int getNumPageMisses(){return PageMiss;} int getNumWrite(){return WriteCtr;} int getNumRead(){return ReadCtr;} }; class Memory{ int NumChannel, NumRankPerChannel, NumBankPerRank; int NumRowPerBank, NumColumnPerRow; int SizeOfMemInByte, PageSize, TotalNumBank; Bank *B; public: Memory(int c, int r, int b, int row, int col){ NumChannel=c; NumRankPerChannel=r; NumBankPerRank=b; NumRowPerBank=row; NumColumnPerRow=col; SizeOfMemInByte=NumChannel*NumRankPerChannel*NumBankPerRank*NumRowPerBank*NumColumnPerRow; PageSize=NumColumnPerRow; TotalNumBank=NumChannel*NumRankPerChannel*NumBankPerRank; B= new Bank[TotalNumBank]; for(int i=0;i0) M.writeblock((OTag*LS*set+index*LS)); //if (DEBUG) cout<<"\nWriteHappens..\n"; } 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 < 25000; 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<