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"
52 =============================================================================
56 =============================================================================
59 void (* beforesort) (void);
60 void (* aftersort) (void);
61 void (* XMSaddr) (void); // far pointer to XMS driver
64 =============================================================================
68 =============================================================================
71 static char *ParmStringsexmm[] = {"noems","noxms",""};
74 ======================
78 = Routine from p36 of Extending DOS
80 =======================
83 boolean MML_CheckForEMS(void)
86 static char emmname[] = "EMMXXXX0"; //fix by andrius4669
87 // mov dx,OFFSET emmname
89 //LEA DX, emmname //fix by andrius4669
90 mov dx,OFFSET emmname //fix by andrius4669
92 int 0x21 // try to open EMMXXXX0 device
98 int 0x21 // get device info
106 int 0x21 // get status
112 int 0x21 // close handle
131 ======================
135 =======================
138 byte MML_SetupEMS(mminfo_t *mm)
142 boolean errorflag=false;
144 unsigned int EMSVer = 0;
146 unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
147 totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
152 int EMS_INT // make sure EMS hardware is present
154 //mov [EMS_status],ah
161 mov [EMSVer],ax // set EMSVer
162 cmp al,0x32 // only work on ems 3.2 or greater
166 int EMS_INT // find the page frame address
169 mov [EMSpageframe],bx
172 int EMS_INT // find out how much EMS is there
175 mov [totalEMSpages],dx
176 mov [freeEMSpages],bx
178 jz noEMS // no EMS at all to allocate
182 cmp bx,[freeEMSpages]
184 mov bx,[freeEMSpages]
189 jle getpages // there is only 1,2,3,or 4 pages
190 mov bx,4 // we can't use more than 4 pages
193 mov [EMSpagesmapped],bx
194 mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
209 //err = CPURegs.h.ah;
210 strcpy(str,"MM_SetupEMS: EMS error ");
212 MM_EMSerr(&str, err);
216 mm->totalEMSpages=totalEMSpages;
217 mm->freeEMSpages=freeEMSpages;
218 mm->EMSpageframe=EMSpageframe;
219 mm->EMSpagesmapped=EMSpagesmapped;
220 mm->EMShandle=EMShandle;
227 ======================
231 =======================
234 void MML_ShutdownEMS(mminfo_t *mm)
236 boolean errorflag=false;
237 unsigned EMShandle=mm->EMShandle;
251 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n"); //++++ add something
259 = Maps the 64k of EMS used by memory manager into the page frame
260 = for general use. This only needs to be called if you are keeping
261 = other things in EMS.
266 byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
271 boolean errorflag=false;
273 EMShandle=mm->EMShandle;
275 for (i=0;i<4/*MAPPAGES*/;i++)
280 mov bx,[i] // logical page
281 mov al,bl // physical page
282 mov dx,[EMShandle] // handle
294 //err = CPURegs.h.ah;
295 strcpy(str,"MM_MapEMS: EMS error ");
299 //printf("FACK! %x\n", err);
303 mmi->EMSmem = (i)*0x4000lu;
307 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
309 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
311 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
312 //PhysicalStart = Physical page first logical page is mapped to
313 //LogicalStart = First logical page to map
314 //NumPages = Number of pages to map (1 to 4)
315 //Handle = EMS handle logical pages are allocated to
317 /*//Create a buffer containing the page information
318 // FOR x = 0 TO NumPages - 1
319 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
322 // Regs.ax = 0x5000 //Map the pages in the buffer
323 // Regs.cx = NumPages //to the pageframe
325 // Regs.ds = VARSEG(MapInfo$)
326 // Regs.si = SADD(MapInfo$)
327 // InterruptX 0x67, Regs, Regs
328 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
334 boolean errorflag=false;
336 EMShandle=mm->EMShandle;
341 for (i=0;i<MAPPAGES;i++)
346 mov cx,[i] // logical page
347 mov al,bl // physical page
348 mov dx,[EMShandle] // handle
360 //err = CPURegs.h.ah;
361 //strcpy(str,"MM_MapXEMS: EMS error 0x");
362 strcpy(str,"MM_MapXEMS: EMS error ");
364 MM_EMSerr(&str, err);
366 //printf("%s%x\n",str, err);
367 //printf("FACK! %x\n", err);
371 mmi->EMSmem = (i)*0x4000lu;
375 //==========================================================================
378 ======================
382 = Check for XMM driver
384 =======================
387 boolean MML_CheckForXMS(mminfo_t *mm)
389 boolean errorflag=false;
395 int 0x2f // query status of installed diver
401 if(errorflag==true) return false;
407 ======================
411 = Try to allocate all upper memory block
413 =======================
416 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
425 mov [WORD PTR XMSaddr],bx
426 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
429 mov dx,0xffff // try for largest block possible
430 //mov ax,dx // Set available Kbytes.
431 call [DWORD PTR XMSaddr]
435 cmp bl,0xb0 // error: smaller UMB is available
439 call [DWORD PTR XMSaddr] // DX holds largest available UMB
441 jz done // another error...
448 printf("base=%u ", base); printf("size=%u\n", size);
449 MML_UseSpace(base,size, mm);
450 mmi->XMSmem += size*16;
451 mm->UMBbase[mm->numUMBs] = base;
453 if(mm->numUMBs < MAXUMBS)
459 ======================
463 ======================
466 void MML_ShutdownXMS(mminfo_t *mm)
471 for (i=0;i<mm->numUMBs;i++)
473 base = mm->UMBbase[i];
478 call [DWORD PTR XMSaddr]
483 //==========================================================================
486 ======================
490 = Marks a range of paragraphs as usable by the memory manager
491 = This is used to mark space for the near heap, far heap, ems page frame,
492 = and upper memory blocks
494 ======================
497 void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
499 mmblocktype huge *scan,huge *last;
504 scan = last = mm->mmhead;
505 mm->mmrover = mm->mmhead; // reset rover to start of memory
508 // search for the block that contains the range of segments
510 while(scan->start+scan->length < segstart)
516 //find out how many blocks it spans!
517 if(seglength>0xffffu)
519 // segm=seglength/0x4000u;
520 segm=seglength/0xffffu;
525 if(segm>1/*extra>0xfffflu*/)
536 //MML_UseSpace(?segstart?, ?length?, mm);
542 //printf("MML_UseSpace: Segment spans two blocks!\n");
546 // take the given range out of the block
548 oldend = scan->start + scan->length;
549 extra = oldend - (segstart+seglength);
551 printf("segm=%u ", segm);
552 printf("ex=%lu ", extra);
553 printf("start+seglen=%lu ", segstart+seglength);
554 printf("len=%u ", scan->length);
555 printf("segsta=%x ", segstart);
556 printf("seglen=%lu\n", seglength);
559 //++++todo: linked list of segment!
560 //printf("segm=%lu\n", segm);
561 if(segstart == scan->start)
563 last->next = scan->next; // unlink block
564 MM_FreeBlock(scan, mm);
568 scan->length = segstart-scan->start; // shorten block
575 mm->mmnew->next = scan->next;
576 scan->next = mm->mmnew;
577 mm->mmnew->start = segstart+seglength;
578 mm->mmnew->length = extra;
579 mm->mmnew->attributes = LOCKBIT;
580 }//else if(segm>0) goto segu;
584 //==========================================================================
591 = We are out of blocks, so free a purgable block
596 void MML_ClearBlock(mminfo_t *mm)
598 mmblocktype huge *scan,huge *last;
600 scan = mm->mmhead->next;
604 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
606 MM_FreePtr(scan->useptr, mm);
612 printf("MM_ClearBlock: No purgable blocks!\n");
616 //==========================================================================
623 = Grabs all space from turbo with malloc/farmalloc
624 = Allocates bufferseg misc buffer
629 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
632 dword length,seglength;
634 unsigned segstart,endfree;
636 printf("mmi->segu=%Fp\n", (mmi->segu));
640 mm->mmstarted = true;
641 mm->bombonerror = true;
644 // set up the linked list (everything in the free list;
646 //printf(" linked list making!\n");
648 mm->mmfree = &(mm->mmblocks[0]);
649 for(i=0;i<MAXBLOCKS-1;i++)
651 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
653 mm->mmblocks[i].next = NULL;
654 printf("mmi->segu=%Fp\n", (mmi->segu));
657 // locked block of all memory until we punch out free space
659 //printf(" newblock making!\n");
661 mm->mmhead = mm->mmnew; // this will allways be the first node
662 mm->mmnew->start = 0;
663 mm->mmnew->length = 0xffff;
664 mm->mmnew->attributes = LOCKBIT;
665 mm->mmnew->next = NULL;
666 mm->mmrover = mm->mmhead;
667 printf("mmi->segu=%Fp\n", (mmi->segu));
670 // get all available near conventional memory segments
672 //printf(" nearheap making!\n");
673 //---- length=coreleft();
676 start = /*(void *)*/(mm->nearheap = _nmalloc(length));
677 length -= 16-(FP_OFF(start)&15);
678 length -= SAVENEARHEAP;
679 seglength = length / 16; // now in paragraphs
680 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
681 MML_UseSpace(segstart,seglength, mm);
682 mmi->nearheap = length;
683 /*switch( _nheapchk() ) {
685 printf( "OK - nearheap is good\n" );
688 printf( "OK - nearheap is empty\n" );
691 printf( "ERROR - nearheap is damaged\n" );
694 printf( "ERROR - bad node in nearheap\n" );
697 //printf(" near heap ok!\n");
700 // get all available far conventional memory segments
702 //---- length=farcoreleft();
703 //printf(" farheap making!\n");
705 length=0xffffUL*4UL;//_memavl();
706 start = mm->farheap = halloc(length, sizeof(byte));
707 //start = mm->farheap = _fmalloc(length);
708 length -= 16-(FP_OFF(start)&15);
709 length -= SAVEFARHEAP;
710 seglength = length / 16; // now in paragraphs
711 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
712 MML_UseSpace(segstart,seglength, mm);
713 mmi->farheap = length;
714 mmi->mainmem = mmi->nearheap + mmi->farheap;
715 /*switch( _fheapchk() ) {
717 printf( "OK - farheap is good\n" );
720 printf( "OK - farheap is empty\n" );
723 printf( "ERROR - farheap is damaged\n" );
726 printf( "ERROR - bad node in farheap\n" );
729 //printf(" far heap ok!\n");
732 // detect EMS and allocate up to 64K at page frame
735 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
737 for(i = 1;i < __argc;i++)
739 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
740 goto emsskip; // param NOEMS
742 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
743 if(MML_CheckForEMS())
746 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
747 MML_SetupEMS(mm); // allocate space
749 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
750 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
751 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
752 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
754 //if(mm->EMSVer<0x40)
755 MM_MapEMS(mm, mmi); // map in used pages
757 //MM_MapXEMS(mm, mmi); // map in used pages
761 // detect XMS and get upper memory blocks
765 for(i = 1;i < __argc;i++)
767 if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
768 goto xmsskip; // param NOXMS
770 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
771 if(MML_CheckForXMS(mm))
773 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
775 //++++ MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
779 // allocate the misc buffer
786 mm->mmrover = mm->mmhead; // start looking for space after low block
788 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
791 //==========================================================================
798 = Frees all conventional, EMS, and XMS allocated
803 void MM_Shutdown(mminfo_t *mm)
809 printf(" far freed\n");
811 printf(" near freed\n");
812 //hfree(mm->hugeheap);
813 //printf("huge freed\n");
814 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); printf(" EMS freed\n"); }
815 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); printf(" XMS freed\n"); }
818 //==========================================================================
825 = Allocates an unlocked, unpurgable block
830 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
832 mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
834 unsigned needed,startseg;
836 needed = (size+15)/16; // convert size from bytes to paragraphs
838 MM_GetNewBlock(mm); // fill in start and next after a spot is found
839 mm->mmnew->length = needed;
840 mm->mmnew->useptr = baseptr;
841 mm->mmnew->attributes = BASEATTRIBUTES;
843 for(search = 0; search<3; search++)
846 // first search: try to allocate right after the rover, then on up
847 // second search: search from the head pointer up to the rover
848 // third search: compress memory, then scan from start
849 if(search == 1 && mm->mmrover == mm->mmhead)
855 lastscan = mm->mmrover;
856 scan = mm->mmrover->next;
860 lastscan = mm->mmhead;
861 scan = mm->mmhead->next;
862 endscan = mm->mmrover;
866 lastscan = mm->mmhead;
867 scan = mm->mmhead->next;
872 startseg = lastscan->start + lastscan->length;
874 while(scan != endscan)
876 if(scan->start - startseg >= needed)
879 // got enough space between the end of lastscan and
880 // the start of scan, so throw out anything in the middle
881 // and allocate the new block
883 purge = lastscan->next;
884 lastscan->next = mm->mmnew;
885 mm->mmnew->start = *(unsigned *)baseptr = startseg;
886 mm->mmnew->next = scan;
888 { // free the purgable block
890 MM_FreeBlock(purge, mm);
891 purge = next; // purge another if not at scan
893 mm->mmrover = mm->mmnew;
894 return; // good allocation!
898 // if this block is purge level zero or locked, skip past it
900 if((scan->attributes & LOCKBIT)
901 || !(scan->attributes & PURGEBITS) )
904 startseg = lastscan->start + lastscan->length;
908 scan=scan->next; // look at next line
913 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
918 //==========================================================================
925 = Allocates an unlocked, unpurgable block
930 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
932 mmblocktype huge *scan,huge *last;
937 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
938 mm->mmrover = mm->mmhead;
940 while(scan->useptr != baseptr && scan)
948 printf("MM_FreePtr: Block not found!\n");
952 last->next = scan->next;
954 MM_FreeBlock(scan, mm);
956 //==========================================================================
959 =====================
963 = Sets the purge level for a block (locked blocks cannot be made purgable)
965 =====================
968 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
970 mmblocktype huge *start;
976 if(mm->mmrover->useptr == baseptr)
979 mm->mmrover = mm->mmrover->next;
982 mm->mmrover = mm->mmhead;
983 else if(mm->mmrover == start)
985 printf("MM_SetPurge: Block not found!");
991 mm->mmrover->attributes &= ~PURGEBITS;
992 mm->mmrover->attributes |= purge;
995 //==========================================================================
998 =====================
1002 = Locks / unlocks the block
1004 =====================
1007 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1009 mmblocktype huge *start;
1011 start = mm->mmrover;
1015 if(mm->mmrover->useptr == baseptr)
1018 mm->mmrover = mm->mmrover->next;
1021 mm->mmrover = mm->mmhead;
1022 else if(mm->mmrover == start)
1024 printf("MM_SetLock: Block not found!");
1030 mm->mmrover->attributes &= ~LOCKBIT;
1031 mm->mmrover->attributes |= locked*LOCKBIT;
1034 //==========================================================================
1037 =====================
1041 = Throws out all purgable stuff and compresses movable blocks
1043 =====================
1046 void MM_SortMem(mminfo_t *mm)
1048 mmblocktype huge *scan,huge *last,huge *next;
1049 unsigned start,length,source,dest,oldborder;
1053 // lock down a currently playing sound
1055 /*++++ playing = SD_SoundPlaying ();
1061 playing += STARTPCSOUNDS;
1064 playing += STARTADLIBSOUNDS;
1067 MM_SetLock(&(memptr)audiosegs[playing],true);
1072 // oldborder = bordercolor;
1073 // VW_ColorBorder (15);
1080 last = NULL; // shut up compiler warning
1084 if(scan->attributes & LOCKBIT)
1087 // block is locked, so try to pile later blocks right after it
1089 start = scan->start + scan->length;
1093 if(scan->attributes & PURGEBITS)
1096 // throw out the purgable block
1099 MM_FreeBlock(scan, mm);
1107 // push the non purgable block on top of the last moved block
1109 if(scan->start != start)
1111 length = scan->length;
1112 source = scan->start;
1114 while(length > 0xf00)
1116 movedata(source,0,dest,0,0xf00*16);
1121 movedata(source,0,dest,0,length*16);
1123 scan->start = start;
1124 *(unsigned *)scan->useptr = start;
1126 start = scan->start + scan->length;
1131 scan = scan->next; // go to next block
1134 mm->mmrover = mm->mmhead;
1139 // VW_ColorBorder (oldborder);
1142 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1146 //==========================================================================
1150 =====================
1154 =====================
1157 void MM_ShowMemory(/*page_t *page, */mminfo_t *mm)
1159 mmblocktype huge *scan;
1163 byte scratch[160],str[16];
1165 //**** VW_SetDefaultColors();
1166 //**** VW_SetLineWidth(40);
1167 //++++mh temp = bufferofs;
1168 //++++mh bufferofs = 0;
1169 //**** VW_SetScreen (0,0);
1182 if(scan->attributes & PURGEBITS)
1183 color = 5; // dark purple = purgable
1185 color = 9; // medium blue = non purgable
1186 if(scan->attributes & LOCKBIT)
1187 color = 12; // red = locked
1188 if(scan->start<=end)
1191 write(debughandle,"\nMM_ShowMemory: Memory block order currupted!\n",strlen("\nMM_ShowMemory: Memory block order currupted!\n"));
1192 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1195 end = scan->start+scan->length-1;
1196 chy = scan->start/320;
1197 chx = scan->start%320;
1198 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1199 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1201 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1204 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1206 //++++ VW_Plot(scan->start,0,15);
1207 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1208 if(scan->next->start > end+1)
1209 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1210 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1212 //++++ chx+=scan->next->start;
1213 //++++ modexClearRegion(page, chx, chy, 4, 4, 2);
1215 //modexhlin(page, end+1,scan->next->start, chy, 0);
1218 end = scan->length-1;
1219 y = scan->start/320;
1220 x = scan->start%320;
1221 VW_Hlin(x,x+end,y,color);
1223 if (scan->next && scan->next->start > end+1)
1224 VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1228 printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); //bug!
1229 strcpy(scratch,"Seg:");
1230 ultoa (scan->start,str,16);
1231 strcat (scratch,str);
1232 strcat (scratch,"\tSize:");
1233 ltoa ((dword)scan->length*16,str,10);
1234 strcat (scratch,str);
1235 strcat (scratch,"\tOwner:0x");
1236 owner = (unsigned)scan->useptr;
1237 ultoa (owner,str,16);
1238 strcat (scratch,str);
1239 strcat (scratch,"\n");
1240 write(debughandle,scratch,strlen(scratch));
1241 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1243 //fprintf(stdout, "%s", scratch);
1252 //**** VW_SetLineWidth(64);
1253 //++++mh bufferofs = temp;
1257 //==========================================================================
1260 =====================
1264 =====================
1267 void MM_DumpData(mminfo_t *mm)
1269 mmblocktype far *scan,far *best;
1270 long lowest,oldlowest;
1276 //++++free(mm->nearheap);
1277 dumpfile = fopen ("mmdump.txt","w");
1279 printf("MM_DumpData: Couldn't open MMDUMP.TXT!\n");
1292 owner = (word)scan->useptr;
1294 if (owner && owner<lowest && owner > oldlowest)
1303 if (lowest != 0xffff)
1305 if (best->attributes & PURGEBITS)
1309 if (best->attributes & LOCKBIT)
1313 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1314 ,(word)lowest,lock,purge,best->length);
1317 } while (lowest != 0xffff);
1320 printf("MMDUMP.TXT created.\n");
1323 //==========================================================================
1327 ======================
1331 = Returns the total free space without purging
1333 ======================
1336 dword MM_UnusedMemory(mminfo_t *mm)
1339 mmblocktype huge *scan;
1346 free += scan->next->start - (scan->start + scan->length);
1354 //==========================================================================
1358 ======================
1362 = Returns the total free space with purging
1364 ======================
1367 dword MM_TotalFree(mminfo_t *mm)
1370 mmblocktype huge *scan;
1377 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1378 free += scan->length;
1379 free += scan->next->start - (scan->start + scan->length);
1387 //==========================================================================
1390 =====================
1394 =====================
1397 void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
1399 if(MML_CheckForEMS())
1401 printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
1402 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1403 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1404 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1406 if(MML_CheckForXMS(mm)) printf("XMSaddr=%Fp\n", *XMSaddr);
1407 printf("near=%lu\n", mmi->nearheap);
1408 printf("far=%lu\n", mmi->farheap);
1409 printf("EMSmem=%lu\n", mmi->EMSmem);
1410 printf("XMSmem=%lu\n", mmi->XMSmem);
1411 printf("mainmem=%lu\n", mmi->mainmem);
1412 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1413 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1415 // printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
1416 // printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
1419 //==========================================================================
1422 =====================
1426 =====================
1429 void MM_EMSerr(byte *stri, byte err)
1431 //Returns a text string describing the error code in EMS.Error.
1435 strcat(stri, "successful");
1438 strcat(stri, "internal error");
1441 strcat(stri, "hardware malfunction");
1444 strcat(stri, "busy .. retry later");
1447 strcat(stri, "invalid handle");
1450 strcat(stri, "undefined function requested by application");
1453 strcat(stri, "no more handles available");
1456 strcat(stri, "error in save or restore of mapping context");
1459 strcat(stri, "insufficient memory pages in system");
1462 strcat(stri, "insufficient memory pages available");
1465 strcat(stri, "zero pages requested");
1468 strcat(stri, "invalid logical page number encountered");
1471 strcat(stri, "invalid physical page number encountered");
1474 strcat(stri, "page-mapping hardware state save area is full");
1477 strcat(stri, "save of mapping context failed");
1480 strcat(stri, "restore of mapping context failed");
1483 strcat(stri, "undefined subfunction");
1486 strcat(stri, "undefined attribute type");
1489 strcat(stri, "feature not supported");
1492 strcat(stri, "successful, but a portion of the source region has been overwritten");
1495 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1498 strcat(stri, "conventional and expanded memory regions overlap");
1501 strcat(stri, "offset within logical page exceeds size of logical page");
1504 strcat(stri, "region length exceeds 1 MB");
1507 strcat(stri, "source and destination EMS regions have same handle and overlap");
1510 strcat(stri, "memory source or destination type undefined");
1513 strcat(stri, "specified alternate map register or DMA register set not supported");
1516 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1519 strcat(stri, "alternate map register or DMA register sets not supported");
1522 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1525 strcat(stri, "dedicated DMA channels not supported");
1528 strcat(stri, "specified dedicated DMA channel not supported");
1531 strcat(stri, "no such handle name");
1534 strcat(stri, "a handle found had no name, or duplicate handle name");
1537 strcat(stri, "attempted to wrap around 1M conventional address space");
1540 strcat(stri, "source array corrupted");
1543 strcat(stri, "operating system denied access");
1546 strcat(stri, "undefined error");
1550 //==========================================================================
1553 =====================
1557 =====================
1560 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1562 mm->bombonerror = bomb;
1565 void MM_GetNewBlock(mminfo_t *mm)
1569 mm->mmnew=mm->mmfree;
1570 mm->mmfree=mm->mmfree->next;
1571 /*if(!(mm->mmnew=mm->mmfree))
1573 printf("MM_GETNEWBLOCK: No free blocks!");
1576 mm->mmfree=mm->mmfree->next;*/
1579 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1586 void MM_seguin(void)
1597 void MM_segude(void)
1606 pull data from far and put it into ds var