]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_mm.c
ok!
[16.git] / src / lib / 16_mm.c
index d39e2683456fdefce413a63c795d469dcd8d42d6..fda0a9ddc8aa5eba65ab9f0efb5386426cac163e 100644 (file)
@@ -83,10 +83,11 @@ static      char *ParmStringsexmm[] = {"noems","noxms",""};
 boolean MML_CheckForEMS(void)
 {
        boolean emmcfems;
-       char    emmname[] = "EMMXXXX0";
+       static char     emmname[] = "EMMXXXX0"; //fix by andrius4669
 //             mov     dx,OFFSET emmname
        __asm {
-               LEA     DX, emmname     //fix by andrius4669
+               //LEA   DX, emmname     //fix by andrius4669
+               mov     dx,OFFSET emmname       //fix by andrius4669
                mov     ax,0x3d00
                int     0x21            // try to open EMMXXXX0 device
                jc      error
@@ -134,14 +135,14 @@ boolean MML_CheckForEMS(void)
 =======================
 */
 
-unsigned MML_SetupEMS(mminfo_t *mm)
+byte MML_SetupEMS(mminfo_t *mm)
 {
-       char    str[80],str2[10];
-       unsigned        err;
+       char    str[80];//,str2[10];
+       byte    err;
        boolean errorflag=false;
-       union REGS CPURegs;
 
        unsigned int EMSVer = 0;
+       //byte  EMS_status;
        unsigned        totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
        totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
 
@@ -150,6 +151,7 @@ unsigned MML_SetupEMS(mminfo_t *mm)
                mov     ah,EMS_STATUS
                int     EMS_INT                                         // make sure EMS hardware is present
                or      ah,ah
+               //mov   [EMS_status],ah
                jnz     error
 
                mov     ah,EMS_VERSION
@@ -174,7 +176,15 @@ unsigned MML_SetupEMS(mminfo_t *mm)
                mov     [freeEMSpages],bx
                or      bx,bx
                jz      noEMS                                           // no EMS at all to allocate
-
+                                                                                       //EXPAND DONG!!!!
+               cmp     [EMSVer],0x40
+               jb      low
+               cmp     bx,[freeEMSpages]
+               jle     getpages
+               mov     bx,[freeEMSpages]
+               jmp     getpages
+
+low:
                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
@@ -188,6 +198,7 @@ getpages:
                mov     [EMShandle],dx
                jmp End
 error:
+               mov     err,ah
                mov     errorflag,1
                jmp End
 noEMS:
@@ -195,11 +206,12 @@ End:
        }
        if(errorflag==true)
        {
-               err = CPURegs.h.ah;
+               //err = CPURegs.h.ah;
                strcpy(str,"MML_SetupEMS: EMS error 0x");
-               itoa(err,str2,16);
-               strcpy(str,str2);
-               printf("%s\n",str);
+               //itoa(err,str2,16);
+               //strcat(str,&err);
+               //printf("%s\n",str);
+               printf("%s%x\n",str,err);
                return err;
        }
        mm->totalEMSpages=totalEMSpages;
@@ -237,7 +249,7 @@ void MML_ShutdownEMS(mminfo_t *mm)
                mov     errorflag,1
                ok:
        }
-       if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!");      //++++ add something
+       if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!\n");    //++++ add something
 }
 
 /*
@@ -252,16 +264,16 @@ void MML_ShutdownEMS(mminfo_t *mm)
 ====================
 */
 
-unsigned MM_MapEMS(mminfo_t *mm)
+byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi)
 {
-       char    str[80],str2[10];
-       unsigned        err, EMShandle;
+       char    str[80];
+       unsigned        EMShandle;
+       byte err;
        boolean errorflag=false;
        int     i;
-       union REGS CPURegs;
        EMShandle=mm->EMShandle;
 
-       for (i=0;i<mm->EMSpagesmapped;i++)
+       for (i=0;i<4/*MAPPAGES*/;i++)
        {
                __asm
                {
@@ -274,19 +286,91 @@ unsigned MM_MapEMS(mminfo_t *mm)
                        jnz     error
                        jmp End
                        error:
+                       mov     err,ah
                        mov     errorflag,1
                        End:
                }
                if(errorflag==true)
                {
-                       err = CPURegs.h.ah;
+                       //err = CPURegs.h.ah;
                        strcpy(str,"MM_MapEMS: EMS error 0x");
-                       itoa(err,str2,16);
-                       strcpy(str,str2);
-                       printf("%s\n",str);
+                       //itoa(err,str2,16);
+                       //strcat(str,&err);
+                       //printf("%s\n",str);
+                       printf("%s%x\n",str, err);
+                       //printf("FACK! %x\n", err);
+                       return err;
+               }
+       }
+       mmi->EMSmem = (i)*0x4000lu;
+       return 0;
+}
+
+byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi)
+{
+
+//SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle)\r
+\r
+       //Maps up to 4 logical EMS pages to physical pages in the page frame, where:\r
+       //PhysicalStart = Physical page first logical page is mapped to\r
+       //LogicalStart  = First logical page to map\r
+       //NumPages      = Number of pages to map (1 to 4)\r
+       //Handle        = EMS handle logical pages are allocated to\r
+\r
+  /*//Create a buffer containing the page information\r
+//  FOR x = 0 TO NumPages - 1\r
+//    MapInfo$ = MapInfo$ + MKI$(LogicalStart + x) + MKI$(PhysicalStart + x)\r
+//  NEXT*/\r
+\r
+//  Regs.ax = &H5000                           //Map the pages in the buffer\r
+//  Regs.cx = NumPages                         //to the pageframe\r
+//  Regs.dx = Handle\r
+//  Regs.ds = VARSEG(MapInfo$)\r
+//  Regs.si = SADD(MapInfo$)\r
+//  InterruptX &H67, Regs, Regs\r
+//     EMS.Error = (Regs.ax AND &HFF00&) \ &H100  //Store the status code\r
+\r
+//END SUB
+       char    str[80];
+       byte err;
+       word    EMShandle;
+       boolean errorflag=false;
+       int     i;
+       EMShandle=mm->EMShandle;
+
+       if(mm->EMSVer<0x40)
+               return 5;
+
+       for (i=0;i<MAPPAGES;i++)
+       {
+               __asm
+               {
+                       mov     ax,EMS_MAPXPAGE
+                       mov     cx,[i]                  // logical page
+                       mov     al,bl                   // physical page
+                       mov     dx,[EMShandle]  // handle
+                       int     EMS_INT
+                       or      ah,ah
+                       jnz     error
+                       jmp End
+                       error:
+                       mov     err,ah
+                       mov     errorflag,1
+                       End:
+               }
+               if(errorflag==true)
+               {
+                       //err = CPURegs.h.ah;
+                       strcpy(str,"MM_MapXEMS: EMS error 0x");
+                       //itoa(err,str2,16);
+                       //strcat(str,&err);
+                       //printf("%s\n",str);
+                       printf("%s%x\n",str, err);
+                       //printf("FACK! %x\n", err);
                        return err;
                }
        }
+       mmi->EMSmem = (i)*0x4000lu;
        return 0;
 }
 
@@ -345,6 +429,7 @@ getmemory:
 
                mov     ah,XMS_ALLOCUMB
                mov     dx,0xffff                                       // try for largest block possible
+               //mov     ax,dx                                         // Set available Kbytes.
                call    [DWORD PTR XMSaddr]
                or      ax,ax
                jnz     gotone
@@ -362,6 +447,7 @@ gotone:
                mov     [size],dx
 done:
        }
+       printf("base=%u ", base); printf("size=%u\n", size);
        MML_UseSpace(base,size, mm);
        mmi->XMSmem += size*16;
        mm->UMBbase[mm->numUMBs] = base;
@@ -410,12 +496,12 @@ void MML_ShutdownXMS(mminfo_t *mm)
 ======================
 */
 
-void MML_UseSpace(unsigned segstart, unsigned seglength, mminfo_t *mm)
+void MML_UseSpace(/*d*/word segstart, dword seglength, mminfo_t *mm)
 {
        mmblocktype huge *scan,huge *last;
-       unsigned        oldend;
-       //++++if(mm->EMSVer)
-       long            extra;
+       word            segm;
+       dword   oldend;
+       dword           extra;
 
        scan = last = mm->mmhead;
        mm->mmrover = mm->mmhead;               // reset rover to start of memory
@@ -429,19 +515,51 @@ void MML_UseSpace(unsigned segstart, unsigned seglength, mminfo_t *mm)
                scan = scan->next;
        }
 
+       //find out how many blocks it spans!
+       if(seglength>0xffffu)
+       {
+//             segm=seglength/0x4000u;
+               segm=seglength/0xffffu;
+       }
+       else segm=1;
+
+       //++++emsver stuff!
+       if(segm>1/*extra>0xfffflu*/)
+       {
+               /*__asm
+               {
+                       push    ds
+                       mov     ax,ds
+                       inc             ax
+                       mov     ds,ax
+               }*/
+
+
+//MML_UseSpace(?segstart?, ?length?, mm);
+
+               /*__asm
+               {
+                       pop ds
+               }*/
+               //printf("MML_UseSpace: Segment spans two blocks!\n");
+       }
+
 //
 // take the given range out of the block
 //
        oldend = scan->start + scan->length;
        extra = oldend - (segstart+seglength);
-       //++++emsver stuff!
-       if(extra < 0)
-       {
-               printf("MML_UseSpace: Segment spans two blocks!");
-               return;
-       }
 
+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
@@ -451,6 +569,8 @@ void MML_UseSpace(unsigned segstart, unsigned seglength, mminfo_t *mm)
        else
                scan->length = segstart-scan->start;    // shorten block
 
+//     segm--;
+
        if(extra > 0)
        {
                MM_GetNewBlock(mm);
@@ -459,7 +579,7 @@ void MML_UseSpace(unsigned segstart, unsigned seglength, mminfo_t *mm)
                mm->mmnew->start = segstart+seglength;
                mm->mmnew->length = extra;
                mm->mmnew->attributes = LOCKBIT;
-       }
+       }//else if(segm>0) goto segu;
 
 }
 
@@ -491,7 +611,7 @@ void MML_ClearBlock(mminfo_t *mm)
                scan = scan->next;
        }
 
-       printf("MM_ClearBlock: No purgable blocks!");
+       printf("MM_ClearBlock: No purgable blocks!\n");
 }
 
 
@@ -511,7 +631,7 @@ void MML_ClearBlock(mminfo_t *mm)
 void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 {
        int i;
-       dword length;//,farlen;
+       dword length;
        void huge       *start;
        unsigned        segstart,seglength,endfree;
 
@@ -537,7 +657,7 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        MM_GetNewBlock(mm);
        mm->mmhead = mm->mmnew;                         // this will allways be the first node
        mm->mmnew->start = 0;
-       mm->mmnew->length = 0xffff;                     //todo: mm make it fucking massive as fuck!~
+       mm->mmnew->length = 0xffff;
        mm->mmnew->attributes = LOCKBIT;
        mm->mmnew->next = NULL;
        mm->mmrover = mm->mmhead;
@@ -551,13 +671,13 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        _nheapgrow();
        length=_memavl();
        start = (void huge *)(mm->nearheap = malloc(length));
-
        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("near heap ok!\n");
 
 //
 // get all available far conventional memory segments
@@ -565,8 +685,7 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
 //---- length=farcoreleft();
        _fheapgrow();
        length=_memavl();
-       //length-=farlen;
-       start = mm->farheap = halloc(length, sizeof(length));
+       start = mm->farheap = halloc(length, sizeof(byte));
        //start = mm->farheap = _fmalloc(length);
        length -= 16-(FP_OFF(start)&15);
        length -= SAVEFARHEAP;
@@ -575,11 +694,13 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        MML_UseSpace(segstart,seglength, mm);
        mmi->farheap = length;
        mmi->mainmem = mmi->nearheap + mmi->farheap;
+       //printf("far heap ok!\n");
 
 
 //
 // detect EMS and allocate up to 64K at page frame
 //
+       printf("EMS1\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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++)
        {
@@ -589,15 +710,17 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi)
        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("EMS1\n");
+printf("EMS2\n");
                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,mm->EMSpagesmapped*0x400, mm);
-//printf("EMS3\n");
-               MM_MapEMS(mm);                                  // map in used pages
-//printf("EMS4\n");
-               mmi->EMSmem = mm->EMSpagesmapped*0x4000l;
+               MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm);
+printf("EMS4\n");
+               if(mm->EMSVer<0x40)
+                       MM_MapEMS(mm, mmi);                                     // map in used pages
+               else
+                       MM_MapXEMS(mm, mmi);                                    // map in used pages
        }
 
 //
@@ -610,11 +733,11 @@ emsskip:
                if(US_CheckParm(__argv[i],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("XMS!\n");
-               MML_SetupXMS(mm, mmi);                                  // allocate as many UMBs as possible
+               printf("XMS!\n");
+               //MML_SetupXMS(mm, mmi);                                        // allocate as many UMBs as possible
        }
 
 //
@@ -783,7 +906,7 @@ void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
 
        if(!scan)
        {
-               printf("MM_FreePtr: Block not found!");
+               printf("MM_FreePtr: Block not found!\n");
                return;
        }
 
@@ -992,12 +1115,13 @@ void MM_SortMem(mminfo_t *mm)
 =====================
 */
 
-void MM_ShowMemory(mminfo_t *mm)
+void MM_ShowMemory(page_t *page, mminfo_t *mm)
 {
        mmblocktype huge *scan;
-       unsigned color,temp;//, i;
+       word color,temp;
        long    end,owner;
-       char    scratch[80],str[10];
+       word chx,chy;
+       byte    scratch[160],str[16];
 
 //**** VW_SetDefaultColors();
 //**** VW_SetLineWidth(40);
@@ -1009,9 +1133,12 @@ void MM_ShowMemory(mminfo_t *mm)
 
        end = -1;
 
-//CA_OpenDebug ();
+CA_OpenDebug ();
+
+       chx=0;
+       chy=0;
 
-       while (scan)
+       while(scan)
        {
                if(scan->attributes & PURGEBITS)
                        color = 5;              // dark purple = purgable
@@ -1021,19 +1148,49 @@ void MM_ShowMemory(mminfo_t *mm)
                        color = 12;             // red = locked
                if(scan->start<=end)
                {
-                       printf("MM_ShowMemory: Memory block order currupted!");
+                       //printf(");
+                       write(debughandle,"\nMM_ShowMemory: Memory block order currupted!\n",strlen("\nMM_ShowMemory: Memory block order currupted!\n"));
+                       //modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
                        return;
                }
                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);
+
 //++++         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
+                       //for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
+                       //{
+                               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;
+               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("Location:");
-printf("%x\t", scan->start);
-strcpy (scratch,"Size:");
+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");
@@ -1041,14 +1198,16 @@ owner = (unsigned)scan->useptr;
 ultoa (owner,str,16);
 strcat (scratch,str);
 strcat (scratch,"\n");
-//++++write (debughandle,scratch,strlen(scratch));
-fprintf(stdout, "%s", scratch);
+write(debughandle,scratch,strlen(scratch));
+//modexprint(page, chx, chy, 1, 0, 24, &scratch);
+chy+=4;
+//fprintf(stdout, "%s", scratch);
 //****#endif
 
                scan = scan->next;
        }
 
-//CA_CloseDebug ();
+CA_CloseDebug ();
 
 //++++mh       IN_Ack();
 //**** VW_SetLineWidth(64);
@@ -1058,6 +1217,72 @@ fprintf(stdout, "%s", scratch);
 
 //==========================================================================
 
+/*
+=====================
+=
+= MM_DumpData
+=
+=====================
+*/
+
+void MM_DumpData(mminfo_t *mm)
+{
+       mmblocktype far *scan,far *best;
+       long    lowest,oldlowest;
+       word    owner;
+       byte    lock,purge;
+       FILE    *dumpfile;
+
+
+       free (mm->nearheap);
+       dumpfile = fopen ("mmdump.txt","w");
+       if (!dumpfile){
+               printf("MM_DumpData: Couldn't open MMDUMP.TXT!\n");
+               return;
+       }
+
+       lowest = -1;
+       do
+       {
+               oldlowest = lowest;
+               lowest = 0xffff;
+
+               scan = mm->mmhead;
+               while (scan)
+               {
+                       owner = (word)scan->useptr;
+
+                       if (owner && owner<lowest && owner > oldlowest)
+                       {
+                               best = scan;
+                               lowest = owner;
+                       }
+
+                       scan = scan->next;
+               }
+
+               if (lowest != 0xffff)
+               {
+                       if (best->attributes & PURGEBITS)
+                               purge = 'P';
+                       else
+                               purge = '-';
+                       if (best->attributes & LOCKBIT)
+                               lock = 'L';
+                       else
+                               lock = '-';
+                       fprintf (dumpfile,"0x%p (%c%c) = %u\n"
+                       ,(word)lowest,lock,purge,best->length);
+               }
+
+       } while (lowest != 0xffff);
+
+       fclose (dumpfile);
+       printf("MMDUMP.TXT created.\n");
+}
+
+//==========================================================================
+
 
 /*
 ======================
@@ -1071,7 +1296,7 @@ fprintf(stdout, "%s", scratch);
 
 dword MM_UnusedMemory(mminfo_t *mm)
 {
-       unsigned free;
+       dword free;
        mmblocktype huge *scan;
 
        free = 0;
@@ -1102,7 +1327,7 @@ dword MM_UnusedMemory(mminfo_t *mm)
 
 dword MM_TotalFree(mminfo_t *mm)
 {
-       unsigned free;
+       dword free;
        mmblocktype huge *scan;
 
        free = 0;
@@ -1130,16 +1355,16 @@ dword MM_TotalFree(mminfo_t *mm)
 =====================
 */
 
-void MM_Report(mminfo_t *mm, mminfotype *mmi)
+void MM_Report(page_t *page, mminfo_t *mm, mminfotype *mmi)
 {
        if(MML_CheckForEMS())
        {
-               printf("EMM %x available\n", mm->EMSVer);
+               printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
                printf("totalEMSpages=%u\n", mm->totalEMSpages);
                printf("freeEMSpages=%u\n", mm->freeEMSpages);
                printf("EMSpageframe=%x\n", mm->EMSpageframe);
        }
-       if(MML_CheckForXMS(mm)) printf("XMSaddr=%x\n", *XMSaddr);
+       if(MML_CheckForXMS(mm)) printf("XMSaddr=%Fp\n", *XMSaddr);
        printf("near=%lu\n", mmi->nearheap);
        printf("far=%lu\n", mmi->farheap);
        printf("EMSmem=%lu\n", mmi->EMSmem);
@@ -1209,3 +1434,33 @@ void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
        x->next=mm->mmfree;
        mm->mmfree=x;
 }
+
+void MM_seguin(void)
+{
+       __asm
+       {
+               push    ds
+               mov     ax,ds
+               inc             ax
+               mov     ds,ax
+       }
+}
+
+void MM_segude(void)
+{
+       __asm
+       {
+               pop ds
+       }
+}
+
+/*
+pull data from far and put it into ds var
+mov ax,es:si
+mov x,ax
+*/
+/*
+ss stack segment
+sp top of stack
+bp bottem of stack
+*/