Example #1
0
int main(int argc, char **argv)
{
    //cout<<argc<<endl;
    //srand((int)time(NULL));
    /**
     * Configuration
     */
    for(int i = 1; i < argc; i++)
    {
        if(i == 1)
        {
            if(strcmp(argv[i], "1") == 0)
            {
                debugMode = true;
            } else {
                debugMode = false;
            }
        } else if(i == 2)
        {
            sscanf(argv[i],"%d",&numberOfTLBEntries);
            numberOfTLBEntries = getNearestBiggerPowerOfTwo(numberOfTLBEntries);
        } else if(i == 3)
        {
            sscanf(argv[i],"%d",&numberOfPageFrames);
            numberOfPageFrames = getNearestBiggerPowerOfTwo(numberOfPageFrames);
        } else if(i == 4)
        {
            sscanf(argv[i],"%d",&numberOfInstructions);
        } else if(i == 5)
        {
            sscanf(argv[i],"%d",&pageChangeAfterInstructionCount);
        }
    }
    /**
     * Initial State
     */

    cout<<"Debug Mode: "<<debugMode<<endl;
    cout<<"Total number of TLB Entries: "<<numberOfTLBEntries<<endl;
    cout<<"Total number of Page Frames: "<<numberOfPageFrames<<endl;
    cout<<"Total number of Instructions: "<<numberOfInstructions<<endl;
    cout<<"Total number of Virtual Pages: "<<numberOfVirtualPages<<endl;
    cout<<"On Average Virtual Address Page Changes after: "<<pageChangeAfterInstructionCount<<endl;

    /**
     * Operation starts...
     * Create a pageTable, a TLB
     */

    PageTable pageTable;
    TranslationLookaheadBuffer tlb(numberOfTLBEntries);
    pageTable.setTranslationLookaheadBuffer(&tlb);

    for(int i = 0; i < numberOfInstructions; i++)
    {
        Instruction nextInstruction = getNextInstruction();
        if(debugMode)
        {
            nextInstruction.print();
        }
        LookupResult l = tlb.lookup(nextInstruction.msbAddress);
        if(l.isFound == true)
        {
            //do nothing
        } else
        {
            if(debugMode)
            {
                printf("Cache Miss for page %02x\n",nextInstruction.msbAddress);
            }
            totalNumberOfCacheMisses += 1;
            l = pageTable.lookup(nextInstruction.msbAddress);
            if(l.isFound == true)
            {
                //do nothing
            } else
            {
                if(debugMode)
                {
                    printf("Page fault on page %02x\n",nextInstruction.msbAddress);
                }
                totalNumberOfPageFaults += 1;
                pageTable.insert(nextInstruction.msbAddress);
                l = pageTable.lookup(nextInstruction.msbAddress);
            }
            tlb.insert(nextInstruction.msbAddress, l.frameIndex);
            //pageTable.insert(nextInstruction.msbAddress);
            //l = pageTable.lookup(nextInstruction.msbAddress);
        }
        //
        if(debugMode)
        {
            printf("Virtual Address: %02x%02x, Physical Address: %02x%02x\n", nextInstruction.msbAddress, nextInstruction.lsbAddress, l.frameIndex, nextInstruction.lsbAddress);
        }
    }
    cout<<"Total number of Cache Misses: "<<totalNumberOfCacheMisses<<endl;
    cout<<"Total number of Page Faults: "<<totalNumberOfPageFaults<<endl;
    cout<<"Total number of Page Used: "<<pageTable.getUsedPageCount()<<endl;
    cout<<"Total number of Page Changed: "<<pageChangeCount<<endl;

    /*TranslationLookaheadBuffer tlb;
    unsigned char accessPattern[] = "ABACBDADEDAEBAC";
    for(int i = 0; i < 15; i++)
    {
        cout<<i<<","<<accessPattern[i]<<endl;
        LookupResult result = tlb.lookup(accessPattern[i]);
        if(result.isFound)
        {
            tlb.print();
        } else {
            cout<<"Cache Miss"<<endl;
            tlb.insert(accessPattern[i],accessPattern[i]);
            tlb.print();
        }
    }*/
    return 0;
}