*/
#include "src/lib/16_mm.h"
+#include "src/lib/16_ca.h"
#pragma hdrstop
#pragma warn -pro
{
boolean emmcfems;
static char emmname[] = "EMMXXXX0"; //fix by andrius4669
-// mov dx,OFFSET emmname
__asm {
mov dx,OFFSET emmname //fix by andrius4669
mov ax,0x3d00
{
word base,size;
-getmemory:
+
__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
//mov ax,dx // Set available Kbytes.
======================
*/
-void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
+/*void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
{
- mmblocktype huge *scan,huge *last;
+ //huge mmblocktype huge *scan,huge *last;
word segm=1;
word oldend;
- dword segmlen=seglength;
+ dword segmlen;
dword extra;
scan = last = mm->mmhead;
scan = scan->next;
}
- //find out how many blocks it spans!
- //
- for(;seglength>0x10000;seglength-=0xffff)
- {
- printf(" seglen=%lu\n", seglength);
- segm++;
- }
-
//
// take the given range out of the block
//
oldend = scan->start + scan->length;
- extra = oldend - (segstart+(word)seglength);
+ extra = oldend - (segstart+seglength);
+
+ segmlen=extra;
+
//++++emsver stuff!
- if(segm>1 || extra>0x10000lu)
+ if(segm>1)/// || extra>=0x10000lu)
//if(extra>0xfffflu)
{
scan->blob=segm;
- /*__asm
- {
- push ds
- mov ax,ds
- inc ax
- mov ds,ax
- }*/
-
//MML_UseSpace(segstart, seglength, mm);
- /*__asm
- {
- pop ds
- }*/
printf("MML_UseSpace: Segment spans two blocks!\n");
//}
- printf("========================================\n");
printf("segm=%u ", segm);
printf("ex=%lu ", extra);
printf("old=%u ", oldend);
printf("len=%lu ", scan->length);
printf("seglen=%lu ", seglength);
printf("segmlen=%lu\n", segmlen);
- printf("========================================\n");
}
//++++todo: linked list of segment!
if(segstart == scan->start)
if(extra > 0)
{
GETNEWBLOCK;
+ mm->mmnew->useptr = NULL;
+
mm->mmnew->next = scan->next;
scan->next = mm->mmnew;
mm->mmnew->start = segstart+seglength;
mm->mmnew->attributes = LOCKBIT;
}//else if(segm>0) goto segu;
+}*/
+void MML_UseSpace(word segstart, dword seglength, mminfo_t *mm)
+{
+ 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
+
+//
+// search for the block that contains the range of segments
+//
+ while (scan->start+scan->length < segstart)
+ {
+ last = scan;
+ scan = scan->next;
+ }
+
+//
+// find out how many blocks it spans!
+//
+ /*for(;seglength>=0x10000;seglength-=0xFFFF)
+ {
+ //printf(" seglen=%lu\n", segmlen);
+ segm++;
+ }*/
+
+//
+// take the given range out of the block
+//
+ oldend = scan->start + scan->length;
+ extra = oldend - (segstart+((unsigned)seglength));
+ if (extra < 0)
+ {
+ printf("========================================\n");
+ printf("start=%x ", scan->start);
+ printf("old=%u ", oldend);
+ printf("start+seglen=%lu\n", segstart+seglength);
+ printf("segsta=%x ", segstart);
+ printf("len=%lu ", scan->length);
+ printf("seglen=%lu ", seglength);
+ printf("\n");
+ printf("MML_UseSpace: Segment spans two blocks! %d\n", extra);
+ printf("========================================\n");
+ //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;
+ 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;
+ }
+
}
//==========================================================================
void MML_ClearBlock(mminfo_t *mm)
{
- mmblocktype huge *scan,huge *last;
+ //huge mmblocktype huge *scan,huge *last;
+ mmblocktype far *scan,far *last;
scan = mm->mmhead->next;
void MM_Startup(mminfo_t *mm, mminfotype *mmi)
{
int i;
- dword length,seglength;
- //dword length; word seglength;
- void huge *start;
+ //dword length,seglength;
+ dword length; word seglength;
+ //huge void huge *start;
+ void far *start;
word segstart;//,endfree;
+ //memptr *peeonself;
// if(mm->mmstarted)
// MM_Shutdown(mm);
mm->mmstarted = true;
mm->bombonerror = true;
- mm->endid=0;
//
// set up the linked list (everything in the free list;
mm->mmnew->length = 0xffff;
mm->mmnew->attributes = LOCKBIT;
mm->mmnew->next = NULL;
+ //mm->mmnew->useptr = peeonself;
mm->mmrover = mm->mmhead;
+ //printf(" %x\n", peeonself);
+ //printf(" %x\n", *peeonself);
//
// get all available near conventional memory segments
//
#ifdef __WATCOMC__
_nheapgrow();
length=(dword)_memavl();//(dword)GetFreeSize();
- start = (void huge *)(mm->nearheap = _nmalloc(length));
+ //huge start = (void huge *)(mm->nearheap = _nmalloc(length));
+ start = (void far *)(mm->nearheap = _nmalloc(length));
#endif
#ifdef __BORLANDC__
length=coreleft();
- start = (void huge *)(mm->nearheap = malloc(length));
+ //huge start = (void huge *)(mm->nearheap = malloc(length));
+ start = (void far *)(mm->nearheap = malloc(length));
#endif
length -= 16-(FP_OFF(start)&15);
length -= SAVENEARHEAP;
// detect EMS and allocate up to 64K at page frame
//
mmi->EMSmem = 0;
+//goto emsskip; //0000
for(i = 1;i <
#ifdef __WATCOMC__
__argc
====================
*/
-void MM_GetPtr(memptr *baseptr,dword size, mminfo_t *mm, mminfotype *mmi)
+void MM_GetPtr (memptr *baseptr, dword size, mminfo_t *mm, mminfotype *mmi)
{
- mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
+ //huge mmblocktype huge *scan,huge *lastscan,huge *endscan,huge *purge,huge *next;
+ mmblocktype far *scan,far *lastscan,far *endscan,far *purge,far *next;
int search;
- dword needed;
- word startseg;
+ unsigned needed,startseg;
needed = (size+15)/16; // convert size from bytes to paragraphs
-printf("."); //0000
+
GETNEWBLOCK; // fill in start and next after a spot is found
mm->mmnew->length = needed;
mm->mmnew->useptr = baseptr;
+ //if(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));
+#endif
+ //exit(-5); }
mm->mmnew->attributes = BASEATTRIBUTES;
-printf("."); //0000
- for(search = 0; search<mm->endid; search++)
+
+//tryagain:
+ for (search = 0; search<3; search++)
{
-printf(" [case]"); //0000
//
// first search: try to allocate right after the rover, then on up
// second search: search from the head pointer up to the rover
// third search: compress memory, then scan from start
- if(search == 1 && mm->mmrover == mm->mmhead)
+ if (search == 1 && mm->mmrover == mm->mmhead)
search++;
- switch(search)
+ switch (search)
{
case 0:
-printf("0 "); //0000
lastscan = mm->mmrover;
scan = mm->mmrover->next;
endscan = NULL;
break;
case 1:
-printf("1 "); //0000
lastscan = mm->mmhead;
scan = mm->mmhead->next;
endscan = mm->mmrover;
break;
case 2:
-printf("2 "); //0000
- MM_SortMem(mm);
+ MM_SortMem (mm);
lastscan = mm->mmhead;
scan = mm->mmhead->next;
endscan = NULL;
break;
}
- startseg = lastscan->start + (word)lastscan->length;
+ startseg = lastscan->start + lastscan->length;
- while(scan != endscan)
+ while (scan != endscan)
{
-//printf(","); //0000
- if(scan->start - startseg >= needed)
+ if (scan->start - startseg >= needed)
{
-printf("."); //0000
//
// got enough space between the end of lastscan and
// the start of scan, so throw out anything in the middle
//
purge = lastscan->next;
lastscan->next = mm->mmnew;
- mm->mmnew->start = *(word *)baseptr = startseg;
+ mm->mmnew->start = *(unsigned *)baseptr = startseg;
mm->mmnew->next = scan;
- while(purge != scan)
+ while ( purge != scan)
{ // free the purgable block
-printf(" freeing block~\n"); //0000
next = purge->next;
FREEBLOCK(purge);
- //MM_FreeBlock(purge, mm);
purge = next; // purge another if not at scan
}
mm->mmrover = mm->mmnew;
//
// if this block is purge level zero or locked, skip past it
//
- if((scan->attributes & LOCKBIT)
+ if ( (scan->attributes & LOCKBIT)
|| !(scan->attributes & PURGEBITS) )
{
-printf(" [lock] "); //0000
-printf("len=%lu ", scan->length);
lastscan = scan;
- startseg = lastscan->start + (word)lastscan->length;
+ startseg = lastscan->start + lastscan->length;
}
-printf("\n");
+
scan=scan->next; // look at next line
}
}
#endif
printf(OUT_OF_MEM_MSG,(size-mmi->nearheap));
printf("for stability reasons the program will shut down! wwww\n");
- printf(" endid=%u\n",(mm->endid));
MM_Shutdown(mm);
exit(-1);
}
void MM_FreePtr(memptr *baseptr, mminfo_t *mm)
{
- mmblocktype huge *scan,huge *last;
+ //huge mmblocktype huge *scan,huge *last;
+ mmblocktype far *scan,far *last;
last = mm->mmhead;
scan = last->next;
void MM_SetPurge(memptr *baseptr, int purge, mminfo_t *mm)
{
- mmblocktype huge *start;
+ //huge mmblocktype huge *start;
+ mmblocktype far *start;
start = mm->mmrover;
void MM_SetLock(memptr *baseptr, boolean locked, mminfo_t *mm)
{
- mmblocktype huge *start;
+ //huge mmblocktype huge *start;
+ mmblocktype far *start;
start = mm->mmrover;
void MM_SortMem(mminfo_t *mm)
{
- mmblocktype huge *scan,huge *last,huge *next;
+ //huge mmblocktype huge *scan,huge *last,huge *next;
+ mmblocktype far *scan,far *last,far *next;
unsigned start,length,source,dest,oldborder;
int playing;
void MM_ShowMemory(global_game_variables_t *gvar,/*page_t *page, */mminfo_t *mm)
{
- mmblocktype huge *scan;
- //byte color;
+ //huge mmblocktype huge *scan;
+ mmblocktype far *scan;
word temp;
sdword end,owner;
//word chx,chy;
word w;
- byte scratch[160],scratch0[4096],str[16];
+ //dword wwww;
+ byte scratch[160],scratch0[4096],scratch1[160],str[16];
//byte d = '#';
//**** VW_SetDefaultColors();
//**** VW_SetLineWidth(40);
//modexprint(&page, chx, chy, 1, 0, 24, "\nMM_ShowMemory: Memory block order currupted!\n");
break;
}
- //for(;scan->length>0xfffflu;scan->length-=0xfffflu);
end = scan->start+(scan->length)-1;
//++++ chy = scan->start/320;
//++++ chx = scan->start%320;
for(w=(scan->start)/80;w<=end/80;w++)
{
//printf("+ %u %lu\n", w, scan->length);
- //if(w==48) getch();
strcat(scratch0, "+");
}
- strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
+ //++==++==optional strcat(scratch0, AARESET); strcat(scratch0, AAGREY); strcat(scratch0,"_");
//++++ VW_Plot(scan->start,0,15);
//++++ modexClearRegion(page, chx, chy, 4, 4, 15);
//++++ VW_Hlin(end+1,scan->next->start,0,0); // black = free
- if((scan->next->start != 0xe000) > end)
+
+ //wwww=(dword)(scan->next->start)-(dword)scan->start;
+ //wwww=(dword)scan->start+(dword)(scan->next->start);
+ if (scan->next && scan->next->start >= end+1)
{
strcat(scratch0, AARESET);
+ //++==++==optional strcat(scratch0, "\n");
strcat(scratch0,AAGREEN);
- for(w=0;w<=((scan->length+1)/80);w++)
+ for(w=(end+1)/80;w<=((scan->next->start-scan->start)/80);w++)
+ //for(w=(wwww)/80;w<=((end+1)/80);w++)
+ //for(w=(end+1)/80;w<=((wwww)/80);w++)
{
//printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
strcat(scratch0,"0");
}
- }else{
+ //printf("==================\n");
+ //printf("w=%u wwww=%lu start=%04x next=%04x end=%lu\n", w/80, wwww/80, scan->start, (scan->next->start), end+1);
+ //printf("==================\n");
+ strcat(scratch0, "\n");
+ //getch();
+ }/*else {//if(scan->next->start <= scan->start){
+ scan->next->start=scan->start+0x1000;
+ wwww=(dword)(scan->next->start)-(dword)scan->start;
strcat(scratch0, AARESET);
+ strcat(scratch0, "\n");
strcat(scratch0,AAGREEN);
- for(w=0;w<=((scan->length+1)/80);w++)
+ for(w=(end+1);w<=(0x1000/80);w++)
{
- //printf("0 %x %u %lu\n", scan->next->start, w, scan->length);
+ //printf("0 %x %x %u\n", scan->start, w);
strcat(scratch0,"0");
}
- printf("================\nstart=%x length==%lu\n next=%x end=%x\nscan->start+(scan->length)==%x\n================\n", scan->start, scan->length, scan->next->start, end, scan->start+(scan->length));
- printf("w=%u (scan->length+1)/80)=%lu\n\n", w, (scan->length+1)/80);
- //getch();
- }
+ printf("================\n");
+ printf("w=%x start=%x next=%x end=%u %lu\n", w, scan->start, (scan->next->start), end+1, wwww);
+ printf("================\n");
+ getch();
+ }*/
strcat(scratch0, AARESET);
- strcat(scratch0,"\n");
+ //strcat(scratch0,"\n");
//for(chx=scan->next->start;chx+4>=(word)end+1;chx+=4)
//{
// chx+=scan->next->start;
ultoa (scan->start,str,16);
strcat (scratch,str);
strcat (scratch,"\tSize:");
- ultoa ((word)scan->length,str,10);
+ ultoa ((unsigned)scan->length,str,10);
strcat (scratch,str);
strcat (scratch,"\tOwner:0x");
owner = (unsigned)scan->useptr;
scan = scan->next;
}
+ /*strcpy(scratch1, AARESET);
+ strcat(scratch1, "========================================\n");
+ strcat(scratch1, "near= ");
+ ultoa (*(mm->nearheap),str,10);
+ strcat (scratch1,str);
+ strcat(scratch1, " far= ");
+ ultoa (*(mm->farheap),str,10);
+ strcat (scratch1,str);
+ strcat(scratch1, "\n");
+ //strcat(scratch1, "&near= %Fp ", &(mm->nearheap));
+ //strcat(scratch1, "&far= %Fp", &(mm->farheap));
+ //strcat(scratch1, "\n");
+ strcat(scratch1, "========================================\n");
+ write(gvar->handle.debughandle,scratch1,strlen(scratch1));*/
+
CA_CloseDebug (gvar);
void MM_DumpData(mminfo_t *mm)
{
- mmblocktype huge *scan,huge *best;
+ //huge mmblocktype huge *scan,huge *best;
+ mmblocktype far *scan,far *best;
long lowest,oldlowest;
word owner;
byte lock,purge;
FILE *dumpfile;
free(mm->nearheap);
- dumpfile = fopen ("mmdump.16","w");
+#ifdef __BORLANDC__
+ dumpfile = fopen ("mmdump.16b","w");
+#endif
+#ifdef __WATCOMC__
+ dumpfile = fopen ("mmdump.16w","w");
+#endif
if (!dumpfile){
printf("MM_DumpData: Couldn't open MMDUMP.16!\n");
return;
scan = mm->mmhead;
while (scan)
{
- owner = (word)scan->useptr;
+ owner = (unsigned)scan->useptr;
if (owner && owner<lowest && owner > oldlowest)
{
else
lock = '-';
fprintf (dumpfile,"0x%p (%c%c) = %u\n"
- ,(word)lowest,lock,purge,best->length);
+ ,(unsigned)lowest,lock,purge,best->length);
}
} while (lowest != 0xffff);
dword MM_UnusedMemory(mminfo_t *mm)
{
dword free;
- mmblocktype huge *scan;
+ //huge mmblocktype huge *scan;
+ mmblocktype far *scan;
free = 0;
scan = mm->mmhead;
dword MM_TotalFree(mminfo_t *mm)
{
dword free;
- mmblocktype huge *scan;
+ //huge mmblocktype huge *scan;
+ mmblocktype far *scan;
free = 0;
scan = mm->mmhead;
=====================
*/
-void MM_Report(/*page_t *page, */mminfo_t *mm, mminfotype *mmi)
+void MM_Report(global_game_variables_t *gvar)
{
+ printf("========================================\n");
+ printf(" MM_Report\n");
+ printf("========================================\n");
if(MML_CheckForEMS())
{
- printf("EMM v%x.%x available\n", mm->EMSVer>>4,mm->EMSVer&0x0F);
- printf("totalEMSpages=%u\n", mm->totalEMSpages);
- printf("freeEMSpages=%u\n", mm->freeEMSpages);
- printf("EMSpageframe=%x\n", mm->EMSpageframe);
+ printf(" LIMEMS\n");
+ printf(" EMM v%x.%x available\n", gvar->mm.EMSVer>>4,gvar->mm.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)))
+ {
+ printf(" XMS\n");
+ printf(" XMSaddr: %X\n", *XMSaddr);
}
- if(MML_CheckForXMS(mm)) printf("XMSaddr=%X\n", *XMSaddr);
- printf("near=%lu\n", mmi->nearheap);
- printf("far=%lu\n", mmi->farheap);
- printf("EMSmem=%lu\n", mmi->EMSmem);
- printf("XMSmem=%lu\n", mmi->XMSmem);
- printf("mainmem=%lu\n", mmi->mainmem);
- printf("UnusedMemory=%lu\n", MM_UnusedMemory(mm));
- printf("TotalFree=%lu\n", MM_TotalFree(mm));
- printf("TotalUsed=%lu\n", mmi->mainmem+mmi->EMSmem+mmi->XMSmem+mmi->XMSmem);
+ 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("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)));
}
//==========================================================================
return;
}
mm->mmfree=mm->mmfree->next;
- mm->endid++; //end of list
}
void MM_FreeBlock(mmblocktype *x, mminfo_t *mm)
x->useptr=NULL;
x->next=mm->mmfree;
mm->mmfree=x;
- mm->endid--; //end of list
}*/
-void MM_seguin(void)
+/*void MM_seguin(void)
{
__asm {
push ds
__asm {
pop ds
}
-}
+}*/
/*
pull data from far and put it into ds var