]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_mm.c
added info display image of wolf 3d mem which should help me think about pm
[16.git] / src / lib / 16_mm.c
1 /* Catacomb Apocalypse Source Code
2  * Copyright (C) 1993-2014 Flat Rock Software
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 // NEWMM.C
20
21 /*
22 =============================================================================
23
24                         ID software memory manager
25                         --------------------------
26
27 Primary coder: John Carmack
28
29 RELIES ON
30 ---------
31 Quit (char *error) function
32
33
34 WORK TO DO
35 ----------
36 MM_SizePtr to change the size of a given pointer
37
38 Multiple purge levels utilized
39
40 EMS / XMS unmanaged routines
41
42 =============================================================================
43 */
44 /*
45
46 Open Watcom port by sparky4
47
48 */
49 #include "src/lib/16_mm.h"
50 #include "src/lib/16_ca.h"
51 #pragma hdrstop
52
53 #pragma warn -pro
54 #pragma warn -use
55
56 /*
57 =============================================================================
58
59                                                  GLOBAL VARIABLES
60
61 =============================================================================
62 */
63
64 void            (* beforesort) (void);
65 void            (* aftersort) (void);
66 void            (* XMSaddr) (void);             // far pointer to XMS driver
67
68 /*
69 =============================================================================
70
71                                                  LOCAL VARIABLES
72
73 =============================================================================
74 */
75
76 static  char *ParmStringsexmm[] = {"noems","noxms",""};
77
78 /*
79 ======================
80 =
81 = MML_CheckForEMS
82 =
83 = Routine from p36 of Extending DOS
84 =
85 =======================
86 */
87
88 boolean MML_CheckForEMS(void)
89 {
90         boolean emmcfems;
91         static char     emmname[] = "EMMXXXX0"; //fix by andrius4669
92         __asm {
93                 mov     dx,OFFSET emmname       //fix by andrius4669
94                 mov     ax,0x3d00
95                 int     0x21            // try to open EMMXXXX0 device
96                 jc      error
97
98                 mov     bx,ax
99                 mov     ax,0x4400
100
101                 int     0x21            // get device info
102                 jc      error
103
104                 and     dx,0x80
105                 jz      error
106
107                 mov     ax,0x4407
108
109                 int     0x21            // get status
110                 jc      error
111                 or      al,al
112                 jz      error
113
114                 mov     ah,0x3e
115                 int     0x21            // close handle
116                 jc      error
117                 //
118                 // EMS is good
119                 //
120                 mov     emmcfems,1
121                 jmp End
122 #ifdef __BORLANDC__
123         }
124 #endif
125                 error:
126 #ifdef __BORLANDC__
127         __asm {
128 #endif
129                 //
130                 // EMS is bad
131                 //
132                 mov     emmcfems,0
133 #ifdef __BORLANDC__
134         }
135 #endif
136                 End:
137 #ifdef __WATCOMC__
138         }
139 #endif
140         return(emmcfems);
141 }
142
143
144 /*
145 ======================
146 =
147 = MML_SetupEMS
148 =
149 =======================
150 */
151
152 byte MML_SetupEMS(mminfo_t *mm)
153 {
154         byte    str[160];
155         byte    err;
156         boolean errorflag=false;
157
158         unsigned int EMSVer = 0;
159         //byte  EMS_status;
160         unsigned        totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
161         totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
162
163         __asm {
164                 mov     ah,EMS_STATUS
165                 int     EMS_INT                                         // make sure EMS hardware is present
166                 or      ah,ah
167                 //mov   [EMS_status],ah
168                 jnz     error
169
170                 mov     ah,EMS_VERSION
171                 int     EMS_INT
172                 or      ah,ah
173                 jnz     error
174                 mov     [EMSVer],ax                             //      set EMSVer
175                 cmp     al,0x32                                         // only work on ems 3.2 or greater
176                 jb      error
177
178                 mov     ah,EMS_GETFRAME
179                 int     EMS_INT                                         // find the page frame address
180                 or      ah,ah
181                 jnz     error
182                 mov     [EMSpageframe],bx
183
184                 mov     ah,EMS_GETPAGES
185                 int     EMS_INT                                         // find out how much EMS is there
186                 or      ah,ah
187                 jnz     error
188                 mov     [totalEMSpages],dx
189                 mov     [freeEMSpages],bx
190                 or      bx,bx
191                 jz      noEMS                                           // no EMS at all to allocate
192                                                                                         //EXPAND DONG!!!!
193                 cmp     [EMSVer],0x40
194                 jb      low
195                 cmp     bx,[freeEMSpages]
196                 jle     getpages
197                 mov     bx,[freeEMSpages]
198                 jmp     getpages
199 #ifdef __BORLANDC__
200         }
201 #endif
202         low:
203 #ifdef __BORLANDC__
204         __asm {
205 #endif
206                 cmp     bx,4
207                 jle     getpages                                        // there is only 1,2,3,or 4 pages
208                 mov     bx,4                                            // we can't use more than 4 pages
209 #ifdef __BORLANDC__
210         }
211 #endif
212         getpages:
213 #ifdef __BORLANDC__
214         __asm {
215 #endif
216                 mov     [EMSpagesmapped],bx
217                 mov     ah,EMS_ALLOCPAGES                       // allocate up to 64k of EMS
218                 int     EMS_INT
219                 or      ah,ah
220                 jnz     error
221                 mov     [EMShandle],dx
222                 jmp End
223 #ifdef __BORLANDC__
224         }
225 #endif
226         error:
227 #ifdef __BORLANDC__
228         __asm {
229 #endif
230                 mov     err,ah
231                 mov     errorflag,1
232                 jmp End
233 #ifdef __BORLANDC__
234         }
235 #endif
236 noEMS:
237 End:
238 #ifdef __WATCOMC__
239         }
240 #endif
241         if(errorflag==true)
242         {
243                 strcpy(str,"MM_SetupEMS: EMS error ");
244                 MM_EMSerr(str, err);
245                 printf("%s\n",str);
246                 return err;
247         }
248         mm->totalEMSpages=totalEMSpages;
249         mm->freeEMSpages=freeEMSpages;
250         mm->EMSpageframe=EMSpageframe;
251         mm->EMSpagesmapped=EMSpagesmapped;
252         mm->EMShandle=EMShandle;
253         mm->EMSVer=EMSVer;
254         return 0;
255 }
256
257
258 /*
259 ======================
260 =
261 = MML_ShutdownEMS
262 =
263 =======================
264 */
265
266 void MML_ShutdownEMS(mminfo_t *mm)
267 {
268         boolean errorflag=false;
269         unsigned EMShandle=mm->EMShandle;
270
271         if(!EMShandle)
272                 return;
273         __asm {
274                 mov     ah,EMS_FREEPAGES
275                 mov     dx,[EMShandle]
276                 int     EMS_INT
277                 or      ah,ah
278                 jz      ok
279                 mov     errorflag,1
280 #ifdef __BORLANDC__
281         }
282 #endif
283                 ok:
284 #ifdef __WATCOMC__
285         }
286 #endif
287         if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n");    //++++ add something
288 }
289
290 /*
291 ====================
292 =
293 = MM_MapEMS
294 =
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.
298 =
299 ====================
300 */
301
302 byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
303 {
304         byte    str[160];
305         unsigned        EMShandle;
306         byte err;
307         boolean errorflag=false;
308         int     i;
309         EMShandle=mm->EMShandle;
310
311         for (i=0;i<4/*MAPPAGES*/;i++)
312         {
313                 __asm {
314                         mov     ah,EMS_MAPPAGE
315                         mov     bx,[i]                  // logical page
316                         mov     al,bl                   // physical page
317                         mov     dx,[EMShandle]  // handle
318                         int     EMS_INT
319                         or      ah,ah
320                         jnz     error
321                         jmp End
322 #ifdef __BORLANDC__
323                 }
324 #endif
325                         error:
326 #ifdef __BORLANDC__
327                 __asm {
328 #endif
329                         mov     err,ah
330                         mov     errorflag,1
331 #ifdef __BORLANDC__
332                 }
333 #endif
334                         End:
335 #ifdef __WATCOMC__
336                 }
337 #endif
338                 if(errorflag==true)
339                 {
340                         strcpy(str,"MM_MapEMS: EMS error ");
341                         MM_EMSerr(str, err);
342                         printf("%s\n",str);
343                         return err;
344                 }
345         }
346         mmi->EMSmem = (i)*0x4000lu;
347         //printf("              mmi->EMSmem=%lu\n", mmi->EMSmem);
348         return 0;
349 }
350
351 byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
352 {
353 //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)
354
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
360
361   /*//Create a buffer containing the page information
362 //  FOR x = 0 TO NumPages - 1
363 //    MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)
364 //  NEXT*/
365
366 //  Regs.ax = 0x5000                           //Map the pages in the buffer
367 //  Regs.cx = NumPages                         //to the pageframe
368 //  Regs.dx = Handle
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
373
374 //END SUB
375         byte    str[160];
376         byte err;
377         word    EMShandle;
378         boolean errorflag=false;
379         int     i;
380         EMShandle=mm->EMShandle;
381
382         if(mm->EMSVer<0x40)
383                 return 5;
384
385         for (i=0;i<MAPPAGES;i++)
386         {
387                 __asm {
388                         mov     ah,EMS_MAPXPAGE
389                         mov     cx,[i]                  // logical page
390                         mov     al,bl                   // physical page
391                         mov     dx,[EMShandle]  // handle
392                         int     EMS_INT
393                         or      ah,ah
394                         jnz     error
395                         jmp End
396 #ifdef __BORLANDC__
397                 }
398 #endif
399                         error:
400 #ifdef __BORLANDC__
401                 __asm {
402 #endif
403                         mov     err,ah
404                         mov     errorflag,1
405 #ifdef __BORLANDC__
406                 }
407 #endif
408                         End:
409 #ifdef __WATCOMC__
410                 }
411 #endif
412                 if(errorflag==true)
413                 {
414                         //strcpy(str,"MM_MapXEMS: EMS error 0x");
415                         strcpy(str,"MM_MapXEMS: EMS error ");
416                         //itoa(err,str2,16);
417                         MM_EMSerr(str, err);
418                         printf("%s\n",str);
419                         //printf("%s%x\n",str, err);
420                         //printf("FACK! %x\n", err);
421                         return err;
422                 }
423         }
424         mmi->EMSmem = (i)*0x4000lu;
425         return 0;
426 }
427
428 //==========================================================================
429
430 /*
431 ======================
432 =
433 = MML_CheckForXMS
434 =
435 = Check for XMM driver
436 =
437 =======================
438 */
439
440 boolean MML_CheckForXMS(mminfo_t *mm)
441 {
442         boolean errorflag=false;
443         mm->numUMBs = 0;
444
445         __asm {
446                 mov     ax,0x4300
447                 int     0x2f                            // query status of installed diver
448                 cmp     al,0x80
449                 je      good
450                 mov     errorflag,1
451 #ifdef __BORLANDC__
452         }
453 #endif
454                 good:
455 #ifdef __WATCOMC__
456         }
457 #endif
458         if(errorflag==true) return false;
459         else return true;
460 }
461
462
463 /*
464 ======================
465 =
466 = MML_SetupXMS
467 =
468 = Try to allocate all upper memory block
469 =
470 =======================
471 */
472
473 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
474 {
475         word    base,size;
476
477
478         __asm {
479                 mov     ax,0x4310
480                 int     0x2f
481                 mov     [WORD PTR XMSaddr],bx
482                 mov     [WORD PTR XMSaddr+2],es         // function pointer to XMS driver
483         }
484 getmemory:
485         __asm {
486                 mov     ah,XMS_ALLOCUMB
487                 mov     dx,0xffff                                       // try for largest block possible
488                 //mov     ax,dx                                         // Set available Kbytes.
489                 call    [DWORD PTR XMSaddr]
490                 or      ax,ax
491                 jnz     gotone
492
493                 cmp     bl,0xb0                                         // error: smaller UMB is available
494                 jne     done;
495
496                 mov     ah,XMS_ALLOCUMB
497                 call    [DWORD PTR XMSaddr]             // DX holds largest available UMB
498                 or      ax,ax
499                 jz      done                                            // another error...
500 #ifdef __BORLANDC__
501         }
502 #endif
503                 gotone:
504 #ifdef __BORLANDC__
505         __asm {
506 #endif
507                 mov     [base],bx
508                 mov     [size],dx
509 #ifdef __BORLANDC__
510         }
511 #endif
512                 done:
513 #ifdef __WATCOMC__
514         }
515 #endif
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;
520         mm->numUMBs++;
521         if(mm->numUMBs < MAXUMBS)
522                 goto getmemory;
523 }
524
525
526 /*
527 ======================
528 =
529 = MML_ShutdownXMS
530 =
531 ======================
532 */
533
534 void MML_ShutdownXMS(mminfo_t *mm)
535 {
536         int     i;
537         unsigned        base;
538
539         for (i=0;i<mm->numUMBs;i++)
540         {
541                 base = mm->UMBbase[i];
542                 __asm {
543                         mov     ah,XMS_FREEUMB
544                         mov     dx,[base]
545                         call    [DWORD PTR XMSaddr]
546                 }
547         }
548 }
549
550 //==========================================================================
551
552 /*
553 ======================
554 =
555 = MML_UseSpace
556 =
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
560 =
561 ======================
562 */
563
564 /*void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
565 {
566         //huge mmblocktype huge *scan,huge *last;
567         word            segm=1;
568         word    oldend;
569         dword           segmlen;
570         dword           extra;
571
572         scan = last = mm->mmhead;
573         mm->mmrover = mm->mmhead;               // reset rover to start of memory
574
575 //
576 // search for the block that contains the range of segments
577 //
578         while(scan->start+scan->length < segstart)
579         {
580                 last = scan;
581                 scan = scan->next;
582         }
583
584 //
585 // take the given range out of the block
586 //
587         oldend = scan->start + scan->length;
588         extra = oldend - (segstart+seglength);
589
590         segmlen=extra;
591
592         //++++emsver stuff!
593         if(segm>1)/// || extra>=0x10000lu)
594         //if(extra>0xfffflu)
595         {
596                 scan->blob=segm;
597
598                 //MML_UseSpace(segstart, seglength, mm);
599
600                 printf("MML_UseSpace: Segment spans two blocks!\n");
601         //}
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);
610         }
611 //++++todo: linked list of segment!
612         if(segstart == scan->start)
613         {
614                 last->next = scan->next;                        // unlink block
615                 FREEBLOCK(scan);
616                 scan = last;
617         }
618         else
619                 scan->length = segstart-scan->start;    // shorten block
620
621         if(extra > 0)
622         {
623                 GETNEWBLOCK;
624                 mm->mmnew->useptr = NULL;
625
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;
632
633 }*/
634 void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
635 {
636         mmblocktype far *scan,far *last;
637         word    oldend;
638         sdword          extra;
639         //word segm=1;
640
641         scan = last = mm->mmhead;
642         mm->mmrover = mm->mmhead;               // reset rover to start of memory
643
644 //
645 // search for the block that contains the range of segments
646 //
647         while (scan->start+scan->length < segstart)
648         {
649                 last = scan;
650                 scan = scan->next;
651         }
652
653 //
654 // find out how many blocks it spans!
655 //
656         /*for(;seglength>=0x10000;seglength-=0xFFFF)
657         {
658                 //printf("      seglen=%lu\n", segmlen);
659                 segm++;
660         }*/
661
662 //
663 // take the given range out of the block
664 //
665         oldend = scan->start + scan->length;
666         extra = oldend - (segstart+((unsigned)seglength));
667         if (extra < 0)
668         {
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);
676                 printf("\n");
677                 printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
678                 printf("========================================\n");
679                 //return;
680         }
681
682         if (segstart == scan->start)
683         {
684                 last->next = scan->next;                        // unlink block
685                 FREEBLOCK(scan);
686                 scan = last;
687         }
688         else
689                 scan->length = segstart-scan->start;    // shorten block
690
691         if (extra > 0)
692         {
693                 GETNEWBLOCK;
694                 mm->mmnew->useptr = NULL;
695
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;
701         }
702
703 }
704
705 //==========================================================================
706
707 /*
708 ====================
709 =
710 = MML_ClearBlock
711 =
712 = We are out of blocks, so free a purgable block
713 =
714 ====================
715 */
716
717 void MML_ClearBlock(mminfo_t *mm)
718 {
719         //huge mmblocktype huge *scan,huge *last;
720         mmblocktype far *scan,far *last;
721
722         scan = mm->mmhead->next;
723
724         while(scan)
725         {
726                 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
727                 {
728                         MM_FreePtr(scan->useptr, mm);
729                         return;
730                 }
731                 scan = scan->next;
732         }
733
734         printf("MM_ClearBlock: No purgable blocks!\n");
735 }
736
737
738 //==========================================================================
739
740 /*
741 ===================
742 =
743 = MM_Startup
744 =
745 = Grabs all space from turbo with malloc/farmalloc
746 = Allocates bufferseg misc buffer
747 =
748 ===================
749 */
750
751 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
752 {
753         int i;
754         //dword length,seglength;
755         dword length; word seglength;
756         //huge void huge        *start;
757         void far        *start;
758         word    segstart;//,endfree;
759         //memptr *peeonself;
760
761 //      if(mm->mmstarted)
762 //              MM_Shutdown(mm);
763
764         mm->mmstarted = true;
765         mm->bombonerror = true;
766
767 //
768 // set up the linked list (everything in the free list;
769 //
770         mm->mmhead = NULL;
771         mm->mmfree = &(mm->mmblocks[0]);
772         for(i=0;i<MAXBLOCKS-1;i++)
773         {
774                 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
775         }
776         mm->mmblocks[i].next = NULL;
777
778 //
779 // locked block of all memory until we punch out free space
780 //
781         GETNEWBLOCK;
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;
789
790         //printf("              %x\n", peeonself);
791         //printf("              %x\n", *peeonself);
792 //
793 // get all available near conventional memory segments
794 //
795 #ifdef __WATCOMC__
796         _nheapgrow();
797         length=(dword)_memavl();//(dword)GetFreeSize();
798         //huge start = (void huge *)(mm->nearheap = _nmalloc(length));
799         start = (void far *)(mm->nearheap = _nmalloc(length));
800 #endif
801 #ifdef __BORLANDC__
802         length=coreleft();
803         //huge start = (void huge *)(mm->nearheap = malloc(length));
804         start = (void far *)(mm->nearheap = malloc(length));
805 #endif
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);
813
814 //
815 // get all available far conventional memory segments
816 //
817         //printf("_FARCORELEFT                          %lu\n", _FCORELEFT);
818 #ifdef __WATCOMC__
819         _fheapgrow();
820 #endif
821 #ifdef __BORLANDC__
822         printf("farcoreleft()                           %lu\n", farcoreleft());
823         printf("(farcoreleft()+32)-_FCORELEFT   %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
824 #endif
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);
835
836         mmi->mainmem = mmi->nearheap + mmi->farheap;
837
838 //
839 // detect EMS and allocate up to 64K at page frame
840 //
841         mmi->EMSmem = 0;
842 //goto emsskip; //0000
843         for(i = 1;i <
844 #ifdef __WATCOMC__
845         __argc
846 #endif
847 #ifdef __BORLANDC__
848         _argc
849 #endif
850         ;i++)
851         {
852                 if(US_CheckParm(
853 #ifdef __WATCOMC__
854         __argv[i]
855 #endif
856 #ifdef __BORLANDC__
857         _argv[i]
858 #endif
859                         ,ParmStringsexmm) == 0)
860                         goto emsskip;                           // param NOEMS
861         }
862         if(MML_CheckForEMS())
863         {
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
869                 //else
870                         //MM_MapXEMS(mm, mmi);                                  // map in used pages
871         }
872
873 //
874 // detect XMS and get upper memory blocks
875 //
876 emsskip:
877         mmi->XMSmem = 0;
878 goto xmsskip;//0000
879         for(i = 1;i <
880 #ifdef __WATCOMC__
881         __argc
882 #endif
883 #ifdef __BORLANDC__
884         _argc
885 #endif
886         ;i++)
887         {
888                 if(US_CheckParm(
889 #ifdef __WATCOMC__
890         __argv[i]
891 #endif
892 #ifdef __BORLANDC__
893         _argv[i]
894 #endif
895                         ,ParmStringsexmm) == 0)
896                         goto xmsskip;                           // param NOXMS
897         }
898         if(MML_CheckForXMS(mm))
899         {
900                 MML_SetupXMS(mm, mmi);                                  // allocate as many UMBs as possible
901         }
902
903 //
904 // allocate the misc buffer
905 //
906 xmsskip:
907         mm->mmrover = mm->mmhead;               // start looking for space after low block
908
909         MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
910 }
911
912 //==========================================================================
913
914 /*
915 ====================
916 =
917 = MM_Shutdown
918 =
919 = Frees all conventional, EMS, and XMS allocated
920 =
921 ====================
922 */
923
924 void MM_Shutdown(mminfo_t *mm)
925 {
926         if(!(mm->mmstarted))
927                 return;
928
929         _ffree(mm->farheap);//  printf("                far freed\n");
930 #ifdef __WATCOMC__
931         _nfree(mm->nearheap);// printf("                near freed\n");
932 #endif
933 #ifdef __BORLANDC__
934         free(mm->nearheap);//   printf("                near freed\n");
935 #endif
936         if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf("         EMS freed\n"); }
937         if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf("               XMS freed\n"); }
938 }
939
940 //==========================================================================
941
942 /*
943 ====================
944 =
945 = MM_GetPtr
946 =
947 = Allocates an unlocked, unpurgable block
948 =
949 ====================
950 */
951
952 void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
953 {
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;
956         int                     search;
957         unsigned        needed,startseg;
958
959         needed = (size+15)/16;          // convert size from bytes to paragraphs
960
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){
965 #ifdef __DEBUG__
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));
969 #endif
970         //exit(-5); }
971         mm->mmnew->attributes = BASEATTRIBUTES;
972
973 //tryagain:
974         for (search = 0; search<3; search++)
975         {
976         //
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)
981                         search++;
982
983                 switch (search)
984                 {
985                 case 0:
986                         lastscan = mm->mmrover;
987                         scan = mm->mmrover->next;
988                         endscan = NULL;
989                         break;
990                 case 1:
991                         lastscan = mm->mmhead;
992                         scan = mm->mmhead->next;
993                         endscan = mm->mmrover;
994                         break;
995                 case 2:
996                         MM_SortMem (mm);
997                         lastscan = mm->mmhead;
998                         scan = mm->mmhead->next;
999                         endscan = NULL;
1000                         break;
1001                 }
1002
1003                 startseg = lastscan->start + lastscan->length;
1004
1005                 while (scan != endscan)
1006                 {
1007                         if (scan->start - startseg >= needed)
1008                         {
1009                         //
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
1013                         //
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
1020                                         next = purge->next;
1021                                         FREEBLOCK(purge);
1022                                         purge = next;           // purge another if not at scan
1023                                 }
1024                                 mm->mmrover = mm->mmnew;
1025                                 return; // good allocation!
1026                         }
1027
1028                         //
1029                         // if this block is purge level zero or locked, skip past it
1030                         //
1031                         if ( (scan->attributes & LOCKBIT)
1032                                 || !(scan->attributes & PURGEBITS) )
1033                         {
1034                                 lastscan = scan;
1035                                 startseg = lastscan->start + lastscan->length;
1036                         }
1037
1038
1039                         scan=scan->next;                // look at next line
1040                 }
1041         }
1042
1043         if (mm->bombonerror)
1044         {
1045 #ifdef __WATCOMC__
1046                 //heapdump();
1047 #endif
1048                 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
1049                 printf("for stability reasons the program will shut down! wwww\n");
1050                 MM_Shutdown(mm);
1051                 exit(-1);
1052         }
1053         else
1054                 mm->mmerror = true;
1055 }
1056
1057 //==========================================================================
1058
1059 /*
1060 ====================
1061 =
1062 = MM_FreePtr
1063 =
1064 = Allocates an unlocked, unpurgable block
1065 =
1066 ====================
1067 */
1068
1069 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1070 {
1071         //huge mmblocktype huge *scan,huge *last;
1072         mmblocktype far *scan,far *last;
1073
1074         last = mm->mmhead;
1075         scan = last->next;
1076
1077         if(baseptr == mm->mmrover->useptr)      // removed the last allocated block
1078                 mm->mmrover = mm->mmhead;
1079
1080         while(scan->useptr != baseptr && scan)
1081         {
1082                 last = scan;
1083                 scan = scan->next;
1084         }
1085
1086         if(!scan)
1087         {
1088                 printf("MM_FreePtr: Block not found!\n");
1089                 return;
1090         }
1091
1092         last->next = scan->next;
1093
1094         FREEBLOCK(scan);
1095 }
1096 //==========================================================================
1097
1098 /*
1099 =====================
1100 =
1101 = MM_SetPurge
1102 =
1103 = Sets the purge level for a block (locked blocks cannot be made purgable)
1104 =
1105 =====================
1106 */
1107
1108 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1109 {
1110         //huge mmblocktype huge *start;
1111         mmblocktype far *start;
1112
1113         start = mm->mmrover;
1114
1115         do
1116         {
1117                 if(mm->mmrover->useptr == baseptr)
1118                         break;
1119
1120                 mm->mmrover = mm->mmrover->next;
1121
1122                 if(!mm->mmrover)
1123                         mm->mmrover = mm->mmhead;
1124                 else if(mm->mmrover == start)
1125                 {
1126                         printf("MM_SetPurge: Block not found!");
1127                         return;
1128                 }
1129
1130         } while(1);
1131
1132         mm->mmrover->attributes &= ~PURGEBITS;
1133         mm->mmrover->attributes |= purge;
1134 }
1135
1136 //==========================================================================
1137
1138 /*
1139 =====================
1140 =
1141 = MM_SetLock
1142 =
1143 = Locks / unlocks the block
1144 =
1145 =====================
1146 */
1147
1148 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1149 {
1150         //huge mmblocktype huge *start;
1151         mmblocktype far *start;
1152
1153         start = mm->mmrover;
1154
1155         do
1156         {
1157                 if(mm->mmrover->useptr == baseptr)
1158                         break;
1159
1160                 mm->mmrover = mm->mmrover->next;
1161
1162                 if(!mm->mmrover)
1163                         mm->mmrover = mm->mmhead;
1164                 else if(mm->mmrover == start)
1165                 {
1166                         printf("MM_SetLock: Block not found!");
1167                         return;
1168                 }
1169
1170         } while(1);
1171
1172         mm->mmrover->attributes &= ~LOCKBIT;
1173         mm->mmrover->attributes |= locked*LOCKBIT;
1174 }
1175
1176 //==========================================================================
1177
1178 /*
1179 =====================
1180 =
1181 = MM_SortMem
1182 =
1183 = Throws out all purgable stuff and compresses movable blocks
1184 =
1185 =====================
1186 */
1187
1188 void MM_SortMem(mminfo_t *mm)
1189 {
1190         //huge mmblocktype huge *scan,huge *last,huge *next;
1191         mmblocktype far *scan,far *last,far *next;
1192         unsigned        start,length,source,dest,oldborder;
1193         int                     playing;
1194
1195         //
1196         // lock down a currently playing sound
1197         //
1198 /*++++  playing = SD_SoundPlaying ();
1199         if(playing)
1200         {
1201                 switch (SoundMode)
1202                 {
1203                 case sdm_PC:
1204                         playing += STARTPCSOUNDS;
1205                         break;
1206                 case sdm_AdLib:
1207                         playing += STARTADLIBSOUNDS;
1208                         break;
1209                 }
1210                 MM_SetLock(&(memptr)audiosegs[playing],true);
1211         }
1212
1213
1214         SD_StopSound();*/
1215 //      oldborder = bordercolor;
1216 //      VW_ColorBorder (15);
1217
1218         if(beforesort)
1219                 beforesort();
1220
1221         scan = mm->mmhead;
1222
1223         last = NULL;            // shut up compiler warning
1224
1225         while(scan)
1226         {
1227                 if(scan->attributes & LOCKBIT)
1228                 {
1229                 //
1230                 // block is locked, so try to pile later blocks right after it
1231                 //
1232                         start = scan->start + scan->length;
1233                 }
1234                 else
1235                 {
1236                         if(scan->attributes & PURGEBITS)
1237                         {
1238                         //
1239                         // throw out the purgable block
1240                         //
1241                                 next = scan->next;
1242                                 FREEBLOCK(scan);
1243                                 //MM_FreeBlock(scan, mm);
1244                                 last->next = next;
1245                                 scan = next;
1246                                 continue;
1247                         }
1248                         else
1249                         {
1250                         //
1251                         // push the non purgable block on top of the last moved block
1252                         //
1253                                 if(scan->start != start)
1254                                 {
1255                                         length = scan->length;
1256                                         source = scan->start;
1257                                         dest = start;
1258                                         while(length > 0xf00)
1259                                         {
1260                                                 movedata(source,0,dest,0,0xf00*16);
1261                                                 length -= 0xf00;
1262                                                 source += 0xf00;
1263                                                 dest += 0xf00;
1264                                         }
1265                                         movedata(source,0,dest,0,length*16);
1266
1267                                         scan->start = start;
1268                                         *(unsigned *)scan->useptr = start;
1269                                 }
1270                                 start = scan->start + scan->length;
1271                         }
1272                 }
1273
1274                 last = scan;
1275                 scan = scan->next;              // go to next block
1276         }
1277
1278         mm->mmrover = mm->mmhead;
1279
1280         if(aftersort)
1281                 aftersort();
1282
1283 //      VW_ColorBorder (oldborder);
1284
1285 /*++++  if(playing)
1286                 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1287 }
1288
1289 //==========================================================================
1290
1291 /*
1292 =====================
1293 =
1294 = MM_ShowMemory
1295 =
1296 =====================
1297 */
1298
1299 void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
1300 {
1301         //huge mmblocktype huge *scan;
1302         mmblocktype far *scan;
1303         word temp;
1304         sdword  end,owner;
1305         //word chx,chy;
1306         word w;
1307         //dword wwww;
1308         byte    scratch[160],scratch0[4096],scratch1[160],str[16];
1309         //byte d = '#';
1310 //****  VW_SetDefaultColors();
1311 //****  VW_SetLineWidth(40);
1312 //++++mh        temp = bufferofs;
1313 //++++mh        bufferofs = 0;
1314 //****  VW_SetScreen (0,0);
1315         scan = mm->mmhead;
1316         end = -1;
1317
1318         CA_OpenDebug (gvar);
1319         w=0;
1320         while(scan)
1321         {
1322                 strcpy(scratch, AARESET);
1323                 if(scan->attributes & PURGEBITS)
1324                         strcpy(scratch0, AAMAGENTA);            // dark purple = purgable
1325                 else
1326                         strcpy(scratch0, AABLUE);               // medium blue = non purgable
1327                 if(scan->attributes & LOCKBIT)
1328                         strcpy(scratch0, AARED);                // red = locked
1329                 if(scan->start<=end)
1330                 {
1331                         printf("\nend==%d\n\n", end);
1332                         strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
1333                         strcat(scratch, "End's Size: ");
1334                         ultoa (end,str,10);
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");
1341                         break;
1342                 }
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)
1348                                 //{
1349 //++++                                  modexClearRegion(page, chx, chy, 4, 4, color);
1350                                 //}
1351 //++++          VW_Hlin(scan->start,(unsigned)end,0,color);
1352                 for(w=(scan->start)/80;w<=end/80;w++)
1353                 {
1354                         //printf("+     %u      %lu\n", w, scan->length);
1355                         strcat(scratch0, "+");
1356                 }
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
1361
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)
1365                 {
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++)
1372                         {
1373                                 //printf("0     %x      %u      %lu\n", scan->next->start, w, scan->length);
1374                                 strcat(scratch0,"0");
1375                         }
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");
1380                         //getch();
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++)
1388                         {
1389                                 //printf("0     %x      %x      %u\n", scan->start, w);
1390                                 strcat(scratch0,"0");
1391                         }
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");
1395                         getch();
1396                 }*/
1397                 strcat(scratch0, AARESET);
1398                 //strcat(scratch0,"\n");
1399                         //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1400                         //{
1401 //                              chx+=scan->next->start;
1402 //                              modexClearRegion(page, chx, chy, 4, 4, 2);
1403                         //}
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);
1408                 VW_Plot(x,y,15);*/
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);
1424 //++++chy+=4;
1425 //fprintf(stdout, "%s", scratch);
1426
1427                 scan = scan->next;
1428         }
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));*/
1443
1444
1445         CA_CloseDebug (gvar);
1446
1447 //++++mh        IN_Ack();
1448 //****  VW_SetLineWidth(64);
1449 //++++mh        bufferofs = temp;
1450 }
1451
1452 //==========================================================================
1453
1454 /*
1455 =====================
1456 =
1457 = MM_DumpData
1458 =
1459 =====================
1460 */
1461
1462 void MM_DumpData(mminfo_t *mm)
1463 {
1464         //huge mmblocktype huge *scan,huge *best;
1465         mmblocktype far *scan,far *best;
1466         long    lowest,oldlowest;
1467         word    owner;
1468         byte    lock,purge;
1469         FILE    *dumpfile;
1470
1471         free(mm->nearheap);
1472 #ifdef __BORLANDC__
1473                 dumpfile = fopen ("mmdump.16b","w");
1474 #endif
1475 #ifdef __WATCOMC__
1476                 dumpfile = fopen ("mmdump.16w","w");
1477 #endif
1478         if (!dumpfile){
1479                 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1480                 return;
1481         }
1482
1483         lowest = -1;
1484         do
1485         {
1486                 oldlowest = lowest;
1487                 lowest = 0xffff;
1488
1489                 scan = mm->mmhead;
1490                 while (scan)
1491                 {
1492                         owner = (unsigned)scan->useptr;
1493
1494                         if (owner && owner<lowest && owner > oldlowest)
1495                         {
1496                                 best = scan;
1497                                 lowest = owner;
1498                         }
1499
1500                         scan = scan->next;
1501                 }
1502
1503                 if (lowest != 0xffff)
1504                 {
1505                         if (best->attributes & PURGEBITS)
1506                                 purge = 'P';
1507                         else
1508                                 purge = '-';
1509                         if (best->attributes & LOCKBIT)
1510                                 lock = 'L';
1511                         else
1512                                 lock = '-';
1513                         fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1514                         ,(unsigned)lowest,lock,purge,best->length);
1515                 }
1516
1517         } while (lowest != 0xffff);
1518
1519         fclose(dumpfile);
1520         printf("MMDUMP.16 created.\n");
1521 }
1522
1523 //==========================================================================
1524
1525
1526 /*
1527 ======================
1528 =
1529 = MM_UnusedMemory
1530 =
1531 = Returns the total free space without purging
1532 =
1533 ======================
1534 */
1535
1536 dword MM_UnusedMemory(mminfo_t *mm)
1537 {
1538         dword free;
1539         //huge mmblocktype huge *scan;
1540         mmblocktype far *scan;
1541
1542         free = 0;
1543         scan = mm->mmhead;
1544
1545         while(scan->next)
1546         {
1547                 free += scan->next->start - (scan->start + scan->length);
1548                 scan = scan->next;
1549         }
1550
1551         return free*16lu;
1552 //      return free;
1553 }
1554
1555 //==========================================================================
1556
1557
1558 /*
1559 ======================
1560 =
1561 = MM_TotalFree
1562 =
1563 = Returns the total free space with purging
1564 =
1565 ======================
1566 */
1567
1568 dword MM_TotalFree(mminfo_t *mm)
1569 {
1570         dword free;
1571         //huge mmblocktype huge *scan;
1572         mmblocktype far *scan;
1573
1574         free = 0;
1575         scan = mm->mmhead;
1576
1577         while(scan->next)
1578         {
1579                 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1580                         free += scan->length;
1581                 free += scan->next->start - (scan->start + scan->length);
1582                 scan = scan->next;
1583         }
1584
1585         return free*16lu;
1586 //      return free;
1587 }
1588
1589 //==========================================================================
1590
1591 /*
1592 =====================
1593 =
1594 = MM_Report
1595 =
1596 =====================
1597 */
1598
1599 void MM_Report(global_game_variables_t *gvar)
1600 {
1601         printf("========================================\n");
1602         printf("                MM_Report\n");
1603         printf("========================================\n");
1604         if(MML_CheckForEMS())
1605         {
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);
1610         }
1611         if(MML_CheckForXMS(&(gvar->mm)))
1612         {
1613                 printf("        XMS\n");
1614                 printf("                XMSaddr:        %X\n", *XMSaddr);
1615         }
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)));
1621 }
1622
1623 //==========================================================================
1624
1625 /*
1626 =====================
1627 =
1628 = MM_EMSerr
1629 =
1630 =====================
1631 */
1632
1633 void MM_EMSerr(byte *stri, byte err)
1634 {
1635         //Returns a text string describing the error code in EMS.Error.
1636         switch(err)
1637         {
1638                 case 0x0:
1639                         strcat(stri, "successful");
1640                 break;
1641                 case 0x80:
1642                         strcat(stri, "internal error");
1643                 break;
1644                 case 0x81:
1645                         strcat(stri, "hardware malfunction");
1646                 break;
1647                 case 0x82:
1648                         strcat(stri, "busy .. retry later");
1649                 break;
1650                 case 0x83:
1651                         strcat(stri, "invalid handle");
1652                 break;
1653                 case 0x84:
1654                         strcat(stri, "undefined function requested by application");
1655                 break;
1656                 case 0x85:
1657                         strcat(stri, "no more handles available");
1658                 break;
1659                 case 0x86:
1660                         strcat(stri, "error in save or restore of mapping context");
1661                 break;
1662                 case 0x87:
1663                         strcat(stri, "insufficient memory pages in system");
1664                 break;
1665                 case 0x88:
1666                         strcat(stri, "insufficient memory pages available");
1667                 break;
1668                 case 0x89:
1669                         strcat(stri, "zero pages requested");
1670                 break;
1671                 case 0x8A:
1672                         strcat(stri, "invalid logical page number encountered");
1673                 break;
1674                 case 0x8B:
1675                         strcat(stri, "invalid physical page number encountered");
1676                 break;
1677                 case 0x8C:
1678                         strcat(stri, "page-mapping hardware state save area is full");
1679                 break;
1680                 case 0x8D:
1681                         strcat(stri, "save of mapping context failed");
1682                 break;
1683                 case 0x8E:
1684                         strcat(stri, "restore of mapping context failed");
1685                 break;
1686                 case 0x8F:
1687                         strcat(stri, "undefined subfunction");
1688                 break;
1689                 case 0x90:
1690                         strcat(stri, "undefined attribute type");
1691                 break;
1692                 case 0x91:
1693                         strcat(stri, "feature not supported");
1694                 break;
1695                 case 0x92:
1696                         strcat(stri, "successful, but a portion of the source region has been overwritten");
1697                 break;
1698                 case 0x93:
1699                         strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1700                 break;
1701                 case 0x94:
1702                         strcat(stri, "conventional and expanded memory regions overlap");
1703                 break;
1704                 case 0x95:
1705                         strcat(stri, "offset within logical page exceeds size of logical page");
1706                 break;
1707                 case 0x96:
1708                         strcat(stri, "region length exceeds 1 MB");
1709                 break;
1710                 case 0x97:
1711                         strcat(stri, "source and destination EMS regions have same handle and overlap");
1712                 break;
1713                 case 0x98:
1714                         strcat(stri, "memory source or destination type undefined");
1715                 break;
1716                 case 0x9A:
1717                         strcat(stri, "specified alternate map register or DMA register set not supported");
1718                 break;
1719                 case 0x9B:
1720                         strcat(stri, "all alternate map register or DMA register sets currently allocated");
1721                 break;
1722                 case 0x9C:
1723                         strcat(stri, "alternate map register or DMA register sets not supported");
1724                 break;
1725                 case 0x9D:
1726                         strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1727                 break;
1728                 case 0x9E:
1729                         strcat(stri, "dedicated DMA channels not supported");
1730                 break;
1731                 case 0x9F:
1732                         strcat(stri, "specified dedicated DMA channel not supported");
1733                 break;
1734                 case 0xA0:
1735                         strcat(stri, "no such handle name");
1736                 break;
1737                 case 0xA1:
1738                         strcat(stri, "a handle found had no name, or duplicate handle name");
1739                 break;
1740                 case 0xA2:
1741                         strcat(stri, "attempted to wrap around 1M conventional address space");
1742                 break;
1743                 case 0xA3:
1744                         strcat(stri, "source array corrupted");
1745                 break;
1746                 case 0xA4:
1747                         strcat(stri, "operating system denied access");
1748                 break;
1749                 default:
1750                         strcat(stri, "undefined error");
1751         }
1752 }
1753
1754 //==========================================================================
1755
1756 /*
1757 =====================
1758 =
1759 = MM_BombOnError
1760 =
1761 =====================
1762 */
1763
1764 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1765 {
1766         mm->bombonerror = bomb;
1767 }
1768
1769 /*void MM_GetNewBlock(mminfo_t *mm)
1770 {
1771         if(!mm->mmfree)
1772                 MML_ClearBlock(mm);
1773         mm->mmnew=mm->mmfree;
1774         mm->mmfree=mm->mmfree->next;
1775         if(!(mm->mmnew=mm->mmfree))
1776         {
1777                 printf("MM_GETNEWBLOCK: No free blocks!\n");
1778                 return;
1779         }
1780         mm->mmfree=mm->mmfree->next;
1781 }
1782
1783 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1784 {
1785         x->useptr=NULL;
1786         x->next=mm->mmfree;
1787         mm->mmfree=x;
1788 }*/
1789
1790 /*void MM_seguin(void)
1791 {
1792         __asm {
1793                 push    ds
1794                 mov     ax,ds
1795                 inc             ax
1796                 mov     ds,ax
1797         }
1798 }
1799
1800 void MM_segude(void)
1801 {
1802         __asm {
1803                 pop ds
1804         }
1805 }*/
1806
1807 /*
1808 pull data from far and put it into ds var
1809 mov ax,es:si
1810 mov x,ax
1811 */
1812 /*
1813 ss stack segment
1814 sp top of stack
1815 bp bottem of stack
1816 */