From: sparky4 Date: Tue, 20 Sep 2016 21:07:25 +0000 (-0500) Subject: ==== UNTESTED major tweaking has been done to the core memory module stuff 16_mm... X-Git-Url: http://4ch.mooo.com/gitweb/?a=commitdiff_plain;h=1be58416e7fd292f729d6fe8e759ccd926431fd2;p=16.git ==== UNTESTED major tweaking has been done to the core memory module stuff 16_mm 16_pm 16_ca ==== --- diff --git a/makefile b/makefile index 7c2d12c2..e150bce8 100755 --- a/makefile +++ b/makefile @@ -91,7 +91,7 @@ UPXQ=-qqq S_FLAGS=-sg -st -of+ -zu -zdf -zff -zgf -k32768#54096#60000 Z_FLAGS=-zk0 -zc -zp8 -zm O_FLAGS=-obmilr -oe=24 -out -oh -ei -onac -ol+ -ok##x -T_FLAGS=-bt=dos -mh -0 -fpi87 -fo=.$(OBJ) -d1 -e=20 +T_FLAGS=-bt=dos -mh -0 -fpi87 -fo=.$(OBJ) -d1 -e=65536 CPPFLAGS=-DTARGET_MSDOS=16 -DMSDOS=1 !ifeq DEBUGSERIAL 1 @@ -106,7 +106,7 @@ LIBFLAGS=$(WLIBQ) -b -n # objects # VGMSNDOBJ = vgmSnd.$(OBJ) 16_snd.$(OBJ) -16LIBOBJS = 16_lib.$(OBJ) 16_in.$(OBJ) 16_mm.$(OBJ) wcpu.$(OBJ) 16_head.$(OBJ) 16_ca.$(OBJ) 16_dbg.$(OBJ) kitten.$(OBJ) 16_hc.$(OBJ) 16_timer.$(OBJ) +16LIBOBJS = 16_lib.$(OBJ) 16_in.$(OBJ) 16_mm.$(OBJ) wcpu.$(OBJ) 16_head.$(OBJ) 16_ca.$(OBJ) 16_dbg.$(OBJ) kitten.$(OBJ) 16_hc.$(OBJ) 16_timer.$(OBJ) 16_pm.$(OBJ) GFXLIBOBJS = modex16.$(OBJ) bitmap.$(OBJ) 16text.$(OBJ) bakapee.$(OBJ) scroll16.$(OBJ) 16render.$(OBJ) 16_vrs.$(OBJ) 16_sprit.$(OBJ) #scroll16.$(OBJ) #16planar.$(OBJ) planar.$(OBJ) DOSLIBOBJ = adlib.$(OBJ) 8254.$(OBJ) 8259.$(OBJ) dos.$(OBJ) cpu.$(OBJ) diff --git a/pm.use b/pm.use index a255a3a3..fbd7a2c7 100755 --- a/pm.use +++ b/pm.use @@ -270,3 +270,8 @@ Binary file 16/wolf3d/WOLFSRC/WOLF3D.EXE matches 16/wolf3d/WOLFSRC/WOLF3D.MAP: 1912:114B _PM_Shutdown 16/wolf3d/WOLFSRC/WOLFHACK.C: src = PM_GetPage(0); 16/wolf3d/WOLFSRC/WOLFHACK.C: src = PM_GetPage(1); + +16/wf3d8086/id_pm.c: longword last = MAXLONG; +16/wf3d8086/id_pm.c: for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++) +16/wf3d8086/id_pm.c: for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++) +16/wf3d8086/id_pm.c: if (++PMFrameCount >= MAXLONG - 4) diff --git a/src/exmmtest.c b/src/exmmtest.c index 1bd9671f..ff8be1df 100755 --- a/src/exmmtest.c +++ b/src/exmmtest.c @@ -95,7 +95,7 @@ main(int argc, char *argv[]) bakapee2 = "data/test.map"; } // printf("main()=%Fp start MM\n", *argv[0]); - MM_Startup(&gvar.mm, &gvar.mmi); + MM_Startup(&gvar); //PM_Startup(); //PM_UnlockMainMem(); CA_Startup(&gvar); @@ -133,7 +133,7 @@ for(w=0;w<2;w++) printf("press any key to continue!\n"); getch(); MM_ShowMemory(&gvar); - MM_DumpData(&gvar.mm); + MM_DumpData(&gvar); MM_Report_(&gvar); if(baka) printf("\nyay!\n"); else printf("\npoo!\n"); @@ -142,10 +142,10 @@ for(w=0;w<2;w++) #ifdef FILEREAD } #endif - MM_FreePtr(&bigbuffer, &gvar.mm); + MM_FreePtr(&bigbuffer, &gvar); //PM_Shutdown(); CA_Shutdown(&gvar); - MM_Shutdown(&gvar.mm); + MM_Shutdown(&gvar); free(bakapee1); free(bakapee2); printf("========================================\n"); printf("near= %Fp ", gvar.mm.nearheap); diff --git a/src/lib/16_ca.c b/src/lib/16_ca.c index 27e2d7ee..e2803808 100755 --- a/src/lib/16_ca.c +++ b/src/lib/16_ca.c @@ -226,7 +226,7 @@ boolean CA_FarRead(int handle, byte huge *dest, dword length, global_game_variab boolean flag; //dword fat=0; //word segm=0; - if(gvar->mm.EMSVer<0x40) + if(gvar->pm.emm.EMSVer<0x40) if(length>0xfffflu) { printf("File is a fat bakapee\n"); @@ -298,7 +298,7 @@ boolean CA_FarWrite(int handle, byte huge *source, dword length, global_game_var boolean flag; //dword fat=0; //word segm=0; - if(gvar->mm.EMSVer<0x40) + if(gvar->pm.emm.EMSVer<0x40) if(length>0xfffflu) { printf("File is a fat bakapee\n"); @@ -438,7 +438,7 @@ boolean CA_LoadFile(char *filename, memptr *ptr, global_game_variables_t *gvar) return false; size = filelength(handle); - MM_GetPtr(ptr,size, &(gvar->mm), &(gvar->mmi)); //TODO: gvar parameters + MM_GetPtr(ptr,size, gvar); if(!CA_FarRead(handle,*ptr,size, gvar)) { close(handle); diff --git a/src/lib/16_head.h b/src/lib/16_head.h index b840043d..baa983e8 100755 --- a/src/lib/16_head.h +++ b/src/lib/16_head.h @@ -42,6 +42,7 @@ #include #include #include +#include #ifdef __WATCOMC__ #include #include @@ -161,20 +162,6 @@ extern int profilehandle,debughandle; //make it into game global #define _FCORELEFT 0x90000UL+16UL extern union REGS CPURegs; -#define _AX CPURegs.x.ax -#define _BX CPURegs.x.bx -#define _CX CPURegs.x.cx -#define _DX CPURegs.x.dx - -#define _AH CPURegs.h.ah -#define _AL CPURegs.h.al -#define _BH CPURegs.h.bh -#define _BL CPURegs.h.bl -#define _CH CPURegs.h.ch -#define _CL CPURegs.h.cl -#define _DH CPURegs.h.dh -#define _DL CPURegs.h.dl - #define geninterrupt(n) int86(n,&CPURegs,&CPURegs); #define peekb(segm,ofs) (*(byte far*)MK_FP((segm),(ofs))) #define peekw(segm,ofs) (*(word far*)MK_FP((segm),(ofs))) diff --git a/src/lib/16_mm.c b/src/lib/16_mm.c index d14479d5..f2537685 100755 --- a/src/lib/16_mm.c +++ b/src/lib/16_mm.c @@ -149,7 +149,7 @@ boolean MML_CheckForEMS(void) ======================= */ -byte MML_SetupEMS(mminfo_t *mm) +byte MML_SetupEMS(global_game_variables_t *gvar) { byte str[160]; byte err; @@ -245,12 +245,12 @@ End: printf("%s\n",str); return err; } - mm->totalEMSpages=totalEMSpages; - mm->freeEMSpages=freeEMSpages; - mm->EMSpageframe=EMSpageframe; - mm->EMSpagesmapped=EMSpagesmapped; - mm->EMShandle=EMShandle; - mm->EMSVer=EMSVer; + gvar->mm.totalEMSpages=totalEMSpages; + gvar->mm.freeEMSpages=freeEMSpages; + gvar->mm.EMSpageframe=EMSpageframe; + gvar->mm.EMSpagesmapped=EMSpagesmapped; + gvar->mm.EMShandle=EMShandle; + gvar->pm.emm.EMSVer=EMSVer; return 0; } @@ -263,10 +263,10 @@ End: ======================= */ -void MML_ShutdownEMS(mminfo_t *mm) +void MML_ShutdownEMS(global_game_variables_t *gvar) { boolean errorflag=false; - unsigned EMShandle=mm->EMShandle; + unsigned EMShandle=gvar->mm.EMShandle; if(!EMShandle) return; @@ -300,14 +300,14 @@ void MML_ShutdownEMS(mminfo_t *mm) ==================== */ -byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi) +byte MM_MapEMS(global_game_variables_t *gvar) { byte str[160]; unsigned EMShandle; byte err; boolean errorflag=false; int i; - EMShandle=mm->EMShandle; + EMShandle=gvar->mm.EMShandle; for (i=0;i<4/*MAPPAGES*/;i++) { @@ -344,12 +344,12 @@ byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi) return err; } } - mmi->EMSmem = (i)*0x4000lu; - //printf(" mmi->EMSmem=%lu\n", mmi->EMSmem); + gvar->mmi.EMSmem = (i)*0x4000lu; + //printf(" gvar->mmi.EMSmem=%lu\n", gvar->mmi.EMSmem); return 0; } -byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi) +byte MM_MapXEMS(global_game_variables_t *gvar) { //SUB EMS.MapXPages (PhysicalStart, LogicalStart, NumPages, Handle) @@ -378,9 +378,9 @@ byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi) word EMShandle; boolean errorflag=false; int i; - EMShandle=mm->EMShandle; + EMShandle=gvar->mm.EMShandle; - if(mm->EMSVer<0x40) + if(gvar->pm.emm.EMSVer<0x40) return 5; for (i=0;iEMSmem = (i)*0x4000lu; + gvar->mmi.EMSmem = (i)*0x4000lu; return 0; } @@ -438,10 +438,10 @@ byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi) ======================= */ -boolean MML_CheckForXMS(mminfo_t *mm) +boolean MML_CheckForXMS(global_game_variables_t *gvar) { boolean errorflag=false; - mm->numUMBs = 0; + gvar->mm.numUMBs = 0; __asm { mov ax,0x4300 @@ -471,7 +471,7 @@ boolean MML_CheckForXMS(mminfo_t *mm) ======================= */ -void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi) +void MML_SetupXMS(global_game_variables_t *gvar) { word base,size; @@ -515,11 +515,11 @@ getmemory: } #endif // printf("base=%u ", base); printf("size=%u\n", size); - MML_UseSpace(base,size, mm); - mmi->XMSmem += size*16; - mm->UMBbase[mm->numUMBs] = base; - mm->numUMBs++; - if(mm->numUMBs < MAXUMBS) + MML_UseSpace(base,size, gvar); + gvar->mmi.XMSmem += size*16; + gvar->mm.UMBbase[gvar->mm.numUMBs] = base; + gvar->mm.numUMBs++; + if(gvar->mm.numUMBs < MAXUMBS) goto getmemory; } @@ -532,14 +532,14 @@ getmemory: ====================== */ -void MML_ShutdownXMS(mminfo_t *mm) +void MML_ShutdownXMS(global_game_variables_t *gvar) { int i; unsigned base; - for (i=0;inumUMBs;i++) + for (i=0;imm.numUMBs;i++) { - base = mm->UMBbase[i]; + base = gvar->mm.UMBbase[i]; __asm { mov ah,XMS_FREEUMB mov dx,[base] @@ -562,7 +562,7 @@ void MML_ShutdownXMS(mminfo_t *mm) ====================== */ -/*void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm) +/*void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *last; word segm=1; @@ -570,8 +570,8 @@ void MML_ShutdownXMS(mminfo_t *mm) dword segmlen; dword extra; - scan = last = mm->mmhead; - mm->mmrover = mm->mmhead; // reset rover to start of memory + scan = last = gvar->mm.mmhead; + gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory // // search for the block that contains the range of segments @@ -596,7 +596,7 @@ void MML_ShutdownXMS(mminfo_t *mm) { scan->blob=segm; - //MML_UseSpace(segstart, seglength, mm); + //MML_UseSpace(segstart, seglength, gvar); printf("MML_UseSpace: Segment spans two blocks!\n"); //} @@ -622,25 +622,25 @@ void MML_ShutdownXMS(mminfo_t *mm) if(extra > 0) { GETNEWBLOCK; - mm->mmnew->useptr = NULL; + gvar->mm.mmnew->useptr = NULL; - mm->mmnew->next = scan->next; - scan->next = mm->mmnew; - mm->mmnew->start = segstart+seglength; - mm->mmnew->length = extra; - mm->mmnew->attributes = LOCKBIT; + gvar->mm.mmnew->next = scan->next; + scan->next = gvar->mm.mmnew; + gvar->mm.mmnew->start = segstart+seglength; + gvar->mm.mmnew->length = extra; + gvar->mm.mmnew->attributes = LOCKBIT; }//else if(segm>0) goto segu; }*/ -void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm) +void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar) { mmblocktype far *scan,far *last; word oldend; sdword extra; //word segm=1; - scan = last = mm->mmhead; - mm->mmrover = mm->mmhead; // reset rover to start of memory + scan = last = gvar->mm.mmhead; + gvar->mm.mmrover = gvar->mm.mmhead; // reset rover to start of memory // // search for the block that contains the range of segments @@ -692,13 +692,13 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm) if (extra > 0) { GETNEWBLOCK; - mm->mmnew->useptr = NULL; + gvar->mm.mmnew->useptr = NULL; - mm->mmnew->next = scan->next; - scan->next = mm->mmnew; - mm->mmnew->start = segstart+seglength; - mm->mmnew->length = extra; - mm->mmnew->attributes = LOCKBIT; + gvar->mm.mmnew->next = scan->next; + scan->next = gvar->mm.mmnew; + gvar->mm.mmnew->start = segstart+seglength; + gvar->mm.mmnew->length = extra; + gvar->mm.mmnew->attributes = LOCKBIT; } } @@ -715,18 +715,18 @@ void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm) ==================== */ -void MML_ClearBlock(mminfo_t *mm) +void MML_ClearBlock(global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *last; mmblocktype far *scan,far *last; - scan = mm->mmhead->next; + scan = gvar->mm.mmhead->next; while(scan) { if(!(scan->attributes&LOCKBIT) && (scan->attributes&PURGEBITS)) { - MM_FreePtr(scan->useptr, mm); + MM_FreePtr(scan->useptr, gvar); return; } scan = scan->next; @@ -749,7 +749,7 @@ void MML_ClearBlock(mminfo_t *mm) =================== */ -void MM_Startup(mminfo_t *mm, mminfotype *mmi) +void MM_Startup(global_game_variables_t *gvar) { int i; //dword length,seglength; @@ -759,34 +759,34 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi) word segstart;//,endfree; //memptr *peeonself; -// if(mm->mmstarted) -// MM_Shutdown(mm); + if(gvar->mm.mmstarted) + MM_Shutdown(gvar); - mm->mmstarted = true; - mm->bombonerror = true; + gvar->mm.mmstarted = true; + gvar->mm.bombonerror = true; // // set up the linked list (everything in the free list; // - mm->mmhead = NULL; - mm->mmfree = &(mm->mmblocks[0]); + gvar->mm.mmhead = NULL; + gvar->mm.mmfree = &(gvar->mm.mmblocks[0]); for(i=0;immblocks[i].next = &(mm->mmblocks[i+1]); + gvar->mm.mmblocks[i].next = &(gvar->mm.mmblocks[i+1]); } - mm->mmblocks[i].next = NULL; + gvar->mm.mmblocks[i].next = NULL; // // locked block of all memory until we punch out free space // 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->mmnew->useptr = peeonself; - mm->mmrover = mm->mmhead; + gvar->mm.mmhead = gvar->mm.mmnew; // this will allways be the first node + gvar->mm.mmnew->start = 0; + gvar->mm.mmnew->length = 0xffff; + gvar->mm.mmnew->attributes = LOCKBIT; + gvar->mm.mmnew->next = NULL; + //gvar->mm.mmnew->useptr = peeonself; + gvar->mm.mmrover = gvar->mm.mmhead; //printf(" %x\n", peeonself); //printf(" %x\n", *peeonself); @@ -796,20 +796,20 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi) #ifdef __WATCOMC__ _nheapgrow(); length=(dword)_memavl();//(dword)GetFreeSize(); - //huge start = (void huge *)(mm->nearheap = _nmalloc(length)); - start = (void far *)(mm->nearheap = _nmalloc(length)); + //huge start = (void huge *)(gvar->mm.nearheap = _nmalloc(length)); + start = (void far *)(gvar->mm.nearheap = _nmalloc(length)); #endif #ifdef __BORLANDC__ length=coreleft(); - //huge start = (void huge *)(mm->nearheap = malloc(length)); - start = (void far *)(mm->nearheap = malloc(length)); + //huge start = (void huge *)(gvar->mm.nearheap = malloc(length)); + start = (void far *)(gvar->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; + MML_UseSpace(segstart,seglength, gvar); + gvar->mmi.nearheap = length; //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length); // @@ -824,22 +824,22 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi) 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); + start = gvar->mm.farheap = _fmalloc(length); + //start = gvar->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; + MML_UseSpace(segstart,seglength, gvar); + gvar->mmi.farheap = length; //printf("start=%Fp segstart=%x seglen=%lu len=%lu\n", start, segstart, seglength, length); - mmi->mainmem = mmi->nearheap + mmi->farheap; + gvar->mmi.mainmem = gvar->mmi.nearheap + gvar->mmi.farheap; // // detect EMS and allocate up to 64K at page frame // - mmi->EMSmem = 0; + gvar->mmi.EMSmem = 0; //goto emsskip; //0000 for(i = 1;i < #ifdef __WATCOMC__ @@ -862,20 +862,20 @@ void MM_Startup(mminfo_t *mm, mminfotype *mmi) } if(MML_CheckForEMS()) { - MML_SetupEMS(mm); // allocate space + MML_SetupEMS(gvar); // allocate space //TODO: EMS4! AND EMS 3.2 MASSIVE DATA HANDLMENT! - MML_UseSpace(mm->EMSpageframe,(MAPPAGES)*0x4000lu, mm); - //if(mm->EMSVer<0x40) - MM_MapEMS(mm, mmi); // map in used pages + MML_UseSpace(gvar->mm.EMSpageframe,(MAPPAGES)*0x4000lu, gvar); + //if(gvar->pm.emm.EMSVer<0x40) + MM_MapEMS(gvar); // map in used pages //else - //MM_MapXEMS(mm, mmi); // map in used pages + //MM_MapXEMS(gvar); // map in used pages } // // detect XMS and get upper memory blocks // emsskip: - mmi->XMSmem = 0; + gvar->mmi.XMSmem = 0; goto xmsskip;//0000 for(i = 1;i < #ifdef __WATCOMC__ @@ -896,18 +896,18 @@ goto xmsskip;//0000 ,ParmStringsexmm) == 0) goto xmsskip; // param NOXMS } - if(MML_CheckForXMS(mm)) + if(MML_CheckForXMS(gvar)) { - MML_SetupXMS(mm, mmi); // allocate as many UMBs as possible + MML_SetupXMS(gvar); // allocate as many UMBs as possible } // // allocate the misc buffer // xmsskip: - mm->mmrover = mm->mmhead; // start looking for space after low block + gvar->mm.mmrover = gvar->mm.mmhead; // start looking for space after low block - MM_GetPtr(&(mm->bufferseg),BUFFERSIZE, mm, mmi); + MM_GetPtr(&(gvar->mm.bufferseg),BUFFERSIZE, gvar); } //========================================================================== @@ -922,20 +922,20 @@ xmsskip: ==================== */ -void MM_Shutdown(mminfo_t *mm) +void MM_Shutdown(global_game_variables_t *gvar) { - if(!(mm->mmstarted)) + if(!(gvar->mm.mmstarted)) return; - _ffree(mm->farheap);// printf(" far freed\n"); + _ffree(gvar->mm.farheap);// printf(" far freed\n"); #ifdef __WATCOMC__ - _nfree(mm->nearheap);// printf(" near freed\n"); + _nfree(gvar->mm.nearheap);// printf(" near freed\n"); #endif #ifdef __BORLANDC__ - free(mm->nearheap);// printf(" near freed\n"); + free(gvar->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"); } + if(MML_CheckForEMS()){ MML_ShutdownEMS(gvar); }//printf(" EMS freed\n"); } + if(MML_CheckForXMS(gvar)){ MML_ShutdownXMS(gvar); }//printf(" XMS freed\n"); } } //========================================================================== @@ -950,7 +950,7 @@ void MM_Shutdown(mminfo_t *mm) ==================== */ -void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) +void MM_GetPtr (memptr *baseptr, dword size, global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next; mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next; @@ -960,16 +960,16 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) needed = (size+15)/16; // convert size from bytes to paragraphs GETNEWBLOCK; // fill in start and next after a spot is found - mm->mmnew->length = needed; - mm->mmnew->useptr = baseptr; - //if(mm->mmnew->useptr==NULL){ + gvar->mm.mmnew->length = needed; + gvar->mm.mmnew->useptr = baseptr; + //if(gvar->mm.mmnew->useptr==NULL){ #ifdef __DEBUG__ - printf("baseptr=%04x ", baseptr); printf("useptr=%04x\n", mm->mmnew->useptr); - printf("*baseptr=%04x ", *baseptr); printf("*useptr=%04x\n", *(mm->mmnew->useptr)); - printf("*baseptr=%Fp ", *baseptr); printf("*useptr=%Fp\n", *(mm->mmnew->useptr)); + printf("baseptr=%04x ", baseptr); printf("useptr=%04x\n", gvar->mm.mmnew->useptr); + printf("*baseptr=%04x ", *baseptr); printf("*useptr=%04x\n", *(gvar->mm.mmnew->useptr)); + printf("*baseptr=%Fp ", *baseptr); printf("*useptr=%Fp\n", *(gvar->mm.mmnew->useptr)); #endif //exit(-5); } - mm->mmnew->attributes = BASEATTRIBUTES; + gvar->mm.mmnew->attributes = BASEATTRIBUTES; //tryagain: for (search = 0; search<3; search++) @@ -978,25 +978,25 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) // first search: try to allocate right after the rover, then on up // second search: search from the head pointer up to the rover // third search: compress memory, then scan from start - if (search == 1 && mm->mmrover == mm->mmhead) + if (search == 1 && gvar->mm.mmrover == gvar->mm.mmhead) search++; switch (search) { case 0: - lastscan = mm->mmrover; - scan = mm->mmrover->next; + lastscan = gvar->mm.mmrover; + scan = gvar->mm.mmrover->next; endscan = NULL; break; case 1: - lastscan = mm->mmhead; - scan = mm->mmhead->next; - endscan = mm->mmrover; + lastscan = gvar->mm.mmhead; + scan = gvar->mm.mmhead->next; + endscan = gvar->mm.mmrover; break; case 2: - MM_SortMem (mm); - lastscan = mm->mmhead; - scan = mm->mmhead->next; + MM_SortMem (gvar); + lastscan = gvar->mm.mmhead; + scan = gvar->mm.mmhead->next; endscan = NULL; break; } @@ -1013,16 +1013,16 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) // and allocate the new block // purge = lastscan->next; - lastscan->next = mm->mmnew; - mm->mmnew->start = *(unsigned *)baseptr = startseg; - mm->mmnew->next = scan; + lastscan->next = gvar->mm.mmnew; + gvar->mm.mmnew->start = *(unsigned *)baseptr = startseg; + gvar->mm.mmnew->next = scan; while ( purge != scan) { // free the purgable block next = purge->next; FREEBLOCK(purge); purge = next; // purge another if not at scan } - mm->mmrover = mm->mmnew; + gvar->mm.mmrover = gvar->mm.mmnew; return; // good allocation! } @@ -1041,18 +1041,18 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) } } - if (mm->bombonerror) + if (gvar->mm.bombonerror) { #ifdef __WATCOMC__ //heapdump(); #endif - printf(OUT_OF_MEM_MSG,(size-mmi->nearheap)); + printf(OUT_OF_MEM_MSG,(size-gvar->mmi.nearheap)); printf("for stability reasons the program will shut down! wwww\n"); - MM_Shutdown(mm); + MM_Shutdown(gvar); exit(-1); } else - mm->mmerror = true; + gvar->mm.mmerror = true; } //========================================================================== @@ -1067,16 +1067,16 @@ void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi) ==================== */ -void MM_FreePtr(memptr *baseptr, mminfo_t *mm) +void MM_FreePtr(memptr *baseptr, global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *last; mmblocktype far *scan,far *last; - last = mm->mmhead; + last = gvar->mm.mmhead; scan = last->next; - if(baseptr == mm->mmrover->useptr) // removed the last allocated block - mm->mmrover = mm->mmhead; + if(baseptr == gvar->mm.mmrover->useptr) // removed the last allocated block + gvar->mm.mmrover = gvar->mm.mmhead; while(scan->useptr != baseptr && scan) { @@ -1106,23 +1106,23 @@ void MM_FreePtr(memptr *baseptr, mminfo_t *mm) ===================== */ -void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm) +void MM_SetPurge(memptr *baseptr, int purge, global_game_variables_t *gvar) { //huge mmblocktype huge *start; mmblocktype far *start; - start = mm->mmrover; + start = gvar->mm.mmrover; do { - if(mm->mmrover->useptr == baseptr) + if(gvar->mm.mmrover->useptr == baseptr) break; - mm->mmrover = mm->mmrover->next; + gvar->mm.mmrover = gvar->mm.mmrover->next; - if(!mm->mmrover) - mm->mmrover = mm->mmhead; - else if(mm->mmrover == start) + if(!gvar->mm.mmrover) + gvar->mm.mmrover = gvar->mm.mmhead; + else if(gvar->mm.mmrover == start) { Quit("MM_SetPurge: Block not found!"); //return; @@ -1130,8 +1130,8 @@ void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm) } while(1); - mm->mmrover->attributes &= ~PURGEBITS; - mm->mmrover->attributes |= purge; + gvar->mm.mmrover->attributes &= ~PURGEBITS; + gvar->mm.mmrover->attributes |= purge; } //========================================================================== @@ -1146,23 +1146,23 @@ void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm) ===================== */ -void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm) +void MM_SetLock(memptr *baseptr, boolean locked, global_game_variables_t *gvar) { //huge mmblocktype huge *start; mmblocktype far *start; - start = mm->mmrover; + start = gvar->mm.mmrover; do { - if(mm->mmrover->useptr == baseptr) + if(gvar->mm.mmrover->useptr == baseptr) break; - mm->mmrover = mm->mmrover->next; + gvar->mm.mmrover = gvar->mm.mmrover->next; - if(!mm->mmrover) - mm->mmrover = mm->mmhead; - else if(mm->mmrover == start) + if(!gvar->mm.mmrover) + gvar->mm.mmrover = gvar->mm.mmhead; + else if(gvar->mm.mmrover == start) { Quit("MM_SetLock: Block not found!"); //return; @@ -1170,8 +1170,8 @@ void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm) } while(1); - mm->mmrover->attributes &= ~LOCKBIT; - mm->mmrover->attributes |= locked*LOCKBIT; + gvar->mm.mmrover->attributes &= ~LOCKBIT; + gvar->mm.mmrover->attributes |= locked*LOCKBIT; } //========================================================================== @@ -1186,7 +1186,7 @@ void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm) ===================== */ -void MM_SortMem(mminfo_t *mm) +void MM_SortMem(global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *last,huge *next; mmblocktype far *scan,far *last,far *next; @@ -1219,7 +1219,7 @@ void MM_SortMem(mminfo_t *mm) if(beforesort) beforesort(); - scan = mm->mmhead; + scan = gvar->mm.mmhead; last = NULL; // shut up compiler warning @@ -1241,7 +1241,7 @@ void MM_SortMem(mminfo_t *mm) // next = scan->next; FREEBLOCK(scan); - //MM_FreeBlock(scan, mm); + //MM_FreeBlock(scan, gvar); last->next = next; scan = next; continue; @@ -1276,7 +1276,7 @@ void MM_SortMem(mminfo_t *mm) scan = scan->next; // go to next block } - mm->mmrover = mm->mmhead; + gvar->mm.mmrover = gvar->mm.mmhead; if(aftersort) aftersort(); @@ -1430,14 +1430,14 @@ void MM_ShowMemory(global_game_variables_t *gvar) /*strcpy(scratch1, AARESET); strcat(scratch1, "========================================\n"); strcat(scratch1, "near= "); - ultoa (*(mm->nearheap),str,10); + ultoa (*(gvar->mm.nearheap),str,10); strcat (scratch1,str); strcat(scratch1, " far= "); - ultoa (*(mm->farheap),str,10); + ultoa (*(gvar->mm.farheap),str,10); strcat (scratch1,str); strcat(scratch1, "\n"); - //strcat(scratch1, "&near= %Fp ", &(mm->nearheap)); - //strcat(scratch1, "&far= %Fp", &(mm->farheap)); + //strcat(scratch1, "&near= %Fp ", &(gvar->mm.nearheap)); + //strcat(scratch1, "&far= %Fp", &(gvar->mm.farheap)); //strcat(scratch1, "\n"); strcat(scratch1, "========================================\n"); write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/ @@ -1460,7 +1460,7 @@ void MM_ShowMemory(global_game_variables_t *gvar) ===================== */ -void MM_DumpData(mminfo_t *mm) +void MM_DumpData(global_game_variables_t *gvar) { //huge mmblocktype huge *scan,huge *best; mmblocktype far *scan,far *best; @@ -1469,7 +1469,7 @@ void MM_DumpData(mminfo_t *mm) byte lock,purge; FILE *dumpfile; - free(mm->nearheap); + free(gvar->mm.nearheap); #ifdef __BORLANDC__ dumpfile = fopen ("mmdump.16b","w"); #endif @@ -1487,7 +1487,7 @@ void MM_DumpData(mminfo_t *mm) oldlowest = lowest; lowest = 0xffff; - scan = mm->mmhead; + scan = gvar->mm.mmhead; while (scan) { owner = (unsigned)scan->useptr; @@ -1534,14 +1534,14 @@ void MM_DumpData(mminfo_t *mm) ====================== */ -dword MM_UnusedMemory(mminfo_t *mm) +dword MM_UnusedMemory(global_game_variables_t *gvar) { dword free; //huge mmblocktype huge *scan; mmblocktype far *scan; free = 0; - scan = mm->mmhead; + scan = gvar->mm.mmhead; while(scan->next) { @@ -1566,14 +1566,14 @@ dword MM_UnusedMemory(mminfo_t *mm) ====================== */ -dword MM_TotalFree(mminfo_t *mm) +dword MM_TotalFree(global_game_variables_t *gvar) { dword free; //huge mmblocktype huge *scan; mmblocktype far *scan; free = 0; - scan = mm->mmhead; + scan = gvar->mm.mmhead; while(scan->next) { @@ -1605,20 +1605,20 @@ void MM_Report_(global_game_variables_t *gvar) if(MML_CheckForEMS()) { printf(" LIMEMS\n"); - printf(" EMM v%x.%x available\n", gvar->mm.EMSVer>>4,gvar->mm.EMSVer&0x0F); + printf(" EMM v%x.%x available\n", gvar->pm.emm.EMSVer>>4,gvar->pm.emm.EMSVer&0x0F); printf(" totalEMSpages: %u ", gvar->mm.totalEMSpages); printf("freeEMSpages: %u\n", gvar->mm.freeEMSpages); printf(" EMSpageframe: %x\n", gvar->mm.EMSpageframe); } - if(MML_CheckForXMS(&(gvar->mm))) + if(MML_CheckForXMS(gvar)) { printf(" XMS\n"); printf(" XMSaddr: %X\n", *XMSaddr); } printf("near: %lu ", gvar->mmi.nearheap); printf("far: %lu\n", gvar->mmi.farheap); if(MML_CheckForEMS()) - printf("EMSmem: %lu ", gvar->mmi.EMSmem); if(MML_CheckForXMS(&(gvar->mm))) printf("XMSmem: %lu", gvar->mmi.XMSmem); printf("\n"); + printf("EMSmem: %lu ", gvar->mmi.EMSmem); if(MML_CheckForXMS(gvar)) printf("XMSmem: %lu", gvar->mmi.XMSmem); printf("\n"); //printf("mainmem: %lu\n", gvar->mmi.mainmem); - printf("Total convmem: %lu ", gvar->mmi.mainmem); printf("TotalFree: %lu ", MM_TotalFree(&(gvar->mm))); printf("TotalUsed: %lu\n", gvar->mmi.mainmem+gvar->mmi.EMSmem+gvar->mmi.XMSmem+gvar->mmi.XMSmem); - printf(" UnusedMemory: %lu\n", MM_UnusedMemory(&(gvar->mm))); + printf("Total convmem: %lu ", gvar->mmi.mainmem); printf("TotalFree: %lu ", MM_TotalFree(gvar)); printf("TotalUsed: %lu\n", gvar->mmi.mainmem+gvar->mmi.EMSmem+gvar->mmi.XMSmem+gvar->mmi.XMSmem); + printf(" UnusedMemory: %lu\n", MM_UnusedMemory(gvar)); } //========================================================================== @@ -1762,30 +1762,30 @@ void MM_EMSerr(byte *stri, byte err) ===================== */ -void MM_BombOnError(boolean bomb, mminfo_t *mm) +void MM_BombOnError(boolean bomb, global_game_variables_t *gvar) { - mm->bombonerror = bomb; + gvar->mm.bombonerror = bomb; } -/*void MM_GetNewBlock(mminfo_t *mm) +/*void MM_GetNewBlock(global_game_variables_t *gvar) { - if(!mm->mmfree) - MML_ClearBlock(mm); - mm->mmnew=mm->mmfree; - mm->mmfree=mm->mmfree->next; - if(!(mm->mmnew=mm->mmfree)) + if(!gvar->mm.mmfree) + MML_ClearBlock(gvar); + gvar->mm.mmnew=gvar->mm.mmfree; + gvar->mm.mmfree=gvar->mm.mmfree->next; + if(!(gvar->mm.mmnew=gvar->mm.mmfree)) { printf("MM_GETNEWBLOCK: No free blocks!\n"); return; } - mm->mmfree=mm->mmfree->next; + gvar->mm.mmfree=gvar->mm.mmfree->next; } -void MM_FreeBlock(mmblocktype *x, mminfo_t *mm) +void MM_FreeBlock(mmblocktype *x, global_game_variables_t *gvar) { x->useptr=NULL; - x->next=mm->mmfree; - mm->mmfree=x; + x->next=gvar->mm.mmfree; + gvar->mm.mmfree=x; }*/ /*void MM_seguin(void) diff --git a/src/lib/16_mm.h b/src/lib/16_mm.h index bdc97429..7995bf23 100755 --- a/src/lib/16_mm.h +++ b/src/lib/16_mm.h @@ -36,9 +36,9 @@ #endif //#define GETNEWBLOCK {if(!(mmnew=mmfree))Quit("MM_GETNEWBLOCK: No free blocks!") ;mmfree=mmfree->next;} -#define GETNEWBLOCK {if(!mm->mmfree)MML_ClearBlock(mm);mm->mmnew=mm->mmfree;mm->mmfree=mm->mmfree->next;} +#define GETNEWBLOCK {if(!gvar->mm.mmfree)MML_ClearBlock(gvar);gvar->mm.mmnew=gvar->mm.mmfree;gvar->mm.mmfree=gvar->mm.mmfree->next;} -#define FREEBLOCK(x) {*x->useptr=NULL;x->next=mm->mmfree;mm->mmfree=x;} +#define FREEBLOCK(x) {*x->useptr=NULL;x->next=gvar->mm.mmfree;gvar->mm.mmfree=x;} #define SAVENEARHEAP 0x200 // space to leave in data segment #define SAVEFARHEAP 0//x400 // space to leave in far heap @@ -66,10 +66,16 @@ //-------- #define XMS_INT 0x2f -#define XMSD word XMSDriver=gvar->pm.xmm.XMSDriver; -#define XMS_CALL(v) CPURegs.h.ah = (v);\ - XMSDriver=gvar->pm.xmm.XMSDriver;\ - __asm call [WORD PTR XMSDriver] +#define XMSD dword XMSDriver=gvar->pm.xmm.XMSDriver; +inline void XMS_CALL(byte v, global_game_variables_t *gvar) +{ + XMSD; + XMSDriver=gvar->pm.xmm.XMSDriver; + __asm { + mov v,ah + call [WORD PTR XMSDriver] + } +} #define XMS_VERSION 0x00 @@ -125,7 +131,7 @@ extern void (* XMSaddr) (void); // far pointer to XMS driver //moved to typedefst //#define MAXUMBS 12 -#define MAPPAGES 4//mm->EMSpagesmapped +#define MAPPAGES 4//gvar->mm.EMSpagesmapped //moved to typdefst /*typedef struct mmblockstruct @@ -181,31 +187,31 @@ typedef struct //========================================================================== boolean MML_CheckForEMS(void); -byte MML_SetupEMS(mminfo_t *mm); -void MML_ShutdownEMS(mminfo_t *mm); -byte MM_MapEMS(mminfo_t *mm, mminfotype *mmi); -byte MM_MapXEMS(mminfo_t *mm, mminfotype *mmi); -boolean MML_CheckForXMS(mminfo_t *mm); -void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi); -void MML_ShutdownXMS(mminfo_t *mm); -void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm); -void MML_ClearBlock(mminfo_t *mm); - -void MM_Startup(mminfo_t *mm, mminfotype *mmi); -void MM_Shutdown(mminfo_t *mm); - -void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi); -void MM_FreePtr(memptr *baseptr, mminfo_t *mm); -void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm); -void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm); -void MM_SortMem(mminfo_t *mm); +//byte MML_SetupEMS(mminfo_t *mm); +//void MML_ShutdownEMS(mminfo_t *mm); +byte MM_MapEMS(global_game_variables_t *gvar); +byte MM_MapXEMS(global_game_variables_t *gvar); +//boolean MML_CheckForXMS(mminfo_t *mm); +//void MML_SetupXMS(mminfo_t *mm, mminfotype *mmi); +//void MML_ShutdownXMS(mminfo_t *mm); +void MML_UseSpace(word segstart, dword seglength, global_game_variables_t *gvar); +void MML_ClearBlock(global_game_variables_t *gvar); + +void MM_Startup(global_game_variables_t *gvar); +void MM_Shutdown(global_game_variables_t *gvar); + +void MM_GetPtr(memptr *baseptr,dword size, global_game_variables_t *gvar); +void MM_FreePtr(memptr *baseptr, global_game_variables_t *gvar); +void MM_SetPurge(memptr *baseptr, int purge, global_game_variables_t *gvar); +void MM_SetLock(memptr *baseptr, boolean locked, global_game_variables_t *gvar); +void MM_SortMem(global_game_variables_t *gvar); void MM_ShowMemory(global_game_variables_t *gvar);//,/*page_t *page, */mminfo_t *mm); -void MM_DumpData(mminfo_t *mm); -dword MM_UnusedMemory(mminfo_t *mm); -dword MM_TotalFree(mminfo_t *mm); +void MM_DumpData(global_game_variables_t *gvar); +dword MM_UnusedMemory(global_game_variables_t *gvar); +dword MM_TotalFree(global_game_variables_t *gvar); void MM_Report_(global_game_variables_t *gvar); static void MM_EMSerr(byte *stri, byte err); -void MM_BombOnError(boolean bomb, mminfo_t *mm); +void MM_BombOnError(boolean bomb, global_game_variables_t *gvar); //void MM_GetNewBlock(mminfo_t *mm); //void MM_FreeBlock(mmblocktype *x, mminfo_t *mm); diff --git a/src/lib/16_pm.c b/src/lib/16_pm.c index 0b04b16e..97401951 100755 --- a/src/lib/16_pm.c +++ b/src/lib/16_pm.c @@ -1,3 +1,25 @@ +/* Project 16 Source Code~ + * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover + * + * This file is part of Project 16. + * + * Project 16 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 3 of the License, or + * (at your option) any later version. + * + * Project 16 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, see , or + * write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + // // ID_PM.C // Id Engine's Page Manager v1.0 @@ -6,18 +28,19 @@ #include "src/lib/16_pm.h" #pragma hdrstop +union REGS CPURegs; /* // Main Mem specific variables boolean MainPresent; memptr MainMemPages[PMMaxMainMem]; - PMBlockAttr MainMemUsed[PMMaxMainMem]; - int MainPagesAvail; + PMBlockAttr gvar->pm.mm.MainMemUsed[PMMaxMainMem]; + int gvar->pm.mm.MainPagesAvail; // EMS specific variables boolean gvar->pm.emm.EMSPresent; word gvar->pm.emm.EMSAvail,gvar->pm.emm.EMSPagesAvail,gvar->pm.emm.EMSHandle, gvar->pm.emm.EMSPageFrame,gvar->pm.emm.EMSPhysicalPage; - EMSListStruct EMSList[EMSFrameCount]; + gvar->pm.emm.EMSListStruct gvar->pm.emm.EMSList[EMSFrameCount]; // XMS specific variables boolean gvar->pm.xmm.XMSPresent; @@ -26,22 +49,22 @@ int gvar->pm.xmm.XMSProtectPage = -1; // File specific variables - char PageFileName[13] = {"VSWAP."}; + char gvar->pm.fi.PageFileName[13] = {"VSWAP."}; int PageFile = -1; word gvar->pm.fi.ChunksInFile; word PMSpriteStart,PMSoundStart; // General usage variables boolean PMStarted, - PMPanicMode, - PMThrashing; - word gvar->pm.xmm.XMSPagesUsed, - gvar->pm.emm.EMSPagesUsed, + gvar->pm.PMPanicMode, + gvar->pm.PMThrashing; + word gvar->pm.XMSPagesUsed, + gvar->pm.EMSPagesUsed, MainPagesUsed, - PMNumBlocks; + gvar->pm.PMNumBlocks; long PMFrameCount; - PageListStruct far *PMPages, - _seg *PMSegPages; + PageListStruct far *gvar->pm.PMPages, + _seg *gvar->pm.PMSegPages; */ static char *ParmStrings[] = {"nomain","noems","noxms",nil}; @@ -62,8 +85,7 @@ PML_MapEMS(word logical, word physical, global_game_variables_t *gvar) CPURegs.x.bx = logical; CPURegs.x.dx = gvar->pm.emm.EMSHandle; CPURegs.h.ah = EMS_MAPPAGE; - __asm - { + __asm { int EMS_INT } @@ -257,7 +279,7 @@ PML_StartupXMS(global_game_variables_t *gvar) mov [WORD PTR XMSDriver+2],es // function pointer to XMS driver } - XMS_CALL(XMS_QUERYFREE); // Find out how much XMS is available + XMS_CALL(XMS_QUERYFREE, gvar); // Find out how much XMS is available gvar->pm.xmm.XMSAvail = CPURegs.x.ax; if (!CPURegs.x.ax) // AJR: bugfix 10/8/92 goto error; @@ -267,7 +289,7 @@ PML_StartupXMS(global_game_variables_t *gvar) goto error; CPURegs.x.dx = gvar->pm.xmm.XMSAvail; - XMS_CALL(XMS_ALLOC); // And do the allocation + XMS_CALL(XMS_ALLOC, gvar); // And do the allocation gvar->pm.xmm.XMSHandle = CPURegs.x.dx; if (!CPURegs.x.ax) // AJR: bugfix 10/8/92 @@ -319,7 +341,7 @@ PML_XMSCopy(boolean toxms,byte far *addr,word xmspage,word length, global_game_v push si } CPURegs.x.si = (word)© - XMS_CALL(XMS_MOVE); + XMS_CALL(XMS_MOVE, gvar); __asm { pop si } @@ -331,17 +353,17 @@ PML_XMSCopy(boolean toxms,byte far *addr,word xmspage,word length, global_game_v } #if 1 -#define PML_CopyToXMS(s,t,l) PML_XMSCopy(true,(s),(t),(l)) -#define PML_CopyFromXMS(t,s,l) PML_XMSCopy(false,(t),(s),(l)) +#define PML_CopyToXMS(s,t,l,gvar) PML_XMSCopy(true,(s),(t),(l),(gvar)) +#define PML_CopyFromXMS(t,s,l,gvar) PML_XMSCopy(false,(t),(s),(l),(gvar)) #else // // PML_CopyToXMS() - Copies the specified number of bytes from the real mode // segment address to the specified XMS page // void -PML_CopyToXMS(byte far *source,int targetpage,word length) +PML_CopyToXMS(byte far *source,int targetpage,word length,global_game_variables_t *gvar) { - PML_XMSCopy(true,source,targetpage,length); + PML_XMSCopy(true,source,targetpage,length, global_game_variables_t *gvar); } // @@ -349,9 +371,9 @@ PML_CopyToXMS(byte far *source,int targetpage,word length) // page to the specified real mode address // void -PML_CopyFromXMS(byte far *target,int sourcepage,word length) +PML_CopyFromXMS(byte far *target,int sourcepage,word length, global_game_variables_t *gvar) { - PML_XMSCopy(false,target,sourcepage,length); + PML_XMSCopy(false,target,sourcepage,length, global_game_variables_t *gvar); } #endif @@ -365,7 +387,7 @@ PML_ShutdownXMS(global_game_variables_t *gvar) if (gvar->pm.xmm.XMSPresent) { CPURegs.x.dx = gvar->pm.xmm.XMSHandle; - XMS_CALL(XMS_FREE); + XMS_CALL(XMS_FREE, gvar); if (CPURegs.h.bl) { Quit("PML_ShutdownXMS: Error freeing XMS"); @@ -392,8 +414,7 @@ PM_SetMainMemPurge(int level, global_game_variables_t *gvar) for (i = 0;i < PMMaxMainMem;i++) if (gvar->pm.mm.MainMemPages[i]) - MM_SetPurge(&gvar->pm.mm.MainMemPages[i],level); - return 0;//TODO FIX + MM_SetPurge(&gvar->pm.mm.MainMemPages[i],level, gvar); } // @@ -410,7 +431,7 @@ PM_SetMainMemPurge(int level, global_game_variables_t *gvar) // program makes allocation requests of the Memory Mgr. // void -PM_CheckMainMem(void) +PM_CheckMainMem(global_game_variables_t *gvar) { boolean allocfailed; int i,n; @@ -418,15 +439,15 @@ PM_CheckMainMem(void) PMBlockAttr *used; PageListStruct far *page; - if (!MainPresent) + if (!gvar->pm.mm.MainPresent) return; - for (i = 0,page = PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) + for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) { n = page->mainPage; if (n != -1) // Is the page using main memory? { - if (!MainMemPages[n]) // Yep, was the block purged? + if (!gvar->pm.mm.MainMemPages[n]) // Yep, was the block purged? { page->mainPage = -1; // Yes, mark page as purged & unlocked page->locked = pml_Unlocked; @@ -435,41 +456,41 @@ PM_CheckMainMem(void) } // Prevent allocation attempts from purging any of our other blocks - PM_LockMainMem(); + PM_LockMainMem(gvar); allocfailed = false; - for (i = 0,p = MainMemPages,used = MainMemUsed;i < PMMaxMainMem;i++,p++,used++) + for (i = 0,p = gvar->pm.mm.MainMemPages,used = gvar->pm.mm.MainMemUsed; i < PMMaxMainMem;i++,p++,used++) { if (!*p) // If the page got purged { if (*used & pmba_Allocated) // If it was allocated { *used &= ~pmba_Allocated; // Mark as unallocated - MainPagesAvail--; // and decrease available count + gvar->pm.mm.MainPagesAvail--; // and decrease available count } if (*used & pmba_Used) // If it was used { *used &= ~pmba_Used; // Mark as unused - MainPagesUsed--; // and decrease used count + gvar->pm.MainPagesUsed--; // and decrease used count } if (!allocfailed) { - MM_BombOnError(false); - MM_GetPtr(p,PMPageSize); // Try to reallocate - if (mmerror) // If it failed, + MM_BombOnError(false, gvar); + MM_GetPtr(p,PMPageSize, gvar); // Try to reallocate + if (gvar->mm.mmerror) // If it failed, allocfailed = true; // don't try any more allocations else // If it worked, { *used |= pmba_Allocated; // Mark as allocated - MainPagesAvail++; // and increase available count + gvar->pm.mm.MainPagesAvail++; // and increase available count } - MM_BombOnError(true); + MM_BombOnError(true, gvar); } } } - if (mmerror) - mmerror = false; + if (gvar->mm.mmerror) + gvar->mm.mmerror = false; } // @@ -479,31 +500,31 @@ PM_CheckMainMem(void) // needs to be called. // void -PML_StartupMainMem(void) +PML_StartupMainMem(global_game_variables_t *gvar) { int i,n; memptr *p; - MainPagesAvail = 0; - MM_BombOnError(false); - for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++) + gvar->pm.mm.MainPagesAvail = 0; + MM_BombOnError(false, gvar); + for (i = 0,p = gvar->pm.mm.MainMemPages;i < PMMaxMainMem;i++,p++) { - MM_GetPtr(p,PMPageSize); - if (mmerror) + MM_GetPtr(p,PMPageSize, gvar); + if (gvar->mm.mmerror) break; - MainPagesAvail++; - MainMemUsed[i] = pmba_Allocated; + gvar->pm.mm.MainPagesAvail++; + gvar->pm.mm.MainMemUsed[i] = pmba_Allocated; } - MM_BombOnError(true); - if (mmerror) - mmerror = false; - if (MainPagesAvail < PMMinMainMem) + MM_BombOnError(true, gvar); + if (gvar->mm.mmerror) + gvar->mm.mmerror = false; + if (gvar->pm.mm.MainPagesAvail < PMMinMainMem) { Quit("PM_SetupMainMem: Not enough main memory"); //return; } - MainPresent = true; + gvar->pm.mm.MainPresent = true; } // @@ -511,15 +532,15 @@ PML_StartupMainMem(void) // Page Mgr. // void -PML_ShutdownMainMem(void) +PML_ShutdownMainMem(global_game_variables_t *gvar) { int i; memptr *p; // DEBUG - mark pages as unallocated & decrease page count as appropriate - for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++) + for (i = 0,p = gvar->pm.mm.MainMemPages;i < PMMaxMainMem;i++,p++) if (*p) - MM_FreePtr(p); + MM_FreePtr(p, gvar); } ///////////////////////////////////////////////////////////////////////////// @@ -532,7 +553,7 @@ PML_ShutdownMainMem(void) // PML_ReadFromFile() - Reads some data in from the page file // void -PML_ReadFromFile(byte far *buf,long offset,word length) +PML_ReadFromFile(byte far *buf,long offset,word length, global_game_variables_t *gvar) { if (!buf) { @@ -544,12 +565,13 @@ PML_ReadFromFile(byte far *buf,long offset,word length) Quit("PML_ReadFromFile: Zero offset"); //return; } - if (lseek(PageFile,offset,SEEK_SET) != offset) + if (lseek(gvar->pm.fi.PageFile,offset,SEEK_SET) != offset) { Quit("PML_ReadFromFile: Seek failed"); //return; } - if (!CA_FarRead(PageFile,buf,length)) + if (!CA_FarRead(gvar->pm.fi.PageFile,buf,length, gvar)) + { Quit("PML_ReadFromFile: Read failed"); //return; } @@ -559,73 +581,74 @@ PML_ReadFromFile(byte far *buf,long offset,word length) // PML_OpenPageFile() - Opens the page file and sets up the page info // void -PML_OpenPageFile(void) +PML_OpenPageFile(global_game_variables_t *gvar) { int i; long size; - __SEGA *buf; + //__SEGA buf; + memptr buf; dword far *offsetptr; word far *lengthptr; PageListStruct far *page; - PageFile = open(PageFileName,O_RDONLY + O_BINARY); - if (PageFile == -1) + gvar->pm.fi.PageFile = open(gvar->pm.fi.PageFileName,O_RDONLY + O_BINARY); + if (gvar->pm.fi.PageFile == -1) { Quit("PML_OpenPageFile: Unable to open page file"); //return; } // Read in header variables - read(PageFile,&gvar->pm.fi.ChunksInFile,sizeof(gvar->pm.fi.ChunksInFile)); - read(PageFile,&PMSpriteStart,sizeof(PMSpriteStart)); - read(PageFile,&PMSoundStart,sizeof(PMSoundStart)); + read(gvar->pm.fi.PageFile,&gvar->pm.fi.ChunksInFile,sizeof(gvar->pm.fi.ChunksInFile)); + read(gvar->pm.fi.PageFile,&gvar->pm.fi.PMSpriteStart,sizeof(gvar->pm.fi.PMSpriteStart)); + read(gvar->pm.fi.PageFile,&gvar->pm.fi.PMSoundStart,sizeof(gvar->pm.fi.PMSoundStart)); // Allocate and clear the page list - PMNumBlocks = gvar->pm.fi.ChunksInFile; - MM_GetPtr(&(memptr)PMSegPages,sizeof(PageListStruct) * PMNumBlocks); - MM_SetLock(&(memptr)PMSegPages,true); - PMPages = (PageListStruct far *)PMSegPages; - _fmemset(PMPages,0,sizeof(PageListStruct) * PMNumBlocks); + gvar->pm.PMNumBlocks = gvar->pm.fi.ChunksInFile; + MM_GetPtr((memptr *)gvar->pm.PMSegPages, sizeof(PageListStruct) * (gvar->pm.PMNumBlocks), gvar); + MM_SetLock((memptr *)gvar->pm.PMSegPages,true, gvar); + gvar->pm.PMPages = (PageListStruct far *)gvar->pm.PMSegPages; + _fmemset(gvar->pm.PMPages,0,sizeof(PageListStruct) * gvar->pm.PMNumBlocks); // Read in the chunk offsets size = sizeof(dword) * gvar->pm.fi.ChunksInFile; - MM_GetPtr(&buf,size); - if (!CA_FarRead(PageFile,(byte far *)buf,size)) + MM_GetPtr(&buf, size, gvar); + if (!CA_FarRead(gvar->pm.fi.PageFile,(byte far *)buf,size, gvar)) { Quit("PML_OpenPageFile: Offset read failed"); //return; } offsetptr = (dword far *)buf; - for (i = 0,page = PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) + for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) page->offset = *offsetptr++; - MM_FreePtr(&buf); + MM_FreePtr(&buf, gvar); // Read in the chunk lengths size = sizeof(word) * gvar->pm.fi.ChunksInFile; - MM_GetPtr(&buf,size); - if (!CA_FarRead(PageFile,(byte far *)buf,size)) + MM_GetPtr(&buf,size, gvar); + if (!CA_FarRead(gvar->pm.fi.PageFile,(byte far *)buf,size, gvar)) { Quit("PML_OpenPageFile: Length read failed"); //return; } lengthptr = (word far *)buf; - for (i = 0,page = PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) + for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.fi.ChunksInFile;i++,page++) page->length = *lengthptr++; - MM_FreePtr(&buf); + MM_FreePtr(&buf, gvar); } // // PML_ClosePageFile() - Closes the page file // void -PML_ClosePageFile(void) +PML_ClosePageFile(global_game_variables_t *gvar) { - if (PageFile != -1) - close(PageFile); - if (PMSegPages) + if (gvar->pm.fi.PageFile != -1) + close(gvar->pm.fi.PageFile); + if (gvar->pm.PMSegPages) { - MM_SetLock(&(memptr)PMSegPages,false); - MM_FreePtr(&(__SEGA *)PMSegPages); + MM_SetLock((memptr)gvar->pm.PMSegPages,false, gvar); + MM_FreePtr((memptr)gvar->pm.PMSegPages, gvar); } } @@ -636,16 +659,16 @@ PML_ClosePageFile(void) ///////////////////////////////////////////////////////////////////////////// // -// PML_Getgvar->pm.emm.EMSAddress() +// PML_GetEMSAddress() // // Page is in EMS, so figure out which EMS physical page should be used // to map our page in. If normal page, use EMS physical page 3, else // use the physical page specified by the lock type // -#ifdef __DEBUG__ +#ifndef __DEBUG_2__ #pragma argsused // DEBUG - remove lock parameter memptr -PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) +PML_GetEMSAddress(int page,PMLockType lock, global_game_variables_t *gvar) { int i,emspage; word emsoff,emsbase,offset; @@ -657,7 +680,7 @@ PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) // See if this page is already mapped in for (i = 0;i < EMSFrameCount;i++) { - if (EMSList[i].basegvar->pm.emm.EMSPage == emsbase) + if (gvar->pm.emm.EMSList[i].baseEMSPage == emsbase) { emspage = i; // Yep - don't do a redundant remapping break; @@ -667,31 +690,31 @@ PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) // If page isn't already mapped in, find LRU EMS frame, and use it if (emspage == -1) { - dword last = MAXLONG; + dword last = LONG_MAX; for (i = 0;i < EMSFrameCount;i++) { - if (EMSList[i].lastHit < last) + if (gvar->pm.emm.EMSList[i].lastHit < last) { emspage = i; - last = EMSList[i].lastHit; + last = gvar->pm.emm.EMSList[i].lastHit; } } - EMSList[emspage].basegvar->pm.emm.EMSPage = emsbase; - PML_MapEMS(page / PMEMSSubPage,emspage); + gvar->pm.emm.EMSList[emspage].baseEMSPage = emsbase; + PML_MapEMS(page / PMEMSSubPage,emspage, gvar); } if (emspage == -1) - Quit("PML_Getgvar->pm.emm.EMSAddress: EMS find failed"); + Quit("PML_GetEMSAddress: EMS find failed"); - EMSList[emspage].lastHit = PMFrameCount; + gvar->pm.emm.EMSList[emspage].lastHit = gvar->pm.PMFrameCount; offset = emspage * EMSPageSizeSeg; offset += emsoff * PMPageSizeSeg; return((memptr)(gvar->pm.emm.EMSPageFrame + offset)); } #else memptr -PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) +PML_GetEMSAddress(int page,PMLockType lock, global_game_variables_t *gvar) { word emspage; @@ -699,7 +722,7 @@ PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) PML_MapEMS(page / PMEMSSubPage,emspage); - return((memptr)(gvar->pm.emm.EMSPageFrame + (emspage * EMSPageSizeSeg) + return((memptr)(EMSPageFrame + (emspage * EMSPageSizeSeg) + ((page & (PMEMSSubPage - 1)) * PMPageSizeSeg))); } #endif @@ -711,15 +734,15 @@ PML_Getgvar->pm.emm.EMSAddress(int page,PMLockType lock) // // memptr -PM_GetPageAddress(int pagenum) +PM_GetPageAddress(int pagenum, global_game_variables_t *gvar) { PageListStruct far *page; - page = &PMPages[pagenum]; + page = &gvar->pm.PMPages[pagenum]; if (page->mainPage != -1) - return(MainMemPages[page->mainPage]); + return(gvar->pm.mm.MainMemPages[page->mainPage]); else if (page->emsPage != -1) - return(PML_Getgvar->pm.emm.EMSAddress(page->emsPage,page->locked)); + return(PML_GetEMSAddress(page->emsPage,page->locked, gvar)); else return(nil); } @@ -729,13 +752,13 @@ PM_GetPageAddress(int pagenum) // present & unlocked main/EMS page (or main page if mainonly is true) // int -PML_GiveLRUPage(boolean mainonly) +PML_GiveLRUPage(boolean mainonly, global_game_variables_t *gvar) { int i,lru; long last; PageListStruct far *page; - for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < gvar->pm.fi.ChunksInFile;i++,page++) + for (i = 0,page = gvar->pm.PMPages,lru = -1,last = LONG_MAX;i < gvar->pm.fi.ChunksInFile;i++,page++) { if ( @@ -756,18 +779,18 @@ PML_GiveLRUPage(boolean mainonly) } // -// PML_GiveLRUgvar->pm.xmm.XMSPage() - Returns the page # of the least recently used +// PML_GiveLRUXMSPage() - Returns the page # of the least recently used // (and present) XMS page. // This routine won't return the XMS page protected (by gvar->pm.xmm.XMSProtectPage) // int -PML_GiveLRUgvar->pm.xmm.XMSPage(void) +PML_GiveLRUXMSPage(global_game_variables_t *gvar) { int i,lru; long last; PageListStruct far *page; - for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < gvar->pm.fi.ChunksInFile;i++,page++) + for (i = 0,page = gvar->pm.PMPages,lru = -1,last = LONG_MAX;i < gvar->pm.fi.ChunksInFile;i++,page++) { if ( @@ -788,7 +811,7 @@ PML_GiveLRUgvar->pm.xmm.XMSPage(void) // it with the main/EMS page // void -PML_PutPageInXMS(int pagenum) +PML_PutPageInXMS(int pagenum, global_game_variables_t *gvar) { int usexms; PageListStruct far *page; @@ -796,21 +819,21 @@ PML_PutPageInXMS(int pagenum) if (!gvar->pm.xmm.XMSPresent) return; - page = &PMPages[pagenum]; + page = &gvar->pm.PMPages[pagenum]; if (page->xmsPage != -1) return; // Already in XMS - if (gvar->pm.xmm.XMSPagesUsed < gvar->pm.xmm.XMSPagesAvail) - page->xmsPage = gvar->pm.xmm.XMSPagesUsed++; + if (gvar->pm.XMSPagesUsed < gvar->pm.xmm.XMSPagesAvail) + page->xmsPage = gvar->pm.XMSPagesUsed++; else { - usexms = PML_GiveLRUgvar->pm.xmm.XMSPage(); + usexms = PML_GiveLRUXMSPage(gvar); if (usexms == -1) Quit("PML_PutPageInXMS: No XMS LRU"); - page->xmsPage = PMPages[usexms].xmsPage; - PMPages[usexms].xmsPage = -1; + page->xmsPage = gvar->pm.PMPages[usexms].xmsPage; + gvar->pm.PMPages[usexms].xmsPage = -1; } - PML_CopyToXMS(PM_GetPageAddress(pagenum),page->xmsPage,page->length); + PML_CopyToXMS(PM_GetPageAddress(pagenum, gvar),page->xmsPage,page->length, gvar); } // @@ -818,7 +841,7 @@ PML_PutPageInXMS(int pagenum) // the old one's address space. Returns the address of the new page. // memptr -PML_TransferPageSpace(int orig,int new) +PML_TransferPageSpace(int orig,int new, global_game_variables_t *gvar) { memptr addr; PageListStruct far *origpage,far *newpage; @@ -826,8 +849,8 @@ PML_TransferPageSpace(int orig,int new) if (orig == new) Quit("PML_TransferPageSpace: Identity replacement"); - origpage = &PMPages[orig]; - newpage = &PMPages[new]; + origpage = &gvar->pm.PMPages[orig]; + newpage = &gvar->pm.PMPages[new]; if (origpage->locked != pml_Unlocked) Quit("PML_TransferPageSpace: Killing locked page"); @@ -836,10 +859,10 @@ PML_TransferPageSpace(int orig,int new) Quit("PML_TransferPageSpace: Reusing non-existent page"); // Copy page that's about to be purged into XMS - PML_PutPageInXMS(orig); + PML_PutPageInXMS(orig, gvar); // Get the address, and force EMS into a physical page if necessary - addr = PM_GetPageAddress(orig); + addr = PM_GetPageAddress(orig, gvar); // Steal the address newpage->emsPage = origpage->emsPage; @@ -863,24 +886,24 @@ PML_TransferPageSpace(int orig,int new) // will be looked at by PML_GiveLRUPage(). // byte far * -PML_GetAPageBuffer(int pagenum,boolean mainonly) +PML_GetAPageBuffer(int pagenum,boolean mainonly, global_game_variables_t *gvar) { byte far *addr = nil; int i,n; PMBlockAttr *used; PageListStruct far *page; - page = &PMPages[pagenum]; - if ((gvar->pm.emm.EMSPagesUsed < gvar->pm.emm.EMSPagesAvail) && !mainonly) + page = &gvar->pm.PMPages[pagenum]; + if ((gvar->pm.EMSPagesUsed < gvar->pm.emm.EMSPagesAvail) && !mainonly) { // There's remaining EMS - use it - page->emsPage = gvar->pm.emm.EMSPagesUsed++; - addr = PML_Getgvar->pm.emm.EMSAddress(page->emsPage,page->locked); + page->emsPage = gvar->pm.EMSPagesUsed++; + addr = PML_GetEMSAddress(page->emsPage,page->locked, gvar); } - else if (MainPagesUsed < MainPagesAvail) + else if (gvar->pm.MainPagesUsed < gvar->pm.mm.MainPagesAvail) { // There's remaining main memory - use it - for (i = 0,n = -1,used = MainMemUsed;i < PMMaxMainMem;i++,used++) + for (i = 0,n = -1,used = gvar->pm.mm.MainMemUsed;i < PMMaxMainMem;i++,used++) { if ((*used & pmba_Allocated) && !(*used & pmba_Used)) { @@ -891,14 +914,14 @@ PML_GetAPageBuffer(int pagenum,boolean mainonly) } if (n == -1) Quit("PML_GetPageBuffer: MainPagesAvail lied"); - addr = MainMemPages[n]; + addr = gvar->pm.mm.MainMemPages[n]; if (!addr) Quit("PML_GetPageBuffer: Purged main block"); page->mainPage = n; - MainPagesUsed++; + gvar->pm.MainPagesUsed++; } else - addr = PML_TransferPageSpace(PML_GiveLRUPage(mainonly),pagenum); + addr = PML_TransferPageSpace(PML_GiveLRUPage(mainonly, gvar),pagenum, gvar); if (!addr) Quit("PML_GetPageBuffer: Search failed"); @@ -909,27 +932,27 @@ PML_GetAPageBuffer(int pagenum,boolean mainonly) // PML_GetPageFromXMS() - If page is in XMS, find LRU main/EMS page and // replace it with the page from XMS. If mainonly is true, will only // search for LRU main page. -// gvar->pm.xmm.XMSProtectPage is set to the page to be retrieved from XMS, so that if +// XMSProtectPage is set to the page to be retrieved from XMS, so that if // the page from which we're stealing the main/EMS from isn't in XMS, // it won't copy over the page that we're trying to get from XMS. // (pages that are being purged are copied into XMS, if possible) // memptr -PML_GetPageFromXMS(int pagenum,boolean mainonly) +PML_GetPageFromXMS(int pagenum,boolean mainonly, global_game_variables_t *gvar) { byte far *checkaddr; memptr addr = nil; PageListStruct far *page; - page = &PMPages[pagenum]; + page = &gvar->pm.PMPages[pagenum]; if (gvar->pm.xmm.XMSPresent && (page->xmsPage != -1)) { gvar->pm.xmm.XMSProtectPage = pagenum; - checkaddr = PML_GetAPageBuffer(pagenum,mainonly); + checkaddr = PML_GetAPageBuffer(pagenum,mainonly, gvar); if (FP_OFF(checkaddr)) Quit("PML_GetPageFromXMS: Non segment pointer"); addr = (memptr)FP_SEG(checkaddr); - PML_CopyFromXMS(addr,page->xmsPage,page->length); + PML_CopyFromXMS(addr,page->xmsPage,page->length, gvar); gvar->pm.xmm.XMSProtectPage = -1; } @@ -942,14 +965,14 @@ PML_GetPageFromXMS(int pagenum,boolean mainonly) // only be loaded into main. // void -PML_LoadPage(int pagenum,boolean mainonly) +PML_LoadPage(int pagenum,boolean mainonly, global_game_variables_t *gvar) { byte far *addr; PageListStruct far *page; - addr = PML_GetAPageBuffer(pagenum,mainonly); - page = &PMPages[pagenum]; - PML_ReadFromFile(addr,page->offset,page->length); + addr = PML_GetAPageBuffer(pagenum,mainonly, gvar); + page = &gvar->pm.PMPages[pagenum]; + PML_ReadFromFile(addr,page->offset,page->length, gvar); } // @@ -960,14 +983,14 @@ PML_LoadPage(int pagenum,boolean mainonly) // #pragma warn -pia memptr -PM_GetPage(int pagenum) +PM_GetPage(int pagenum, global_game_variables_t *gvar) { memptr result; if (pagenum >= gvar->pm.fi.ChunksInFile) Quit("PM_GetPage: Invalid page request"); -#if __DEBUG__ // for debugging +#ifdef __DEBUG_2__ // for debugging __asm { mov dx,STATUS_REGISTER_1 in al,dx @@ -979,23 +1002,23 @@ PM_GetPage(int pagenum) } #endif - if (!(result = PM_GetPageAddress(pagenum))) + if (!(result = PM_GetPageAddress(pagenum, gvar))) { - boolean mainonly = (pagenum >= PMSoundStart); -if (!PMPages[pagenum].offset) // JDC: sparse page + boolean mainonly = (pagenum >= gvar->pm.fi.PMSoundStart); +if (!gvar->pm.PMPages[pagenum].offset) // JDC: sparse page Quit ("Tried to load a sparse page!"); - if (!(result = PML_GetPageFromXMS(pagenum,mainonly))) + if (!(result = PML_GetPageFromXMS(pagenum,mainonly, gvar))) { - if (PMPages[pagenum].lastHit == PMFrameCount) - PMThrashing++; + if (gvar->pm.PMPages[pagenum].lastHit == gvar->pm.PMFrameCount) + gvar->pm.PMThrashing++; - PML_LoadPage(pagenum,mainonly); - result = PM_GetPageAddress(pagenum); + PML_LoadPage(pagenum,mainonly, gvar); + result = PM_GetPageAddress(pagenum, gvar); } } - PMPages[pagenum].lastHit = PMFrameCount; + gvar->pm.PMPages[pagenum].lastHit = gvar->pm.PMFrameCount; -#if __DEBUG__ // for debugging +#ifdef __DEBUG_2__ // for debugging __asm{ mov dx,STATUS_REGISTER_1 in al,dx @@ -1020,12 +1043,12 @@ if (!PMPages[pagenum].offset) // JDC: sparse page // specified when returning the address. For sound stuff. // void -PM_SetPageLock(int pagenum,PMLockType lock) +PM_SetPageLock(int pagenum,PMLockType lock, global_game_variables_t *gvar) { - if (pagenum < PMSoundStart) + if (pagenum < gvar->pm.fi.PMSoundStart) Quit("PM_SetPageLock: Locking/unlocking non-sound page"); - PMPages[pagenum].locked = lock; + gvar->pm.PMPages[pagenum].locked = lock; } // @@ -1034,7 +1057,7 @@ PM_SetPageLock(int pagenum,PMLockType lock) // page, and the total pages that need to be loaded (for thermometer). // void -PM_Preload(boolean (*update)(word current,word total)) +PM_Preload(boolean (*update)(word current,word total), global_game_variables_t *gvar) { int i,j, page,oogypage; @@ -1046,17 +1069,17 @@ PM_Preload(boolean (*update)(word current,word total)) memptr addr; PageListStruct far *p; - mainfree = (MainPagesAvail - MainPagesUsed) + (gvar->pm.emm.EMSPagesAvail - gvar->pm.emm.EMSPagesUsed); - xmsfree = (gvar->pm.xmm.XMSPagesAvail - gvar->pm.xmm.XMSPagesUsed); + mainfree = (gvar->pm.mm.MainPagesAvail - gvar->pm.MainPagesUsed) + (gvar->pm.emm.EMSPagesAvail - gvar->pm.EMSPagesUsed); + xmsfree = (gvar->pm.xmm.XMSPagesAvail - gvar->pm.XMSPagesUsed); xmstotal = maintotal = 0; for (i = 0;i < gvar->pm.fi.ChunksInFile;i++) { - if (!PMPages[i].offset) + if (!gvar->pm.PMPages[i].offset) continue; // sparse - if ( PMPages[i].emsPage != -1 || PMPages[i].mainPage != -1 ) + if ( gvar->pm.PMPages[i].emsPage != -1 || gvar->pm.PMPages[i].mainPage != -1 ) continue; // already in main mem if ( mainfree ) @@ -1064,7 +1087,7 @@ PM_Preload(boolean (*update)(word current,word total)) maintotal++; mainfree--; } - else if ( xmsfree && (PMPages[i].xmsPage == -1) ) + else if ( xmsfree && (gvar->pm.PMPages[i].xmsPage == -1) ) { xmstotal++; xmsfree--; @@ -1085,14 +1108,14 @@ PM_Preload(boolean (*update)(word current,word total)) // while (maintotal) { - while ( !PMPages[page].offset || PMPages[page].mainPage != -1 - || PMPages[page].emsPage != -1 ) + while ( !gvar->pm.PMPages[page].offset || gvar->pm.PMPages[page].mainPage != -1 + || gvar->pm.PMPages[page].emsPage != -1 ) page++; if (page >= gvar->pm.fi.ChunksInFile) Quit ("PM_Preload: Pages>=gvar->pm.fi.ChunksInFile"); - PM_GetPage(page); + PM_GetPage(page, gvar); page++; current++; @@ -1105,30 +1128,30 @@ PM_Preload(boolean (*update)(word current,word total)) // if (xmstotal) { - for (oogypage = 0 ; PMPages[oogypage].mainPage == -1 ; oogypage++) + for (oogypage = 0 ; gvar->pm.PMPages[oogypage].mainPage == -1 ; oogypage++) ; - addr = PM_GetPage(oogypage); + addr = PM_GetPage(oogypage, gvar); if (!addr) Quit("PM_Preload: XMS buffer failed"); while (xmstotal) { - while ( !PMPages[page].offset || PMPages[page].xmsPage != -1 ) + while ( !gvar->pm.PMPages[page].offset || gvar->pm.PMPages[page].xmsPage != -1 ) page++; if (page >= gvar->pm.fi.ChunksInFile) Quit ("PM_Preload: Pages>=gvar->pm.fi.ChunksInFile"); - p = &PMPages[page]; + p = &gvar->pm.PMPages[page]; - p->xmsPage = gvar->pm.xmm.XMSPagesUsed++; - if (gvar->pm.xmm.XMSPagesUsed > gvar->pm.xmm.XMSPagesAvail) + p->xmsPage = gvar->pm.XMSPagesUsed++; + if (gvar->pm.XMSPagesUsed > gvar->pm.xmm.XMSPagesAvail) Quit("PM_Preload: Exceeded XMS pages"); if (p->length > PMPageSize) Quit("PM_Preload: Page too long"); - PML_ReadFromFile((byte far *)addr,p->offset,p->length); - PML_CopyToXMS((byte far *)addr,p->xmsPage,p->length); + PML_ReadFromFile((byte far *)addr,p->offset,p->length, gvar); + PML_CopyToXMS((byte far *)addr,p->xmsPage,p->length, gvar); page++; current++; @@ -1136,8 +1159,8 @@ PM_Preload(boolean (*update)(word current,word total)) update(current,total); } - p = &PMPages[oogypage]; - PML_ReadFromFile((byte far *)addr,p->offset,p->length); + p = &gvar->pm.PMPages[oogypage]; + PML_ReadFromFile((byte far *)addr,p->offset,p->length, gvar); } update(total,total); @@ -1159,48 +1182,48 @@ PM_Preload(boolean (*update)(word current,word total)) // // void -PM_NextFrame(void) +PM_NextFrame(global_game_variables_t *gvar) { int i; // Frame count overrun - kill the LRU hit entries & reset frame count - if (++PMFrameCount >= MAXLONG - 4) + if (++gvar->pm.PMFrameCount >= LONG_MAX - 4) { - for (i = 0;i < PMNumBlocks;i++) - PMPages[i].lastHit = 0; - PMFrameCount = 0; + for (i = 0;i < gvar->pm.PMNumBlocks;i++) + gvar->pm.PMPages[i].lastHit = 0; + gvar->pm.PMFrameCount = 0; } -#if 0 - for (i = 0;i < PMSoundStart;i++) +//#if 0 + for (i = 0;i < gvar->pm.fi.PMSoundStart;i++) { - if (PMPages[i].locked) + if (gvar->pm.PMPages[i].locked) { char buf[40]; sprintf(buf,"PM_NextFrame: Page %d is locked",i); Quit(buf); } } -#endif +//#endif - if (PMPanicMode) + if (gvar->pm.PMPanicMode) { // DEBUG - set border color - if ((!PMThrashing) && (!--PMPanicMode)) + if ((!gvar->pm.PMThrashing) && (!--gvar->pm.PMPanicMode)) { // DEBUG - reset border color } } - if (PMThrashing >= PMThrashThreshold) - PMPanicMode = PMUnThrashThreshold; - PMThrashing = false; + if (gvar->pm.PMThrashing >= PMThrashThreshold) + gvar->pm.PMPanicMode = PMUnThrashThreshold; + gvar->pm.PMThrashing = false; } // // PM_Reset() - Sets up caching structures // void -PM_Reset(void) +PM_Reset(global_game_variables_t *gvar) { int i; PageListStruct far *page; @@ -1210,12 +1233,12 @@ PM_Reset(void) gvar->pm.emm.EMSPagesAvail = gvar->pm.emm.EMSAvail * (EMSPageSizeKB / PMPageSizeKB); gvar->pm.emm.EMSPhysicalPage = 0; - MainPagesUsed = gvar->pm.emm.EMSPagesUsed = gvar->pm.xmm.XMSPagesUsed = 0; + gvar->pm.MainPagesUsed = gvar->pm.EMSPagesUsed = gvar->pm.XMSPagesUsed = 0; - PMPanicMode = false; + gvar->pm.PMPanicMode = false; // Initialize page list - for (i = 0,page = PMPages;i < PMNumBlocks;i++,page++) + for (i = 0,page = gvar->pm.PMPages;i < gvar->pm.PMNumBlocks;i++,page++) { page->mainPage = -1; page->emsPage = -1; @@ -1228,18 +1251,36 @@ PM_Reset(void) // PM_Startup() - Start up the Page Mgr // void -PM_Startup(void) +PM_Startup(global_game_variables_t *gvar) { boolean nomain,noems,noxms; int i; - if (PMStarted) + if (gvar->pm.PMStarted) return; + strcat(&(gvar->pm.fi.PageFileName), "VSWAP."); + gvar->pm.fi.PageFile = -1; + gvar->pm.xmm.XMSProtectPage = -1; + nomain = noems = noxms = false; - for (i = 1;i < _argc;i++) + for (i = 1;i < +#ifdef __WATCOMC__ + __argc +#endif +#ifdef __BORLANDC__ + _argc +#endif + ;i++) { - switch (US_CheckParm(_argv[i],ParmStrings)) + switch (US_CheckParm( +#ifdef __WATCOMC__ + __argv[i] +#endif +#ifdef __BORLANDC__ + _argv[i] +#endif + ,ParmStrings)) { case 0: nomain = true; @@ -1253,12 +1294,12 @@ PM_Startup(void) } } - PML_OpenPageFile(); + PML_OpenPageFile(gvar); if (!noems) - PML_StartupEMS(); + PML_StartupEMS(gvar); if (!noxms) - PML_StartupXMS(); + PML_StartupXMS(gvar); if (nomain && !gvar->pm.emm.EMSPresent) { @@ -1266,26 +1307,26 @@ PM_Startup(void) //return; } else - PML_StartupMainMem(); + PML_StartupMainMem(gvar); - PM_Reset(); + PM_Reset(gvar); - PMStarted = true; + gvar->pm.PMStarted = true; } // // PM_Shutdown() - Shut down the Page Mgr // void -PM_Shutdown(void) +PM_Shutdown(global_game_variables_t *gvar) { - PML_ShutdownXMS(); - PML_ShutdownEMS(); + PML_ShutdownXMS(gvar); + PML_ShutdownEMS(gvar); - if (!PMStarted) + if (!gvar->pm.PMStarted) return; - PML_ClosePageFile(); + PML_ClosePageFile(gvar); - PML_ShutdownMainMem(); + PML_ShutdownMainMem(gvar); } diff --git a/src/lib/16_pm.h b/src/lib/16_pm.h index 973b08b4..85135d91 100755 --- a/src/lib/16_pm.h +++ b/src/lib/16_pm.h @@ -1,3 +1,28 @@ +/* Project 16 Source Code~ + * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover + * + * This file is part of Project 16. + * + * Project 16 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 3 of the License, or + * (at your option) any later version. + * + * Project 16 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, see , or + * write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef __16_PM__ +#define __16_PM__ + // // ID_PM.H // Header file for Id Engine's Page Manager @@ -67,22 +92,23 @@ extern PageListStruct far *PMPages;*///moved to src/lib/typdefst.h #define PM_GetSoundPage(v) PM_GetPage(PMSoundStart + (v)) #define PM_GetSpritePage(v) PM_GetPage(PMSpriteStart + (v)) -#define PM_LockMainMem() PM_SetMainMemPurge(0) -#define PM_UnlockMainMem() PM_SetMainMemPurge(3) +#define PM_LockMainMem(gvar) PM_SetMainMemPurge(0, gvar) +#define PM_UnlockMainMem(gvar) PM_SetMainMemPurge(3, gvar) extern char PageFileName[13]; -extern void PM_Startup(void), - PM_Shutdown(void), - PM_Reset(void), - PM_Preload(boolean (*update)(word current,word total)), - PM_NextFrame(void), - PM_SetPageLock(int pagenum,PMLockType lock), +extern void PM_Startup(global_game_variables_t *gvar), + PM_Shutdown(global_game_variables_t *gvar), + PM_Reset(global_game_variables_t *gvar), + PM_Preload(boolean (*update)(word current,word total), global_game_variables_t *gvar), + PM_NextFrame(global_game_variables_t *gvar), + PM_SetPageLock(int pagenum,PMLockType lock, global_game_variables_t *gvar), PM_SetMainPurge(int level), - PM_CheckMainMem(void); -extern memptr PM_GetPageAddress(int pagenum), - PM_GetPage(int pagenum); // Use this one to cache page + PM_CheckMainMem(global_game_variables_t *gvar); +extern memptr PM_GetPageAddress(int pagenum, global_game_variables_t *gvar), + PM_GetPage(int pagenum, global_game_variables_t *gvar); // Use this one to cache page -PM_SetMainMemPurge(int level, global_game_variables_t *gvar); +void PM_SetMainMemPurge(int level, global_game_variables_t *gvar); +#endif diff --git a/src/lib/typdefst.h b/src/lib/typdefst.h index 43e46906..a0203ca7 100755 --- a/src/lib/typdefst.h +++ b/src/lib/typdefst.h @@ -170,8 +170,8 @@ typedef struct void __near *nearheap; #endif //byte EMS_status; - unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle; - unsigned int EMSVer; + unsigned totalEMSpages,freeEMSpages,EMSpageframe,EMSpagesmapped,EMShandle;//TODO: PM use + //unsigned int EMSVer; word numUMBs,UMBbase[MAXUMBS]; //dword numUMBs,UMBbase[MAXUMBS]; //huge mmblocktype huge mmblocks[MAXBLOCKS],huge *mmhead,huge *mmfree,huge *mmrover,huge *mmnew; @@ -247,7 +247,7 @@ typedef struct { boolean EMSPresent; word EMSAvail,EMSPagesAvail,EMSHandle, - EMSPageFrame,EMSPhysicalPage; + EMSPageFrame,EMSPhysicalPage, EMSVer; EMSListStruct EMSList[EMSFrameCount]; } pm_emmi_t; @@ -256,7 +256,7 @@ typedef struct { boolean XMSPresent; word XMSAvail,XMSPagesAvail,XMSHandle; - word XMSDriver; //TODO: changed to word + dword XMSDriver; //TODO: changed to word int XMSProtectPage;// = -1; } pm_xmmi_t; diff --git a/src/vrstest.c b/src/vrstest.c index 1c8a15aa..8ed43620 100755 --- a/src/vrstest.c +++ b/src/vrstest.c @@ -72,7 +72,7 @@ void main() { //gvar.video.page[0]=modexDefaultPage(&gvar.video.page[0]); gvar.mm.mmstarted=0; - MM_Startup(&gvar.mm, &gvar.mmi); + MM_Startup(&gvar); CA_Startup(&gvar); // What should be done by read_vrs: //sega = (mm.bufferseg); @@ -153,12 +153,12 @@ void main() { } VGAmodeX(0, 1, &gvar); MM_ShowMemory(&gvar); - MM_DumpData(&gvar.mm); + MM_DumpData(&gvar); free(spri.sprite_vrl_cont); - MM_FreePtr(&bigbuffer, &gvar.mm); + MM_FreePtr(&bigbuffer, &gvar); //MM_FreePtr(&((void __based(sega)*)spri.spritesheet->buffer), &mm); CA_Shutdown(&gvar); - MM_Shutdown(&gvar.mm); + MM_Shutdown(&gvar); //printf("CPU to VGA: %f\n", t1); //printf("VGA to VGA: %f\n", t2); heapdump(&gvar);