int getFrame(int notme) { int UPTClock; // clock for UPT int frame; frame = getAvailableFrame(); if (frame >=0) return frame; if(RPTClock == 0){ RPTClock = LC3_RPT; } int RPTBegin = RPTClock; int first = 1; int rptCount = 0; //iterate the first time through the tables while(first || RPTClock != RPTBegin){ rptCount++; first = 0; //Check and make sure the notme condition holds if(DEFINED(memory[RPTClock])){ UPTClock = (FRAME(memory[RPTClock])<<6); int UPTBegin = UPTClock; int uFirst = 1; int hadDefined = 0; int uptcount = 0; while(uFirst || UPTClock != UPTBegin){ uptcount++; uFirst = 0; if(DEFINED(memory[UPTClock])){ hadDefined = 1; } if(notme != UPTClock && DEFINED(memory[UPTClock])){ //swap out if not referenced if(!REFERENCED(memory[UPTClock])){ //if it's dirty, write it to memory int swapPage = 0; if (DIRTY(memory[UPTClock])){ if(PAGED(memory[UPTClock + 1])) swapPage = accessPage(SWAPPAGE(memory[UPTClock + 1]), FRAME(memory[UPTClock]), PAGE_OLD_WRITE); else swapPage = accessPage(0, FRAME(memory[UPTClock]), PAGE_NEW_WRITE); memory[UPTClock] = CLEAR_DIRTY(memory[UPTClock]); memory[UPTClock+1] = SET_PAGED(swapPage); pageWrites++; } //no longer in main memory--it's swapped! memory[UPTClock] = CLEAR_DEFINED(memory[UPTClock]); //increment the clock if(RPTClock + 2 < LC3_RPT_END) RPTClock += 2; else RPTClock = LC3_RPT; return FRAME(memory[UPTClock]); } memory[UPTClock] = CLEAR_REF(memory[UPTClock]); } //sanity check assert(UPTClock >= 0x3000); if(UPTClock + 2 < UPTBegin + LC3_FRAME_SIZE) UPTClock += 2; else UPTClock = UPTBegin; } if(!REFERENCED(memory[RPTClock]) && !hadDefined && notme != RPTClock){ int swapPage; if(PAGED(memory[RPTClock + 1])) swapPage = accessPage(SWAPPAGE(memory[RPTClock + 1]), FRAME(memory[RPTClock]), PAGE_OLD_WRITE); else swapPage = accessPage(0, FRAME(memory[RPTClock]), PAGE_NEW_WRITE); memory[RPTClock] = CLEAR_DIRTY(memory[RPTClock]); memory[RPTClock+1] = SET_PAGED(swapPage); pageWrites++; memory[RPTClock] = CLEAR_DEFINED(memory[RPTClock]); int rE1 = memory[RPTClock]; if(RPTClock + 2 < LC3_RPT_END) RPTClock += 2; else RPTClock = LC3_RPT; return FRAME(rE1); } //clear the reference bit memory[RPTClock] = CLEAR_REF(memory[RPTClock]); } if(RPTClock + 2 < LC3_RPT_END){ RPTClock += 2; } else{ RPTClock = LC3_RPT; } } RPTBegin = RPTClock; first = 1; while(first || RPTClock != RPTBegin){ rptCount++; first = 0; //Check and make sure the notme condition holds if(DEFINED(memory[RPTClock])){ UPTClock = (FRAME(memory[RPTClock])<<6); int UPTBegin = UPTClock; int uFirst = 1; int hadDefined = 0; int uptcount = 0; while(uFirst || UPTClock != UPTBegin){ uptcount++; uFirst = 0; if(DEFINED(memory[UPTClock])){ hadDefined = 1; } if(notme != UPTClock && DEFINED(memory[UPTClock])){ //swap out if not referenced if(!REFERENCED(memory[UPTClock])){ //if it's dirty, write it to memory int swapPage = 0; if (DIRTY(memory[UPTClock])){ if(PAGED(memory[UPTClock + 1])) swapPage = accessPage(SWAPPAGE(memory[UPTClock + 1]), FRAME(memory[UPTClock]), PAGE_OLD_WRITE); else swapPage = accessPage(0, FRAME(memory[UPTClock]), PAGE_NEW_WRITE); memory[UPTClock] = CLEAR_DIRTY(memory[UPTClock]); memory[UPTClock+1] = SET_PAGED(swapPage); pageWrites++; } //no longer in main memory--it's swapped! memory[UPTClock] = CLEAR_DEFINED(memory[UPTClock]); //increment the clock if(RPTClock + 2 < LC3_RPT_END) RPTClock += 2; else RPTClock = LC3_RPT; return FRAME(memory[UPTClock]); } memory[UPTClock] = CLEAR_REF(memory[UPTClock]); } //sanity check assert(UPTClock >= 0x3000); if(UPTClock + 2 < UPTBegin + LC3_FRAME_SIZE) UPTClock += 2; else UPTClock = UPTBegin; } if(!REFERENCED(memory[RPTClock]) && !hadDefined && notme != RPTClock){ int swapPage; if(PAGED(memory[RPTClock + 1])) swapPage = accessPage(SWAPPAGE(memory[RPTClock + 1]), FRAME(memory[RPTClock]), PAGE_OLD_WRITE); else swapPage = accessPage(0, FRAME(memory[RPTClock]), PAGE_NEW_WRITE); memory[RPTClock] = CLEAR_DIRTY(memory[RPTClock]); memory[RPTClock+1] = SET_PAGED(swapPage); pageWrites++; memory[RPTClock] = CLEAR_DEFINED(memory[RPTClock]); int rE1 = memory[RPTClock]; if(RPTClock + 2 < LC3_RPT_END) RPTClock += 2; else RPTClock = LC3_RPT; return FRAME(rE1); } //clear the reference bit memory[RPTClock] = CLEAR_REF(memory[RPTClock]); } if(RPTClock + 2 < LC3_RPT_END){ RPTClock += 2; } else{ RPTClock = LC3_RPT; } } //we should never ever be here assert(0); return -1; }
int getFrame(int notme) { int frame; frame = getAvailableFrame(); if (frame >=0) return frame; // run clock int i, j, upta, frameToRemove; i = lastRpte; // i = 0x2400; // j = lastUpte; while(1){ // for(i=0x2400;i<0x3000;i+=2){ // printf("%x", i); if(DEFINED(memory[i])){ upta = FRAME(memory[i]); upta = (upta<<6); //double confirm int k; bool nothingInFrame = 1; for(k=upta;k<upta+64;k+=2){ if(DEFINED(memory[k])){ nothingInFrame = 0; break; } } if(nothingInFrame && FRAME(memory[i])!=notme){ frameToRemove = FRAME(memory[i]); lastRpte=i+2; memory[i] = CLEAR_DEFINED(memory[i]); memory[i] = CLEAR_REF(memory[i]); if(PAGED(memory[i+1]) && DIRTY(memory[i])){ memory[i+1] = accessPage(SWAPPAGE(memory[i+1]), frameToRemove, PAGE_OLD_WRITE); }else{ memory[i+1] = accessPage(0, frameToRemove, PAGE_NEW_WRITE); } memory[i+1] = SET_PAGED(memory[i+1]); memory[i] = SET_DIRTY(memory[i]); //possibly clear frame number return frameToRemove; // i can swap out this upt } // for(j=upta;j<upta+64;j+=2){ while(uptOffset < 64){ j = upta + uptOffset; if(DEFINED(memory[j])){ if(REFERENCED(memory[j])){ memory[j] = CLEAR_REF(memory[j]); }else{ //use this data frame frameToRemove = FRAME(memory[j]); lastRpte = i; memory[j] = CLEAR_DEFINED(memory[j]); memory[j] = CLEAR_REF(memory[j]); // memory[j] = SWAPPAGE(memory[j]); if(PAGED(memory[j+1]) && DIRTY(memory[j])){ memory[j+1] = accessPage(SWAPPAGE(memory[j+1]), frameToRemove, PAGE_OLD_WRITE); }else{ memory[j+1] = accessPage(0, frameToRemove, PAGE_NEW_WRITE); } memory[j+1] = SET_PAGED(memory[j+1]); uptOffset+=2; // may not need this line //need to loop through rest next time but if nothing dont remove this one return frameToRemove; } } uptOffset+=2; } uptOffset = 0; // } } i+=2; if(i==0x3000){ i=0x2400; } // } } // memory[(frameToRemove<<6)] return frame; }