3 * Module: All Modules
\r
4 * Author(s): Chris Somers
\r
5 * Date: December 1, 1992
\r
8 minor mods by Alex Russell to simplify
\r
10 Must use memory model with FAR code
\r
12 Open Watcom patch by sparky4~
18 #if !defined(__LARGE__) && !defined(__COMPACT__) && !defined(__HUGE__)
\r
19 #error Invalid memory model for compiling MEMORY.C
\r
28 //static globals --------------------------------
\r
30 static int ActiveEMList[MAXEMHANDLES];
\r
31 static unsigned long EMMSeg;
\r
33 //forward declarations ---------------------------------
\r
35 static int EMPresent(void);
\r
36 static int EMReady(void);
\r
37 static unsigned long GetEMMSeg(void);
\r
38 static int GetEMHandle(int NumPages);
\r
39 static int EMMap(int Handle, int LogPg, int PhyPg);
\r
40 static int FreeEMHandle(int Handle);
\r
41 static int GetNumPages(int Handle);
\r
42 static int EMStateSave(int Handle);
\r
43 static void EMStateRestore(int Handle);
\r
45 /********************************************************************/
57 return((unsigned long)EMSinfo);
\r
60 /********************************************************************/
75 /********************************************************************/
80 if (!EMPresent() || !EMReady()) return(NOTREADY);
\r
81 if (!(EMMSeg = GetEMMSeg())) return(NOTREADY); /*lint !e720 */
\r
83 } /* End of OpenEMM() */
\r
85 /********************************************************************/
\r
92 if (!EMMSeg) return;
\r
93 for (i = 0; i < MAXEMHANDLES; i++) {
\r
94 if (ActiveEMList[i]) {
\r
95 FreeEMHandle(ActiveEMList[i]);
\r
96 ActiveEMList[i] = 0;
\r
100 } /* End of CloseEMM() */
\r
102 /********************************************************************/
\r
107 return((EMMSeg) ? TRUE : FALSE); /* successfully opened? */
\r
108 } /* End of EMMInstalled() */
\r
110 /********************************************************************/
\r
116 unsigned long RtnVal = 0UL;
117 unsigned short interr=0;
\r
120 mov ah,0x42 /* get EMM free page count */
\r
123 js InternalError /* returns 80, 81, or 84 hex on error */
\r
124 mov Pages,bx /* number of unallocated 16K pages */
131 RtnVal = ((unsigned long)Pages); /* Pages * 16K rtns bytes*/ //<< 14);
134 } /* End of EMMCoreLeft() */
\r
136 /********************************************************************/
\r
139 EMMalloc(int *Handle, int Pages)
\r
142 char *RtnPtr = NULL;
\r
145 *Handle = NOTREADY;
\r
148 if ((Pages < 1) || (Pages > EMMCoreLeft(/*1020*/))) {
\r
149 *Handle = VALUE_OUTF_RANGE;
\r
152 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i]); i++) ;
\r
153 if (i == MAXEMHANDLES) {
\r
154 *Handle = NOFREEITEMS;
\r
157 if ((ActiveEMList[i] = GetEMHandle(Pages)) > 0) {
\r
158 RtnPtr = MK_FP(EMMSeg, 0);
\r
160 *Handle = ActiveEMList[i];
\r
161 return((void far *)RtnPtr);
\r
162 } /* End of EMMalloc() */
\r
164 /********************************************************************/
\r
167 EMMRealloc(int Handle, int Pages)
\r
169 int RtnCode = FALSE;
\r
171 if (!EMMSeg || (Pages < 0) || (Pages > EMMCoreLeft(/*1020*/))) {
\r
175 mov ah,0x51 /* change # of pages */
\r
180 js NoGo /* returns 80 to 88 hex on error */
187 } /* End of EMMRealloc() */
\r
189 /********************************************************************/
\r
192 EMMFree(int Handle)
\r
196 if (!EMMSeg) return;
\r
197 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
198 if (i >= MAXEMHANDLES) return;
\r
201 if (FreeEMHandle(ActiveEMList[i])) break;
\r
203 ActiveEMList[i] = 0;
\r
204 } /* End of EMMFree() */
\r
206 /********************************************************************/
\r
208 int /* EMM map for application */
\r
209 MapEMM(int Handle, int Start, int Pages)
\r
213 if (!EMMSeg) return(NOTREADY);
\r
214 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
215 if (i == MAXEMHANDLES) return (NO_DATA);
\r
216 if ((GetNumPages(Handle) < Pages) || (Pages < 1) || (Pages > 4)) {
\r
217 return (VALUE_OUTF_RANGE);
\r
219 for (i = Start; i < Start + Pages; i++) {
\r
220 if (!EMMap(Handle, i, i - Start)) return(NO_DATA);
\r
223 } /* End of MapEMM() */
\r
225 /********************************************************************/
\r
227 void /* EMM unmap for application */
\r
228 UnmapEMM(int Handle, int Start, int Pages)
\r
232 if (!EMMSeg) return;
\r
233 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
234 if (i == MAXEMHANDLES) return;
\r
236 if ((Pages < 1) || (j > 4)) return;
\r
238 for (i = Start; i < j; i++) {
\r
239 EMMap(Handle, NONE, i);
\r
241 } /* End of UnmapEMM() */
\r
243 /********************************************************************/
\r
245 int /* EMM map for devices - saves EMM state */
\r
246 UseEMM(int Handle, int Start, int Pages)
\r
248 EMStateSave(Handle);
\r
249 return(MapEMM(Handle, Start, Pages));
\r
250 } /* End of UseEMM() */
\r
252 /********************************************************************/
\r
254 void /* EMM unmap for devices - restores EMM state */
\r
255 SaveEMM(int Handle, int Start, int Pages)
\r
257 UnmapEMM(Handle, Start, Pages);
\r
258 EMStateRestore(Handle);
\r
259 } /* End of SaveEMM() */
\r
261 /********************************************************************/
\r
267 char EMName[] = "EMMXXXX0";
\r
270 _asm { /* can be replaced with getvect() */
\r
272 mov ax,0x3567 /* get vector for int 67h */
\r
278 t = MK_FP(Segment, 0x0A); /* point to driver name */
\r
280 for (i = 0; (i < 8) && (*s++ == *t++); i++) ; /* strncmp equivalent */
\r
282 if (i == 8) return(TRUE);
\r
284 } /*End of EMPresent() */
\r
286 /********************************************************************/
\r
293 mov ah,0x40 /* get EM Manager Status */
\r
296 jns Ready /* returns 80, 81, or 84 hex on error */
307 } /* End of EMReady() */
\r
309 /********************************************************************/
\r
311 static unsigned long
\r
314 unsigned int EMSegment;
\r
317 mov ah,0x41 /* get EMM page frame segment */
\r
320 js NotReady /* returns 80, 81, or 84 hex on error */
\r
324 mov EMSegment,NOTREADY
327 return(EMSegment); /*lint !e530 */
\r
330 // return(NOTREADY);
\r
331 } /* End of GetEMMSeg() */
333 /********************************************************************/
\r
338 unsigned int EMSegment;
\r
341 mov ah,0x41 /* get EMM page frame segment */
\r
344 js NotReady /* returns 80, 81, or 84 hex on error */
\r
348 mov EMSegment,NOTREADY
351 return(EMSegment); /*lint !e530 */
\r
354 // return(NOTREADY);
\r
355 } /* End of GetEMMSeg0() */
\r
357 /********************************************************************/
\r
360 GetEMHandle(int NumPages)
\r
365 mov ah,0x43 /* get handle and allocate EM */
\r
366 mov bx,NumPages /* number of 16K pages to allocate */
\r
368 or ah,ah /* returns 80 to 89 hex on error */
\r
370 mov NewHandle,dx /* retrieve handle */
373 mov NewHandle,NO_DATA
379 // return(NO_DATA);
\r
380 } /* End of GetEMHandle() */
\r
382 /********************************************************************/
\r
385 EMMap(int Handle, int LogPg, int PhyPg)
\r
387 int RtnCode = NO_DATA;
\r
390 mov ax,PhyPg /* physical page: 0 - 3 in AL only */
\r
391 mov ah,0x44 /* map logical to physical page */
\r
392 mov bx,LogPg /* logical page: 0 - 1020 */
\r
395 or ah,ah /* returns 80 to 8B hex on error */
\r
402 // RtnCode = SUCCESS;
\r
406 } /* End of EMMap() */
\r
408 /********************************************************************/
\r
411 FreeEMHandle(int Handle)
\r
415 mov ah,0x45 /* free handle and deallocate EM */
\r
418 or ah,ah /* returns 80 to 86 hex on error */
\r
420 mov FreeEMShandle,SUCCESS
422 NotFreed: /* must retry if unsuccessful */
423 mov FreeEMShandle,NO_DATA
426 return(FreeEMShandle);
\r
428 //NotFreed: /* must retry if unsuccessful */
\r
429 // return(NO_DATA);
\r
430 } /* End of FreeEMHandle() */
\r
432 /********************************************************************/
\r
435 GetNumPages(int Handle)
\r
440 mov ah,0x4C /* get allocated pages for Handle */
\r
443 or ah,ah /* returns 80 to 84 hex on error */
\r
453 } /* End of GetNumPages() */
\r
455 /********************************************************************/
\r
458 EMStateSave(int Handle)
\r
460 int RtnCode = NO_MEMORY;
\r
462 mov ah,0x47 /* save page map under Handle */
\r
466 js Unsaved /* out of save space error */
470 // RtnCode = SUCCESS;
\r
474 } /* End of EMStateSave() */
\r
476 /********************************************************************/
\r
479 EMStateRestore(int Handle)
\r
482 mov ah,0x48 /* restore page map for Handle */
\r
484 int 0x67 /* ignore error */
\r
486 } /* End of EMStateRestore() */
\r