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