1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
25 // Id Engine's Page Manager v1.0
\r
26 // Primary coder: Jason Blochowiak
\r
29 #include "src/lib/16_pm.h"
\r
32 // Main Mem specific variables
\r
33 //boolean MainPresent;
\r
34 /* memptr MainMemPages[PMMaxMainMem];
\r
35 PMBlockAttr gvar->pm.mm.MainMemUsed[PMMaxMainMem];
\r
36 int gvar->pm.mm.MainPagesAvail;*/
\r
38 // EMS specific variables
\r
39 //boolean EMSPresent;
\r
40 /* word gvar->pm.emm.EMSAvail,gvar->pm.emm.EMSPagesAvail,gvar->pm.emm.EMSHandle,
\r
41 gvar->pm.emm.EMSPageFrame,gvar->pm.emm.EMSPhysicalPage;
\r
42 gvar->pm.emm.EMSListStruct gvar->pm.emm.EMSList[EMSFrameCount];*/
\r
44 // XMS specific variables
\r
45 //boolean XMSPresent;
\r
46 //word gvar->pm.xmm.XMSAvail,gvar->pm.xmm.XMSPagesAvail,gvar->pm.xmm.XMSHandle;
\r
47 dword XMSDriver; //hard to put in gvar
\r
48 word XMSVer; //hard to put in gvar
\r
49 /* int gvar->pm.xmm.XMSProtectPage = -1;
\r
51 // File specific variables
\r
52 char gvar->pm.fi.PageFileName[13] = {"VSWAP."};
\r
54 word gvar->pm.fi.ChunksInFile;
\r
55 word PMSpriteStart,PMSoundStart;*/
\r
57 // General usage variables
\r
58 /* boolean PMStarted,
\r
59 gvar->pm.PMPanicMode,
\r
60 gvar->pm.PMThrashing;
\r
61 word gvar->pm.XMSPagesUsed,
\r
62 gvar->pm.EMSPagesUsed,
\r
64 gvar->pm.PMNumBlocks;
\r
66 PageListStruct far *gvar->pm.PMPages,
\r
67 _seg *gvar->pm.PMSegPages;*/
\r
69 static char *ParmStrings[] = {"nomain","noems","noxms",nil};
\r
71 /////////////////////////////////////////////////////////////////////////////
\r
73 // EMS Management code
\r
75 /////////////////////////////////////////////////////////////////////////////
\r
78 // PML_MapEMS() - Maps a logical page to a physical page
\r
81 PML_MapEMS(word logical, byte physical, global_game_variables_t *gvar)
\r
83 byte err=0, str[160];
\r
87 boolean errorflag=false;
\r
88 EMShandle=gvar->pm.emm.EMSHandle;
\r
103 #ifdef __BORLANDC__
\r
108 #ifdef __BORLANDC__
\r
115 if(errorflag==true)
\r
117 strcpy(str,"MM_MapEMS: EMS error ");
\r
118 MM_EMSerr(str, err);
\r
119 printf("%s\n",str);
\r
120 Quit (gvar, "PML_MapEMS: Page mapping failed\n");
\r
127 // PML_StartupEMS() - Sets up EMS for Page Mgr's use
\r
128 // Checks to see if EMS driver is present
\r
129 // Verifies that EMS hardware is present
\r
130 // Make sure that EMS version is 3.2 or later
\r
131 // If there's more than our minimum (2 pages) available, allocate it (up
\r
132 // to the maximum we need)
\r
134 // Please call MML_CheckForEMS() before calling this function.
\r
135 // MML_CheckForEMS is not local despite the name wwww.
\r
139 PML_StartupEMS(global_game_variables_t *gvar)
\r
142 #ifdef __PM__NOHOGEMS__
\r
145 byte err=0, str[64];
\r
147 boolean errorflag=false;
\r
149 unsigned totalEMSpages,freeEMSpages,EMSPageFrame,EMSHandle,EMSAvail;
\r
150 totalEMSpages = freeEMSpages = EMSPageFrame = EMSHandle = EMSAvail = EMSVer = 0; // set all to 0~
\r
151 gvar->pm.emm.EMSPresent = false; // Assume that we'll fail
\r
152 gvar->pm.emm.EMSAvail = gvar->mmi.EMSmem = 0;
\r
155 //MML_CheckForEMS() takes care of what the code did here
\r
158 jc error1 // make sure EMS hardware is present
\r
161 int EMS_INT // only work on EMS 3.2 or greater (silly, but...)
\r
164 mov [EMSVer],ax // set EMSVer
\r
165 cmp al,0x32 // only work on ems 3.2 or greater
\r
168 mov ah,EMS_GETFRAME
\r
169 int EMS_INT // find the page frame address
\r
172 mov [EMSPageFrame],bx
\r
174 mov ah,EMS_GETPAGES
\r
175 int EMS_INT // find out how much EMS is there
\r
179 jz noEMS // no EMS at all to allocate
\r
181 jl noEMS // Require at least 2 pages (32k)
\r
182 mov [totalEMSpages],dx
\r
183 mov [freeEMSpages],bx
\r
186 #ifdef __BORLANDC__
\r
190 #ifdef __BORLANDC__
\r
196 #ifdef __BORLANDC__
\r
204 #ifdef __PM__NOHOGEMS__
\r
205 if(errorflag==false)
\r
207 // Don't hog all available EMS
\r
208 size = gvar->pm.emm.EMSAvail * (long)EMSPageSize;
\r
209 if (size - (EMSPageSize * 2) > (gvar->pm.fi.ChunksInFile * (long)PMPageSize))
\r
211 size = (gvar->pm.fi.ChunksInFile * (long)PMPageSize) + EMSPageSize;
\r
212 gvar->pm.emm.EMSAvail = size / EMSPageSize;
\r
217 mov ah,EMS_ALLOCPAGES
\r
224 #ifdef __BORLANDC__
\r
228 #ifdef __BORLANDC__
\r
234 #ifdef __BORLANDC__
\r
241 if(errorflag==true)
\r
243 strcpy(str,"PML_StartupEMS: EMS error ");
\r
244 MM_EMSerr(str, err);
\r
245 printf("%s\n",str);
\r
246 return(gvar->pm.emm.EMSPresent);
\r
248 gvar->mmi.EMSmem = EMSAvail * (dword)EMSPageSize;
\r
250 // Initialize EMS mapping cache
\r
251 for (i = 0;i < EMSFrameCount;i++)
\r
252 gvar->pm.emm.EMSList[i].baseEMSPage = -1;
\r
254 gvar->pm.emm.EMSPresent = true; // We have EMS
\r
255 gvar->pm.emm.EMSPageFrame = EMSPageFrame;
\r
256 gvar->pm.emm.EMSAvail = EMSAvail;
\r
257 gvar->pm.emm.EMSVer = EMSVer;
\r
258 gvar->pm.emm.EMSHandle = EMSHandle;
\r
259 gvar->pm.emm.freeEMSpages = freeEMSpages;
\r
260 gvar->pm.emm.totalEMSpages = totalEMSpages;
\r
262 return(gvar->pm.emm.EMSPresent);
\r
266 // PML_ShutdownEMS() - If EMS was used, deallocate it
\r
269 PML_ShutdownEMS(global_game_variables_t *gvar)
\r
272 byte err=0, str[64];
\r
274 boolean errorflag=false;
\r
275 EMSHandle=gvar->pm.emm.EMSHandle;
\r
277 if (gvar->pm.emm.EMSPresent)
\r
280 mov ah,EMS_FREEPAGES
\r
285 #ifdef __BORLANDC__
\r
289 #ifdef __BORLANDC__
\r
295 #ifdef __BORLANDC__
\r
302 if(errorflag==true)
\r
304 strcpy(str,"PML_ShutdownEMS: Error freeing EMS ");
\r
305 MM_EMSerr(str, err);
\r
306 printf("%s\n",str);
\r
307 Quit (gvar, "PML_ShutdownEMS: Error freeing EMS\n");
\r
313 /////////////////////////////////////////////////////////////////////////////
\r
315 // XMS Management code
\r
317 /////////////////////////////////////////////////////////////////////////////
\r
320 // PML_StartupXMS() - Starts up XMS for the Page Mgr's use
\r
321 // Checks for presence of an XMS driver
\r
322 // Makes sure that there's at least a page of XMS available
\r
323 // Allocates any remaining XMS (rounded down to the nearest page size)
\r
326 PML_StartupXMS(global_game_variables_t *gvar)
\r
328 //TODO: translate the _REG into working assembly
\r
329 //#define STARTUPXMSASM
\r
331 word XMSAvail, XMSHandle;//, XMSVer;
\r
332 boolean errorflag=false;
\r
334 gvar->pm.xmm.XMSPresent = false; // Assume failure
\r
335 XMSAvail = gvar->mmi.XMSmem = 0;
\r
339 int XMS_INT // Check for presence of XMS driver
\r
345 int XMS_INT // Get address of XMS driver
\r
346 mov [WORD PTR XMSDriver],bx
\r
347 mov [WORD PTR XMSDriver+2],es // function pointer to XMS driver
\r
350 call [DWORD PTR XMSDriver] //; Get XMS Version Number
\r
354 #ifdef STARTUPXMSASM
\r
355 mov ah,XMS_QUERYFREE // Find out how much XMS is available
\r
356 call [DWORD PTR XMSDriver]
\r
358 or ax,ax // AJR: bugfix 10/8/92
\r
363 #ifdef __BORLANDC__
\r
367 #ifdef __BORLANDC__
\r
373 #ifdef __BORLANDC__
\r
380 if(errorflag==true) goto error;
\r
381 #ifndef STARTUPXMSASM
\r
382 XMS_CALL(XMS_QUERYFREE); // Find out how much XMS is available
\r
384 if (!_AX) // AJR: bugfix 10/8/92
\r
393 #ifdef __DEBUG_PM__
\r
394 printf("XMSVer=%02X ", XMSVer);
\r
395 printf("XMSAvail=%u\n", XMSAvail);
\r
398 XMSAvail &= ~(PMPageSizeKB - 1); // Round off to nearest page size
\r
399 if (XMSAvail < (PMPageSizeKB * 2)) // Need at least 2 pages
\r
404 #ifdef STARTUPXMSASM
\r
407 mov ah,XMS_ALLOC // And do the allocation
\r
408 call [DWORD PTR XMSDriver]
\r
410 or ax,ax // AJR: bugfix 10/8/92
\r
414 #ifdef __BORLANDC__
\r
418 #ifdef __BORLANDC__
\r
424 #ifdef __BORLANDC__
\r
433 XMS_CALL(XMS_ALLOC); // And do the allocation
\r
435 if (!_AX) // AJR: bugfix 10/8/92
\r
444 if(errorflag==false)
\r
446 gvar->mmi.XMSmem = (dword)(XMSAvail) * 1024;
\r
447 gvar->pm.xmm.XMSAvail = XMSAvail;
\r
448 gvar->pm.xmm.XMSHandle = XMSHandle;
\r
449 //gvar->pm.xmm.XMSVer = XMSVer;
\r
450 gvar->pm.xmm.XMSPresent = true;
\r
451 #ifdef __DEBUG_PM__
\r
452 printf(" XMSmem=%lu XMSAvail=%u\n", gvar->mmi.XMSmem, XMSAvail);
\r
457 #ifdef __DEBUG_PM__
\r
458 //printf("XMSHandle\n");
\r
459 //printf(" 1=%u 2=%u 3=%u 4=%u\n", XMSHandle1, XMSHandle2, XMSHandle3, XMSHandle4);
\r
460 //printf(" 2=%u ", XMSHandle);
\r
461 //printf(" %u", gvar->pm.xmm.XMSHandle);
\r
462 printf(" err=%02X e=%u\n", err, e);
\r
465 return(gvar->pm.xmm.XMSPresent);
\r
469 // PML_XMSCopy() - Copies a main/EMS page to or from XMS
\r
470 // Will round an odd-length request up to the next even value
\r
473 PML_XMSCopy(boolean toxms,byte far *addr,word xmspage,word length, global_game_variables_t *gvar)
\r
479 word source_handle;
\r
480 dword source_offset;
\r
481 word target_handle;
\r
482 dword target_offset;
\r
487 Quit (gvar, "PML_XMSCopy: zero address\n");
\r
490 xoffset = (dword)xmspage * PMPageSize;
\r
492 copy.length = (length + 1) & ~1;
\r
493 copy.source_handle = toxms? 0 : gvar->pm.xmm.XMSHandle;
\r
494 copy.source_offset = toxms? (long)addr : xoffset;
\r
495 copy.target_handle = toxms? gvar->pm.xmm.XMSHandle : 0;
\r
496 copy.target_offset = toxms? xoffset : (long)addr;
\r
502 XMS_CALL(XMS_MOVE);
\r
508 Quit (gvar, "PML_XMSCopy: Error on copy");
\r
514 #define PML_CopyToXMS(s,t,l,gvar) PML_XMSCopy(true,(s),(t),(l),(gvar))
\r
515 #define PML_CopyFromXMS(t,s,l,gvar) PML_XMSCopy(false,(t),(s),(l),(gvar))
\r
518 // PML_CopyToXMS() - Copies the specified number of bytes from the real mode
\r
519 // segment address to the specified XMS page
\r
522 PML_CopyToXMS(byte far *source,int targetpage,word length,global_game_variables_t *gvar)
\r
524 PML_XMSCopy(true,source,targetpage,length, global_game_variables_t *gvar);
\r
528 // PML_CopyFromXMS() - Copies the specified number of bytes from an XMS
\r
529 // page to the specified real mode address
\r
532 PML_CopyFromXMS(byte far *target,int sourcepage,word length, global_game_variables_t *gvar)
\r
534 PML_XMSCopy(false,target,sourcepage,length, global_game_variables_t *gvar);
\r
539 // PML_ShutdownXMS()
\r
542 PML_ShutdownXMS(global_game_variables_t *gvar)
\r
544 boolean errorflag=false;
\r
545 word XMSHandle = gvar->pm.xmm.XMSHandle;
\r
546 if (gvar->pm.xmm.XMSPresent)
\r
550 //XMS_CALL(XMS_FREE);
\r
552 call [DWORD PTR XMSDriver]
\r
556 #ifdef __BORLANDC__
\r
560 #ifdef __BORLANDC__
\r
566 #ifdef __BORLANDC__
\r
573 if(errorflag==true)
\r
575 Quit (gvar, "PML_ShutdownXMS: Error freeing XMS");
\r
581 /////////////////////////////////////////////////////////////////////////////
\r
583 // Main memory code
\r
585 /////////////////////////////////////////////////////////////////////////////
\r
588 // PM_SetMainMemPurge() - Sets the purge level for all allocated main memory
\r
589 // blocks. This shouldn't be called directly - the PM_LockMainMem() and
\r
590 // PM_UnlockMainMem() macros should be used instead.
\r
593 PM_SetMainMemPurge(int level, global_game_variables_t *gvar)
\r
597 if(gvar->pm.mm.MainPresent)
\r
598 for (i = 0;i < PMMaxMainMem;i++)
\r
600 #ifdef __DEBUG_PM__
\r
601 #ifdef __DEBUG_PM_MAIN__
\r
602 printf("PM_SetMainMemPurge() info of gvar->pm.mm.MainMemPages[i]\n");
\r
603 printf("& %Fp, %Fp\n", &gvar->pm.mm.MainMemPages[i], &(gvar->pm.mm.MainMemPages[i]));
\r
606 if (gvar->pm.mm.MainMemPages[i])
\r
607 MM_SetPurge(&(gvar->pm.mm.MainMemPages[i]),level, gvar);
\r
612 Quit (gvar, "MainPresent IS NULL\n");
\r
617 // PM_CheckMainMem() - If something besides the Page Mgr makes requests of
\r
618 // the Memory Mgr, some of the Page Mgr's blocks may have been purged,
\r
619 // so this function runs through the block list and checks to see if
\r
620 // any of the blocks have been purged. If so, it marks the corresponding
\r
621 // page as purged & unlocked, then goes through the block list and
\r
622 // tries to reallocate any blocks that have been purged.
\r
623 // This routine now calls PM_LockMainMem() to make sure that any allocation
\r
624 // attempts made during the block reallocation sweep don't purge any
\r
625 // of the other blocks. Because PM_LockMainMem() is called,
\r
626 // PM_UnlockMainMem() needs to be called before any other part of the
\r
627 // program makes allocation requests of the Memory Mgr.
\r
630 PM_CheckMainMem(global_game_variables_t *gvar)
\r
632 boolean allocfailed;
\r
636 PageListStruct far *page;
\r
638 if (!gvar->pm.mm.MainPresent)
\r
641 for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++)
\r
643 n = page->mainPage;
\r
644 if (n != -1) // Is the page using main memory?
\r
646 if (!gvar->pm.mm.MainMemPages[n]) // Yep, was the block purged?
\r
648 page->mainPage = -1; // Yes, mark page as purged & unlocked
\r
649 page->locked = pml_Unlocked;
\r
654 // Prevent allocation attempts from purging any of our other blocks
\r
655 PM_LockMainMem(gvar);
\r
656 allocfailed = false;
\r
657 for (i = 0,p = gvar->pm.mm.MainMemPages,used = gvar->pm.mm.MainMemUsed; i < PMMaxMainMem;i++,p++,used++)
\r
659 if (!*p) // If the page got purged
\r
661 if (*used & pmba_Allocated) // If it was allocated
\r
663 *used &= ~pmba_Allocated; // Mark as unallocated
\r
664 gvar->pm.mm.MainPagesAvail--; // and decrease available count
\r
667 if (*used & pmba_Used) // If it was used
\r
669 *used &= ~pmba_Used; // Mark as unused
\r
670 gvar->pm.MainPagesUsed--; // and decrease used count
\r
675 MM_BombOnError(false, gvar);
\r
676 MM_GetPtr(p,PMPageSize, gvar); // Try to reallocate
\r
677 if (gvar->mm.mmerror) // If it failed,
\r
678 allocfailed = true; // don't try any more allocations
\r
679 else // If it worked,
\r
681 *used |= pmba_Allocated; // Mark as allocated
\r
682 gvar->pm.mm.MainPagesAvail++; // and increase available count
\r
684 MM_BombOnError(true, gvar);
\r
688 if (gvar->mm.mmerror)
\r
689 gvar->mm.mmerror = false;
\r
693 // PML_StartupMainMem() - Allocates as much main memory as is possible for
\r
694 // the Page Mgr. The memory is allocated as non-purgeable, so if it's
\r
695 // necessary to make requests of the Memory Mgr, PM_UnlockMainMem()
\r
696 // needs to be called.
\r
699 PML_StartupMainMem(global_game_variables_t *gvar)
\r
704 gvar->pm.mm.MainPagesAvail = 0;
\r
705 gvar->pm.mm.MainPresent = false;
\r
706 MM_BombOnError(false, gvar);
\r
707 for (i = 0,p = gvar->pm.mm.MainMemPages;i < PMMaxMainMem;i++,p++)
\r
709 MM_GetPtr(p,PMPageSize, gvar);
\r
710 if (gvar->mm.mmerror)
\r
713 gvar->pm.mm.MainPagesAvail++;
\r
714 gvar->pm.mm.MainMemUsed[i] = pmba_Allocated;
\r
716 MM_BombOnError(true, gvar);
\r
717 if (gvar->mm.mmerror)
\r
718 gvar->mm.mmerror = false;
\r
719 if (gvar->pm.mm.MainPagesAvail < PMMinMainMem)
\r
721 Quit (gvar, "PM_SetupMainMem: Not enough main memory");
\r
724 gvar->pm.mm.MainPresent = true;
\r
728 // PML_ShutdownMainMem() - Frees all of the main memory blocks used by the
\r
732 PML_ShutdownMainMem(global_game_variables_t *gvar)
\r
737 // DEBUG - mark pages as unallocated & decrease page count as appropriate
\r
738 for (i = 0,p = gvar->pm.mm.MainMemPages;i < PMMaxMainMem;i++,p++)
\r
740 MM_FreePtr(p, gvar);
\r
743 /////////////////////////////////////////////////////////////////////////////
\r
745 // File management code
\r
747 /////////////////////////////////////////////////////////////////////////////
\r
750 // PML_ReadFromFile() - Reads some data in from the page file
\r
753 PML_ReadFromFile(byte far *buf,long offset,word length, global_game_variables_t *gvar)
\r
757 Quit (gvar, "PML_ReadFromFile: Null pointer");
\r
762 Quit (gvar, "PML_ReadFromFile: Zero offset");
\r
765 if (lseek(gvar->pm.fi.PageFile,offset,SEEK_SET) != offset)
\r
767 Quit (gvar, "PML_ReadFromFile: Seek failed");
\r
770 if (!CA_FarRead(gvar->pm.fi.PageFile,buf,length, gvar))
\r
772 Quit (gvar, "PML_ReadFromFile: Read failed");
\r
778 // PML_OpenPageFile() - Opens the page file and sets up the page info
\r
782 PML_OpenPageFile(global_game_variables_t *gvar)
\r
787 dword far *offsetptr;
\r
788 word far *lengthptr;
\r
789 PageListStruct far *page;
\r
791 gvar->pm.fi.PageFile = open(gvar->pm.fi.PageFileName,O_RDONLY + O_BINARY);
\r
792 if (gvar->pm.fi.PageFile == -1)
\r
794 Quit (gvar, "PML_OpenPageFile: Unable to open page file");
\r
798 // Read in header variables
\r
799 read(gvar->pm.fi.PageFile,&gvar->pm.fi.ChunksInFile,sizeof(gvar->pm.fi.ChunksInFile));
\r
800 read(gvar->pm.fi.PageFile,&gvar->pm.fi.PMSpriteStart,sizeof(gvar->pm.fi.PMSpriteStart));
\r
801 read(gvar->pm.fi.PageFile,&gvar->pm.fi.PMSoundStart,sizeof(gvar->pm.fi.PMSoundStart));
\r
803 // Allocate and clear the page list
\r
804 gvar->pm.PMNumBlocks = gvar->pm.fi.ChunksInFile;
\r
805 MM_GetPtr((memptr *)&gvar->pm.PMSegPages, sizeof(PageListStruct) * (gvar->pm.PMNumBlocks), gvar);
\r
806 MM_SetLock((memptr *)&gvar->pm.PMSegPages,true, gvar);
\r
807 gvar->pm.PMPages = (PageListStruct far *)gvar->pm.PMSegPages;
\r
808 _fmemset(gvar->pm.PMPages,0,sizeof(PageListStruct) * gvar->pm.PMNumBlocks);
\r
810 // Read in the chunk offsets
\r
811 size = sizeof(dword) * gvar->pm.fi.ChunksInFile;
\r
812 MM_GetPtr((memptr *)&buf, size, gvar);
\r
813 if (!CA_FarRead(gvar->pm.fi.PageFile,(byte far *)buf,size, gvar))
\r
815 Quit (gvar, "PML_OpenPageFile: Offset read failed");
\r
818 offsetptr = (dword far *)buf;
\r
819 for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++)
\r
820 page->offset = *offsetptr++;
\r
821 MM_FreePtr((memptr *)&buf, gvar);
\r
823 // Read in the chunk lengths
\r
824 size = sizeof(word) * gvar->pm.fi.ChunksInFile;
\r
825 MM_GetPtr(&buf,size, gvar);
\r
826 if (!CA_FarRead(gvar->pm.fi.PageFile,(byte far *)buf,size, gvar))
\r
828 Quit (gvar, "PML_OpenPageFile: Length read failed");
\r
831 lengthptr = (word far *)buf;
\r
832 for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++)
\r
833 page->length = *lengthptr++;
\r
834 MM_FreePtr((memptr *)&buf, gvar);
\r
838 // PML_ClosePageFile() - Closes the page file
\r
841 PML_ClosePageFile(global_game_variables_t *gvar)
\r
843 if (gvar->pm.fi.PageFile != -1)
\r
844 close(gvar->pm.fi.PageFile);
\r
845 if (gvar->pm.PMSegPages)
\r
847 MM_SetLock((memptr *)&gvar->pm.PMSegPages,false, gvar);
\r
848 MM_FreePtr((void _seg *)&gvar->pm.PMSegPages, gvar);
\r
853 /////////////////////////////////////////////////////////////////////////////
\r
855 // Allocation, etc., code
\r
857 /////////////////////////////////////////////////////////////////////////////
\r
860 // PML_GetEMSAddress()
\r
862 // Page is in EMS, so figure out which EMS physical page should be used
\r
863 // to map our page in. If normal page, use EMS physical page 3, else
\r
864 // use the physical page specified by the lock type
\r
866 #ifndef __DEBUG_2__
\r
867 #pragma argsused // DEBUG - remove lock parameter
\r
869 PML_GetEMSAddress(int page,PMLockType lock, global_game_variables_t *gvar)
\r
872 word emsoff,emsbase,offset;
\r
874 emsoff = page & (PMEMSSubPage - 1);
\r
875 emsbase = page - emsoff;
\r
878 // See if this page is already mapped in
\r
879 for (i = 0;i < EMSFrameCount;i++)
\r
881 if (gvar->pm.emm.EMSList[i].baseEMSPage == emsbase)
\r
883 emspage = i; // Yep - don't do a redundant remapping
\r
888 // If page isn't already mapped in, find LRU EMS frame, and use it
\r
891 dword last = LONG_MAX;
\r
892 for (i = 0;i < EMSFrameCount;i++)
\r
894 if (gvar->pm.emm.EMSList[i].lastHit < last)
\r
897 last = gvar->pm.emm.EMSList[i].lastHit;
\r
901 gvar->pm.emm.EMSList[emspage].baseEMSPage = emsbase;
\r
902 PML_MapEMS(page / PMEMSSubPage,emspage, gvar);
\r
906 Quit (gvar, "PML_GetEMSAddress: EMS find failed");
\r
908 gvar->pm.emm.EMSList[emspage].lastHit = gvar->pm.PMFrameCount;
\r
909 offset = emspage * EMSPageSizeSeg;
\r
910 offset += emsoff * PMPageSizeSeg;
\r
911 return((memptr)(gvar->pm.emm.EMSPageFrame + offset));
\r
915 PML_GetEMSAddress(int page,PMLockType lock, global_game_variables_t *gvar)
\r
919 emspage = (lock < pml_EMSLock)? 3 : (lock - pml_EMSLock);
\r
921 PML_MapEMS(page / PMEMSSubPage,emspage);
\r
923 return((memptr)(EMSPageFrame + (emspage * EMSPageSizeSeg)
\r
924 + ((page & (PMEMSSubPage - 1)) * PMPageSizeSeg)));
\r
929 // PM_GetPageAddress() - Returns the address of a given page
\r
930 // Maps in EMS if necessary
\r
931 // Returns nil if block isn't cached into Main Memory or EMS
\r
935 PM_GetPageAddress(int pagenum, global_game_variables_t *gvar)
\r
937 PageListStruct far *page;
\r
939 page = &gvar->pm.PMPages[pagenum];
\r
940 if (page->mainPage != -1)
\r
941 return(gvar->pm.mm.MainMemPages[page->mainPage]);
\r
942 else if (page->emsPage != -1)
\r
943 return(PML_GetEMSAddress(page->emsPage,page->locked, gvar));
\r
949 // PML_GiveLRUPage() - Returns the page # of the least recently used
\r
950 // present & unlocked main/EMS page (or main page if mainonly is true)
\r
953 PML_GiveLRUPage(boolean mainonly, global_game_variables_t *gvar)
\r
957 PageListStruct far *page;
\r
959 for (i = 0,page = gvar->pm.PMPages,lru = -1,last = LONG_MAX;i < gvar->pm.fi.ChunksInFile;i++,page++)
\r
963 (page->lastHit < last)
\r
964 && ((page->emsPage != -1) || (page->mainPage != -1))
\r
965 && (page->locked == pml_Unlocked)
\r
966 && (!(mainonly && (page->mainPage == -1)))
\r
969 last = page->lastHit;
\r
975 Quit (gvar, "PML_GiveLRUPage: LRU Search failed");
\r
980 // PML_GiveLRUXMSPage() - Returns the page # of the least recently used
\r
981 // (and present) XMS page.
\r
982 // This routine won't return the XMS page protected (by XMSProtectPage)
\r
985 PML_GiveLRUXMSPage(global_game_variables_t *gvar)
\r
989 PageListStruct far *page;
\r
991 for (i = 0,page = gvar->pm.PMPages,lru = -1,last = LONG_MAX;i < gvar->pm.fi.ChunksInFile;i++,page++)
\r
995 (page->xmsPage != -1)
\r
996 && (page->lastHit < last)
\r
997 && (i != gvar->pm.xmm.XMSProtectPage)
\r
1000 last = page->lastHit;
\r
1008 // PML_PutPageInXMS() - If page isn't in XMS, find LRU XMS page and replace
\r
1009 // it with the main/EMS page
\r
1012 PML_PutPageInXMS(int pagenum, global_game_variables_t *gvar)
\r
1015 PageListStruct far *page;
\r
1017 if (!gvar->pm.xmm.XMSPresent)
\r
1020 page = &gvar->pm.PMPages[pagenum];
\r
1021 if (page->xmsPage != -1)
\r
1022 return; // Already in XMS
\r
1024 if (gvar->pm.XMSPagesUsed < gvar->pm.xmm.XMSPagesAvail)
\r
1025 page->xmsPage = gvar->pm.XMSPagesUsed++;
\r
1028 usexms = PML_GiveLRUXMSPage(gvar);
\r
1030 Quit (gvar, "PML_PutPageInXMS: No XMS LRU");
\r
1031 page->xmsPage = gvar->pm.PMPages[usexms].xmsPage;
\r
1032 gvar->pm.PMPages[usexms].xmsPage = -1;
\r
1034 PML_CopyToXMS(PM_GetPageAddress(pagenum, gvar),page->xmsPage,page->length, gvar);
\r
1038 // PML_TransferPageSpace() - A page is being replaced, so give the new page
\r
1039 // the old one's address space. Returns the address of the new page.
\r
1042 PML_TransferPageSpace(int orig,int new, global_game_variables_t *gvar)
\r
1045 PageListStruct far *origpage,far *newpage;
\r
1048 Quit (gvar, "PML_TransferPageSpace: Identity replacement");
\r
1050 origpage = &gvar->pm.PMPages[orig];
\r
1051 newpage = &gvar->pm.PMPages[new];
\r
1053 if (origpage->locked != pml_Unlocked)
\r
1054 Quit (gvar, "PML_TransferPageSpace: Killing locked page");
\r
1056 if ((origpage->emsPage == -1) && (origpage->mainPage == -1))
\r
1057 Quit (gvar, "PML_TransferPageSpace: Reusing non-existent page");
\r
1059 // Copy page that's about to be purged into XMS
\r
1060 PML_PutPageInXMS(orig, gvar);
\r
1062 // Get the address, and force EMS into a physical page if necessary
\r
1063 addr = PM_GetPageAddress(orig, gvar);
\r
1065 // Steal the address
\r
1066 newpage->emsPage = origpage->emsPage;
\r
1067 newpage->mainPage = origpage->mainPage;
\r
1069 // Mark replaced page as purged
\r
1070 origpage->mainPage = origpage->emsPage = -1;
\r
1073 Quit (gvar, "PML_TransferPageSpace: Zero replacement");
\r
1079 // PML_GetAPageBuffer() - A page buffer is needed. Either get it from the
\r
1080 // main/EMS free pool, or use PML_GiveLRUPage() to find which page to
\r
1081 // steal the buffer from. Returns a far pointer to the page buffer, and
\r
1082 // sets the fields inside the given page structure appropriately.
\r
1083 // If mainonly is true, free EMS will be ignored, and only main pages
\r
1084 // will be looked at by PML_GiveLRUPage().
\r
1087 PML_GetAPageBuffer(int pagenum,boolean mainonly, global_game_variables_t *gvar)
\r
1089 byte far *addr = nil;
\r
1091 PMBlockAttr *used;
\r
1092 PageListStruct far *page;
\r
1094 page = &gvar->pm.PMPages[pagenum];
\r
1095 if ((gvar->pm.EMSPagesUsed < gvar->pm.emm.EMSPagesAvail) && !mainonly)
\r
1097 // There's remaining EMS - use it
\r
1098 page->emsPage = gvar->pm.EMSPagesUsed++;
\r
1099 addr = PML_GetEMSAddress(page->emsPage,page->locked, gvar);
\r
1101 else if (gvar->pm.MainPagesUsed < gvar->pm.mm.MainPagesAvail)
\r
1103 // There's remaining main memory - use it
\r
1104 for (i = 0,n = -1,used = gvar->pm.mm.MainMemUsed;i < PMMaxMainMem;i++,used++)
\r
1106 if ((*used & pmba_Allocated) && !(*used & pmba_Used))
\r
1109 *used |= pmba_Used;
\r
1114 Quit (gvar, "PML_GetPageBuffer: MainPagesAvail lied");
\r
1115 addr = gvar->pm.mm.MainMemPages[n];
\r
1117 Quit (gvar, "PML_GetPageBuffer: Purged main block");
\r
1118 page->mainPage = n;
\r
1119 gvar->pm.MainPagesUsed++;
\r
1122 addr = PML_TransferPageSpace(PML_GiveLRUPage(mainonly, gvar),pagenum, gvar);
\r
1125 Quit (gvar, "PML_GetPageBuffer: Search failed");
\r
1130 // PML_GetPageFromXMS() - If page is in XMS, find LRU main/EMS page and
\r
1131 // replace it with the page from XMS. If mainonly is true, will only
\r
1132 // search for LRU main page.
\r
1133 // XMSProtectPage is set to the page to be retrieved from XMS, so that if
\r
1134 // the page from which we're stealing the main/EMS from isn't in XMS,
\r
1135 // it won't copy over the page that we're trying to get from XMS.
\r
1136 // (pages that are being purged are copied into XMS, if possible)
\r
1139 PML_GetPageFromXMS(int pagenum,boolean mainonly, global_game_variables_t *gvar)
\r
1141 byte far *checkaddr;
\r
1142 memptr addr = nil;
\r
1143 PageListStruct far *page;
\r
1145 page = &gvar->pm.PMPages[pagenum];
\r
1146 if (gvar->pm.xmm.XMSPresent && (page->xmsPage != -1))
\r
1148 gvar->pm.xmm.XMSProtectPage = pagenum;
\r
1149 checkaddr = PML_GetAPageBuffer(pagenum,mainonly, gvar);
\r
1150 if (FP_OFF(checkaddr))
\r
1151 Quit (gvar, "PML_GetPageFromXMS: Non segment pointer");
\r
1152 addr = (memptr)FP_SEG(checkaddr);
\r
1153 PML_CopyFromXMS(addr,page->xmsPage,page->length, gvar);
\r
1154 gvar->pm.xmm.XMSProtectPage = -1;
\r
1161 // PML_LoadPage() - A page is not in main/EMS memory, and it's not in XMS.
\r
1162 // Load it into either main or EMS. If mainonly is true, the page will
\r
1163 // only be loaded into main.
\r
1166 PML_LoadPage(int pagenum,boolean mainonly, global_game_variables_t *gvar)
\r
1169 PageListStruct far *page;
\r
1171 addr = PML_GetAPageBuffer(pagenum,mainonly, gvar);
\r
1172 page = &gvar->pm.PMPages[pagenum];
\r
1173 PML_ReadFromFile(addr,page->offset,page->length, gvar);
\r
1177 // PM_GetPage() - Returns the address of the page, loading it if necessary
\r
1178 // First, check if in Main Memory or EMS
\r
1179 // Then, check XMS
\r
1180 // If not in XMS, load into Main Memory or EMS
\r
1184 PM_GetPage(int pagenum, global_game_variables_t *gvar)
\r
1188 if (pagenum >= gvar->pm.fi.ChunksInFile)
\r
1189 Quit (gvar, "PM_GetPage: Invalid page request");
\r
1191 #ifdef __DEBUG_2__ // for debugging
\r
1193 mov dx,STATUS_REGISTER_1
\r
1196 mov al,ATR_OVERSCAN
\r
1198 mov al,10 // bright green
\r
1203 if (!(result = PM_GetPageAddress(pagenum, gvar)))
\r
1205 boolean mainonly = (pagenum >= gvar->pm.fi.PMSoundStart);
\r
1206 if (!gvar->pm.PMPages[pagenum].offset) // JDC: sparse page
\r
1207 Quit (gvar, "Tried to load a sparse page!");
\r
1208 if (!(result = PML_GetPageFromXMS(pagenum,mainonly, gvar)))
\r
1210 if (gvar->pm.PMPages[pagenum].lastHit == gvar->pm.PMFrameCount)
\r
1211 gvar->pm.PMThrashing++;
\r
1213 PML_LoadPage(pagenum,mainonly, gvar);
\r
1214 result = PM_GetPageAddress(pagenum, gvar);
\r
1217 gvar->pm.PMPages[pagenum].lastHit = gvar->pm.PMFrameCount;
\r
1219 #ifdef __DEBUG_2__ // for debugging
\r
1221 mov dx,STATUS_REGISTER_1
\r
1224 mov al,ATR_OVERSCAN
\r
1228 mov al,0x20 // normal
\r
1238 // PM_SetPageLock() - Sets the lock type on a given page
\r
1239 // pml_Unlocked: Normal, page can be purged
\r
1240 // pml_Locked: Cannot be purged
\r
1241 // pml_EMS?: Same as pml_Locked, but if in EMS, use the physical page
\r
1242 // specified when returning the address. For sound stuff.
\r
1245 PM_SetPageLock(int pagenum,PMLockType lock, global_game_variables_t *gvar)
\r
1247 if (pagenum < gvar->pm.fi.PMSoundStart)
\r
1248 Quit (gvar, "PM_SetPageLock: Locking/unlocking non-sound page");
\r
1250 gvar->pm.PMPages[pagenum].locked = lock;
\r
1254 // PM_Preload() - Loads as many pages as possible into all types of memory.
\r
1255 // Calls the update function after each load, indicating the current
\r
1256 // page, and the total pages that need to be loaded (for thermometer).
\r
1259 PM_Preload(boolean (*update)(word current,word total), global_game_variables_t *gvar)
\r
1263 word current,total,
\r
1264 //totalnonxms,totalxms,
\r
1265 mainfree,maintotal,
\r
1266 //emstotal,emsfree,
\r
1269 PageListStruct __far *p;
\r
1271 mainfree = (gvar->pm.mm.MainPagesAvail - gvar->pm.MainPagesUsed) + (gvar->pm.emm.EMSPagesAvail - gvar->pm.EMSPagesUsed);
\r
1272 xmsfree = (gvar->pm.xmm.XMSPagesAvail - gvar->pm.XMSPagesUsed);
\r
1274 xmstotal = maintotal = 0;
\r
1276 for (i = 0;i < gvar->pm.fi.ChunksInFile;i++)
\r
1278 if (!gvar->pm.PMPages[i].offset)
\r
1279 continue; // sparse
\r
1281 if ( gvar->pm.PMPages[i].emsPage != -1 || gvar->pm.PMPages[i].mainPage != -1 )
\r
1282 continue; // already in main mem
\r
1289 else if ( xmsfree && (gvar->pm.PMPages[i].xmsPage == -1) )
\r
1297 total = maintotal + xmstotal;
\r
1306 // cache main/ems blocks
\r
1310 while ( !gvar->pm.PMPages[page].offset || gvar->pm.PMPages[page].mainPage != -1
\r
1311 || gvar->pm.PMPages[page].emsPage != -1 )
\r
1314 if (page >= gvar->pm.fi.ChunksInFile)
\r
1315 Quit (gvar, "PM_Preload: Pages>=gvar->pm.fi.ChunksInFile");
\r
1317 PM_GetPage(page, gvar);
\r
1322 update(current,total);
\r
1326 // load stuff to XMS
\r
1330 for (oogypage = 0 ; gvar->pm.PMPages[oogypage].mainPage == -1 ; oogypage++)
\r
1332 addr = PM_GetPage(oogypage, gvar);
\r
1334 Quit (gvar, "PM_Preload: XMS buffer failed");
\r
1338 while ( !gvar->pm.PMPages[page].offset || gvar->pm.PMPages[page].xmsPage != -1 )
\r
1341 if (page >= gvar->pm.fi.ChunksInFile)
\r
1342 Quit (gvar, "PM_Preload: Pages>=gvar->pm.fi.ChunksInFile");
\r
1344 p = &gvar->pm.PMPages[page];
\r
1346 p->xmsPage = gvar->pm.XMSPagesUsed++;
\r
1347 if (gvar->pm.XMSPagesUsed > gvar->pm.xmm.XMSPagesAvail)
\r
1348 Quit (gvar, "PM_Preload: Exceeded XMS pages");
\r
1349 if (p->length > PMPageSize)
\r
1350 Quit (gvar, "PM_Preload: Page too long");
\r
1352 PML_ReadFromFile((byte far *)addr,p->offset,p->length, gvar);
\r
1353 PML_CopyToXMS((byte far *)addr,p->xmsPage,p->length, gvar);
\r
1358 update(current,total);
\r
1361 p = &gvar->pm.PMPages[oogypage];
\r
1362 PML_ReadFromFile((byte far *)addr,p->offset,p->length, gvar);
\r
1365 update(total,total);
\r
1368 /////////////////////////////////////////////////////////////////////////////
\r
1372 /////////////////////////////////////////////////////////////////////////////
\r
1375 // PM_NextFrame() - Increments the frame counter and adjusts the thrash
\r
1376 // avoidence variables
\r
1378 // If currently in panic mode (to avoid thrashing), check to see if the
\r
1379 // appropriate number of frames have passed since the last time that
\r
1380 // we would have thrashed. If so, take us out of panic mode.
\r
1384 PM_NextFrame(global_game_variables_t *gvar)
\r
1388 // Frame count overrun - kill the LRU hit entries & reset frame count
\r
1389 if (++gvar->pm.PMFrameCount >= LONG_MAX - 4)
\r
1391 for (i = 0;i < gvar->pm.PMNumBlocks;i++)
\r
1392 gvar->pm.PMPages[i].lastHit = 0;
\r
1393 gvar->pm.PMFrameCount = 0;
\r
1397 for (i = 0;i < gvar->pm.fi.PMSoundStart;i++)
\r
1399 if (gvar->pm.PMPages[i].locked)
\r
1402 sprintf(buf,"PM_NextFrame: Page %d is locked",i);
\r
1408 if (gvar->pm.PMPanicMode)
\r
1410 // DEBUG - set border color
\r
1411 if ((!gvar->pm.PMThrashing) && (!--gvar->pm.PMPanicMode))
\r
1413 // DEBUG - reset border color
\r
1416 if (gvar->pm.PMThrashing >= PMThrashThreshold)
\r
1417 gvar->pm.PMPanicMode = PMUnThrashThreshold;
\r
1418 gvar->pm.PMThrashing = false;
\r
1422 // PM_Reset() - Sets up caching structures
\r
1425 PM_Reset(global_game_variables_t *gvar)
\r
1428 PageListStruct far *page;
\r
1430 gvar->pm.xmm.XMSPagesAvail = gvar->pm.xmm.XMSAvail / PMPageSizeKB;
\r
1432 gvar->pm.emm.EMSPagesAvail = gvar->pm.emm.EMSAvail * (EMSPageSizeKB / PMPageSizeKB);
\r
1433 gvar->pm.emm.EMSPhysicalPage = 0;
\r
1435 gvar->pm.MainPagesUsed = gvar->pm.EMSPagesUsed = gvar->pm.XMSPagesUsed = 0;
\r
1437 gvar->pm.PMPanicMode = false;
\r
1439 gvar->pm.fi.PageFile = -1;
\r
1440 gvar->pm.xmm.XMSProtectPage = -1;
\r
1442 // Initialize page list
\r
1443 for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.PMNumBlocks;i++,page++)
\r
1445 page->mainPage = -1;
\r
1446 page->emsPage = -1;
\r
1447 page->xmsPage = -1;
\r
1448 page->locked = false;
\r
1453 // PM_Startup() - Start up the Page Mgr
\r
1456 PM_Startup(global_game_variables_t *gvar)
\r
1458 boolean nomain,noems,noxms;
\r
1461 if (gvar->pm.PMStarted)
\r
1464 //0000+=+=strcpy(&(gvar->pm.fi.PageFileName), "VSWAP.");
\r
1466 nomain = noems = noxms = false;
\r
1467 for (i = 1;i < _argc;i++)
\r
1469 switch (US_CheckParm(_argv[i],ParmStrings))
\r
1483 //0000+=+=PML_OpenPageFile(gvar);
\r
1485 if (!noems && MML_CheckForEMS())
\r
1486 PML_StartupEMS(gvar);
\r
1487 if (!noxms && MML_CheckForXMS())
\r
1488 PML_StartupXMS(gvar);
\r
1490 PML_StartupMainMem(gvar);
\r
1492 if (!gvar->pm.mm.MainPresent && !gvar->pm.emm.EMSPresent && !gvar->pm.xmm.XMSPresent)
\r
1494 Quit (gvar, "PM_Startup: No main or EMS\n");
\r
1500 gvar->pm.PMStarted = true;
\r
1504 // PM_Shutdown() - Shut down the Page Mgr
\r
1507 PM_Shutdown(global_game_variables_t *gvar)
\r
1509 if(MML_CheckForXMS()) PML_ShutdownXMS(gvar);
\r
1510 if(MML_CheckForEMS()) PML_ShutdownEMS(gvar);
\r
1512 if (!gvar->pm.PMStarted)
\r
1515 //0000+=+=PML_ClosePageFile(gvar);
\r
1517 PML_ShutdownMainMem(gvar);
\r