;--------------------------------------------------------------------
; Amiga-Atari Net 1.0 7-23-94
;--------------------------------------------------------------------

; This program works in conjunction with "AtariNet.s".

; This program is strictly CLI/Shell-only.  Do not run from Workbench!

; Don't use this program unless you know what you're doing!
; *** I will not be responsible if you damage your computer! ***

; Using this program incorrectly could cause damage to your parallel port,
; the Atari PIA, or both.  Exercise extreme caution!

		include	exec/exec.i

FUNCCNT		SET	LIB_USERDEF

FUNCDEF		MACRO
_LVO\1		EQU	FUNCCNT
FUNCCNT		SET	FUNCCNT-6
		ENDM

		include	exec/exec_lib.i
		include	dos/dos.i
		include	dos/dos_lib.i
		include	hardware/custom.i

custom		EQU	$DFF000

CALLEXEC	MACRO
		movea.l	4,a6
		jsr	_LVO\1(a6)
		ENDM

CALLDOS		MACRO
		movea.l	_DOSBase,a6
		jsr	_LVO\1(a6)
		ENDM

Start		lea	DOSName(pc),a1
		moveq	#0,d0
		CALLEXEC OpenLibrary
		move.l	d0,_DOSBase

		move.l	#MyFileName,d1
		move.l	#MODE_NEWFILE,d2
		CALLDOS	Open
		move.l	d0,MyFile
		bne.s	.ok
		rts

.ok		move.b	#0,$BFE301		; set all in

.1		move.b	$BFE101,d0		; get data
		andi.b	#$5F,d0
		cmpi.b	#$4F,d0			; D4 = 0 ?
		bne.s	.1

		move.b	#$A0,$BFE301		; use bits 5,7 out
		bra.s	.next2

.next		move.b	$BFE101,d0		; get data
		andi.b	#$5F,d0
		cmpi.b	#$4F,d0			; D4 = 0 ?
		bne.s	.next

.next2		move.b	#$80,$BFE101		; D5 = 0

		lea	Buf1(pc),a0
		moveq	#0,d1			; clear count
		moveq	#0,d2			; clear chksum

.loop		bsr	wait1			; wait for D4 = 1
		move.b	#$A0,$BFE101		; reply D5 = 1
		moveq	#15,d3
		and.b	d0,d3			; save data

.res1		move.b	$BFE101,d0		; wait for $4F  (D4 = 0)
		andi.b	#$5F,d0
		cmpi.b	#$4F,d0
		bne.s	.res1
		move.b	#$80,$BFE101		; reply D5 = 0

		bsr	wait1			; wait for D4 = 1
		move.b	#$A0,$BFE101		; reply D5 = 1
		btst	#6,d0			; stop ?
		beq.s	.2

.res2		move.b	$BFE101,d4		; wait for $4F  (D4 = 0)
		andi.b	#$5F,d4
		cmpi.b	#$4F,d4
		bne.s	.res2
		move.b	#$80,$BFE101		; reply D5 = 0

		lsl.b	#4,d0			; shift data left
		or.b	d3,d0			; add low nib
		move.b	d0,(a0)+
		addq.w	#1,d1			; count++
		add.b	d0,d2			; add data to chksum
		bcc.s	.loop
		addq.b	#1,d2
		bra.s	.loop

.2		lsl.b	#4,d0
		or.b	d3,d0			; get chksum sent
		cmp.b	d2,d0
		beq.s	.3

		bra	exit1			; chksum error!

.3		move.b	$BFE101,d0		; wait for all bits 1
		andi.b	#$5F,d0
		cmpi.b	#$5F,d0
		bne.s	.3
		move.b	#$80,$BFE101		; reply D5 = 0

.4		move.b	$BFE101,d0		; get data
		andi.b	#$5F,d0
		beq.s	.5			; all bits 0
		cmpi.b	#$1F,d0			; continue
		bne.s	.4

		move.b	#$A0,$BFE101		; D5 = 1

		move.l	d1,d3			; length
		move.l	MyFile(pc),d1		; file
		move.l	#Buf1,d2		; buffer
		CALLDOS	Write
		bra	.next

.5		move.b	#$A0,$BFE101		; D5 = 1

		move.l	d1,d3			; length
		move.l	MyFile(pc),d1		; file
		move.l	#Buf1,d2		; buffer
		CALLDOS	Write

exit1		move.b	#0,$BFE301		; set all bits in

		move.l	MyFile(pc),d1
		CALLDOS	Close

		movea.l	_DOSBase,a1
		CALLEXEC CloseLibrary
		rts

wait1		move.b	$BFE101,d0
		btst	#4,d0
		beq.s	wait1
		rts

		cnop	0,4

Buf1		ds.b	5120
MyFile		dc.l	0
_DOSBase	dc.l	0
DOSName		dc.b	'dos.library',0
MyFileName	dc.b	'ram:DiskImage',0

