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