-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmsapi.cpp
350 lines (283 loc) · 6.28 KB
/
xmsapi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdio.h>
#include <dos.h>
#include <mem.h>
#include "xmsapi.h"
int (far *xms_control)() = NULL;
static int xms_errno = 0;
typedef unsigned int UINT;
/** return size in number of pages... */
static int size_in_page(size_t size)
{
if ((size % 16) != 0)
return ((size / 16) + 1);
return size / 16;
}
static int size_in_kb(size_t size)
{
if ((size % 1024) != 0)
return ((size / 1024) + 1);
return size / 1024;
}
/** XMSReady return XMS driver status => 0 ok. 1 error */
int XMSReady()
{
_AX = 0x4300; // service is installed?
geninterrupt(0x2f);
int ret = _AL;
if (ret == 0x80)
{
_AX = 0x4310; // query xms control pointers
geninterrupt(0x2f);
_asm {
mov word ptr [xms_control], bx
mov word ptr [xms_control+2], es
}
printf("XMSControl found at %Fp\n", xms_control);
return 0;
}
// error .. service not ready!
return 1;
}
/** XMM Version
* ARGS: None
* RETS: AX = XMS Version
* BX = Internal Driver Version Number
* DX = 1 if HMA exists, 0 if it doesn't
*/
void XMM_Version(REGS *regs)
{
regs->x.ax = XMS_VERSION;
regs->x.bx = HIMEM_VERSION;
return;
}
static char fHMAInUse = 0;
static char fHMAExists = 0;
static size_t minHMASize = 0;
/** XMM RequestHMA
* ARGS: bytes (DX)
* RETS: error or 0 */
int XMM_RequestHMA(REGS *regs, size_t bytes)
{
if (fHMAInUse == 1)
return ERR_HMAINUSE;
if (fHMAExists == 0)
return ERR_HMANOTEXIST;
if (minHMASize < bytes)
return ERR_HMAMINSIZE;
fHMAInUse = 1;
regs->x.ax = 1;
return ERR_NOERROR;
}
int XMM_ReleaseHMA(REGS *regs)
{
if (fHMAInUse == 0)
return ERR_HMANOTALLOCED;
regs->x.ax = 1;
fHMAInUse = 0;
return ERR_NOERROR;
}
static char fCanChangeA20 = 0;
static int EnableCount = 0;
static int (*a20Handler)(int enable) = NULL;
int XMM_LocalEnableA20(REGS *regs)
{
return ERR_A20;
}
int XMM_LocalDisableA20(REGS *regs)
{
return ERR_A20;
}
/** IsA20On => compare two different memory location if a20 is on,
otherwise the address on bus is the same! */
int XMM_IsA20On(REGS *regs)
{
void far *lowmem = MK_FP(0x0000, 0x0080);
void far *highmem = MK_FP(0xFFFF, 0x0090);
if (_fmemcmp(lowmem, highmem, 8) == 0)
regs->x.ax = 1;
else
regs->x.ax = 0;
return ERR_NOERROR;
}
static char fGlobalEnable = 0;
int XMM_GlobalEnableA20(REGS *regs)
{
if (fGlobalEnable == 0) {
int r = XMM_LocalEnableA20(regs);
if (r == ERR_NOERROR) {
fGlobalEnable = 1;
} else {
return r;
}
}
regs->x.ax = 1;
return ERR_NOERROR;
}
int XMM_GlobalDisableA20(REGS *regs)
{
if (fGlobalEnable == 1) {
int r = XMM_LocalDisableA20(regs);
if (r == ERR_NOERROR) {
fGlobalEnable = 0;
} else {
return r;
}
}
regs->x.ax = 1;
return ERR_NOERROR;
}
union SegOff
{
struct {
unsigned offset;
unsigned segment;
} addr;
unsigned long address;
};
struct ExtMemMove
{
unsigned long Length;
HANDLE SourceHandle;
union SegOff SourceOffset;
HANDLE DestHandle;
union SegOff DestOffset;
};
/** AmountOfMemory */
static long AmountOfMemory = 0;
static long SizeOfLargestFreeBlock = 0;
static long AmountOfUsedMemory = 0;
int XMM_QueryExtMemory(REGS *regs)
{
int r = ERR_OUTOFMEMORY;
regs->x.ax = (int) SizeOfLargestFreeBlock / 1024;
regs->x.dx = (int) (AmountOfMemory - AmountOfUsedMemory) / 1024;
r = ERR_NOERROR;
return r;
}
int XMM_AllocExtMemory(REGS *regs, size_t size)
{
long mem_available = AmountOfMemory - AmountOfUsedMemory;
long mem_required = size * 1024;
if (mem_available < mem_required) {
return ERR_OUTOFMEMORY;
}
unsigned segp;
if (_dos_allocmem(1, &segp) != 0) { // cannot allocate handle!
return ERR_OUTOFMEMORY;
}
HANDLE handle = (HANDLE)(MK_FP(segp, 0));
_fmemset(handle, 0, sizeof(HANDLE));
handle->TAG = XMM_TAG;
regs->x.ax = 1;
regs->x.dx = segp;
return ERR_NOERROR;
}
int XMM_FreeExtMemory(REGS *regs, unsigned argdx)
{
HANDLE handle = (HANDLE) MK_FP(argdx, 0);
if (handle->TAG != XMM_TAG) {
return ERR_INVALIDHANDLE;
}
// mark as free memory...
_dos_freemem(argdx); // deallocate memory...
return ERR_NOERROR;
}
/** this operation require a switch in 32bit! */
int XMM_MoveExtMemory(REGS *regs, struct ExtMemMove *far emb)
{
return ERR_INVALIDHANDLE;
}
int XMM_LockExtMemory(REGS *regs, unsigned argdx)
{
HANDLE handle = (HANDLE) MK_FP(argdx, 0);
if (handle->TAG != XMM_TAG) {
return ERR_INVALIDHANDLE;
}
return ERR_NOERROR;
}
int XMM_UnlockExtMemory(REGS *regs, unsigned argdx)
{
HANDLE handle = (HANDLE) MK_FP(argdx, 0);
if (handle->TAG != XMM_TAG) {
return ERR_INVALIDHANDLE;
}
return ERR_NOERROR;
}
int XMM_GetExtMemoryInfo(REGS *regs, unsigned argdx)
{
HANDLE handle = (HANDLE) MK_FP(argdx, 0);
if (handle->TAG != XMM_TAG) {
return ERR_INVALIDHANDLE;
}
regs->x.ax = 1;
regs->h.bh = 0;
regs->h.bl = 0;
regs->x.dx = 0;
return ERR_NOERROR;
}
int XMM_ReallocExtMemory(REGS *regs, unsigned argdx)
{
HANDLE handle = (HANDLE) MK_FP(argdx, 0);
if (handle->TAG != XMM_TAG) {
return ERR_INVALIDHANDLE;
}
regs->x.ax = 1;
regs->h.bh = 0;
regs->h.bl = 0;
regs->x.dx = 0;
return ERR_NOERROR;
}
/** Entry point of XMM Control called by software */
void XMM_DISPATCH(int Arg1, int Arg2, void far *arg)
{
REGS regs;
memset(®s, 0, sizeof(REGS));
int err = ERR_NOERROR;
int FunctionId = (Arg1 & 0xFF00) >> 8;
switch(FunctionId) {
case 0: /* Version */
XMM_Version(®s); break;
case 1: /* RequestHMA */
err = XMM_RequestHMA(®s, Arg2); break;
case 2:
err = XMM_ReleaseHMA(®s); break;
case 3:
err = XMM_GlobalEnableA20(®s); break;
case 4:
err = XMM_GlobalDisableA20(®s); break;
case 5:
err = XMM_LocalEnableA20(®s); break;
case 6:
err = XMM_LocalDisableA20(®s); break;
case 7:
err = XMM_IsA20On(®s); break;
case 8:
err = XMM_QueryExtMemory(®s); break;
case 9:
err = XMM_AllocExtMemory(®s, Arg2); break;
case 10:
err = XMM_FreeExtMemory(®s, Arg2); break;
case 11:
err = XMM_MoveExtMemory(®s, (struct ExtMemMove *far ) arg); break;
case 12:
err = XMM_LockExtMemory(®s, Arg2); break;
case 13:
err = XMM_UnlockExtMemory(®s, Arg2); break;
case 14:
err = XMM_GetExtMemoryInfo(®s, Arg2); break;
case 15:
err = XMM_ReallocExtMemory(®s, Arg2); break;
default:
err = ERR_NOTIMPLEMENTED;
break;
}
if (err == ERR_NOERROR) {
_AX = regs.x.ax;
_DX = regs.x.dx;
_BX = regs.x.bx;
} else {
_AX = regs.x.ax;
_BH = 0;
_BL = err;
}
}