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