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(mminfo_t *mm)
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 mm->totalEMSpages=totalEMSpages;
249 mm->freeEMSpages=freeEMSpages;
250 mm->EMSpageframe=EMSpageframe;
251 mm->EMSpagesmapped=EMSpagesmapped;
252 mm->EMShandle=EMShandle;
259 ======================
263 =======================
266 void MML_ShutdownEMS(mminfo_t *mm)
268 boolean errorflag=false;
269 unsigned EMShandle=mm->EMShandle;
287 if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n"); //++++ add something
295 = Maps the 64k of EMS used by memory manager into the page frame
296 = for general use. This only needs to be called if you are keeping
297 = other things in EMS.
302 byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
307 boolean errorflag=false;
309 EMShandle=mm->EMShandle;
311 for (i=0;i<4/*MAPPAGES*/;i++)
315 mov bx,[i] // logical page
316 mov al,bl // physical page
317 mov dx,[EMShandle] // handle
340 strcpy(str,"MM_MapEMS: EMS error ");
346 mmi->EMSmem = (i)*0x4000lu;
347 //printf(" mmi->EMSmem=%lu\n", mmi->EMSmem);
351 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
353 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
355 //Maps up to 4 logical EMS pages to physical pages in the page frame, where:
356 //PhysicalStart = Physical page first logical page is mapped to
357 //LogicalStart = First logical page to map
358 //NumPages = Number of pages to map (1 to 4)
359 //Handle = EMS handle logical pages are allocated to
361 /*//Create a buffer containing the page information
362 // FOR x = 0 TO NumPages - 1
363 // MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
366 // Regs.ax = 0x5000 //Map the pages in the buffer
367 // Regs.cx = NumPages //to the pageframe
369 // Regs.ds = VARSEG(MapInfo$)
370 // Regs.si = SADD(MapInfo$)
371 // InterruptX 0x67, Regs, Regs
372 // EMS.Error = (Regs.ax AND 0xFF00&) \ 0x100 //Store the status code
378 boolean errorflag=false;
380 EMShandle=mm->EMShandle;
385 for (i=0;i<MAPPAGES;i++)
389 mov cx,[i] // logical page
390 mov al,bl // physical page
391 mov dx,[EMShandle] // handle
414 //strcpy(str,"MM_MapXEMS: EMS error 0x");
415 strcpy(str,"MM_MapXEMS: EMS error ");
419 //printf("%s%x\n",str, err);
420 //printf("FACK! %x\n", err);
424 mmi->EMSmem = (i)*0x4000lu;
428 //==========================================================================
431 ======================
435 = Check for XMM driver
437 =======================
440 boolean MML_CheckForXMS(mminfo_t *mm)
442 boolean errorflag=false;
447 int 0x2f // query status of installed diver
458 if(errorflag==true) return false;
464 ======================
468 = Try to allocate all upper memory block
470 =======================
473 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
481 mov [WORD PTR XMSaddr],bx
482 mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
487 mov dx,0xffff // try for largest block possible
488 //mov ax,dx // Set available Kbytes.
489 call [DWORD PTR XMSaddr]
493 cmp bl,0xb0 // error: smaller UMB is available
497 call [DWORD PTR XMSaddr] // DX holds largest available UMB
499 jz done // another error...
516 // printf("base=%u ", base); printf("size=%u\n", size);
517 MML_UseSpace(base,size, mm);
518 mmi->XMSmem += size*16;
519 mm->UMBbase[mm->numUMBs] = base;
521 if(mm->numUMBs < MAXUMBS)
527 ======================
531 ======================
534 void MML_ShutdownXMS(mminfo_t *mm)
539 for (i=0;i<mm->numUMBs;i++)
541 base = mm->UMBbase[i];
545 call [DWORD PTR XMSaddr]
550 //==========================================================================
553 ======================
557 = Marks a range of paragraphs as usable by the memory manager
558 = This is used to mark space for the near heap, far heap, ems page frame,
559 = and upper memory blocks
561 ======================
564 /*void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
566 //huge mmblocktype huge *scan,huge *last;
572 scan = last = mm->mmhead;
573 mm->mmrover = mm->mmhead; // reset rover to start of memory
576 // search for the block that contains the range of segments
578 while(scan->start+scan->length < segstart)
585 // take the given range out of the block
587 oldend = scan->start + scan->length;
588 extra = oldend - (segstart+seglength);
593 if(segm>1)/// || extra>=0x10000lu)
598 //MML_UseSpace(segstart, seglength, mm);
600 printf("MML_UseSpace: Segment spans two blocks!\n");
602 printf("segm=%u ", segm);
603 printf("ex=%lu ", extra);
604 printf("old=%u ", oldend);
605 printf("start+seglen=%lu\n", segstart+seglength);
606 printf("segsta=%x ", segstart);
607 printf("len=%lu ", scan->length);
608 printf("seglen=%lu ", seglength);
609 printf("segmlen=%lu\n", segmlen);
611 //++++todo: linked list of segment!
612 if(segstart == scan->start)
614 last->next = scan->next; // unlink block
619 scan->length = segstart-scan->start; // shorten block
624 mm->mmnew->useptr = NULL;
626 mm->mmnew->next = scan->next;
627 scan->next = mm->mmnew;
628 mm->mmnew->start = segstart+seglength;
629 mm->mmnew->length = extra;
630 mm->mmnew->attributes = LOCKBIT;
631 }//else if(segm>0) goto segu;
634 void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
636 mmblocktype far *scan,far *last;
641 scan = last = mm->mmhead;
642 mm->mmrover = mm->mmhead; // reset rover to start of memory
645 // search for the block that contains the range of segments
647 while (scan->start+scan->length < segstart)
654 // find out how many blocks it spans!
656 /*for(;seglength>=0x10000;seglength-=0xFFFF)
658 //printf(" seglen=%lu\n", segmlen);
663 // take the given range out of the block
665 oldend = scan->start + scan->length;
666 extra = oldend - (segstart+((unsigned)seglength));
669 printf("========================================\n");
670 printf("start=%x ", scan->start);
671 printf("old=%u ", oldend);
672 printf("start+seglen=%lu\n", segstart+seglength);
673 printf("segsta=%x ", segstart);
674 printf("len=%lu ", scan->length);
675 printf("seglen=%lu ", seglength);
677 printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
678 printf("========================================\n");
682 if (segstart == scan->start)
684 last->next = scan->next; // unlink block
689 scan->length = segstart-scan->start; // shorten block
694 mm->mmnew->useptr = NULL;
696 mm->mmnew->next = scan->next;
697 scan->next = mm->mmnew;
698 mm->mmnew->start = segstart+seglength;
699 mm->mmnew->length = extra;
700 mm->mmnew->attributes = LOCKBIT;
705 //==========================================================================
712 = We are out of blocks, so free a purgable block
717 void MML_ClearBlock(mminfo_t *mm)
719 //huge mmblocktype huge *scan,huge *last;
720 mmblocktype far *scan,far *last;
722 scan = mm->mmhead->next;
726 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
728 MM_FreePtr(scan->useptr, mm);
734 printf("MM_ClearBlock: No purgable blocks!\n");
738 //==========================================================================
745 = Grabs all space from turbo with malloc/farmalloc
746 = Allocates bufferseg misc buffer
751 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
754 //dword length,seglength;
755 dword length; word seglength;
756 //huge void huge *start;
758 word segstart;//,endfree;
764 mm->mmstarted = true;
765 mm->bombonerror = true;
768 // set up the linked list (everything in the free list;
771 mm->mmfree = &(mm->mmblocks[0]);
772 for(i=0;i<MAXBLOCKS-1;i++)
774 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
776 mm->mmblocks[i].next = NULL;
779 // locked block of all memory until we punch out free space
782 mm->mmhead = mm->mmnew; // this will allways be the first node
783 mm->mmnew->start = 0;
784 mm->mmnew->length = 0xffff;
785 mm->mmnew->attributes = LOCKBIT;
786 mm->mmnew->next = NULL;
787 //mm->mmnew->useptr = peeonself;
788 mm->mmrover = mm->mmhead;
790 //printf(" %x\n", peeonself);
791 //printf(" %x\n", *peeonself);
793 // get all available near conventional memory segments
797 length=(dword)_memavl();//(dword)GetFreeSize();
798 //huge start = (void huge *)(mm->nearheap = _nmalloc(length));
799 start = (void far *)(mm->nearheap = _nmalloc(length));
803 //huge start = (void huge *)(mm->nearheap = malloc(length));
804 start = (void far *)(mm->nearheap = malloc(length));
806 length -= 16-(FP_OFF(start)&15);
807 length -= SAVENEARHEAP;
808 seglength = length / 16; // now in paragraphs
809 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
810 MML_UseSpace(segstart,seglength, mm);
811 mmi->nearheap = length;
812 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
815 // get all available far conventional memory segments
817 //printf("_FARCORELEFT %lu\n", _FCORELEFT);
822 printf("farcoreleft() %lu\n", farcoreleft());
823 printf("(farcoreleft()+32)-_FCORELEFT %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
825 length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
826 start = mm->farheap = _fmalloc(length);
827 //start = mm->farheap = halloc(length, 1);
828 length -= 16-(FP_OFF(start)&15);
829 length -= SAVEFARHEAP;
830 seglength = length / 16; // now in paragraphs
831 segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
832 MML_UseSpace(segstart,seglength, mm);
833 mmi->farheap = length;
834 //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length);
836 mmi->mainmem = mmi->nearheap + mmi->farheap;
839 // detect EMS and allocate up to 64K at page frame
842 //goto emsskip; //0000
859 ,ParmStringsexmm) == 0)
860 goto emsskip; // param NOEMS
862 if(MML_CheckForEMS())
864 MML_SetupEMS(mm); // allocate space
865 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
866 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
867 //if(mm->EMSVer<0x40)
868 MM_MapEMS(mm, mmi); // map in used pages
870 //MM_MapXEMS(mm, mmi); // map in used pages
874 // detect XMS and get upper memory blocks
895 ,ParmStringsexmm) == 0)
896 goto xmsskip; // param NOXMS
898 if(MML_CheckForXMS(mm))
900 MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible
904 // allocate the misc buffer
907 mm->mmrover = mm->mmhead; // start looking for space after low block
909 MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
912 //==========================================================================
919 = Frees all conventional, EMS, and XMS allocated
924 void MM_Shutdown(mminfo_t *mm)
929 _ffree(mm->farheap);// printf(" far freed\n");
931 _nfree(mm->nearheap);// printf(" near freed\n");
934 free(mm->nearheap);// printf(" near freed\n");
936 if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf(" EMS freed\n"); }
937 if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf(" XMS freed\n"); }
940 //==========================================================================
947 = Allocates an unlocked, unpurgable block
952 void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
954 //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
955 mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next;
957 unsigned needed,startseg;
959 needed = (size+15)/16; // convert size from bytes to paragraphs
961 GETNEWBLOCK; // fill in start and next after a spot is found
962 mm->mmnew->length = needed;
963 mm->mmnew->useptr = baseptr;
964 //if(mm->mmnew->useptr==NULL){
966 printf("baseptr=%04x ", baseptr); printf("useptr=%04x\n", mm->mmnew->useptr);
967 printf("*baseptr=%04x ", *baseptr); printf("*useptr=%04x\n", *(mm->mmnew->useptr));
968 printf("*baseptr=%Fp ", *baseptr); printf("*useptr=%Fp\n", *(mm->mmnew->useptr));
971 mm->mmnew->attributes = BASEATTRIBUTES;
974 for (search = 0; search<3; search++)
977 // first search: try to allocate right after the rover, then on up
978 // second search: search from the head pointer up to the rover
979 // third search: compress memory, then scan from start
980 if (search == 1 && mm->mmrover == mm->mmhead)
986 lastscan = mm->mmrover;
987 scan = mm->mmrover->next;
991 lastscan = mm->mmhead;
992 scan = mm->mmhead->next;
993 endscan = mm->mmrover;
997 lastscan = mm->mmhead;
998 scan = mm->mmhead->next;
1003 startseg = lastscan->start + lastscan->length;
1005 while (scan != endscan)
1007 if (scan->start - startseg >= needed)
1010 // got enough space between the end of lastscan and
1011 // the start of scan, so throw out anything in the middle
1012 // and allocate the new block
1014 purge = lastscan->next;
1015 lastscan->next = mm->mmnew;
1016 mm->mmnew->start = *(unsigned *)baseptr = startseg;
1017 mm->mmnew->next = scan;
1018 while ( purge != scan)
1019 { // free the purgable block
1022 purge = next; // purge another if not at scan
1024 mm->mmrover = mm->mmnew;
1025 return; // good allocation!
1029 // if this block is purge level zero or locked, skip past it
1031 if ( (scan->attributes & LOCKBIT)
1032 || !(scan->attributes & PURGEBITS) )
1035 startseg = lastscan->start + lastscan->length;
1039 scan=scan->next; // look at next line
1043 if (mm->bombonerror)
1048 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
1049 printf("for stability reasons the program will shut down! wwww\n");
1057 //==========================================================================
1060 ====================
1064 = Allocates an unlocked, unpurgable block
1066 ====================
1069 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1071 //huge mmblocktype huge *scan,huge *last;
1072 mmblocktype far *scan,far *last;
1077 if(baseptr == mm->mmrover->useptr) // removed the last allocated block
1078 mm->mmrover = mm->mmhead;
1080 while(scan->useptr != baseptr && scan)
1088 printf("MM_FreePtr: Block not found!\n");
1092 last->next = scan->next;
1096 //==========================================================================
1099 =====================
1103 = Sets the purge level for a block (locked blocks cannot be made purgable)
1105 =====================
1108 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1110 //huge mmblocktype huge *start;
1111 mmblocktype far *start;
1113 start = mm->mmrover;
1117 if(mm->mmrover->useptr == baseptr)
1120 mm->mmrover = mm->mmrover->next;
1123 mm->mmrover = mm->mmhead;
1124 else if(mm->mmrover == start)
1126 printf("MM_SetPurge: Block not found!");
1132 mm->mmrover->attributes &= ~PURGEBITS;
1133 mm->mmrover->attributes |= purge;
1136 //==========================================================================
1139 =====================
1143 = Locks / unlocks the block
1145 =====================
1148 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1150 //huge mmblocktype huge *start;
1151 mmblocktype far *start;
1153 start = mm->mmrover;
1157 if(mm->mmrover->useptr == baseptr)
1160 mm->mmrover = mm->mmrover->next;
1163 mm->mmrover = mm->mmhead;
1164 else if(mm->mmrover == start)
1166 printf("MM_SetLock: Block not found!");
1172 mm->mmrover->attributes &= ~LOCKBIT;
1173 mm->mmrover->attributes |= locked*LOCKBIT;
1176 //==========================================================================
1179 =====================
1183 = Throws out all purgable stuff and compresses movable blocks
1185 =====================
1188 void MM_SortMem(mminfo_t *mm)
1190 //huge mmblocktype huge *scan,huge *last,huge *next;
1191 mmblocktype far *scan,far *last,far *next;
1192 unsigned start,length,source,dest,oldborder;
1196 // lock down a currently playing sound
1198 /*++++ playing = SD_SoundPlaying ();
1204 playing += STARTPCSOUNDS;
1207 playing += STARTADLIBSOUNDS;
1210 MM_SetLock(&(memptr)audiosegs[playing],true);
1215 // oldborder = bordercolor;
1216 // VW_ColorBorder (15);
1223 last = NULL; // shut up compiler warning
1227 if(scan->attributes & LOCKBIT)
1230 // block is locked, so try to pile later blocks right after it
1232 start = scan->start + scan->length;
1236 if(scan->attributes & PURGEBITS)
1239 // throw out the purgable block
1243 //MM_FreeBlock(scan, mm);
1251 // push the non purgable block on top of the last moved block
1253 if(scan->start != start)
1255 length = scan->length;
1256 source = scan->start;
1258 while(length > 0xf00)
1260 movedata(source,0,dest,0,0xf00*16);
1265 movedata(source,0,dest,0,length*16);
1267 scan->start = start;
1268 *(unsigned *)scan->useptr = start;
1270 start = scan->start + scan->length;
1275 scan = scan->next; // go to next block
1278 mm->mmrover = mm->mmhead;
1283 // VW_ColorBorder (oldborder);
1286 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1289 //==========================================================================
1292 =====================
1296 =====================
1299 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
1301 //huge mmblocktype huge *scan;
1302 mmblocktype far *scan;
1308 byte scratch[160],scratch0[4096],scratch1[160],str[16];
1310 //**** VW_SetDefaultColors();
1311 //**** VW_SetLineWidth(40);
1312 //++++mh temp = bufferofs;
1313 //++++mh bufferofs = 0;
1314 //**** VW_SetScreen (0,0);
1318 CA_OpenDebug (gvar);
1322 strcpy(scratch, AARESET);
1323 if(scan->attributes & PURGEBITS)
1324 strcpy(scratch0, AAMAGENTA); // dark purple = purgable
1326 strcpy(scratch0, AABLUE); // medium blue = non purgable
1327 if(scan->attributes & LOCKBIT)
1328 strcpy(scratch0, AARED); // red = locked
1329 if(scan->start<=end)
1331 printf("\nend==%d\n\n", end);
1332 strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1333 strcat(scratch, "End's Size: ");
1335 strcat (scratch,str);
1336 strcat(scratch, "\nscan->start's Size: ");
1337 ultoa (scan->start,str,10);
1338 strcat (scratch,str);
1339 write(gvar->handle.debughandle,scratch,strlen(scratch));
1340 //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1343 end = scan->start+(scan->length)-1;
1344 //++++ chy = scan->start/320;
1345 //++++ chx = scan->start%320;
1346 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1347 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1349 //++++ modexClearRegion(page, chx, chy, 4, 4, color);
1351 //++++ VW_Hlin(scan->start,(unsigned)end,0,color);
1352 for(w=(scan->start)/80;w<=end/80;w++)
1354 //printf("+ %u %lu\n", w, scan->length);
1355 strcat(scratch0, "+");
1357 //++==++==optional strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
1358 //++++ VW_Plot(scan->start,0,15);
1359 //++++ modexClearRegion(page, chx, chy, 4, 4, 15);
1360 //++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
1362 //wwww=(dword)(scan->next->start)-(dword)scan->start;
1363 //wwww=(dword)scan->start+(dword)(scan->next->start);
1364 if (scan->next && scan->next->start >= end+1)
1366 strcat(scratch0, AARESET);
1367 //++==++==optional strcat(scratch0, "\n");
1368 strcat(scratch0,AAGREEN);
1369 for(w=(end+1)/80;w<=((scan->next->start-scan->start)/80);w++)
1370 //for(w=(wwww)/80;w<=((end+1)/80);w++)
1371 //for(w=(end+1)/80;w<=((wwww)/80);w++)
1373 //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
1374 strcat(scratch0,"0");
1376 //printf("==================\n");
1377 //printf("w=%u wwww=%lu start=%04x next=%04x end=%lu\n", w/80, wwww/80, scan->start, (scan->next->start), end+1);
1378 //printf("==================\n");
1379 strcat(scratch0, "\n");
1381 }/*else {//if(scan->next->start <= scan->start){
1382 scan->next->start=scan->start+0x1000;
1383 wwww=(dword)(scan->next->start)-(dword)scan->start;
1384 strcat(scratch0, AARESET);
1385 strcat(scratch0, "\n");
1386 strcat(scratch0,AAGREEN);
1387 for(w=(end+1);w<=(0x1000/80);w++)
1389 //printf("0 %x %x %u\n", scan->start, w);
1390 strcat(scratch0,"0");
1392 printf("================\n");
1393 printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
1394 printf("================\n");
1397 strcat(scratch0, AARESET);
1398 //strcat(scratch0,"\n");
1399 //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1401 // chx+=scan->next->start;
1402 // modexClearRegion(page, chx, chy, 4, 4, 2);
1404 //modexhlin(page, end+1,scan->next->start, chy, 0);
1405 /* y = scan->start/320;
1406 x = scan->start%320;
1407 VW_Hlin(x,x+end,y,color);
1409 //++++ VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1410 strcat(scratch,"Seg:");
1411 ultoa (scan->start,str,16);
1412 strcat (scratch,str);
1413 strcat (scratch,"\tSize:");
1414 ultoa ((unsigned)scan->length,str,10);
1415 strcat (scratch,str);
1416 strcat (scratch,"\tOwner:0x");
1417 owner = (unsigned)scan->useptr;
1418 ultoa (owner,str,16);
1419 strcat (scratch,str);
1420 strcat (scratch,"\n");
1421 write(gvar->handle.debughandle,scratch,strlen(scratch));
1422 write(gvar->handle.debughandle,scratch0,strlen(scratch0));
1423 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1425 //fprintf(stdout, "%s", scratch);
1429 /*strcpy(scratch1, AARESET);
1430 strcat(scratch1, "========================================\n");
1431 strcat(scratch1, "near= ");
1432 ultoa (*(mm->nearheap),str,10);
1433 strcat (scratch1,str);
1434 strcat(scratch1, " far= ");
1435 ultoa (*(mm->farheap),str,10);
1436 strcat (scratch1,str);
1437 strcat(scratch1, "\n");
1438 //strcat(scratch1, "&near= %Fp ", &(mm->nearheap));
1439 //strcat(scratch1, "&far= %Fp", &(mm->farheap));
1440 //strcat(scratch1, "\n");
1441 strcat(scratch1, "========================================\n");
1442 write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/
1445 CA_CloseDebug (gvar);
1448 //**** VW_SetLineWidth(64);
1449 //++++mh bufferofs = temp;
1452 //==========================================================================
1455 =====================
1459 =====================
1462 void MM_DumpData(mminfo_t *mm)
1464 //huge mmblocktype huge *scan,huge *best;
1465 mmblocktype far *scan,far *best;
1466 long lowest,oldlowest;
1473 dumpfile = fopen ("mmdump.16b","w");
1476 dumpfile = fopen ("mmdump.16w","w");
1479 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1492 owner = (unsigned)scan->useptr;
1494 if (owner && owner<lowest && owner > oldlowest)
1503 if (lowest != 0xffff)
1505 if (best->attributes & PURGEBITS)
1509 if (best->attributes & LOCKBIT)
1513 fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1514 ,(unsigned)lowest,lock,purge,best->length);
1517 } while (lowest != 0xffff);
1520 printf("MMDUMP.16 created.\n");
1523 //==========================================================================
1527 ======================
1531 = Returns the total free space without purging
1533 ======================
1536 dword MM_UnusedMemory(mminfo_t *mm)
1539 //huge mmblocktype huge *scan;
1540 mmblocktype far *scan;
1547 free += scan->next->start - (scan->start + scan->length);
1555 //==========================================================================
1559 ======================
1563 = Returns the total free space with purging
1565 ======================
1568 dword MM_TotalFree(mminfo_t *mm)
1571 //huge mmblocktype huge *scan;
1572 mmblocktype far *scan;
1579 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1580 free += scan->length;
1581 free += scan->next->start - (scan->start + scan->length);
1589 //==========================================================================
1592 =====================
1596 =====================
1599 void MM_Report(global_game_variables_t *gvar)
1601 printf("========================================\n");
1602 printf(" MM_Report\n");
1603 printf("========================================\n");
1604 if(MML_CheckForEMS())
1606 printf(" LIMEMS\n");
1607 printf(" EMM v%x.%x available\n", gvar->mm.EMSVer>>4,gvar->mm.EMSVer&0x0F);
1608 printf(" totalEMSpages: %u ", gvar->mm.totalEMSpages); printf("freeEMSpages: %u\n", gvar->mm.freeEMSpages);
1609 printf(" EMSpageframe: %x\n", gvar->mm.EMSpageframe);
1611 if(MML_CheckForXMS(&(gvar->mm)))
1614 printf(" XMSaddr: %X\n", *XMSaddr);
1616 printf("near: %lu ", gvar->mmi.nearheap); printf("far: %lu\n", gvar->mmi.farheap); if(MML_CheckForEMS())
1617 printf("EMSmem: %lu ", gvar->mmi.EMSmem); if(MML_CheckForXMS(&(gvar->mm))) printf("XMSmem: %lu", gvar->mmi.XMSmem); printf("\n");
1618 //printf("mainmem: %lu\n", gvar->mmi.mainmem);
1619 printf("Total convmem: %lu ", gvar->mmi.mainmem); printf("TotalFree: %lu ", MM_TotalFree(&(gvar->mm))); printf("TotalUsed: %lu\n", gvar->mmi.mainmem+gvar->mmi.EMSmem+gvar->mmi.XMSmem+gvar->mmi.XMSmem);
1620 printf(" UnusedMemory: %lu\n", MM_UnusedMemory(&(gvar->mm)));
1623 //==========================================================================
1626 =====================
1630 =====================
1633 void MM_EMSerr(byte *stri, byte err)
1635 //Returns a text string describing the error code in EMS.Error.
1639 strcat(stri, "successful");
1642 strcat(stri, "internal error");
1645 strcat(stri, "hardware malfunction");
1648 strcat(stri, "busy .. retry later");
1651 strcat(stri, "invalid handle");
1654 strcat(stri, "undefined function requested by application");
1657 strcat(stri, "no more handles available");
1660 strcat(stri, "error in save or restore of mapping context");
1663 strcat(stri, "insufficient memory pages in system");
1666 strcat(stri, "insufficient memory pages available");
1669 strcat(stri, "zero pages requested");
1672 strcat(stri, "invalid logical page number encountered");
1675 strcat(stri, "invalid physical page number encountered");
1678 strcat(stri, "page-mapping hardware state save area is full");
1681 strcat(stri, "save of mapping context failed");
1684 strcat(stri, "restore of mapping context failed");
1687 strcat(stri, "undefined subfunction");
1690 strcat(stri, "undefined attribute type");
1693 strcat(stri, "feature not supported");
1696 strcat(stri, "successful, but a portion of the source region has been overwritten");
1699 strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1702 strcat(stri, "conventional and expanded memory regions overlap");
1705 strcat(stri, "offset within logical page exceeds size of logical page");
1708 strcat(stri, "region length exceeds 1 MB");
1711 strcat(stri, "source and destination EMS regions have same handle and overlap");
1714 strcat(stri, "memory source or destination type undefined");
1717 strcat(stri, "specified alternate map register or DMA register set not supported");
1720 strcat(stri, "all alternate map register or DMA register sets currently allocated");
1723 strcat(stri, "alternate map register or DMA register sets not supported");
1726 strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1729 strcat(stri, "dedicated DMA channels not supported");
1732 strcat(stri, "specified dedicated DMA channel not supported");
1735 strcat(stri, "no such handle name");
1738 strcat(stri, "a handle found had no name, or duplicate handle name");
1741 strcat(stri, "attempted to wrap around 1M conventional address space");
1744 strcat(stri, "source array corrupted");
1747 strcat(stri, "operating system denied access");
1750 strcat(stri, "undefined error");
1754 //==========================================================================
1757 =====================
1761 =====================
1764 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1766 mm->bombonerror = bomb;
1769 /*void MM_GetNewBlock(mminfo_t *mm)
1773 mm->mmnew=mm->mmfree;
1774 mm->mmfree=mm->mmfree->next;
1775 if(!(mm->mmnew=mm->mmfree))
1777 printf("MM_GETNEWBLOCK: No free blocks!\n");
1780 mm->mmfree=mm->mmfree->next;
1783 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1790 /*void MM_seguin(void)
1800 void MM_segude(void)
1808 pull data from far and put it into ds var