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