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->pm.emm.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;
348 //printf(" gvar->mmi.EMSmem=%lu\n", gvar->mmi.EMSmem);
352 byte MM_MapXEMS(global_game_variables_t *gvar)
354 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
356 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
357 //PhysicalStart = Physical page first logical page is mapped to
358 //LogicalStart = First logical page to map
359 //NumPages = Number of pages to map (1 to 4)
360 //Handle = EMS handle logical pages are allocated to
362 /*//Create a buffer containing the page information
363 // FOR x = 0 TO NumPages - 1
364 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
367 // Regs.ax = 0x5000 //Map the pages in the buffer
368 // Regs.cx = NumPages //to the pageframe
370 // Regs.ds = VARSEG(MapInfo$)
371 // Regs.si = SADD(MapInfo$)
372 // InterruptX 0x67, Regs, Regs
373 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
379 boolean errorflag=false;
381 EMShandle=gvar->mm.EMShandle;
383 if(gvar->pm.emm.EMSVer<0x40)
386 for (i=0;i<MAPPAGES;i++)
390 mov cx,[i] // logical page
391 mov al,bl // physical page
392 mov dx,[EMShandle] // handle
415 //strcpy(str,"MM_MapXEMS: EMS error 0x");
416 strcpy(str,"MM_MapXEMS: EMS error ");
420 //printf("%s%x\n",str, err);
421 //printf("FACK! %x\n", err);
425 gvar->mmi.EMSmem = (i)*0x4000lu;
429 //==========================================================================
432 ======================
436 = Check for XMM driver
438 =======================
441 boolean MML_CheckForXMS(global_game_variables_t *gvar)
443 boolean errorflag=false;
444 gvar->mm.numUMBs = 0;
448 int 0x2f // query status of installed diver
459 if(errorflag==true) return false;
465 ======================
469 = Try to allocate all upper memory block
471 =======================
474 void MML_SetupXMS(global_game_variables_t *gvar)
482 mov [WORD PTR XMSaddr],bx
483 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
488 mov dx,0xffff // try for largest block possible
489 //mov ax,dx // Set available Kbytes.
490 call [DWORD PTR XMSaddr]
494 cmp bl,0xb0 // error: smaller UMB is available
498 call [DWORD PTR XMSaddr] // DX holds largest available UMB
500 jz done // another error...
517 // printf("base=%u ", base); printf("size=%u\n", size);
518 MML_UseSpace(base,size, gvar);
519 gvar->mmi.XMSmem += size*16;
520 gvar->mm.UMBbase[gvar->mm.numUMBs] = base;
522 if(gvar->mm.numUMBs < MAXUMBS)
528 ======================
532 ======================
535 void MML_ShutdownXMS(global_game_variables_t *gvar)
540 for (i=0;i<gvar->mm.numUMBs;i++)
542 base = gvar->mm.UMBbase[i];
546 call [DWORD PTR XMSaddr]
551 //==========================================================================
554 ======================
558 = Marks a range of paragraphs as usable by the memory manager
559 = This is used to mark space for the near heap, far heap, ems page frame,
560 = and upper memory blocks
562 ======================
565 /*void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar)
567 //huge mmblocktype huge *scan,huge *last;
573 scan = last = gvar->mm.mmhead;
574 gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory
577 // search for the block that contains the range of segments
579 while(scan->start+scan->length < segstart)
586 // take the given range out of the block
588 oldend = scan->start + scan->length;
589 extra = oldend - (segstart+seglength);
594 if(segm>1)/// || extra>=0x10000lu)
599 //MML_UseSpace(segstart, seglength, gvar);
601 printf("MML_UseSpace: Segment spans two blocks!\n");
603 printf("segm=%u ", segm);
604 printf("ex=%lu ", extra);
605 printf("old=%u ", oldend);
606 printf("start+seglen=%lu\n", segstart+seglength);
607 printf("segsta=%x ", segstart);
608 printf("len=%lu ", scan->length);
609 printf("seglen=%lu ", seglength);
610 printf("segmlen=%lu\n", segmlen);
612 //++++todo: linked list of segment!
613 if(segstart == scan->start)
615 last->next = scan->next; // unlink block
620 scan->length = segstart-scan->start; // shorten block
625 gvar->mm.mmnew->useptr = NULL;
627 gvar->mm.mmnew->next = scan->next;
628 scan->next = gvar->mm.mmnew;
629 gvar->mm.mmnew->start = segstart+seglength;
630 gvar->mm.mmnew->length = extra;
631 gvar->mm.mmnew->attributes = LOCKBIT;
632 }//else if(segm>0) goto segu;
635 void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar)
637 mmblocktype far *scan,far *last;
642 scan = last = gvar->mm.mmhead;
643 gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory
646 // search for the block that contains the range of segments
648 while (scan->start+scan->length < segstart)
655 // find out how many blocks it spans!
657 /*for(;seglength>=0x10000;seglength-=0xFFFF)
659 //printf(" seglen=%lu\n", segmlen);
664 // take the given range out of the block
666 oldend = scan->start + scan->length;
667 extra = oldend - (segstart+((unsigned)seglength));
670 printf("========================================\n");
671 printf("start=%x ", scan->start);
672 printf("old=%u ", oldend);
673 printf("start+seglen=%lu\n", segstart+seglength);
674 printf("segsta=%x ", segstart);
675 printf("len=%lu ", scan->length);
676 printf("seglen=%lu ", seglength);
678 printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
679 printf("========================================\n");
683 if (segstart == scan->start)
685 last->next = scan->next; // unlink block
690 scan->length = segstart-scan->start; // shorten block
695 gvar->mm.mmnew->useptr = NULL;
697 gvar->mm.mmnew->next = scan->next;
698 scan->next = gvar->mm.mmnew;
699 gvar->mm.mmnew->start = segstart+seglength;
700 gvar->mm.mmnew->length = extra;
701 gvar->mm.mmnew->attributes = LOCKBIT;
706 //==========================================================================
713 = We are out of blocks, so free a purgable block
718 void MML_ClearBlock(global_game_variables_t *gvar)
720 //huge mmblocktype huge *scan,huge *last;
721 mmblocktype far *scan,far *last;
723 scan = gvar->mm.mmhead->next;
727 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
729 MM_FreePtr(scan->useptr, gvar);
735 printf("MM_ClearBlock: No purgable blocks!\n");
739 //==========================================================================
746 = Grabs all space from turbo with malloc/farmalloc
747 = Allocates bufferseg misc buffer
752 void MM_Startup(global_game_variables_t *gvar)
755 //dword length,seglength;
756 dword length; word seglength;
757 //huge void huge *start;
759 word segstart;//,endfree;
762 if(gvar->mm.mmstarted)
765 gvar->mm.mmstarted = true;
766 gvar->mm.bombonerror = true;
769 // set up the linked list (everything in the free list;
771 gvar->mm.mmhead = NULL;
772 gvar->mm.mmfree = &(gvar->mm.mmblocks[0]);
773 for(i=0;i<MAXBLOCKS-1;i++)
775 gvar->mm.mmblocks[i].next = &(gvar->mm.mmblocks[i+1]);
777 gvar->mm.mmblocks[i].next = NULL;
780 // locked block of all memory until we punch out free space
783 gvar->mm.mmhead = gvar->mm.mmnew; // this will allways be the first node
784 gvar->mm.mmnew->start = 0;
785 gvar->mm.mmnew->length = 0xffff;
786 gvar->mm.mmnew->attributes = LOCKBIT;
787 gvar->mm.mmnew->next = NULL;
788 //gvar->mm.mmnew->useptr = peeonself;
789 gvar->mm.mmrover = gvar->mm.mmhead;
791 //printf(" %x\n", peeonself);
792 //printf(" %x\n", *peeonself);
794 // get all available near conventional memory segments
798 length=(dword)_memavl();//(dword)GetFreeSize();
799 //huge start = (void huge *)(gvar->mm.nearheap = _nmalloc(length));
800 start = (void far *)(gvar->mm.nearheap = _nmalloc(length));
804 //huge start = (void huge *)(gvar->mm.nearheap = malloc(length));
805 start = (void far *)(gvar->mm.nearheap = malloc(length));
807 length -= 16-(FP_OFF(start)&15);
808 length -= SAVENEARHEAP;
809 seglength = length / 16; // now in paragraphs
810 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
811 MML_UseSpace(segstart,seglength, gvar);
812 gvar->mmi.nearheap = length;
813 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
816 // get all available far conventional memory segments
818 //printf("_FARCORELEFT %lu\n", _FCORELEFT);
823 printf("farcoreleft() %lu\n", farcoreleft());
824 printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
826 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
827 start = gvar->mm.farheap = _fmalloc(length);
828 //start = gvar->mm.farheap = halloc(length, 1);
829 length -= 16-(FP_OFF(start)&15);
830 length -= SAVEFARHEAP;
831 seglength = length / 16; // now in paragraphs
832 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
833 MML_UseSpace(segstart,seglength, gvar);
834 gvar->mmi.farheap = length;
835 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
837 gvar->mmi.mainmem = gvar->mmi.nearheap + gvar->mmi.farheap;
840 // detect EMS and allocate up to 64K at page frame
842 gvar->mmi.EMSmem = 0;
843 //goto emsskip; //0000
860 ,ParmStringsexmm) == 0)
861 goto emsskip; // param NOEMS
863 if(MML_CheckForEMS())
865 MML_SetupEMS(gvar); // allocate space
866 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
867 MML_UseSpace(gvar->mm.EMSpageframe,(MAPPAGES)*0x4000lu, gvar);
868 //if(gvar->pm.emm.EMSVer<0x40)
869 MM_MapEMS(gvar); // map in used pages
871 //MM_MapXEMS(gvar); // map in used pages
875 // detect XMS and get upper memory blocks
878 gvar->mmi.XMSmem = 0;
896 ,ParmStringsexmm) == 0)
897 goto xmsskip; // param NOXMS
899 if(MML_CheckForXMS(gvar))
901 MML_SetupXMS(gvar); // allocate as many UMBs as possible
905 // allocate the misc buffer
908 gvar->mm.mmrover = gvar->mm.mmhead; // start looking for space after low block
910 MM_GetPtr(&(gvar->mm.bufferseg),BUFFERSIZE, gvar);
913 //==========================================================================
920 = Frees all conventional, EMS, and XMS allocated
925 void MM_Shutdown(global_game_variables_t *gvar)
927 if(!(gvar->mm.mmstarted))
930 _ffree(gvar->mm.farheap);// printf(" far freed\n");
932 _nfree(gvar->mm.nearheap);// printf(" near freed\n");
935 free(gvar->mm.nearheap);// printf(" near freed\n");
937 if(MML_CheckForEMS()){ MML_ShutdownEMS(gvar); }//printf(" EMS freed\n"); }
938 if(MML_CheckForXMS(gvar)){ MML_ShutdownXMS(gvar); }//printf(" XMS freed\n"); }
941 //==========================================================================
948 = Allocates an unlocked, unpurgable block
953 void MM_GetPtr (memptr *baseptr, dword size, global_game_variables_t *gvar)
955 //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
956 mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next;
958 unsigned needed,startseg;
960 needed = (size+15)/16; // convert size from bytes to paragraphs
962 GETNEWBLOCK; // fill in start and next after a spot is found
963 gvar->mm.mmnew->length = needed;
964 gvar->mm.mmnew->useptr = baseptr;
965 //if(gvar->mm.mmnew->useptr==NULL){
967 printf("baseptr=%04x ", baseptr); printf("useptr=%04x\n", gvar->mm.mmnew->useptr);
968 printf("*baseptr=%04x ", *baseptr); printf("*useptr=%04x\n", *(gvar->mm.mmnew->useptr));
969 printf("*baseptr=%Fp ", *baseptr); printf("*useptr=%Fp\n", *(gvar->mm.mmnew->useptr));
972 gvar->mm.mmnew->attributes = BASEATTRIBUTES;
975 for (search = 0; search<3; search++)
978 // first search: try to allocate right after the rover, then on up
979 // second search: search from the head pointer up to the rover
980 // third search: compress memory, then scan from start
981 if (search == 1 && gvar->mm.mmrover == gvar->mm.mmhead)
987 lastscan = gvar->mm.mmrover;
988 scan = gvar->mm.mmrover->next;
992 lastscan = gvar->mm.mmhead;
993 scan = gvar->mm.mmhead->next;
994 endscan = gvar->mm.mmrover;
998 lastscan = gvar->mm.mmhead;
999 scan = gvar->mm.mmhead->next;
1004 startseg = lastscan->start + lastscan->length;
1006 while (scan != endscan)
1008 if (scan->start - startseg >= needed)
1011 // got enough space between the end of lastscan and
1012 // the start of scan, so throw out anything in the middle
1013 // and allocate the new block
1015 purge = lastscan->next;
1016 lastscan->next = gvar->mm.mmnew;
1017 gvar->mm.mmnew->start = *(unsigned *)baseptr = startseg;
1018 gvar->mm.mmnew->next = scan;
1019 while ( purge != scan)
1020 { // free the purgable block
1023 purge = next; // purge another if not at scan
1025 gvar->mm.mmrover = gvar->mm.mmnew;
1026 return; // good allocation!
1030 // if this block is purge level zero or locked, skip past it
1032 if ( (scan->attributes & LOCKBIT)
1033 || !(scan->attributes & PURGEBITS) )
1036 startseg = lastscan->start + lastscan->length;
1040 scan=scan->next; // look at next line
1044 if (gvar->mm.bombonerror)
1049 printf(OUT_OF_MEM_MSG,(size-gvar->mmi.nearheap));
1050 printf("for stability reasons the program will shut down! wwww\n");
1055 gvar->mm.mmerror = true;
1058 //==========================================================================
1061 ====================
1065 = Allocates an unlocked, unpurgable block
1067 ====================
1070 void MM_FreePtr(memptr *baseptr, global_game_variables_t *gvar)
1072 //huge mmblocktype huge *scan,huge *last;
1073 mmblocktype far *scan,far *last;
1075 last = gvar->mm.mmhead;
1078 if(baseptr == gvar->mm.mmrover->useptr) // removed the last allocated block
1079 gvar->mm.mmrover = gvar->mm.mmhead;
1081 while(scan->useptr != baseptr && scan)
1089 printf("MM_FreePtr: Block not found!\n");
1093 last->next = scan->next;
1097 //==========================================================================
1100 =====================
1104 = Sets the purge level for a block (locked blocks cannot be made purgable)
1106 =====================
1109 void MM_SetPurge(memptr *baseptr, int purge, global_game_variables_t *gvar)
1111 //huge mmblocktype huge *start;
1112 mmblocktype far *start;
1114 start = gvar->mm.mmrover;
1118 if(gvar->mm.mmrover->useptr == baseptr)
1121 gvar->mm.mmrover = gvar->mm.mmrover->next;
1123 if(!gvar->mm.mmrover)
1124 gvar->mm.mmrover = gvar->mm.mmhead;
1125 else if(gvar->mm.mmrover == start)
1127 Quit("MM_SetPurge: Block not found!");
1133 gvar->mm.mmrover->attributes &= ~PURGEBITS;
1134 gvar->mm.mmrover->attributes |= purge;
1137 //==========================================================================
1140 =====================
1144 = Locks / unlocks the block
1146 =====================
1149 void MM_SetLock(memptr *baseptr, boolean locked, global_game_variables_t *gvar)
1151 //huge mmblocktype huge *start;
1152 mmblocktype far *start;
1154 start = gvar->mm.mmrover;
1158 if(gvar->mm.mmrover->useptr == baseptr)
1161 gvar->mm.mmrover = gvar->mm.mmrover->next;
1163 if(!gvar->mm.mmrover)
1164 gvar->mm.mmrover = gvar->mm.mmhead;
1165 else if(gvar->mm.mmrover == start)
1167 Quit("MM_SetLock: Block not found!");
1173 gvar->mm.mmrover->attributes &= ~LOCKBIT;
1174 gvar->mm.mmrover->attributes |= locked*LOCKBIT;
1177 //==========================================================================
1180 =====================
1184 = Throws out all purgable stuff and compresses movable blocks
1186 =====================
1189 void MM_SortMem(global_game_variables_t *gvar)
1191 //huge mmblocktype huge *scan,huge *last,huge *next;
1192 mmblocktype far *scan,far *last,far *next;
1193 unsigned start,length,source,dest,oldborder;
1197 // lock down a currently playing sound
1199 /*++++ playing = SD_SoundPlaying ();
1205 playing += STARTPCSOUNDS;
1208 playing += STARTADLIBSOUNDS;
1211 MM_SetLock(&(memptr)audiosegs[playing],true);
1216 // oldborder = bordercolor;
1217 // VW_ColorBorder (15);
1222 scan = gvar->mm.mmhead;
1224 last = NULL; // shut up compiler warning
1228 if(scan->attributes & LOCKBIT)
1231 // block is locked, so try to pile later blocks right after it
1233 start = scan->start + scan->length;
1237 if(scan->attributes & PURGEBITS)
1240 // throw out the purgable block
1244 //MM_FreeBlock(scan, gvar);
1252 // push the non purgable block on top of the last moved block
1254 if(scan->start != start)
1256 length = scan->length;
1257 source = scan->start;
1259 while(length > 0xf00)
1261 movedata(source,0,dest,0,0xf00*16);
1266 movedata(source,0,dest,0,length*16);
1268 scan->start = start;
1269 *(unsigned *)scan->useptr = start;
1271 start = scan->start + scan->length;
1276 scan = scan->next; // go to next block
1279 gvar->mm.mmrover = gvar->mm.mmhead;
1284 // VW_ColorBorder (oldborder);
1287 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1290 //==========================================================================
1293 =====================
1297 =====================
1300 void MM_ShowMemory(global_game_variables_t *gvar)
1302 //huge mmblocktype huge *scan;
1303 mmblocktype far *scan;
1309 byte scratch[160],scratch0[4096],scratch1[160],str[16];
1311 //**** VW_SetDefaultColors();
1312 //**** VW_SetLineWidth(40);
1313 //++++mh temp = bufferofs;
1314 //++++mh bufferofs = 0;
1315 //**** VW_SetScreen (0,0);
1316 scan = gvar->mm.mmhead;
1319 CA_OpenDebug (gvar);
1323 strcpy(scratch, AARESET);
1324 if(scan->attributes & PURGEBITS)
1325 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1327 strcpy(scratch0, AABLUE); // medium blue = non purgable
1328 if(scan->attributes & LOCKBIT)
1329 strcpy(scratch0, AARED); // red = locked
1330 if(scan->start<=end)
1332 printf("\nend==%d\n\n", end);
1333 strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1334 strcat(scratch, "End's Size: ");
1336 strcat (scratch,str);
1337 strcat(scratch, "\nscan->start's Size: ");
1338 ultoa (scan->start,str,10);
1339 strcat (scratch,str);
1340 write(gvar->handle.debughandle,scratch,strlen(scratch));
1341 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1344 end = scan->start+(scan->length)-1;
1345 //++++ chy = scan->start/320;
1346 //++++ chx = scan->start%320;
1347 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1348 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1350 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1352 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1353 for(w=(scan->start)/80;w<=end/80;w++)
1355 //printf("+ %u %lu\n", w, scan->length);
1356 strcat(scratch0, "+");
1358 //++==++==optional strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
1359 //++++ VW_Plot(scan->start,0,15);
1360 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1361 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1363 //wwww=(dword)(scan->next->start)-(dword)scan->start;
1364 //wwww=(dword)scan->start+(dword)(scan->next->start);
1365 if (scan->next && scan->next->start >= end+1)
1367 strcat(scratch0, AARESET);
1368 //++==++==optional strcat(scratch0, "\n");
1369 strcat(scratch0,AAGREEN);
1370 for(w=(end+1)/80;w<=((scan->next->start-scan->start)/80);w++)
1371 //for(w=(wwww)/80;w<=((end+1)/80);w++)
1372 //for(w=(end+1)/80;w<=((wwww)/80);w++)
1374 //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
1375 strcat(scratch0,"0");
1377 //printf("==================\n");
1378 //printf("w=%u wwww=%lu start=%04x next=%04x end=%lu\n", w/80, wwww/80, scan->start, (scan->next->start), end+1);
1379 //printf("==================\n");
1380 strcat(scratch0, "\n");
1382 }/*else {//if(scan->next->start <= scan->start){
1383 scan->next->start=scan->start+0x1000;
1384 wwww=(dword)(scan->next->start)-(dword)scan->start;
1385 strcat(scratch0, AARESET);
1386 strcat(scratch0, "\n");
1387 strcat(scratch0,AAGREEN);
1388 for(w=(end+1);w<=(0x1000/80);w++)
1390 //printf("0 %x %x %u\n", scan->start, w);
1391 strcat(scratch0,"0");
1393 printf("================\n");
1394 printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1395 printf("================\n");
1398 strcat(scratch0, AARESET);
1399 //strcat(scratch0,"\n");
1400 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1402 // chx+=scan->next->start;
1403 // modexClearRegion(page, chx, chy, 4, 4, 2);
1405 //modexhlin(page, end+1,scan->next->start, chy, 0);
1406 /* y = scan->start/320;
1407 x = scan->start%320;
1408 VW_Hlin(x,x+end,y,color);
1410 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1411 strcat(scratch,"Seg:");
1412 ultoa (scan->start,str,16);
1413 strcat (scratch,str);
1414 strcat (scratch,"\tSize:");
1415 ultoa ((unsigned)scan->length,str,10);
1416 strcat (scratch,str);
1417 strcat (scratch,"\tOwner:0x");
1418 owner = (unsigned)scan->useptr;
1419 ultoa (owner,str,16);
1420 strcat (scratch,str);
1421 strcat (scratch,"\n");
1422 write(gvar->handle.debughandle,scratch,strlen(scratch));
1423 write(gvar->handle.debughandle,scratch0,strlen(scratch0));
1424 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1426 //fprintf(stdout, "%s", scratch);
1430 /*strcpy(scratch1, AARESET);
1431 strcat(scratch1, "========================================\n");
1432 strcat(scratch1, "near= ");
1433 ultoa (*(gvar->mm.nearheap),str,10);
1434 strcat (scratch1,str);
1435 strcat(scratch1, " far= ");
1436 ultoa (*(gvar->mm.farheap),str,10);
1437 strcat (scratch1,str);
1438 strcat(scratch1, "\n");
1439 //strcat(scratch1, "&near= %Fp ", &(gvar->mm.nearheap));
1440 //strcat(scratch1, "&far= %Fp", &(gvar->mm.farheap));
1441 //strcat(scratch1, "\n");
1442 strcat(scratch1, "========================================\n");
1443 write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/
1446 CA_CloseDebug (gvar);
1449 //**** VW_SetLineWidth(64);
1450 //++++mh bufferofs = temp;
1453 //==========================================================================
1456 =====================
1460 =====================
1463 void MM_DumpData(global_game_variables_t *gvar)
1465 //huge mmblocktype huge *scan,huge *best;
1466 mmblocktype far *scan,far *best;
1467 long lowest,oldlowest;
1472 free(gvar->mm.nearheap);
1474 dumpfile = fopen ("mmdump.16b","w");
1477 dumpfile = fopen ("mmdump.16w","w");
1480 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1490 scan = gvar->mm.mmhead;
1493 owner = (unsigned)scan->useptr;
1495 if (owner && owner<lowest && owner > oldlowest)
1504 if (lowest != 0xffff)
1506 if (best->attributes & PURGEBITS)
1510 if (best->attributes & LOCKBIT)
1514 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1515 ,(unsigned)lowest,lock,purge,best->length);
1518 } while (lowest != 0xffff);
1521 printf("MMDUMP.16 created.\n");
1524 //==========================================================================
1528 ======================
1532 = Returns the total free space without purging
1534 ======================
1537 dword MM_UnusedMemory(global_game_variables_t *gvar)
1540 //huge mmblocktype huge *scan;
1541 mmblocktype far *scan;
1544 scan = gvar->mm.mmhead;
1548 free += scan->next->start - (scan->start + scan->length);
1556 //==========================================================================
1560 ======================
1564 = Returns the total free space with purging
1566 ======================
1569 dword MM_TotalFree(global_game_variables_t *gvar)
1572 //huge mmblocktype huge *scan;
1573 mmblocktype far *scan;
1576 scan = gvar->mm.mmhead;
1580 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1581 free += scan->length;
1582 free += scan->next->start - (scan->start + scan->length);
1590 //==========================================================================
1593 =====================
1597 =====================
1600 void MM_Report_(global_game_variables_t *gvar)
1602 printf("========================================\n");
1603 printf(" MM_Report\n");
1604 printf("========================================\n");
1605 if(MML_CheckForEMS())
1607 printf(" LIMEMS\n");
1608 printf(" EMM v%x.%x available\n", gvar->pm.emm.EMSVer>>4,gvar->pm.emm.EMSVer&0x0F);
1609 printf(" totalEMSpages: %u ", gvar->mm.totalEMSpages); printf("freeEMSpages: %u\n", gvar->mm.freeEMSpages);
1610 printf(" EMSpageframe: %x\n", gvar->mm.EMSpageframe);
1612 if(MML_CheckForXMS(gvar))
1615 printf(" XMSaddr: %X\n", *XMSaddr);
1617 printf("near: %lu ", gvar->mmi.nearheap); printf("far: %lu\n", gvar->mmi.farheap); if(MML_CheckForEMS())
1618 printf("EMSmem: %lu ", gvar->mmi.EMSmem); if(MML_CheckForXMS(gvar)) printf("XMSmem: %lu", gvar->mmi.XMSmem); printf("\n");
1619 //printf("mainmem: %lu\n", gvar->mmi.mainmem);
1620 printf("Total convmem: %lu ", gvar->mmi.mainmem); printf("TotalFree: %lu ", MM_TotalFree(gvar)); printf("TotalUsed: %lu\n", gvar->mmi.mainmem+gvar->mmi.EMSmem+gvar->mmi.XMSmem+gvar->mmi.XMSmem);
1621 printf(" UnusedMemory: %lu\n", MM_UnusedMemory(gvar));
1624 //==========================================================================
1627 =====================
1631 =====================
1634 void MM_EMSerr(byte *stri, byte err)
1636 //Returns a text string describing the error code in EMS.Error.
1640 strcat(stri, "successful");
1643 strcat(stri, "internal error");
1646 strcat(stri, "hardware malfunction");
1649 strcat(stri, "busy .. retry later");
1652 strcat(stri, "invalid handle");
1655 strcat(stri, "undefined function requested by application");
1658 strcat(stri, "no more handles available");
1661 strcat(stri, "error in save or restore of mapping context");
1664 strcat(stri, "insufficient memory pages in system");
1667 strcat(stri, "insufficient memory pages available");
1670 strcat(stri, "zero pages requested");
1673 strcat(stri, "invalid logical page number encountered");
1676 strcat(stri, "invalid physical page number encountered");
1679 strcat(stri, "page-mapping hardware state save area is full");
1682 strcat(stri, "save of mapping context failed");
1685 strcat(stri, "restore of mapping context failed");
1688 strcat(stri, "undefined subfunction");
1691 strcat(stri, "undefined attribute type");
1694 strcat(stri, "feature not supported");
1697 strcat(stri, "successful, but a portion of the source region has been overwritten");
1700 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1703 strcat(stri, "conventional and expanded memory regions overlap");
1706 strcat(stri, "offset within logical page exceeds size of logical page");
1709 strcat(stri, "region length exceeds 1 MB");
1712 strcat(stri, "source and destination EMS regions have same handle and overlap");
1715 strcat(stri, "memory source or destination type undefined");
1718 strcat(stri, "specified alternate map register or DMA register set not supported");
1721 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1724 strcat(stri, "alternate map register or DMA register sets not supported");
1727 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1730 strcat(stri, "dedicated DMA channels not supported");
1733 strcat(stri, "specified dedicated DMA channel not supported");
1736 strcat(stri, "no such handle name");
1739 strcat(stri, "a handle found had no name, or duplicate handle name");
1742 strcat(stri, "attempted to wrap around 1M conventional address space");
1745 strcat(stri, "source array corrupted");
1748 strcat(stri, "operating system denied access");
1751 strcat(stri, "undefined error");
1755 //==========================================================================
1758 =====================
1762 =====================
1765 void MM_BombOnError(boolean bomb, global_game_variables_t *gvar)
1767 gvar->mm.bombonerror = bomb;
1770 /*void MM_GetNewBlock(global_game_variables_t *gvar)
1772 if(!gvar->mm.mmfree)
1773 MML_ClearBlock(gvar);
1774 gvar->mm.mmnew=gvar->mm.mmfree;
1775 gvar->mm.mmfree=gvar->mm.mmfree->next;
1776 if(!(gvar->mm.mmnew=gvar->mm.mmfree))
1778 printf("MM_GETNEWBLOCK: No free blocks!\n");
1781 gvar->mm.mmfree=gvar->mm.mmfree->next;
1784 void MM_FreeBlock(mmblocktype *x, global_game_variables_t *gvar)
1787 x->next=gvar->mm.mmfree;
1791 /*void MM_seguin(void)
1801 void MM_segude(void)
1809 pull data from far and put it into ds var