1 /* Catacomb Apocalypse Source Code
2 * Copyright (C) 1993-2014 Flat Rock Software
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =============================================================================
24 ID software memory manager
25 --------------------------
27 Primary coder: John Carmack
31 Quit (char *error) function
36 MM_SizePtr to change the size of a given pointer
38 Multiple purge levels utilized
40 EMS / XMS unmanaged routines
42 =============================================================================
46 Open Watcom port by sparky4
49 #include "src/lib/16_mm.h"
50 #include "src/lib/16_ca.h"
57 =============================================================================
61 =============================================================================
64 void (* beforesort) (void);
65 void (* aftersort) (void);
66 void (* XMSaddr) (void); // far pointer to XMS driver
69 =============================================================================
73 =============================================================================
76 static char *ParmStringsexmm[] = {"noems","noxms",""};
79 ======================
83 = Routine from p36 of Extending DOS
85 =======================
88 boolean MML_CheckForEMS(void)
91 static char emmname[] = "EMMXXXX0"; //fix by andrius4669
93 mov dx,OFFSET emmname //fix by andrius4669
95 int 0x21 // try to open EMMXXXX0 device
101 int 0x21 // get device info
109 int 0x21 // get status
115 int 0x21 // close handle
145 ======================
149 =======================
152 byte MML_SetupEMS(global_game_variables_t *gvar)
156 boolean errorflag=false;
158 unsigned int EMSVer = 0;
160 unsigned totalEMSpages,freeEMSpages,EMSPageFrame,EMSpagesmapped,EMSHandle;
161 totalEMSpages = freeEMSpages = EMSPageFrame = EMSpagesmapped = 0;
165 int EMS_INT // make sure EMS hardware is present
167 //mov [EMS_status],ah
174 mov [EMSVer],ax // set EMSVer
175 cmp al,0x32 // only work on ems 3.2 or greater
179 int EMS_INT // find the page frame address
182 mov [EMSPageFrame],bx
185 int EMS_INT // find out how much EMS is there
188 mov [totalEMSpages],dx
189 mov [freeEMSpages],bx
191 jz noEMS // no EMS at all to allocate
195 cmp bx,[freeEMSpages]
197 mov bx,[freeEMSpages]
207 jle getpages // there is only 1,2,3,or 4 pages
208 mov bx,4 // we can't use more than 4 pages
216 mov [EMSpagesmapped],bx
217 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
243 strcpy(str,"MM_SetupEMS: EMS error ");
248 gvar->mm.totalEMSpages=totalEMSpages;
249 gvar->mm.freeEMSpages=freeEMSpages;
250 gvar->mm.EMSPageFrame=EMSPageFrame;
251 gvar->mm.EMSpagesmapped=EMSpagesmapped;
252 gvar->mm.EMSHandle=EMSHandle;
253 gvar->mm.EMSVer=EMSVer;
259 ======================
263 =======================
266 void MML_ShutdownEMS(global_game_variables_t *gvar)
268 boolean errorflag=false;
269 unsigned EMSHandle=gvar->mm.EMSHandle;
288 Quit("MML_ShutdownEMS: Error freeing EMS!\n"); //++++ add something
296 = Maps the 64k of EMS used by memory manager into the page frame
297 = for general use. This only needs to be called if you are keeping
298 = other things in EMS.
303 byte MM_MapEMS(global_game_variables_t *gvar)
308 boolean errorflag=false;
310 EMSHandle=gvar->mm.EMSHandle;
312 for (i=0;i<4/*MAPPAGES*/;i++)
316 mov bx,[i] // logical page
317 mov al,bl // physical page
318 mov dx,[EMSHandle] // handle
341 strcpy(str,"MM_MapEMS: EMS error ");
347 gvar->mmi.EMSmem = (i)*0x4000lu;
351 byte MM_MapXEMS(global_game_variables_t *gvar)
353 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
355 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
356 //PhysicalStart = Physical page first logical page is mapped to
357 //LogicalStart = First logical page to map
358 //NumPages = Number of pages to map (1 to 4)
359 //Handle = EMS handle logical pages are allocated to
361 /*//Create a buffer containing the page information
362 // FOR x = 0 TO NumPages - 1
363 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
366 // Regs.ax = 0x5000 //Map the pages in the buffer
367 // Regs.cx = NumPages //to the pageframe
369 // Regs.ds = VARSEG(MapInfo$)
370 // Regs.si = SADD(MapInfo$)
371 // InterruptX 0x67, Regs, Regs
372 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
378 boolean errorflag=false;
380 EMSHandle=gvar->mm.EMSHandle;
382 if(gvar->mm.EMSVer<0x40)
385 for (i=0;i<MAPPAGES;i++)
389 mov cx,[i] // logical page
390 mov al,bl // physical page
391 mov dx,[EMSHandle] // handle
414 //strcpy(str,"MM_MapXEMS: EMS error 0x");
415 strcpy(str,"MM_MapXEMS: EMS error ");
419 //printf("%s%x\n",str, err);
420 //printf("FACK! %x\n", err);
424 gvar->mmi.EMSmem = (i)*0x4000lu;
428 //==========================================================================
431 ======================
435 = Check for XMM driver
437 =======================
440 boolean MML_CheckForXMS(void)
443 boolean errorflag=false;
447 int 0x2f // query status of installed diver
458 if(errorflag==true) return false;
464 ======================
468 = Try to allocate all upper memory block
470 =======================
473 void MML_SetupXMS(global_game_variables_t *gvar)
481 mov [WORD PTR XMSaddr],bx
482 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
487 mov dx,0xffff // try for largest block possible
488 //mov ax,dx // Set available Kbytes.
489 call [DWORD PTR XMSaddr]
493 cmp bl,0xb0 // error: smaller UMB is available
497 call [DWORD PTR XMSaddr] // DX holds largest available UMB
499 jz done // another error...
516 // printf("base=%u ", base); printf("size=%u\n", size);
517 MML_UseSpace(base,size, gvar);
518 gvar->mmi.XMSmem += size*16;
519 gvar->mm.UMBbase[gvar->mm.numUMBs] = base;
521 if(gvar->mm.numUMBs < MAXUMBS)
527 ======================
531 ======================
534 void MML_ShutdownXMS(global_game_variables_t *gvar)
539 for (i=0;i<gvar->mm.numUMBs;i++)
541 base = gvar->mm.UMBbase[i];
545 call [DWORD PTR XMSaddr]
550 //==========================================================================
553 ======================
557 = Marks a range of paragraphs as usable by the memory manager
558 = This is used to mark space for the near heap, far heap, ems page frame,
559 = and upper memory blocks
561 ======================
564 /*void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar)
566 //huge mmblocktype huge *scan,huge *last;
572 scan = last = gvar->mm.mmhead;
573 gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory
576 // search for the block that contains the range of segments
578 while(scan->start+scan->length < segstart)
585 // take the given range out of the block
587 oldend = scan->start + scan->length;
588 extra = oldend - (segstart+seglength);
593 if(segm>1)/// || extra>=0x10000lu)
598 //MML_UseSpace(segstart, seglength, gvar);
600 printf("MML_UseSpace: Segment spans two blocks!\n");
602 printf("segm=%u ", segm);
603 printf("ex=%lu ", extra);
604 printf("old=%u ", oldend);
605 printf("start+seglen=%lu\n", segstart+seglength);
606 printf("segsta=%x ", segstart);
607 printf("len=%lu ", scan->length);
608 printf("seglen=%lu ", seglength);
609 printf("segmlen=%lu\n", segmlen);
611 //++++todo: linked list of segment!
612 if(segstart == scan->start)
614 last->next = scan->next; // unlink block
619 scan->length = segstart-scan->start; // shorten block
624 gvar->mm.mmnew->useptr = NULL;
626 gvar->mm.mmnew->next = scan->next;
627 scan->next = gvar->mm.mmnew;
628 gvar->mm.mmnew->start = segstart+seglength;
629 gvar->mm.mmnew->length = extra;
630 gvar->mm.mmnew->attributes = LOCKBIT;
631 }//else if(segm>0) goto segu;
634 void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar)
636 mmblocktype far *scan,far *last;
641 scan = last = gvar->mm.mmhead;
642 gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory
645 // search for the block that contains the range of segments
647 while (scan->start+scan->length < segstart)
654 // find out how many blocks it spans!
656 /*for(;seglength>=0x10000;seglength-=0xFFFF)
658 //printf(" seglen=%lu\n", segmlen);
663 // take the given range out of the block
665 oldend = scan->start + scan->length;
666 extra = oldend - (segstart+((unsigned)seglength));
669 printf("========================================\n");
670 printf("start=%x ", scan->start);
671 printf("old=%u ", oldend);
672 printf("start+seglen=%lu\n", segstart+seglength);
673 printf("segsta=%x ", segstart);
674 printf("len=%lu ", scan->length);
675 printf("seglen=%lu ", seglength);
677 printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
678 printf("========================================\n");
682 if (segstart == scan->start)
684 last->next = scan->next; // unlink block
689 scan->length = segstart-scan->start; // shorten block
694 gvar->mm.mmnew->useptr = NULL;
696 gvar->mm.mmnew->next = scan->next;
697 scan->next = gvar->mm.mmnew;
698 gvar->mm.mmnew->start = segstart+seglength;
699 gvar->mm.mmnew->length = extra;
700 gvar->mm.mmnew->attributes = LOCKBIT;
705 //==========================================================================
712 = We are out of blocks, so free a purgable block
717 void MML_ClearBlock(global_game_variables_t *gvar)
719 //huge mmblocktype huge *scan,huge *last;
720 mmblocktype __far *scan;//,far *last;
722 scan = gvar->mm.mmhead->next;
726 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
728 MM_FreePtr(scan->useptr, gvar);
734 printf("MM_ClearBlock: No purgable blocks!\n");
738 //==========================================================================
745 = Grabs all space from turbo with malloc/farmalloc
746 = Allocates bufferseg misc buffer
751 void MM_Startup(global_game_variables_t *gvar)
754 //dword length,seglength;
755 dword length; word seglength;
756 //huge void huge *start;
758 word segstart;//,endfree;
760 if(gvar->mm.mmstarted)
763 gvar->mm.mmstarted = true;
764 gvar->mm.bombonerror = true;
767 // set up the linked list (everything in the free list;
769 gvar->mm.mmhead = NULL;
770 gvar->mm.mmfree = &(gvar->mm.mmblocks[0]);
771 for(i=0;i<MAXBLOCKS-1;i++)
772 gvar->mm.mmblocks[i].next = &(gvar->mm.mmblocks[i+1]);
773 gvar->mm.mmblocks[i].next = NULL;
776 // locked block of all memory until we punch out free space
779 gvar->mm.mmhead = gvar->mm.mmnew; // this will allways be the first node
780 gvar->mm.mmnew->start = 0;
781 gvar->mm.mmnew->length = 0xffff;
782 gvar->mm.mmnew->attributes = LOCKBIT;
783 gvar->mm.mmnew->next = NULL;
784 gvar->mm.mmrover = gvar->mm.mmhead;
787 // get all available near conventional memory segments
791 length=(dword)_memavl();//(dword)GetFreeSize();
792 //huge start = (void huge *)(gvar->mm.nearheap = _nmalloc(length));
793 start = (void far *)(gvar->mm.nearheap = _nmalloc(length));
797 //huge start = (void huge *)(gvar->mm.nearheap = malloc(length));
798 start = (void far *)(gvar->mm.nearheap = malloc(length));
800 length -= 16-(FP_OFF(start)&15);
801 length -= SAVENEARHEAP;
802 seglength = length / 16; // now in paragraphs
803 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
804 MML_UseSpace(segstart,seglength, gvar);
805 gvar->mmi.nearheap = length;
806 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
809 // get all available far conventional memory segments
811 //printf("_FARCORELEFT %lu\n", _FCORELEFT);
814 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
817 printf("farcoreleft() %lu\n", farcoreleft());
818 printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
819 length=farcoreleft();//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
821 start = gvar->mm.farheap = _fmalloc(length);
822 //start = gvar->mm.farheap = halloc(length, 1);
823 length -= 16-(FP_OFF(start)&15);
824 length -= SAVEFARHEAP;
825 seglength = length / 16; // now in paragraphs
826 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
827 MML_UseSpace(segstart,seglength, gvar);
828 gvar->mmi.farheap = length;
829 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
831 gvar->mmi.mainmem = gvar->mmi.nearheap + gvar->mmi.farheap;
836 // detect EMS and allocate up to 64K at page frame
838 gvar->mmi.EMSmem = 0;
839 //goto emsskip; //0000
840 for(i = 1;i < _argc;i++)
842 if(US_CheckParm(_argv[i],ParmStringsexmm) == 0)
843 goto emsskip; // param NOEMS
845 if(MML_CheckForEMS())
847 MML_SetupEMS(gvar); // allocate space
848 //16_PM: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
849 MML_UseSpace(gvar->mm.EMSPageFrame,(MAPPAGES)*0x4000lu, gvar);
850 //if(gvar->pm.emm.EMSVer<0x40)
851 MM_MapEMS(gvar); // map in used pages
853 //MM_MapXEMS(gvar); // map in used pages
857 // detect XMS and get upper memory blocks
860 gvar->mmi.XMSmem = 0;
862 for(i = 1;i < _argc;i++)
864 if(US_CheckParm( _argv[i],ParmStringsexmm) == 0)
865 goto xmsskip; // param NOXMS
867 if(MML_CheckForXMS())
869 MML_SetupXMS(gvar); // allocate as many UMBs as possible
876 // allocate the misc buffer
878 gvar->mm.mmrover = gvar->mm.mmhead; // start looking for space after low block
880 MM_GetPtr(&(gvar->mm.bufferseg),BUFFERSIZE, gvar);
883 //==========================================================================
890 = Frees all conventional, EMS, and XMS allocated
895 void MM_Shutdown(global_game_variables_t *gvar)
897 if(!(gvar->mm.mmstarted))
900 _ffree(gvar->mm.farheap);// printf(" far freed\n");
902 _nfree(gvar->mm.nearheap);// printf(" near freed\n");
905 free(gvar->mm.nearheap);// printf(" near freed\n");
911 if(MML_CheckForEMS()){ MML_ShutdownEMS(gvar); }//printf(" EMS freed\n"); }
912 if(MML_CheckForXMS()){ MML_ShutdownXMS(gvar); }//printf(" XMS freed\n"); }
919 //==========================================================================
926 = Allocates an unlocked, unpurgable block
931 void MM_GetPtr (memptr *baseptr, dword size, global_game_variables_t *gvar)
933 //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
934 mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next;
936 unsigned needed,startseg;
938 needed = (size+15)/16; // convert size from bytes to paragraphs
940 GETNEWBLOCK; // fill in start and next after a spot is found
941 gvar->mm.mmnew->length = needed;
942 gvar->mm.mmnew->useptr = baseptr;
943 //if(gvar->mm.mmnew->useptr==NULL){
945 printf("MM_GetPtr\n");
947 printf(" baseptr=%Fp ", baseptr); printf("useptr=%Fp\n", gvar->mm.mmnew->useptr);
948 printf(" *baseptr=%Fp ", *baseptr); printf("*useptr=%Fp\n", *(gvar->mm.mmnew->useptr));
949 printf(" &baseptr=%Fp ", &baseptr); printf("&useptr=%Fp\n", &(gvar->mm.mmnew->useptr));
952 gvar->mm.mmnew->attributes = BASEATTRIBUTES;
955 for (search = 0; search<3; search++)
958 // first search: try to allocate right after the rover, then on up
959 // second search: search from the head pointer up to the rover
960 // third search: compress memory, then scan from start
961 if (search == 1 && gvar->mm.mmrover == gvar->mm.mmhead)
967 lastscan = gvar->mm.mmrover;
968 scan = gvar->mm.mmrover->next;
972 lastscan = gvar->mm.mmhead;
973 scan = gvar->mm.mmhead->next;
974 endscan = gvar->mm.mmrover;
978 lastscan = gvar->mm.mmhead;
979 scan = gvar->mm.mmhead->next;
984 startseg = lastscan->start + lastscan->length;
986 while (scan != endscan)
988 if (scan->start - startseg >= needed)
991 // got enough space between the end of lastscan and
992 // the start of scan, so throw out anything in the middle
993 // and allocate the new block
995 purge = lastscan->next;
996 lastscan->next = gvar->mm.mmnew;
997 gvar->mm.mmnew->start = *(unsigned *)baseptr = startseg;
998 gvar->mm.mmnew->next = scan;
999 while ( purge != scan)
1000 { // free the purgable block
1003 purge = next; // purge another if not at scan
1005 gvar->mm.mmrover = gvar->mm.mmnew;
1006 return; // good allocation!
1010 // if this block is purge level zero or locked, skip past it
1012 if ( (scan->attributes & LOCKBIT)
1013 || !(scan->attributes & PURGEBITS) )
1016 startseg = lastscan->start + lastscan->length;
1020 scan=scan->next; // look at next line
1024 if (gvar->mm.bombonerror)
1029 printf(OUT_OF_MEM_MSG,(size-gvar->mmi.nearheap));
1030 printf("for stability reasons the program will shut down! wwww\n");
1035 gvar->mm.mmerror = true;
1038 //==========================================================================
1041 ====================
1045 = Allocates an unlocked, unpurgable block
1047 ====================
1050 void MM_FreePtr(memptr *baseptr, global_game_variables_t *gvar)
1052 //huge mmblocktype huge *scan,huge *last;
1053 mmblocktype far *scan,far *last;
1055 last = gvar->mm.mmhead;
1058 if(baseptr == gvar->mm.mmrover->useptr) // removed the last allocated block
1059 gvar->mm.mmrover = gvar->mm.mmhead;
1061 while(scan->useptr != baseptr && scan)
1069 printf("MM_FreePtr: Block not found!\n");
1073 last->next = scan->next;
1077 //==========================================================================
1080 =====================
1084 = Sets the purge level for a block (locked blocks cannot be made purgable)
1086 =====================
1090 void MM_SetPurge(memptr *baseptr, int purge, global_game_variables_t *gvar)
1092 //huge mmblocktype huge *start;
1093 mmblocktype far *start;
1095 start = gvar->mm.mmrover;
1099 if(gvar->mm.mmrover->useptr == baseptr)
1102 gvar->mm.mmrover = gvar->mm.mmrover->next;
1104 if(!gvar->mm.mmrover)
1105 gvar->mm.mmrover = gvar->mm.mmhead;
1106 else if(gvar->mm.mmrover == start)
1108 Quit("MM_SetPurge: Block not found!");
1114 gvar->mm.mmrover->attributes &= ~PURGEBITS;
1115 gvar->mm.mmrover->attributes |= purge;
1118 //==========================================================================
1121 =====================
1125 = Locks / unlocks the block
1127 =====================
1130 void MM_SetLock(memptr *baseptr, boolean locked, global_game_variables_t *gvar)
1132 //huge mmblocktype huge *start;
1133 mmblocktype far *start;
1135 start = gvar->mm.mmrover;
1139 if(gvar->mm.mmrover->useptr == baseptr)
1142 gvar->mm.mmrover = gvar->mm.mmrover->next;
1144 if(!gvar->mm.mmrover)
1145 gvar->mm.mmrover = gvar->mm.mmhead;
1146 else if(gvar->mm.mmrover == start)
1148 Quit("MM_SetLock: Block not found!");
1154 gvar->mm.mmrover->attributes &= ~LOCKBIT;
1155 gvar->mm.mmrover->attributes |= locked*LOCKBIT;
1158 //==========================================================================
1161 =====================
1165 = Throws out all purgable stuff and compresses movable blocks
1167 =====================
1170 void MM_SortMem(global_game_variables_t *gvar)
1172 //huge mmblocktype huge *scan,huge *last,huge *next;
1173 mmblocktype far *scan,far *last,far *next;
1174 unsigned start,length,source,dest;//++++,oldborder;
1178 // lock down a currently playing sound
1180 /*++++ playing = SD_SoundPlaying ();
1186 playing += STARTPCSOUNDS;
1189 playing += STARTADLIBSOUNDS;
1192 MM_SetLock(&(memptr)audiosegs[playing],true);
1197 // oldborder = bordercolor;
1198 // VW_ColorBorder (15);
1203 scan = gvar->mm.mmhead;
1205 last = NULL; // shut up compiler warning
1209 if(scan->attributes & LOCKBIT)
1212 // block is locked, so try to pile later blocks right after it
1214 start = scan->start + scan->length;
1218 if(scan->attributes & PURGEBITS)
1221 // throw out the purgable block
1225 //MM_FreeBlock(scan, gvar);
1233 // push the non purgable block on top of the last moved block
1235 if(scan->start != start)
1237 length = scan->length;
1238 source = scan->start;
1240 while(length > 0xf00)
1242 movedata(source,0,dest,0,0xf00*16);
1247 movedata(source,0,dest,0,length*16);
1249 scan->start = start;
1250 *(unsigned *)scan->useptr = start;
1252 start = scan->start + scan->length;
1257 scan = scan->next; // go to next block
1260 gvar->mm.mmrover = gvar->mm.mmhead;
1265 // VW_ColorBorder (oldborder);
1268 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1271 //==========================================================================
1274 =====================
1278 =====================
1281 void MM_ShowMemory(global_game_variables_t *gvar)
1283 //huge mmblocktype huge *scan;
1284 mmblocktype far *scan;
1290 byte scratch[160],scratch0[4096],str[16];//[[[[scratch1[160],
1292 //[[[[ VW_SetDefaultColors();
1293 //[[[[ VW_SetLineWidth(40);
1294 //++++mh temp = bufferofs;
1295 //++++mh bufferofs = 0;
1296 //[[[[ VW_SetScreen (0,0);
1297 scan = gvar->mm.mmhead;
1300 CA_OpenDebug (gvar);
1304 strcpy(scratch, AARESET);
1305 if(scan->attributes & PURGEBITS)
1306 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1308 strcpy(scratch0, AABLUE); // medium blue = non purgable
1309 if(scan->attributes & LOCKBIT)
1310 strcpy(scratch0, AARED); // red = locked
1311 if(scan->start<=end)
1313 printf("\nend==%d\n\n", end);
1314 strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1315 strcat(scratch, "End's Size: ");
1317 strcat (scratch,str);
1318 strcat(scratch, "\nscan->start's Size: ");
1319 ultoa (scan->start,str,10);
1320 strcat (scratch,str);
1321 write(gvar->handle.debughandle,scratch,strlen(scratch));
1322 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1325 end = scan->start+(scan->length)-1;
1326 //++++ chy = scan->start/320;
1327 //++++ chx = scan->start%320;
1328 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1329 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1331 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1333 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1334 for(w=(scan->start)/80;w<=end/80;w++)
1336 //printf("+ %u %lu\n", w, scan->length);
1337 strcat(scratch0, "+");
1339 //++==++==optional strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
1340 //++++ VW_Plot(scan->start,0,15);
1341 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1342 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1344 //wwww=(dword)(scan->next->start)-(dword)scan->start;
1345 //wwww=(dword)scan->start+(dword)(scan->next->start);
1346 if (scan->next && scan->next->start >= end+1)
1348 strcat(scratch0, AARESET);
1349 //++==++==optional strcat(scratch0, "\n");
1350 strcat(scratch0,AAGREEN);
1351 for(w=(end+1)/80;w<=((scan->next->start-scan->start)/80);w++)
1352 //for(w=(wwww)/80;w<=((end+1)/80);w++)
1353 //for(w=(end+1)/80;w<=((wwww)/80);w++)
1355 //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
1356 strcat(scratch0,"0");
1358 //printf("==================\n");
1359 //printf("w=%u wwww=%lu start=%04x next=%04x end=%lu\n", w/80, wwww/80, scan->start, (scan->next->start), end+1);
1360 //printf("==================\n");
1361 strcat(scratch0, "\n");
1363 }/*else {//if(scan->next->start <= scan->start){
1364 scan->next->start=scan->start+0x1000;
1365 wwww=(dword)(scan->next->start)-(dword)scan->start;
1366 strcat(scratch0, AARESET);
1367 strcat(scratch0, "\n");
1368 strcat(scratch0,AAGREEN);
1369 for(w=(end+1);w<=(0x1000/80);w++)
1371 //printf("0 %x %x %u\n", scan->start, w);
1372 strcat(scratch0,"0");
1374 printf("================\n");
1375 printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1376 printf("================\n");
1379 strcat(scratch0, AARESET);
1380 //strcat(scratch0,"\n");
1381 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1383 // chx+=scan->next->start;
1384 // modexClearRegion(page, chx, chy, 4, 4, 2);
1386 //modexhlin(page, end+1,scan->next->start, chy, 0);
1387 /* y = scan->start/320;
1388 x = scan->start%320;
1389 VW_Hlin(x,x+end,y,color);
1391 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1392 strcat(scratch,"Seg:");
1393 ultoa (scan->start,str,16);
1394 strcat (scratch,str);
1395 strcat (scratch,"\tSize:");
1396 ultoa ((unsigned)scan->length,str,10);
1397 strcat (scratch,str);
1398 strcat (scratch,"\tOwner:0x");
1399 owner = (unsigned)scan->useptr;
1400 ultoa (owner,str,16);
1401 strcat (scratch,str);
1402 strcat (scratch,"\n");
1403 write(gvar->handle.debughandle,scratch,strlen(scratch));
1404 write(gvar->handle.debughandle,scratch0,strlen(scratch0));
1405 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1407 //fprintf(stdout, "%s", scratch);
1411 /*strcpy(scratch1, AARESET);
1412 strcat(scratch1, "========================================\n");
1413 strcat(scratch1, "near= ");
1414 ultoa (*(gvar->mm.nearheap),str,10);
1415 strcat (scratch1,str);
1416 strcat(scratch1, " far= ");
1417 ultoa (*(gvar->mm.farheap),str,10);
1418 strcat (scratch1,str);
1419 strcat(scratch1, "\n");
1420 //strcat(scratch1, "&near= %Fp ", &(gvar->mm.nearheap));
1421 //strcat(scratch1, "&far= %Fp", &(gvar->mm.farheap));
1422 //strcat(scratch1, "\n");
1423 strcat(scratch1, "========================================\n");
1424 write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/
1427 CA_CloseDebug (gvar);
1430 //**** VW_SetLineWidth(64);
1431 //++++mh bufferofs = temp;
1434 //==========================================================================
1437 =====================
1441 =====================
1444 void MM_DumpData(global_game_variables_t *gvar)
1446 //huge mmblocktype huge *scan,huge *best;
1447 mmblocktype far *scan,far *best;
1448 long lowest,oldlowest;
1453 free(gvar->mm.nearheap);
1455 dumpfile = fopen ("mmdump.16b","w");
1458 dumpfile = fopen ("mmdump.16w","w");
1461 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1471 scan = gvar->mm.mmhead;
1474 owner = (unsigned)scan->useptr;
1476 if (owner && owner<lowest && owner > oldlowest)
1485 if (lowest != 0xffff)
1487 if (best->attributes & PURGEBITS)
1491 if (best->attributes & LOCKBIT)
1495 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1496 ,(unsigned)lowest,lock,purge,best->length);
1499 } while (lowest != 0xffff);
1502 printf("MMDUMP.16 created.\n");
1505 //==========================================================================
1509 ======================
1513 = Returns the total free space without purging
1515 ======================
1518 dword MM_UnusedMemory(global_game_variables_t *gvar)
1521 //huge mmblocktype huge *scan;
1522 mmblocktype far *scan;
1525 scan = gvar->mm.mmhead;
1529 free += scan->next->start - (scan->start + scan->length);
1537 //==========================================================================
1541 ======================
1545 = Returns the total free space with purging
1547 ======================
1550 dword MM_TotalFree(global_game_variables_t *gvar)
1553 //huge mmblocktype huge *scan;
1554 mmblocktype far *scan;
1557 scan = gvar->mm.mmhead;
1561 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1562 free += scan->length;
1563 free += scan->next->start - (scan->start + scan->length);
1571 //==========================================================================
1574 =====================
1578 =====================
1581 void MM_Report_(global_game_variables_t *gvar)
1583 printf("========================================\n");
1584 printf(" MM_Report_\n");
1585 printf("========================================\n");
1586 if(MML_CheckForEMS())
1588 printf(" %cLIMEMS %u\n", 0xC9, gvar->pm.emm.EMSPresent);
1589 printf(" %c%cEMM v%x.%x available\n", 0xC7, 0xC4, gvar->pm.emm.EMSVer>>4,gvar->pm.emm.EMSVer&0x0F);
1590 printf(" %c%ctotalEMSpages: %u ", 0xC7, 0xC4, gvar->pm.emm.totalEMSpages); printf("freeEMSpages: %u\n", gvar->pm.emm.freeEMSpages);
1591 printf(" %c%cEMSPageFrame: %04x\n", 0xC7, 0xC4, gvar->pm.emm.EMSPageFrame);
1592 printf(" %c%cEMSmem: %lu\n", 0xD3, 0xC4, gvar->mmi.EMSmem);
1594 if(MML_CheckForXMS())
1596 printf(" %cXMS %u\n", 0xC9, gvar->pm.xmm.XMSPresent);
1597 printf(" %c%cXMS v%x.%x available\n", 0xC7, 0xC4, XMSVer>>8,XMSVer&0x0F);
1598 printf(" %c%cXMSDriver: %Fp\n", 0xC7, 0xC4, XMSDriver);
1599 printf(" %c%cXMSHandle: %04x\n", 0xC7, 0xC4, gvar->pm.xmm.XMSHandle);
1600 printf(" %c%cXMSmem: %lu\n", 0xD3, 0xC4, gvar->mmi.XMSmem);
1602 printf(" %cConv. %u\n", 0xC9, gvar->pm.mm.MainPresent); DebugMemory_(gvar, 0);
1603 //printf("mainmem: %lu\n", gvar->mmi.mainmem);
1604 //printf("Total convmem: %lu ", gvar->mmi.mainmem); printf("TotalFree: %lu ", MM_TotalFree(gvar)+gvar->mmi.EMSmem+gvar->mmi.XMSmem+gvar->mmi.XMSmem); printf("TotalUsed: %lu\n", gvar->mmi.mainmem);
1605 //printf(" UnusedMemory: %lu\n", MM_UnusedMemory(gvar));
1606 printf("nearheap: %lu ", gvar->mmi.nearheap); printf("farheap: %lu\n", gvar->mmi.farheap);
1609 //==========================================================================
1612 =====================
1616 =====================
1619 void MM_EMSerr(byte *stri, byte err)
1621 //Returns a text string describing the error code in EMS.Error.
1625 strcat(stri, "successful");
1628 strcat(stri, "internal error");
1631 strcat(stri, "hardware malfunction");
1634 strcat(stri, "busy .. retry later");
1637 strcat(stri, "invalid handle");
1640 strcat(stri, "undefined function requested by application");
1643 strcat(stri, "no more handles available");
1646 strcat(stri, "error in save or restore of mapping context");
1649 strcat(stri, "insufficient memory pages in system");
1652 strcat(stri, "insufficient memory pages available");
1655 strcat(stri, "zero pages requested");
1658 strcat(stri, "invalid logical page number encountered");
1661 strcat(stri, "invalid physical page number encountered");
1664 strcat(stri, "page-mapping hardware state save area is full");
1667 strcat(stri, "save of mapping context failed");
1670 strcat(stri, "restore of mapping context failed");
1673 strcat(stri, "undefined subfunction");
1676 strcat(stri, "undefined attribute type");
1679 strcat(stri, "feature not supported");
1682 strcat(stri, "successful, but a portion of the source region has been overwritten");
1685 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1688 strcat(stri, "conventional and expanded memory regions overlap");
1691 strcat(stri, "offset within logical page exceeds size of logical page");
1694 strcat(stri, "region length exceeds 1 MB");
1697 strcat(stri, "source and destination EMS regions have same handle and overlap");
1700 strcat(stri, "memory source or destination type undefined");
1703 strcat(stri, "specified alternate map register or DMA register set not supported");
1706 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1709 strcat(stri, "alternate map register or DMA register sets not supported");
1712 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1715 strcat(stri, "dedicated DMA channels not supported");
1718 strcat(stri, "specified dedicated DMA channel not supported");
1721 strcat(stri, "no such handle name");
1724 strcat(stri, "a handle found had no name, or duplicate handle name");
1727 strcat(stri, "attempted to wrap around 1M conventional address space");
1730 strcat(stri, "source array corrupted");
1733 strcat(stri, "operating system denied access");
1736 strcat(stri, "undefined error");
1740 //==========================================================================
1743 =====================
1747 =====================
1750 void MM_BombOnError(boolean bomb, global_game_variables_t *gvar)
1752 gvar->mm.bombonerror = bomb;
1755 /*void MM_GetNewBlock(global_game_variables_t *gvar)
1757 if(!gvar->mm.mmfree)
1758 MML_ClearBlock(gvar);
1759 gvar->mm.mmnew=gvar->mm.mmfree;
1760 gvar->mm.mmfree=gvar->mm.mmfree->next;
1761 if(!(gvar->mm.mmnew=gvar->mm.mmfree))
1763 printf("MM_GETNEWBLOCK: No free blocks!\n");
1766 gvar->mm.mmfree=gvar->mm.mmfree->next;
1769 void MM_FreeBlock(mmblocktype *x, global_game_variables_t *gvar)
1772 x->next=gvar->mm.mmfree;
1776 void xms_call(byte v, global_game_variables_t *gvar)
1778 dword XMSDriver = gvar->pm.xmm.XMSDriver;
1781 call [DWORD PTR XMSDriver]
1785 /*void MM_seguin(void)
1795 void MM_segude(void)
1803 pull data from far and put it into ds var