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