-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXMS.ASM
566 lines (450 loc) · 17.8 KB
/
XMS.ASM
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
;;
;; XMS.ASM - routines to use Extended Memory from a DOS program.
;;
;; Copyright (C) 1999, 2000, 2001, Imre Leber.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have recieved a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;
;; If you have any questions, comments, suggestions, or fixes please
;; email me at: [email protected]
;;
;;
;;*************************************************************************
;;
;; XMS.ASM
;;
;; Routines to use Extended Memory, the HMA and upper memory from
;; a DOS program.
;;
;; NOTE: Some of these routines are translations from the XMS routines
;; by Cliff Rhodes to NASM by Imre Leber.
;;
;; The C version was released to the public domain by Cliff Rhodes with
;; no guarantees of any kind.
;;
;; The assembly version is hereby put under GNU General Public License by
;; Imre Leber.
;;
;;**************************************************************************
;; version: 24 dec 1999
;;
;; just to be on the save side.
%macro SaveRegs 0
push si
push di
push ds
%endmacro
%macro RestoreRegs 0
pop ds
pop di
pop si
%endmacro
%define XMS_INT 002fh ;; DOS Multiplex interrupt
segment _DATA class=DATA
XMSDriver DD 0
initFlag DB -1
;;struct XMSRequestBlock bd.
bd
nbytes dd 0 ;; Number of bytes to move
shandle dw 0 ;; Handle of source memory
soffset dd 0 ;; Offset of source in handle's memory area
dhandle dw 0 ;; Handle of destination memory
doffset dd 0 ;; Offset of destination in memory
UMBsize dw 0 ;; size of the last successfully allocated UMB.
segment _TEXT class=CODE
;*********************** routines for the EMB's **************************
;==========================================================================
;=== XMMinit (XMSinit) ===
;==========================================================================
;=== int XMMinit(void); or int XMSinit(void); ===
;=== ===
;=== Verifies wether an eXtended Memory Manager is installed. ===
;=== ===
;=== Returns 1 if manager found, 0 if not. ===
;=== ===
;=== NOTE:This function should be called before any other XMS function! ===
;==========================================================================
global _XMSinit
global _XMMinit
_XMSinit:
_XMMinit:
push bp
SaveRegs
cmp [initFlag], byte -1
jne .EndOfProc
mov [initFlag], byte 0
mov ax, 4300h ;; Verify XMS manager present.
int XMS_INT
cmp al, 80h
je .next
xor ax, ax
jmp .EndOfProc
.next:
mov ax, 4310h ;; Get XMS manager entry point.
int XMS_INT
mov [word XMSDriver], bx ;; Save entry point.
mov [word XMSDriver+02h], es
xor ah, ah
call far [dword XMSDriver] ;; See if at least version 2.0
cmp ax, 0200h
jb .EndOfProc
mov [initFlag], byte 1
.EndOfProc:
xor ah, ah
mov al, [initFlag]
RestoreRegs
pop bp
ret
;=========================================================================
;=== XMScoreleft ===
;=========================================================================
;=== long XMScoreleft(void); ===
;=== ===
;=== Returns number of bytes available in largest free block. ===
;=========================================================================
global _XMScoreleft
_XMScoreleft:
push bp
SaveRegs
cmp [initFlag], byte 0
jne .next
xor ax, ax
xor dx, dx
jmp .EndOfProc
.next:
mov ax, 0800h
call far [dword XMSDriver]
mov bx, 1024
mul bx
.EndOfProc:
RestoreRegs
pop bp
ret
;==========================================================================
;=== XMSalloc ===
;==========================================================================
;=== unsigned int XMSalloc(long size); ===
;=== ===
;=== Attempts to allocate size bytes of extended memory. ===
;=== ===
;=== Returns handle if successful, 0 if not. ===
;=== ===
;=== NOTE: Actual size allocated will be the smallest multiple of 1024 ===
;=== that is larger than size. ===
;==========================================================================
global _XMSalloc
_XMSalloc:
push bp
mov bp, sp
SaveRegs
cmp [initFlag], byte 0
jne .next
xor ax, ax
jmp .EndOfProc
.next:
;;Get the number of 1024 byte units required by size.
mov ax, [bp+04h]
mov dx, [bp+06h]
mov bx, 1024
div bx
cmp dx, 0
je .next1
;;Add a block for any excess.
inc ax
.next1:
mov dx, ax
mov ax, 0900h
call far [dword XMSDriver]
cmp ax, 1
je .next2
xor ax, ax
jmp .EndOfProc
.next2:
mov ax, dx
.EndOfProc:
RestoreRegs
pop bp
ret
%if 0
;==========================================================================
;=== XMSrealloc ===
;==========================================================================
;=== int XMSrealloc(unsigned int handle, long size); ===
;=== ===
;=== Tries to expand or schrink a certain extended memory block. ===
;=== ===
;=== Returns 1 if successful, 0 if not. ===
;==========================================================================
global _XMSrealloc
_XMSrealloc:
push bp
mov bp, sp
mov ax, [bp+06h]
mov dx, [bp+08h]
mov bx, 1024
div bx
cmp dx, 0
je .next1
;;Add a block for any excess.
inc ax
.next1:
mov dx, [bp+04h]
mov bx, ax
mov ax, 0f00h
call far [dword XMSDriver]
pop bp
ret
%endif
;===========================================================================
;=== XMSfree ===
;===========================================================================
;=== int XMSfree(unsigned int handle); ===
;=== ===
;=== Attempts to free extended memory referred to by handle. Returns 1 ===
;=== if successful, 0 if not. ===
;===========================================================================
global _XMSfree
_XMSfree:
push bp
mov bp, sp
SaveRegs
cmp [byte initFlag], byte 0
jne .next
xor ax, ax
jmp .EndOfProc
.next:
mov dx, [bp+04h]
mov ax, 0a00h
call far [dword XMSDriver]
.EndOfProc:
RestoreRegs
pop bp
ret
;------------------------------------------------------------------------
;--- XMSmove ---
;------------------------------------------------------------------------
;--- private: XMSmove ---
;--- ---
;--- Copy memory to and from XMS. ---
;--- ---
;--- in: ax: number of bytes to copy. ---
;--- ---
;--- out: ax: number of bytes copied (0 if error). ---
;------------------------------------------------------------------------
XMSmove:
push ax
mov [word nbytes], ax
mov [word nbytes+02h], word 0
mov si, bd
mov ah, 0Bh
call far [dword XMSDriver]
pop dx
cmp ax, 0
je .EndOfProc
mov ax, dx
.EndOfProc
ret
;===========================================================================
;=== DOStoXMSmove ===
;===========================================================================
;=== int DOStoXMSmove(unsigned int desthandle, long destoff, ===
;=== const char *src, unsigned n); ===
;=== ===
;=== Attempts to copy n bytes from DOS src buffer to desthandle memory ===
;=== area. ===
;=== Returns number of bytes copied, or 0 on error. ===
;===========================================================================
global _DOStoXMSmove
_DOStoXMSmove:
push bp
mov bp, sp
SaveRegs
cmp [initFlag], byte 0
jne .next
xor ax, ax
jmp .EndOfProc
.next:
mov [shandle], word 0
mov ax, [bp+04h]
mov [dhandle], ax
mov ax, [bp+06h]
mov [doffset], ax
mov ax, [bp+08h]
mov [doffset+02h], ax
mov ax, [bp+0Ah]
mov [word soffset], ax
mov ax, ds
mov [word soffset+02h], ax
mov ax, [bp+0Ch]
call XMSmove
.EndOfProc:
RestoreRegs
pop bp
ret
;==========================================================================
;=== XMStoDOSmove ===
;==========================================================================
;=== int XMStoDOSmove(char *dest, unsigned int srchandle, long srcoff, ===
;=== unsigned n); ===
;=== ===
;=== Attempts to copy n bytes to DOS dest buffer from srchandle memory ===
;=== area. ===
;=== ===
;=== Returns number of bytes copied, or 0 on error. ===
;==========================================================================
global _XMStoDOSmove
_XMStoDOSmove:
push bp
mov bp, sp
SaveRegs
cmp [initFlag], byte 0
jne .next
xor ax, ax
jmp .EndOfProc
.next:
mov [dhandle], word 0
mov ax, [bp+04h]
mov [word doffset], ax
mov ax, ds
mov [word doffset+02h], ax
mov ax, [bp+06h]
mov [word shandle], ax
mov ax, [bp+08h]
mov [word soffset], ax
mov ax, [bp+0Ah]
mov [word soffset+02h], ax
mov ax, [bp+0Ch]
call XMSmove
.EndOfProc:
RestoreRegs
pop bp
ret
;*********************** routines for the HMA ****************************
%ifdef INCLUDEHMA
;==========================================================================
;=== HMAalloc ===
;==========================================================================
;=== int HMAalloc(void); ===
;=== ===
;=== Allocates the HMA if it is available. ===
;=== ===
;=== Returns: 1 on success, 0 on failure. ===
;==========================================================================
global _HMAalloc
_HMAalloc:
SaveRegs
mov ah, 01h
mov dx, 0FFFFh
call far [dword XMSDriver] ;; Allocate HMA.
cmp ax, 0
je .EndOfProc ;; exit on error.
mov ah, 03h
call far [dword XMSDriver] ;; Open gate A20.
cmp ax, 0 ;; exit on success.
jne .EndOfProc
mov ah, 02h
call far [dword XMSDriver] ;; release the HMA on failure.
xor ax, ax
.EndOfProc
RestoreRegs
ret
;==========================================================================
;=== HMAcoreleft ===
;==========================================================================
;=== int HMAcoreleft(void); ===
;=== ===
;=== Returns the size of the HMA in bytes. ===
;=== ===
;=== Remark: Only returns the right size after the HMA has been ===
;=== succesfully allocated. ===
;==========================================================================
global _HMAcoreleft
_HMAcoreleft:
mov ax, 65520
ret
;==========================================================================
;=== HMAfree ===
;==========================================================================
;=== int HMAfree(void); ===
;=== ===
;=== Deallocates the HMA. ===
;=== ===
;=== Only call if the HMA has been successfully allocated. ===
;==========================================================================
global _HMAfree
_HMAfree:
SaveRegs
mov ah, 04h
call far [dword XMSDriver]
mov ah, 02h
call far [dword XMSDriver]
RestoreRegs
ret
%endif
;*********************** routines for the UMB's ****************************
%ifdef INCLUDEUMB
;==========================================================================
;=== UMBalloc ===
;==========================================================================
;=== unsigned int UMBalloc(void); ===
;=== ===
;=== Allocates the largest UMB that can be allocated and returns ===
;=== it's segment or 0 if error. ===
;=== ===
;=== Remark: UMB's work with 16 byte blocks. ===
;==========================================================================
global _UMBalloc
_UMBalloc:
SaveRegs
mov ah, 10h
mov dx, 0FFFFh
call far [dword XMSDriver] ;; Get largest free UMB size.
mov ah, 10h
call far [dword XMSDriver] ;; Allocate largest UMB.
cmp ax, 1
jne .EndOfProc
mov ax, bx
mov [UMBsize], dx
.EndOfProc:
RestoreRegs
ret
;==========================================================================
;=== GetUMBSize ===
;==========================================================================
;=== unsigned int GetUMBsize(void); ===
;=== ===
;=== Returns the size of the most recent successfully allocated UMB. ===
;==========================================================================
global _GetUMBSize
_GetUMBSize:
mov ax, [UMBsize]
ret
;==========================================================================
;=== UMBfree ===
;==========================================================================
;=== int UMBfree (unsigned int segment); ===
;=== ===
;=== Releases an UMB (returns 1 on success, 0 on error). ===
;==========================================================================
global _UMBfree
_UMBfree:
mov bx, sp
SaveRegs
mov ah, 11h
mov dx, [ss:bx+02h]
call far [dword XMSDriver]
RestoreRegs
ret
%endif