]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_mm.c
ok i am working on makin the debug.16 data and tinking abut large amount of data...
[16.git] / src / lib / 16_mm.c
index bf7536fcb341ebdd15da46bdc06c433f768399c8..cc5a894cc3dbfeb8cab2e69ea8b60389e36c2a7a 100644 (file)
@@ -47,9 +47,9 @@ Open Watcom port by sparky4
 
 */
 #include "src/lib/16_mm.h"
-#pragma hdrstop\r
-\r
-#pragma warn -pro\r
+#pragma hdrstop
+
+#pragma warn -pro
 #pragma warn -use
 
 /*
@@ -120,13 +120,24 @@ boolean MML_CheckForEMS(void)
                //
                mov     emmcfems,1
                jmp End
+#ifdef __BORLANDC__
+       }
+#endif
                error:
+#ifdef __BORLANDC__
+       __asm {
+#endif
                //
                // EMS is bad
                //
                mov     emmcfems,0
+#ifdef __BORLANDC__
+       }
+#endif
                End:
+#ifdef __WATCOMC__
        }
+#endif
        return(emmcfems);
 }
 
@@ -150,8 +161,7 @@ byte MML_SetupEMS(mminfo_t *mm)
        unsigned        totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
        totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
 
-       __asm
-               {
+       __asm {
                mov     ah,EMS_STATUS
                int     EMS_INT                                         // make sure EMS hardware is present
                or      ah,ah
@@ -187,13 +197,23 @@ byte MML_SetupEMS(mminfo_t *mm)
                jle     getpages
                mov     bx,[freeEMSpages]
                jmp     getpages
-
-low:
+#ifdef __BORLANDC__
+       }
+#endif
+       low:
+#ifdef __BORLANDC__
+       __asm {
+#endif
                cmp     bx,4
                jle     getpages                                        // there is only 1,2,3,or 4 pages
                mov     bx,4                                            // we can't use more than 4 pages
-
-getpages:
+#ifdef __BORLANDC__
+       }
+#endif
+       getpages:
+#ifdef __BORLANDC__
+       __asm {
+#endif
                mov     [EMSpagesmapped],bx
                mov     ah,EMS_ALLOCPAGES                       // allocate up to 64k of EMS
                int     EMS_INT
@@ -201,19 +221,30 @@ getpages:
                jnz     error
                mov     [EMShandle],dx
                jmp End
-error:
+#ifdef __BORLANDC__
+       }
+#endif
+       error:
+#ifdef __BORLANDC__
+       __asm {
+#endif
                mov     err,ah
                mov     errorflag,1
                jmp End
+#ifdef __BORLANDC__
+       }
+#endif
 noEMS:
 End:
+#ifdef __WATCOMC__
        }
+#endif
        if(errorflag==true)
        {
                //err = CPURegs.h.ah;
                strcpy(str,"MM_SetupEMS: EMS error ");
                //itoa(err,str2,16);
-               MM_EMSerr(&str, err);
+               MM_EMSerr(str, err);
                printf("%s\n",str);
                return err;
        }
@@ -242,16 +273,20 @@ void MML_ShutdownEMS(mminfo_t *mm)
 
        if(!EMShandle)
                return;
-       __asm
-       {
+       __asm {
                mov     ah,EMS_FREEPAGES
                mov     dx,[EMShandle]
                int     EMS_INT
                or      ah,ah
                jz      ok
                mov     errorflag,1
+#ifdef __BORLANDC__
+       }
+#endif
                ok:
+#ifdef __WATCOMC__
        }
+#endif
        if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n");    //++++ add something
 }
 
@@ -278,8 +313,7 @@ byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
 
        for (i=0;i<4/*MAPPAGES*/;i++)
        {
-               __asm
-               {
+               __asm {
                        mov     ah,EMS_MAPPAGE
                        mov     bx,[i]                  // logical page
                        mov     al,bl                   // physical page
@@ -288,11 +322,22 @@ byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
                        or      ah,ah
                        jnz     error
                        jmp End
+#ifdef __BORLANDC__
+               }
+#endif
                        error:
+#ifdef __BORLANDC__
+               __asm {
+#endif
                        mov     err,ah
                        mov     errorflag,1
+#ifdef __BORLANDC__
+               }
+#endif
                        End:
+#ifdef __WATCOMC__
                }
+#endif
                if(errorflag==true)
                {
                        //err = CPURegs.h.ah;
@@ -344,8 +389,7 @@ byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
 
        for (i=0;i<MAPPAGES;i++)
        {
-               __asm
-               {
+               __asm {
                        mov     ah,EMS_MAPXPAGE
                        mov     cx,[i]                  // logical page
                        mov     al,bl                   // physical page
@@ -354,18 +398,29 @@ byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
                        or      ah,ah
                        jnz     error
                        jmp End
+#ifdef __BORLANDC__
+               }
+#endif
                        error:
+#ifdef __BORLANDC__
+               __asm {
+#endif
                        mov     err,ah
                        mov     errorflag,1
+#ifdef __BORLANDC__
+               }
+#endif
                        End:
+#ifdef __WATCOMC__
                }
+#endif
                if(errorflag==true)
                {
                        //err = CPURegs.h.ah;
                        //strcpy(str,"MM_MapXEMS: EMS error 0x");
                        strcpy(str,"MM_MapXEMS: EMS error ");
                        //itoa(err,str2,16);
-                       MM_EMSerr(&str, err);
+                       MM_EMSerr(str, err);
                        printf("%s\n",str);
                        //printf("%s%x\n",str, err);
                        //printf("FACK! %x\n", err);
@@ -393,15 +448,19 @@ boolean MML_CheckForXMS(mminfo_t *mm)
        boolean errorflag=false;
        mm->numUMBs = 0;
 
-       __asm
-       {
+       __asm {
                mov     ax,0x4300
                int     0x2f                            // query status of installed diver
                cmp     al,0x80
                je      good
                mov     errorflag,1
+#ifdef __BORLANDC__
+       }
+#endif
                good:
+#ifdef __WATCOMC__
        }
+#endif
        if(errorflag==true) return false;
        else return true;
 }
@@ -419,11 +478,10 @@ boolean MML_CheckForXMS(mminfo_t *mm)
 
 void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi)
 {
-       unsigned        base,size;
+       word    base,size;
 
 getmemory:
-       __asm
-       {
+       __asm {
                mov     ax,0x4310
                int     0x2f
                mov     [WORD PTR XMSaddr],bx
@@ -443,13 +501,23 @@ getmemory:
                call    [DWORD PTR XMSaddr]             // DX holds largest available UMB
                or      ax,ax
                jz      done                                            // another error...
-
-gotone:
+#ifdef __BORLANDC__
+       }
+#endif
+               gotone:
+#ifdef __BORLANDC__
+       __asm {
+#endif
                mov     [base],bx
                mov     [size],dx
-done:
+#ifdef __BORLANDC__
        }
-       printf("base=%u ", base); printf("size=%u\n", size);
+#endif
+               done:
+#ifdef __WATCOMC__
+       }
+#endif
+//     printf("base=%u ", base); printf("size=%u\n", size);
        MML_UseSpace(base,size, mm);
        mmi->XMSmem += size*16;
        mm->UMBbase[mm->numUMBs] = base;
@@ -475,8 +543,7 @@ void MML_ShutdownXMS(mminfo_t *mm)
        for (i=0;i<mm->numUMBs;i++)
        {
                base = mm->UMBbase[i];
-               __asm
-               {
+               __asm {
                        mov     ah,XMS_FREEUMB
                        mov     dx,[base]
                        call    [DWORD PTR XMSaddr]
@@ -521,7 +588,7 @@ void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
        if(seglength>0xffffu)
        {
 //             segm=seglength/0x4000u;
-               segm=seglength/0xffffu;
+               segm=(word)seglength/0xffffu;
        }
        else segm=1;
 
@@ -543,7 +610,7 @@ void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
                {
                        pop ds
                }*/
-               //printf("MML_UseSpace: Segment spans two blocks!\n");
+               printf("MML_UseSpace: Segment spans two blocks!\n");
        }
 
 //
@@ -565,7 +632,7 @@ printf("seglen=%lu\n", seglength);
        if(segstart == scan->start)
        {
                last->next = scan->next;                        // unlink block
-               MM_FreeBlock(scan, mm);
+               FREEBLOCK(scan);
                scan = last;
        }
        else
@@ -575,7 +642,8 @@ printf("seglen=%lu\n", seglength);
 
        if(extra > 0)
        {
-               MM_GetNewBlock(mm);
+               //MM_GetNewBlock(mm);
+               GETNEWBLOCK;
                mm->mmnew->next = scan->next;
                scan->next = mm->mmnew;
                mm->mmnew->start = segstart+seglength;
@@ -634,15 +702,16 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 {
        int i;
        dword length,seglength;
+       //dword length; word seglength;
        void huge       *start;
-       unsigned        segstart,endfree;
+       word    segstart;//,endfree;
 
-       //printf("mmi->segu=%Fp\n", (mmi->segu));
-       if(mm->mmstarted)
-               MM_Shutdown(mm);
+//     if(mm->mmstarted)
+//             MM_Shutdown(mm);
 
        mm->mmstarted = true;
        mm->bombonerror = true;
+       mm->endid=0;
 
 //
 // set up the linked list (everything in the free list;
@@ -655,106 +724,109 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
                mm->mmblocks[i].next = &(mm->mmblocks[i+1]);
        }
        mm->mmblocks[i].next = NULL;
-       //printf("mmi->segu=%Fp\n", (mmi->segu));
 
 //
 // locked block of all memory until we punch out free space
 //
        //printf("              newblock making!\n");
-       MM_GetNewBlock(mm);
+       //MM_GetNewBlock(mm);
+       GETNEWBLOCK;
        mm->mmhead = mm->mmnew;                         // this will allways be the first node
        mm->mmnew->start = 0;
        mm->mmnew->length = 0xffff;
        mm->mmnew->attributes = LOCKBIT;
        mm->mmnew->next = NULL;
        mm->mmrover = mm->mmhead;
-       //printf("mmi->segu=%Fp\n", (mmi->segu));
 
 //
 // get all available near conventional memory segments
 //
-       //printf("              nearheap making!\n");
-//---- length=coreleft();
-       //_nheapgrow();
-       length=_memmax();
+//     printf("                nearheap making!\n");
+#ifdef __WATCOMC__
+       _nheapgrow();
+       length=(dword)_memavl();//(dword)GetFreeSize();
        start = (void huge *)(mm->nearheap = _nmalloc(length));
+#endif
+#ifdef __BORLANDC__
+       length=coreleft();
+       start = (void huge *)(mm->nearheap = malloc(length));
+#endif
        length -= 16-(FP_OFF(start)&15);
        length -= SAVENEARHEAP;
        seglength = length / 16;                        // now in paragraphs
        segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
        MML_UseSpace(segstart,seglength, mm);
        mmi->nearheap = length;
-       /*switch( _nheapchk() ) {
-               case _HEAPOK:
-                       printf( "OK - nearheap is good\n" );
-               break;
-               case _HEAPEMPTY:
-                       printf( "OK - nearheap is empty\n" );
-               break;
-               case _HEAPBADBEGIN:
-                       printf( "ERROR - nearheap is damaged\n" );
-               break;
-               case _HEAPBADNODE:
-                       printf( "ERROR - bad node in nearheap\n" );
-               break;
-       }*/
-       //printf("              near heap ok!\n");
+       printf("start=%Fp       segstart=%x     seglen=%lu      len=%lu\n", start, segstart, seglength, length);
+       //heapdump();
 
 //
 // get all available far conventional memory segments
 //
-//---- length=farcoreleft();
-       //printf("              farheap making!\n");
+//     printf("                farheap making!\n");
+       printf("_FARCORELEFT                            %lu\n", _FCORELEFT);
+#ifdef __WATCOMC__
        _fheapgrow();
-       length=0xff;//UL*4UL;//_memavl();
-       start = mm->farheap = halloc(length, sizeof(byte));
-       //start = mm->farheap = _fmalloc(length);
+#endif
+#ifdef __BORLANDC__
+       printf("                                %lu\n", farcoreleft());
+       printf("                                %d\n", (sword)((farcoreleft()+32)-_FCORELEFT));
+#endif
+       length=_FCORELEFT;//_fcoreleft();//(dword)GetFarFreeSize();//0xffffUL*4UL;
+       start = mm->farheap = _fmalloc(length);
+       //start = mm->farheap = halloc(length, 1);
        length -= 16-(FP_OFF(start)&15);
        length -= SAVEFARHEAP;
        seglength = length / 16;                        // now in paragraphs
        segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;
        MML_UseSpace(segstart,seglength, mm);
        mmi->farheap = length;
+       printf("start=%Fp       segstart=%x     seglen=%lu      len=%lu\n", start, segstart, seglength, length);
+       //heapdump();
+
        mmi->mainmem = mmi->nearheap + mmi->farheap;
-       /*switch( _fheapchk() ) {
-               case _HEAPOK:
-                       printf( "OK - farheap is good\n" );
-               break;
-               case _HEAPEMPTY:
-                       printf( "OK - farheap is empty\n" );
-               break;
-               case _HEAPBADBEGIN:
-                       printf( "ERROR - farheap is damaged\n" );
-               break;
-               case _HEAPBADNODE:
-                       printf( "ERROR - bad node in farheap\n" );
-               break;
-       }*/
-       //printf("              far heap ok!\n");
+
+//     getch();
+
+//goto xmsskip;
 
 //
 // detect EMS and allocate up to 64K at page frame
 //
-printf("               EMS1\n");
-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!
+//printf("             EMS1\n");
+//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!
        mmi->EMSmem = 0;
-       for(i = 1;i < __argc;i++)
+       for(i = 1;i <
+#ifdef __WATCOMC__
+       __argc
+#endif
+#ifdef __BORLANDC__
+       _argc
+#endif
+       ;i++)
        {
-               if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
+               if(US_CheckParm(
+#ifdef __WATCOMC__
+       __argv[i]
+#endif
+#ifdef __BORLANDC__
+       _argv[i]
+#endif
+                       ,ParmStringsexmm) == 0)
                        goto emsskip;                           // param NOEMS
        }
-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!
+//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!
        if(MML_CheckForEMS())
        {
-printf("               EMS2\n");
-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!
+//printf("             EMS2\n");
+//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!
                MML_SetupEMS(mm);                                       // allocate space
-printf("               EMS3\n");
-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!
+//printf("             EMS3\n");
+//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!
                //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT!
                MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
-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!
-printf("               EMS4\n");
+//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!
+//printf("             EMS4\n");
                //if(mm->EMSVer<0x40)
                        MM_MapEMS(mm, mmi);                                     // map in used pages
                //else
@@ -766,27 +838,38 @@ printf("          EMS4\n");
 //
 emsskip:
        mmi->XMSmem = 0;
-       for(i = 1;i < __argc;i++)
+       goto xmsskip;
+       for(i = 1;i <
+#ifdef __WATCOMC__
+       __argc
+#endif
+#ifdef __BORLANDC__
+       _argc
+#endif
+       ;i++)
        {
-               if(US_CheckParm(__argv[i],ParmStringsexmm) == 0)
+               if(US_CheckParm(
+#ifdef __WATCOMC__
+       __argv[i]
+#endif
+#ifdef __BORLANDC__
+       _argv[i]
+#endif
+                       ,ParmStringsexmm) == 0)
                        goto xmsskip;                           // param NOXMS
        }
-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!
+//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!
        if(MML_CheckForXMS(mm))
        {
-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!
-printf("               XMS!\n");
-               //++++          MML_SetupXMS(mm, mmi);                                  // allocate as many UMBs as possible
+//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!
+//printf("             XMS!\n");
+               MML_SetupXMS(mm, mmi);                                  // allocate as many UMBs as possible
        }
 
 //
 // allocate the misc buffer
 //
 xmsskip:
-/*mmi->nearheap = 0;
-mmi->farheap = 0;
-mmi->EMSmem = 0;
-mmi->XMSmem = 0;*/
        mm->mmrover = mm->mmhead;               // start looking for space after low block
 
        MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi);
@@ -809,14 +892,15 @@ void MM_Shutdown(mminfo_t *mm)
        if(!(mm->mmstarted))
                return;
 
-       _ffree(mm->farheap);
-       printf("                far freed\n");
-       free(mm->nearheap);
-       printf("                near freed\n");
-       //hfree(mm->hugeheap);
-       //printf("huge freed\n");
-       if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); printf("            EMS freed\n"); }
-       if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); printf("          XMS freed\n"); }
+       _ffree(mm->farheap);//  printf("                far freed\n");
+#ifdef __WATCOMC__
+       _nfree(mm->nearheap);// printf("                near freed\n");
+#endif
+#ifdef __BORLANDC__
+       free(mm->nearheap);//   printf("                near freed\n");
+#endif
+       if(MML_CheckForEMS()){ MML_ShutdownEMS(mm); }//printf("         EMS freed\n"); }
+       if(MML_CheckForXMS(mm)){ MML_ShutdownXMS(mm); }//printf("               XMS freed\n"); }
 }
 
 //==========================================================================
@@ -835,17 +919,20 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
 {
        mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
        int                     search;
-       unsigned        needed,startseg;
+       dword   needed;
+       word    startseg;
 
        needed = (size+15)/16;          // convert size from bytes to paragraphs
-
-       MM_GetNewBlock(mm);                             // fill in start and next after a spot is found
+printf(".");   //0000
+       //MM_GetNewBlock(mm);
+       GETNEWBLOCK;                            // fill in start and next after a spot is found
        mm->mmnew->length = needed;
        mm->mmnew->useptr = baseptr;
        mm->mmnew->attributes = BASEATTRIBUTES;
-
-       for(search = 0; search<3; search++)
+printf(".");   //0000
+       for(search = 0; search<mm->endid; search++)
        {
+printf("       [case]");       //0000
        //
        // first search:        try to allocate right after the rover, then on up
        // second search:       search from the head pointer up to the rover
@@ -856,16 +943,19 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
                switch(search)
                {
                case 0:
+printf("0      ");     //0000
                        lastscan = mm->mmrover;
                        scan = mm->mmrover->next;
                        endscan = NULL;
                        break;
                case 1:
+printf("1      ");     //0000
                        lastscan = mm->mmhead;
                        scan = mm->mmhead->next;
                        endscan = mm->mmrover;
                        break;
                case 2:
+printf("2      ");     //0000
                        MM_SortMem(mm);
                        lastscan = mm->mmhead;
                        scan = mm->mmhead->next;
@@ -873,12 +963,14 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
                        break;
                }
 
-               startseg = lastscan->start + lastscan->length;
+               startseg = lastscan->start + (word)lastscan->length;
 
                while(scan != endscan)
                {
+//printf(","); //0000
                        if(scan->start - startseg >= needed)
                        {
+printf(".");   //0000
                        //
                        // got enough space between the end of lastscan and
                        // the start of scan, so throw out anything in the middle
@@ -886,12 +978,14 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
                        //
                                purge = lastscan->next;
                                lastscan->next = mm->mmnew;
-                               mm->mmnew->start = *(unsigned *)baseptr = startseg;
+                               mm->mmnew->start = *(word *)baseptr = startseg;
                                mm->mmnew->next = scan;
                                while(purge != scan)
                                {       // free the purgable block
+printf("               freeing block~\n");     //0000
                                        next = purge->next;
-                                       MM_FreeBlock(purge, mm);
+                                       FREEBLOCK(purge);
+                                       //MM_FreeBlock(purge, mm);
                                        purge = next;           // purge another if not at scan
                                }
                                mm->mmrover = mm->mmnew;
@@ -904,17 +998,28 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
                        if((scan->attributes & LOCKBIT)
                                || !(scan->attributes & PURGEBITS) )
                        {
+printf("       [lock] ");      //0000
+printf("len=%lu ", scan->length);
                                lastscan = scan;
-                               startseg = lastscan->start + lastscan->length;
+                               startseg = lastscan->start + (word)lastscan->length;
                        }
 
-
+printf("\n");
                        scan=scan->next;                // look at next line
                }
        }
 
        if (mm->bombonerror)
+       {
+#ifdef __WATCOMC__
+               //heapdump();
+#endif
                printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
+               printf("for stability reasons the program will shut down! wwww\n");
+               printf("                endid=%u\n",(mm->endid));
+               MM_Shutdown(mm);
+               exit(-1);
+       }
        else
                mm->mmerror = true;
 }
@@ -955,7 +1060,8 @@ void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
 
        last->next = scan->next;
 
-       MM_FreeBlock(scan, mm);
+       FREEBLOCK(scan);
+       //MM_FreeBlock(scan, mm);
 }
 //==========================================================================
 
@@ -1100,7 +1206,8 @@ void MM_SortMem(mminfo_t *mm)
                        // throw out the purgable block
                        //
                                next = scan->next;
-                               MM_FreeBlock(scan, mm);
+                               FREEBLOCK(scan);
+                               //MM_FreeBlock(scan, mm);
                                last->next = next;
                                scan = next;
                                continue;
@@ -1146,10 +1253,8 @@ void MM_SortMem(mminfo_t *mm)
                MM_SetLock(&(memptr)audiosegs[playing],false);*/
 }
 
-
 //==========================================================================
 
-//****#if 0
 /*
 =====================
 =
@@ -1158,105 +1263,120 @@ void MM_SortMem(mminfo_t *mm)
 =====================
 */
 
-void MM_ShowMemory(/*page_t *page, */mminfo_t *mm)
+void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
 {
        mmblocktype huge *scan;
-       word color,temp;
-       long    end,owner;
-       word chx,chy;
-       byte    scratch[160],str[16];
-
+       //byte color;
+       word temp;
+       sdword  end,owner;
+       //word chx,chy;
+       word w;
+       byte    scratch[160],scratch0[4096],str[16];
+       byte d = '#';
 //**** VW_SetDefaultColors();
 //**** VW_SetLineWidth(40);
 //++++mh       temp = bufferofs;
 //++++mh       bufferofs = 0;
 //**** VW_SetScreen (0,0);
-
        scan = mm->mmhead;
-
        end = -1;
 
-CA_OpenDebug ();
-
-       chx=0;
-       chy=0;
-
+       CA_OpenDebug (gvar);
+       w=0;
        while(scan)
        {
+               strcpy(scratch, AARESET);
                if(scan->attributes & PURGEBITS)
-                       color = 5;              // dark purple = purgable
+                       strcpy(scratch0, AAMAGENTA);            // dark purple = purgable
                else
-                       color = 9;              // medium blue = non purgable
+                       strcpy(scratch0, AABLUE);               // medium blue = non purgable
                if(scan->attributes & LOCKBIT)
-                       color = 12;             // red = locked
+                       strcpy(scratch0, AARED);                // red = locked
                if(scan->start<=end)
                {
-                       //printf(");
-                       write(debughandle,"\nMM_ShowMemory: Memory block order currupted!\n",strlen("\nMM_ShowMemory: Memory block order currupted!\n"));
+                       printf("\n%d\n\n", end);
+                       strcat(scratch, "MM_ShowMemory: Memory block order currupted!\n");
+                       strcat(scratch, "End's Size: ");
+                       ultoa (end,str,10);
+                       strcat (scratch,str);
+                       strcat(scratch, "\nscan->start's Size: ");
+                       ultoa (scan->start,str,10);
+                       strcat (scratch,str);
+                       write(gvar->handle.debughandle,scratch,strlen(scratch));
                        //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
-                       return;
+                       break;
                }
-               end = scan->start+scan->length-1;
-               chy = scan->start/320;
-               chx = scan->start%320;
+               end = scan->start+(word)scan->length-1;
+//++++         chy = scan->start/320;
+//++++         chx = scan->start%320;
                                //modexhlin(page, scan->start, (unsigned)end, chy, color);
                                //for(chx=scan->start;chx+4>=(word)end;chx+=4)
                                //{
 //++++                                 modexClearRegion(page, chx, chy, 4, 4, color);
                                //}
-
 //++++         VW_Hlin(scan->start,(unsigned)end,0,color);
-
+//             for(w=0;w>=(word)scan->length/80;w++)
+//             {
+//                     printf("+       %lu\n", w);
+               //ultoa (w,str,10);
+               //strcat (scratch0,str);
+                       strcat(scratch0, "+");
+//             }
+               strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
 //++++         VW_Plot(scan->start,0,15);
 //++++                         modexClearRegion(page, chx, chy, 4, 4, 15);
-               if(scan->next->start > end+1)
 //++++                 VW_Hlin(end+1,scan->next->start,0,0);   // black = free
+               if(scan->next->start > end+1)
+               {
+                       strcat(scratch0, AARESET);
+                       //ultoa (w,str,10);
+                       //strcat (scratch0,str);
+                       strcat(scratch0,AAGREEN);
+//                     for(w=(scan->next->start)/80;w>=(word)((scan->length+1)/80);w++)
+//                     {
+//                             printf("0       %lu\n", w);
+                               strcat(scratch0,"0");
+//                     }
+               }
+               strcat(scratch0, AARESET);
+               strcat(scratch0,"\n");
                        //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
                        //{
-//++++                         chx+=scan->next->start;
-//++++                         modexClearRegion(page, chx, chy, 4, 4, 2);
+//                             chx+=scan->next->start;
+//                             modexClearRegion(page, chx, chy, 4, 4, 2);
                        //}
                                        //modexhlin(page, end+1,scan->next->start, chy, 0);
-
-/*
-               end = scan->length-1;
-               y = scan->start/320;
+/*             y = scan->start/320;
                x = scan->start%320;
                VW_Hlin(x,x+end,y,color);
-               VW_Plot(x,y,15);
-               if (scan->next && scan->next->start > end+1)
-                       VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
-*/
-
-//****#if 0
-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!
-strcpy(scratch,"Seg:");
-ultoa (scan->start,str,16);
-strcat (scratch,str);
-strcat (scratch,"\tSize:");
-ltoa ((dword)scan->length*16,str,10);
-strcat (scratch,str);
-strcat (scratch,"\tOwner:0x");
-owner = (unsigned)scan->useptr;
-ultoa (owner,str,16);
-strcat (scratch,str);
-strcat (scratch,"\n");
-write(debughandle,scratch,strlen(scratch));
+               VW_Plot(x,y,15);*/
+//++++                 VW_Hlin(x+end+1,x+(scan->next->start-scan->start),y,0); // black = free
+               strcat(scratch,"Seg:");
+               ultoa (scan->start,str,16);
+               strcat (scratch,str);
+               strcat (scratch,"\tSize:");
+               ultoa ((dword)scan->length,str,10);
+               strcat (scratch,str);
+               strcat (scratch,"\tOwner:0x");
+               owner = (unsigned)scan->useptr;
+               ultoa (owner,str,16);
+               strcat (scratch,str);
+               strcat (scratch,"\n");
+               write(gvar->handle.debughandle,scratch,strlen(scratch));
+               write(gvar->handle.debughandle,scratch0,strlen(scratch0));
 //modexprint(page, chx, chy, 1, 0, 24, &scratch);
 //++++chy+=4;
 //fprintf(stdout, "%s", scratch);
-//****#endif
 
                scan = scan->next;
        }
 
-CA_CloseDebug ();
+       CA_CloseDebug (gvar);
 
 //++++mh       IN_Ack();
 //**** VW_SetLineWidth(64);
 //++++mh       bufferofs = temp;
 }
-//****#endif
 
 //==========================================================================
 
@@ -1270,17 +1390,16 @@ CA_CloseDebug ();
 
 void MM_DumpData(mminfo_t *mm)
 {
-       mmblocktype far *scan,far *best;
+       mmblocktype huge *scan,huge *best;
        long    lowest,oldlowest;
        word    owner;
        byte    lock,purge;
        FILE    *dumpfile;
 
-
-       //++++free(mm->nearheap);
-       dumpfile = fopen ("mmdump.txt","w");
+       free(mm->nearheap);
+       dumpfile = fopen ("mmdump.16","w");
        if (!dumpfile){
-               printf("MM_DumpData: Couldn't open MMDUMP.TXT!\n");
+               printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
                return;
        }
 
@@ -1321,7 +1440,7 @@ void MM_DumpData(mminfo_t *mm)
        } while (lowest != 0xffff);
 
        fclose(dumpfile);
-       printf("MMDUMP.TXT created.\n");
+       printf("MMDUMP.16 created.\n");
 }
 
 //==========================================================================
@@ -1351,7 +1470,7 @@ dword MM_UnusedMemory(mminfo_t *mm)
                scan = scan->next;
        }
 
-       return free*16l;
+       return free*16lu;
 //     return free;
 }
 
@@ -1384,7 +1503,7 @@ dword MM_TotalFree(mminfo_t *mm)
                scan = scan->next;
        }
 
-       return free*16l;
+       return free*16lu;
 //     return free;
 }
 
@@ -1407,7 +1526,7 @@ void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
                printf("freeEMSpages=%u\n", mm->freeEMSpages);
                printf("EMSpageframe=%x\n", mm->EMSpageframe);
        }
-       if(MML_CheckForXMS(mm)) printf("XMSaddr=%Fp\n", *XMSaddr);
+       if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
        printf("near=%lu\n", mmi->nearheap);
        printf("far=%lu\n", mmi->farheap);
        printf("EMSmem=%lu\n", mmi->EMSmem);
@@ -1415,6 +1534,8 @@ void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
        printf("mainmem=%lu\n", mmi->mainmem);
        printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
        printf("TotalFree=%lu\n", MM_TotalFree(mm));
+       //mmi->nearheap+mmi->farheap+
+       printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem);//+);
 //     printf("\n");
 //     printf("UnusedMemory=%lu kb\n", MM_UnusedMemory()/10248);
 //     printf("TotalFree=%lu kb\n", MM_TotalFree()/10248);
@@ -1566,18 +1687,19 @@ void MM_BombOnError(boolean bomb, mminfo_t *mm)
        mm->bombonerror = bomb;
 }
 
-void MM_GetNewBlock(mminfo_t *mm)
+/*void MM_GetNewBlock(mminfo_t *mm)
 {
        if(!mm->mmfree)
                MML_ClearBlock(mm);
        mm->mmnew=mm->mmfree;
        mm->mmfree=mm->mmfree->next;
-       /*if(!(mm->mmnew=mm->mmfree))
+       if(!(mm->mmnew=mm->mmfree))
        {
-               printf("MM_GETNEWBLOCK: No free blocks!");
+               printf("MM_GETNEWBLOCK: No free blocks!\n");
                return;
        }
-       mm->mmfree=mm->mmfree->next;*/
+       mm->mmfree=mm->mmfree->next;
+       mm->endid++;    //end of list
 }
 
 void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
@@ -1585,12 +1707,12 @@ void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
        x->useptr=NULL;
        x->next=mm->mmfree;
        mm->mmfree=x;
-}
+       mm->endid--;    //end of list
+}*/
 
 void MM_seguin(void)
 {
-       __asm
-       {
+       __asm {
                push    ds
                mov     ax,ds
                inc             ax
@@ -1600,8 +1722,7 @@ void MM_seguin(void)
 
 void MM_segude(void)
 {
-       __asm
-       {
+       __asm {
                pop ds
        }
 }