-/* Catacomb Armageddon Source Code
- * Copyright (C) 1993-2014 Flat Rock Software
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-// NEWMM.C
-
-/*
-=============================================================================
-
- ID software memory manager
- --------------------------
-
-Primary coder: John Carmack
-
-RELIES ON
----------
-Quit (char *error) function
-
-
-WORK TO DO
-----------
-MM_SizePtr to change the size of a given pointer
-
-Multiple purge levels utilized
-
-EMS / XMS unmanaged routines
-
-=============================================================================
-*/
-
-#include "src/lib/16_mm.h"
-
-/*
-======================
-=
-= MML_CheckForEMS
-=
-= Routine from p36 of Extending DOS
-=
-=======================
-*/
-
-boolean MML_CheckForEMS (void)
-{
- boolean emmcfems;
- char emmname[] = "EMMXXXX0";
-// mov dx,OFFSET emmname
- __asm {
- LEA DX, emmname //fix by andrius4669
- mov ax,0x3d00
- int 0x21 // try to open EMMXXXX0 device
- jc error
-
- mov bx,ax
- mov ax,0x4400
-
- int 0x21 // get device info
- jc error
-
- and dx,0x80
- jz error
-
- mov ax,0x4407
-
- int 0x21 // get status
- jc error
- or al,al
- jz error
-
- mov ah,0x3e
- int 0x21 // close handle
- jc error
- //
- // EMS is good
- //
- mov emmcfems,1
- jmp End
- error:
- //
- // EMS is bad
- //
- mov emmcfems,0
- End:
- }
- return(emmcfems);
-}
-
-
-/*
-======================
-=
-= MML_SetupEMS
-=
-=======================
-*/
-
-void MML_SetupEMS (void)
-{
- char str[80],str2[10];
- unsigned err;
- boolean errorflag=false;
- union REGS CPURegs;
-
- totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;
-
- __asm
- {
- mov ah,EMS_STATUS
- int EMS_INT // make sure EMS hardware is present
- or ah,ah
- jnz error
-
- mov ah,EMS_VERSION
- int EMS_INT
- or ah,ah
- jnz error
- cmp al,0x32 // only work on ems 3.2 or greater
- jb error
-
- mov ah,EMS_GETFRAME
- int EMS_INT // find the page frame address
- or ah,ah
- jnz error
- mov [EMSpageframe],bx
-
- mov ah,EMS_GETPAGES
- int EMS_INT // find out how much EMS is there
- or ah,ah
- jnz error
- mov [totalEMSpages],dx
- mov [freeEMSpages],bx
- or bx,bx
- jz noEMS // no EMS at all to allocate
-
- 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:
- mov [EMSpagesmapped],bx
- mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS
- int EMS_INT
- or ah,ah
- jnz error
- mov [EMShandle],dx
- jmp End
-error:
- mov errorflag,1
- jmp End
-noEMS:
-End:
- }
- if(errorflag==true)
- {
- err = CPURegs.h.ah;
- strcpy(str,"MML_SetupEMS: EMS error 0x");
- itoa(err,str2,16);
- strcpy(str,str2);
- printf("%s\n",str);
- }
-}
-
-
-/*
-======================
-=
-= MML_ShutdownEMS
-=
-=======================
-*/
-
-void MML_ShutdownEMS (void)
-{
- boolean errorflag=false;
- if (!EMShandle)
- return;
- __asm
- {
- mov ah,EMS_FREEPAGES
- mov dx,[EMShandle]
- int EMS_INT
- or ah,ah
- jz ok
- mov errorflag,1
- ok:
- }
- if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something
-}
-
-/*
-====================
-=
-= MM_MapEMS
-=
-= Maps the 64k of EMS used by memory manager into the page frame
-= for general use. This only needs to be called if you are keeping
-= other things in EMS.
-=
-====================
-*/
-
-void MM_MapEMS (void)
-{
- char str[80],str2[10];
- unsigned err;
- boolean errorflag=false;
- int i;
- union REGS CPURegs;
-
- for (i=0;i<EMSpagesmapped;i++)
- {
- __asm
- {
- mov ah,EMS_MAPPAGE
- mov bx,[i] // logical page
- mov al,bl // physical page
- mov dx,[EMShandle] // handle
- int EMS_INT
- or ah,ah
- jnz error
- jmp End
- error:
- mov errorflag,1
- End:
- }
- if(errorflag==true)
- {
- err = CPURegs.h.ah;
- strcpy(str,"MM_MapEMS: EMS error 0x");
- itoa(err,str2,16);
- strcpy(str,str2);
- printf("%s\n",str);
- }
- }
- return;
-}
-
-//==========================================================================
-
-/*
-======================
-=
-= MML_CheckForXMS
-=
-= Check for XMM driver
-=
-=======================
-*/
-
-boolean MML_CheckForXMS (void)
-{
- boolean errorflag=false;
- numUMBs = 0;
-
- __asm
- {
- mov ax,0x4300
- int 0x2f // query status of installed diver
- cmp al,0x80
- je good
- mov errorflag,1
- good:
- }
- if(errorflag==true) return false;
- else return true;
-}
-
-
-/*
-======================
-=
-= MML_SetupXMS
-=
-= Try to allocate all upper memory block
-=
-=======================
-*/
-
-void MML_SetupXMS (void)
-{
- unsigned base,size;
-
- __asm
- {
- mov ax,0x4310
- int 0x2f
- mov [WORD PTR XMSaddr],bx
- mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver
- }
-getmemory:
- __asm
- {
- mov ah,XMS_ALLOCUMB
- mov dx,0xffff // try for largest block possible
- call [DWORD PTR XMSaddr]
- or ax,ax
- jnz gotone
-
- cmp bl,0xb0 // error: smaller UMB is available
- jne done;
-
- mov ah,XMS_ALLOCUMB
- call [DWORD PTR XMSaddr] // DX holds largest available UMB
- or ax,ax
- jz done // another error...
-
-gotone:
- mov [base],bx
- mov [size],dx
-done:
- }
- MML_UseSpace (base,size);
- mminfo.XMSmem += size*16;
- UMBbase[numUMBs] = base;
- numUMBs++;
- if (numUMBs < MAXUMBS)
- goto getmemory;
-}
-
-
-/*
-======================
-=
-= MML_ShutdownXMS
-=
-======================
-*/
-
-void MML_ShutdownXMS (void)
-{
- int i;
- unsigned base;
-
- for (i=0;i<numUMBs;i++)
- {
- base = UMBbase[i];
- __asm
- {
- mov ah,XMS_FREEUMB
- mov dx,[base]
- call [DWORD PTR XMSaddr]
- }
- }
-}
-
-//==========================================================================
-
-/*
-======================
-=
-= MML_UseSpace
-=
-= Marks a range of paragraphs as usable by the memory manager
-= This is used to mark space for the near heap, far heap, ems page frame,
-= and upper memory blocks
-=
-======================
-*/
-
-void MML_UseSpace (unsigned segstart, unsigned seglength)
-{
- mmblocktype far *scan,far *last;
- unsigned oldend;
- long extra;
-
- scan = last = mmhead;
- mmrover = mmhead; // reset rover to start of memory
-
-//
-// search for the block that contains the range of segments
-//
- while (scan->start+scan->length < segstart)
- {
- last = scan;
- scan = scan->next;
- }
-
-//
-// take the given range out of the block
-//
- oldend = scan->start + scan->length;
- extra = oldend - (segstart+seglength);
- if (extra < 0)
- {
- printf("MML_UseSpace: Segment spans two blocks!");
- return;
- }
-
-
- if (segstart == scan->start)
- {
- last->next = scan->next; // unlink block
- FREEBLOCK(scan);
- scan = last;
- }
- else
- scan->length = segstart-scan->start; // shorten block
-
- if (extra > 0)
- {
- GETNEWBLOCK;
- mmnew->next = scan->next;
- scan->next = mmnew;
- mmnew->start = segstart+seglength;
- mmnew->length = extra;
- mmnew->attributes = LOCKBIT;
- }
-
-}
-
-//==========================================================================
-
-/*
-====================
-=
-= MML_ClearBlock
-=
-= We are out of blocks, so free a purgable block
-=
-====================
-*/
-
-void MML_ClearBlock (void)
-{
- mmblocktype far *scan,far *last;
-
- scan = mmhead->next;
-
- while (scan)
- {
- if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )
- {
- MM_FreePtr(scan->useptr);
- return;
- }
- scan = scan->next;
- }
-
- printf("MM_ClearBlock: No purgable blocks!");
-}
-
-
-//==========================================================================
-
-/*
-===================
-=
-= MM_Startup
-=
-= Grabs all space from turbo with malloc/farmalloc
-= Allocates bufferseg misc buffer
-=
-===================
-*/
-
-void MM_Startup (void)
-{
- int i;
- dword length;
- void far *start;
- unsigned segstart,seglength,endfree;
-
- if (mmstarted)
- MM_Shutdown ();
-
-
- mmstarted = true;
- bombonerror = true;
-//
-// set up the linked list (everything in the free list;
-//
- mmhead = NULL;
- mmfree = &mmblocks[0];
- for (i=0;i<MAXBLOCKS-1;i++)
- mmblocks[i].next = &mmblocks[i+1];
- mmblocks[i].next = NULL;
-
-//
-// locked block of all memory until we punch out free space
-//
- GETNEWBLOCK;
- mmhead = mmnew; // this will allways be the first node
- mmnew->start = 0;
- mmnew->length = 0xffff;
- mmnew->attributes = LOCKBIT;
- mmnew->next = NULL;
- mmrover = mmhead;
-
-
-//
-// get all available near conventional memory segments
-//
-//---- length=coreleft();
- length=_memavl();
- start = (void far *)(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);
- mminfo.nearheap = length;
-
-//
-// get all available far conventional memory segments
-//
-//---- length=farcoreleft();
- length=_memmax();
- start = farheap = _fmalloc(length);
- 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);
- mminfo.farheap = length;
- mminfo.mainmem = mminfo.nearheap + mminfo.farheap;
-
-
-//
-// detect EMS and allocate up to 64K at page frame
-//
- mminfo.EMSmem = 0;
- for (i = 1;i < __argc;i++)
- {
- if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
- goto emsskip; // param NOEMS
- }
-
- if (MML_CheckForEMS())
- {
- printf("EMS!\n");
- MML_SetupEMS(); // allocate space
- MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);
- MM_MapEMS(); // map in used pages
- mminfo.EMSmem = EMSpagesmapped*0x4000l;
- }
-
-//
-// detect XMS and get upper memory blocks
-//
-emsskip:
- mminfo.XMSmem = 0;
- for (i = 1;i < __argc;i++)
- {
- if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)
- goto xmsskip; // param NOXMS
- }
-
- if (MML_CheckForXMS())
- {
- printf("XMS!\n");
- MML_SetupXMS(); // allocate as many UMBs as possible
- }
-
-//
-// allocate the misc buffer
-//
-xmsskip:
- mmrover = mmhead; // start looking for space after low block
-
- MM_GetPtr (&bufferseg,BUFFERSIZE);
-}
-
-//==========================================================================
-
-/*
-====================
-=
-= MM_Shutdown
-=
-= Frees all conventional, EMS, and XMS allocated
-=
-====================
-*/
-
-void MM_Shutdown (void)
-{
- if (!mmstarted)
- return;
-
+/* Catacomb Armageddon Source Code\r
+ * Copyright (C) 1993-2014 Flat Rock Software\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ */\r
+\r
+// NEWMM.C\r
+\r
+/*\r
+=============================================================================\r
+\r
+ ID software memory manager\r
+ --------------------------\r
+\r
+Primary coder: John Carmack\r
+\r
+RELIES ON\r
+---------\r
+Quit (char *error) function\r
+\r
+\r
+WORK TO DO\r
+----------\r
+MM_SizePtr to change the size of a given pointer\r
+\r
+Multiple purge levels utilized\r
+\r
+EMS / XMS unmanaged routines\r
+\r
+=============================================================================\r
+*/\r
+\r
+#include "src/lib/16_mm.h"\r
+
+/*\r
+=============================================================================\r
+\r
+ GLOBAL VARIABLES\r
+\r
+=============================================================================\r
+*/\r
+\r
+mminfotype mminfo;\r
+memptr bufferseg;\r
+boolean mmerror;\r
+\r
+void (* beforesort) (void);\r
+void (* aftersort) (void);\r
+\r
+/*\r
+=============================================================================\r
+\r
+ LOCAL VARIABLES\r
+\r
+=============================================================================\r
+*/\r
+\r
+boolean mmstarted;\r
+\r
+void far *farheap;\r
+void *nearheap;\r
+\r
+mmblocktype far mmblocks[MAXBLOCKS]\r
+ ,far *mmhead,far *mmfree,far *mmrover,far *mmnew;\r
+\r
+boolean bombonerror;\r
+\r
+unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;
+unsigned int EMSVer;\r
+\r
+void (* XMSaddr) (void); // far pointer to XMS driver\r
+\r
+unsigned numUMBs,UMBbase[MAXUMBS];
+
+static char *ParmStringsexmm[] = {"noems","noxms",""};
+\r
+/*\r
+======================\r
+=\r
+= MML_CheckForEMS\r
+=\r
+= Routine from p36 of Extending DOS\r
+=\r
+=======================\r
+*/\r
+\r
+boolean MML_CheckForEMS (void)\r
+{\r
+ boolean emmcfems;\r
+ char emmname[] = "EMMXXXX0";\r
+// mov dx,OFFSET emmname\r
+ __asm {\r
+ LEA DX, emmname //fix by andrius4669\r
+ mov ax,0x3d00\r
+ int 0x21 // try to open EMMXXXX0 device\r
+ jc error\r
+\r
+ mov bx,ax\r
+ mov ax,0x4400\r
+\r
+ int 0x21 // get device info\r
+ jc error\r
+\r
+ and dx,0x80\r
+ jz error\r
+\r
+ mov ax,0x4407\r
+\r
+ int 0x21 // get status\r
+ jc error\r
+ or al,al\r
+ jz error\r
+\r
+ mov ah,0x3e\r
+ int 0x21 // close handle\r
+ jc error\r
+ //\r
+ // EMS is good\r
+ //\r
+ mov emmcfems,1\r
+ jmp End\r
+ error:\r
+ //\r
+ // EMS is bad\r
+ //\r
+ mov emmcfems,0\r
+ End:\r
+ }\r
+ return(emmcfems);\r
+}\r
+\r
+\r
+/*\r
+======================\r
+=\r
+= MML_SetupEMS\r
+=\r
+=======================\r
+*/\r
+\r
+void MML_SetupEMS (void)\r
+{\r
+ char str[80],str2[10];\r
+ unsigned err;\r
+ boolean errorflag=false;\r
+ union REGS CPURegs;\r
+\r
+ EMSVer = 0;\r
+ totalEMSpages = freeEMSpages = EMSpageframe = EMSpagesmapped = 0;\r
+\r
+ __asm\r
+ {\r
+ mov ah,EMS_STATUS\r
+ int EMS_INT // make sure EMS hardware is present\r
+ or ah,ah\r
+ jnz error\r
+\r
+ mov ah,EMS_VERSION\r
+ int EMS_INT\r
+ or ah,ah\r
+ jnz error\r
+ mov [EMSVer],ax // set EMSVer\r
+ cmp al,0x32 // only work on ems 3.2 or greater\r
+ jb error\r
+\r
+ mov ah,EMS_GETFRAME\r
+ int EMS_INT // find the page frame address\r
+ or ah,ah\r
+ jnz error\r
+ mov [EMSpageframe],bx\r
+\r
+ mov ah,EMS_GETPAGES\r
+ int EMS_INT // find out how much EMS is there\r
+ or ah,ah\r
+ jnz error\r
+ mov [totalEMSpages],dx\r
+ mov [freeEMSpages],bx\r
+ or bx,bx\r
+ jz noEMS // no EMS at all to allocate\r
+\r
+ cmp bx,4\r
+ jle getpages // there is only 1,2,3,or 4 pages\r
+ mov bx,4 // we can't use more than 4 pages\r
+\r
+getpages:\r
+ mov [EMSpagesmapped],bx\r
+ mov ah,EMS_ALLOCPAGES // allocate up to 64k of EMS\r
+ int EMS_INT\r
+ or ah,ah\r
+ jnz error\r
+ mov [EMShandle],dx\r
+ jmp End\r
+error:\r
+ mov errorflag,1\r
+ jmp End\r
+noEMS:\r
+End:\r
+ }\r
+ if(errorflag==true)\r
+ {\r
+ err = CPURegs.h.ah;\r
+ strcpy(str,"MML_SetupEMS: EMS error 0x");\r
+ itoa(err,str2,16);\r
+ strcpy(str,str2);\r
+ printf("%s\n",str);\r
+ }\r
+}\r
+\r
+\r
+/*\r
+======================\r
+=\r
+= MML_ShutdownEMS\r
+=\r
+=======================\r
+*/\r
+\r
+void MML_ShutdownEMS (void)\r
+{\r
+ boolean errorflag=false;\r
+ if (!EMShandle)\r
+ return;\r
+ __asm\r
+ {\r
+ mov ah,EMS_FREEPAGES\r
+ mov dx,[EMShandle]\r
+ int EMS_INT\r
+ or ah,ah\r
+ jz ok\r
+ mov errorflag,1\r
+ ok:\r
+ }\r
+ if(errorflag==true) printf("MML_ShutdownEMS: Error freeing EMS!"); //++++ add something\r
+}\r
+\r
+/*\r
+====================\r
+=\r
+= MM_MapEMS\r
+=\r
+= Maps the 64k of EMS used by memory manager into the page frame\r
+= for general use. This only needs to be called if you are keeping\r
+= other things in EMS.\r
+=\r
+====================\r
+*/\r
+\r
+void MM_MapEMS (void)\r
+{\r
+ char str[80],str2[10];\r
+ unsigned err;\r
+ boolean errorflag=false;\r
+ int i;\r
+ union REGS CPURegs;\r
+\r
+ for (i=0;i<EMSpagesmapped;i++)\r
+ {\r
+ __asm\r
+ {\r
+ mov ah,EMS_MAPPAGE\r
+ mov bx,[i] // logical page\r
+ mov al,bl // physical page\r
+ mov dx,[EMShandle] // handle\r
+ int EMS_INT\r
+ or ah,ah\r
+ jnz error\r
+ jmp End\r
+ error:\r
+ mov errorflag,1\r
+ End:\r
+ }\r
+ if(errorflag==true)\r
+ {\r
+ err = CPURegs.h.ah;\r
+ strcpy(str,"MM_MapEMS: EMS error 0x");\r
+ itoa(err,str2,16);\r
+ strcpy(str,str2);\r
+ printf("%s\n",str);\r
+ }\r
+ }\r
+ return;\r
+}\r
+\r
+//==========================================================================\r
+\r
+/*\r
+======================\r
+=\r
+= MML_CheckForXMS\r
+=\r
+= Check for XMM driver\r
+=\r
+=======================\r
+*/\r
+\r
+boolean MML_CheckForXMS (void)\r
+{\r
+ boolean errorflag=false;\r
+ numUMBs = 0;\r
+\r
+ __asm\r
+ {\r
+ mov ax,0x4300\r
+ int 0x2f // query status of installed diver\r
+ cmp al,0x80\r
+ je good\r
+ mov errorflag,1\r
+ good:\r
+ }\r
+ if(errorflag==true) return false;\r
+ else return true;\r
+}\r
+\r
+\r
+/*\r
+======================\r
+=\r
+= MML_SetupXMS\r
+=\r
+= Try to allocate all upper memory block\r
+=\r
+=======================\r
+*/\r
+\r
+void MML_SetupXMS (void)\r
+{\r
+ unsigned base,size;\r
+\r
+ __asm\r
+ {\r
+ mov ax,0x4310\r
+ int 0x2f\r
+ mov [WORD PTR XMSaddr],bx\r
+ mov [WORD PTR XMSaddr+2],es // function pointer to XMS driver\r
+ }\r
+getmemory:\r
+ __asm\r
+ {\r
+ mov ah,XMS_ALLOCUMB\r
+ mov dx,0xffff // try for largest block possible\r
+ call [DWORD PTR XMSaddr]\r
+ or ax,ax\r
+ jnz gotone\r
+\r
+ cmp bl,0xb0 // error: smaller UMB is available\r
+ jne done;\r
+\r
+ mov ah,XMS_ALLOCUMB\r
+ call [DWORD PTR XMSaddr] // DX holds largest available UMB\r
+ or ax,ax\r
+ jz done // another error...\r
+\r
+gotone:\r
+ mov [base],bx\r
+ mov [size],dx\r
+done:\r
+ }\r
+ MML_UseSpace (base,size);\r
+ mminfo.XMSmem += size*16;\r
+ UMBbase[numUMBs] = base;\r
+ numUMBs++;\r
+ if (numUMBs < MAXUMBS)\r
+ goto getmemory;\r
+}\r
+\r
+\r
+/*\r
+======================\r
+=\r
+= MML_ShutdownXMS\r
+=\r
+======================\r
+*/\r
+\r
+void MML_ShutdownXMS (void)\r
+{\r
+ int i;\r
+ unsigned base;\r
+\r
+ for (i=0;i<numUMBs;i++)\r
+ {\r
+ base = UMBbase[i];\r
+ __asm\r
+ {\r
+ mov ah,XMS_FREEUMB\r
+ mov dx,[base]\r
+ call [DWORD PTR XMSaddr]\r
+ }\r
+ }\r
+}\r
+\r
+//==========================================================================\r
+\r
+/*\r
+======================\r
+=\r
+= MML_UseSpace\r
+=\r
+= Marks a range of paragraphs as usable by the memory manager\r
+= This is used to mark space for the near heap, far heap, ems page frame,\r
+= and upper memory blocks\r
+=\r
+======================\r
+*/\r
+\r
+void MML_UseSpace (unsigned segstart, unsigned seglength)\r
+{\r
+ mmblocktype far *scan,far *last;\r
+ unsigned oldend;\r
+ long extra;\r
+\r
+ scan = last = mmhead;\r
+ mmrover = mmhead; // reset rover to start of memory\r
+\r
+//\r
+// search for the block that contains the range of segments\r
+//\r
+ while (scan->start+scan->length < segstart)\r
+ {\r
+ last = scan;\r
+ scan = scan->next;\r
+ }\r
+\r
+//\r
+// take the given range out of the block\r
+//\r
+ oldend = scan->start + scan->length;\r
+ extra = oldend - (segstart+seglength);\r
+ if (extra < 0)\r
+ {\r
+ printf("MML_UseSpace: Segment spans two blocks!");\r
+ return;\r
+ }\r
+\r
+\r
+ if (segstart == scan->start)\r
+ {\r
+ last->next = scan->next; // unlink block\r
+ FREEBLOCK(scan);\r
+ scan = last;\r
+ }\r
+ else\r
+ scan->length = segstart-scan->start; // shorten block\r
+\r
+ if (extra > 0)\r
+ {\r
+ GETNEWBLOCK;\r
+ mmnew->next = scan->next;\r
+ scan->next = mmnew;\r
+ mmnew->start = segstart+seglength;\r
+ mmnew->length = extra;\r
+ mmnew->attributes = LOCKBIT;\r
+ }\r
+\r
+}\r
+\r
+//==========================================================================\r
+\r
+/*\r
+====================\r
+=\r
+= MML_ClearBlock\r
+=\r
+= We are out of blocks, so free a purgable block\r
+=\r
+====================\r
+*/\r
+\r
+void MML_ClearBlock (void)\r
+{\r
+ mmblocktype far *scan,far *last;\r
+\r
+ scan = mmhead->next;\r
+\r
+ while (scan)\r
+ {\r
+ if (!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS) )\r
+ {\r
+ MM_FreePtr(scan->useptr);\r
+ return;\r
+ }\r
+ scan = scan->next;\r
+ }\r
+\r
+ printf("MM_ClearBlock: No purgable blocks!");\r
+}\r
+\r
+\r
+//==========================================================================\r
+\r
+/*\r
+===================\r
+=\r
+= MM_Startup\r
+=\r
+= Grabs all space from turbo with malloc/farmalloc\r
+= Allocates bufferseg misc buffer\r
+=\r
+===================\r
+*/\r
+\r
+void MM_Startup (void)\r
+{\r
+ int i;\r
+ dword length;\r
+ void far *start;\r
+ unsigned segstart,seglength,endfree;\r
+\r
+ if (mminfo.mmstarted)\r
+ MM_Shutdown ();\r
+\r
+\r
+ mminfo.mmstarted = true;\r
+ mminfo.bombonerror = true;\r
+//\r
+// set up the linked list (everything in the free list;\r
+//\r
+ mmhead = NULL;\r
+ mmfree = &mmblocks[0];\r
+ for (i=0;i<MAXBLOCKS-1;i++)\r
+ mmblocks[i].next = &mmblocks[i+1];\r
+ mmblocks[i].next = NULL;\r
+\r
+//\r
+// locked block of all memory until we punch out free space\r
+//\r
+ GETNEWBLOCK;\r
+ mmhead = mmnew; // this will allways be the first node\r
+ mmnew->start = 0;\r
+ mmnew->length = 0xffff;\r
+ mmnew->attributes = LOCKBIT;\r
+ mmnew->next = NULL;\r
+ mmrover = mmhead;\r
+\r
+\r
+//\r
+// get all available near conventional memory segments\r
+//\r
+//---- length=coreleft();\r
+ _nheapgrow();\r
+ length=_memavl();\r
+ start = (void far *)(nearheap = malloc(length));\r
+\r
+ length -= 16-(FP_OFF(start)&15);\r
+ length -= SAVENEARHEAP;\r
+ seglength = length / 16; // now in paragraphs\r
+ segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;\r
+ MML_UseSpace (segstart,seglength);\r
+ mminfo.nearheap = length;\r
+\r
+//\r
+// get all available far conventional memory segments\r
+//\r
+//---- length=farcoreleft();\r
+ _fheapgrow();\r
+ length=_memavl();\r
+ start = farheap = _fmalloc(length);\r
+ length -= 16-(FP_OFF(start)&15);\r
+ length -= SAVEFARHEAP;\r
+ seglength = length / 16; // now in paragraphs\r
+ segstart = FP_SEG(start)+(FP_OFF(start)+15)/16;\r
+ MML_UseSpace (segstart,seglength);\r
+ mminfo.farheap = length;\r
+ mminfo.mainmem = mminfo.nearheap + mminfo.farheap;\r
+\r
+\r
+//\r
+// detect EMS and allocate up to 64K at page frame\r
+//\r
+ mminfo.EMSmem = 0;\r
+ for (i = 1;i < __argc;i++)\r
+ {\r
+ if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)\r
+ goto emsskip; // param NOEMS\r
+ }\r
+\r
+ if (MML_CheckForEMS())\r
+ {\r
+ //printf("EMS1\n");\r
+ MML_SetupEMS(); // allocate space\r
+ //printf("EMS2\n");\r
+ MML_UseSpace (EMSpageframe,EMSpagesmapped*0x400);\r
+ //printf("EMS3\n");\r
+ MM_MapEMS(); // map in used pages\r
+ //printf("EMS4\n");\r
+ mminfo.EMSmem = EMSpagesmapped*0x4000l;\r
+ }\r
+\r
+//\r
+// detect XMS and get upper memory blocks\r
+//\r
+emsskip:\r
+ mminfo.XMSmem = 0;\r
+ for (i = 1;i < __argc;i++)\r
+ {\r
+ if ( US_CheckParm(__argv[i],ParmStringsexmm) == 0)\r
+ goto xmsskip; // param NOXMS\r
+ }\r
+\r
+ if (MML_CheckForXMS())\r
+ {\r
+// printf("XMS!\n");\r
+//++++ MML_SetupXMS(); // allocate as many UMBs as possible\r
+ }\r
+\r
+//\r
+// allocate the misc buffer\r
+//\r
+xmsskip:\r
+ mmrover = mmhead; // start looking for space after low block\r
+\r
+ MM_GetPtr (&bufferseg,BUFFERSIZE);\r
+}\r
+\r
+//==========================================================================\r
+\r
+/*\r
+====================\r
+=\r
+= MM_Shutdown\r
+=\r
+= Frees all conventional, EMS, and XMS allocated\r
+=\r
+====================\r
+*/\r
+\r
+void MM_Shutdown (void)\r
+{\r
+ if (!mminfo.mmstarted)\r
+ return;\r
+\r