3 // Id Engine's Page Manager v1.0
\r
4 // Primary coder: Jason Blochowiak
\r
7 #include "ID_HEADS.H"
\r
10 // Main Mem specific variables
\r
11 boolean MainPresent;
\r
12 memptr MainMemPages[PMMaxMainMem];
\r
13 PMBlockAttr MainMemUsed[PMMaxMainMem];
\r
16 // EMS specific variables
\r
18 word EMSAvail,EMSPagesAvail,EMSHandle,
\r
19 EMSPageFrame,EMSPhysicalPage;
\r
20 EMSListStruct EMSList[EMSFrameCount];
\r
22 // XMS specific variables
\r
24 word XMSAvail,XMSPagesAvail,XMSHandle;
\r
26 int XMSProtectPage = -1;
\r
28 // File specific variables
\r
29 char PageFileName[13] = {"VSWAP."};
\r
32 word PMSpriteStart,PMSoundStart;
\r
34 // General usage variables
\r
43 PageListStruct far *PMPages,
\r
46 static char *ParmStrings[] = {"nomain","noems","noxms",nil};
\r
48 /////////////////////////////////////////////////////////////////////////////
\r
50 // EMS Management code
\r
52 /////////////////////////////////////////////////////////////////////////////
\r
55 // PML_MapEMS() - Maps a logical page to a physical page
\r
58 PML_MapEMS(word logical,word physical)
\r
67 Quit("PML_MapEMS: Page mapping failed");
\r
71 // PML_StartupEMS() - Sets up EMS for Page Mgr's use
\r
72 // Checks to see if EMS driver is present
\r
73 // Verifies that EMS hardware is present
\r
74 // Make sure that EMS version is 3.2 or later
\r
75 // If there's more than our minimum (2 pages) available, allocate it (up
\r
76 // to the maximum we need)
\r
79 char EMMDriverName[9] = "EMMXXXX0";
\r
82 PML_StartupEMS(void)
\r
87 EMSPresent = false; // Assume that we'll fail
\r
90 _DX = (word)EMMDriverName;
\r
92 geninterrupt(0x21); // try to open EMMXXXX0 device
\r
99 geninterrupt(0x21); // get device info
\r
109 geninterrupt(0x21); // get status
\r
115 geninterrupt(0x21); // close handle
\r
118 geninterrupt(EMS_INT);
\r
120 goto error; // make sure EMS hardware is present
\r
123 geninterrupt(EMS_INT);
\r
124 if (_AH || (_AL < 0x32)) // only work on EMS 3.2 or greater (silly, but...)
\r
127 _AH = EMS_GETFRAME;
\r
128 geninterrupt(EMS_INT);
\r
130 goto error; // find the page frame address
\r
131 EMSPageFrame = _BX;
\r
133 _AH = EMS_GETPAGES;
\r
134 geninterrupt(EMS_INT);
\r
138 goto error; // Require at least 2 pages (32k)
\r
141 // Don't hog all available EMS
\r
142 size = EMSAvail * (long)EMSPageSize;
\r
143 if (size - (EMSPageSize * 2) > (ChunksInFile * (long)PMPageSize))
\r
145 size = (ChunksInFile * (long)PMPageSize) + EMSPageSize;
\r
146 EMSAvail = size / EMSPageSize;
\r
149 _AH = EMS_ALLOCPAGES;
\r
151 geninterrupt(EMS_INT);
\r
156 mminfo.EMSmem += EMSAvail * (long)EMSPageSize;
\r
158 // Initialize EMS mapping cache
\r
159 for (i = 0;i < EMSFrameCount;i++)
\r
160 EMSList[i].baseEMSPage = -1;
\r
162 EMSPresent = true; // We have EMS
\r
165 return(EMSPresent);
\r
169 // PML_ShutdownEMS() - If EMS was used, deallocate it
\r
172 PML_ShutdownEMS(void)
\r
176 asm mov ah,EMS_FREEPAGES
\r
177 asm mov dx,[EMSHandle]
\r
180 Quit ("PML_ShutdownEMS: Error freeing EMS");
\r
184 /////////////////////////////////////////////////////////////////////////////
\r
186 // XMS Management code
\r
188 /////////////////////////////////////////////////////////////////////////////
\r
191 // PML_StartupXMS() - Starts up XMS for the Page Mgr's use
\r
192 // Checks for presence of an XMS driver
\r
193 // Makes sure that there's at least a page of XMS available
\r
194 // Allocates any remaining XMS (rounded down to the nearest page size)
\r
197 PML_StartupXMS(void)
\r
199 XMSPresent = false; // Assume failure
\r
203 asm int XMS_INT // Check for presence of XMS driver
\r
209 asm int XMS_INT // Get address of XMS driver
\r
210 asm mov [WORD PTR XMSDriver],bx
\r
211 asm mov [WORD PTR XMSDriver+2],es // function pointer to XMS driver
\r
213 XMS_CALL(XMS_QUERYFREE); // Find out how much XMS is available
\r
215 if (!_AX) // AJR: bugfix 10/8/92
\r
218 XMSAvail &= ~(PMPageSizeKB - 1); // Round off to nearest page size
\r
219 if (XMSAvail < (PMPageSizeKB * 2)) // Need at least 2 pages
\r
223 XMS_CALL(XMS_ALLOC); // And do the allocation
\r
226 if (!_AX) // AJR: bugfix 10/8/92
\r
232 mminfo.XMSmem += XMSAvail * 1024;
\r
236 return(XMSPresent);
\r
240 // PML_XMSCopy() - Copies a main/EMS page to or from XMS
\r
241 // Will round an odd-length request up to the next even value
\r
244 PML_XMSCopy(boolean toxms,byte far *addr,word xmspage,word length)
\r
250 word source_handle;
\r
251 longword source_offset;
\r
252 word target_handle;
\r
253 longword target_offset;
\r
257 Quit("PML_XMSCopy: zero address");
\r
259 xoffset = (longword)xmspage * PMPageSize;
\r
261 copy.length = (length + 1) & ~1;
\r
262 copy.source_handle = toxms? 0 : XMSHandle;
\r
263 copy.source_offset = toxms? (long)addr : xoffset;
\r
264 copy.target_handle = toxms? XMSHandle : 0;
\r
265 copy.target_offset = toxms? xoffset : (long)addr;
\r
269 XMS_CALL(XMS_MOVE);
\r
272 Quit("PML_XMSCopy: Error on copy");
\r
276 #define PML_CopyToXMS(s,t,l) PML_XMSCopy(true,(s),(t),(l))
\r
277 #define PML_CopyFromXMS(t,s,l) PML_XMSCopy(false,(t),(s),(l))
\r
280 // PML_CopyToXMS() - Copies the specified number of bytes from the real mode
\r
281 // segment address to the specified XMS page
\r
284 PML_CopyToXMS(byte far *source,int targetpage,word length)
\r
286 PML_XMSCopy(true,source,targetpage,length);
\r
290 // PML_CopyFromXMS() - Copies the specified number of bytes from an XMS
\r
291 // page to the specified real mode address
\r
294 PML_CopyFromXMS(byte far *target,int sourcepage,word length)
\r
296 PML_XMSCopy(false,target,sourcepage,length);
\r
301 // PML_ShutdownXMS()
\r
304 PML_ShutdownXMS(void)
\r
309 XMS_CALL(XMS_FREE);
\r
311 Quit("PML_ShutdownXMS: Error freeing XMS");
\r
315 /////////////////////////////////////////////////////////////////////////////
\r
317 // Main memory code
\r
319 /////////////////////////////////////////////////////////////////////////////
\r
322 // PM_SetMainMemPurge() - Sets the purge level for all allocated main memory
\r
323 // blocks. This shouldn't be called directly - the PM_LockMainMem() and
\r
324 // PM_UnlockMainMem() macros should be used instead.
\r
327 PM_SetMainMemPurge(int level)
\r
331 for (i = 0;i < PMMaxMainMem;i++)
\r
332 if (MainMemPages[i])
\r
333 MM_SetPurge(&MainMemPages[i],level);
\r
337 // PM_CheckMainMem() - If something besides the Page Mgr makes requests of
\r
338 // the Memory Mgr, some of the Page Mgr's blocks may have been purged,
\r
339 // so this function runs through the block list and checks to see if
\r
340 // any of the blocks have been purged. If so, it marks the corresponding
\r
341 // page as purged & unlocked, then goes through the block list and
\r
342 // tries to reallocate any blocks that have been purged.
\r
343 // This routine now calls PM_LockMainMem() to make sure that any allocation
\r
344 // attempts made during the block reallocation sweep don't purge any
\r
345 // of the other blocks. Because PM_LockMainMem() is called,
\r
346 // PM_UnlockMainMem() needs to be called before any other part of the
\r
347 // program makes allocation requests of the Memory Mgr.
\r
350 PM_CheckMainMem(void)
\r
352 boolean allocfailed;
\r
356 PageListStruct far *page;
\r
361 for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
\r
363 n = page->mainPage;
\r
364 if (n != -1) // Is the page using main memory?
\r
366 if (!MainMemPages[n]) // Yep, was the block purged?
\r
368 page->mainPage = -1; // Yes, mark page as purged & unlocked
\r
369 page->locked = pml_Unlocked;
\r
374 // Prevent allocation attempts from purging any of our other blocks
\r
376 allocfailed = false;
\r
377 for (i = 0,p = MainMemPages,used = MainMemUsed;i < PMMaxMainMem;i++,p++,used++)
\r
379 if (!*p) // If the page got purged
\r
381 if (*used & pmba_Allocated) // If it was allocated
\r
383 *used &= ~pmba_Allocated; // Mark as unallocated
\r
384 MainPagesAvail--; // and decrease available count
\r
387 if (*used & pmba_Used) // If it was used
\r
389 *used &= ~pmba_Used; // Mark as unused
\r
390 MainPagesUsed--; // and decrease used count
\r
395 MM_BombOnError(false);
\r
396 MM_GetPtr(p,PMPageSize); // Try to reallocate
\r
397 if (mmerror) // If it failed,
\r
398 allocfailed = true; // don't try any more allocations
\r
399 else // If it worked,
\r
401 *used |= pmba_Allocated; // Mark as allocated
\r
402 MainPagesAvail++; // and increase available count
\r
404 MM_BombOnError(true);
\r
413 // PML_StartupMainMem() - Allocates as much main memory as is possible for
\r
414 // the Page Mgr. The memory is allocated as non-purgeable, so if it's
\r
415 // necessary to make requests of the Memory Mgr, PM_UnlockMainMem()
\r
416 // needs to be called.
\r
419 PML_StartupMainMem(void)
\r
424 MainPagesAvail = 0;
\r
425 MM_BombOnError(false);
\r
426 for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++)
\r
428 MM_GetPtr(p,PMPageSize);
\r
433 MainMemUsed[i] = pmba_Allocated;
\r
435 MM_BombOnError(true);
\r
438 if (MainPagesAvail < PMMinMainMem)
\r
439 Quit("PM_SetupMainMem: Not enough main memory");
\r
440 MainPresent = true;
\r
444 // PML_ShutdownMainMem() - Frees all of the main memory blocks used by the
\r
448 PML_ShutdownMainMem(void)
\r
453 // DEBUG - mark pages as unallocated & decrease page count as appropriate
\r
454 for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++)
\r
459 /////////////////////////////////////////////////////////////////////////////
\r
461 // File management code
\r
463 /////////////////////////////////////////////////////////////////////////////
\r
466 // PML_ReadFromFile() - Reads some data in from the page file
\r
469 PML_ReadFromFile(byte far *buf,long offset,word length)
\r
472 Quit("PML_ReadFromFile: Null pointer");
\r
474 Quit("PML_ReadFromFile: Zero offset");
\r
475 if (lseek(PageFile,offset,SEEK_SET) != offset)
\r
476 Quit("PML_ReadFromFile: Seek failed");
\r
477 if (!CA_FarRead(PageFile,buf,length))
\r
478 Quit("PML_ReadFromFile: Read failed");
\r
482 // PML_OpenPageFile() - Opens the page file and sets up the page info
\r
485 PML_OpenPageFile(void)
\r
490 longword far *offsetptr;
\r
491 word far *lengthptr;
\r
492 PageListStruct far *page;
\r
494 PageFile = open(PageFileName,O_RDONLY + O_BINARY);
\r
495 if (PageFile == -1)
\r
496 Quit("PML_OpenPageFile: Unable to open page file");
\r
498 // Read in header variables
\r
499 read(PageFile,&ChunksInFile,sizeof(ChunksInFile));
\r
500 read(PageFile,&PMSpriteStart,sizeof(PMSpriteStart));
\r
501 read(PageFile,&PMSoundStart,sizeof(PMSoundStart));
\r
503 // Allocate and clear the page list
\r
504 PMNumBlocks = ChunksInFile;
\r
505 MM_GetPtr(&(memptr)PMSegPages,sizeof(PageListStruct) * PMNumBlocks);
\r
506 MM_SetLock(&(memptr)PMSegPages,true);
\r
507 PMPages = (PageListStruct far *)PMSegPages;
\r
508 _fmemset(PMPages,0,sizeof(PageListStruct) * PMNumBlocks);
\r
510 // Read in the chunk offsets
\r
511 size = sizeof(longword) * ChunksInFile;
\r
512 MM_GetPtr(&buf,size);
\r
513 if (!CA_FarRead(PageFile,(byte far *)buf,size))
\r
514 Quit("PML_OpenPageFile: Offset read failed");
\r
515 offsetptr = (longword far *)buf;
\r
516 for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
\r
517 page->offset = *offsetptr++;
\r
520 // Read in the chunk lengths
\r
521 size = sizeof(word) * ChunksInFile;
\r
522 MM_GetPtr(&buf,size);
\r
523 if (!CA_FarRead(PageFile,(byte far *)buf,size))
\r
524 Quit("PML_OpenPageFile: Length read failed");
\r
525 lengthptr = (word far *)buf;
\r
526 for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
\r
527 page->length = *lengthptr++;
\r
532 // PML_ClosePageFile() - Closes the page file
\r
535 PML_ClosePageFile(void)
\r
537 if (PageFile != -1)
\r
541 MM_SetLock(&(memptr)PMSegPages,false);
\r
542 MM_FreePtr(&(void _seg *)PMSegPages);
\r
546 /////////////////////////////////////////////////////////////////////////////
\r
548 // Allocation, etc., code
\r
550 /////////////////////////////////////////////////////////////////////////////
\r
553 // PML_GetEMSAddress()
\r
555 // Page is in EMS, so figure out which EMS physical page should be used
\r
556 // to map our page in. If normal page, use EMS physical page 3, else
\r
557 // use the physical page specified by the lock type
\r
560 #pragma argsused // DEBUG - remove lock parameter
\r
562 PML_GetEMSAddress(int page,PMLockType lock)
\r
565 word emsoff,emsbase,offset;
\r
567 emsoff = page & (PMEMSSubPage - 1);
\r
568 emsbase = page - emsoff;
\r
571 // See if this page is already mapped in
\r
572 for (i = 0;i < EMSFrameCount;i++)
\r
574 if (EMSList[i].baseEMSPage == emsbase)
\r
576 emspage = i; // Yep - don't do a redundant remapping
\r
581 // If page isn't already mapped in, find LRU EMS frame, and use it
\r
584 longword last = MAXLONG;
\r
585 for (i = 0;i < EMSFrameCount;i++)
\r
587 if (EMSList[i].lastHit < last)
\r
590 last = EMSList[i].lastHit;
\r
594 EMSList[emspage].baseEMSPage = emsbase;
\r
595 PML_MapEMS(page / PMEMSSubPage,emspage);
\r
599 Quit("PML_GetEMSAddress: EMS find failed");
\r
601 EMSList[emspage].lastHit = PMFrameCount;
\r
602 offset = emspage * EMSPageSizeSeg;
\r
603 offset += emsoff * PMPageSizeSeg;
\r
604 return((memptr)(EMSPageFrame + offset));
\r
608 PML_GetEMSAddress(int page,PMLockType lock)
\r
612 emspage = (lock < pml_EMSLock)? 3 : (lock - pml_EMSLock);
\r
614 PML_MapEMS(page / PMEMSSubPage,emspage);
\r
616 return((memptr)(EMSPageFrame + (emspage * EMSPageSizeSeg)
\r
617 + ((page & (PMEMSSubPage - 1)) * PMPageSizeSeg)));
\r
622 // PM_GetPageAddress() - Returns the address of a given page
\r
623 // Maps in EMS if necessary
\r
624 // Returns nil if block isn't cached into Main Memory or EMS
\r
628 PM_GetPageAddress(int pagenum)
\r
630 PageListStruct far *page;
\r
632 page = &PMPages[pagenum];
\r
633 if (page->mainPage != -1)
\r
634 return(MainMemPages[page->mainPage]);
\r
635 else if (page->emsPage != -1)
\r
636 return(PML_GetEMSAddress(page->emsPage,page->locked));
\r
642 // PML_GiveLRUPage() - Returns the page # of the least recently used
\r
643 // present & unlocked main/EMS page (or main page if mainonly is true)
\r
646 PML_GiveLRUPage(boolean mainonly)
\r
650 PageListStruct far *page;
\r
652 for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++)
\r
656 (page->lastHit < last)
\r
657 && ((page->emsPage != -1) || (page->mainPage != -1))
\r
658 && (page->locked == pml_Unlocked)
\r
659 && (!(mainonly && (page->mainPage == -1)))
\r
662 last = page->lastHit;
\r
668 Quit("PML_GiveLRUPage: LRU Search failed");
\r
673 // PML_GiveLRUXMSPage() - Returns the page # of the least recently used
\r
674 // (and present) XMS page.
\r
675 // This routine won't return the XMS page protected (by XMSProtectPage)
\r
678 PML_GiveLRUXMSPage(void)
\r
682 PageListStruct far *page;
\r
684 for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++)
\r
688 (page->xmsPage != -1)
\r
689 && (page->lastHit < last)
\r
690 && (i != XMSProtectPage)
\r
693 last = page->lastHit;
\r
701 // PML_PutPageInXMS() - If page isn't in XMS, find LRU XMS page and replace
\r
702 // it with the main/EMS page
\r
705 PML_PutPageInXMS(int pagenum)
\r
708 PageListStruct far *page;
\r
713 page = &PMPages[pagenum];
\r
714 if (page->xmsPage != -1)
\r
715 return; // Already in XMS
\r
717 if (XMSPagesUsed < XMSPagesAvail)
\r
718 page->xmsPage = XMSPagesUsed++;
\r
721 usexms = PML_GiveLRUXMSPage();
\r
723 Quit("PML_PutPageInXMS: No XMS LRU");
\r
724 page->xmsPage = PMPages[usexms].xmsPage;
\r
725 PMPages[usexms].xmsPage = -1;
\r
727 PML_CopyToXMS(PM_GetPageAddress(pagenum),page->xmsPage,page->length);
\r
731 // PML_TransferPageSpace() - A page is being replaced, so give the new page
\r
732 // the old one's address space. Returns the address of the new page.
\r
735 PML_TransferPageSpace(int orig,int new)
\r
738 PageListStruct far *origpage,far *newpage;
\r
741 Quit("PML_TransferPageSpace: Identity replacement");
\r
743 origpage = &PMPages[orig];
\r
744 newpage = &PMPages[new];
\r
746 if (origpage->locked != pml_Unlocked)
\r
747 Quit("PML_TransferPageSpace: Killing locked page");
\r
749 if ((origpage->emsPage == -1) && (origpage->mainPage == -1))
\r
750 Quit("PML_TransferPageSpace: Reusing non-existent page");
\r
752 // Copy page that's about to be purged into XMS
\r
753 PML_PutPageInXMS(orig);
\r
755 // Get the address, and force EMS into a physical page if necessary
\r
756 addr = PM_GetPageAddress(orig);
\r
758 // Steal the address
\r
759 newpage->emsPage = origpage->emsPage;
\r
760 newpage->mainPage = origpage->mainPage;
\r
762 // Mark replaced page as purged
\r
763 origpage->mainPage = origpage->emsPage = -1;
\r
766 Quit("PML_TransferPageSpace: Zero replacement");
\r
772 // PML_GetAPageBuffer() - A page buffer is needed. Either get it from the
\r
773 // main/EMS free pool, or use PML_GiveLRUPage() to find which page to
\r
774 // steal the buffer from. Returns a far pointer to the page buffer, and
\r
775 // sets the fields inside the given page structure appropriately.
\r
776 // If mainonly is true, free EMS will be ignored, and only main pages
\r
777 // will be looked at by PML_GiveLRUPage().
\r
780 PML_GetAPageBuffer(int pagenum,boolean mainonly)
\r
782 byte far *addr = nil;
\r
785 PageListStruct far *page;
\r
787 page = &PMPages[pagenum];
\r
788 if ((EMSPagesUsed < EMSPagesAvail) && !mainonly)
\r
790 // There's remaining EMS - use it
\r
791 page->emsPage = EMSPagesUsed++;
\r
792 addr = PML_GetEMSAddress(page->emsPage,page->locked);
\r
794 else if (MainPagesUsed < MainPagesAvail)
\r
796 // There's remaining main memory - use it
\r
797 for (i = 0,n = -1,used = MainMemUsed;i < PMMaxMainMem;i++,used++)
\r
799 if ((*used & pmba_Allocated) && !(*used & pmba_Used))
\r
802 *used |= pmba_Used;
\r
807 Quit("PML_GetPageBuffer: MainPagesAvail lied");
\r
808 addr = MainMemPages[n];
\r
810 Quit("PML_GetPageBuffer: Purged main block");
\r
811 page->mainPage = n;
\r
815 addr = PML_TransferPageSpace(PML_GiveLRUPage(mainonly),pagenum);
\r
818 Quit("PML_GetPageBuffer: Search failed");
\r
823 // PML_GetPageFromXMS() - If page is in XMS, find LRU main/EMS page and
\r
824 // replace it with the page from XMS. If mainonly is true, will only
\r
825 // search for LRU main page.
\r
826 // XMSProtectPage is set to the page to be retrieved from XMS, so that if
\r
827 // the page from which we're stealing the main/EMS from isn't in XMS,
\r
828 // it won't copy over the page that we're trying to get from XMS.
\r
829 // (pages that are being purged are copied into XMS, if possible)
\r
832 PML_GetPageFromXMS(int pagenum,boolean mainonly)
\r
834 byte far *checkaddr;
\r
836 PageListStruct far *page;
\r
838 page = &PMPages[pagenum];
\r
839 if (XMSPresent && (page->xmsPage != -1))
\r
841 XMSProtectPage = pagenum;
\r
842 checkaddr = PML_GetAPageBuffer(pagenum,mainonly);
\r
843 if (FP_OFF(checkaddr))
\r
844 Quit("PML_GetPageFromXMS: Non segment pointer");
\r
845 addr = (memptr)FP_SEG(checkaddr);
\r
846 PML_CopyFromXMS(addr,page->xmsPage,page->length);
\r
847 XMSProtectPage = -1;
\r
854 // PML_LoadPage() - A page is not in main/EMS memory, and it's not in XMS.
\r
855 // Load it into either main or EMS. If mainonly is true, the page will
\r
856 // only be loaded into main.
\r
859 PML_LoadPage(int pagenum,boolean mainonly)
\r
862 PageListStruct far *page;
\r
864 addr = PML_GetAPageBuffer(pagenum,mainonly);
\r
865 page = &PMPages[pagenum];
\r
866 PML_ReadFromFile(addr,page->offset,page->length);
\r
870 // PM_GetPage() - Returns the address of the page, loading it if necessary
\r
871 // First, check if in Main Memory or EMS
\r
873 // If not in XMS, load into Main Memory or EMS
\r
877 PM_GetPage(int pagenum)
\r
881 if (pagenum >= ChunksInFile)
\r
882 Quit("PM_GetPage: Invalid page request");
\r
884 #if 0 // for debugging
\r
885 asm mov dx,STATUS_REGISTER_1
\r
887 asm mov dx,ATR_INDEX
\r
888 asm mov al,ATR_OVERSCAN
\r
890 asm mov al,10 // bright green
\r
894 if (!(result = PM_GetPageAddress(pagenum)))
\r
896 boolean mainonly = (pagenum >= PMSoundStart);
\r
897 if (!PMPages[pagenum].offset) // JDC: sparse page
\r
898 Quit ("Tried to load a sparse page!");
\r
899 if (!(result = PML_GetPageFromXMS(pagenum,mainonly)))
\r
901 if (PMPages[pagenum].lastHit == PMFrameCount)
\r
904 PML_LoadPage(pagenum,mainonly);
\r
905 result = PM_GetPageAddress(pagenum);
\r
908 PMPages[pagenum].lastHit = PMFrameCount;
\r
910 #if 0 // for debugging
\r
911 asm mov dx,STATUS_REGISTER_1
\r
913 asm mov dx,ATR_INDEX
\r
914 asm mov al,ATR_OVERSCAN
\r
916 asm mov al,3 // blue
\r
918 asm mov al,0x20 // normal
\r
927 // PM_SetPageLock() - Sets the lock type on a given page
\r
928 // pml_Unlocked: Normal, page can be purged
\r
929 // pml_Locked: Cannot be purged
\r
930 // pml_EMS?: Same as pml_Locked, but if in EMS, use the physical page
\r
931 // specified when returning the address. For sound stuff.
\r
934 PM_SetPageLock(int pagenum,PMLockType lock)
\r
936 if (pagenum < PMSoundStart)
\r
937 Quit("PM_SetPageLock: Locking/unlocking non-sound page");
\r
939 PMPages[pagenum].locked = lock;
\r
943 // PM_Preload() - Loads as many pages as possible into all types of memory.
\r
944 // Calls the update function after each load, indicating the current
\r
945 // page, and the total pages that need to be loaded (for thermometer).
\r
948 PM_Preload(boolean (*update)(word current,word total))
\r
952 word current,total,
\r
953 totalnonxms,totalxms,
\r
954 mainfree,maintotal,
\r
958 PageListStruct far *p;
\r
960 mainfree = (MainPagesAvail - MainPagesUsed) + (EMSPagesAvail - EMSPagesUsed);
\r
961 xmsfree = (XMSPagesAvail - XMSPagesUsed);
\r
963 xmstotal = maintotal = 0;
\r
965 for (i = 0;i < ChunksInFile;i++)
\r
967 if (!PMPages[i].offset)
\r
968 continue; // sparse
\r
970 if ( PMPages[i].emsPage != -1 || PMPages[i].mainPage != -1 )
\r
971 continue; // already in main mem
\r
978 else if ( xmsfree && (PMPages[i].xmsPage == -1) )
\r
986 total = maintotal + xmstotal;
\r
995 // cache main/ems blocks
\r
999 while ( !PMPages[page].offset || PMPages[page].mainPage != -1
\r
1000 || PMPages[page].emsPage != -1 )
\r
1003 if (page >= ChunksInFile)
\r
1004 Quit ("PM_Preload: Pages>=ChunksInFile");
\r
1011 update(current,total);
\r
1015 // load stuff to XMS
\r
1019 for (oogypage = 0 ; PMPages[oogypage].mainPage == -1 ; oogypage++)
\r
1021 addr = PM_GetPage(oogypage);
\r
1023 Quit("PM_Preload: XMS buffer failed");
\r
1027 while ( !PMPages[page].offset || PMPages[page].xmsPage != -1 )
\r
1030 if (page >= ChunksInFile)
\r
1031 Quit ("PM_Preload: Pages>=ChunksInFile");
\r
1033 p = &PMPages[page];
\r
1035 p->xmsPage = XMSPagesUsed++;
\r
1036 if (XMSPagesUsed > XMSPagesAvail)
\r
1037 Quit("PM_Preload: Exceeded XMS pages");
\r
1038 if (p->length > PMPageSize)
\r
1039 Quit("PM_Preload: Page too long");
\r
1041 PML_ReadFromFile((byte far *)addr,p->offset,p->length);
\r
1042 PML_CopyToXMS((byte far *)addr,p->xmsPage,p->length);
\r
1047 update(current,total);
\r
1050 p = &PMPages[oogypage];
\r
1051 PML_ReadFromFile((byte far *)addr,p->offset,p->length);
\r
1054 update(total,total);
\r
1057 /////////////////////////////////////////////////////////////////////////////
\r
1061 /////////////////////////////////////////////////////////////////////////////
\r
1064 // PM_NextFrame() - Increments the frame counter and adjusts the thrash
\r
1065 // avoidence variables
\r
1067 // If currently in panic mode (to avoid thrashing), check to see if the
\r
1068 // appropriate number of frames have passed since the last time that
\r
1069 // we would have thrashed. If so, take us out of panic mode.
\r
1073 PM_NextFrame(void)
\r
1077 // Frame count overrun - kill the LRU hit entries & reset frame count
\r
1078 if (++PMFrameCount >= MAXLONG - 4)
\r
1080 for (i = 0;i < PMNumBlocks;i++)
\r
1081 PMPages[i].lastHit = 0;
\r
1086 for (i = 0;i < PMSoundStart;i++)
\r
1088 if (PMPages[i].locked)
\r
1091 sprintf(buf,"PM_NextFrame: Page %d is locked",i);
\r
1099 // DEBUG - set border color
\r
1100 if ((!PMThrashing) && (!--PMPanicMode))
\r
1102 // DEBUG - reset border color
\r
1105 if (PMThrashing >= PMThrashThreshold)
\r
1106 PMPanicMode = PMUnThrashThreshold;
\r
1107 PMThrashing = false;
\r
1111 // PM_Reset() - Sets up caching structures
\r
1117 PageListStruct far *page;
\r
1119 XMSPagesAvail = XMSAvail / PMPageSizeKB;
\r
1121 EMSPagesAvail = EMSAvail * (EMSPageSizeKB / PMPageSizeKB);
\r
1122 EMSPhysicalPage = 0;
\r
1124 MainPagesUsed = EMSPagesUsed = XMSPagesUsed = 0;
\r
1126 PMPanicMode = false;
\r
1128 // Initialize page list
\r
1129 for (i = 0,page = PMPages;i < PMNumBlocks;i++,page++)
\r
1131 page->mainPage = -1;
\r
1132 page->emsPage = -1;
\r
1133 page->xmsPage = -1;
\r
1134 page->locked = false;
\r
1139 // PM_Startup() - Start up the Page Mgr
\r
1144 boolean nomain,noems,noxms;
\r
1150 nomain = noems = noxms = false;
\r
1151 for (i = 1;i < _argc;i++)
\r
1153 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1167 PML_OpenPageFile();
\r
1174 if (nomain && !EMSPresent)
\r
1175 Quit("PM_Startup: No main or EMS");
\r
1177 PML_StartupMainMem();
\r
1185 // PM_Shutdown() - Shut down the Page Mgr
\r
1190 PML_ShutdownXMS();
\r
1191 PML_ShutdownEMS();
\r
1196 PML_ClosePageFile();
\r
1198 PML_ShutdownMainMem();
\r