]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_mm.c
I am understanding why the memory management system is not working now!!
[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         unsigned        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                 MM_FreeBlock(scan, mm);
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                 mm->mmnew->next = scan->next;
647                 scan->next = mm->mmnew;
648                 mm->mmnew->start = segstart+seglength;
649                 mm->mmnew->length = extra;
650                 mm->mmnew->attributes = LOCKBIT;
651         }//else if(segm>0) goto segu;
652
653 }
654
655 //==========================================================================
656
657 /*
658 ====================
659 =
660 = MML_ClearBlock
661 =
662 = We are out of blocks, so free a purgable block
663 =
664 ====================
665 */
666
667 void MML_ClearBlock(mminfo_t *mm)
668 {
669         mmblocktype huge *scan,huge *last;
670
671         scan = mm->mmhead->next;
672
673         while(scan)
674         {
675                 if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS))
676                 {
677                         MM_FreePtr(scan->useptr, mm);
678                         return;
679                 }
680                 scan = scan->next;
681         }
682
683         printf("MM_ClearBlock: No purgable blocks!\n");
684 }
685
686
687 //==========================================================================
688
689 /*
690 ===================
691 =
692 = MM_Startup
693 =
694 = Grabs all space from turbo with malloc/farmalloc
695 = Allocates bufferseg misc buffer
696 =
697 ===================
698 */
699
700 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
701 {
702         int i;
703         dword length,seglength;
704         //dword length; word seglength;
705         void huge       *start;
706         word    segstart;//,endfree;
707
708         if(mm->mmstarted)
709                 MM_Shutdown(mm);
710
711         mm->mmstarted = true;
712         mm->bombonerror = true;
713         mm->endid=0;
714
715 //
716 // set up the linked list (everything in the free list;
717 //
718         //printf("              linked list making!\n");
719         mm->mmhead = NULL;
720         mm->mmfree = &(mm->mmblocks[0]);
721         for(i=0;i<MAXBLOCKS-1;i++)
722         {
723                 mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
724         }
725         mm->mmblocks[i].next = NULL;
726
727 //
728 // locked block of all memory until we punch out free space
729 //
730         //printf("              newblock making!\n");
731         MM_GetNewBlock(mm);
732         mm->mmhead = mm->mmnew;                         // this will allways be the first node
733         mm->mmnew->start = 0;
734         mm->mmnew->length = 0xffff;
735         mm->mmnew->attributes = LOCKBIT;
736         mm->mmnew->next = NULL;
737         mm->mmrover = mm->mmhead;
738
739 //
740 // get all available near conventional memory segments
741 //
742 //      printf("                nearheap making!\n");
743 #ifdef __WATCOMC__
744         _nheapgrow();
745         length=(dword)_memmax();//(dword)GetFreeSize();
746         start = (void huge *)(mm->nearheap = _nmalloc(length));
747 #endif
748 #ifdef __BORLANDC__
749         length=coreleft();
750         start = (void huge *)(mm->nearheap = malloc(length));
751 #endif
752         length -= 16-(FP_OFF(start)&15);
753         length -= SAVENEARHEAP;
754         seglength = length / 16;                        // now in paragraphs
755         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
756         MML_UseSpace(segstart,seglength, mm);
757         mmi->nearheap = length;
758         printf("start=%Fp       segstart=%x     seglen=%l       len=%l\n", start, segstart, seglength, length);
759         //heapdump();
760
761 //
762 // get all available far conventional memory segments
763 //
764 //      printf("                farheap making!\n");
765 #ifdef __WATCOMC__
766         _fheapgrow();
767         length=(dword)GetFarFreeSize();//0xffffUL*4UL;
768 #endif
769 #ifdef __BORLANDC__
770         length=farcoreleft();
771 #endif
772         //start = mm->farheap = halloc(length, 1);
773         start = mm->farheap = _fmalloc(length);
774         length -= 16-(FP_OFF(start)&15);
775         length -= SAVEFARHEAP;
776         seglength = length / 16;                        // now in paragraphs
777         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
778         MML_UseSpace(segstart,seglength, mm);
779         mmi->farheap = length;
780         printf("start=%Fp       segstart=%x     seglen=%l       len=%l\n", start, segstart, seglength, length);
781         //heapdump();
782
783         mmi->mainmem = mmi->nearheap + mmi->farheap;
784
785 //      getch();
786
787 //goto xmsskip;
788
789 //
790 // detect EMS and allocate up to 64K at page frame
791 //
792 //printf("              EMS1\n");
793 //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!
794         mmi->EMSmem = 0;
795         for(i = 1;i <
796 #ifdef __WATCOMC__
797         __argc
798 #endif
799 #ifdef __BORLANDC__
800         _argc
801 #endif
802         ;i++)
803         {
804                 if(US_CheckParm(
805 #ifdef __WATCOMC__
806         __argv[i]
807 #endif
808 #ifdef __BORLANDC__
809         _argv[i]
810 #endif
811                         ,ParmStringsexmm) == 0)
812                         goto emsskip;                           // param NOEMS
813         }
814 //printf("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");   //bug!
815         if(MML_CheckForEMS())
816         {
817 //printf("              EMS2\n");
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                 MML_SetupEMS(mm);                                       // allocate space
820 //printf("              EMS3\n");
821 //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!
822                 //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
823                 MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
824 //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!
825 //printf("              EMS4\n");
826                 //if(mm->EMSVer<0x40)
827                         MM_MapEMS(mm, mmi);                                     // map in used pages
828                 //else
829                         //MM_MapXEMS(mm, mmi);                                  // map in used pages
830         }
831
832 mmi->XMSmem=0;
833 goto xmsskip;
834
835 //
836 // detect XMS and get upper memory blocks
837 //
838 emsskip:
839         mmi->XMSmem = 0;
840         for(i = 1;i <
841 #ifdef __WATCOMC__
842         __argc
843 #endif
844 #ifdef __BORLANDC__
845         _argc
846 #endif
847         ;i++)
848         {
849                 if(US_CheckParm(
850 #ifdef __WATCOMC__
851         __argv[i]
852 #endif
853 #ifdef __BORLANDC__
854         _argv[i]
855 #endif
856                         ,ParmStringsexmm) == 0)
857                         goto xmsskip;                           // param NOXMS
858         }
859 //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!
860         if(MML_CheckForXMS(mm))
861         {
862 //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!
863 //printf("              XMS!\n");
864                 MML_SetupXMS(mm, mmi);                                  // allocate as many UMBs as possible
865         }
866
867 //
868 // allocate the misc buffer
869 //
870 xmsskip:
871         mm->mmrover = mm->mmhead;               // start looking for space after low block
872
873         MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
874 }
875
876 //==========================================================================
877
878 /*
879 ====================
880 =
881 = MM_Shutdown
882 =
883 = Frees all conventional, EMS, and XMS allocated
884 =
885 ====================
886 */
887
888 void MM_Shutdown(mminfo_t *mm)
889 {
890         if(!(mm->mmstarted))
891                 return;
892
893         _ffree(mm->farheap);    printf("                far freed\n");
894 #ifdef __WATCOMC__
895         _nfree(mm->nearheap);   printf("                near freed\n");
896 #endif
897 #ifdef __BORLANDC__
898         free(mm->nearheap);     printf("                near freed\n");
899 #endif
900         if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); printf("            EMS freed\n"); }
901         if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); printf("          XMS freed\n"); }
902 }
903
904 //==========================================================================
905
906 /*
907 ====================
908 =
909 = MM_GetPtr
910 =
911 = Allocates an unlocked, unpurgable block
912 =
913 ====================
914 */
915
916 void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
917 {
918         mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
919         int                     search;
920         dword   needed;
921         word    startseg;
922
923         needed = (size+15)/16;          // convert size from bytes to paragraphs
924 printf(".");    //0000
925         MM_GetNewBlock(mm);                             // fill in start and next after a spot is found
926         mm->mmnew->length = needed;
927         mm->mmnew->useptr = baseptr;
928         mm->mmnew->attributes = BASEATTRIBUTES;
929 printf(".");    //0000
930         for(search = 0; search<mm->endid; search++)
931         {
932 printf("        [case]");       //0000
933         //
934         // first search:        try to allocate right after the rover, then on up
935         // second search:       search from the head pointer up to the rover
936         // third search:        compress memory, then scan from start
937                 if(search == 1 && mm->mmrover == mm->mmhead)
938                         search++;
939
940                 switch(search)
941                 {
942                 case 0:
943 printf("0       ");     //0000
944                         lastscan = mm->mmrover;
945                         scan = mm->mmrover->next;
946                         endscan = NULL;
947                         break;
948                 case 1:
949 printf("1       ");     //0000
950                         lastscan = mm->mmhead;
951                         scan = mm->mmhead->next;
952                         endscan = mm->mmrover;
953                         break;
954                 case 2:
955 printf("2       ");     //0000
956                         MM_SortMem(mm);
957                         lastscan = mm->mmhead;
958                         scan = mm->mmhead->next;
959                         endscan = NULL;
960                         break;
961                 }
962
963                 startseg = lastscan->start + (word)lastscan->length;
964
965                 while(scan != endscan)
966                 {
967 //printf(",");  //0000
968                         if(scan->start - startseg >= needed)
969                         {
970 printf(".");    //0000
971                         //
972                         // got enough space between the end of lastscan and
973                         // the start of scan, so throw out anything in the middle
974                         // and allocate the new block
975                         //
976                                 purge = lastscan->next;
977                                 lastscan->next = mm->mmnew;
978                                 mm->mmnew->start = *(word *)baseptr = startseg;
979                                 mm->mmnew->next = scan;
980                                 while(purge != scan)
981                                 {       // free the purgable block
982 printf("                freeing block~\n");     //0000
983                                         next = purge->next;
984                                         MM_FreeBlock(purge, mm);
985                                         purge = next;           // purge another if not at scan
986                                 }
987                                 mm->mmrover = mm->mmnew;
988                                 return; // good allocation!
989                         }
990
991                         //
992                         // if this block is purge level zero or locked, skip past it
993                         //
994                         if((scan->attributes & LOCKBIT)
995                                 || !(scan->attributes & PURGEBITS) )
996                         {
997 printf("        [lock] ");      //0000
998 printf("len=%lu ", scan->length);
999                                 lastscan = scan;
1000                                 startseg = lastscan->start + (word)lastscan->length;
1001                         }
1002
1003 printf("\n");
1004                         scan=scan->next;                // look at next line
1005                 }
1006         }
1007
1008         if (mm->bombonerror)
1009         {
1010 #ifdef __WATCOMC__
1011                 heapdump();
1012 #endif
1013                 printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
1014                 printf("for stability reasons the program will shut down! wwww\n");
1015                 printf("                endid=%u\n",(mm->endid));
1016                 MM_Shutdown(mm);
1017                 exit(-1);
1018         }
1019         else
1020                 mm->mmerror = true;
1021 }
1022
1023 //==========================================================================
1024
1025 /*
1026 ====================
1027 =
1028 = MM_FreePtr
1029 =
1030 = Allocates an unlocked, unpurgable block
1031 =
1032 ====================
1033 */
1034
1035 void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
1036 {
1037         mmblocktype huge *scan,huge *last;
1038
1039         last = mm->mmhead;
1040         scan = last->next;
1041
1042         if(baseptr == mm->mmrover->useptr)      // removed the last allocated block
1043                 mm->mmrover = mm->mmhead;
1044
1045         while(scan->useptr != baseptr && scan)
1046         {
1047                 last = scan;
1048                 scan = scan->next;
1049         }
1050
1051         if(!scan)
1052         {
1053                 printf("MM_FreePtr: Block not found!\n");
1054                 return;
1055         }
1056
1057         last->next = scan->next;
1058
1059         MM_FreeBlock(scan, mm);
1060 }
1061 //==========================================================================
1062
1063 /*
1064 =====================
1065 =
1066 = MM_SetPurge
1067 =
1068 = Sets the purge level for a block (locked blocks cannot be made purgable)
1069 =
1070 =====================
1071 */
1072
1073 void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
1074 {
1075         mmblocktype huge *start;
1076
1077         start = mm->mmrover;
1078
1079         do
1080         {
1081                 if(mm->mmrover->useptr == baseptr)
1082                         break;
1083
1084                 mm->mmrover = mm->mmrover->next;
1085
1086                 if(!mm->mmrover)
1087                         mm->mmrover = mm->mmhead;
1088                 else if(mm->mmrover == start)
1089                 {
1090                         printf("MM_SetPurge: Block not found!");
1091                         return;
1092                 }
1093
1094         } while(1);
1095
1096         mm->mmrover->attributes &= ~PURGEBITS;
1097         mm->mmrover->attributes |= purge;
1098 }
1099
1100 //==========================================================================
1101
1102 /*
1103 =====================
1104 =
1105 = MM_SetLock
1106 =
1107 = Locks / unlocks the block
1108 =
1109 =====================
1110 */
1111
1112 void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
1113 {
1114         mmblocktype huge *start;
1115
1116         start = mm->mmrover;
1117
1118         do
1119         {
1120                 if(mm->mmrover->useptr == baseptr)
1121                         break;
1122
1123                 mm->mmrover = mm->mmrover->next;
1124
1125                 if(!mm->mmrover)
1126                         mm->mmrover = mm->mmhead;
1127                 else if(mm->mmrover == start)
1128                 {
1129                         printf("MM_SetLock: Block not found!");
1130                         return;
1131                 }
1132
1133         } while(1);
1134
1135         mm->mmrover->attributes &= ~LOCKBIT;
1136         mm->mmrover->attributes |= locked*LOCKBIT;
1137 }
1138
1139 //==========================================================================
1140
1141 /*
1142 =====================
1143 =
1144 = MM_SortMem
1145 =
1146 = Throws out all purgable stuff and compresses movable blocks
1147 =
1148 =====================
1149 */
1150
1151 void MM_SortMem(mminfo_t *mm)
1152 {
1153         mmblocktype huge *scan,huge *last,huge *next;
1154         unsigned        start,length,source,dest,oldborder;
1155         int                     playing;
1156
1157         //
1158         // lock down a currently playing sound
1159         //
1160 /*++++  playing = SD_SoundPlaying ();
1161         if(playing)
1162         {
1163                 switch (SoundMode)
1164                 {
1165                 case sdm_PC:
1166                         playing += STARTPCSOUNDS;
1167                         break;
1168                 case sdm_AdLib:
1169                         playing += STARTADLIBSOUNDS;
1170                         break;
1171                 }
1172                 MM_SetLock(&(memptr)audiosegs[playing],true);
1173         }
1174
1175
1176         SD_StopSound();*/
1177 //      oldborder = bordercolor;
1178 //      VW_ColorBorder (15);
1179
1180         if(beforesort)
1181                 beforesort();
1182
1183         scan = mm->mmhead;
1184
1185         last = NULL;            // shut up compiler warning
1186
1187         while(scan)
1188         {
1189                 if(scan->attributes & LOCKBIT)
1190                 {
1191                 //
1192                 // block is locked, so try to pile later blocks right after it
1193                 //
1194                         start = scan->start + scan->length;
1195                 }
1196                 else
1197                 {
1198                         if(scan->attributes & PURGEBITS)
1199                         {
1200                         //
1201                         // throw out the purgable block
1202                         //
1203                                 next = scan->next;
1204                                 MM_FreeBlock(scan, mm);
1205                                 last->next = next;
1206                                 scan = next;
1207                                 continue;
1208                         }
1209                         else
1210                         {
1211                         //
1212                         // push the non purgable block on top of the last moved block
1213                         //
1214                                 if(scan->start != start)
1215                                 {
1216                                         length = scan->length;
1217                                         source = scan->start;
1218                                         dest = start;
1219                                         while(length > 0xf00)
1220                                         {
1221                                                 movedata(source,0,dest,0,0xf00*16);
1222                                                 length -= 0xf00;
1223                                                 source += 0xf00;
1224                                                 dest += 0xf00;
1225                                         }
1226                                         movedata(source,0,dest,0,length*16);
1227
1228                                         scan->start = start;
1229                                         *(unsigned *)scan->useptr = start;
1230                                 }
1231                                 start = scan->start + scan->length;
1232                         }
1233                 }
1234
1235                 last = scan;
1236                 scan = scan->next;              // go to next block
1237         }
1238
1239         mm->mmrover = mm->mmhead;
1240
1241         if(aftersort)
1242                 aftersort();
1243
1244 //      VW_ColorBorder (oldborder);
1245
1246 /*++++  if(playing)
1247                 MM_SetLock(&(memptr)audiosegs[playing],false);*/
1248 }
1249
1250
1251 //==========================================================================
1252
1253 //****#if 0
1254 /*
1255 =====================
1256 =
1257 = MM_ShowMemory
1258 =
1259 =====================
1260 */
1261
1262 void MM_ShowMemory(/*page_t *page, */mminfo_t *mm)
1263 {
1264         mmblocktype huge *scan;
1265 //++++  word color;
1266         word temp;
1267         long    end,owner;
1268 //++++  word chx,chy;
1269         byte    scratch[160],str[16];
1270
1271 //****  VW_SetDefaultColors();
1272 //****  VW_SetLineWidth(40);
1273 //++++mh        temp = bufferofs;
1274 //++++mh        bufferofs = 0;
1275 //****  VW_SetScreen (0,0);
1276
1277         scan = mm->mmhead;
1278
1279         end = -1;
1280
1281 CA_OpenDebug ();
1282
1283 //++++  chx=0;
1284 //++++  chy=0;
1285
1286         while(scan)
1287         {
1288 /*++++          if(scan->attributes & PURGEBITS)
1289                         color = 5;              // dark purple = purgable
1290                 else
1291                         color = 9;              // medium blue = non purgable
1292                 if(scan->attributes & LOCKBIT)
1293                         color = 12;             // red = locked*/
1294                 if(scan->start<=end)
1295                 {
1296                         //printf(");
1297                         write(debughandle,"\nMM_ShowMemory: Memory block order currupted!\n",strlen("\nMM_ShowMemory: Memory block order currupted!\n"));
1298                         //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
1299                         return;
1300                 }
1301                 end = scan->start+scan->length-1;
1302 //++++          chy = scan->start/320;
1303 //++++          chx = scan->start%320;
1304                                 //modexhlin(page, scan->start, (unsigned)end, chy, color);
1305                                 //for(chx=scan->start;chx+4>=(word)end;chx+=4)
1306                                 //{
1307 //++++                                  modexClearRegion(page, chx, chy, 4, 4, color);
1308                                 //}
1309
1310 //++++          VW_Hlin(scan->start,(unsigned)end,0,color);
1311
1312 //++++          VW_Plot(scan->start,0,15);
1313 //++++                          modexClearRegion(page, chx, chy, 4, 4, 15);
1314                 if(scan->next->start > end+1)
1315 //++++                  VW_Hlin(end+1,scan->next->start,0,0);   // black = free
1316                         //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
1317                         //{
1318 //++++                          chx+=scan->next->start;
1319 //++++                          modexClearRegion(page, chx, chy, 4, 4, 2);
1320                         //}
1321                                         //modexhlin(page, end+1,scan->next->start, chy, 0);
1322
1323 /*
1324                 end = scan->length-1;
1325                 y = scan->start/320;
1326                 x = scan->start%320;
1327                 VW_Hlin(x,x+end,y,color);
1328                 VW_Plot(x,y,15);
1329                 if (scan->next && scan->next->start > end+1)
1330                         VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
1331 */
1332
1333 //****#if 0
1334 //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!
1335 strcpy(scratch,"Seg:");
1336 ultoa (scan->start,str,16);
1337 strcat (scratch,str);
1338 strcat (scratch,"\tSize:");
1339 ultoa ((dword)scan->length,str,10);
1340 strcat (scratch,str);
1341 strcat (scratch,"\tOwner:0x");
1342 owner = (unsigned)scan->useptr;
1343 ultoa (owner,str,16);
1344 strcat (scratch,str);
1345 strcat (scratch,"\n");
1346 write(debughandle,scratch,strlen(scratch));
1347 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
1348 //++++chy+=4;
1349 //fprintf(stdout, "%s", scratch);
1350 //****#endif
1351
1352                 scan = scan->next;
1353         }
1354
1355 CA_CloseDebug ();
1356
1357 //++++mh        IN_Ack();
1358 //****  VW_SetLineWidth(64);
1359 //++++mh        bufferofs = temp;
1360 }
1361 //****#endif
1362
1363 //==========================================================================
1364
1365 /*
1366 =====================
1367 =
1368 = MM_DumpData
1369 =
1370 =====================
1371 */
1372
1373 void MM_DumpData(mminfo_t *mm)
1374 {
1375         mmblocktype far *scan,far *best;
1376         long    lowest,oldlowest;
1377         word    owner;
1378         byte    lock,purge;
1379         FILE    *dumpfile;
1380
1381
1382         //++++free(mm->nearheap);
1383         dumpfile = fopen ("mmdump.16","w");
1384         if (!dumpfile){
1385                 printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
1386                 return;
1387         }
1388
1389         lowest = -1;
1390         do
1391         {
1392                 oldlowest = lowest;
1393                 lowest = 0xffff;
1394
1395                 scan = mm->mmhead;
1396                 while (scan)
1397                 {
1398                         owner = (word)scan->useptr;
1399
1400                         if (owner && owner<lowest && owner > oldlowest)
1401                         {
1402                                 best = scan;
1403                                 lowest = owner;
1404                         }
1405
1406                         scan = scan->next;
1407                 }
1408
1409                 if (lowest != 0xffff)
1410                 {
1411                         if (best->attributes & PURGEBITS)
1412                                 purge = 'P';
1413                         else
1414                                 purge = '-';
1415                         if (best->attributes & LOCKBIT)
1416                                 lock = 'L';
1417                         else
1418                                 lock = '-';
1419                         fprintf (dumpfile,"0x%p (%c%c) = %u\n"
1420                         ,(word)lowest,lock,purge,best->length);
1421                 }
1422
1423         } while (lowest != 0xffff);
1424
1425         fclose(dumpfile);
1426         printf("MMDUMP.16 created.\n");
1427 }
1428
1429 //==========================================================================
1430
1431
1432 /*
1433 ======================
1434 =
1435 = MM_UnusedMemory
1436 =
1437 = Returns the total free space without purging
1438 =
1439 ======================
1440 */
1441
1442 dword MM_UnusedMemory(mminfo_t *mm)
1443 {
1444         dword free;
1445         mmblocktype huge *scan;
1446
1447         free = 0;
1448         scan = mm->mmhead;
1449
1450         while(scan->next)
1451         {
1452                 free += scan->next->start - (scan->start + scan->length);
1453                 scan = scan->next;
1454         }
1455
1456 //      return free*16l;
1457         return free;
1458 }
1459
1460 //==========================================================================
1461
1462
1463 /*
1464 ======================
1465 =
1466 = MM_TotalFree
1467 =
1468 = Returns the total free space with purging
1469 =
1470 ======================
1471 */
1472
1473 dword MM_TotalFree(mminfo_t *mm)
1474 {
1475         dword free;
1476         mmblocktype huge *scan;
1477
1478         free = 0;
1479         scan = mm->mmhead;
1480
1481         while(scan->next)
1482         {
1483                 if((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1484                         free += scan->length;
1485                 free += scan->next->start - (scan->start + scan->length);
1486                 scan = scan->next;
1487         }
1488
1489 //      return free*16l;
1490         return free;
1491 }
1492
1493 //==========================================================================
1494
1495 /*
1496 =====================
1497 =
1498 = MM_Report
1499 =
1500 =====================
1501 */
1502
1503 void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
1504 {
1505         if(MML_CheckForEMS())
1506         {
1507                 printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
1508                 printf("totalEMSpages=%u\n", mm->totalEMSpages);
1509                 printf("freeEMSpages=%u\n", mm->freeEMSpages);
1510                 printf("EMSpageframe=%x\n", mm->EMSpageframe);
1511         }
1512         if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
1513         printf("near=%lu\n", mmi->nearheap);
1514         printf("far=%lu\n", mmi->farheap);
1515         printf("EMSmem=%lu\n", mmi->EMSmem);
1516         printf("XMSmem=%lu\n", mmi->XMSmem);
1517         printf("mainmem=%lu\n", mmi->mainmem);
1518         printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
1519         printf("TotalFree=%lu\n", MM_TotalFree(mm));
1520         //mmi->nearheap+mmi->farheap+
1521         printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem);//+);
1522 //      printf("\n");
1523 //      printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
1524 //      printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
1525 }
1526
1527 //==========================================================================
1528
1529 /*
1530 =====================
1531 =
1532 = MM_EMSerr
1533 =
1534 =====================
1535 */
1536
1537 void MM_EMSerr(byte *stri, byte err)
1538 {
1539         //Returns a text string describing the error code in EMS.Error.
1540         switch(err)
1541         {
1542                 case 0x0:
1543                         strcat(stri, "successful");
1544                 break;
1545                 case 0x80:
1546                         strcat(stri, "internal error");
1547                 break;
1548                 case 0x81:
1549                         strcat(stri, "hardware malfunction");
1550                 break;
1551                 case 0x82:
1552                         strcat(stri, "busy .. retry later");
1553                 break;
1554                 case 0x83:
1555                         strcat(stri, "invalid handle");
1556                 break;
1557                 case 0x84:
1558                         strcat(stri, "undefined function requested by application");
1559                 break;
1560                 case 0x85:
1561                         strcat(stri, "no more handles available");
1562                 break;
1563                 case 0x86:
1564                         strcat(stri, "error in save or restore of mapping context");
1565                 break;
1566                 case 0x87:
1567                         strcat(stri, "insufficient memory pages in system");
1568                 break;
1569                 case 0x88:
1570                         strcat(stri, "insufficient memory pages available");
1571                 break;
1572                 case 0x89:
1573                         strcat(stri, "zero pages requested");
1574                 break;
1575                 case 0x8A:
1576                         strcat(stri, "invalid logical page number encountered");
1577                 break;
1578                 case 0x8B:
1579                         strcat(stri, "invalid physical page number encountered");
1580                 break;
1581                 case 0x8C:
1582                         strcat(stri, "page-mapping hardware state save area is full");
1583                 break;
1584                 case 0x8D:
1585                         strcat(stri, "save of mapping context failed");
1586                 break;
1587                 case 0x8E:
1588                         strcat(stri, "restore of mapping context failed");
1589                 break;
1590                 case 0x8F:
1591                         strcat(stri, "undefined subfunction");
1592                 break;
1593                 case 0x90:
1594                         strcat(stri, "undefined attribute type");
1595                 break;
1596                 case 0x91:
1597                         strcat(stri, "feature not supported");
1598                 break;
1599                 case 0x92:
1600                         strcat(stri, "successful, but a portion of the source region has been overwritten");
1601                 break;
1602                 case 0x93:
1603                         strcat(stri, "length of source or destination region exceeds length of region allocated to either source or destination handle");
1604                 break;
1605                 case 0x94:
1606                         strcat(stri, "conventional and expanded memory regions overlap");
1607                 break;
1608                 case 0x95:
1609                         strcat(stri, "offset within logical page exceeds size of logical page");
1610                 break;
1611                 case 0x96:
1612                         strcat(stri, "region length exceeds 1 MB");
1613                 break;
1614                 case 0x97:
1615                         strcat(stri, "source and destination EMS regions have same handle and overlap");
1616                 break;
1617                 case 0x98:
1618                         strcat(stri, "memory source or destination type undefined");
1619                 break;
1620                 case 0x9A:
1621                         strcat(stri, "specified alternate map register or DMA register set not supported");
1622                 break;
1623                 case 0x9B:
1624                         strcat(stri, "all alternate map register or DMA register sets currently allocated");
1625                 break;
1626                 case 0x9C:
1627                         strcat(stri, "alternate map register or DMA register sets not supported");
1628                 break;
1629                 case 0x9D:
1630                         strcat(stri, "undefined or unallocated alternate map register or DMA register set");
1631                 break;
1632                 case 0x9E:
1633                         strcat(stri, "dedicated DMA channels not supported");
1634                 break;
1635                 case 0x9F:
1636                         strcat(stri, "specified dedicated DMA channel not supported");
1637                 break;
1638                 case 0xA0:
1639                         strcat(stri, "no such handle name");
1640                 break;
1641                 case 0xA1:
1642                         strcat(stri, "a handle found had no name, or duplicate handle name");
1643                 break;
1644                 case 0xA2:
1645                         strcat(stri, "attempted to wrap around 1M conventional address space");
1646                 break;
1647                 case 0xA3:
1648                         strcat(stri, "source array corrupted");
1649                 break;
1650                 case 0xA4:
1651                         strcat(stri, "operating system denied access");
1652                 break;
1653                 default:
1654                         strcat(stri, "undefined error");
1655         }
1656 }
1657
1658 //==========================================================================
1659
1660 /*
1661 =====================
1662 =
1663 = MM_BombOnError
1664 =
1665 =====================
1666 */
1667
1668 void MM_BombOnError(boolean bomb, mminfo_t *mm)
1669 {
1670         mm->bombonerror = bomb;
1671 }
1672
1673 void MM_GetNewBlock(mminfo_t *mm)
1674 {
1675         if(!mm->mmfree)
1676                 MML_ClearBlock(mm);
1677         mm->mmnew=mm->mmfree;
1678         mm->mmfree=mm->mmfree->next;
1679         if(!(mm->mmnew=mm->mmfree))
1680         {
1681                 printf("MM_GETNEWBLOCK: No free blocks!\n");
1682                 return;
1683         }
1684         mm->mmfree=mm->mmfree->next;
1685         mm->endid++;    //end of list
1686 }
1687
1688 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
1689 {
1690         x->useptr=NULL;
1691         x->next=mm->mmfree;
1692         mm->mmfree=x;
1693         mm->endid--;    //end of list
1694 }
1695
1696 void MM_seguin(void)
1697 {
1698         __asm {
1699                 push    ds
1700                 mov     ax,ds
1701                 inc             ax
1702                 mov     ds,ax
1703         }
1704 }
1705
1706 void MM_segude(void)
1707 {
1708         __asm {
1709                 pop ds
1710         }
1711 }
1712
1713 /*
1714 pull data from far and put it into ds var
1715 mov ax,es:si
1716 mov x,ax
1717 */
1718 /*
1719 ss stack segment
1720 sp top of stack
1721 bp bottem of stack
1722 */