

; This source belongs to LSD so don't change it , except for the 
; incbin statement, Cath of LSD 0257 270441



	; This compiles under devpac 3.00
	; needs a system.gs file or your own includes

	opt O+

	section	code,code

Write	macro
	movem.l d0-d7/a0-a6,-(sp)
	move.l  #\1,d2
	move.l  #l_\1,d3                 ; length of message
	jsr     _Write
	movem.l (sp)+,d0-d7/a0-a6
	endm

length	macro
l_\1	equ     *-\1
	even
	endm

ptr	macro
	dc.l   \1
	dc.w   l_\1
	endm

	movem.l	a0/d0,-(sp)
	lea	DOSName(pc),a1
	CALLEXEC OldOpenLibrary			; Open dos.library
	move.l	d0,_DosBase
	move.l	d0,a5

	jsr	_LVOOutput(a5)
	move.l	d0,Output			; store output handle
	jsr	_LVOInput(a5)
	move.l	d0,Input			; store input handle	
	movem.l	(sp)+,d0/a0

	Write	Copyright

	sub.l	a1,a1
	CALLEXEC FindTask			; Find this task

	lea	DiskRply,a1
	move.l	d0,MP_SIGTASK(a1)		; Task to signal
	CALLEXEC AddPort			; Add in our MsgPort 

	lea	DiskIO,a1			; IOSTDReq
	moveq	#0,d0				; Drive
	moveq	#0,d1				; Flags
	lea	TdName,a0			; trackdisk.device name
	CALLEXEC OpenDevice			; Open it

	lea	DiskRply,a2			; Reply port
	move.l	a2,MN_REPLYPORT(a1)		; Store it in Reply Port

	Write	Working

	lea	DiskIO,a1			; IOSTDReq
	move.l	IO_ACTUAL(a1),d7
	move.w	#TD_MOTOR,IO_COMMAND(a1)	; MOTOR On/Off command
	move.l	#1,IO_LENGTH(a1)		; Turn motor on
	CALLEXEC DoIO				; Do it

	ifne	0
	lea	DiskIO,a1			; IOSTDReq
	move.w	#TD_CHANGESTATE,IO_COMMAND(a1)	; Get State
	CALLEXEC DoIO				; Do it
	move.l	IO_ACTUAL(a1),d0		; Check if write protected
	endc


	move.l	Buffer,a0			; ptr to buffer
	move.w	(a0)+,_NumberOfBlocks		; Number of blocks in buffer

	clr.l	d1				; Clear out any d1
	move.w	_NumberOfBlocks,d1		; Get number of blocks
	lsl.l	#1,d1				; Make for words
	add.l	d1,a0				; Point to data offset

	move.l	a0,Data				; Store ptr to data

	move.w	_NumberOfBlocks,d7		; Number of Blocks
	subq.w	#1,d7
LoopWrite

	move.l	Buffer,a0
	move.l	Data,a2

	addq.l	#2,a0				; Skip NumberBlocks
	add.l	Count,a0
	move.l	Count,d0
	lsr.l	#1,d0
	mulu	#512,d0
	add.l	d0,a2

	move.w	(a0),d0				; get block number
	mulu	#512,d0				; convert it to offset

	addq.l	#2,Count
	
	lea	DiskIO,a1			; IOSTDReq
	move.w	#CMD_WRITE,IO_COMMAND(a1)	; write command
	move.l	#512,IO_LENGTH(a1)		; length to write
	move.l	a2,IO_DATA(a1)          	; where to read data from
	move.l	d0,IO_OFFSET(a1)		; offset on disk to write to
	CALLEXEC DoIO


	dbra	d7,LoopWrite

	move.w	#CMD_UPDATE,IO_COMMAND(a1)
	CALLEXEC DoIO

	Write	Finished

CouldntLoad1
	lea	DiskIO,a1			; IOSTDReq
	move.w	#TD_MOTOR,IO_COMMAND(a1)	; Motor On/Off command
	move.l	#0,IO_LENGTH(a1)		; Motor Off
	CALLEXEC DoIO				; Do it

	lea	DiskIO,a1			; IOSTDReq
	CALLEXEC CloseDevice			; Close device
	lea	DiskRply,a1			; Reply MsgPort
	CALLEXEC RemPort			; Remove Port
Exit
	move.l	_DosBase,a1
	CALLEXEC CloseLibrary			; close dos.library
	clr.l	d0				; No error
	rts					; Quit to dos

_Write
	move.l	_DosBase,a5
	move.l  Output,d1
	jsr	_LVOWrite(a5)
	rts

	cnop	0,4
DiskRply	dcb.b	88
	cnop	0,4
DiskIO		dcb.b	88

_DosBase	dc.l	0
Buffer		dc.l	DataSect
Data		dc.l	0
Count		dc.l	0
_NumberOfBlocks	dc.w	0

TdName	dc.b	'trackdisk.device',0
	even
DOSName	dc.b	'dos.library',0
	even
Output	dc.l	0				; i/o handles
Input	dc.l	0

* ----------------------- text and messages

Copyright	dc.b 'UNDSFX (C)LSD 1992/3 written by Cath, Idea by Pazza',LF
		length Copyright

Working		dc.b 'Working - please wait',LF
		length Working

Finished	dc.b 'Finished - Have an LSD day!',LF
		length Finished

VERSION		dc.b '$VER: UNDSFX Cath of LSD 1993',0

	even

DataSect

; Change this to point to your archived file

	incbin	ram:ArchivedFile

	END
