From: sparky4 Date: Thu, 12 Feb 2015 21:40:33 +0000 (-0600) Subject: new file: EMSTEST.EXE X-Git-Url: http://4ch.mooo.com/gitweb/?a=commitdiff_plain;h=dc4fcdb6ec7b3fd591a88ddd97ede2b5f66e96d1;p=16.git new file: EMSTEST.EXE new file: src/emstest.c new file: src/lib/cguide3.zip modified: src/lib/ems.c new file: src/lib/ems.h deleted: src/lib/ems1.c --- diff --git a/EMSTEST.EXE b/EMSTEST.EXE new file mode 100644 index 00000000..554dd96e Binary files /dev/null and b/EMSTEST.EXE differ diff --git a/src/emstest.c b/src/emstest.c new file mode 100644 index 00000000..f9c6500d --- /dev/null +++ b/src/emstest.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include "src\lib\ems.c" + /* + Test function for the EMM routines + */ + + void main() + { + long emmhandle; + long avail; + char teststr[80]; + int i; + + if(!emmtest()) + { + printf("Expanded memory is not present\n"); + exit(0); + } + + if(!emmok()) + { + printf("Expanded memory manager is not present\n"); + exit(0); + } + + avail = emmavail(); + if (avail == -1) + { + printf("Expanded memory manager error\n"); + exit(0); + } + printf("There are %ld pages available\n",avail); + + /* Request 10 pages of expanded memory */ + if((emmhandle = emmalloc(avail)) < 0) + { + printf("Insufficient pages available\n"); + exit(0); + } + + for (i = 0; i < avail; i++) + { + sprintf(teststr,"%02d This is a test string\n",i); + emmmap(emmhandle,i,0); + emmmove(0,teststr,strlen(teststr) + 1); + } + + for (i = 0; i < avail; i++) + { + emmmap(emmhandle,i,0); + emmget(0,teststr,strlen(teststr) + 1); + printf("READING BLOCK %d: %s\n",i,teststr); + } + + emmclose(emmhandle); + } diff --git a/src/lib/cguide3.zip b/src/lib/cguide3.zip new file mode 100644 index 00000000..093439c2 Binary files /dev/null and b/src/lib/cguide3.zip differ diff --git a/src/lib/ems.c b/src/lib/ems.c index 9883f60c..9d371053 100644 --- a/src/lib/ems.c +++ b/src/lib/ems.c @@ -1,289 +1,181 @@ -//------- PRACE S EMS pameti --------------- -#include -#include -#include -#include -#include - -typedef struct - { - unsigned long length; /* velikost prenasene pameti */ - unsigned int sourceH; /* handle pro zdroj (0=konvencni */ - unsigned long sourceOff; /* offset zdroje pameti */ - unsigned int destH; /* handle pro terc (0=konvencni */ - unsigned long destOff; /* offset terce pameti */ - } XMOVE; - -int get_emem(void); // Fce pro zachazeni s EMS -int alloc_emem(int n); // Alokuje n KB pameti, vraci handle -int dealloc_emem(int h); // Dealokuje EMS (handle) -int move_emem(XMOVE *p); // presune blok z/do EMS -int mem_emem(unsigned *total, unsigned *free); - -#define pagesizeEMS 0x4000 // pamet EMS je ze 16k stranek - -//int pagesAllocated = 0; -//int totalPages; -char *EmsFrame; - - -//------ Zda je EMS driver dostupny: ret= 1 - ANO, 0 - NE -int isEMS(void) -{ - int fh; - union REGS rg; - - if((fh=open("EMMXXXX0",O_RDONLY,&fh)) == -1) return( 0 ); - - rg.h.ah = 0x44; - rg.h.al = 0x00; - rg.x.bx = fh; - int86(0x21,&rg,&rg); - close(fh); - if(rg.x.cflag) return(0); - if(rg.x.dx & 0x80) - return( 1 ); - else - return( 0 ); -} - -//----- Zda je EMS HW dostupny ret= 1 - ANO, 0 - NE -int checkEMS(void) -{ - union REGS rg; - - rg.h.ah = 0x40; - int86(0x67,&rg,&rg); - if(rg.h.ah == 0) - return( 1 ); - else - return( 0 ); -} - -//----- Vraci totalni pocet stranek EMS nebo -1 ---- -int coretotalEMS(void) -{ - union REGS rg; - - rg.h.ah = 0x42; - int86(0x67,&rg,&rg); - if(rg.x.cflag) return( -1 ); - //if(!pagesAllocated) - // { pagesAllocated = 1; - // totalPages = rg.x.dx; - // } - return(rg.x.bx); -} - -//----- Vraci pocet volnych stranek EMS nebo -1 ---- -int coreleftEMS(void) -{ - union REGS rg; - - //if(pagesAllocated) return(totalPages); - rg.h.ah = 0x42; - int86(0x67,&rg,&rg); - if(rg.x.cflag) return( -1 ); - //if(!pagesAllocated) - //pagesAllocated = 1; - //totalPages = rg.x.dx; - //return(totalPages); - return(rg.x.dx); -} - -//----- Vraci EMS page frame (pointr na EMS) nebo NULL ---- -char *pageframeEMS(void) -{ - union REGS rg; - - rg.h.ah = 0x41; - int86(0x67,&rg,&rg); - if(rg.h.ah != 0) - return( NULL ); - else - return((char *)MK_FP(rg.x.bx,0)); -} - -//----- Alokuje n stranek - vraci handle na blok stranek nebo 0 ---- -unsigned mallocEMS(int n) -{ - union REGS rg; - - if(n > coreleftEMS() ) return( 0 ); - rg.h.ah = 0x43; - rg.x.bx = n; - int86(0x67,&rg,&rg); - if(rg.h.ah) - return( 0 ); - else - return(rg.x.dx); -} - -//----- Dealokuje blok stranek ret = 1-O.K. 0-ERR ----- -unsigned freeEMS(unsigned h) -{ - union REGS rg; - int i; - - for(i=0; i<5; i++) - { rg.h.ah = 0x45; - rg.x.dx = h; - int86(0x67,&rg,&rg); - if(rg.h.ah == 0) break; +/* + + THE IBM PC PROGRAMMER'S GUIDE TO C + + + + 3rd Edition + + + + Matthew Probert + + + + COPYRIGHT NOTICE + + +This publication remains the property of Matthew Probert. License is +hereby given for this work to be freely distibuted in whole under the +proviso that credit is given to the author. Sections of this work may be +used and distributed without payment under the proviso that credit is +given to both this work and the author. Source code occuring in this work +may be used within commercial and non-commercial applications without +charge and without reference to the author. +*/ + /* + Various functions for using Expanded memory + */ +#include "src\lib\ems.h" + emmtest() + { + /* + Tests for the presence of expnaded memory by attempting to + open the file EMMXXXX0. + */ + + union REGS regs; + struct SREGS sregs; + int error; + long handle; + + /* Attempt to open the file device EMMXXXX0 */ + regs.x.ax = 0x3d00; + regs.x.dx = (int)"EMMXXXX0"; + sregs.ds = 0; //???? + intdosx(®s,®s,&sregs); + handle = regs.x.ax; + error = regs.x.cflag; + + if (!error) + { + regs.h.ah = 0x3e; + regs.x.bx = handle; + intdos(®s,®s); + } + return error; } - if(rg.h.ah == 0) - return( 1 ); - else - return( 0 ); -} - -//----- Mapuje logiclou stranku do fyzicke stranky -int mapEMS(unsigned h, int Ppage, int Lpage) -{ - union REGS rg; - - if(Ppage < 0 || Ppage > 3) return( 0 ); - rg.h.ah = 0x44; - rg.h.al = Ppage; - rg.x.bx = Lpage; - rg.x.dx = h; - int86(0x67,&rg,&rg); - if(rg.h.ah != 0) - return( 0 ); - else - return( 1 ); -} - -// ##### Fce se stejnymi parametry pro EMS jako pro XMS - -//----- Zda je EMS dostupna -int get_emem(void) -{ - int ist; - - ist = checkEMS(); - if(ist == 1) - { ist = isEMS(); - if(ist == 1) return( 0x0300 ); - } - return( -1 ); -} - -//----- Allokuje Kb pameti ------- -int alloc_emem(int kb) -{ - int Pages,hhh; - - Pages = kb / 16; - if((Pages * 16) < kb) Pages++; - - hhh = mallocEMS(Pages); - if(hhh == 0) - return( -1); - else - return(hhh); -} - -//----- dealokuje EMS pres handle -int dealloc_emem(int h) -{ - return( freeEMS( h ) ); -} - -//----- presune blok pameti -// unsigned long length; /* velikost prenasene pameti */ -// unsigned int sourceH; /* handle pro zdroj (0=konvencni */ -// unsigned long sourceOff; /* offset zdroje pameti */ -// unsigned int destH; /* handle pro terc (0=konvencni */ -// unsigned long destOff; /* offset terce pameti */ -int move_emem(XMOVE *pxm) -{ - unsigned char *SrcBuf,*DstBuf; - int ist; - unsigned int SrcOff, DstOff, BegPage, BegOff, FreeByte, CopyLen; - - if(pxm->sourceH == 0 && pxm->destH != 0) // Buffer->EMS - { - SrcBuf = (unsigned char *)pxm->sourceOff;// buffer - SrcOff = 0; - BegPage = pxm->destOff / pagesizeEMS; // pocatecni page - BegOff = pxm->destOff % pagesizeEMS; // offset in page - FreeByte= pagesizeEMS - BegOff; // volnych B na page - CopyLen = pxm->length; // celkova delka - - Next_page: - if(CopyLen > FreeByte) + + emmok() { - ist = mapEMS(pxm->destH, 0, BegPage); - if(ist==0) return( 0 ); - memcpy(EmsFrame+BegOff, SrcBuf+SrcOff, FreeByte); - - CopyLen = CopyLen - FreeByte; - SrcOff += FreeByte; - BegPage++; - BegOff = 0; - FreeByte = pagesizeEMS; - goto Next_page; + /* + Checks whether the expanded memory manager responds correctly + */ + + union REGS regs; + + regs.h.ah = 0x40; + int86(EMM,®s,®s); + + if (regs.h.ah) + return 0; + + regs.h.ah = 0x41; + int86(EMM,®s,®s); + + if (regs.h.ah) + return 0; + + emmbase = MK_FP(regs.x.bx,0); + return 1; } - else + + long emmavail() { - ist = mapEMS(pxm->destH, 0, BegPage); - if(ist==0) return( 0 ); - memcpy(EmsFrame+BegOff, SrcBuf+SrcOff, CopyLen); + /* + Returns the number of available (free) 16K pages of expanded + memory + or -1 if an error occurs. + */ + + union REGS regs; + + regs.h.ah = 0x42; + int86(EMM,®s,®s); + if (!regs.h.ah) + return regs.x.bx; + return -1; } - } - else if(pxm->sourceH != 0 && pxm->destH == 0) // EMS->Buffer - { - DstBuf = (unsigned char *)pxm->destOff;// buffer - DstOff = 0; - BegPage = pxm->sourceOff / pagesizeEMS; // pocatecni page - BegOff = pxm->sourceOff % pagesizeEMS; // offset in page - FreeByte= pagesizeEMS - BegOff; // volnych B na page - CopyLen = pxm->length; // celkova delka - - Next_page2: - if(CopyLen > FreeByte) + + long emmalloc(int n) { - ist = mapEMS(pxm->sourceH, 0, BegPage); - if(ist==0) return( 0 ); - memcpy(DstBuf+DstOff, EmsFrame+BegOff, FreeByte); - - CopyLen = CopyLen - FreeByte; - DstOff += FreeByte; - BegPage++; - BegOff = 0; - FreeByte = pagesizeEMS; - goto Next_page2; + /* + Requests 'n' pages of expanded memory and returns the file + handle + assigned to the pages or -1 if there is an error + */ + + union REGS regs; + + regs.h.ah = 0x43; + regs.x.bx = n; + int86(EMM,®s,®s); + if (regs.h.ah) + return -1; + return regs.x.dx; } - else + + emmmap(long handle, int phys, int page) { - ist = mapEMS(pxm->sourceH, 0, BegPage); - if(ist==0) return( 0 ); - memcpy(DstBuf+DstOff, EmsFrame+BegOff, CopyLen); + /* + Maps a physical page from expanded memory into the page frame + in the + conventional memory 16K window so that data can be transfered + between + the expanded memory and conventional memory. + */ + + union REGS regs; + + regs.h.ah = 0x44; + regs.h.al = page; + regs.x.bx = phys; + regs.x.dx = handle; + int86(EMM,®s,®s); + return (regs.h.ah == 0); } - } - else // Error - { return( 0 ); - } - - return 1; -} - -// ----- Vrati pocet volnych a max. KB EMS -int mem_emem(unsigned int *total, unsigned int *freeall) -{ - int pom; - - pom = coretotalEMS(); - if(pom != -1 ) - *total = pom * 16; - else - return( 0 ); - pom = coreleftEMS(); - if(pom != -1) - *freeall = pom * 16; - else - return( 0 ); - - return( 1 ); -} + + void emmmove(int page, char *str, int n) + { + /* + Move 'n' bytes from conventional memory to the specified + expanded memory + page + */ + + char far *ptr; + + ptr = emmbase + page * 16384; + while(n-- > 0) + *ptr++ = *str++; + } + + void emmget(int page, char *str, int n) + { + /* + Move 'n' bytes from the specified expanded memory page into + conventional + memory + */ + + char far *ptr; + + ptr = emmbase + page * 16384; + while(n-- > 0) + *str++ = *ptr++; + } + + emmclose(long handle) + { + /* + Release control of the expanded memory pages allocated to + 'handle' + */ + + union REGS regs; + + regs.h.ah = 0x45; + regs.x.dx = handle; + int86(EMM,®s,®s); + return (regs.h.ah == 0); + } diff --git a/src/lib/ems.h b/src/lib/ems.h new file mode 100644 index 00000000..b288a548 --- /dev/null +++ b/src/lib/ems.h @@ -0,0 +1,44 @@ +/* + + THE IBM PC PROGRAMMER'S GUIDE TO C + + + + 3rd Edition + + + + Matthew Probert + + + + COPYRIGHT NOTICE + + +This publication remains the property of Matthew Probert. License is +hereby given for this work to be freely distibuted in whole under the +proviso that credit is given to the author. Sections of this work may be +used and distributed without payment under the proviso that credit is +given to both this work and the author. Source code occuring in this work +may be used within commercial and non-commercial applications without +charge and without reference to the author. + */ + +#ifndef __EMS_H_ +#define __EMS_H_ + +#include +#define EMM 0x67 + +char far *emmbase; + +emmtest(); +emmok(); +long emmavail(); +long emmalloc(int n); +emmmap(long handle, int phys, int page); +void emmmove(int page, char *str, int n); +void emmget(int page, char *str, int n); +emmclose(long handle); + +#endif __EMS_H_ diff --git a/src/lib/ems1.c b/src/lib/ems1.c deleted file mode 100644 index dd9ebe53..00000000 --- a/src/lib/ems1.c +++ /dev/null @@ -1,110 +0,0 @@ -#if !defined(__EMS_H) -#define __EMS_H - -#define EMSPAGESIZE (4096) -#define EMS_ERROR (-1) -struct { - unsigned long length; - unsigned char source_type; - unsigned int source_handle; - unsigned int source_ofs; - unsigned int source_seg; - unsigned char dest_type; - unsigned int dest_handle; - unsigned int dest_ofs; - unsigned int dest_seg; -} myEMSmove; - - -void conv2ems(int PAGE, int HANDLE, char *output) { - char *x = (char *) &myEMSmove; - myEMSmove.length = EMSPAGESIZE; - myEMSmove.source_type = 0; - myEMSmove.source_handle = 0; - myEMSmove.source_ofs = FP_OFF(output); - myEMSmove.source_seg = FP_SEG(output); - myEMSmove.dest_type = 1; - myEMSmove.dest_handle = HANDLE; - myEMSmove.dest_ofs = (PAGE & 3) << 12; - myEMSmove.dest_seg = PAGE >> 2; - __asm push ds - __asm mov ax, 5700h; - __asm lds si, x - __asm int 67h - __asm pop ds -} - -void ems2conv(int PAGE, int HANDLE, char *output) { - char *x = (char *) &myEMSmove; - myEMSmove.length = EMSPAGESIZE; - myEMSmove.source_type = 1; - myEMSmove.source_handle = HANDLE; - myEMSmove.source_ofs = (PAGE & 3) << 12; - myEMSmove.source_seg = PAGE>>2; - myEMSmove.dest_type = 0; - myEMSmove.dest_handle = 0; - myEMSmove.dest_ofs = FP_OFF(output); - myEMSmove.dest_seg = FP_SEG(output); - __asm push ds - __asm mov ax, 5700h - __asm lds si, x - __asm int 67h - __asm pop ds -} - - -void freeEMS(int handle) { - __asm mov dx, handle; - __asm mov ah, 45h; - __asm int 67h -} - -int checkEMS() { - char *x = "EMMXXXX0"; - __asm { - mov ax, 3567h - int 21h - mov di, 10 - mov si, offset x - mov cx, 4 - rep cmpsw - je good - } - return -1; - good: - __asm { - sub ax, ax - mov ah, 40h - int 67h - } - return _AX; -} - -int getavailEMS() { - __asm { - mov ah, 42h - int 67h - cmp ah, 00h - je getavailEMS_Okay - mov bx, -1 - } - getavailEMS_Okay: - __asm shl bx, 2 - return _BX; -} - -int allocEMS(int pages) { - __asm { - mov bx, pages - shr bx, 2 - mov ah, 43h - int 67h - cmp ah, 0 - je allocEMS_Okay - mov dx, -1 - } - allocEMS_Okay: - return _DX; -} - -#endif