***********************************
*=================================*
* RESERVOIR GODS LIBRARY ROUTINES *
*=================================*
*       |                |        *
*       |DISK_OS LIBRARY |        *
*       |                |        *
***********************************
* CODE: MR. PINK | (C): 19/02/96  *
***********************************
* UPDATE: 26/5/96  *
********************

**************************************************************************
	SECTION TEXT
**************************************************************************

rldk_disk_constructor
	move.l	a0,-(a7)
	bsr	rldk_d_getdrive
	lea	rldk_disk_space,a0
	move.w	d0,rldk_old_drive_num(a0)
	lea	rldk_old_path(a0),a0
	moveq	#0,d0
	bsr	rldk_d_getpath
	move.l	(a7)+,a0
	rts

;a0->filename, a1->address, d0->bytes

rldk_load_file:
	movem.l	a0-2/d1-2,-(a7)
	lea	rldk_disk_space,a2
	move.l	d0,rldk_read_amount(a2)		;store read amount
	bsr	rldk_open_file
	move.w	d0,d1				;copy+test file handle
	bmi	.error
	move.l	a1,a0
	move.l	rldk_read_amount(a2),d0		;bytes to read
	bsr	rldk_read_file
	move.w	d1,d0				;file handle
	bsr	rldk_close_file
.error	movem.l	(a7)+,a0-2/d1-2
	rts

rldk_save_file:
	movem.l	a0-2/d0-2,-(a7)
	lea	rldk_disk_space,a2
	move.l	d0,rldk_write_amount(a2)	;save bytes to write
	bsr	rldk_create_file		;create file
	move.w	d0,d1				;copy+save file handle
	bmi.s	.error				;error
	move.l	a1,a0
	move.l	rldk_write_amount(a2),d0	;bytes to write
	bsr	rldk_write_file
	move.w	d1,d0				;file handle
	bsr	rldk_close_file
.error	movem.l	(a7)+,a0-2/d0-2
	rts

;a0->file name. returns d0=length

rldk_get_file_length
	movem.l	d1-2,-(a7)
	bsr	rldk_open_file
	tst.w	d0
	bmi	.err
	move.w	d0,d1				;copy handle
	moveq	#0,d2				;offset=0
	moveq	#2,d0				;seek from end
	bsr	rldk_file_seek
	move.l	d0,-(a7)			;save file size
	move.w	d1,d0				;handle
	bsr	rldk_close_file
	move.l	(a7)+,d0			;return file size
.err	movem.l	(a7)+,d1-2
	rts

rldk_get_connected_drives:
	bsr	rldk_d_getdrive
	bsr	rldk_d_setdrive
	rts

rldk_open_file:
	movem.l	d1-2/a0-2,-(a7)
	clr.w	-(a7)
	pea	(a0)
	move.w	#$3d,-(a7)
	trap	#1
	addq.l	#8,a7
	movem.l	(a7)+,d1-2/a0-a2
	rts

rldk_create_file:
	movem.l	d1-2/a0-2,-(a7)
	clr.w	-(a7)
	pea	(a0)			; file name address
	move.w	#$3c,-(a7)
	trap	#1
	addq.l	#8,a7
	movem.l	(a7)+,d1-2/a0-a2
	rts

rldk_read_file:
	movem.l	d1-2/a0-2,-(a7)
	pea	(a0)				; disk buffer adr
	move.l	d0,-(a7)			; read amount
	move.w	d1,-(a7)			; file handle
	move.w	#$3f,-(a7)
	trap	#1
	lea	12(a7),a7
	lea	rldk_disk_space,a0
	move.l	d0,rldk_bytes_read(a0)
	movem.l	(a7)+,d1-2/a0-a2
	rts

rldk_write_file:
	movem.l	d1-2/a0-2,-(a7)
	pea	(a0)				; disk buffer adr
	move.l	d0,-(a7)			; write amount
	move.w	d1,-(a7)			; file handle
	move.w	#$40,-(a7)	
	trap	#1
	lea	12(a7),a7
	movem.l	(a7)+,d1-2/a0-a2
	rts

rldk_close_file:
	movem.l	d1-2/a0-2,-(a7)
	move.w	d0,-(a7)			;file handle
	move.w	#$3e,-(a7)
	trap	#1
	addq.l	#4,a7
	movem.l	(a7)+,d1-2/a0-a2
	rts

;seek mode: 0=from start; 1=from current; 2=from end

rldk_file_seek:
	movem.l	d1-2/a0-2,-(a7)
	move.w	d0,-(a7)			;seek mode
	move.w	d1,-(a7)			;file handle
	move.l	d2,-(a7)			;offset
	move.w	#$42,-(a7)
	trap	#1
	lea	10(a7),a7
	movem.l	(a7)+,d1-2/a0-a2
	rts

rldk_f_setdta:
	movem.l	d1-2/a0-2,-(a7)
	pea	(a0)					;dta address
	move.w	#$1a,-(a7)	
	trap	#1
	addq.l	#6,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_fs_first:
	movem.l	d1-2/a0-2,-(a7)
	move.w	d0,-(a7)				;attributes
	pea	(a0)					;file spec
	move.w	#$4e,-(a7)
	trap	#1
	addq.l	#8,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_fs_next:
	movem.l	d1-2/a0-2,-(a7)
	move.w	#$4f,-(a7)
	trap	#1
	addq.l	#2,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_d_getdrive:
	movem.l	d1-2/a0-2,-(a7)
	move.w	#$19,-(a7)
	trap	#1
	addq.l	#2,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_d_setdrive:
	movem.l	d1-2/a0-2,-(a7)
	move.w	d0,-(a7) 			;drive: a=0, b=1 etc.
	move.w	#$e,-(a7)
	trap	#1
	addq.l	#4,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_d_getpath:
	movem.l	d1-2/a0-2,-(a7)
	move.w	d0,-(a7)
	pea	(a0)				;space for path
	move.w	#$47,-(a7)
	trap	#1
	addq.l	#8,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_d_setpath:
	movem.l	d1-2/a0-2,-(a7)
	pea	(a0)				;path ptr
	move.w	#$3b,-(a7)
	trap	#1
	addq.l	#6,a7
	movem.l	(a7)+,d1-2/a0-2
	rts

rldk_disk_deconstructor
	movem.l	a0-1/d0-2,-(a7)
	lea	rldk_disk_space,a1
	move.w	rldk_old_drive_num(a1),d0
	bsr	rldk_d_setdrive
	lea	rldk_old_path(a1),a0
	bsr	rldk_d_setpath
	movem.l	(a7)+,a0-1/d0-2
	rts

**************************************************************************
	SECTION BSS
**************************************************************************
	
	even
	RSRESET

rldk_bytes_read		rs.l	1
rldk_bytes_written	rs.l	1
rldk_read_amount	rs.l	1
rldk_write_amount	rs.l	1
rldk_old_path		rs.b	512
rldk_old_drive_num	rs.w	1
rldk_file_handle	rs.w	1
rldk_disk_buffer	rs.w	1

rldk_disk_space:	ds.b	rldk_disk_buffer
			even