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