home *** CD-ROM | disk | FTP | other *** search
- /* ReSizeable RAMDisk - disk I/O
- ** Copyright (c) 1992 Marko Kohtala
- */
-
- #include "srdisk.h"
- #include <assert.h>
- #include <dos.h>
- #include <string.h>
-
- /*
- ** Read/Write sector from/to disk
- **
- ** Return 0 for failure, transferred sector count otherwise
- */
-
- static int xfer_sector(char rw, int count, dword start, void far *buffer)
- {
- struct config_s far *subconf = conf;
- int sectors = count;
-
- while(count) {
- asm {
- mov ax,word ptr start
- mov dx,word ptr start+2
- mov cx,count
- mov bh,rw
- les di,buffer
- push ds
- lds si,subconf
- call dword ptr [si+2] /* !!!! Configuration format dependent */
- pop ds
- jc fail
- sub count,ax
- jbe done
- }
- start -= subconf->sectors;
- if ( !(subconf = conf_ptr(subconf->next)) )
- return 0;
- }
- done:
- return sectors;
- fail:
- fatal("Cannot access disk");
- return 0;
- }
-
- /*
- ** Read sector from disk
- **
- ** Return 0 for failure, transferred sector count otherwise
- */
-
- int read_sector(int count, dword start, void *buffer)
- {
- return xfer_sector(0, count, start, buffer);
- }
-
- /*
- ** Write sector to disk
- **
- ** Return 0 for failure, transferred sector count otherwise
- */
-
- int write_sector(int count, dword start, void *buffer)
- {
- return xfer_sector(1, count, start, buffer);
- }
-
-
- /*
- ** Allocate memory on a part of the disk
- */
-
- dword disk_alloc(struct config_s far *conf, dword size)
- {
- struct dev_hdr _seg *dev = (struct dev_hdr _seg *)FP_SEG(conf);
- byte far *alloc = MK_FP(dev, conf->malloc_off);
-
- if (!(conf->flags & C_NOALLOC))
- return ((dword (far *)(dword))alloc)(size);
-
- if (_fstrncmp("XMS ", dev->u.s.memory, 4) == 0) {
- if (size >= 0x10000L) /* Can not allocate over 0xFFFF K */
- fail:
- return conf->size;
-
- #define XMS_handle 0 /* Dependant on the XMS_alloc structure format */
- #define XMS_entry 2
-
- if (*(word far *)(alloc+XMS_handle) != 0) {
- /* Has already a handle - reallocate to new size */
- asm {
- les si,alloc
- mov dx,es:[si+XMS_handle]
- mov bx,word ptr size
- mov ah,0xF /* Reallocate */
- call dword ptr es:[si+XMS_entry]
- or ax,ax
- jnz realloc_ok
-
- /* Failed to allocate, check if there is memory anyway */
- /* This kludge hopefully fixes some problems with DesqView */
- mov ah,8 /* Query free extended memory */
- call dword ptr es:[si+XMS_entry]
- push ax
- mov dx,es:[si+XMS_handle]
- mov ah,0xE /* Get handle information */
- call dword ptr es:[si+XMS_entry]
- or ax,ax
- pop ax
- jz fail
-
- add ax,dx /* AX = free memory + current block */
- cmp ax,word ptr size
- jb fail
-
- mov dx,es:[si+XMS_handle]
- mov ah,0xA /* Free extended memory block */
- call dword ptr es:[si+XMS_entry]
- or ax,ax
- jz fail
- jmp alloc_handle
- }
- realloc_ok:;
- }
- else {
- /* Handle 0 is no handle - must allocate */
- alloc_handle:
- asm {
- les si,alloc
- mov dx,word ptr size
- mov ah,0x9 /* Allocate */
- call dword ptr es:[si+XMS_entry]
- or ax,ax
- jz alloc_fail
- mov es:[si+XMS_handle],dx
- jmp alloc_ok
- }
- alloc_fail:
- error("Can not allocate XMS memory");
- return 0;
- alloc_ok:;
- }
- }
- else
- fatal("Don't know how to allocate memory");
- return size;
- }
-
-
-