-
Notifications
You must be signed in to change notification settings - Fork 1
/
srdems3.inc
executable file
·404 lines (350 loc) · 12.1 KB
/
srdems3.inc
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
; SRDISK - EMS memory device driver specific code
; Copyright (C) 1993-1996, 2005 Marko Kohtala
; Released under GNU GPL, read the file 'COPYING' for more information
; EMS 3.2
MEMSTR equ 'EMS3' ; Define 4 char memory type string
MAX_SIZE equ 7FFFh ; Maximum possible size
ALLOCBLOCK equ 16 ; Memory allocation block size
include EMS.INC
;**************************************************************************
;
; I/O ROUTINE TO THE RAM DISK
;
; This routine will read a group of sectors inside this part of
; the ram disk. If starting sector is not on this part of the disk,
; return without error with 0 sectors transferred. If not all sectors
; are on this part of the disk, transfer as many as can and report the
; number of sectors transferred.
;
; On entry
; DS:SI - local configuration structure
; dIO_bytes word ptr [bp-6] - bytes to move
; dIO_byteoffset dword ptr [bp-4] - byte offset of start
; dIO_buffer dword ptr [bp+6] - buffer
; dIO_sector dword ptr [bp+10] - sector
; dIO_count word ptr [bp+14] - count
; dIO_RW byte ptr [bp+16] - RW (0 = read, nonzero write)
;
; Preserve
; ds, si, bp and stack contents
;
; Return
; - carry clear if success
; - carry set on error
;
;**************************************************************************
mac_disk_IO macro
local set_segments, next_page, write, map_page
local move_sector, fail, done, finnish
push ds
push si
mov dx,EMS_alloc.EMS_handle
mov ah,EMS_SAVE_PMAP
int 67h
or ah,ah
jnz fail
mov ax,word ptr dIO_buffer+2
mov si,word ptr dIO_buffer
mov bx,EMS_alloc.EMS_frame
cmp dIO_RW,0
jnz set_segments
xchg ax,bx
mov di,si
set_segments: mov ds,ax
mov es,bx
mov bx,-1
assume ds:nothing
; es:di - target address on disk or memory
; ds:si - source address on memory or disk
; bx - last sectors page
next_page: mov dx,word ptr dIO_byteoffset+2
mov ax,word ptr dIO_byteoffset
mov cx,16*1024
div cx ; ax = page, dx = offset
cmp ax,bx
jz move_sector
mov bx,ax ; Logical page
cmp dIO_RW,0
jnz write
mov si,dx ; read, si = offset into page
jmp map_page
write: mov di,dx ; write, di = offset into page
map_page: xor al,al ; Phys page 0
mov dx,EMS_alloc.EMS_handle
mov ah,EMS_MAP_HANDLE_PAGES
int 67h
or ah,ah
jnz fail
move_sector: mov ax,conf.c_BPB_bps
mov cx,ax
shr cx,1
rep movsw
sub dIO_bytes,ax
jbe done
add word ptr dIO_byteoffset,ax
adc word ptr dIO_byteoffset+2,0
jmp next_page
fail: stc
jmp finnish
done: clc
finnish:
pushf
mov dx,EMS_alloc.EMS_handle
mov ah,EMS_RESTORE_PMAP
int 67h
; cmp ah,1 ; Set carry if nonzero
; cmc
popf
pop si
pop ds
ret
assume ds:d_seg
endm
;**************************************************************************
;
; QUERY FREE MEMORY
;
; This routine is only used if CAPABLE has C_NOALLOC bit clear.
;
; Preserve
; ds, si, di, bp and stack contents
;
; Return
; - dx:ax set to maximum safe size to reallocate to
; - cx:bx set to maximum size to reallocate to (may not be valid)
;
;**************************************************************************
mac_freemem macro
assume ds:nothing
mov ah,EMS_FREE_PAGE_COUNT
int 67h ; bx = unallocated pages
mov ax,16 ; of 16K each
mul bx ; dx:ax = total free K
add ax,word ptr conf.size
adc dx,word ptr conf.size+2
mov bx,ax
mov cx,dx
ret
assume ds:d_seg
endm
;**************************************************************************
;
; EXTERNAL MEMORY ALLOCATION ROUTINE
;
; This routine is only used if CAPABLE has C_NOALLOC bit clear.
;
; Allocate requested amount of memory. If memory is available in chunks,
; the amount can be rounded up. If not enough memory available, allocate
; as much as possible or just report the amount that was previously
; allocated. It is expected that at least as much memory can be allocated
; as there previously was. Reallocation should not destroy memory
; contents - it is essential to be able to resize a disk without loosing
; the contents.
;
; On entry
; DWORD [sp+4] - Kbytes to allocate
;
; Preserve
; es, ds
; si, di
;
; Return dx:ax = Kbytes allocated
;
;**************************************************************************
mac_malloc macro
arg kbytes:dword
local no_roundup, realloc, ok, fail
assume ds:nothing
mov ax,word ptr kbytes ; New disk size
mov dx,word ptr kbytes+2
cmp dx,15*1024
jae fail
mov cx,16*1024
div cx
or dx,dx
jz no_roundup
inc ax
no_roundup: mov bx,ax
mov dx,EMS_alloc.EMS_handle ; Handle to the memory
or ax,ax
jz free
cmp dx,-1 ; Is the handle valid?
jne realloc
mov ah,EMS_ALLOCATE
int 67h
or ah,ah
jnz fail
mov EMS_alloc.EMS_handle,dx
jmp ok
free:
mov ah,EMS_DEALLOCATE
int 67h
or ah,ah
jnz fail
mov EMS_alloc.EMS_handle,dx
jmp ok
realloc: ; !!!! Not a 3.2 call!
mov ah,EMS_REALLOCATE_PAGES ; Reallocate
int 67h
or ah,ah
jnz fail
ok: mov ax,word ptr kbytes ; Ok, return requested
xor dx,dx
ret
fail: mov ax,word ptr conf.c_size ; Fail, return current
xor dx,dx
ret
assume ds:d_seg
endm
;**************************************************************************
;
; Warm Boot of Machine
;
; Release used XMS memory on warm boot.
;
; I guess this may be important if some virtual machine (VM) in some
; multitasking system has installed this driver and the VM is ended.
; Without this the other VMs could loose the space reserved for RAM disk
; in this VM.
;**************************************************************************
if HOOKINT19
mac_int_19 macro
local int19_1
assume ds:nothing
push ax
push dx
push ds
pushf
mov dx,EMS_alloc.XMS_handle
cmp dx,-1
jne int19_1 ; Jump if no EMS handle
mov ah,EMS_DEALLOCATE
int 67h ; Free EMS memory
mov EMS_alloc.EMS_handle,-1
int19_1:
xor ax,ax
mov ds,ax
mov ax,old_int19_off
cli ; Disable interrupts
mov ds:[19h*4],ax ; for the time to write
mov ax,old_int19_seg ; old interrupt vector back
mov ds:[19h*4+2],ax
popf ; Enable interrupts
pop ds
pop dx
pop ax
jmp old_int19
assume ds:d_seg
endm
endif
;**************************************************************************
;
; Local data
;
; This data is used by the two above routines that are needed
; resident in any case.
;
;**************************************************************************
EMS_alloc_s struc ; Changing this structure needs changes in srdisk.exe
EMS_handle dw -1 ; EMS handle to disk memory (-1=no handle)
EMS_frame dw ? ; EMS page frame segment
EMS_alloc_s ends
mac_resident_data macro
EMS_alloc EMS_alloc_s <>
if CAPABLE and C_NOALLOC
malloc EQU offset EMS_alloc
endif
endm
;**************************************************************************
;
; Memory initialization
;
; Returns
; carry set if error
;**************************************************************************
mac_init_memory macro
local init1, init2, init3, init4, init6, init7, init_ret
push es
mov ax,3567h ; Compare device name
int 21h
mov di,0Ah
mov si,offset EMM_name
mov cx,8
cld
repz cmpsb
jne init1
mov ah,EMS_GET_STATUS ; Get status of EMM
int 67h
or ah,ah
jnz init1
mov ah,EMS_FRAME_SEGMENT ; Get page frame address
int 67h
mov EMS_alloc.EMS_frame,bx
or ah,ah
jz init2
init1:
mov dx,offset errs_noEMS ; "No expanded mem driver"
jmp init4
init2:
mov ah,EMS_GET_VERSION ; Verify EMS version
int 67h
or ah,ah
jnz init1
cmp al,40h
jae init3
mov dx,offset errs_badEMS
jmp init4
init3:
; Try out the functions that are needed
mov ah,EMS_ALLOCATE
mov bx,1
int 67h
or ah,ah
jnz init6
mov EMS_alloc.EMS_handle,dx
mov ah,EMS_DEALLOCATE
mov dx,EMS_alloc.EMS_handle
int 67h
jz init7
init6:
mov dx,offset errs_nofuncEMS
jmp init4
init7:
mov EMS_alloc.EMS_handle,-1 ; -1 handle for no handle
clc
jmp init_ret
init4:
stc
init_ret:
pop es
endm
;**************************************************************************
;
; Memory deinitialization
;
;**************************************************************************
mac_deinit_memory macro
local done
cmp EMS_alloc.EMS_handle,-1
je done
mov dx,EMS_alloc.EMS_handle
mov ah,EMS_DEALLOCATE
int 67h ; Free EMS memory
mov EMS_alloc.EMS_handle,-1
done:
endm
;**************************************************************************
;
; Initialization time data
;
;**************************************************************************
mac_init_data macro
EMM_name db 'EMMXXXX0'
errs_noEMS db 'RAMDisk: Expanded Memory Manager not present.'
db 0Dh, 0Ah, '$'
errs_badEMS db 'RAMDisk: Invalid EMS version. EMS 4.0 needed.'
db 0Dh, 0Ah, '$'
errs_nofuncEMS db 'RAMDisk: EMS driver does not support needed functions'
db ' or no memory available.'
db 0Dh, 0Ah, '$'
endm