]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_mm.c
wwww debugy~
[16.git] / src / lib / 16_mm.c
1 /* Catacomb Armageddon 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 #include "src/lib/16_mm.h"
46
47 /*
48 ======================
49 =
50 = MML_CheckForEMS
51 =
52 = Routine from p36 of Extending DOS
53 =
54 =======================
55 */
56
57 boolean MML_CheckForEMS (void)
58 {
59         boolean emmcfems;
60         char    emmname[] = "EMMXXXX0";
61 //              mov     dx,OFFSET emmname
62         __asm {
63                 LEA     DX, emmname     //fix by andrius4669
64                 mov     ax,0x3d00
65                 int     0x21            // try to open EMMXXXX0 device
66                 jc      error
67
68                 mov     bx,ax
69                 mov     ax,0x4400
70
71                 int     0x21            // get device info
72                 jc      error
73
74                 and     dx,0x80
75                 jz      error
76
77                 mov     ax,0x4407
78
79                 int     0x21            // get status
80                 jc      error
81                 or      al,al
82                 jz      error
83
84                 mov     ah,0x3e
85                 int     0x21            // close handle
86                 jc      error
87                 //
88                 // EMS is good
89                 //
90                 mov     emmcfems,1
91                 jmp End
92                 error:
93                 //
94                 // EMS is bad
95                 //
96                 mov     emmcfems,0
97                 End:
98         }
99         return(emmcfems);
100 }
101
102
103 /*
104 ======================
105 =
106 = MML_SetupEMS
107 =
108 =======================
109 */
110
111 void MML_SetupEMS (void)
112 {
113         char    str[80],str2[10];
114         unsigned        err;
115         boolean errorflag=false;
116         union REGS CPURegs;
117
118         EMSVer = 0;
119         totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
120
121         __asm
122                 {
123                 mov     ah,EMS_STATUS
124                 int     EMS_INT                                         // make sure EMS hardware is present
125                 or      ah,ah
126                 jnz     error
127
128                 mov     ah,EMS_VERSION
129                 int     EMS_INT
130                 or      ah,ah
131                 jnz     error
132                 mov     [EMSVer],ax                             //      set EMSVer
133                 cmp     al,0x32                                         // only work on ems 3.2 or greater
134                 jb      error
135
136                 mov     ah,EMS_GETFRAME
137                 int     EMS_INT                                         // find the page frame address
138                 or      ah,ah
139                 jnz     error
140                 mov     [EMSpageframe],bx
141
142                 mov     ah,EMS_GETPAGES
143                 int     EMS_INT                                         // find out how much EMS is there
144                 or      ah,ah
145                 jnz     error
146                 mov     [totalEMSpages],dx
147                 mov     [freeEMSpages],bx
148                 or      bx,bx
149                 jz      noEMS                                           // no EMS at all to allocate
150
151                 cmp     bx,4
152                 jle     getpages                                        // there is only 1,2,3,or 4 pages
153                 mov     bx,4                                            // we can't use more than 4 pages
154
155 getpages:
156                 mov     [EMSpagesmapped],bx
157                 mov     ah,EMS_ALLOCPAGES                       // allocate up to 64k of EMS
158                 int     EMS_INT
159                 or      ah,ah
160                 jnz     error
161                 mov     [EMShandle],dx
162                 jmp End
163 error:
164                 mov     errorflag,1
165                 jmp End
166 noEMS:
167 End:
168         }
169         if(errorflag==true)
170         {
171                 err = CPURegs.h.ah;
172                 strcpy(str,"MML_SetupEMS: EMS error 0x");
173                 itoa(err,str2,16);
174                 strcpy(str,str2);
175                 printf("%s\n",str);
176         }
177 }
178
179
180 /*
181 ======================
182 =
183 = MML_ShutdownEMS
184 =
185 =======================
186 */
187
188 void MML_ShutdownEMS (void)
189 {
190         boolean errorflag=false;
191         if (!EMShandle)
192                 return;
193         __asm
194         {
195                 mov     ah,EMS_FREEPAGES
196                 mov     dx,[EMShandle]
197                 int     EMS_INT
198                 or      ah,ah
199                 jz      ok
200                 mov     errorflag,1
201                 ok:
202         }
203         if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!");      //++++ add something
204 }
205
206 /*
207 ====================
208 =
209 = MM_MapEMS
210 =
211 = Maps the 64k of EMS used by memory manager into the page frame
212 = for general use.  This only needs to be called if you are keeping
213 = other things in EMS.
214 =
215 ====================
216 */
217
218 void MM_MapEMS (void)
219 {
220         char    str[80],str2[10];
221         unsigned        err;
222         boolean errorflag=false;
223         int     i;
224         union REGS CPURegs;
225
226         for (i=0;i<EMSpagesmapped;i++)
227         {
228                 __asm
229                 {
230                         mov     ah,EMS_MAPPAGE
231                         mov     bx,[i]                  // logical page
232                         mov     al,bl                   // physical page
233                         mov     dx,[EMShandle]  // handle
234                         int     EMS_INT
235                         or      ah,ah
236                         jnz     error
237                         jmp End
238                         error:
239                         mov     errorflag,1
240                         End:
241                 }
242                 if(errorflag==true)
243                 {
244                         err = CPURegs.h.ah;
245                         strcpy(str,"MM_MapEMS: EMS error 0x");
246                         itoa(err,str2,16);
247                         strcpy(str,str2);
248                         printf("%s\n",str);
249                 }
250         }
251         return;
252 }
253
254 //==========================================================================
255
256 /*
257 ======================
258 =
259 = MML_CheckForXMS
260 =
261 = Check for XMM driver
262 =
263 =======================
264 */
265
266 boolean MML_CheckForXMS (void)
267 {
268         boolean errorflag=false;
269         numUMBs = 0;
270
271         __asm
272         {
273                 mov     ax,0x4300
274                 int     0x2f                            // query status of installed diver
275                 cmp     al,0x80
276                 je      good
277                 mov     errorflag,1
278                 good:
279         }
280         if(errorflag==true) return false;
281         else return true;
282 }
283
284
285 /*
286 ======================
287 =
288 = MML_SetupXMS
289 =
290 = Try to allocate all upper memory block
291 =
292 =======================
293 */
294
295 void MML_SetupXMS (void)
296 {
297         unsigned        base,size;
298
299         __asm
300         {
301                 mov     ax,0x4310
302                 int     0x2f
303                 mov     [WORD PTR XMSaddr],bx
304                 mov     [WORD PTR XMSaddr+2],es         // function pointer to XMS driver
305         }
306 getmemory:
307         __asm
308         {
309                 mov     ah,XMS_ALLOCUMB
310                 mov     dx,0xffff                                       // try for largest block possible
311                 call    [DWORD PTR XMSaddr]
312                 or      ax,ax
313                 jnz     gotone
314
315                 cmp     bl,0xb0                                         // error: smaller UMB is available
316                 jne     done;
317
318                 mov     ah,XMS_ALLOCUMB
319                 call    [DWORD PTR XMSaddr]             // DX holds largest available UMB
320                 or      ax,ax
321                 jz      done                                            // another error...
322
323 gotone:
324                 mov     [base],bx
325                 mov     [size],dx
326 done:
327         }
328         MML_UseSpace (base,size);
329         mminfo.XMSmem += size*16;
330         UMBbase[numUMBs] = base;
331         numUMBs++;
332         if (numUMBs < MAXUMBS)
333                 goto getmemory;
334 }
335
336
337 /*
338 ======================
339 =
340 = MML_ShutdownXMS
341 =
342 ======================
343 */
344
345 void MML_ShutdownXMS (void)
346 {
347         int     i;
348         unsigned        base;
349
350         for (i=0;i<numUMBs;i++)
351         {
352                 base = UMBbase[i];
353                 __asm
354                 {
355                         mov     ah,XMS_FREEUMB
356                         mov     dx,[base]
357                         call    [DWORD PTR XMSaddr]
358                 }
359         }
360 }
361
362 //==========================================================================
363
364 /*
365 ======================
366 =
367 = MML_UseSpace
368 =
369 = Marks a range of paragraphs as usable by the memory manager
370 = This is used to mark space for the near heap, far heap, ems page frame,
371 = and upper memory blocks
372 =
373 ======================
374 */
375
376 void MML_UseSpace (unsigned segstart, unsigned seglength)
377 {
378         mmblocktype far *scan,far *last;
379         unsigned        oldend;
380         long            extra;
381
382         scan = last = mmhead;
383         mmrover = mmhead;               // reset rover to start of memory
384
385 //
386 // search for the block that contains the range of segments
387 //
388         while (scan->start+scan->length < segstart)
389         {
390                 last = scan;
391                 scan = scan->next;
392         }
393
394 //
395 // take the given range out of the block
396 //
397         oldend = scan->start + scan->length;
398         extra = oldend - (segstart+seglength);
399         if (extra < 0)
400         {
401                 printf("MML_UseSpace: Segment spans two blocks!");
402                 return;
403         }
404
405
406         if (segstart == scan->start)
407         {
408                 last->next = scan->next;                        // unlink block
409                 FREEBLOCK(scan);
410                 scan = last;
411         }
412         else
413                 scan->length = segstart-scan->start;    // shorten block
414
415         if (extra > 0)
416         {
417                 GETNEWBLOCK;
418                 mmnew->next = scan->next;
419                 scan->next = mmnew;
420                 mmnew->start = segstart+seglength;
421                 mmnew->length = extra;
422                 mmnew->attributes = LOCKBIT;
423         }
424
425 }
426
427 //==========================================================================
428
429 /*
430 ====================
431 =
432 = MML_ClearBlock
433 =
434 = We are out of blocks, so free a purgable block
435 =
436 ====================
437 */
438
439 void MML_ClearBlock (void)
440 {
441         mmblocktype far *scan,far *last;
442
443         scan = mmhead->next;
444
445         while (scan)
446         {
447                 if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
448                 {
449                         MM_FreePtr(scan->useptr);
450                         return;
451                 }
452                 scan = scan->next;
453         }
454
455         printf("MM_ClearBlock: No purgable blocks!");
456 }
457
458
459 //==========================================================================
460
461 /*
462 ===================
463 =
464 = MM_Startup
465 =
466 = Grabs all space from turbo with malloc/farmalloc
467 = Allocates bufferseg misc buffer
468 =
469 ===================
470 */
471
472 void MM_Startup (void)
473 {
474         int i;
475         dword length;
476         void far        *start;
477         unsigned        segstart,seglength,endfree;
478
479         if (mmstarted)
480                 MM_Shutdown ();
481
482
483         mmstarted = true;
484         bombonerror = true;
485 //
486 // set up the linked list (everything in the free list;
487 //
488         mmhead = NULL;
489         mmfree = &mmblocks[0];
490         for (i=0;i<MAXBLOCKS-1;i++)
491                 mmblocks[i].next = &mmblocks[i+1];
492         mmblocks[i].next = NULL;
493
494 //
495 // locked block of all memory until we punch out free space
496 //
497         GETNEWBLOCK;
498         mmhead = mmnew;                         // this will allways be the first node
499         mmnew->start = 0;
500         mmnew->length = 0xffff;
501         mmnew->attributes = LOCKBIT;
502         mmnew->next = NULL;
503         mmrover = mmhead;
504
505
506 //
507 // get all available near conventional memory segments
508 //
509 //----  length=coreleft();
510         length=_memavl();
511         start = (void far *)(nearheap = malloc(length));
512
513         length -= 16-(FP_OFF(start)&15);
514         length -= SAVENEARHEAP;
515         seglength = length / 16;                        // now in paragraphs
516         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
517         MML_UseSpace (segstart,seglength);
518         mminfo.nearheap = length;
519
520 //
521 // get all available far conventional memory segments
522 //
523 //----  length=farcoreleft();
524         length=_memmax();
525         start = farheap = _fmalloc(length);
526         length -= 16-(FP_OFF(start)&15);
527         length -= SAVEFARHEAP;
528         seglength = length / 16;                        // now in paragraphs
529         segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
530         MML_UseSpace (segstart,seglength);
531         mminfo.farheap = length;
532         mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
533
534
535 //
536 // detect EMS and allocate up to 64K at page frame
537 //
538         mminfo.EMSmem = 0;
539         for (i = 1;i < __argc;i++)
540         {
541                 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
542                         goto emsskip;                           // param NOEMS
543         }
544
545         if (MML_CheckForEMS())
546         {
547                 //printf("EMS1\n");
548                 MML_SetupEMS();                                 // allocate space
549                 //printf("EMS2\n");
550                 MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
551                 //printf("EMS3\n");
552                 MM_MapEMS();                                    // map in used pages
553                 //printf("EMS4\n");
554                 mminfo.EMSmem = EMSpagesmapped*0x4000l;
555         }
556
557 //
558 // detect XMS and get upper memory blocks
559 //
560 emsskip:
561         mminfo.XMSmem = 0;
562         for (i = 1;i < __argc;i++)
563         {
564                 if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
565                         goto xmsskip;                           // param NOXMS
566         }
567
568         if (MML_CheckForXMS())
569         {
570 //              printf("XMS!\n");
571                 MML_SetupXMS();                                 // allocate as many UMBs as possible
572         }
573
574 //
575 // allocate the misc buffer
576 //
577 xmsskip:
578         mmrover = mmhead;               // start looking for space after low block
579
580         MM_GetPtr (&bufferseg,BUFFERSIZE);
581 }
582
583 //==========================================================================
584
585 /*
586 ====================
587 =
588 = MM_Shutdown
589 =
590 = Frees all conventional, EMS, and XMS allocated
591 =
592 ====================
593 */
594
595 void MM_Shutdown (void)
596 {
597   if (!mmstarted)
598         return;
599
600   _ffree (farheap);
601   free (nearheap);
602   hfree(hugeheap);
603   MML_ShutdownEMS ();
604   MML_ShutdownXMS ();
605 }
606
607 //==========================================================================
608
609 /*
610 ====================
611 =
612 = MM_GetPtr
613 =
614 = Allocates an unlocked, unpurgable block
615 =
616 ====================
617 */
618
619 void MM_GetPtr (memptr *baseptr,dword size)
620 {
621         mmblocktype far *scan,far *lastscan,far *endscan
622                                 ,far *purge,far *next;
623         int                     search;
624         unsigned        needed,startseg;
625
626         needed = (size+15)/16;          // convert size from bytes to paragraphs
627
628         GETNEWBLOCK;                            // fill in start and next after a spot is found
629         mmnew->length = needed;
630         mmnew->useptr = baseptr;
631         mmnew->attributes = BASEATTRIBUTES;
632
633         for (search = 0; search<3; search++)
634         {
635         //
636         // first search:        try to allocate right after the rover, then on up
637         // second search:       search from the head pointer up to the rover
638         // third search:        compress memory, then scan from start
639                 if (search == 1 && mmrover == mmhead)
640                         search++;
641
642                 switch (search)
643                 {
644                 case 0:
645                         lastscan = mmrover;
646                         scan = mmrover->next;
647                         endscan = NULL;
648                         break;
649                 case 1:
650                         lastscan = mmhead;
651                         scan = mmhead->next;
652                         endscan = mmrover;
653                         break;
654                 case 2:
655                         MM_SortMem ();
656                         lastscan = mmhead;
657                         scan = mmhead->next;
658                         endscan = NULL;
659                         break;
660                 }
661
662                 startseg = lastscan->start + lastscan->length;
663
664                 while (scan != endscan)
665                 {
666                         if (scan->start - startseg >= needed)
667                         {
668                         //
669                         // got enough space between the end of lastscan and
670                         // the start of scan, so throw out anything in the middle
671                         // and allocate the new block
672                         //
673                                 purge = lastscan->next;
674                                 lastscan->next = mmnew;
675                                 mmnew->start = *(unsigned *)baseptr = startseg;
676                                 mmnew->next = scan;
677                                 while ( purge != scan)
678                                 {       // free the purgable block
679                                         next = purge->next;
680                                         FREEBLOCK(purge);
681                                         purge = next;           // purge another if not at scan
682                                 }
683                                 mmrover = mmnew;
684                                 return; // good allocation!
685                         }
686
687                         //
688                         // if this block is purge level zero or locked, skip past it
689                         //
690                         if ( (scan->attributes & LOCKBIT)
691                                 || !(scan->attributes & PURGEBITS) )
692                         {
693                                 lastscan = scan;
694                                 startseg = lastscan->start + lastscan->length;
695                         }
696
697
698                         scan=scan->next;                // look at next line
699                 }
700         }
701
702         if (bombonerror)
703                 printf(OUT_OF_MEM_MSG,(size-mminfo.nearheap));
704         else
705                 mmerror = true;
706 }
707
708 //==========================================================================
709
710 /*
711 ====================
712 =
713 = MM_FreePtr
714 =
715 = Allocates an unlocked, unpurgable block
716 =
717 ====================
718 */
719
720 void MM_FreePtr (memptr *baseptr)
721 {
722         mmblocktype far *scan,far *last;
723
724         last = mmhead;
725         scan = last->next;
726
727         if (baseptr == mmrover->useptr) // removed the last allocated block
728                 mmrover = mmhead;
729
730         while (scan->useptr != baseptr && scan)
731         {
732                 last = scan;
733                 scan = scan->next;
734         }
735
736         if (!scan)
737         {
738                 printf("MM_FreePtr: Block not found!");
739                 return;
740         }
741
742         last->next = scan->next;
743
744         FREEBLOCK(scan);
745 }
746 //==========================================================================
747
748 /*
749 =====================
750 =
751 = MM_SetPurge
752 =
753 = Sets the purge level for a block (locked blocks cannot be made purgable)
754 =
755 =====================
756 */
757
758 void MM_SetPurge (memptr *baseptr, int purge)
759 {
760         mmblocktype far *start;
761
762         start = mmrover;
763
764         do
765         {
766                 if (mmrover->useptr == baseptr)
767                         break;
768
769                 mmrover = mmrover->next;
770
771                 if (!mmrover)
772                         mmrover = mmhead;
773                 else if (mmrover == start)
774                 {
775                         printf("MM_SetPurge: Block not found!");
776                         return;
777                 }
778
779         } while (1);
780
781         mmrover->attributes &= ~PURGEBITS;
782         mmrover->attributes |= purge;
783 }
784
785 //==========================================================================
786
787 /*
788 =====================
789 =
790 = MM_SetLock
791 =
792 = Locks / unlocks the block
793 =
794 =====================
795 */
796
797 void MM_SetLock (memptr *baseptr, boolean locked)
798 {
799         mmblocktype far *start;
800
801         start = mmrover;
802
803         do
804         {
805                 if (mmrover->useptr == baseptr)
806                         break;
807
808                 mmrover = mmrover->next;
809
810                 if (!mmrover)
811                         mmrover = mmhead;
812                 else if (mmrover == start)
813                 {
814                         printf("MM_SetLock: Block not found!");
815                         return;
816                 }
817
818         } while (1);
819
820         mmrover->attributes &= ~LOCKBIT;
821         mmrover->attributes |= locked*LOCKBIT;
822 }
823
824 //==========================================================================
825
826 /*
827 =====================
828 =
829 = MM_SortMem
830 =
831 = Throws out all purgable stuff and compresses movable blocks
832 =
833 =====================
834 */
835
836 void MM_SortMem (void)
837 {
838         mmblocktype far *scan,far *last,far *next;
839         unsigned        start,length,source,dest,oldborder;
840         int                     playing;
841
842         //
843         // lock down a currently playing sound
844         //
845 /*++++  playing = SD_SoundPlaying ();
846         if (playing)
847         {
848                 switch (SoundMode)
849                 {
850                 case sdm_PC:
851                         playing += STARTPCSOUNDS;
852                         break;
853                 case sdm_AdLib:
854                         playing += STARTADLIBSOUNDS;
855                         break;
856                 }
857                 MM_SetLock(&(memptr)audiosegs[playing],true);
858         }
859
860
861         SD_StopSound();*/
862 //      oldborder = bordercolor;
863 //      VW_ColorBorder (15);
864
865         if (beforesort)
866                 beforesort();
867
868         scan = mmhead;
869
870         last = NULL;            // shut up compiler warning
871
872         while (scan)
873         {
874                 if (scan->attributes & LOCKBIT)
875                 {
876                 //
877                 // block is locked, so try to pile later blocks right after it
878                 //
879                         start = scan->start + scan->length;
880                 }
881                 else
882                 {
883                         if (scan->attributes & PURGEBITS)
884                         {
885                         //
886                         // throw out the purgable block
887                         //
888                                 next = scan->next;
889                                 FREEBLOCK(scan);
890                                 last->next = next;
891                                 scan = next;
892                                 continue;
893                         }
894                         else
895                         {
896                         //
897                         // push the non purgable block on top of the last moved block
898                         //
899                                 if (scan->start != start)
900                                 {
901                                         length = scan->length;
902                                         source = scan->start;
903                                         dest = start;
904                                         while (length > 0xf00)
905                                         {
906                                                 movedata(source,0,dest,0,0xf00*16);
907                                                 length -= 0xf00;
908                                                 source += 0xf00;
909                                                 dest += 0xf00;
910                                         }
911                                         movedata(source,0,dest,0,length*16);
912
913                                         scan->start = start;
914                                         *(unsigned *)scan->useptr = start;
915                                 }
916                                 start = scan->start + scan->length;
917                         }
918                 }
919
920                 last = scan;
921                 scan = scan->next;              // go to next block
922         }
923
924         mmrover = mmhead;
925
926         if (aftersort)
927                 aftersort();
928
929 //      VW_ColorBorder (oldborder);
930
931 /*++++  if (playing)
932                 MM_SetLock(&(memptr)audiosegs[playing],false);*/
933 }
934
935
936 //==========================================================================
937
938 //****#if 0
939 /*
940 =====================
941 =
942 = MM_ShowMemory
943 =
944 =====================
945 */
946
947 void MM_ShowMemory (void)
948 {
949         mmblocktype far *scan;
950         unsigned color,temp;//, i;
951         long    end,owner;
952         char    scratch[80],str[10];
953
954 //****  VW_SetDefaultColors();
955 //****  VW_SetLineWidth(40);
956 //++++mh        temp = bufferofs;
957 //++++mh        bufferofs = 0;
958 //****  VW_SetScreen (0,0);
959
960         scan = mmhead;
961
962         end = -1;
963
964 //CA_OpenDebug ();
965
966         while (scan)
967         {
968                 if (scan->attributes & PURGEBITS)
969                         color = 5;              // dark purple = purgable
970                 else
971                         color = 9;              // medium blue = non purgable
972                 if (scan->attributes & LOCKBIT)
973                         color = 12;             // red = locked
974                 if (scan->start<=end)
975                 {
976                         printf("MM_ShowMemory: Memory block order currupted!");
977                         return;
978                 }
979                 end = scan->start+scan->length-1;
980 //++++          VW_Hlin(scan->start,(unsigned)end,0,color);
981 //++++          VW_Plot(scan->start,0,15);
982                 if (scan->next->start > end+1)
983 //++++                  VW_Hlin(end+1,scan->next->start,0,0);   // black = free
984
985 //****#if 0
986 printf("Location:");
987 printf("%Fp\t", scan->start);
988 strcpy (scratch,"Size:");
989 ltoa ((long)scan->length*16,str,10);
990 strcat (scratch,str);
991 strcat (scratch,"\tOwner:0x");
992 owner = (unsigned)scan->useptr;
993 ultoa (owner,str,16);
994 strcat (scratch,str);
995 strcat (scratch,"\n");
996 //++++write (debughandle,scratch,strlen(scratch));
997 fprintf(stdout, "%s", scratch);
998 //****#endif
999
1000                 scan = scan->next;
1001         }
1002
1003 //CA_CloseDebug ();
1004
1005 //++++mh        IN_Ack();
1006 //****  VW_SetLineWidth(64);
1007 //++++mh        bufferofs = temp;
1008 }
1009 //****#endif
1010
1011 //==========================================================================
1012
1013
1014 /*
1015 ======================
1016 =
1017 = MM_UnusedMemory
1018 =
1019 = Returns the total free space without purging
1020 =
1021 ======================
1022 */
1023
1024 dword MM_UnusedMemory (void)
1025 {
1026         unsigned free;
1027         mmblocktype far *scan;
1028
1029         free = 0;
1030         scan = mmhead;
1031
1032         while (scan->next)
1033         {
1034                 free += scan->next->start - (scan->start + scan->length);
1035                 scan = scan->next;
1036         }
1037
1038         return free*16l;
1039 }
1040
1041 //==========================================================================
1042
1043
1044 /*
1045 ======================
1046 =
1047 = MM_TotalFree
1048 =
1049 = Returns the total free space with purging
1050 =
1051 ======================
1052 */
1053
1054 dword MM_TotalFree (void)
1055 {
1056         unsigned free;
1057         mmblocktype far *scan;
1058
1059         free = 0;
1060         scan = mmhead;
1061
1062         while (scan->next)
1063         {
1064                 if ((scan->attributes&PURGEBITS) && !(scan->attributes&LOCKBIT))
1065                         free += scan->length;
1066                 free += scan->next->start - (scan->start + scan->length);
1067                 scan = scan->next;
1068         }
1069
1070         return free*16l;
1071 }
1072
1073 //==========================================================================
1074
1075 /*
1076 =====================
1077 =
1078 = MM_Report
1079 =
1080 =====================
1081 */
1082
1083 void MM_Report(void)
1084 {
1085         printf("EMM %x available\n", EMSVer);
1086         printf("totalEMSpages=%u\n", totalEMSpages);
1087         printf("freeEMSpages=%u\n", freeEMSpages);
1088         printf("EMSpageframe=%Fp\n", EMSpageframe);
1089         printf("UnusedMemory=%lu\n", MM_UnusedMemory());
1090         printf("TotalFree=%lu\n", MM_TotalFree());
1091 }
1092
1093 //==========================================================================
1094
1095 /*
1096 =====================
1097 =
1098 = MM_EMSVer
1099 =
1100 =====================
1101
1102
1103 int MM_EMSVer(void)\r
1104 {
1105         int EMSver;\r
1106         __asm
1107         {
1108                 mov             ah,EMS_VERSION\r
1109                 int             EMS_INT
1110                 mov             EMSver,ax\r
1111         }
1112         return(EMSver);\r
1113 }*/
1114
1115 //==========================================================================
1116
1117 /*
1118 =====================
1119 =
1120 = MM_BombOnError
1121 =
1122 =====================
1123 */
1124
1125 void MM_BombOnError (boolean bomb)
1126 {
1127         bombonerror = bomb;
1128 }
1129
1130 //==========================================================================
1131
1132 ///////////////////////////////////////////////////////////////////////////
1133 //
1134 //      US_CheckParm() - checks to see if a string matches one of a set of
1135 //              strings. The check is case insensitive. The routine returns the
1136 //              index of the string that matched, or -1 if no matches were found
1137 //
1138 ///////////////////////////////////////////////////////////////////////////
1139 int
1140 US_CheckParm(char *parm,char **strings)
1141 {
1142         char    cp,cs,
1143                         *p,*s;
1144         int             i;
1145
1146         while (!isalpha(*parm)) // Skip non-alphas
1147                 parm++;
1148
1149         for (i = 0;*strings && **strings;i++)
1150         {
1151                 for (s = *strings++,p = parm,cs = cp = 0;cs == cp;)
1152                 {
1153                         cs = *s++;
1154                         if (!cs)
1155                                 return(i);
1156                         cp = *p++;
1157
1158                         if (isupper(cs))
1159                                 cs = tolower(cs);
1160                         if (isupper(cp))
1161                                 cp = tolower(cp);
1162                 }
1163         }
1164         return(-1);
1165 }
1166