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
92 // mov dx,OFFSET emmname
94 mov dx,OFFSET emmname //fix by andrius4669
96 int 0x21 // try to open EMMXXXX0 device
102 int 0x21 // get device info
110 int 0x21 // get status
116 int 0x21 // close handle
146 ======================
150 =======================
153 byte MML_SetupEMS(mminfo_t *mm)
157 boolean errorflag=false;
159 unsigned int EMSVer = 0;
161 unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
162 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
166 int EMS_INT // make sure EMS hardware is present
168 //mov [EMS_status],ah
175 mov [EMSVer],ax // set EMSVer
176 cmp al,0x32 // only work on ems 3.2 or greater
180 int EMS_INT // find the page frame address
183 mov [EMSpageframe],bx
186 int EMS_INT // find out how much EMS is there
189 mov [totalEMSpages],dx
190 mov [freeEMSpages],bx
192 jz noEMS // no EMS at all to allocate
196 cmp bx,[freeEMSpages]
198 mov bx,[freeEMSpages]
208 jle getpages // there is only 1,2,3,or 4 pages
209 mov bx,4 // we can't use more than 4 pages
217 mov [EMSpagesmapped],bx
218 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
244 strcpy(str,"MM_SetupEMS: EMS error ");
249 mm->totalEMSpages=totalEMSpages;
250 mm->freeEMSpages=freeEMSpages;
251 mm->EMSpageframe=EMSpageframe;
252 mm->EMSpagesmapped=EMSpagesmapped;
253 mm->EMShandle=EMShandle;
260 ======================
264 =======================
267 void MML_ShutdownEMS(mminfo_t *mm)
269 boolean errorflag=false;
270 unsigned EMShandle=mm->EMShandle;
288 if(errorflag==true) printf("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(mminfo_t *mm, mminfotype *mmi)
308 boolean errorflag=false;
310 EMShandle=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 mmi->EMSmem = (i)*0x4000lu;
348 //printf(" mmi->EMSmem=%lu\n", mmi->EMSmem);
352 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
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=mm->EMShandle;
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 mmi->EMSmem = (i)*0x4000lu;
429 //==========================================================================
432 ======================
436 = Check for XMM driver
438 =======================
441 boolean MML_CheckForXMS(mminfo_t *mm)
443 boolean errorflag=false;
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(mminfo_t *mm, mminfotype *mmi)
482 mov [WORD PTR XMSaddr],bx
483 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
486 mov dx,0xffff // try for largest block possible
487 //mov ax,dx // Set available Kbytes.
488 call [DWORD PTR XMSaddr]
492 cmp bl,0xb0 // error: smaller UMB is available
496 call [DWORD PTR XMSaddr] // DX holds largest available UMB
498 jz done // another error...
515 // printf("base=%u ", base); printf("size=%u\n", size);
516 MML_UseSpace(base,size, mm);
517 mmi->XMSmem += size*16;
518 mm->UMBbase[mm->numUMBs] = base;
520 if(mm->numUMBs < MAXUMBS)
526 ======================
530 ======================
533 void MML_ShutdownXMS(mminfo_t *mm)
538 for (i=0;i<mm->numUMBs;i++)
540 base = mm->UMBbase[i];
544 call [DWORD PTR XMSaddr]
549 //==========================================================================
552 ======================
556 = Marks a range of paragraphs as usable by the memory manager
557 = This is used to mark space for the near heap, far heap, ems page frame,
558 = and upper memory blocks
560 ======================
563 void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
565 mmblocktype huge *scan,huge *last;
568 dword segmlen=seglength;
571 scan = last = mm->mmhead;
572 mm->mmrover = mm->mmhead; // reset rover to start of memory
575 // search for the block that contains the range of segments
577 while(scan->start+scan->length < segstart)
581 //printf(" start=%x",scan->start);
582 //printf(" seglen=%lu\n", scan->length);
585 //find out how many blocks it spans!
587 for(;segmlen>0x10000;segmlen-=0x10000)
589 //printf(" seglen=%lu\n", segmlen);
594 // take the given range out of the block
596 oldend = scan->start + scan->length;
597 extra = oldend - (segstart+(word)seglength);
599 if(segm>1 || extra>=0x10000lu)
612 //MML_UseSpace(segstart, seglength, mm);
618 printf("MML_UseSpace: Segment spans two blocks!\n");
620 printf("========================================\n");
621 printf("segm=%u ", segm);
622 printf("ex=%lu ", extra);
623 printf("old=%u ", oldend);
624 printf("start+seglen=%lu\n", segstart+seglength);
625 printf("segsta=%x ", segstart);
626 printf("len=%lu ", scan->length);
627 printf("seglen=%lu ", seglength);
628 printf("segmlen=%lu\n", segmlen);
629 printf("========================================\n");
631 //++++todo: linked list of segment!
632 if(segstart == scan->start)
634 last->next = scan->next; // unlink block
639 scan->length = segstart-scan->start; // shorten block
644 mm->mmnew->next = scan->next;
645 scan->next = mm->mmnew;
646 mm->mmnew->start = segstart+seglength;
647 mm->mmnew->length = extra;
648 mm->mmnew->attributes = LOCKBIT;
649 }//else if(segm>0) goto segu;
653 //==========================================================================
660 = We are out of blocks, so free a purgable block
665 void MML_ClearBlock(mminfo_t *mm)
667 mmblocktype huge *scan,huge *last;
669 scan = mm->mmhead->next;
673 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
675 MM_FreePtr(scan->useptr, mm);
681 printf("MM_ClearBlock: No purgable blocks!\n");
685 //==========================================================================
692 = Grabs all space from turbo with malloc/farmalloc
693 = Allocates bufferseg misc buffer
698 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
701 dword length,seglength;
702 //dword length; word seglength;
704 word segstart;//,endfree;
709 mm->mmstarted = true;
710 mm->bombonerror = true;
714 // set up the linked list (everything in the free list;
717 mm->mmfree = &(mm->mmblocks[0]);
718 for(i=0;i<MAXBLOCKS-1;i++)
720 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
722 mm->mmblocks[i].next = NULL;
725 // locked block of all memory until we punch out free space
728 mm->mmhead = mm->mmnew; // this will allways be the first node
729 mm->mmnew->start = 0;
730 mm->mmnew->length = 0xffff;
731 mm->mmnew->attributes = LOCKBIT;
732 mm->mmnew->next = NULL;
733 mm->mmrover = mm->mmhead;
736 // get all available near conventional memory segments
740 length=(dword)_memavl();//(dword)GetFreeSize();
741 start = (void huge *)(mm->nearheap = _nmalloc(length));
745 start = (void huge *)(mm->nearheap = malloc(length));
747 length -= 16-(FP_OFF(start)&15);
748 length -= SAVENEARHEAP;
749 seglength = length / 16; // now in paragraphs
750 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
751 MML_UseSpace(segstart,seglength, mm);
752 mmi->nearheap = length;
753 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
756 // get all available far conventional memory segments
758 //printf("_FARCORELEFT %lu\n", _FCORELEFT);
763 printf("farcoreleft() %lu\n", farcoreleft());
764 printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
766 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
767 start = mm->farheap = _fmalloc(length);
768 //start = mm->farheap = halloc(length, 1);
769 length -= 16-(FP_OFF(start)&15);
770 length -= SAVEFARHEAP;
771 seglength = length / 16; // now in paragraphs
772 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
773 MML_UseSpace(segstart,seglength, mm);
774 mmi->farheap = length;
775 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
777 mmi->mainmem = mmi->nearheap + mmi->farheap;
780 // detect EMS and allocate up to 64K at page frame
783 //goto emsskip; //0000
800 ,ParmStringsexmm) == 0)
801 goto emsskip; // param NOEMS
803 if(MML_CheckForEMS())
805 MML_SetupEMS(mm); // allocate space
806 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
807 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
808 //if(mm->EMSVer<0x40)
809 MM_MapEMS(mm, mmi); // map in used pages
811 //MM_MapXEMS(mm, mmi); // map in used pages
815 // detect XMS and get upper memory blocks
836 ,ParmStringsexmm) == 0)
837 goto xmsskip; // param NOXMS
839 if(MML_CheckForXMS(mm))
841 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
845 // allocate the misc buffer
848 mm->mmrover = mm->mmhead; // start looking for space after low block
850 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
853 //==========================================================================
860 = Frees all conventional, EMS, and XMS allocated
865 void MM_Shutdown(mminfo_t *mm)
870 _ffree(mm->farheap);// printf(" far freed\n");
872 _nfree(mm->nearheap);// printf(" near freed\n");
875 free(mm->nearheap);// printf(" near freed\n");
877 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf(" EMS freed\n"); }
878 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf(" XMS freed\n"); }
881 //==========================================================================
888 = Allocates an unlocked, unpurgable block
893 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
895 mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
900 needed = (size+15)/16; // convert size from bytes to paragraphs
901 //printf("."); //0000
902 GETNEWBLOCK; // fill in start and next after a spot is found
903 mm->mmnew->length = needed;
904 mm->mmnew->useptr = baseptr;
905 mm->mmnew->attributes = BASEATTRIBUTES;
906 //printf("."); //0000
907 for(search = 0; search<mm->endid; search++)
909 printf("\n [case]"); //0000
911 // first search: try to allocate right after the rover, then on up
912 // second search: search from the head pointer up to the rover
913 // third search: compress memory, then scan from start
914 if(search == 1 && mm->mmrover == mm->mmhead)
921 lastscan = mm->mmrover;
922 scan = mm->mmrover->next;
927 lastscan = mm->mmhead;
928 scan = mm->mmhead->next;
929 endscan = mm->mmrover;
934 lastscan = mm->mmhead;
935 scan = mm->mmhead->next;
940 startseg = lastscan->start + lastscan->length;
941 printf(" %x\n", startseg);
942 while(scan != endscan)
945 if(scan->start - startseg >= needed)
949 // got enough space between the end of lastscan and
950 // the start of scan, so throw out anything in the middle
951 // and allocate the new block
953 purge = lastscan->next;
954 lastscan->next = mm->mmnew;
955 mm->mmnew->start = *(word *)baseptr = startseg;
956 mm->mmnew->next = scan;
958 { // free the purgable block
959 printf(" freeing block~\n"); //0000
962 purge = next; // purge another if not at scan
964 mm->mmrover = mm->mmnew;
965 return; // good allocation!
969 // if this block is purge level zero or locked, skip past it
971 if((scan->attributes & LOCKBIT)
972 || !(scan->attributes & PURGEBITS) )
974 printf(" [lock] "); //0000
976 printf("start=%x ", lastscan->start);
977 printf("len=%x ", lastscan->length);
979 printf(" %x", lastscan->start + lastscan->length); //0000
980 printf(" %x", lastscan->start + (word)lastscan->length); //0000
981 printf(" %x", (word)lastscan->start + (word)lastscan->length); //0000
983 startseg = (word)lastscan->start + (word)lastscan->length+1;
984 printf("startseg =%x ok", startseg); //0000
988 scan=scan->next; // look at next line
989 printf("boop\n");//0000
998 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
999 printf("for stability reasons the program will shut down! wwww\n");
1000 printf(" endid=%u\n",(mm->endid));
1008 //==========================================================================
1011 ====================
1015 = Allocates an unlocked, unpurgable block
1017 ====================
1020 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1022 mmblocktype huge *scan,huge *last;
1027 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
1028 mm->mmrover = mm->mmhead;
1030 while(scan->useptr != baseptr && scan)
1038 printf("MM_FreePtr: Block not found!\n");
1042 last->next = scan->next;
1046 //==========================================================================
1049 =====================
1053 = Sets the purge level for a block (locked blocks cannot be made purgable)
1055 =====================
1058 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1060 mmblocktype huge *start;
1062 start = mm->mmrover;
1066 if(mm->mmrover->useptr == baseptr)
1069 mm->mmrover = mm->mmrover->next;
1072 mm->mmrover = mm->mmhead;
1073 else if(mm->mmrover == start)
1075 printf("MM_SetPurge: Block not found!");
1081 mm->mmrover->attributes &= ~PURGEBITS;
1082 mm->mmrover->attributes |= purge;
1085 //==========================================================================
1088 =====================
1092 = Locks / unlocks the block
1094 =====================
1097 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1099 mmblocktype huge *start;
1101 start = mm->mmrover;
1105 if(mm->mmrover->useptr == baseptr)
1108 mm->mmrover = mm->mmrover->next;
1111 mm->mmrover = mm->mmhead;
1112 else if(mm->mmrover == start)
1114 printf("MM_SetLock: Block not found!");
1120 mm->mmrover->attributes &= ~LOCKBIT;
1121 mm->mmrover->attributes |= locked*LOCKBIT;
1124 //==========================================================================
1127 =====================
1131 = Throws out all purgable stuff and compresses movable blocks
1133 =====================
1136 void MM_SortMem(mminfo_t *mm)
1138 mmblocktype huge *scan,huge *last,huge *next;
1139 unsigned start,length,source,dest,oldborder;
1143 // lock down a currently playing sound
1145 /*++++ playing = SD_SoundPlaying ();
1151 playing += STARTPCSOUNDS;
1154 playing += STARTADLIBSOUNDS;
1157 MM_SetLock(&(memptr)audiosegs[playing],true);
1162 // oldborder = bordercolor;
1163 // VW_ColorBorder (15);
1170 last = NULL; // shut up compiler warning
1174 if(scan->attributes & LOCKBIT)
1177 // block is locked, so try to pile later blocks right after it
1179 start = scan->start + scan->length;
1183 if(scan->attributes & PURGEBITS)
1186 // throw out the purgable block
1190 //MM_FreeBlock(scan, mm);
1198 // push the non purgable block on top of the last moved block
1200 if(scan->start != start)
1202 length = scan->length;
1203 source = scan->start;
1205 while(length > 0xf00)
1207 movedata(source,0,dest,0,0xf00*16);
1212 movedata(source,0,dest,0,length*16);
1214 scan->start = start;
1215 *(unsigned *)scan->useptr = start;
1217 start = scan->start + scan->length;
1222 scan = scan->next; // go to next block
1225 mm->mmrover = mm->mmhead;
1230 // VW_ColorBorder (oldborder);
1233 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1236 //==========================================================================
1239 =====================
1243 =====================
1246 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
1248 mmblocktype huge *scan;
1254 byte scratch[160],scratch0[4096],str[16];
1256 //**** VW_SetDefaultColors();
1257 //**** VW_SetLineWidth(40);
1258 //++++mh temp = bufferofs;
1259 //++++mh bufferofs = 0;
1260 //**** VW_SetScreen (0,0);
1264 CA_OpenDebug (gvar);
1268 strcpy(scratch, AARESET);
1269 if(scan->attributes & PURGEBITS)
1270 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1272 strcpy(scratch0, AABLUE); // medium blue = non purgable
1273 if(scan->attributes & LOCKBIT)
1274 strcpy(scratch0, AARED); // red = locked
1275 if(scan->start<=end)
1277 printf("\nend==%d\n\n", end);
1278 strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1279 strcat(scratch, "End's Size: ");
1281 strcat (scratch,str);
1282 strcat(scratch, "\nscan->start's Size: ");
1283 ultoa (scan->start,str,10);
1284 strcat (scratch,str);
1285 write(gvar->handle.debughandle,scratch,strlen(scratch));
1286 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1289 end = scan->start+(scan->length)-1;
1290 //++++ chy = scan->start/320;
1291 //++++ chx = scan->start%320;
1292 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1293 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1295 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1297 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1298 for(w=(scan->start)/80;w<=end/80;w++)
1300 //printf("+ %u %lu\n", w, scan->length);
1301 strcat(scratch0, "+");
1303 strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
1304 //++++ VW_Plot(scan->start,0,15);
1305 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1306 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1308 wwww=(dword)(scan->next->start)-(dword)scan->start;
1309 //wwww=(dword)scan->start+(dword)(scan->next->start);
1310 if (scan->next && scan->next->start >= end+1)
1312 strcat(scratch0, AARESET);
1313 strcat(scratch0, "\n");
1314 strcat(scratch0,AAGREEN);
1315 for(w=(end+1)/80;w<=(wwww/80);w++)
1317 //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
1318 strcat(scratch0,"0");
1320 //printf("==================\n");
1321 //printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1322 //printf("==================\n");
1323 }/*else {//if(scan->next->start <= scan->start){
1324 scan->next->start=scan->start+0x1000;
1325 wwww=(dword)(scan->next->start)-(dword)scan->start;
1326 strcat(scratch0, AARESET);
1327 strcat(scratch0, "\n");
1328 strcat(scratch0,AAGREEN);
1329 for(w=(end+1);w<=(0x1000/80);w++)
1331 //printf("0 %x %x %u\n", scan->start, w);
1332 strcat(scratch0,"0");
1334 printf("================\n");
1335 printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1336 printf("================\n");
1339 strcat(scratch0, AARESET);
1340 strcat(scratch0,"\n");
1341 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1343 // chx+=scan->next->start;
1344 // modexClearRegion(page, chx, chy, 4, 4, 2);
1346 //modexhlin(page, end+1,scan->next->start, chy, 0);
1347 /* y = scan->start/320;
1348 x = scan->start%320;
1349 VW_Hlin(x,x+end,y,color);
1351 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1352 strcat(scratch,"Seg:");
1353 ultoa (scan->start,str,16);
1354 strcat (scratch,str);
1355 strcat (scratch,"\tSize:");
1356 ultoa ((word)scan->length,str,10);
1357 strcat (scratch,str);
1358 strcat (scratch,"\tOwner:0x");
1359 owner = (unsigned)scan->useptr;
1360 ultoa (owner,str,16);
1361 strcat (scratch,str);
1362 strcat (scratch,"\n");
1363 write(gvar->handle.debughandle,scratch,strlen(scratch));
1364 write(gvar->handle.debughandle,scratch0,strlen(scratch0));
1365 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1367 //fprintf(stdout, "%s", scratch);
1372 CA_CloseDebug (gvar);
1375 //**** VW_SetLineWidth(64);
1376 //++++mh bufferofs = temp;
1379 //==========================================================================
1382 =====================
1386 =====================
1389 void MM_DumpData(mminfo_t *mm)
1391 mmblocktype huge *scan,huge *best;
1392 long lowest,oldlowest;
1398 dumpfile = fopen ("mmdump.16","w");
1400 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1413 owner = (word)scan->useptr;
1415 if (owner && owner<lowest && owner > oldlowest)
1424 if (lowest != 0xffff)
1426 if (best->attributes & PURGEBITS)
1430 if (best->attributes & LOCKBIT)
1434 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1435 ,(word)lowest,lock,purge,best->length);
1438 } while (lowest != 0xffff);
1441 printf("MMDUMP.16 created.\n");
1444 //==========================================================================
1448 ======================
1452 = Returns the total free space without purging
1454 ======================
1457 dword MM_UnusedMemory(mminfo_t *mm)
1460 mmblocktype huge *scan;
1467 free += scan->next->start - (scan->start + scan->length);
1475 //==========================================================================
1479 ======================
1483 = Returns the total free space with purging
1485 ======================
1488 dword MM_TotalFree(mminfo_t *mm)
1491 mmblocktype huge *scan;
1498 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1499 free += scan->length;
1500 free += scan->next->start - (scan->start + scan->length);
1508 //==========================================================================
1511 =====================
1515 =====================
1518 void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
1520 if(MML_CheckForEMS())
1522 printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
1523 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1524 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1525 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1527 if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
1528 printf("near=%lu\n", mmi->nearheap);
1529 printf("far=%lu\n", mmi->farheap);
1530 printf("EMSmem=%lu\n", mmi->EMSmem);
1531 printf("XMSmem=%lu\n", mmi->XMSmem);
1532 printf("mainmem=%lu\n", mmi->mainmem);
1533 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1534 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1535 printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem+mmi->XMSmem);
1538 //==========================================================================
1541 =====================
1545 =====================
1548 void MM_EMSerr(byte *stri, byte err)
1550 //Returns a text string describing the error code in EMS.Error.
1554 strcat(stri, "successful");
1557 strcat(stri, "internal error");
1560 strcat(stri, "hardware malfunction");
1563 strcat(stri, "busy .. retry later");
1566 strcat(stri, "invalid handle");
1569 strcat(stri, "undefined function requested by application");
1572 strcat(stri, "no more handles available");
1575 strcat(stri, "error in save or restore of mapping context");
1578 strcat(stri, "insufficient memory pages in system");
1581 strcat(stri, "insufficient memory pages available");
1584 strcat(stri, "zero pages requested");
1587 strcat(stri, "invalid logical page number encountered");
1590 strcat(stri, "invalid physical page number encountered");
1593 strcat(stri, "page-mapping hardware state save area is full");
1596 strcat(stri, "save of mapping context failed");
1599 strcat(stri, "restore of mapping context failed");
1602 strcat(stri, "undefined subfunction");
1605 strcat(stri, "undefined attribute type");
1608 strcat(stri, "feature not supported");
1611 strcat(stri, "successful, but a portion of the source region has been overwritten");
1614 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1617 strcat(stri, "conventional and expanded memory regions overlap");
1620 strcat(stri, "offset within logical page exceeds size of logical page");
1623 strcat(stri, "region length exceeds 1 MB");
1626 strcat(stri, "source and destination EMS regions have same handle and overlap");
1629 strcat(stri, "memory source or destination type undefined");
1632 strcat(stri, "specified alternate map register or DMA register set not supported");
1635 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1638 strcat(stri, "alternate map register or DMA register sets not supported");
1641 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1644 strcat(stri, "dedicated DMA channels not supported");
1647 strcat(stri, "specified dedicated DMA channel not supported");
1650 strcat(stri, "no such handle name");
1653 strcat(stri, "a handle found had no name, or duplicate handle name");
1656 strcat(stri, "attempted to wrap around 1M conventional address space");
1659 strcat(stri, "source array corrupted");
1662 strcat(stri, "operating system denied access");
1665 strcat(stri, "undefined error");
1669 //==========================================================================
1672 =====================
1676 =====================
1679 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1681 mm->bombonerror = bomb;
1684 /*void MM_GetNewBlock(mminfo_t *mm)
1688 mm->mmnew=mm->mmfree;
1689 mm->mmfree=mm->mmfree->next;
1690 if(!(mm->mmnew=mm->mmfree))
1692 printf("MM_GETNEWBLOCK: No free blocks!\n");
1695 mm->mmfree=mm->mmfree->next;
1696 mm->endid++; //end of list
1699 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1704 mm->endid--; //end of list
1707 /*void MM_seguin(void)
1717 void MM_segude(void)
1725 pull data from far and put it into ds var