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
16 #if !defined(__LARGE__) && !defined(__COMPACT__) && !defined(__HUGE__)
\r
17 #error Invalid memory model for compiling MEMORY.C
\r
26 //static globals --------------------------------
\r
28 static int ActiveEMList[MAXEMHANDLES];
\r
29 static unsigned int EMMSeg;
\r
31 //forward declarations ---------------------------------
\r
33 static int EMPresent(void);
\r
34 static int EMReady(void);
\r
35 static unsigned int GetEMMSeg(void);
\r
36 static int GetEMHandle(int NumPages);
\r
37 static int EMMap(int Handle, int LogPg, int PhyPg);
\r
38 static int FreeEMHandle(int Handle);
\r
39 static int GetNumPages(int Handle);
\r
40 static int EMStateSave(int Handle);
\r
41 static void EMStateRestore(int Handle);
\r
44 /********************************************************************/
\r
48 if (!EMPresent() || !EMReady()) return(NOTREADY);
\r
49 if (!(EMMSeg = GetEMMSeg())) return(NOTREADY); /*lint !e720 */
\r
51 } /* End of OpenEMM() */
\r
53 /********************************************************************/
\r
60 if (!EMMSeg) return;
\r
61 for (i = 0; i < MAXEMHANDLES; i++) {
\r
62 if (ActiveEMList[i]) {
\r
63 FreeEMHandle(ActiveEMList[i]);
\r
64 ActiveEMList[i] = 0;
\r
68 } /* End of CloseEMM() */
\r
70 /********************************************************************/
\r
75 return((EMMSeg) ? TRUE : FALSE); /* successfully opened? */
\r
76 } /* End of EMMInstalled() */
\r
78 /********************************************************************/
\r
84 unsigned long RtnVal = 0UL;
\r
87 mov ah,0x42 /* get EMM free page count */
\r
90 js InternalError /* returns 80, 81, or 84 hex on error */
\r
91 mov Pages,bx /* number of unallocated 16K pages */
\r
93 RtnVal = ((unsigned long)Pages << 14); /* Pages * 16K rtns bytes*/
\r
97 } /* End of EMMCoreLeft() */
\r
99 /********************************************************************/
\r
102 EMMalloc(int *Handle, int Pages)
\r
105 char *RtnPtr = NULL;
\r
108 *Handle = NOTREADY;
\r
111 if ((Pages < 1) || (Pages > 1020)) {
\r
112 *Handle = VALUE_OUTF_RANGE;
\r
115 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i]); i++) ;
\r
116 if (i == MAXEMHANDLES) {
\r
117 *Handle = NOFREEITEMS;
\r
120 if ((ActiveEMList[i] = GetEMHandle(Pages)) > 0) {
\r
121 RtnPtr = MK_FP(EMMSeg, 0);
\r
123 *Handle = ActiveEMList[i];
\r
124 return((void far *)RtnPtr);
\r
125 } /* End of EMMalloc() */
\r
127 /********************************************************************/
\r
130 EMMRealloc(int Handle, int Pages)
\r
132 int RtnCode = FALSE;
\r
134 if (!EMMSeg || (Pages < 0) || (Pages > 1020)) {
\r
138 mov ah,0x51 /* change # of pages */
\r
143 js NoGo /* returns 80 to 88 hex on error */
\r
150 } /* End of EMMRealloc() */
\r
152 /********************************************************************/
\r
155 EMMFree(int Handle)
\r
159 if (!EMMSeg) return;
\r
160 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
161 if (i >= MAXEMHANDLES) return;
\r
164 if (FreeEMHandle(ActiveEMList[i])) break;
\r
166 ActiveEMList[i] = 0;
\r
167 } /* End of EMMFree() */
\r
169 /********************************************************************/
\r
171 int /* EMM map for application */
\r
172 MapEMM(int Handle, int Start, int Pages)
\r
176 if (!EMMSeg) return(NOTREADY);
\r
177 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
178 if (i == MAXEMHANDLES) return (NO_DATA);
\r
179 if ((GetNumPages(Handle) < Pages) || (Pages < 1) || (Pages > 4)) {
\r
180 return (VALUE_OUTF_RANGE);
\r
182 for (i = Start; i < Start + Pages; i++) {
\r
183 if (!EMMap(Handle, i, i - Start)) return(NO_DATA);
\r
186 } /* End of MapEMM() */
\r
188 /********************************************************************/
\r
190 void /* EMM unmap for application */
\r
191 UnmapEMM(int Handle, int Start, int Pages)
\r
195 if (!EMMSeg) return;
\r
196 for (i = 0; (i < MAXEMHANDLES) && (ActiveEMList[i] != Handle); i++) ;
\r
197 if (i == MAXEMHANDLES) return;
\r
199 if ((Pages < 1) || (j > 4)) return;
\r
201 for (i = Start; i < j; i++) {
\r
202 EMMap(Handle, NONE, i);
\r
204 } /* End of UnmapEMM() */
\r
206 /********************************************************************/
\r
208 int /* EMM map for devices - saves EMM state */
\r
209 UseEMM(int Handle, int Start, int Pages)
\r
211 EMStateSave(Handle);
\r
212 return(MapEMM(Handle, Start, Pages));
\r
213 } /* End of UseEMM() */
\r
215 /********************************************************************/
\r
217 void /* EMM unmap for devices - restores EMM state */
\r
218 SaveEMM(int Handle, int Start, int Pages)
\r
220 UnmapEMM(Handle, Start, Pages);
\r
221 EMStateRestore(Handle);
\r
222 } /* End of SaveEMM() */
\r
224 /********************************************************************/
\r
230 char EMName[] = "EMMXXXX0";
\r
233 _asm { /* can be replaced with getvect() */
\r
235 mov ax,0x3567 /* get vector for int 67h */
\r
241 t = MK_FP(Segment, 0x0A); /* point to driver name */
\r
243 for (i = 0; (i < 8) && (*s++ == *t++); i++) ; /* strncmp equivalent */
\r
245 if (i == 8) return(TRUE);
\r
247 } /*End of EMPresent() */
\r
249 /********************************************************************/
\r
255 mov ah,0x40 /* get EM Manager Status */
\r
258 jns Ready /* returns 80, 81, or 84 hex on error */
\r
264 } /* End of EMReady() */
\r
266 /********************************************************************/
\r
268 static unsigned int
\r
271 unsigned int EMSegment;
\r
274 mov ah,0x41 /* get EMM page frame segment */
\r
277 js NotReady /* returns 80, 81, or 84 hex on error */
\r
280 return(EMSegment); /*lint !e530 */
\r
284 } /* End of GetEMMSeg() */
\r
286 /********************************************************************/
\r
289 GetEMHandle(int NumPages)
\r
294 mov ah,0x43 /* get handle and allocate EM */
\r
295 mov bx,NumPages /* number of 16K pages to allocate */
\r
297 or ah,ah /* returns 80 to 89 hex on error */
\r
299 mov NewHandle,dx /* retrieve handle */
\r
305 } /* End of GetEMHandle() */
\r
307 /********************************************************************/
\r
310 EMMap(int Handle, int LogPg, int PhyPg)
\r
312 int RtnCode = NO_DATA;
\r
315 mov ax,PhyPg /* physical page: 0 - 3 in AL only */
\r
316 mov ah,0x44 /* map logical to physical page */
\r
317 mov bx,LogPg /* logical page: 0 - 1020 */
\r
320 or ah,ah /* returns 80 to 8B hex on error */
\r
327 } /* End of EMMap() */
\r
329 /********************************************************************/
\r
332 FreeEMHandle(int Handle)
\r
335 mov ah,0x45 /* free handle and deallocate EM */
\r
338 or ah,ah /* returns 80 to 86 hex on error */
\r
343 NotFreed: /* must retry if unsuccessful */
\r
345 } /* End of FreeEMHandle() */
\r
347 /********************************************************************/
\r
350 GetNumPages(int Handle)
\r
355 mov ah,0x4C /* get allocated pages for Handle */
\r
358 or ah,ah /* returns 80 to 84 hex on error */
\r
365 } /* End of GetNumPages() */
\r
367 /********************************************************************/
\r
370 EMStateSave(int Handle)
\r
372 int RtnCode = NO_MEMORY;
\r
374 mov ah,0x47 /* save page map under Handle */
\r
378 js Unsaved /* out of save space error */
\r
384 } /* End of EMStateSave() */
\r
386 /********************************************************************/
\r
389 EMStateRestore(int Handle)
\r
392 mov ah,0x48 /* restore page map for Handle */
\r
394 int 0x67 /* ignore error */
\r
396 } /* End of EMStateRestore() */
\r