Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="page"></param>
        /// <param name="virtualIndex"></param>
        /// <returns></returns>
        public uint VirtualAddrToPhysical(MemoryPage page, uint virtualIndex)
        {
            if (page.isValid == false)
            {
                int i = 0;
                for (; i < freePhysicalPages.Length; i++)
                {
                    if (freePhysicalPages[i] == true)
                    {
                        // Found a free physical page!
                        freePhysicalPages[i] = false;
                        break;
                    }
                }

                // If we have reach the end of the freePhysicalPages
                // without finding a free page - we are out of physical memory, therefore
                // we PageFault and start looking for victim pages to swap out
                if (i == freePhysicalPages.Length)
                {
                    MemoryPage currentVictim = null;
                    foreach (MemoryPage possibleVictim in _pageTable)
                    {
                        if (!page.Equals(possibleVictim) && possibleVictim.isValid == true)
                        {
                            if (currentVictim == null)
                            {
                                currentVictim = possibleVictim;
                            }

                            // If this is the least accessed Memory Page we've found so far
                            if (possibleVictim.lastAccessed < currentVictim.lastAccessed)
                            {
                                currentVictim = possibleVictim;
                            }
                        }
                    }
                    // Did we find no victims?  That's a HUGE problem, and shouldn't ever happen
                    Debug.Assert(currentVictim != null);

                    SwapOut(currentVictim);

                    // Take the physical address of this page
                    page.addrPhysical = currentVictim.addrPhysical;

                    SwapIn(page);
                }
                else                 // no page fault
                {
                    // Map this page to free physical page "i"
                    page.addrPhysical = (uint)(i * CPU.pageSize);
                    SwapIn(page);
                }
            }

            // Adjust the physical address with pageOffset from a page boundary
            uint pageOffset    = virtualIndex % CPU.pageSize;
            uint physicalIndex = page.addrPhysical + pageOffset;

            return(physicalIndex);
        }