kusano 7d535a
;
kusano 7d535a
; jmemdosa.asm
kusano 7d535a
;
kusano 7d535a
; Copyright (C) 1992, Thomas G. Lane.
kusano 7d535a
; This file is part of the Independent JPEG Group's software.
kusano 7d535a
; For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
;
kusano 7d535a
; This file contains low-level interface routines to support the MS-DOS
kusano 7d535a
; backing store manager (jmemdos.c).  Routines are provided to access disk
kusano 7d535a
; files through direct DOS calls, and to access XMS and EMS drivers.
kusano 7d535a
;
kusano 7d535a
; This file should assemble with Microsoft's MASM or any compatible
kusano 7d535a
; assembler (including Borland's Turbo Assembler).  If you haven't got
kusano 7d535a
; a compatible assembler, better fall back to jmemansi.c or jmemname.c.
kusano 7d535a
;
kusano 7d535a
; To minimize dependence on the C compiler's register usage conventions,
kusano 7d535a
; we save and restore all 8086 registers, even though most compilers only
kusano 7d535a
; require SI,DI,DS to be preserved.  Also, we use only 16-bit-wide return
kusano 7d535a
; values, which everybody returns in AX.
kusano 7d535a
;
kusano 7d535a
; Based on code contributed by Ge' Weijers.
kusano 7d535a
;
kusano 7d535a
kusano 7d535a
JMEMDOSA_TXT	segment byte public 'CODE'
kusano 7d535a
kusano 7d535a
		assume	cs:JMEMDOSA_TXT
kusano 7d535a
kusano 7d535a
		public	_jdos_open
kusano 7d535a
		public	_jdos_close
kusano 7d535a
		public	_jdos_seek
kusano 7d535a
		public	_jdos_read
kusano 7d535a
		public	_jdos_write
kusano 7d535a
		public	_jxms_getdriver
kusano 7d535a
		public	_jxms_calldriver
kusano 7d535a
		public	_jems_available
kusano 7d535a
		public	_jems_calldriver
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jdos_open (short far * handle, char far * filename)
kusano 7d535a
;
kusano 7d535a
; Create and open a temporary file
kusano 7d535a
;
kusano 7d535a
_jdos_open	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	cx,0			; normal file attributes
kusano 7d535a
		lds	dx,dword ptr [bp+10]	; get filename pointer
kusano 7d535a
		mov	ah,3ch			; create file
kusano 7d535a
		int	21h
kusano 7d535a
		jc	open_err		; if failed, return error code
kusano 7d535a
		lds	bx,dword ptr [bp+6]	; get handle pointer
kusano 7d535a
		mov	word ptr [bx],ax	; save the handle
kusano 7d535a
		xor	ax,ax			; return zero for OK
kusano 7d535a
open_err:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jdos_open	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jdos_close (short handle)
kusano 7d535a
;
kusano 7d535a
; Close the file handle
kusano 7d535a
;
kusano 7d535a
_jdos_close	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	bx,word ptr [bp+6]	; file handle
kusano 7d535a
		mov	ah,3eh			; close file
kusano 7d535a
		int	21h
kusano 7d535a
		jc	close_err		; if failed, return error code
kusano 7d535a
		xor	ax,ax			; return zero for OK
kusano 7d535a
close_err:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jdos_close	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jdos_seek (short handle, long offset)
kusano 7d535a
;
kusano 7d535a
; Set file position
kusano 7d535a
;
kusano 7d535a
_jdos_seek	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	bx,word ptr [bp+6]	; file handle
kusano 7d535a
		mov	dx,word ptr [bp+8]	; LS offset
kusano 7d535a
		mov	cx,word ptr [bp+10]	; MS offset
kusano 7d535a
		mov	ax,4200h		; absolute seek
kusano 7d535a
		int	21h
kusano 7d535a
		jc	seek_err		; if failed, return error code
kusano 7d535a
		xor	ax,ax			; return zero for OK
kusano 7d535a
seek_err:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jdos_seek	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jdos_read (short handle, void far * buffer, unsigned short count)
kusano 7d535a
;
kusano 7d535a
; Read from file
kusano 7d535a
;
kusano 7d535a
_jdos_read	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	bx,word ptr [bp+6]	; file handle
kusano 7d535a
		lds	dx,dword ptr [bp+8]	; buffer address
kusano 7d535a
		mov	cx,word ptr [bp+12]	; number of bytes
kusano 7d535a
		mov	ah,3fh			; read file
kusano 7d535a
		int	21h
kusano 7d535a
		jc	read_err		; if failed, return error code
kusano 7d535a
		cmp	ax,word ptr [bp+12]	; make sure all bytes were read
kusano 7d535a
		je	read_ok
kusano 7d535a
		mov	ax,1			; else return 1 for not OK
kusano 7d535a
		jmp	short read_err
kusano 7d535a
read_ok:	xor	ax,ax			; return zero for OK
kusano 7d535a
read_err:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jdos_read	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jdos_write (short handle, void far * buffer, unsigned short count)
kusano 7d535a
;
kusano 7d535a
; Write to file
kusano 7d535a
;
kusano 7d535a
_jdos_write	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	bx,word ptr [bp+6]	; file handle
kusano 7d535a
		lds	dx,dword ptr [bp+8]	; buffer address
kusano 7d535a
		mov	cx,word ptr [bp+12]	; number of bytes
kusano 7d535a
		mov	ah,40h			; write file
kusano 7d535a
		int	21h
kusano 7d535a
		jc	write_err		; if failed, return error code
kusano 7d535a
		cmp	ax,word ptr [bp+12]	; make sure all bytes written
kusano 7d535a
		je	write_ok
kusano 7d535a
		mov	ax,1			; else return 1 for not OK
kusano 7d535a
		jmp	short write_err
kusano 7d535a
write_ok:	xor	ax,ax			; return zero for OK
kusano 7d535a
write_err:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jdos_write	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; void far jxms_getdriver (XMSDRIVER far *)
kusano 7d535a
;
kusano 7d535a
; Get the address of the XMS driver, or NULL if not available
kusano 7d535a
;
kusano 7d535a
_jxms_getdriver	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov 	ax,4300h		; call multiplex interrupt with
kusano 7d535a
		int	2fh			; a magic cookie, hex 4300
kusano 7d535a
		cmp 	al,80h			; AL should contain hex 80
kusano 7d535a
		je	xmsavail
kusano 7d535a
		xor 	dx,dx			; no XMS driver available
kusano 7d535a
		xor 	ax,ax			; return a nil pointer
kusano 7d535a
		jmp	short xmsavail_done
kusano 7d535a
xmsavail:	mov 	ax,4310h		; fetch driver address with
kusano 7d535a
		int	2fh			; another magic cookie
kusano 7d535a
		mov 	dx,es			; copy address to dx:ax
kusano 7d535a
		mov 	ax,bx
kusano 7d535a
xmsavail_done:	les 	bx,dword ptr [bp+6]	; get pointer to return value
kusano 7d535a
		mov	word ptr es:[bx],ax
kusano 7d535a
		mov	word ptr es:[bx+2],dx
kusano 7d535a
		pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop	bp
kusano 7d535a
		ret
kusano 7d535a
_jxms_getdriver	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; void far jxms_calldriver (XMSDRIVER, XMScontext far *)
kusano 7d535a
;
kusano 7d535a
; The XMScontext structure contains values for the AX,DX,BX,SI,DS registers.
kusano 7d535a
; These are loaded, the XMS call is performed, and the new values of the
kusano 7d535a
; AX,DX,BX registers are written back to the context structure.
kusano 7d535a
;
kusano 7d535a
_jxms_calldriver 	proc	far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		les 	bx,dword ptr [bp+10]	; get XMScontext pointer
kusano 7d535a
		mov 	ax,word ptr es:[bx]	; load registers
kusano 7d535a
		mov 	dx,word ptr es:[bx+2]
kusano 7d535a
		mov 	si,word ptr es:[bx+6]
kusano 7d535a
		mov 	ds,word ptr es:[bx+8]
kusano 7d535a
		mov 	bx,word ptr es:[bx+4]
kusano 7d535a
		call	dword ptr [bp+6]	; call the driver
kusano 7d535a
		mov	cx,bx			; save returned BX for a sec
kusano 7d535a
		les 	bx,dword ptr [bp+10]	; get XMScontext pointer
kusano 7d535a
		mov 	word ptr es:[bx],ax	; put back ax,dx,bx
kusano 7d535a
		mov 	word ptr es:[bx+2],dx
kusano 7d535a
		mov 	word ptr es:[bx+4],cx
kusano 7d535a
		pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jxms_calldriver 	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; short far jems_available (void)
kusano 7d535a
;
kusano 7d535a
; Have we got an EMS driver? (this comes straight from the EMS 4.0 specs)
kusano 7d535a
;
kusano 7d535a
_jems_available	proc	far
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		mov	ax,3567h		; get interrupt vector 67h
kusano 7d535a
		int	21h
kusano 7d535a
		push	cs
kusano 7d535a
		pop	ds
kusano 7d535a
		mov	di,000ah		; check offs 10 in returned seg
kusano 7d535a
		lea	si,ASCII_device_name	; against literal string
kusano 7d535a
		mov	cx,8
kusano 7d535a
		cld
kusano 7d535a
		repe cmpsb
kusano 7d535a
		jne	no_ems
kusano 7d535a
		mov	ax,1			; match, it's there
kusano 7d535a
		jmp	short avail_done
kusano 7d535a
no_ems:		xor	ax,ax			; it's not there
kusano 7d535a
avail_done:	pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		ret
kusano 7d535a
kusano 7d535a
ASCII_device_name	db	"EMMXXXX0"
kusano 7d535a
kusano 7d535a
_jems_available	endp
kusano 7d535a
kusano 7d535a
kusano 7d535a
;
kusano 7d535a
; void far jems_calldriver (EMScontext far *)
kusano 7d535a
;
kusano 7d535a
; The EMScontext structure contains values for the AX,DX,BX,SI,DS registers.
kusano 7d535a
; These are loaded, the EMS trap is performed, and the new values of the
kusano 7d535a
; AX,DX,BX registers are written back to the context structure.
kusano 7d535a
;
kusano 7d535a
_jems_calldriver	proc far
kusano 7d535a
		push	bp			; linkage
kusano 7d535a
		mov 	bp,sp
kusano 7d535a
		push	si			; save all registers for safety
kusano 7d535a
		push	di
kusano 7d535a
		push	bx
kusano 7d535a
		push	cx
kusano 7d535a
		push	dx
kusano 7d535a
		push	es
kusano 7d535a
		push	ds
kusano 7d535a
		les 	bx,dword ptr [bp+6]	; get EMScontext pointer
kusano 7d535a
		mov 	ax,word ptr es:[bx]	; load registers
kusano 7d535a
		mov 	dx,word ptr es:[bx+2]
kusano 7d535a
		mov 	si,word ptr es:[bx+6]
kusano 7d535a
		mov 	ds,word ptr es:[bx+8]
kusano 7d535a
		mov 	bx,word ptr es:[bx+4]
kusano 7d535a
		int	67h			; call the EMS driver
kusano 7d535a
		mov	cx,bx			; save returned BX for a sec
kusano 7d535a
		les 	bx,dword ptr [bp+6]	; get EMScontext pointer
kusano 7d535a
		mov 	word ptr es:[bx],ax	; put back ax,dx,bx
kusano 7d535a
		mov 	word ptr es:[bx+2],dx
kusano 7d535a
		mov 	word ptr es:[bx+4],cx
kusano 7d535a
		pop	ds			; restore registers and exit
kusano 7d535a
		pop	es
kusano 7d535a
		pop	dx
kusano 7d535a
		pop	cx
kusano 7d535a
		pop	bx
kusano 7d535a
		pop	di
kusano 7d535a
		pop	si
kusano 7d535a
		pop 	bp
kusano 7d535a
		ret
kusano 7d535a
_jems_calldriver	endp
kusano 7d535a
kusano 7d535a
JMEMDOSA_TXT	ends
kusano 7d535a
kusano 7d535a
		end