]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_mm.c
facking etptr is broke!! wwww
[16.git] / src / lib / 16_mm.c
index a784e4fc20d8f3abffb1e0b38b8996d280e9e7e2..09e7d9a7751e21dccb8c047cf9f959de1690e14b 100644 (file)
@@ -47,6 +47,7 @@ Open Watcom port by sparky4
 
 */
 #include "src/lib/16_mm.h"
+#include "src/lib/16_ca.h"
 #pragma hdrstop
 
 #pragma warn -pro
@@ -90,7 +91,6 @@ boolean MML_CheckForEMS(void)
        static char     emmname[] = "EMMXXXX0"; //fix by andrius4669
 //             mov     dx,OFFSET emmname
        __asm {
-               //LEA   DX, emmname     //fix by andrius4669
                mov     dx,OFFSET emmname       //fix by andrius4669
                mov     ax,0x3d00
                int     0x21            // try to open EMMXXXX0 device
@@ -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,28 @@ 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 +271,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 +311,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,23 +320,32 @@ 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;
                        strcpy(str,"MM_MapEMS: EMS error ");
-                       //itoa(err,str2,16);
                        MM_EMSerr(str, err);
                        printf("%s\n",str);
-                       //printf("FACK! %x\n", err);
                        return err;
                }
        }
        mmi->EMSmem = (i)*0x4000lu;
+       //printf("              mmi->EMSmem=%lu\n", mmi->EMSmem);
        return 0;
 }
 
@@ -344,8 +385,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 +394,28 @@ 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 +443,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 +473,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 +496,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__
+       }
+#endif
+               done:
+#ifdef __WATCOMC__
        }
-       printf("base=%u ", base); printf("size=%u\n", size);
+#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 +538,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]
@@ -498,11 +560,12 @@ void MML_ShutdownXMS(mminfo_t *mm)
 ======================
 */
 
-void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
+void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
 {
        mmblocktype huge *scan,huge *last;
-       word            segm;
-       dword   oldend;
+       word            segm=1;
+       word    oldend;
+       dword           segmlen=seglength;
        dword           extra;
 
        scan = last = mm->mmhead;
@@ -515,19 +578,28 @@ void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
        {
                last = scan;
                scan = scan->next;
+               //printf("              start=%x",scan->start);
+               //printf("              seglen=%lu\n", scan->length);
        }
 
        //find out how many blocks it spans!
-       if((word)seglength>0xffffu)
+       //
+       for(;segmlen>0x10000;segmlen-=0x10000)
        {
-//             segm=seglength/0x4000u;
-               segm=(word)seglength/0xffffu;
+               //printf("      seglen=%lu\n", segmlen);
+               segm++;
        }
-       else segm=1;
 
+//
+// take the given range out of the block
+//
+       oldend = scan->start + scan->length;
+       extra = oldend - (segstart+(word)seglength);
        //++++emsver stuff!
-       if(segm>1/*extra>0xfffflu*/)
+       if(segm>1 || extra>=0x10000lu)
+       //if(extra>0xfffflu)
        {
+               scan->blob=segm;
                /*__asm
                {
                        push    ds
@@ -537,45 +609,38 @@ void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
                }*/
 
 
-//MML_UseSpace(?segstart?, ?length?, mm);
+               //MML_UseSpace(segstart, seglength, mm);
 
                /*__asm
                {
                        pop ds
                }*/
-               //printf("MML_UseSpace: Segment spans two blocks!\n");
+               printf("MML_UseSpace: Segment spans two blocks!\n");
+       //}
+       printf("========================================\n");
+       printf("segm=%u         ", segm);
+       printf("ex=%lu  ", extra);
+       printf("old=%u  ", oldend);
+       printf("start+seglen=%lu\n", segstart+seglength);
+       printf("segsta=%x       ", segstart);
+       printf("len=%lu ", scan->length);
+       printf("seglen=%lu      ", seglength);
+       printf("segmlen=%lu\n", segmlen);
+       printf("========================================\n");
        }
-
-//
-// take the given range out of the block
-//
-       oldend = scan->start + scan->length;
-       extra = oldend - (segstart+seglength);
-/*
-printf("segm=%u        ", segm);
-printf("ex=%lu ", extra);
-printf("start+seglen=%lu       ", segstart+seglength);
-printf("len=%u ", scan->length);
-printf("segsta=%x      ", segstart);
-printf("seglen=%lu\n", seglength);
-*/
-//segu:
 //++++todo: linked list of segment!
-//printf("segm=%lu\n", segm);
        if(segstart == scan->start)
        {
                last->next = scan->next;                        // unlink block
-               MM_FreeBlock(scan, mm);
+               FREEBLOCK(scan);
                scan = last;
        }
        else
                scan->length = segstart-scan->start;    // shorten block
 
-//     segm--;
-
        if(extra > 0)
        {
-               MM_GetNewBlock(mm);
+               GETNEWBLOCK;
                mm->mmnew->next = scan->next;
                scan->next = mm->mmnew;
                mm->mmnew->start = segstart+seglength;
@@ -638,8 +703,8 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        void huge       *start;
        word    segstart;//,endfree;
 
-       if(mm->mmstarted)
-               MM_Shutdown(mm);
+//     if(mm->mmstarted)
+//             MM_Shutdown(mm);
 
        mm->mmstarted = true;
        mm->bombonerror = true;
@@ -648,7 +713,6 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 //
 // set up the linked list (everything in the free list;
 //
-       //printf("              linked list making!\n");
        mm->mmhead = NULL;
        mm->mmfree = &(mm->mmblocks[0]);
        for(i=0;i<MAXBLOCKS-1;i++)
@@ -660,8 +724,7 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 //
 // locked block of all memory until we punch out free space
 //
-       //printf("              newblock making!\n");
-       MM_GetNewBlock(mm);
+       GETNEWBLOCK;
        mm->mmhead = mm->mmnew;                         // this will allways be the first node
        mm->mmnew->start = 0;
        mm->mmnew->length = 0xffff;
@@ -672,91 +735,109 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 //
 // get all available near conventional memory segments
 //
-//---- length=coreleft();
-       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;
-       printf("start=%FP       segstart=%X     seglen=%lu      len=%lu\n", start, segstart, seglength, length);
-       //heapdump();
+       //printf("start=%Fp     segstart=%x     seglen=%lu      len=%lu\n", start, segstart, seglength, length);
 
 //
 // get all available far conventional memory segments
 //
-//---- length=farcoreleft();
-       printf("                farheap making!\n");
+       //printf("_FARCORELEFT                          %lu\n", _FCORELEFT);
+#ifdef __WATCOMC__
        _fheapgrow();
-       length=(dword)GetFarFreeSize();//0xffffUL*4UL;
-       //start = mm->farheap = halloc(length, 1);
+#endif
+#ifdef __BORLANDC__
+       printf("farcoreleft()                           %lu\n", farcoreleft());
+       printf("(farcoreleft()+32)-_FCORELEFT   %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();
+       //printf("start=%Fp     segstart=%x     seglen=%lu      len=%lu\n", start, segstart, seglength, length);
 
        mmi->mainmem = mmi->nearheap + mmi->farheap;
 
-//     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!
        mmi->EMSmem = 0;
-       for(i = 1;i < __argc;i++)
+//goto emsskip;        //0000
+       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!
        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!
                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!
                //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");
                //if(mm->EMSVer<0x40)
                        MM_MapEMS(mm, mmi);                                     // map in used pages
                //else
                        //MM_MapXEMS(mm, mmi);                                  // map in used pages
        }
 
-mmi->XMSmem=0;
-goto xmsskip;
-
 //
 // detect XMS and get upper memory blocks
 //
 emsskip:
        mmi->XMSmem = 0;
-       for(i = 1;i < __argc;i++)
+goto xmsskip;//0000
+       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!
        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
        }
 
@@ -786,10 +867,15 @@ void MM_Shutdown(mminfo_t *mm)
        if(!(mm->mmstarted))
                return;
 
-       _ffree(mm->farheap);    printf("                far freed\n");
-       _nfree(mm->nearheap);   printf("                near 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"); }
 }
 
 //==========================================================================
@@ -812,15 +898,15 @@ void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
        word    startseg;
 
        needed = (size+15)/16;          // convert size from bytes to paragraphs
-printf(".");   //0000
-       MM_GetNewBlock(mm);                             // fill in start and next after a spot is found
+//printf("."); //0000
+       GETNEWBLOCK;                            // fill in start and next after a spot is found
        mm->mmnew->length = needed;
        mm->mmnew->useptr = baseptr;
        mm->mmnew->attributes = BASEATTRIBUTES;
-printf(".");   //0000
+//printf("."); //0000
        for(search = 0; search<mm->endid; search++)
        {
-printf("       [case]");       //0000
+printf("\n     [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
@@ -851,11 +937,11 @@ printf("2 ");     //0000
                        break;
                }
 
-               startseg = lastscan->start + (word)lastscan->length;
-
+               startseg = lastscan->start + lastscan->length;
+printf(" %x\n", startseg);
                while(scan != endscan)
                {
-//printf(","); //0000
+printf(",");   //0000
                        if(scan->start - startseg >= needed)
                        {
 printf(".");   //0000
@@ -872,7 +958,7 @@ printf(".");        //0000
                                {       // free the purgable block
 printf("               freeing block~\n");     //0000
                                        next = purge->next;
-                                       MM_FreeBlock(purge, mm);
+                                       FREEBLOCK(purge);
                                        purge = next;           // purge another if not at scan
                                }
                                mm->mmrover = mm->mmnew;
@@ -886,19 +972,29 @@ printf("          freeing block~\n");     //0000
                                || !(scan->attributes & PURGEBITS) )
                        {
 printf("       [lock] ");      //0000
-printf("len=%lu ", scan->length);
                                lastscan = scan;
-                               startseg = lastscan->start + (word)lastscan->length;
+printf("start=%x ", lastscan->start);
+printf("len=%x ", lastscan->length);
+printf("\n");  //0000
+printf("       %x", lastscan->start + lastscan->length);       //0000
+printf("       %x", lastscan->start + (word)lastscan->length); //0000
+printf("       %x", (word)lastscan->start + (word)lastscan->length);   //0000
+printf("\n");  //0000
+                               startseg = (word)lastscan->start + (word)lastscan->length+1;
+printf("startseg =%x   ok", startseg); //0000
                        }
 
-printf("\n");
+printf("       end\n");
                        scan=scan->next;                // look at next line
+printf("boop\n");//0000
                }
        }
 
        if (mm->bombonerror)
        {
-               heapdump();
+#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));
@@ -945,7 +1041,7 @@ void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
 
        last->next = scan->next;
 
-       MM_FreeBlock(scan, mm);
+       FREEBLOCK(scan);
 }
 //==========================================================================
 
@@ -1090,7 +1186,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;
@@ -1136,10 +1233,8 @@ void MM_SortMem(mminfo_t *mm)
                MM_SetLock(&(memptr)audiosegs[playing],false);*/
 }
 
-
 //==========================================================================
 
-//****#if 0
 /*
 =====================
 =
@@ -1148,105 +1243,138 @@ 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];
-
+       word temp;
+       sdword  end,owner;
+       //word chx,chy;
+       word w;
+       dword wwww;
+       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("\nend==%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+(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=(scan->start)/80;w<=end/80;w++)
+               {
+                       //printf("+     %u      %lu\n", w, scan->length);
+                       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
+
+               wwww=(dword)(scan->next->start)-(dword)scan->start;
+               //wwww=(dword)scan->start+(dword)(scan->next->start);
+               if (scan->next && scan->next->start >= end+1)
+               {
+                       strcat(scratch0, AARESET);
+                       strcat(scratch0, "\n");
+                       strcat(scratch0,AAGREEN);
+                       for(w=(end+1)/80;w<=(wwww/80);w++)
+                       {
+                               //printf("0     %x      %u      %lu\n", scan->next->start, w, scan->length);
+                               strcat(scratch0,"0");
+                       }
+                       //printf("==================\n");
+                       //printf("w=%x  start=%x        next=%x end=%u  %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
+                       //printf("==================\n");
+               }/*else {//if(scan->next->start <= scan->start){
+                       scan->next->start=scan->start+0x1000;
+                       wwww=(dword)(scan->next->start)-(dword)scan->start;
+                       strcat(scratch0, AARESET);
+                       strcat(scratch0, "\n");
+                       strcat(scratch0,AAGREEN);
+                       for(w=(end+1);w<=(0x1000/80);w++)
+                       {
+                               //printf("0     %x      %x      %u\n", scan->start, w);
+                               strcat(scratch0,"0");
+                       }
+                       printf("================\n");
+                       printf("w=%x    start=%x        next=%x end=%u  %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
+                       printf("================\n");
+//                     getch();
+               }*/
+               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:");
-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(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 ((word)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
 
 //==========================================================================
 
@@ -1260,14 +1388,13 @@ 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);
+       free(mm->nearheap);
        dumpfile = fopen ("mmdump.16","w");
        if (!dumpfile){
                printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
@@ -1341,8 +1468,8 @@ dword MM_UnusedMemory(mminfo_t *mm)
                scan = scan->next;
        }
 
-//     return free*16l;
-       return free;
+       return free*16lu;
+//     return free;
 }
 
 //==========================================================================
@@ -1374,8 +1501,8 @@ dword MM_TotalFree(mminfo_t *mm)
                scan = scan->next;
        }
 
-//     return free*16l;
-       return free;
+       return free*16lu;
+//     return free;
 }
 
 //==========================================================================
@@ -1405,11 +1532,7 @@ 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);
+       printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem+mmi->XMSmem);
 }
 
 //==========================================================================
@@ -1558,7 +1681,7 @@ 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);
@@ -1579,9 +1702,9 @@ void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
        x->next=mm->mmfree;
        mm->mmfree=x;
        mm->endid--;    //end of list
-}
+}*/
 
-void MM_seguin(void)
+/*void MM_seguin(void)
 {
        __asm {
                push    ds
@@ -1596,7 +1719,7 @@ void MM_segude(void)
        __asm {
                pop ds
        }
-}
+}*/
 
 /*
 pull data from far and put it into ds var