1 /* Catacomb Armageddon Source Code
\r
2 * Copyright (C) 1993-2014 Flat Rock Software
\r
4 * This program is free software; you can redistribute it and/or modify
\r
5 * it under the terms of the GNU General Public License as published by
\r
6 * the Free Software Foundation; either version 2 of the License, or
\r
7 * (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License along
\r
15 * with this program; if not, write to the Free Software Foundation, Inc.,
\r
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
22 =============================================================================
\r
24 ID software memory manager
\r
25 --------------------------
\r
27 Primary coder: John Carmack
\r
31 Quit (char *error) function
\r
36 MM_SizePtr to change the size of a given pointer
\r
38 Multiple purge levels utilized
\r
40 EMS / XMS unmanaged routines
\r
42 =============================================================================
\r
45 #include "src/lib/16_mm.h"
\r
48 ======================
\r
52 = Routine from p36 of Extending DOS
\r
54 =======================
\r
57 boolean MML_CheckForEMS (void)
\r
60 char emmname[] = "EMMXXXX0";
\r
61 // mov dx,OFFSET emmname
\r
63 LEA DX, emmname //fix by andrius4669
\r
65 int 0x21 // try to open EMMXXXX0 device
\r
71 int 0x21 // get device info
\r
79 int 0x21 // get status
\r
85 int 0x21 // close handle
\r
104 ======================
\r
108 =======================
\r
111 void MML_SetupEMS (void)
\r
113 char str[80],str2[10];
\r
115 boolean errorflag=false;
\r
116 union REGS CPURegs;
\r
119 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
\r
124 int EMS_INT // make sure EMS hardware is present
\r
132 mov [EMSVer],ax // set EMSVer
\r
133 cmp al,0x32 // only work on ems 3.2 or greater
\r
136 mov ah,EMS_GETFRAME
\r
137 int EMS_INT // find the page frame address
\r
140 mov [EMSpageframe],bx
\r
142 mov ah,EMS_GETPAGES
\r
143 int EMS_INT // find out how much EMS is there
\r
146 mov [totalEMSpages],dx
\r
147 mov [freeEMSpages],bx
\r
149 jz noEMS // no EMS at all to allocate
\r
152 jle getpages // there is only 1,2,3,or 4 pages
\r
153 mov bx,4 // we can't use more than 4 pages
\r
156 mov [EMSpagesmapped],bx
\r
157 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
\r
169 if(errorflag==true)
\r
171 err = CPURegs.h.ah;
\r
172 strcpy(str,"MML_SetupEMS: EMS error 0x");
\r
175 printf("%s\n",str);
\r
181 ======================
\r
185 =======================
\r
188 void MML_ShutdownEMS (void)
\r
190 boolean errorflag=false;
\r
195 mov ah,EMS_FREEPAGES
\r
203 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something
\r
207 ====================
\r
211 = Maps the 64k of EMS used by memory manager into the page frame
\r
212 = for general use. This only needs to be called if you are keeping
\r
213 = other things in EMS.
\r
215 ====================
\r
218 void MM_MapEMS (void)
\r
220 char str[80],str2[10];
\r
222 boolean errorflag=false;
\r
224 union REGS CPURegs;
\r
226 for (i=0;i<EMSpagesmapped;i++)
\r
231 mov bx,[i] // logical page
\r
232 mov al,bl // physical page
\r
233 mov dx,[EMShandle] // handle
\r
242 if(errorflag==true)
\r
244 err = CPURegs.h.ah;
\r
245 strcpy(str,"MM_MapEMS: EMS error 0x");
\r
248 printf("%s\n",str);
\r
254 //==========================================================================
\r
257 ======================
\r
261 = Check for XMM driver
\r
263 =======================
\r
266 boolean MML_CheckForXMS (void)
\r
268 boolean errorflag=false;
\r
274 int 0x2f // query status of installed diver
\r
280 if(errorflag==true) return false;
\r
286 ======================
\r
290 = Try to allocate all upper memory block
\r
292 =======================
\r
295 void MML_SetupXMS (void)
\r
297 unsigned base,size;
\r
303 mov [WORD PTR XMSaddr],bx
\r
304 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
\r
309 mov ah,XMS_ALLOCUMB
\r
310 mov dx,0xffff // try for largest block possible
\r
311 call [DWORD PTR XMSaddr]
\r
315 cmp bl,0xb0 // error: smaller UMB is available
\r
318 mov ah,XMS_ALLOCUMB
\r
319 call [DWORD PTR XMSaddr] // DX holds largest available UMB
\r
321 jz done // another error...
\r
328 MML_UseSpace (base,size);
\r
329 mminfo.XMSmem += size*16;
\r
330 UMBbase[numUMBs] = base;
\r
332 if (numUMBs < MAXUMBS)
\r
338 ======================
\r
342 ======================
\r
345 void MML_ShutdownXMS (void)
\r
350 for (i=0;i<numUMBs;i++)
\r
357 call [DWORD PTR XMSaddr]
\r
362 //==========================================================================
\r
365 ======================
\r
369 = Marks a range of paragraphs as usable by the memory manager
\r
370 = This is used to mark space for the near heap, far heap, ems page frame,
\r
371 = and upper memory blocks
\r
373 ======================
\r
376 void MML_UseSpace (unsigned segstart, unsigned seglength)
\r
378 mmblocktype far *scan,far *last;
\r
382 scan = last = mmhead;
\r
383 mmrover = mmhead; // reset rover to start of memory
\r
386 // search for the block that contains the range of segments
\r
388 while (scan->start+scan->length < segstart)
\r
395 // take the given range out of the block
\r
397 oldend = scan->start + scan->length;
\r
398 extra = oldend - (segstart+seglength);
\r
401 printf("MML_UseSpace: Segment spans two blocks!");
\r
406 if (segstart == scan->start)
\r
408 last->next = scan->next; // unlink block
\r
413 scan->length = segstart-scan->start; // shorten block
\r
418 mmnew->next = scan->next;
\r
419 scan->next = mmnew;
\r
420 mmnew->start = segstart+seglength;
\r
421 mmnew->length = extra;
\r
422 mmnew->attributes = LOCKBIT;
\r
427 //==========================================================================
\r
430 ====================
\r
434 = We are out of blocks, so free a purgable block
\r
436 ====================
\r
439 void MML_ClearBlock (void)
\r
441 mmblocktype far *scan,far *last;
\r
443 scan = mmhead->next;
\r
447 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
\r
449 MM_FreePtr(scan->useptr);
\r
455 printf("MM_ClearBlock: No purgable blocks!");
\r
459 //==========================================================================
\r
462 ===================
\r
466 = Grabs all space from turbo with malloc/farmalloc
\r
467 = Allocates bufferseg misc buffer
\r
469 ===================
\r
472 void MM_Startup (void)
\r
477 unsigned segstart,seglength,endfree;
\r
479 if (mminfo.mmstarted)
\r
483 mminfo.mmstarted = true;
\r
484 mminfo.bombonerror = true;
\r
486 // set up the linked list (everything in the free list;
\r
489 mmfree = &mmblocks[0];
\r
490 for (i=0;i<MAXBLOCKS-1;i++)
\r
491 mmblocks[i].next = &mmblocks[i+1];
\r
492 mmblocks[i].next = NULL;
\r
495 // locked block of all memory until we punch out free space
\r
498 mmhead = mmnew; // this will allways be the first node
\r
500 mmnew->length = 0xffff;
\r
501 mmnew->attributes = LOCKBIT;
\r
502 mmnew->next = NULL;
\r
507 // get all available near conventional memory segments
\r
509 //---- length=coreleft();
\r
512 start = (void far *)(nearheap = malloc(length));
\r
514 length -= 16-(FP_OFF(start)&15);
\r
515 length -= SAVENEARHEAP;
\r
516 seglength = length / 16; // now in paragraphs
\r
517 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
518 MML_UseSpace (segstart,seglength);
\r
519 mminfo.nearheap = length;
\r
522 // get all available far conventional memory segments
\r
524 //---- length=farcoreleft();
\r
527 start = farheap = _fmalloc(length);
\r
528 length -= 16-(FP_OFF(start)&15);
\r
529 length -= SAVEFARHEAP;
\r
530 seglength = length / 16; // now in paragraphs
\r
531 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
\r
532 MML_UseSpace (segstart,seglength);
\r
533 mminfo.farheap = length;
\r
534 mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
\r
538 // detect EMS and allocate up to 64K at page frame
\r
541 for (i = 1;i < __argc;i++)
\r
543 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
\r
544 goto emsskip; // param NOEMS
\r
547 if (MML_CheckForEMS())
\r
549 //printf("EMS1\n");
\r
550 MML_SetupEMS(); // allocate space
\r
551 //printf("EMS2\n");
\r
552 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
\r
553 //printf("EMS3\n");
\r
554 MM_MapEMS(); // map in used pages
\r
555 //printf("EMS4\n");
\r
556 mminfo.EMSmem = EMSpagesmapped*0x4000l;
\r
560 // detect XMS and get upper memory blocks
\r
564 for (i = 1;i < __argc;i++)
\r
566 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
\r
567 goto xmsskip; // param NOXMS
\r
570 if (MML_CheckForXMS())
\r
572 // printf("XMS!\n");
\r
573 MML_SetupXMS(); // allocate as many UMBs as possible
\r
577 // allocate the misc buffer
\r
580 mmrover = mmhead; // start looking for space after low block
\r
582 MM_GetPtr (&bufferseg,BUFFERSIZE);
\r
585 //==========================================================================
\r
588 ====================
\r
592 = Frees all conventional, EMS, and XMS allocated
\r
594 ====================
\r
597 void MM_Shutdown (void)
\r
599 if (!mminfo.mmstarted)
\r
605 MML_ShutdownEMS ();
\r
606 MML_ShutdownXMS ();
\r
609 //==========================================================================
\r
612 ====================
\r
616 = Allocates an unlocked, unpurgable block
\r
618 ====================
\r
621 void MM_GetPtr (memptr *baseptr,dword size)
\r
623 mmblocktype far *scan,far *lastscan,far *endscan
\r
624 ,far *purge,far *next;
\r
626 unsigned needed,startseg;
\r
628 needed = (size+15)/16; // convert size from bytes to paragraphs
\r
630 GETNEWBLOCK; // fill in start and next after a spot is found
\r
631 mmnew->length = needed;
\r
632 mmnew->useptr = baseptr;
\r
633 mmnew->attributes = BASEATTRIBUTES;
\r
635 for (search = 0; search<3; search++)
\r
638 // first search: try to allocate right after the rover, then on up
\r
639 // second search: search from the head pointer up to the rover
\r
640 // third search: compress memory, then scan from start
\r
641 if (search == 1 && mmrover == mmhead)
\r
647 lastscan = mmrover;
\r
648 scan = mmrover->next;
\r
653 scan = mmhead->next;
\r
659 scan = mmhead->next;
\r
664 startseg = lastscan->start + lastscan->length;
\r
666 while (scan != endscan)
\r
668 if (scan->start - startseg >= needed)
\r
671 // got enough space between the end of lastscan and
\r
672 // the start of scan, so throw out anything in the middle
\r
673 // and allocate the new block
\r
675 purge = lastscan->next;
\r
676 lastscan->next = mmnew;
\r
677 mmnew->start = *(unsigned *)baseptr = startseg;
\r
678 mmnew->next = scan;
\r
679 while ( purge != scan)
\r
680 { // free the purgable block
\r
681 next = purge->next;
\r
683 purge = next; // purge another if not at scan
\r
686 return; // good allocation!
\r
690 // if this block is purge level zero or locked, skip past it
\r
692 if ( (scan->attributes & LOCKBIT)
\r
693 || !(scan->attributes & PURGEBITS) )
\r
696 startseg = lastscan->start + lastscan->length;
\r
700 scan=scan->next; // look at next line
\r
704 if (mminfo.bombonerror)
\r
705 printf(OUT_OF_MEM_MSG,(size-mminfo.nearheap));
\r
707 mminfo.mmerror = true;
\r
710 //==========================================================================
\r
713 ====================
\r
717 = Allocates an unlocked, unpurgable block
\r
719 ====================
\r
722 void MM_FreePtr (memptr *baseptr)
\r
724 mmblocktype far *scan,far *last;
\r
729 if (baseptr == mmrover->useptr) // removed the last allocated block
\r
732 while (scan->useptr != baseptr && scan)
\r
740 printf("MM_FreePtr: Block not found!");
\r
744 last->next = scan->next;
\r
748 //==========================================================================
\r
751 =====================
\r
755 = Sets the purge level for a block (locked blocks cannot be made purgable)
\r
757 =====================
\r
760 void MM_SetPurge (memptr *baseptr, int purge)
\r
762 mmblocktype far *start;
\r
768 if (mmrover->useptr == baseptr)
\r
771 mmrover = mmrover->next;
\r
775 else if (mmrover == start)
\r
777 printf("MM_SetPurge: Block not found!");
\r
783 mmrover->attributes &= ~PURGEBITS;
\r
784 mmrover->attributes |= purge;
\r
787 //==========================================================================
\r
790 =====================
\r
794 = Locks / unlocks the block
\r
796 =====================
\r
799 void MM_SetLock (memptr *baseptr, boolean locked)
\r
801 mmblocktype far *start;
\r
807 if (mmrover->useptr == baseptr)
\r
810 mmrover = mmrover->next;
\r
814 else if (mmrover == start)
\r
816 printf("MM_SetLock: Block not found!");
\r
822 mmrover->attributes &= ~LOCKBIT;
\r
823 mmrover->attributes |= locked*LOCKBIT;
\r
826 //==========================================================================
\r
829 =====================
\r
833 = Throws out all purgable stuff and compresses movable blocks
\r
835 =====================
\r
838 void MM_SortMem (void)
\r
840 mmblocktype far *scan,far *last,far *next;
\r
841 unsigned start,length,source,dest,oldborder;
\r
845 // lock down a currently playing sound
\r
847 /*++++ playing = SD_SoundPlaying ();
\r
853 playing += STARTPCSOUNDS;
\r
856 playing += STARTADLIBSOUNDS;
\r
859 MM_SetLock(&(memptr)audiosegs[playing],true);
\r
864 // oldborder = bordercolor;
\r
865 // VW_ColorBorder (15);
\r
872 last = NULL; // shut up compiler warning
\r
876 if (scan->attributes & LOCKBIT)
\r
879 // block is locked, so try to pile later blocks right after it
\r
881 start = scan->start + scan->length;
\r
885 if (scan->attributes & PURGEBITS)
\r
888 // throw out the purgable block
\r
899 // push the non purgable block on top of the last moved block
\r
901 if (scan->start != start)
\r
903 length = scan->length;
\r
904 source = scan->start;
\r
906 while (length > 0xf00)
\r
908 movedata(source,0,dest,0,0xf00*16);
\r
913 movedata(source,0,dest,0,length*16);
\r
915 scan->start = start;
\r
916 *(unsigned *)scan->useptr = start;
\r
918 start = scan->start + scan->length;
\r
923 scan = scan->next; // go to next block
\r
931 // VW_ColorBorder (oldborder);
\r
933 /*++++ if (playing)
\r
934 MM_SetLock(&(memptr)audiosegs[playing],false);*/
\r
938 //==========================================================================
\r
942 =====================
\r
946 =====================
\r
949 void MM_ShowMemory (void)
\r
951 mmblocktype far *scan;
\r
952 unsigned color,temp;//, i;
\r
954 char scratch[80],str[10];
\r
956 //**** VW_SetDefaultColors();
\r
957 //**** VW_SetLineWidth(40);
\r
958 //++++mh temp = bufferofs;
\r
959 //++++mh bufferofs = 0;
\r
960 //**** VW_SetScreen (0,0);
\r
970 if (scan->attributes & PURGEBITS)
\r
971 color = 5; // dark purple = purgable
\r
973 color = 9; // medium blue = non purgable
\r
974 if (scan->attributes & LOCKBIT)
\r
975 color = 12; // red = locked
\r
976 if (scan->start<=end)
\r
978 printf("MM_ShowMemory: Memory block order currupted!");
\r
981 end = scan->start+scan->length-1;
\r
982 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
\r
983 //++++ VW_Plot(scan->start,0,15);
\r
984 if (scan->next->start > end+1)
\r
985 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
\r
988 printf("Location:");
\r
989 printf("%Fp\t", scan->start);
\r
990 strcpy (scratch,"Size:");
\r
991 ltoa ((long)scan->length*16,str,10);
\r
992 strcat (scratch,str);
\r
993 strcat (scratch,"\tOwner:0x");
\r
994 owner = (unsigned)scan->useptr;
\r
995 ultoa (owner,str,16);
\r
996 strcat (scratch,str);
\r
997 strcat (scratch,"\n");
\r
998 //++++write (debughandle,scratch,strlen(scratch));
\r
999 fprintf(stdout, "%s", scratch);
\r
1002 scan = scan->next;
\r
1005 //CA_CloseDebug ();
\r
1007 //++++mh IN_Ack();
\r
1008 //**** VW_SetLineWidth(64);
\r
1009 //++++mh bufferofs = temp;
\r
1013 //==========================================================================
\r
1017 ======================
\r
1021 = Returns the total free space without purging
\r
1023 ======================
\r
1026 dword MM_UnusedMemory (void)
\r
1029 mmblocktype far *scan;
\r
1034 while (scan->next)
\r
1036 free += scan->next->start - (scan->start + scan->length);
\r
1037 scan = scan->next;
\r
1040 // return free*16l;
\r
1044 //==========================================================================
\r
1048 ======================
\r
1052 = Returns the total free space with purging
\r
1054 ======================
\r
1057 dword MM_TotalFree (void)
\r
1060 mmblocktype far *scan;
\r
1065 while (scan->next)
\r
1067 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
\r
1068 free += scan->length;
\r
1069 free += scan->next->start - (scan->start + scan->length);
\r
1070 scan = scan->next;
\r
1073 // return free*16l;
\r
1077 //==========================================================================
\r
1080 =====================
\r
1084 =====================
\r
1087 void MM_Report(void)
\r
1089 printf("EMM %x available\n", EMSVer);
\r
1090 printf("totalEMSpages=%u\n", totalEMSpages);
\r
1091 printf("freeEMSpages=%u\n", freeEMSpages);
\r
1092 printf("EMSpageframe=%Fp\n", EMSpageframe);
\r
1093 printf("near=%lu\n", mminfo.nearheap);
\r
1094 printf("far=%lu\n", mminfo.farheap);
\r
1095 printf("EMSmem=%lu\n", mminfo.EMSmem);
\r
1096 printf("XMSmem=%lu\n", mminfo.XMSmem);
\r
1097 printf("mainmem=%lu\n", mminfo.mainmem);
\r
1098 printf("UnusedMemory=%lu\n", MM_UnusedMemory());
\r
1099 printf("TotalFree=%lu\n", MM_TotalFree());
\r
1101 // printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
\r
1102 // printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
\r
1105 //==========================================================================
\r
1108 =====================
\r
1112 =====================
\r
1115 int MM_EMSVer(void)
\r
1120 mov ah,EMS_VERSION
\r
1127 //==========================================================================
\r
1130 =====================
\r
1134 =====================
\r
1137 void MM_BombOnError (boolean bomb)
\r
1139 mminfo.bombonerror = bomb;
\r
1142 //==========================================================================
\r
1144 ///////////////////////////////////////////////////////////////////////////
\r
1146 // US_CheckParm() - checks to see if a string matches one of a set of
\r
1147 // strings. The check is case insensitive. The routine returns the
\r
1148 // index of the string that matched, or -1 if no matches were found
\r
1150 ///////////////////////////////////////////////////////////////////////////
\r
1152 US_CheckParm(char *parm,char **strings)
\r
1158 while (!isalpha(*parm)) // Skip non-alphas
\r
1161 for (i = 0;*strings && **strings;i++)
\r
1163 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)
\r