OSDN Git Service

wwww
[proj16/16.git] / src / lib / ems.c
1 /*
2
3                    THE IBM PC PROGRAMMER'S GUIDE TO C\r
4                                     \r
5                                     \r
6                                     \r
7                                3rd Edition\r
8                                     \r
9                                     \r
10                                     \r
11                              Matthew Probert\r
12 \r
13 \r
14                                     \r
15                             COPYRIGHT NOTICE\r
16 \r
17 \r
18 This publication remains the property of Matthew Probert. License is\r
19 hereby given for this work to be freely distibuted in whole under the\r
20 proviso that credit is given to the author. Sections of this work may be\r
21 used and distributed without payment under the proviso that credit is\r
22 given to both this work and the author. Source code occuring in this work\r
23 may be used within commercial and non-commercial applications without\r
24 charge and without reference to the author.
25 */
26      /*\r
27      Various functions for using Expanded memory\r
28      */\r
29 #include "src\lib\ems.h"\r
30      emmtest()\r
31      {\r
32           /*\r
33           Tests for the presence of expnaded memory by attempting to\r
34           open the file EMMXXXX0.\r
35           */\r
36      \r
37           union REGS regs;\r
38           struct SREGS sregs;\r
39           int error;\r
40           long handle;\r
41      \r
42           /* Attempt to open the file device EMMXXXX0 */\r
43           regs.x.ax = 0x3d00;\r
44           regs.x.dx = (int)"EMMXXXX0";\r
45           sregs.ds = 0; //????\r
46           intdosx(&regs,&regs,&sregs);\r
47           handle = regs.x.ax;\r
48           error = regs.x.cflag;\r
49      \r
50           if (!error)\r
51           {\r
52                regs.h.ah = 0x3e;\r
53                regs.x.bx = handle;\r
54                intdos(&regs,&regs);\r
55           }\r
56           return error;\r
57      }\r
58      \r
59      emmok()\r
60      {\r
61           /*\r
62           Checks whether the expanded memory manager responds correctly\r
63           */\r
64      \r
65           union REGS regs;\r
66      \r
67           regs.h.ah = 0x40;\r
68           int86(EMM,&regs,&regs);\r
69      \r
70           if (regs.h.ah)\r
71                return 0;\r
72      \r
73           regs.h.ah = 0x41;\r
74           int86(EMM,&regs,&regs);\r
75      \r
76           if (regs.h.ah)\r
77                return 0;\r
78      \r
79           emmbase = MK_FP(regs.x.bx,0);\r
80           return 1;\r
81      }\r
82      \r
83      long emmavail()\r
84      {\r
85         /*\r
86         Returns the number of available (free) 16K pages of expanded\r
87      memory\r
88         or -1 if an error occurs.\r
89         */\r
90      \r
91              union REGS regs;\r
92      \r
93           regs.h.ah = 0x42;\r
94           int86(EMM,&regs,&regs);\r
95           if (!regs.h.ah)\r
96                return regs.x.bx;\r
97           return -1;\r
98      }\r
99      \r
100      long emmalloc(int n)\r
101      {\r
102           /*\r
103           Requests 'n' pages of expanded memory and returns the file\r
104      handle\r
105           assigned to the pages or -1 if there is an error\r
106           */\r
107      \r
108           union REGS regs;\r
109      \r
110           regs.h.ah = 0x43;\r
111           regs.x.bx = n;\r
112           int86(EMM,&regs,&regs);\r
113           if (regs.h.ah)\r
114                return -1;\r
115           return regs.x.dx;\r
116      }\r
117      \r
118      emmmap(long handle, int phys, int page)\r
119      {\r
120           /*\r
121           Maps a physical page from expanded memory into the page frame\r
122      in the\r
123           conventional memory 16K window so that data can be transfered\r
124      between\r
125           the expanded memory and conventional memory.\r
126           */\r
127      \r
128           union REGS regs;\r
129      \r
130           regs.h.ah = 0x44;\r
131           regs.h.al = page;\r
132           regs.x.bx = phys;\r
133           regs.x.dx = handle;\r
134           int86(EMM,&regs,&regs);\r
135           return (regs.h.ah == 0);\r
136      }\r
137      \r
138      void emmmove(int page, char *str, int n)\r
139      {\r
140           /*\r
141           Move 'n' bytes from conventional memory to the specified\r
142      expanded memory\r
143           page\r
144           */\r
145      \r
146           char far *ptr;\r
147      \r
148           ptr = emmbase + page * 16384;\r
149           while(n-- > 0)\r
150                *ptr++ = *str++;\r
151      }\r
152      \r
153      void emmget(int page, char *str, int n)\r
154      {\r
155           /*\r
156           Move 'n' bytes from the specified expanded memory page into\r
157      conventional\r
158           memory\r
159           */\r
160      \r
161           char far *ptr;\r
162      \r
163           ptr = emmbase + page * 16384;\r
164           while(n-- > 0)\r
165                *str++ = *ptr++;\r
166      }\r
167      \r
168      emmclose(long handle)\r
169      {\r
170           /*\r
171           Release control of the expanded memory pages allocated to\r
172      'handle'\r
173           */\r
174      \r
175           union REGS regs;\r
176      \r
177           regs.h.ah = 0x45;\r
178           regs.x.dx = handle;\r
179           int86(EMM,&regs,&regs);\r
180           return (regs.h.ah == 0);\r
181      }