forked from Tronix286/DOSMID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxms.c
172 lines (156 loc) · 6.1 KB
/
xms.c
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
/*
* XMS driver for DOSMid
*
* Copyright (C) 2014-2018 Mateusz Viste
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "defines.h"
#include "xms.h" /* include self for control */
#include <dos.h> /* REGS */
#ifndef MK_FP
#define MK_FP(S,O) (void __far *)(((unsigned long int)(S) << 16) + (unsigned long int)(O))
#endif
/* a function pointer to save the XMS driver address */
void (__far *xmsdrv)(void);
/* descriptor for XMS moves */
struct xms_move {
long count; /* number of bytes to move */
unsigned short srchandle; /* source handle (0 for real memory) */
long srcoffset; /* source offset (or far pointer) */
unsigned short dsthandle; /* destination handle (0 for real memory) */
long dstoffset; /* destination offset (or far pointer) */
};
/* used by xms_push() and xms_pull() to call the xms driver for data copy.
returns 0 on success, non-zero otherwise */
static int xms_move(struct xms_move far *xmove) {
unsigned short resax = 0;
unsigned char resbl = 0;
__asm {
push bp ; save BP
push ds ; save DS
push si ; save SI
mov ah, 0x0b ; xms function is 0x0b
lds si, [xmove] ;
call dword ptr [xmsdrv] ; call the XMS driver
pop si ; restore original SI
pop ds ; restore original DS
pop bp ; restore original BP
mov resax, ax ; save exit status
mov resbl, bl ; save result
}
/* check result */
if (resax == 1) return(0); /* AX = 0001h if the move is successful, 0000h otherwise */
return(resbl);
}
/* returns the largest available block of free XMS memory */
static unsigned int xms_memfree(void) {
unsigned short res = 0;
__asm {
mov ah, 0x08
call dword ptr [xmsdrv] ; call the XMS driver
mov res, ax ; AX = Size of the largest free block in K-bytes
}
return(res);
}
/* checks if a XMS driver is installed, inits it and allocates a memory block of memsize K-bytes.
* if memsize is 0, then the maximum possible block will be allocated.
* returns the amount of allocated memory (in K-bytes) on success, 0 otherwise. */
unsigned int xms_init(struct xms *xms, unsigned short int memsize) {
union REGS regs;
struct SREGS sregs;
unsigned short axres = 0, dxres = 0;
unsigned short freemem;
/* check that an XMS driver is present */
regs.x.ax = 0x4300;
int86(0x2F, ®s, ®s);
if (regs.h.al != 0x80) return(0);
/* fetch the driver's API address */
regs.x.ax = 0x4310;
int86x(0x2F, ®s, ®s, &sregs);
xmsdrv = (void (__far *)())MK_FP(sregs.es, regs.x.bx);
xms->handle = 0;
xms->memsize = 0;
/* if memsize is 0, allocate as much as we can */
freemem = xms_memfree();
if ((memsize == 0) || (freemem < memsize)) memsize = freemem;
if (memsize == 0) return(0);
/* allocate the memory block */
__asm {
mov ah, 0x09
mov dx, memsize
call dword ptr [xmsdrv] ; call the XMS driver
mov dxres, dx
mov axres, ax
}
xms->handle = dxres;
if (axres != 1) return(0); /* if allocation failed... */
xms->memsize = memsize;
xms->memsize <<= 10; /* memsize is in kbytes, but we want to have it in bytes now */
return(memsize);
}
/* free XMS memory */
void xms_close(struct xms *xms) {
unsigned short handle;
handle = xms->handle;
__asm {
mov ah, 0x0a
push dx ; save DX
mov dx, handle ; DX = Handle to the block to be freed
call dword ptr [xmsdrv] ; call the XMS driver
pop dx; ; restore original DX
}
}
/* copies a chunk of memory from conventional memory into the XMS block.
returns 0 on sucess, non-zero otherwise. */
int xms_push(struct xms *xms, void far *src, unsigned short int len, long int xmsoffset) {
int res;
struct xms_move xmove, far *ptr;
ptr = &xmove;
/* prepare the xms move struct */
xmove.count = len;
xmove.srchandle = 0; /* handle == 0 means 'see in conv. memory' */
xmove.srcoffset = (unsigned long)src;
xmove.dsthandle = xms->handle;
xmove.dstoffset = xmsoffset;
/* call the xms api */
res = xms_move(ptr);
return(res);
}
/* copies a chunk of memory from the XMS block into conventional memory.
returns 0 on success, non-zero otherwise. */
int xms_pull(struct xms *xms, long int xmsoffset, void far *dst, unsigned short int len) {
int res;
struct xms_move xmove, far *ptr;
ptr = &xmove;
/* prepare the xms move struct */
xmove.count = len;
xmove.srchandle = xms->handle;
xmove.srcoffset = xmsoffset;
xmove.dsthandle = 0; /* handle == 0 means 'see in conv. memory' */
xmove.dstoffset = (unsigned long)dst;
/* call the xms api */
res = xms_move(ptr);
return(res);
}