⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ems.c

📁 EMS内存进行存取的函数库
💻 C
字号:
/**********************************************************************
 *               这是一个对EMS内存进行存取的函数库                    *
 **********************************************************************/

#include<dos.h>
#include<alloc.h>

#define PAGE0 0
#define PAGE1 0x4000
#define PAGE2 0x8000
#define PAGE3 0xc000

int                     EMS_ERR;
unsigned char           emm_name[]="EMMXXXX0";
unsigned int            emm_handle;
unsigned char           emm_alloc=0;

union REGS in,out;

err_table[]={03,04,15,15,15,6,10,7,7,8,11,15,12,13,14};

/**********************************************************************
 * 这个函数返回当前EMS内存的空余16K页面数,如果函数返回0值,则在EMS_ERR *
 * 变量中包含有下列出错代码:                                          *
 *                                                                    *
 *      01 Expanded memory not installed.                             *
 *      02 No free expanded memory pages available.                   *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 **********************************************************************/

int ememavail(void)
{
unsigned char far *emm;
	emm=(unsigned char far *)(0x67*4+2);
	emm=MK_FP(*((unsigned int *)emm),0xA);
	if(memcmp(emm_name,emm,8)){
		EMS_ERR=1;return(0);
		}
	in.h.ah=0x42;
	int86(0x67,&in,&out);
	if(out.h.ah){
		err_rtn(out.h.ah);return(0);
		}
	if(out.x.dx){
		EMS_ERR=0;return(out.x.dx);
		}
	else{
		EMS_ERR=2;return(0);
		}
}

err_rtn(unsigned char ah)
{
if(!ah){EMS_ERR=0;return;}
EMS_ERR=err_table[ah&0x80];
}

/**********************************************************************
 * 本函数申请所需EMS内存的16K页面数,并返回一个指向内存影像的起始地址  *
 * 的远指针,如果指针值为空,则EMS_ERR内存变量中包含有下列出错代码      *
 *                                                                    *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 *      05 EMS memory already allocated by this process.              *
 *      06 NO EMM handles free.                                       *
 *      07 Insufficient EMS pages to fill request.                    *
 *      08 Attempt to allocated 0 pages.                              *
 **********************************************************************/

char far *ealloc(unsigned int pages)
{
	if(emm_alloc){EMS_ERR=5;return(NULL);};
	in.h.ah=0x43;
	in.x.bx=pages;
	int86(0x67,&in,&out);
	if(out.h.ah){err_rtn(out.h.ah);return(NULL);}
	emm_alloc=1;
	emm_handle=out.x.dx;
	in.h.ah=0x41;
	int86(0x67,&in,&out);
	if(out.h.ah){err_rtn(out.h.ah);return(NULL);}
	EMS_ERR=0;
	return(MK_FP(out.x.bx,0x0));
}

/**********************************************************************
 * 这个函数释放所有的用本函数库申请的EMS内存页,如果成功,返回0值,如果  *
 * 失败返回-1,并且在EMS_ERR变量中包含有下列错误代码                   *
								      *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 *      09 EMS memory not alocated.                                   *
 *      10 'esave' called without subsequent 'erestore'.              *
 **********************************************************************/

int efree(void)
{
if(!emm_alloc){EMS_ERR=9;return(-1);}
in.h.ah=0x45;
in.x.dx=emm_handle;
int86(0x67,&in,&out);
if(out.h.ah){err_rtn(out.h.ah);return(-1);}
emm_alloc=0;
EMS_ERR=0;
return(0);
}

/**********************************************************************
 * 这个函数映射逻辑EMS内存页到四个物理页窗口中,如果映射成功则返回0值, *
 * 否则返回-1,并且EMS_ERR包含有下列出错代码                           *
 *                                                                    *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 *      09 EMS memory not alocated.                                   *
 *      11 Logical page out of range allocated to process.            *
 *                                                                    *
 * Notes: p0 through p3 contains the logical page numbers to be mapped*
 * onto physical pages 0 though 3. If any parameter=-1,then NO        *
 * mapping is done to that physical page.                             *
 **********************************************************************/

int emap(unsigned int p0,unsigned int p1,unsigned int p2,unsigned int p3)
{
int i;
if(!emm_alloc){EMS_ERR=9;return(-1);}
if(emap_(p0,0x0))return(-1);
if(emap_(p1,0x1))return(-1);
if(emap_(p2,0x2))return(-1);
if(emap_(p3,0x3))return(-1);
else return(0);
}

int emap_(unsigned int p,unsigned char i)
{
	if(p==65535)return(0);
	in.h.ah=0x44;
	in.h.al=i;
	in.x.bx=p;
	in.x.dx=emm_handle;
	int86(0x67,&in,&out);
	if(out.h.ah){err_rtn(out.h.ah);return(-1);}
	else return(0);
}

/**********************************************************************
 * 这个函数保存当前的EMS状态,如果返回0,则调用成功,如果有错误发生,则返 *
 * 回-1,并且将EMS_ERR变量设置成下面的取值之一                         *
 *                                                                    *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 *      09 EMS memory not alocated.                                   *
 *      12 NO room in save area.                                      *
 *      13 'Save' already called by this process.                     *
 **********************************************************************/

int esave(void)
{
if(!emm_alloc){EMS_ERR=9;return(-1);}
in.h.ah=0x47;
in.x.dx=emm_handle;
int86(0x67,&in,&out);
if(out.h.ah){err_rtn(out.h.ah);return(-1);}
EMS_ERR=0;
return(0);
}

/**********************************************************************
 * 这个函数将EMS内存恢复到用本函数库中的esave函数保存的EMS内存状态,这 *
 * 个功能主要用于中断程序设计,在中断程序中使用EMS内存后,可将EMS内存状 *
 * 态恢复到使用前的状态                                               *
 * 函数调用成功则返回0值,如果有错误发生则返回-1,并且将EMS_ERR内存变量 *
 * 设置成下面的取值                                                   *
 *                                                                    *
 *      03 software mal function.                                     *
 *      04 hardware mal function.                                     *
 *      09 EMS memory not alocated.                                   *
 *      14 'erestore' called without prior 'esave'.                   *
 **********************************************************************/

int erestore(void)
{
if(!emm_alloc){EMS_ERR=9;return(-1);}
in.h.ah=0x48;
in.x.dx=emm_handle;
int86(0x67,&in,&out);
if(out.h.ah){err_rtn(out.h.ah);return(-1);}
EMS_ERR=0;
return(0);
}

/**********************************************************************
 *               这个函数返回相应出错号的错误信息串                   *
 **********************************************************************/

char *err_desc(int code)
{
static char *err_list[]=
	{
	"No error."
	"Expanded memory not installed."
	"No free expanded memory pages."
	"Software mal function."
	"Hardware mal function."
	"EMS memory already allocated by this process"
	"No EMM handles free."
	"Insufficient EMS pages to fill request."
	"Attempt to allocated 0 pages."
	"EMS memory not available."
	"'esave' called without subsequent 'erestore'."
	"Logical page out of range allocated to process."
	"No room in save area."
	"'save' already called by process."
	"'erestore' called without prior 'esave'."
	"Unidentified error."
	};
return(err_list[code]);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -