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"
56 =============================================================================
60 =============================================================================
63 void (* beforesort) (void);
64 void (* aftersort) (void);
65 void (* XMSaddr) (void); // far pointer to XMS driver
68 =============================================================================
72 =============================================================================
75 static char *ParmStringsexmm[] = {"noems","noxms",""};
78 ======================
82 = Routine from p36 of Extending DOS
84 =======================
87 boolean MML_CheckForEMS(void)
90 static char emmname[] = "EMMXXXX0"; //fix by andrius4669
91 // mov dx,OFFSET emmname
93 //LEA DX, emmname //fix by andrius4669
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 //err = CPURegs.h.ah;
245 strcpy(str,"MM_SetupEMS: EMS error ");
251 mm->totalEMSpages=totalEMSpages;
252 mm->freeEMSpages=freeEMSpages;
253 mm->EMSpageframe=EMSpageframe;
254 mm->EMSpagesmapped=EMSpagesmapped;
255 mm->EMShandle=EMShandle;
262 ======================
266 =======================
269 void MML_ShutdownEMS(mminfo_t *mm)
271 boolean errorflag=false;
272 unsigned EMShandle=mm->EMShandle;
290 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n"); //++++ add something
298 = Maps the 64k of EMS used by memory manager into the page frame
299 = for general use. This only needs to be called if you are keeping
300 = other things in EMS.
305 byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
310 boolean errorflag=false;
312 EMShandle=mm->EMShandle;
314 for (i=0;i<4/*MAPPAGES*/;i++)
318 mov bx,[i] // logical page
319 mov al,bl // physical page
320 mov dx,[EMShandle] // handle
343 //err = CPURegs.h.ah;
344 strcpy(str,"MM_MapEMS: EMS error ");
348 //printf("FACK! %x\n", err);
352 mmi->EMSmem = (i)*0x4000lu;
356 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
358 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
360 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
361 //PhysicalStart = Physical page first logical page is mapped to
362 //LogicalStart = First logical page to map
363 //NumPages = Number of pages to map (1 to 4)
364 //Handle = EMS handle logical pages are allocated to
366 /*//Create a buffer containing the page information
367 // FOR x = 0 TO NumPages - 1
368 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
371 // Regs.ax = 0x5000 //Map the pages in the buffer
372 // Regs.cx = NumPages //to the pageframe
374 // Regs.ds = VARSEG(MapInfo$)
375 // Regs.si = SADD(MapInfo$)
376 // InterruptX 0x67, Regs, Regs
377 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
383 boolean errorflag=false;
385 EMShandle=mm->EMShandle;
390 for (i=0;i<MAPPAGES;i++)
394 mov cx,[i] // logical page
395 mov al,bl // physical page
396 mov dx,[EMShandle] // handle
419 //err = CPURegs.h.ah;
420 //strcpy(str,"MM_MapXEMS: EMS error 0x");
421 strcpy(str,"MM_MapXEMS: EMS error ");
425 //printf("%s%x\n",str, err);
426 //printf("FACK! %x\n", err);
430 mmi->EMSmem = (i)*0x4000lu;
434 //==========================================================================
437 ======================
441 = Check for XMM driver
443 =======================
446 boolean MML_CheckForXMS(mminfo_t *mm)
448 boolean errorflag=false;
453 int 0x2f // query status of installed diver
464 if(errorflag==true) return false;
470 ======================
474 = Try to allocate all upper memory block
476 =======================
479 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
487 mov [WORD PTR XMSaddr],bx
488 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
491 mov dx,0xffff // try for largest block possible
492 //mov ax,dx // Set available Kbytes.
493 call [DWORD PTR XMSaddr]
497 cmp bl,0xb0 // error: smaller UMB is available
501 call [DWORD PTR XMSaddr] // DX holds largest available UMB
503 jz done // another error...
520 // printf("base=%u ", base); printf("size=%u\n", size);
521 MML_UseSpace(base,size, mm);
522 mmi->XMSmem += size*16;
523 mm->UMBbase[mm->numUMBs] = base;
525 if(mm->numUMBs < MAXUMBS)
531 ======================
535 ======================
538 void MML_ShutdownXMS(mminfo_t *mm)
543 for (i=0;i<mm->numUMBs;i++)
545 base = mm->UMBbase[i];
549 call [DWORD PTR XMSaddr]
554 //==========================================================================
557 ======================
561 = Marks a range of paragraphs as usable by the memory manager
562 = This is used to mark space for the near heap, far heap, ems page frame,
563 = and upper memory blocks
565 ======================
568 void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
570 mmblocktype huge *scan,huge *last;
575 scan = last = mm->mmhead;
576 mm->mmrover = mm->mmhead; // reset rover to start of memory
579 // search for the block that contains the range of segments
581 while(scan->start+scan->length < segstart)
587 //find out how many blocks it spans!
588 if(seglength>0xffffu)
590 // segm=seglength/0x4000u;
591 segm=(word)seglength/0xffffu;
596 if(segm>1/*extra>0xfffflu*/)
607 //MML_UseSpace(?segstart?, ?length?, mm);
613 //printf("MML_UseSpace: Segment spans two blocks!\n");
617 // take the given range out of the block
619 oldend = scan->start + scan->length;
620 extra = oldend - (segstart+seglength);
622 printf("segm=%u ", segm);
623 printf("ex=%lu ", extra);
624 printf("start+seglen=%lu ", segstart+seglength);
625 printf("len=%u ", scan->length);
626 printf("segsta=%x ", segstart);
627 printf("seglen=%lu\n", seglength);
630 //++++todo: linked list of segment!
631 //printf("segm=%lu\n", segm);
632 if(segstart == scan->start)
634 last->next = scan->next; // unlink block
639 scan->length = segstart-scan->start; // shorten block
645 //MM_GetNewBlock(mm);
647 mm->mmnew->next = scan->next;
648 scan->next = mm->mmnew;
649 mm->mmnew->start = segstart+seglength;
650 mm->mmnew->length = extra;
651 mm->mmnew->attributes = LOCKBIT;
652 }//else if(segm>0) goto segu;
656 //==========================================================================
663 = We are out of blocks, so free a purgable block
668 void MML_ClearBlock(mminfo_t *mm)
670 mmblocktype huge *scan,huge *last;
672 scan = mm->mmhead->next;
676 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
678 MM_FreePtr(scan->useptr, mm);
684 printf("MM_ClearBlock: No purgable blocks!\n");
688 //==========================================================================
695 = Grabs all space from turbo with malloc/farmalloc
696 = Allocates bufferseg misc buffer
701 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
704 dword length,seglength;
705 //dword length; word seglength;
707 word segstart;//,endfree;
712 mm->mmstarted = true;
713 mm->bombonerror = true;
717 // set up the linked list (everything in the free list;
719 //printf(" linked list making!\n");
721 mm->mmfree = &(mm->mmblocks[0]);
722 for(i=0;i<MAXBLOCKS-1;i++)
724 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
726 mm->mmblocks[i].next = NULL;
729 // locked block of all memory until we punch out free space
731 //printf(" newblock making!\n");
732 //MM_GetNewBlock(mm);
734 mm->mmhead = mm->mmnew; // this will allways be the first node
735 mm->mmnew->start = 0;
736 mm->mmnew->length = 0xffff;
737 mm->mmnew->attributes = LOCKBIT;
738 mm->mmnew->next = NULL;
739 mm->mmrover = mm->mmhead;
742 // get all available near conventional memory segments
744 // printf(" nearheap making!\n");
747 length=(dword)_memavl();//(dword)GetFreeSize();
748 start = (void huge *)(mm->nearheap = _nmalloc(length));
752 start = (void huge *)(mm->nearheap = malloc(length));
754 length -= 16-(FP_OFF(start)&15);
755 length -= SAVENEARHEAP;
756 seglength = length / 16; // now in paragraphs
757 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
758 MML_UseSpace(segstart,seglength, mm);
759 mmi->nearheap = length;
760 printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
764 // get all available far conventional memory segments
766 // printf(" farheap making!\n");
767 printf("_FARCORELEFT %lu\n", _FCORELEFT);
772 printf(" %lu\n", farcoreleft());
773 printf(" %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
775 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
776 start = mm->farheap = _fmalloc(length);
777 //start = mm->farheap = halloc(length, 1);
778 length -= 16-(FP_OFF(start)&15);
779 length -= SAVEFARHEAP;
780 seglength = length / 16; // now in paragraphs
781 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
782 MML_UseSpace(segstart,seglength, mm);
783 mmi->farheap = length;
784 printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
787 mmi->mainmem = mmi->nearheap + mmi->farheap;
794 // detect EMS and allocate up to 64K at page frame
797 //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!
815 ,ParmStringsexmm) == 0)
816 goto emsskip; // param NOEMS
818 //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!
819 if(MML_CheckForEMS())
822 //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!
823 MML_SetupEMS(mm); // allocate space
825 //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!
826 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
827 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
828 //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!
830 //if(mm->EMSVer<0x40)
831 MM_MapEMS(mm, mmi); // map in used pages
833 //MM_MapXEMS(mm, mmi); // map in used pages
837 // detect XMS and get upper memory blocks
857 ,ParmStringsexmm) == 0)
858 goto xmsskip; // param NOXMS
860 //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!
861 if(MML_CheckForXMS(mm))
863 //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!
865 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
869 // allocate the misc buffer
872 mm->mmrover = mm->mmhead; // start looking for space after low block
874 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
877 //==========================================================================
884 = Frees all conventional, EMS, and XMS allocated
889 void MM_Shutdown(mminfo_t *mm)
894 _ffree(mm->farheap);// printf(" far freed\n");
896 _nfree(mm->nearheap);// printf(" near freed\n");
899 free(mm->nearheap);// printf(" near freed\n");
901 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf(" EMS freed\n"); }
902 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf(" XMS freed\n"); }
905 //==========================================================================
912 = Allocates an unlocked, unpurgable block
917 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
919 mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
924 needed = (size+15)/16; // convert size from bytes to paragraphs
926 //MM_GetNewBlock(mm);
927 GETNEWBLOCK; // fill in start and next after a spot is found
928 mm->mmnew->length = needed;
929 mm->mmnew->useptr = baseptr;
930 mm->mmnew->attributes = BASEATTRIBUTES;
932 for(search = 0; search<mm->endid; search++)
934 printf(" [case]"); //0000
936 // first search: try to allocate right after the rover, then on up
937 // second search: search from the head pointer up to the rover
938 // third search: compress memory, then scan from start
939 if(search == 1 && mm->mmrover == mm->mmhead)
946 lastscan = mm->mmrover;
947 scan = mm->mmrover->next;
952 lastscan = mm->mmhead;
953 scan = mm->mmhead->next;
954 endscan = mm->mmrover;
959 lastscan = mm->mmhead;
960 scan = mm->mmhead->next;
965 startseg = lastscan->start + (word)lastscan->length;
967 while(scan != endscan)
969 //printf(","); //0000
970 if(scan->start - startseg >= needed)
974 // got enough space between the end of lastscan and
975 // the start of scan, so throw out anything in the middle
976 // and allocate the new block
978 purge = lastscan->next;
979 lastscan->next = mm->mmnew;
980 mm->mmnew->start = *(word *)baseptr = startseg;
981 mm->mmnew->next = scan;
983 { // free the purgable block
984 printf(" freeing block~\n"); //0000
987 //MM_FreeBlock(purge, mm);
988 purge = next; // purge another if not at scan
990 mm->mmrover = mm->mmnew;
991 return; // good allocation!
995 // if this block is purge level zero or locked, skip past it
997 if((scan->attributes & LOCKBIT)
998 || !(scan->attributes & PURGEBITS) )
1000 printf(" [lock] "); //0000
1001 printf("len=%lu ", scan->length);
1003 startseg = lastscan->start + (word)lastscan->length;
1007 scan=scan->next; // look at next line
1011 if (mm->bombonerror)
1016 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
1017 printf("for stability reasons the program will shut down! wwww\n");
1018 printf(" endid=%u\n",(mm->endid));
1026 //==========================================================================
1029 ====================
1033 = Allocates an unlocked, unpurgable block
1035 ====================
1038 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1040 mmblocktype huge *scan,huge *last;
1045 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
1046 mm->mmrover = mm->mmhead;
1048 while(scan->useptr != baseptr && scan)
1056 printf("MM_FreePtr: Block not found!\n");
1060 last->next = scan->next;
1063 //MM_FreeBlock(scan, mm);
1065 //==========================================================================
1068 =====================
1072 = Sets the purge level for a block (locked blocks cannot be made purgable)
1074 =====================
1077 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1079 mmblocktype huge *start;
1081 start = mm->mmrover;
1085 if(mm->mmrover->useptr == baseptr)
1088 mm->mmrover = mm->mmrover->next;
1091 mm->mmrover = mm->mmhead;
1092 else if(mm->mmrover == start)
1094 printf("MM_SetPurge: Block not found!");
1100 mm->mmrover->attributes &= ~PURGEBITS;
1101 mm->mmrover->attributes |= purge;
1104 //==========================================================================
1107 =====================
1111 = Locks / unlocks the block
1113 =====================
1116 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1118 mmblocktype huge *start;
1120 start = mm->mmrover;
1124 if(mm->mmrover->useptr == baseptr)
1127 mm->mmrover = mm->mmrover->next;
1130 mm->mmrover = mm->mmhead;
1131 else if(mm->mmrover == start)
1133 printf("MM_SetLock: Block not found!");
1139 mm->mmrover->attributes &= ~LOCKBIT;
1140 mm->mmrover->attributes |= locked*LOCKBIT;
1143 //==========================================================================
1146 =====================
1150 = Throws out all purgable stuff and compresses movable blocks
1152 =====================
1155 void MM_SortMem(mminfo_t *mm)
1157 mmblocktype huge *scan,huge *last,huge *next;
1158 unsigned start,length,source,dest,oldborder;
1162 // lock down a currently playing sound
1164 /*++++ playing = SD_SoundPlaying ();
1170 playing += STARTPCSOUNDS;
1173 playing += STARTADLIBSOUNDS;
1176 MM_SetLock(&(memptr)audiosegs[playing],true);
1181 // oldborder = bordercolor;
1182 // VW_ColorBorder (15);
1189 last = NULL; // shut up compiler warning
1193 if(scan->attributes & LOCKBIT)
1196 // block is locked, so try to pile later blocks right after it
1198 start = scan->start + scan->length;
1202 if(scan->attributes & PURGEBITS)
1205 // throw out the purgable block
1209 //MM_FreeBlock(scan, mm);
1217 // push the non purgable block on top of the last moved block
1219 if(scan->start != start)
1221 length = scan->length;
1222 source = scan->start;
1224 while(length > 0xf00)
1226 movedata(source,0,dest,0,0xf00*16);
1231 movedata(source,0,dest,0,length*16);
1233 scan->start = start;
1234 *(unsigned *)scan->useptr = start;
1236 start = scan->start + scan->length;
1241 scan = scan->next; // go to next block
1244 mm->mmrover = mm->mmhead;
1249 // VW_ColorBorder (oldborder);
1252 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1255 //==========================================================================
1258 =====================
1262 =====================
1265 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
1267 mmblocktype huge *scan;
1272 byte scratch[160],scratch0[4096],str[16];
1274 //**** VW_SetDefaultColors();
1275 //**** VW_SetLineWidth(40);
1276 //++++mh temp = bufferofs;
1277 //++++mh bufferofs = 0;
1278 //**** VW_SetScreen (0,0);
1283 CA_OpenDebug (gvar);
1288 if(scan->attributes & PURGEBITS)
1289 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1291 strcpy(scratch0, AABLUE); // medium blue = non purgable
1292 if(scan->attributes & LOCKBIT)
1293 strcpy(scratch0, AARED); // red = locked
1294 if(scan->start<=end)
1296 write(gvar->handle.debughandle,"\nMM_ShowMemory: Memory block order currupted!\n",strlen("\nMM_ShowMemory: Memory block order currupted!\n"));
1297 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1300 end = scan->start+scan->length-1;
1301 //++++ chy = scan->start/320;
1302 //++++ chx = scan->start%320;
1303 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1304 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1306 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1308 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1309 for(chx=scan->start;chx>=(word)end;chx++)
1311 strcat(scratch0,&d);
1313 strcat (scratch0,AAWHITE); strcat(scratch0,&d);
1314 //++++ VW_Plot(scan->start,0,15);
1315 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1316 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1317 if(scan->next->start > end+1)
1319 strcat(scratch0,AABLACK);
1320 for(chx=end+1;chx>=(word)scan->next->start;chx++)
1322 strcat(scratch0,&d);
1325 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1327 // chx+=scan->next->start;
1328 // modexClearRegion(page, chx, chy, 4, 4, 2);
1330 //modexhlin(page, end+1,scan->next->start, chy, 0);
1331 /* y = scan->start/320;
1332 x = scan->start%320;
1333 VW_Hlin(x,x+end,y,color);
1335 if (scan->next && scan->next->start > end+1)
1337 //write(gvar->handle.showmemhandle,AABLACK,strlen(AABLACK));
1338 //for(chx=scan->start;chx>=(word)end;chx++)
1340 //write(gvar->handle.showmemhandle,"_",1);
1342 //write(gvar->handle.showmemhandle,"\n",strlen("\n"));
1343 strcat(scratch0,AABLACK); strcat(scratch0,0x10);
1345 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1346 write(gvar->handle.showmemhandle,scratch0,strlen(scratch0));
1347 strcpy(scratch,"Seg:");
1348 ultoa (scan->start,str,16);
1349 strcat (scratch,str);
1350 strcat (scratch,"\tSize:");
1351 ultoa ((dword)scan->length,str,10);
1352 strcat (scratch,str);
1353 strcat (scratch,"\tOwner:0x");
1354 owner = (unsigned)scan->useptr;
1355 ultoa (owner,str,16);
1356 strcat (scratch,str);
1357 strcat (scratch,"\n");
1358 write(gvar->handle.debughandle,scratch,strlen(scratch));
1359 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1361 //fprintf(stdout, "%s", scratch);
1366 CA_CloseDebug (gvar);
1369 //**** VW_SetLineWidth(64);
1370 //++++mh bufferofs = temp;
1373 //==========================================================================
1376 =====================
1380 =====================
1383 void MM_DumpData(mminfo_t *mm)
1385 mmblocktype huge *scan,huge *best;
1386 long lowest,oldlowest;
1392 dumpfile = fopen ("mmdump.16","w");
1394 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1407 owner = (word)scan->useptr;
1409 if (owner && owner<lowest && owner > oldlowest)
1418 if (lowest != 0xffff)
1420 if (best->attributes & PURGEBITS)
1424 if (best->attributes & LOCKBIT)
1428 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1429 ,(word)lowest,lock,purge,best->length);
1432 } while (lowest != 0xffff);
1435 printf("MMDUMP.16 created.\n");
1438 //==========================================================================
1442 ======================
1446 = Returns the total free space without purging
1448 ======================
1451 dword MM_UnusedMemory(mminfo_t *mm)
1454 mmblocktype huge *scan;
1461 free += scan->next->start - (scan->start + scan->length);
1469 //==========================================================================
1473 ======================
1477 = Returns the total free space with purging
1479 ======================
1482 dword MM_TotalFree(mminfo_t *mm)
1485 mmblocktype huge *scan;
1492 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1493 free += scan->length;
1494 free += scan->next->start - (scan->start + scan->length);
1502 //==========================================================================
1505 =====================
1509 =====================
1512 void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
1514 if(MML_CheckForEMS())
1516 printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
1517 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1518 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1519 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1521 if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
1522 printf("near=%lu\n", mmi->nearheap);
1523 printf("far=%lu\n", mmi->farheap);
1524 printf("EMSmem=%lu\n", mmi->EMSmem);
1525 printf("XMSmem=%lu\n", mmi->XMSmem);
1526 printf("mainmem=%lu\n", mmi->mainmem);
1527 printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1528 printf("TotalFree=%lu\n", MM_TotalFree(mm));
1529 //mmi->nearheap+mmi->farheap+
1530 printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem);//+);
1532 // printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
1533 // printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
1536 //==========================================================================
1539 =====================
1543 =====================
1546 void MM_EMSerr(byte *stri, byte err)
1548 //Returns a text string describing the error code in EMS.Error.
1552 strcat(stri, "successful");
1555 strcat(stri, "internal error");
1558 strcat(stri, "hardware malfunction");
1561 strcat(stri, "busy .. retry later");
1564 strcat(stri, "invalid handle");
1567 strcat(stri, "undefined function requested by application");
1570 strcat(stri, "no more handles available");
1573 strcat(stri, "error in save or restore of mapping context");
1576 strcat(stri, "insufficient memory pages in system");
1579 strcat(stri, "insufficient memory pages available");
1582 strcat(stri, "zero pages requested");
1585 strcat(stri, "invalid logical page number encountered");
1588 strcat(stri, "invalid physical page number encountered");
1591 strcat(stri, "page-mapping hardware state save area is full");
1594 strcat(stri, "save of mapping context failed");
1597 strcat(stri, "restore of mapping context failed");
1600 strcat(stri, "undefined subfunction");
1603 strcat(stri, "undefined attribute type");
1606 strcat(stri, "feature not supported");
1609 strcat(stri, "successful, but a portion of the source region has been overwritten");
1612 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1615 strcat(stri, "conventional and expanded memory regions overlap");
1618 strcat(stri, "offset within logical page exceeds size of logical page");
1621 strcat(stri, "region length exceeds 1 MB");
1624 strcat(stri, "source and destination EMS regions have same handle and overlap");
1627 strcat(stri, "memory source or destination type undefined");
1630 strcat(stri, "specified alternate map register or DMA register set not supported");
1633 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1636 strcat(stri, "alternate map register or DMA register sets not supported");
1639 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1642 strcat(stri, "dedicated DMA channels not supported");
1645 strcat(stri, "specified dedicated DMA channel not supported");
1648 strcat(stri, "no such handle name");
1651 strcat(stri, "a handle found had no name, or duplicate handle name");
1654 strcat(stri, "attempted to wrap around 1M conventional address space");
1657 strcat(stri, "source array corrupted");
1660 strcat(stri, "operating system denied access");
1663 strcat(stri, "undefined error");
1667 //==========================================================================
1670 =====================
1674 =====================
1677 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1679 mm->bombonerror = bomb;
1682 /*void MM_GetNewBlock(mminfo_t *mm)
1686 mm->mmnew=mm->mmfree;
1687 mm->mmfree=mm->mmfree->next;
1688 if(!(mm->mmnew=mm->mmfree))
1690 printf("MM_GETNEWBLOCK: No free blocks!\n");
1693 mm->mmfree=mm->mmfree->next;
1694 mm->endid++; //end of list
1697 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1702 mm->endid--; //end of list
1705 void MM_seguin(void)
1715 void MM_segude(void)
1723 pull data from far and put it into ds var