	opt	l-,o1+,o2+,w-,p+
	
	incdir	:include/
	include	exec/exec_lib.i
	include	exec/nodes.i
	include	exec/execbase.i
	include	exec/io.i
	include	exec/memory.i

MsgDf0	equ	$24
Timer	equ	$86
ProtFlag	equ	$40
IO_SECS	equ	$20
IO_MICROS	equ	$24

	move.l	4,a0
	lea	_LVOSendIO(a0),a0
	add.l	#2,a0
	move.l	(a0),d0		; Routine della SendIO
	move.l	d0,a0
	cmp.l	#$F00000,d0
	bge	Start
	move.l	d0,d5
	add.l	#Routine-20,a0
	move.l	(a0),d0		; Recupero Old Pointer
	move.w	#_LVOSendIO,a0
	move.l	4,a1
	CALLEXEC	SetFunction	; Riporto tutto come prima
	move.l	d5,a1
	move.l	#Routine,d0
	CALLEXEC	FreeMem		; Libero NewPointer
	rts
Start	move.l	4,a0
	move.l	DeviceList(a0),a0
	bsr	CercaNodo
	tst.l	d0
	beq	errore
	CALLEXEC	Forbid
	move.l	MsgDf0(a0),a0
	move.l	Timer(a0),a0
Inizio	tst.l	LN_PRED(a0)
	beq	CercaTask
	move.l	LN_PRED(a0),a0
	bra	Inizio
CercaTask	bsr	CercaNodo
	tst.l	d0
	beq	errore
	lea	Df0Port(pc),a4
GliAltri	move.l	a0,(a4)+
	tst.l	LN_SUCC(a0)
	beq	check
	move.l	LN_SUCC(a0),a0
	bsr	CercaNodo
	tst.l	d0
	beq	check
	bra	GliAltri		; trovo Message degli Altri
	CALLEXEC	Permit
check	move.l	#Routine,d0
	move.l	#MEMF_FAST,d1
	CALLEXEC	AllocMem
	tst.l	d0
	bne	Prosegui
	move.l	#Routine,d0
	move.l	#MEMF_CHIP,d1
	CALLEXEC	AllocMem
	tst.l	d0
	beq	errore
Prosegui	move.l	4,a1
	lea	_LVOSendIO(a1),a2
	add.l	#2,a2
	lea	OldSendIO(pc),a3
	move.l	(a2),(a3)
	move.l	d0,a1
	lea	NewSendIO(pc),a0
	move.l	#Routine-1,d1
Copy	move.b	(a0)+,(a1)+
	dbra	d1,Copy
	move.l	4,a1
	move.w	#_LVOSendIO,a0	; d0=New Address
	CALLEXEC	SetFunction
errore	clr.l	d0
	rts
		
NewSendIO	movem.l	d0-d7/a0-a6,-(sp)
	lea	Df0Port(pc),a0
	move.l	(a0),a0
	cmp.l	a1,a0
	beq.s	Parti
	lea	Df1Port(pc),a0
	move.l	(a0),a0
	cmp.l	a1,a0
	beq.s	Parti
	lea	Df2Port(pc),a0
	move.l	(a0),a0
	cmp.l	a1,a0
	beq.s	Parti
	lea	Df3Port(pc),a0
	move.l	(a0),a0
	cmp.l	a1,a0
	bne.s	Fine
Parti	move.l	MN_REPLYPORT(a1),a0
	move.l	ProtFlag(a0),d0
	btst	#1,d0
	bne	Fine		; Disco Inserito
	cmp.w	#9,IO_COMMAND(a1)
	bne.s	Fine	
	cmp.l	#2,IO_SECS(a1)
	bne.s	Fine
	cmp.l	#500000,IO_MICROS(a1)
	bne.s	Fine
	clr.l	IO_SECS(a1)
	move.l	#700000,IO_MICROS(a1)	; da 2.5 secs a 0.7 secs
Fine	movem.l	(sp)+,d0-d7/a0-a6
	dc.w	$4EF9
OldSendIO	ds.l	1
Df0Port	dc.l	0
Df1Port	dc.l	0
Df2Port	dc.l	0
Df3Port	dc.l	0
Routine	equ	*-NewSendIO

CercaNodo	move.l	LN_NAME(a0),a1
	lea	DiskName(pc),a2
	move.l	#DiskLung-1,d0
Loop	cmp.b	(a2)+,(a1)+
	dbne	d0,Loop
	cmp	#-1,d0
	beq	trovato
NextOne	tst.l	LN_SUCC(a0)
	beq	fine
	move.l	LN_SUCC(a0),a0
	bra	CercaNodo
fine	move.l	#0,d0
	rts			; Device non presente
trovato	move.l	a0,d0
	rts

DiskName	dc.b	'trackdisk.device',0
DiskLung	equ	*-DiskName


	
		
