*	opt	x+			dump long labels for debugging
*	opt	a+			automatic PC mode to save bytes

brate	equ	9		300 baud default baud rate
xon	equ	$11		Ctrl Q
xoff	equ	$13		Ctrl S
esc	equ	27		Escape character in vt52 mode
cr	equ	13		Carriage Return
lf	equ	10		Line Feed

* Xmodem definitions...
soh	equ	$01
eot	equ	$04
ack	equ	$06
nak	equ	$15
can	equ	$18

recsize:	equ	19
atari:		equ	0
port:		equ	1

backdrop	EQU	    0

clearselected	EQU	    1
atariclosedir	EQU	    8
ataridirectory	EQU	    9
ad1	EQU	   10
aupslow	EQU	   12
aupfast	EQU	   13
adownfast	EQU	   14
adownslow	EQU	   15
ad2	EQU	   16
ad3	EQU	   17
ad4	EQU	   18
ad5	EQU	   19
ad6	EQU	   20
ad7	EQU	   21
ad8	EQU	   22
ad9	EQU	   23
ad10	EQU	   24
portclosedir	EQU	   26
portdirectory	EQU	   27
pd1	EQU	   28
pupslow	EQU	   30
pupfast	EQU	   31
pdownfast	EQU	   32
pdownslow	EQU	   33
pd2	EQU	   34
pd3	EQU	   35
pd4	EQU	   36
pd5	EQU	   37
pd6	EQU	   38
pd7	EQU	   39
pd8	EQU	   40
pd9	EQU	   41
pd10	EQU	   42
selectallatari	EQU	   43
stdrivechange	EQU	   44
portdrivechange	EQU	   45
copyfiles	EQU	   46
deletefiles	EQU	   47
exitalltogether	EQU	   48
help	EQU	   49
selectallport	EQU	   50

driveselect	EQU	    1

adriveselect	EQU	    1
bdriveselect	EQU	    2
cdriveselect	EQU	    3
ddriveselect	EQU	    4
edriveselect	EQU	    5
fdriveselect	EQU	    6
gdriveselect	EQU	    7
hdriveselect	EQU	    8
idriveselect	EQU	    9
jdriveselect	EQU	   10
kdriveselect	EQU	   11
ldriveselect	EQU	   12
mdriveselect	EQU	   13
ndriveselect	EQU	   14
odriveselect	EQU	   15
pdriveselect	EQU	   16
dsok	EQU	   17

helpform	EQU	    2

okexit	EQU	   16

progress	EQU	    3

tftitle	EQU	    1
tfile	EQU	    2
bargraph	EQU	    3
meter	EQU	    4
tfname	EQU	    5

pfdriveselect	EQU	    4

pfdrivea	EQU	    2
pfdrivec	EQU	    3


RUNNER	equ	1		1 for .PRG, 0 for .ACC

sendbyte:	macro	byte
	move.w	\1,-(sp)
	move.w	#1,-(sp)
	move.w	#3,-(sp)
	trap	#13
	addq.l	#6,sp
	endm

* NOTE: with this particular program assembled stand-alone there is no way
* to exit from the program!

	IFEQ	RUNNER
	OUTPUT	.ACC
	ELSEIF
	opt x+				dump long labels for debugging
	OUTPUT	.PRG
	ENDC

	include	gemmacro.s

* the program proper
	IFEQ	RUNNER
start	move.l	#mystack,a7		must have a stack!
	ELSEIF
start	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp
	ENDC

	appl_init
	move.w	d0,ap_id		store the application id

	IFEQ	RUNNER
	menu_register	ap_id,#mymenu
	ENDC


	move.w	#4,-(sp)
	trap	#14
	addq.l	#2,sp
	cmpi.w	#1,d0
	bgt	loadhi
loadlo:
	rsrc_load	#rsrcnamemed
	bra	loadedrsc
loadhi:
	rsrc_load	#rsrcnamehi
loadedrsc:
	move.w	int_out,d0
	tst.w	d0
	bne	rscok
*********************************************************
*	Simple loop for no rsc loaded			*
*	just warns user.				*
*********************************************************
	IFEQ	RUNNER
waitforeventnor:
	evnt_mesag	#messagebuf
	move.l	#messagebuf,a0
	move.w	(a0),d0			message type
	cmp.w	#40,d0
	beq	open_accnor
	bra.s	waitforeventnor
open_accnor:
	form_alert	#1,#erroralert
	bra	waitforeventnor

erroralert:
	dc.b	'[3][Could not find the .RSC file.|'
	dc.b	'Make sure both :-|'
	dc.b	'PFMSTHI.RSC and PFMSTMED.RSC|'
	dc.b	'are in the same directory as|'
	dc.b	'this accessory.][SORRY]',0
	even
	ELSEIF
	form_alert	#1,#erroralert
	bra	exit2
erroralert:
	dc.b	'[3][Could not find the .RSC file.|'
	dc.b	'Make sure both :-|'
	dc.b	'PFMSTHI.RSC and PFMSTMED.RSC|'
	dc.b	'are in the same directory as|'
	dc.b	'this programme.][SORRY]',0
	even
	ENDC

*********************************************************
rscok:
	rsrc_gaddr	#0,#backdrop
	move.l	addr_out.L,bdaddress

	IFEQ	RUNNER
waitforevent:
	evnt_mesag	#messagebuf
	move.l	#messagebuf,a0
	move.w	(a0),d0			message type
	cmp.w	#40,d0
	beq	open_acc
	bra.s	waitforevent
	ENDC

open_acc:
	graf_handle
	move.w	d0,current_handle	Desktop's VDI handle

* start by opening a virtual workstation
*	lea	intin,a0
	move.l	#intin,a0
	moveq	#10-1,d0		-1 for DBF
.fill	move.w	#1,(a0)+		most params are 1
	dbf	d0,.fill
	move.w	#2,(a0)+		use RC system
	v_opnvwk			open it
	wind_get	#0,#4		work area of Desktop

	movem.w	int_out+2.L,d0-d3
	movem.w	d0-d3,savebounds

	form_dial	#0,#0,#0,#0,#0,d0,d1,d2,d3

	form_center	bdaddress
	movem.w	int_out+2,d0-d3
	movem.w	d0-d3,savesizes

	wind_create	#0,d0,d1,d2,d3
	move.w	d0,w_handle		save the handle (error checks?)
	movem.w	savesizes,d0-d3
	wind_open	w_handle,d0,d1,d2,d3

	wind_get	w_handle,#5		work area of Desktop

	movem.w	int_out+2.L,d0-d3
	movem.w	d0-d3,savebounds

	form_dial	#0,#0,#0,#0,#0,d0,d1,d2,d3

	wind_update	#3

	bsr	drawmydial
*	graf_mouse	#0	;arrow please
	jsr	setupdisk
	jsr	getataridirectory
	jsr	setrs232
	bsr	getstring
	graf_mouse	#2	;arrow please
cp1:
	move.w	#100,d7
cp:
	move.w	d7,-(sp)
	sendbyte	#13
	jsr	getstring	;same as clear port
	sendbyte	#13
	jsr	getstring	;same as clear port
	move.w	(sp)+,d7

	lea	bytes,a6
	cmpi.b	#13,(a6)
	beq	portisthere
	dbf	d7,cp

	form_alert	#1,#noportalert
	move.w	int_out.L,d0
	cmpi.w	#1,d0
	beq	cp1
	bra	exit

portisthere:
	graf_mouse	#0	;arrow please
*	jsr	checkport
	jsr	getpfdirectory
	jsr	displayad
	jsr	displaypd
	jsr	showpath
	jsr	showpfpath
*********************************************************
*	Now that the dialogue is drawn we go into an	*
*	event loop and wait for a mouse click. On	*
*	receiving such an event we find out if it was	*
*	clicked over any important object and take the	*
*	appropriate action.				*
*********************************************************
mainloop:
	evnt_button	#1,#1,#1
wasmouse:
	movem.w	int_out+2.L,d0-d1	;get the coords of the mouse when button was
	movem.w	d0-d1,mousex		;pressed, and save em

	objc_find	bdaddress,#backdrop,#3,d0,d1
	moveq	#0,d0
	move.w	int_out,d0		;get object number
	move.w	d0,saveindex

	move.l	bdaddress,a0
	mulu	#24,d0
	add.w	d0,a0
	move.w	8(a0),d1
	move.w	d1,d7
	btst	#0,d1
	beq	cantselect
	move.w	10(a0),d1
	btst	#0,d1
	beq	setit
	bclr	#0,d1
	objc_change	bdaddress,saveindex,#0,#0,#0,#0,d1,#1
	btst	#2,d7
	beq	cantselect
	evnt_button	#1,#1,#0
	bra	cantselect
setit:
	bset	#0,d1
	objc_change	bdaddress,saveindex,#0,#0,#0,#0,d1,#1
	btst	#2,d7
	beq	cantselect
	evnt_button	#1,#1,#0
cantselect:
	move.w	saveindex,d0
isitexit:
	cmpi.w	#exitalltogether,d0
	bne	isitstdrivechange
	bsr	resetobject
	IFEQ	RUNNER
	bra	exit
	ELSEIF
	bra	exit3
	ENDC

isitstdrivechange:
	cmpi.w	#stdrivechange,d0
	bne	isitaupslow
	bsr	handlestdrivechange
	bsr	resetobject
	bra	mainloop

isitaupslow:
	cmpi.w	#aupslow,d0
	bne	isitadownslow
	subq.w	#1,startname
	bsr	checkadbounds
	bsr	displayad
	bsr	resetobject
	bra	mainloop

isitadownslow:
	cmpi.w	#adownslow,d0
	bne	isitaupfast
	addq.w	#1,startname
	bsr	checkadbounds
	bsr	displayad
	bsr	resetobject
	bra	mainloop
isitaupfast:
	cmpi.w	#aupfast,d0
	bne	isitadownfast
	sub.w	#9,startname
	bsr	checkadbounds
	bsr	displayad
	bsr	resetobject
	bra	mainloop
isitadownfast:
	cmpi.w	#adownfast,d0
	bne	isithelp
	add.w	#9,startname
	bsr	checkadbounds
	bsr	displayad
	bsr	resetobject
	bra	mainloop

isithelp:
	cmpi.w	#help,d0
	bne	isitad1
	bsr	dohelp
	bsr	resetobject
	bra	mainloop
isitad1:
	cmpi.w	#ad1,d0
	bne	isitad2
	moveq	#0,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad2:
	cmpi.w	#ad2,d0
	bne	isitad3
	moveq	#1,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad3:
	cmpi.w	#ad3,d0
	bne	isitad4
	moveq	#2,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad4:
	cmpi.w	#ad4,d0
	bne	isitad5
	moveq	#3,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad5:
	cmpi.w	#ad5,d0
	bne	isitad6
	moveq	#4,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad6:
	cmpi.w	#ad6,d0
	bne	isitad7
	moveq	#5,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad7:
	cmpi.w	#ad7,d0
	bne	isitad8
	moveq	#6,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad8:
	cmpi.w	#ad8,d0
	bne	isitad9
	moveq	#7,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad9:
	cmpi.w	#ad9,d0
	bne	isitad10
	moveq	#8,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitad10:
	cmpi.w	#ad10,d0
	bne	isitatariclosedir
	moveq	#9,d0		;amount to add to startname
	bsr	setoropen
	bra	mainloop
isitatariclosedir:
	cmpi.w	#atariclosedir,d0
	bne	isitpupslow
	bsr	backupdir
	bra	mainloop

isitpupslow:
	cmpi.w	#pupslow,d0
	bne	isitpdownslow
	subq.w	#1,startname2
	bsr	checkpdbounds
	bsr	displaypd
	bsr	resetobject
	bra	mainloop

isitpdownslow:
	cmpi.w	#pdownslow,d0
	bne	isitpupfast
	addq.w	#1,startname2
	bsr	checkpdbounds
	bsr	displaypd
	bsr	resetobject
	bra	mainloop
isitpupfast:
	cmpi.w	#pupfast,d0
	bne	isitpdownfast
	sub.w	#9,startname2
	bsr	checkpdbounds
	bsr	displaypd
	bsr	resetobject
	bra	mainloop
isitpdownfast:
	cmpi.w	#pdownfast,d0
	bne	isitpd1
	add.w	#9,startname2
	bsr	checkpdbounds
	bsr	displaypd
	bsr	resetobject
	bra	mainloop

isitpd1:
	cmpi.w	#pd1,d0
	bne	isitpd2
	moveq	#0,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd2:
	cmpi.w	#pd2,d0
	bne	isitpd3
	moveq	#1,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd3:
	cmpi.w	#pd3,d0
	bne	isitpd4
	moveq	#2,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd4:
	cmpi.w	#pd4,d0
	bne	isitpd5
	moveq	#3,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd5:
	cmpi.w	#pd5,d0
	bne	isitpd6
	moveq	#4,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd6:
	cmpi.w	#pd6,d0
	bne	isitpd7
	moveq	#5,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd7:
	cmpi.w	#pd7,d0
	bne	isitpd8
	moveq	#6,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd8:
	cmpi.w	#pd8,d0
	bne	isitpd9
	moveq	#7,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd9:
	cmpi.w	#pd9,d0
	bne	isitpd10
	moveq	#8,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpd10:
	cmpi.w	#pd10,d0
	bne	isitpfclosedir
	moveq	#9,d0		;amount to add to startname
	bsr	setoropenpf
	bra	mainloop
isitpfclosedir:
	cmpi.w	#portclosedir,d0
	bne	isitportdrivechange
	bsr	backupdirpf
	bra	mainloop
isitportdrivechange:
	cmpi.w	#portdrivechange,d0
	bne	isitselectallatari
	bsr	handlepfdrivechange
	bsr	resetobject
	bra	mainloop
isitselectallatari:
	cmpi.w	#selectallatari,d0
	bne	isitselectallport
	bsr	doselectallatari
	bsr	resetobject
	bra	mainloop
isitselectallport:
	cmpi.w	#selectallport,d0
	bne	isitclearselected
	bsr	doselectallpf
	bsr	resetobject
	bra	mainloop
isitclearselected:
	cmpi.w	#clearselected,d0
	bne	isitdeletefiles
	bsr	doclearselected
	bsr	resetobject
	bra	mainloop
isitdeletefiles:
	cmpi.w	#deletefiles,d0
	bne	isitcopyfiles
	bsr	dodelete
	bsr	resetobject
	bra	mainloop
isitcopyfiles:
	cmpi.w	#copyfiles,d0
	bne	dummy
	bsr	docopyfiles
	bsr	resetobject
	bra	mainloop


dummy:
*	bsr	resetobject
	bra	mainloop


resetobject:
	move.l	bdaddress,a0
	moveq	#0,d0
	move.w	saveindex,d0
	mulu	#24,d0
	move.w	10(a0,d0.w),d1
	bclr	#0,d1
	objc_change	bdaddress,saveindex,#0,#0,#0,#0,d1,#1
	rts

eventflags:	dc.w	%0000000000000011
saveindex:	dc.w	0
numfiles:	dc.w	0
numfiles2:	dc.w	0
*********************************************************
*	docopyfiles, trickiest part of the prog, must	*
*	check size of file before sending or receiving	*
*	and skip if not enough room on disk. Must setup	*
*	the bar display and do xmodem transfers.	*
*********************************************************
docopyfiles:
	cmpi.w	#atari,whichmachine
	bne	docopypf
docopyatari:
	lea	afilelist,a6
dcal:
	tst.b	1(a6)		;are we at the end op the list
	bmi	dcovera
	tst.b	(a6)
	beq	cnotsela
	lea	2(a6),a5	;get the name
	lea	genbuffer,a0
dcal2:
	move.b	(a5)+,d0
	tst.b	d0
	beq	allcc
	cmpi.b	#' ',d0
	beq	sskip.
	move.b	d0,(a0)+
sskip.:
	bra	dcal2
allcc:
	move.b	#0,(a0)+	;nul terminate the string
	move.l	a6,a5
	lea	15(a5),a5	;a5 points at length
	movem.l	d0-a6,-(sp)
	bsr	dosendfile
	movem.l	(sp)+,d0-a6
	tst.w	sentflag
	bmi	cnotsela	;didn't send cause not enough room?
	move.b	#0,(a6)		;deselect it if sent ok
cnotsela:
	lea	recsize(a6),a6
	bra	dcal
dcovera:
	bsr	drawmydial
	move.w	#0,startname	;set to display from top of dir
	bsr	displayad
	bsr	getpfdirectory
	move.w	#0,startname2	;set to display from top of dir
	bsr	displaypd
	rts

docopypf:
	lea	pfilelist,a6
dcalp:
	tst.b	1(a6)		;are we at the end op the list
	bmi	dcoverap
	tst.b	(a6)
	beq	cnotselap
	lea	2(a6),a5	;get the name
	lea	genbuffer,a0
dcal2p:
	move.b	(a5)+,d0
	tst.b	d0
	beq	allccp
	cmpi.b	#' ',d0
	beq	sskipp.
	move.b	d0,(a0)+
sskipp.:
	bra	dcal2p
allccp:
	move.b	#0,(a0)+	;nul terminate the string
	move.l	a6,a5
	lea	15(a5),a5	;a5 points at length
	movem.l	d0-a6,-(sp)
	bsr	doxmodemdown
	movem.l	(sp)+,d0-a6
	tst.w	sentflag
	bmi	cnotselap	;didn't send cause not enough room?
	move.b	#0,(a6)		;deselect it if sent ok
cnotselap:
	lea	recsize(a6),a6
	bra	dcalp
dcoverap:
	bsr	drawmydial
	move.w	#0,startname	;set to display from top of dir
	bsr	getataridirectory
	bsr	displayad
	move.w	#0,startname2	;set to display from top of dir
	bsr	displaypd
	rts




*********************************************************
*	dodeletefiles, warns the user then determines	*
*	which machine has the selected files and	*
*	deletes them.					*
*********************************************************
dodelete:
	form_alert	#2,#delwarn
	move.w	int_out,d0
	cmpi.w	#2,d0
	beq	nodelete
	graf_mouse	#2
	cmpi.w	#atari,whichmachine
	beq	dodelatari
dodelpf:
	lea	pfilelist,a6
ddpfloop:
	tst.b	1(a6)
	bmi	ddover		;is it the end of the list
	tst.b	(a6)
	beq	notseldpf	;is it selected
	lea	2(a6),a5
	movem.l	a5-a6,-(sp)
	sendbyte	#'9'
	bsr	getstring
	sendbyte	#13
	bsr	getstring
	movem.l	(sp),a5-a6
	bsr	sendname
	bsr	getstring
	movem.l	(sp)+,a5-a6
notseldpf:
	lea	recsize(a6),a6
	bra	ddpfloop
ddover:
	bsr	getpfdirectory
	move.w	#0,startname2	;set to display from top of dir
	bsr	displaypd
	graf_mouse	#0
	rts

sendname:
	move.b	(a5)+,d1
	tst.b	d1
	beq	sentname
	cmpi.b	#' ',d1
	beq	skipthisbyte
	move.l	a5,-(sp)
	sendbyte	d1
	bsr	getstring
	move.l	(sp)+,a5
skipthisbyte:
	bra	sendname
sentname:
	sendbyte	#13
	bsr	getstring
	rts

dodelatari:
	lea	afilelist,a6
ddal:
	tst.b	1(a6)
	bmi	ddovera
	tst.b	(a6)
	beq	notsela
	lea	2(a6),a5
	lea	genbuffer,a0
ddal2:
	move.b	(a5)+,d0
	tst.b	d0
	beq	allc
	cmpi.b	#' ',d0
	beq	skip.
	move.b	d0,(a0)+
skip.:
	bra	ddal2
allc:
	move.b	#0,(a0)+
	move.l	#genbuffer,-(sp)
	move.w	#$41,-(sp)
	trap	#1
	addq.l	#6,sp
notsela:
	lea	recsize(a6),a6
	bra	ddal
ddovera:
	bsr	getataridirectory
	move.w	#0,startname	;set to display from top of dir
	bsr	displayad
nodelete:
	graf_mouse	#0
	rts

delwarn:
	dc.b	'[3][DELETE FILES will delete|'
	dc.b	'all the files that are|'
	dc.b	'currently selected.| Are you sure?]'
	dc.b	'[YES|NO]',0
	even
*********************************************************
*	doclearselected, deselects all the files in both*
*	directories.					*
*********************************************************
doclearselected:
	bsr	deselectatari
	bsr	deselectpf
	rts

deselectatari:
	lea	afilelist,a0
dcsaloop:
	tst.b	1(a0)
	bmi	dcsa1
	cmpi.b	#7,1(a0)	;skip folders of course
	beq	skipthisc
	move.b	#0,(a0)		;select it
skipthisc:
	add.l	#recsize,a0
	bra	dcsaloop
dcsa1:
	bsr	displayad
	rts

deselectpf:
	lea	pfilelist,a0
dcsploop:
	tst.b	1(a0)
	bmi	dcsp1
	cmpi.b	#7,1(a0)	;skip folders of course
	beq	skipthispc
	move.b	#0,(a0)		;select it
skipthispc:
	add.l	#recsize,a0
	bra	dcsploop
dcsp1:
	bsr	displaypd
	rts

*********************************************************
*	Selectallatari, selects all the files in the	*
*	current directory.				*
*********************************************************
doselectallatari:
	move.w	#-1,donta
	bsr	deselectpf
	lea	afilelist,a0
dsaloop:
	tst.b	1(a0)
	bmi	dsa1
	cmpi.b	#7,1(a0)	;skip folders of course
	beq	skipthis
	move.b	#1,(a0)		;select it
skipthis:
	add.l	#recsize,a0
	bra	dsaloop
dsa1:
	bsr	displayad
	rts
*********************************************************
*	Selectallpf, selects all the files in the	*
*	current directory.				*
*********************************************************
doselectallpf:
	move.w	#-1,dontp
	bsr	deselectatari
	lea	pfilelist,a0
dsploop:
	tst.b	1(a0)
	bmi	dsp1
	cmpi.b	#7,1(a0)	;skip folders of course
	beq	skipthisp
	move.b	#1,(a0)		;select it
skipthisp:
	add.l	#recsize,a0
	bra	dsploop
dsp1:
	bsr	displaypd
	rts


*********************************************************
*	Resetallfiles					*
*	resets all of the file objects to no selected	*
*********************************************************
resetallfiles:
	objc_change	bdaddress,#ad1,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad2,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad3,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad4,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad5,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad6,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad7,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad8,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad9,#0,#0,#0,#0,#0,#1
	objc_change	bdaddress,#ad10,#0,#0,#0,#0,#0,#1
	rts
*********************************************************
*	setrs232, set the rs232 port to 9600 baud	*
*********************************************************
setrs232:
	move.w	#-1,-(sp)
	move.w	#-1,-(sp)
	move.w	#-1,-(sp)    43
	move.w	#$008a,-(sp)
	move.w	#0,-(sp)	;no handshake
	move.w	#1,-(sp)	;9600 baud
	move.w	#15,-(sp)
	trap	#14
	add.l	#14,sp
	rts
*********************************************************
*	getpfdirectory, gets the portfolio's directory	*
*	and organises all the shit associated with it	*
*	pretty similar to getataridirectory.		*
*********************************************************
getpfdirectory:
	graf_mouse	#2
	move.w	#0,numfiles2
	bsr	cleardirectory2
	sendbyte	#'3'	;first request dir
	bsr	getstring		;get string after each sent byte
	sendbyte	#13		;is necessary cause the character
	bsr	getstring		;is echoed
	sendbyte	#13	;use *.* mask
	bsr	getstring
getpfdirs:
	lea	bytes,a6
	add.l	#28,a6		;go to start of path
	bsr	getpfpath
	bsr	track2cr
	lea	pfilelist,a5
gpfdirs:
	cmpi.b	#'<',13(a6)
	bne	notdir
	cmpi.b	#'.',(a6)	;if first char is . then toss it
	beq	notdir		;don't display the parent and root
	move.b	#0,(a5)+		;signify not selected
	move.b	#07,(a5)+		;signify directory
	bsr	copypname
notdir:
	cmpi.b	#13,22(a6)	;is it the end of the list
	beq	getpffiles
	add.l	#20,a6		;skip to next file or directory name
	bra	gpfdirs
getpffiles:
	lea	bytes,a6
	add.l	#28,a6		;go to start of path
	bsr	track2cr
*	lea	pfilelist,a5
gpffiles:
	cmpi.b	#'<',13(a6)
	beq	notfile
	move.b	#0,(a5)+		;signify not selected
	move.b	#' ',(a5)+		;signify not directory
	bsr	copypname
notfile:
	cmpi.b	#13,22(a6)	;is it the end of the list
	beq	getpfdone
	add.l	#20,a6		;skip to next file or directory name
	bra	gpffiles
getpfdone:
	graf_mouse	#0
	rts


copypname:
	addq.w	#1,numfiles2
	moveq	#7,d7
	move.l	a6,a0
cpnl1:
	move.b	(a0)+,d0
	cmpi.b	#'.',d0
	beq	cpnl2
	move.b	d0,(a5)+
	dbf	d7,cpnl1
	move.b	#'.',(a5)+
	addq.l	#1,a0
	bra	cpnl3
cpnl2:
	move.b	#' ',(a5)+
	dbf	d7,cpnl2
	move.b	#'.',(a5)+
cpnl3:
	moveq	#2,d7
cpnl1b:
	move.b	(a0)+,d0
	cmpi.b	#'.',d0
	beq	cpnl2b
	move.b	d0,(a5)+
	dbf	d7,cpnl1b
	move.b	#0,(a5)+
	bra	cpnl3b
cpnl2b:
	move.b	#' ',(a5)+
	dbf	d7,cpnl2b
	move.b	#0,(a5)+
cpnl3b:
	lea	string,a2
	move.l	a2,a3
.cpl:
	move.b	(a0)+,d0	;this bit gets the size digits
	cmpi.b	#'.',d0
	beq	.cpl
	cmpi.b	#' ',d0
	beq	endofsize
	move.b	d0,(a2)+
	bra	.cpl
endofsize:
	move.b	#0,(a2)+
	exg	a0,a3
	bsr	decbin

	move.b	d0,3(a5)		;size field
	lsr.l	#8,d0
	move.b	d0,2(a5)
	lsr.l	#8,d0
	move.b	d0,1(a5)
	lsr.l	#8,d0
	move.b	d0,0(a5)
	exg	a0,a3
	addq.l	#4,a5
	rts

track2cr:
	move.b	(a6)+,d0
	cmpi.b	#13,d0		;track to cr in fact
	bne	track2cr
	rts

getpfpath:
	move.l	a6,a5
	lea	pfpath,a0
getpfloop:
	move.b	(a5)+,d0
	cmpi.b	#13,d0		;track to cr in fact
	beq	gpfo
	move.b	d0,(a0)+
	bra	getpfloop
gpfo:
	subq.l	#6,a0
	cmpi.b	#'\',(a0)
	beq.s	okpfpath
	addq.l	#1,a0
	move.b	#'\',(a0)+
	move.b	#'*',(a0)+
	move.b	#'.',(a0)+
	move.b	#'*',(a0)+
	move.b	#0,(a0)
	rts
okpfpath:
	addq.l	#4,a0
	move.b	#0,(a0)
	rts

string:		dc.b	0,0,0,0,0,0,0
pfpath:		ds.b	100
*********************************************************
*	Checkport, see if the portfolio is there.	*
*	we do this by requesting a file and seeing	*
*	if the PF responds with the correct string.	*
*	If it does just send a return instead of a	*
*	filename.					*
*********************************************************
checkport:
	sendbyte	#'1'
	bsr	getstring
	sendbyte	#13
	move.w	#1,-(sp)
	move.w	#1,-(sp)
	trap	#13
	addq.l	#4,sp
	tst.l	d0
	beq	nonethere
	bra	characterhere
nonethere:
	form_alert	#1,#noportalert
	move.w	int_out,d0
	cmpi.w	#1,d0
	beq	checkport
	bra	exit			;get outta here
characterhere:
	bsr	getstring
	sendbyte	#13		;cancels the request
	bsr	getstring
	rts


*********************************************************
*	General getstring routine, gets garbage strings	*
*	and tosses them away.				*
*********************************************************
getstring:
	lea	bytes,a6
	move.l	#800,d7		;times to try on each 0
	move.l	d7,d6
gsloop:
	move.w	#1,-(sp)
	move.w	#1,-(sp)
	trap	#13
	addq.l	#4,sp
	tst.l	d0
	beq	timeout
	move.l	d7,d6		;reset on each success
	move.w	#1,-(sp)
	move.w	#2,-(sp)
	trap	#13
	addq.l	#4,sp
	move.b	d0,(a6)+
	bra	gsloop
timeout:
*	dbf	d7,gsloop
	subq.l	#1,d6
	bpl	gsloop
gotstring:
	move.b	#0,(a6)+	;null terminate
*	pea	bytes
*	move.w	#$9,-(sp)
*	trap	#1
*	addq.l	#6,sp
	rts


bytes:
	ds.b	2000
noportalert:
	dc.b	'[3][Portfolio is not responding.|'
	dc.b	'Check that the cable is|'
	dc.b	'connected correctly and that|'
	dc.b	'you have run GOPLUS on the|'
	dc.b	'Portfolio.][Retry|Quit]',0
	even
*********************************************************
*	Backupdir, backs up one directory level from	*
*	the current one. Has to check if it is the	*
*	highest level or sparks fly.			*
*********************************************************
backupdir:
	move.w	#0,-(sp)
	pea	pathbuffer
	move.w	#$47,-(sp)
	trap	#1
	addq.l	#8,sp

	lea	pathbuffer,a6
	move.l	a6,a5
.loop:
	tst.b	(a6)+		;track to the null
	bne	.loop
	subq.l	#1,a6
.loop2:
	cmp.l	a6,a5
	beq	rootdir
	cmpi.b	#'\',-(a6)	;track back to the last '\'
	bne	.loop2
	move.b	#0,(a6)		;make it end of string
	cmp.l	a5,a6
	bne	okpath
rootdir:	
	move.w	currentdrive,d0
	add.b	#'A',d0
	move.b	d0,(a6)+
	move.b	#':',(a6)+
	move.b	#'\',(a6)+
	move.b	#0,(a6)+
okpath:
	pea	pathbuffer
	move.w	#$3b,-(sp)
	trap	#1
	addq.l	#6,sp

	bsr	resetobject
*	bsr	resetallfiles
	bsr	getataridirectory
	move.w	#0,startname	;set to display from top of dir
	bsr	displayad
	bsr	showpath
	rts

*********************************************************
*	Backupdir, backs up one directory level from	*
*	the current one. Has to check if it is the	*
*	highest level or sparks fly.			*
*********************************************************
backupdirpf:
	lea	pfpath,a6
	move.l	a6,a5
.loop:
	tst.b	(a6)+		;track to the null
	bne	.loop
	subq.l	#1,a6
.loop2:
	cmpi.b	#'\',-(a6)	;track back to the last '\'
	bne	.loop2
.loop3:
	cmp.l	a5,a6
	beq	rootdirpf
	cmpi.b	#'\',-(a6)	;track back to the second last '\'
	bne	.loop3
	addq.l	#1,a6
	move.b	#0,(a6)		;make it end of string
rootdirpf:	
	sendbyte	#'5'
	bsr	getstring
	sendbyte	#13
	bsr	getstring
	lea	pfpath,a6
sendpathloop2:
	move.b	(a6)+,d1
	tst.b	d1
	beq	sent2
	move.l	a6,-(sp)
	sendbyte	d1
	bsr	getstring
	move.l	(sp)+,a6
	bra	sendpathloop2
sent2:
	sendbyte	#13
	bsr	getstring

	bsr	resetobject
	bsr	getpfdirectory
	move.w	#0,startname2	;set to display from top of dir
	bsr	displaypd
	bsr	showpfpath
	rts

*********************************************************
*	Setoropen, if the object clicked on is one of	*
*	the directory entries, this will determine if	*
*	it is a folder or file. If it's a folder then	*
*	it changes to that directory, gets the directory*
*	and displays it. If it's a file it will toggle	*
*	it's selected status.				*
*	D0, contains the amount to add to startname	*
*	in order to calculate which filename has been	*
*	selected.					*
*	Also makes sure that the file is actually there	*
*	ie: numfiles > d0				*
*********************************************************
setoropen:
	add.w	startname,d0
	move.w	d0,d1
	addq.w	#1,d1
	cmp.w	numfiles,d1
	bgt	specialso
	lea	afilelist,a6
	mulu	#recsize,d0
	add.l	d0,a6
	cmpi.b	#07,1(a6)	;is it a folder
	beq	openfolder
	cmpi.b	#1,(a6)
	beq	deselect
	move.b	#1,(a6)
	move.w	#atari,whichmachine
	move.w	#-1,donta
	tst.w	dontp
	bpl	skip0
	bsr	deselectpf
	move.w	#1,dontp
skip0:
	rts
deselect:
	move.b	#0,(a6)
	tst.w	dontp
	bpl	skip0
	bsr	deselectpf
	move.w	#1,dontp
	rts
specialso:
	bsr	resetobject
	rts
openfolder:
	move.l	a6,-(sp)	;save the file name location

	move.w	#0,-(sp)
	pea	pathbuffer
	move.w	#$47,-(sp)
	trap	#1
	addq.l	#8,sp

	lea	pathbuffer,a5
.loop:
	tst.b	(a5)+		;track to the null
	bne	.loop
	subq.l	#1,a5
	move.b	#'\',(a5)+
	move.l	(sp)+,a6
	addq.l	#2,a6		;skip past select and folder flag
ofloop:
	move.b	(a6)+,d0
	cmpi.b	#' ',d0
	beq	ofloop		;don't copy padded spaces
	tst.b	d0
	beq	ofover
	move.b	d0,(a5)+
	bra	ofloop
ofover:
	move.b	d0,(a5)+
	pea	pathbuffer
	move.w	#$3b,-(sp)
	trap	#1
	addq.l	#6,sp

	bsr	resetobject
*	bsr	resetallfiles
	bsr	getataridirectory
	move.w	#0,startname	;set to display from top of dir
	bsr	displayad
	bsr	showpath

	rts
donta:	dc.w	-1
dontp:	dc.w	-1
*********************************************************
*	Setoropenpf, if the object clicked on is one of	*
*	the directory entries, this will determine if	*
*	it is a folder or file. If it's a folder then	*
*	it changes to that directory, gets the directory*
*	and displays it. If it's a file it will toggle	*
*	it's selected status.				*
*	D0, contains the amount to add to startname	*
*	in order to calculate which filename has been	*
*	selected.					*
*	Also makes sure that the file is actually there	*
*	ie: numfiles > d0				*
*********************************************************
setoropenpf:
	add.w	startname2,d0
	move.w	d0,d1
	addq.w	#1,d1
	cmp.w	numfiles2,d1
	bgt	specialso2
	lea	pfilelist,a6
	mulu	#recsize,d0
	add.l	d0,a6
	cmpi.b	#07,1(a6)	;is it a folder
	beq	openfolder2
	cmpi.b	#1,(a6)
	beq	deselect2
	move.b	#1,(a6)
	move.w	#-1,dontp
	move.w	#port,whichmachine
	tst.w	donta
	bpl	skipd
	bsr	deselectatari
	move.w	#1,donta
skipd:
	rts
deselect2:
	move.b	#0,(a6)
	tst.w	donta
	bpl	skipd
	bsr	deselectatari
	move.w	#1,donta
	rts

specialso2:
	bsr	resetobject
	rts
openfolder2:
	move.l	a6,-(sp)	;save the file name location
	lea	pfpath,a5
.loop:
	tst.b	(a5)+		;track to the null
	bne	.loop
	subq.l	#4,a5

	move.l	(sp)+,a6
	addq.l	#2,a6		;skip past select and folder flag
ofloop2:
	move.b	(a6)+,d0
	cmpi.b	#' ',d0
	beq	ofloop2		;don't copy padded spaces
	tst.b	d0
	beq	ofover2
	move.b	d0,(a5)+
	bra	ofloop2
ofover2:
	move.b	d0,(a5)+
	sendbyte	#'5'
	bsr	getstring
	sendbyte	#13
	bsr	getstring

	lea	pfpath,a6
sendpathloop:
	move.b	(a6)+,d1
	tst.b	d1
	beq	sent
	move.l	a6,-(sp)
	sendbyte	d1
	bsr	getstring
	move.l	(sp)+,a6
	bra	sendpathloop
sent:
	sendbyte	#13
	bsr	getstring

	bsr	resetobject
*	bsr	resetallfiles
	bsr	getpfdirectory
	move.w	#0,startname	;set to display from top of dir
	bsr	displaypd
	bsr	showpfpath
	rts




*********************************************************
*	Dohelp, just puts up the help dialogue and	*
*	waits for the user to exit.			*
*	redraws the backdrop dialogue before return.	*
*********************************************************
dohelp:
	rsrc_gaddr	#0,#helpform
	move.l	addr_out,haddress
	form_center	haddress

	movem.w	savebounds,d0-d3
	objc_draw	haddress,#0,#3,d0,d1,d2,d3
	form_do		haddress,#0
	move.w	int_out,d0
	objc_change	haddress,d0,#0,#0,#0,#0,#0,#0	;change OK back

	bsr	drawmydial
	rts

haddress:	dc.l	0
*********************************************************
*	Checkadbounds, checks that startname is within	*
*	the bounds of the number of files available	*
*********************************************************
checkadbounds:
	tst.w	startname
	bge	nottofew
	move.w	#0,startname
	rts
nottofew:
	move.w	numfiles,d0	;is it a special case
	sub.w	#10,d0
	bmi	lessthan10
	cmp.w	startname,d0
	bgt	bok
	move.w	d0,startname
bok:
	rts
lessthan10:
	move.w	#0,startname
	rts
*********************************************************
*	Checkadbounds, checks that startname is within	*
*	the bounds of the number of files available	*
*********************************************************
checkpdbounds:
	tst.w	startname2
	bge	nottofew2
	move.w	#0,startname2
	rts
nottofew2:
	move.w	numfiles2,d0	;is it a special case
	sub.w	#10,d0
	bmi	lessthan102
	cmp.w	startname2,d0
	bgt	bok2
	move.w	d0,startname2
bok2:
	rts
lessthan102:
	move.w	#0,startname2
	rts
*********************************************************
*	handlepfdrivechange, takes care of this by	*
*	popping up the drive change dialogue and	*
*	seeing which they choose.			*
*********************************************************
handlepfdrivechange:
	rsrc_gaddr	#0,#pfdriveselect
	move.l	addr_out,daddress
	form_center	daddress

	lea	pfpath,a0
	cmpi.b	#'A',(a0)
	beq	seta
	objc_change	daddress,#pfdrivec,#0,#0,#0,#0,#1,#0
	objc_change	daddress,#pfdrivea,#0,#0,#0,#0,#0,#0
	bra	skipy
seta:
	objc_change	daddress,#pfdrivec,#0,#0,#0,#0,#0,#0
	objc_change	daddress,#pfdrivea,#0,#0,#0,#0,#1,#0
skipy:
	movem.w	savebounds,d0-d3
	objc_draw	daddress,#0,#3,d0,d1,d2,d3
	form_do		daddress,#0
	move.w	int_out,d0
	objc_change	daddress,d0,#0,#0,#0,#0,#0,#0	;change OK back

	move.l	daddress,a6

	moveq	#0,d6
	move.l	#pfdrivea,d0
	mulu	#24,d0
	move.w	10(a6,d0.w),d4
	btst	#0,d4		;check ob state
	bne	founddrive2
	move.b	#'C',d6
	bra	skipfd
founddrive2:
	move.b	#'A',d6
skipfd:
	move.w	d6,-(sp)
	sendbyte	#'8'
	bsr	getstring
	sendbyte	#13
	bsr	getstring
	move.w	(sp)+,d6
	sendbyte	d6
	bsr	getstring
	sendbyte	#13
	bsr	getstring

	bsr	drawmydial
	bsr	getpfdirectory
	move.w	#0,startname2	;set to display from top of dir
	bsr	displaypd
	bsr	showpfpath
	rts


*********************************************************
*	Handle the changing of ST drives.		*
*	Pops up the dialogue box of possible drives.	*
*	setting the current drive first.		*
*	Finds the new drive and sets it after return	*
*	from form_do.					*
*	Must call ataridirectory after completion	*
*********************************************************
handlestdrivechange:
	rsrc_gaddr	#0,#driveselect
	move.l	addr_out,daddress
	form_center	daddress

	pea	getdrivebits	;first get the drives that are active
	move.w	#38,-(sp)	;ans set all the buttons that should
	trap	#14		;be selectable
	addq.l	#6,sp

	moveq	#1,d5
	moveq	#15,d6
	move.l	drvbits,d7
setdrvsloop:
	move.w	d5,d4
	subq.w	#1,d4
	btst	d4,d7
	beq.s	setnodrive
	objc_change	daddress,d5,#0,#0,#0,#0,#0,#0
	bra	skipsetno
setnodrive:
	objc_change	daddress,d5,#0,#0,#0,#0,#8,#0
skipsetno:
	addq.w	#1,d5
	dbf	d6,setdrvsloop

	move.w	#$19,-(sp)	;gets the active drive
	trap	#1
	addq.l	#2,sp
	addq.w	#1,d0
	objc_change	daddress,d0,#0,#0,#0,#0,#1,#0

	movem.w	savebounds,d0-d3
	objc_draw	daddress,#0,#3,d0,d1,d2,d3
	form_do		daddress,#0
	move.w	int_out,d0
	objc_change	daddress,d0,#0,#0,#0,#0,#0,#0	;change OK back


	moveq	#15,d5		;now find and set the current drive
	move.l	daddress,a6
	moveq	#0,d6
getdriveloop:
	lea	24(a6),a6	;1st time thru skips first object
	move.w	10(a6),d4
	btst	#0,d4		;check ob state
	bne	founddrive
	addq.w	#1,d6
	dbf	d5,getdriveloop

founddrive:
	move.w	d6,currentdrive
	move.w	d6,-(sp)	;make the selected drive the current
	move.w	#$0e,-(sp)
	trap	#1
	addq.l	#4,sp

	bsr	drawmydial
	bsr	getataridirectory
	move.w	#0,startname	;set to display from top of dir
	bsr	displayad
	bsr	showpath
	rts


getdrivebits:
	move.l	$4c2,drvbits.L
	rts

drvbits:	dc.l	0
currentdrive:	dc.w	0
*********************************************************
*	Showpath, gets the full pathname and shows	*
*	as much of it as it can in the top bar of the	*
*	window.						*
*********************************************************
showpath:
	move.w	#0,-(sp)
	pea	pathbuffer
	move.w	#$47,-(sp)
	trap	#1
	addq.l	#8,sp
	lea	pathbuffer,a0
.loopa:
	move.b	(a0)+,d0
	bne	.loopa
	subq.l	#1,a0
	move.b	#'\',(a0)+
	move.b	#'*',(a0)+
	move.b	#'.',(a0)+
	move.b	#'*',(a0)+
	move.b	#0,(a0)+




	move.l	bdaddress,a5
	moveq	#0,d0
	move.w	#ataridirectory,d0
	mulu	#24,d0		;size of each object structure
	lea	(a5,d0.w),a3	;pointer to correct object
	lea	12(a3),a3	;pointer to tedinfo
	move.l	(a3),a3		;pointer to text
	move.l	(a3),a3		;pointer to text
	move.w	currentdrive,d0
	add.b	#'A',d0
	move.b	d0,(a3)+
	move.b	#':',(a3)+
	moveq	#16,d7
	lea	pathbuffer,a0
.loop:
	move.b	(a0)+,d0
	tst.b	d0
	beq	spover
	move.b	d0,(a3)+
	dbf	d7,.loop
	move.b	#0,(a3)+
	bra	toobig
spover:
	move.b	d0,(a3)+
toobig:
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,#ataridirectory,#0,d0,d1,d2,d3
	rts
*********************************************************
*	Showpfpath, gets the full pathname and shows	*
*	as much of it as it can in the top bar of the	*
*	window.						*
*********************************************************
showpfpath:
	move.l	bdaddress,a5
	moveq	#0,d0
	move.w	#portdirectory,d0
	mulu	#24,d0		;size of each object structure
	lea	(a5,d0.w),a3	;pointer to correct object
	lea	12(a3),a3	;pointer to tedinfo
	move.l	(a3),a3		;pointer to text
	move.l	(a3),a3		;pointer to text

	lea	pfpath,a0
	moveq	#18,d7
.loop:
	move.b	(a0)+,d0
	tst.b	d0
	beq	spover2
	move.b	d0,(a3)+
	dbf	d7,.loop
	move.b	#0,(a3)+
	bra	toobig2
spover2:
	move.b	d0,(a3)+
toobig2:
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,#portdirectory,#0,d0,d1,d2,d3
	rts



*********************************************************
*	Draw the complete dialogue			*
*********************************************************
drawmydial:
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,#backdrop,#3,d0,d1,d2,d3
	rts
*********************************************************
*	Display the Atari's directory.			*
*	Set startname to how far down the list to begin	*
*********************************************************
displayad:
	lea	ataridlist,a4
	move.l	bdaddress,a5
	lea	afilelist,a6
	move.w	startname,d0
	mulu	#recsize,d0
	add.w	d0,a6
	moveq	#10,d7		;counter
dloop:
	move.w	(a4)+,d0
	tst.w	d0
	bmi	dadover
	move.w	d0,d6
	mulu	#24,d0		;size of each object structure
	lea	(a5,d0.w),a3	;pointer to correct object
	lea	12(a3),a3	;pointer to tedinfo
	move.l	(a3),a3		;pointer to text
	move.l	(a3),a3		;pointer to text
	move.l	a6,-(sp)
	move.b	(a6)+,selectedflag
	move.b	(a6)+,d0
	tst.b	d0
	bmi	noname
	move.b	d0,(a3)+	;the folder symbol or space
	move.b	#' ',(a3)+
cnloop:
	move.b	(a6)+,d0
	tst.b	d0
	beq	cnloopover
	move.b	d0,(a3)+
	bra	cnloop
cnloopover:
	move.b	d0,(a3)+	;put the null in
	move.l	(sp)+,a6
	lea	recsize(a6),a6

	moveq	#0,d0
	move.b	selectedflag,d0
	objc_change	bdaddress,d6,#0,#0,#0,#0,d0,#0
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,d6,#0,d0,d1,d2,d3
*	objc_change	bdaddress,d6,#0,#0,#0,#0,#4,#0
*	objc_change	bdaddress,d6,d0,d1,d2,d3,#4,#1
	dbf	d7,dloop
	rts
dadover:
	rts
noname:
	move.b	#' ',(a3)+
	move.b	#' ',(a3)+
	rept	8
	move.b	#'_',(a3)+
	endr
	move.b	#'.',(a3)+
	move.b	#'_',(a3)+
	move.b	#'_',(a3)+
	move.b	#'_',(a3)+
	move.b	#0,(a3)+
	move.l	(sp)+,a6
	lea	recsize(a6),a6
	objc_change	bdaddress,d6,#0,#0,#0,#0,selectedflag,#0
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,d6,#0,d0,d1,d2,d3
	dbf	d7,dloop
	rts




startname:	dc.w	0
ataridlist:	dc.w	ad1,ad2,ad3,ad4,ad5,ad6,ad7,ad8,ad9,ad10
		dc.w	-1	;end of list


*********************************************************
*	Display the pf's directory.			*
*	Set startname to how far down the list to begin	*
*********************************************************
displaypd:
	lea	pfdlist,a4
	move.l	bdaddress,a5
	lea	pfilelist,a6
	move.w	startname2,d0
	mulu	#recsize,d0
	add.w	d0,a6
	moveq	#10,d7		;counter
dloop2:
	moveq	#0,d0
	move.w	(a4)+,d0
	tst.w	d0
	bmi	dadover2
	move.w	d0,d6
	mulu	#24,d0		;size of each object structure
	lea	(a5,d0.w),a3	;pointer to correct object
	lea	12(a3),a3	;pointer to tedinfo
	move.l	(a3),a3		;pointer to text
	move.l	(a3),a3		;pointer to text
	move.l	a6,-(sp)
	move.b	(a6)+,selectedflag
	move.b	(a6)+,d0
	tst.b	d0
	bmi	noname2
	move.b	d0,(a3)+	;the folder symbol or space
	move.b	#' ',(a3)+
cnloop2:
	move.b	(a6)+,d0
	tst.b	d0
	beq	cnloopover2
	move.b	d0,(a3)+
	bra	cnloop2
cnloopover2:
	move.b	d0,(a3)+	;put the null in
	move.l	(sp)+,a6
	lea	recsize(a6),a6

	moveq	#0,d0
	move.b	selectedflag,d0
	objc_change	bdaddress,d6,#0,#0,#0,#0,d0,#0
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,d6,#0,d0,d1,d2,d3
	dbf	d7,dloop2
	rts
dadover2:
	rts
noname2:
	move.b	#' ',(a3)+
	move.b	#' ',(a3)+
	rept	8
	move.b	#'_',(a3)+
	endr
	move.b	#'.',(a3)+
	move.b	#'_',(a3)+
	move.b	#'_',(a3)+
	move.b	#'_',(a3)+
	move.b	#0,(a3)+
	move.l	(sp)+,a6
	lea	recsize(a6),a6
	objc_change	bdaddress,d6,#0,#0,#0,#0,selectedflag,#0
	movem.w	savebounds,d0-d3
	objc_draw	bdaddress,d6,#0,d0,d1,d2,d3
	dbf	d7,dloop2
	rts




startname2:	dc.w	0
pfdlist:	dc.w	pd1,pd2,pd3,pd4,pd5,pd6,pd7,pd8,pd9,pd10
		dc.w	-1	;end of list

*********************************************************
*	Atari Directory.				*
*	Gets the directory on the Atari side.		*
*	Changes the text in the buttons of the dialogue	*
*	First searches for all directories, then files	*
*********************************************************
getataridirectory:
	graf_mouse	#2
	bsr	cleardirectory
	move.w	#0,numfiles
	lea	afilelist,a6
	lea	dta,a5
	lea	21(a5),a4
	lea	30(a5),a5
	move.w	#-1,dodir

	move.w	#$0010,-(sp)
	pea	search
	move.w	#$4e,-(sp)
	trap	#1
	addq.l	#8,sp
	cmpi.w	#-33,d0
	beq.s	nodirectories
	cmpi.b	#'.',(a5)	;skip the . and ..
	beq	dirsearch
	bsr	copyfname
dirsearch:
	move.w	#$4f,-(sp)
	trap	#1
	addq.l	#2,sp
	tst.w	d0
	bne	nodirectories
	cmpi.w	#'..',(a5)
	beq	dirsearch
	bsr	copyfname
	bra	dirsearch
nodirectories:
	move.w	#1,dodir
	move.w	#$0000,-(sp)
	pea	search
	move.w	#$4e,-(sp)
	trap	#1
	addq.l	#8,sp
	cmpi.w	#-33,d0
	beq.s	nodirectories2
*	cmpi.b	#'.',(a5)	;skip the . and ..
*	beq	dirsearch
	bsr	copyfname
dirsearch2:
	move.w	#$4f,-(sp)
	trap	#1
	addq.l	#2,sp
	tst.w	d0
	bne	nodirectories2
*	cmpi.w	#'..',(a5)
*	beq	dirsearch
	bsr	copyfname
	bra	dirsearch2
nodirectories2:
	graf_mouse	#0
	rts

dodir:	dc.w	0

*	from a5 to a6
copyfname:
	tst.w	dodir
	bmi	justdirs
justfiles:
	cmpi.b	#$10,(a4)
	beq	cfback
	addq.w	#1,numfiles
	lea	dta,a3
	move.b	26(a3),15(a6)		;size of file
	move.b	27(a3),16(a6)		;size of file
	move.b	28(a3),17(a6)		;size of file
	move.b	29(a3),18(a6)		;size of file
	move.b	#0,(a6)+		;signify not selected
	move.b	#' ',(a6)+		;signify file
	bra	docopyf
justdirs:
	cmpi.b	#$10,(a4)
	bne	cfback
	addq.w	#1,numfiles
	move.b	#0,(a6)+		;signify not selected
	move.b	#07,(a6)+		;signify directory
docopyf:
	moveq	#8,d7		;counter
	move.l	a5,a0
.loop:
	move.b	(a0)+,d0
	subq.w	#1,d7
	cmpi.b	#'.',d0
	beq	extend
	tst.b	d0
	beq	extend
	move.b	d0,(a6)+
	bra	.loop
extend:
	tst.w	d7
	blt	.skip
.extend:
	move.b	#' ',(a6)+	;pad with spaces to '.'
	dbf	d7,.extend
.skip:
	move.b	#'.',(a6)+
	moveq	#3,d7
	tst.b	d0
	beq	cfover2
doextender:
	move.b	(a0)+,d0
	subq.w	#1,d7
	tst.b	d0
	beq	cfover
	move.b	d0,(a6)+
	bra	doextender
cfover2:
	subq.w	#1,d7
	bra	cfover3
cfover:
	tst.w	d7
	blt	sskip
cfover3:
	move.b	#' ',(a6)+	;pad with spaces to '.'
	dbf	d7,cfover3
sskip:
	move.b	#0,(a6)+	;copy the null and return
	addq.l	#4,a6		;skip the size data
cfback:
	rts


cleardirectory:
	lea	afilelist,a0
	move.w	#399,d0
cdloop:
	move.b	#0,(a0)+	;flag for selected or not 0 not 1 yes
	move.b	#-1,(a0)+	;not used flag
	rept	8
	move.b	#'_',(a0)+
	endr
	move.b	#'.',(a0)+
	move.b	#'_',(a0)+
	move.b	#'_',(a0)+
	move.b	#'_',(a0)+
	move.b	#0,(a0)+	;null at end of string
	move.b	#0,(a0)+	;null at end of string
	move.b	#0,(a0)+	;null at end of string
	move.b	#0,(a0)+	;null at end of string
	move.b	#0,(a0)+	;null at end of string
	dbf	d0,cdloop
	rts
cleardirectory2:
	lea	pfilelist,a0
	move.w	#399,d0
cdloop2:
	move.b	#0,(a0)+	;flag for selected or not 0 not 1 yes
	move.b	#-1,(a0)+	;not used flag
	rept	8
	move.b	#'_',(a0)+
	endr
	move.b	#'.',(a0)+
	move.b	#'_',(a0)+
	move.b	#'_',(a0)+
	move.b	#'_',(a0)+
	move.b	#0,(a0)+	;null at end of string
	move.b	#0,(a0)+	;size
	move.b	#0,(a0)+
	move.b	#0,(a0)+
	move.b	#0,(a0)+
	dbf	d0,cdloop2
	rts

search:	dc.b	'*.*',0		;global search string
	even
savedta:	dc.l	0
*********************************************************
*	Sets the disk transfer address and calls sfirst	*
*********************************************************
setupdisk:
	move.w	#$2f,-(sp)
	trap	#1
	addq.l	#2,sp
	move.l	d0,savedta

	pea	dta
	move.w	#$1a,-(sp)	;set disk transfer address
	trap	#1
	addq.l	#6,sp

	move.w	#$19,-(sp)	;gets the active drive
	trap	#1
	addq.l	#2,sp
	move.w	d0,currentdrive
	rts

exit:
	IFEQ	RUNNER
	move.l	savedta,-(sp)
	move.w	#$1a,-(sp)	;set disk transfer address
	trap	#1
	addq.l	#6,sp
	wind_update	#2
	wind_close	w_handle
	wind_delete	w_handle
	v_clsvwk			close it
	bra	waitforevent
	ENDC
exit3:
	move.l	savedta,-(sp)
	move.w	#$1a,-(sp)	;set disk transfer address
	trap	#1
	addq.l	#6,sp
	wind_update	#2
	wind_close	w_handle
	wind_delete	w_handle
	v_clsvwk			close it

	rsrc_free
	v_clsvwk			close it
exit2:
	appl_exit			tell GEM I've finished

	clr.w	-(a7)			status code
	move.w	#$4c,-(a7)		P_TERM
	trap	#1			and go away



decbin:	movem.l	d1/a0,-(sp)	* save starting registers
*	clr.l	d0		* Zero out accumulator
	moveq	#0,d0
loop:	cmpi.b	#'9',(a0)	* upper bound
	bhi	notdec		* not a decimal digit
	cmpi.b	#'0',(a0)	* lower bound
	blo	notdec		* not a decimal digit
	lsl.l	#1,d0		* multiply by 2
	move.l	d0,d1		* save this
	lsl.l	#2,d0		* Now multiply by 8
	add.l	d1,d0		* Now By 10
	move.b	(a0)+,d1	* fetch digit
	andi.l	#$0f,d1		* Isolate binary digit
	add.l	d1,d0		* Add into accumulator
	bra	loop		* Try another digit
notdec:	movem.l	(SP)+,a0/d1	* Unsave registers
	rts

genaddress:	dc.l	0
*****************************************************************
*	dosendfile						*
*	sends the file whose name is in genbuffer.		*
*	sets sent to -1 if not sent due to lack of room		*
*	or 1 of sent and OK.					*
*	a5 points at length of file.				*
*****************************************************************
dosendfile:
	move.l	a5,-(sp)
	graf_mouse	#2
	rsrc_gaddr	#0,#progress
	move.l	addr_out,genaddress
	form_center	genaddress

	move.l	genaddress,a0		;display the progress box
	move.l	#tftitle,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	#fromatari,12(a0)

	move.l	genaddress,a0		;display the progress box
	move.l	#tfname,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	#genbuffer,12(a0)	;filename

	move.l	genaddress,a0		;display the progress box
	move.l	#meter,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	a0,baraddress
	move.w	#2,20(a0)		;make meter small

	move.l	(sp),a5			;get pointer to length
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7	;get length
	move.l	d7,savesize

	divu	#128,d7
	swap	d7
	move.w	d7,d6
	clr.w	d7
	swap	d7
	tst.w	d6
	beq	.noadex
	addq.l	#1,d7
.noadex:
	move.w	d7,baradd


	movem.w	savebounds,d0-d3
	objc_draw	genaddress,#0,#3,d0,d1,d2,d3
	sendbyte	#'0'		;request free space
	bsr	getstring
	sendbyte	#13

	move.b	#$0a,d7			;byte to wait for
	bsr	getstringspecific
	lea	bytes,a6

	addq.l	#1,a6		;skip previous $0d
	move.l	a6,a0
	bsr	decbin
	move.l	savesize,d7
	cmp.l	d0,d7
	blt	oksizea
	form_alert	#1,#nospacealert
	move.l	(sp)+,a5
	bra	setnotsent
oksizea:
	sendbyte	#'1'
	bsr	getstring
	sendbyte	#13
	bsr	getstring
	lea	genbuffer,a5
	bsr	sendname
	bsr	getstring
	move.l	(sp)+,a5
	lea	genbuffer,a6
	bsr	xmodemup
	tst.w	d0
	bmi	setnotsent
	bsr	waiterloop
	move.w	#1,sentflag
	graf_mouse	#0
	rts

setnotsent:
	bsr	waiterloop
	move.w	#-1,sentflag
	graf_mouse	#0
	rts

waiterloop:
	sendbyte	#13
	bsr	getstring
	cmpi.b	#13,bytes
	bne	waiterloop
	rts

getstringspecific:
	lea	bytes,a6
gss:
	move.w	#1,-(sp)
	move.w	#2,-(sp)
	trap	#13
	addq.l	#4,sp
	cmp.b	d7,d0
	beq	gotit
	move.b	d0,(a6)+
	bra	gss
gotit:
	rts

sentflag:	dc.w	0
savesize:	dc.l	0
*****************************************************************
*	XMODEM stuff from here down....WOW!!			*
*****************************************************************
*****************************************************************
*	xmodemup,	pass the address of the filename	*
*	to be opened and subseqently sent to the portfolio.	*
*	address of fname=a6					*
*****************************************************************
xmodemup:
	bsr	getstring
	bsr	getstring
	bsr	getstring
	bsr	getstring



	move.w	#0,-(sp)	;read only
	move.l	a6,-(sp)
	move.w	#$3d,-(sp)
	trap	#1
	addq.l	#8,sp
	move.w	d0,handle	;save the file handle

*	lea.l	buffer128,a4	;start of buffer
	moveq	#1,d2		;block number in d2, default=1
xmodeup0:			;wait for nak from receive side
	move.w	#12,d6		;timeout
	bsr	waitser		;wait for a char for a bit?
	bmi.s	xsendblk
	cmp.b	#nak,d0		;any luck?
	bne.s	xmodeup0
bp:
	nop
	nop

* Check for End of File here...

xsendblk:
	bsr	clearbuffer
	pea	buffer128

	move.l	#128,-(sp)
	move.w	handle,-(sp)
	move.w	#$3f,-(sp)
	trap	#1
	add.l	#12,sp
	tst.l	d0
	beq	specialendup

	movem.l	d0-a6,-(sp)
	move.l	#318,d0
	mulu	d2,d0
	divu	baradd,d0
	move.l	baraddress,a0
	move.w	d0,20(a0)		;make meter small
	movem.w	savebounds,d0-d3
	objc_draw	genaddress,#meter,#3,d0,d1,d2,d3
	movem.l	(sp)+,d0-a6

resendblock:
	lea.l	buffer128,a4	;start of buffer
	bra	xsblock
specialendup:
	bra	finxup			show an end of file, if we are
*********************************************************
*	Xsblock, send a block, xmodem fashion		*
*	d2, is block number, a4 pointer to data		*
*							*
*********************************************************
xsblock:
*	clr.b	d3			checksum
	moveq	#0,d3
	move.b	#soh,d1			start of block
	bsr	sendser
	move.b	d2,d1			block number
	bsr	sendser
	move.b	d2,d1
	not.b	d1			invert it to be sure
	bsr	sendser
	move.w	#127,d4			counter for number of bytes
xmodeulp:
	move.b	(a4)+,d1		get a byte
	add.b	d1,d3			compute checksum
	bsr	sendser			send it
	dbra	d4,xmodeulp		and go back for more
	move.b	d3,d1			send checksum
	bsr	sendser
wait10s	
	move.w	#10,d6			wait 10 secs...
	bsr	waitser
	movem.l	d0-a6,-(sp)
	bsr	printbyte
	movem.l	(sp)+,d0-a6
	tst.b	d0
	bmi.s	upnogood
	cmp.b	#ack,d0			acknowledged.. whee...
	beq.s	upgonext

	cmp.b	#can,d0			cancelled, hmmm!
	beq.s	xcancel

	cmp.b	#nak,d0
	bne.s	wait10s

upnogood:
uretry:
	bra.s	resendblock
upgonext:
	addq.w	#1,d2		;all went well so send next block
	bra	xsendblk	;rts perhaps

xcancel:
	move.b	#can,d1
	bsr	sendser
	move.w	handle,-(sp)	;close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	moveq	#-1,d0
	rts
		;add to later

printbyte:
	tst.b	d0
	bpl	wasitack
	lea	timeouts,a0
	bra	printit
wasitack:
	cmp.b	#ack,d0
	bne	wasitnack
	lea	ackstring,a0
	bra	printit
wasitnack:
	cmp.b	#nak,d0
	bne	wasitcan
	lea	nackstring,a0
	bra	printit
wasitcan:
	cmp.b	#can,d0
	bne	wasitshit
	lea	canstring,a0
	bra	printit
wasitshit:
	lea	shitstring,a0
printit:
	move.l	a0,-(sp)
	move.w	#$9,-(sp)
	trap	#1
	addq.l	#6,sp
	rts
timeouts:	dc.b	'Timeout!!',0
ackstring:	dc.b	'ACK',0
nackstring:	dc.b	'NAK',0
canstring:	dc.b	'CANCELLED',0
shitstring:	dc.b	'Shit received',0

	even


finxup:
*	special last block stuff here	*
	move.b	#eot,d1			send end of file control
	bsr	sendser
finxup1:
	move.w	#10,d6
	bsr	waitser
	bmi.s	finxup

	cmp.b	#ack,d0			did they accept?
	beq.s	finxup2
	bra.s	finxup1
finxup2:
	move.w	handle,-(sp)	;close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	moveq	#1,d0
	rts
savea5:	dc.l	0
*****************************************************************
*	doxmodemdown						*
*	creates the file whose name is in genbuffer		*
*	length in a5						*
*	sent is set depending on whether file made it or	*
*	not due to size.					*
*****************************************************************
doxmodemdown:
	graf_mouse	#2
	bsr	diskfree		;get the free space on cd
	move.l	a5,savea5
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7	;get length
	cmp.l	d7,d0
	bgt	isenough
	form_alert	#1,#nospacealert
	bra	notreceived
isenough:
	move.w	#0,-(sp)
	move.l	#genbuffer,-(sp)
	move.w	#$3c,-(sp)
	trap	#1
	addq.l	#8,sp
	tst.w	d0
	bpl	cok
	form_alert	#2,#createerror
	move.w	int_out,d0
	cmpi.w	#2,d0
	beq	isenough
	bra	notreceived
cok:
	move.w	d0,handle
	

	rsrc_gaddr	#0,#progress
	move.l	addr_out,genaddress
	form_center	genaddress

	move.l	genaddress,a0		;display the progress box
	move.l	#tftitle,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	#fromport,12(a0)

	move.l	genaddress,a0		;display the progress box
	move.l	#tfname,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	#genbuffer,12(a0)	;filename

	move.l	genaddress,a0		;display the progress box
	move.l	#meter,d0
	mulu	#24,d0
	add.l	d0,a0
	move.l	a0,baraddress
	move.w	#2,20(a0)		;make meter small

	move.l	savea5,a5			;get pointer to length
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7
	lsl.l	#8,d7
	move.b	(a5)+,d7	;get length


	divu	#128,d7
	swap	d7
	move.w	d7,d6
	clr.w	d7
	swap	d7
	tst.w	d6
	beq	.noadex
	addq.l	#1,d7
.noadex:
	move.w	d7,baradd


	movem.w	savebounds,d0-d3
	objc_draw	genaddress,#0,#3,d0,d1,d2,d3


	sendbyte	#'2'
	bsr	getstring
	sendbyte	#13
	bsr	getstring
	lea	genbuffer,a5
	bsr	sendname
	bsr	getstring
	lea	genbuffer,a6
	bsr	xmodemdown
	tst.w	d0
	bmi	setnotrec
	bsr	waiterloop
	move.w	#1,sentflag
	graf_mouse	#0
	rts

setnotrec:
	bsr	waiterloop
	move.w	#-1,sentflag
	graf_mouse	#0
	rts



	rts
notreceived:
	move.w	#-1,sentflag
	rts

diskfree:
*	movem.l	d1-a6,-(sp)
	move.w	#0,-(sp)	;get info for the active drive
	move.l	#dbuffer,-(sp)
	move.w	#$36,-(sp)
	trap	#1
	addq.l	#8,sp
	moveq	#0,d0
	move.l	freeal,d0	;get the number allocation units free
	move.l	pspal,d1
	mulu	d1,d0		;mult by number of sectors per au
	move.l	bps,d1
	mulu	d1,d0		;mult by bytes per sector
*	movem.l	(sp)+,d1-a6
	rts
dbuffer:
freeal:		dc.l	0
totalal:	dc.l	0
bps:		dc.l	0
pspal:		dc.l	0

createerror:
	dc.b	'[3][An error has occured while|'
	dc.b	'trying to create a|'
	dc.b	'file on disk. Ensure the|'
	dc.b	'disk is not write-protected]'
	dc.b	'[Cancel|Retry]',0
writeerror:
	dc.b	'[3][An error has occured while|'
	dc.b	'trying to write to the disk.]'
	dc.b	'[Cancel|Retry]',0
nospacealert:
	dc.b	'[3][There is not enough space|'
	dc.b	'on the disk to accommodate|'
	dc.b	'this file.|'
	dc.b	'Try changing to another disk.][OK]',0
	even


*****************************************************************
*	Xmodem download.					*
*	writes to the already created file with handle		*
*****************************************************************
xmodemdown:
	bsr	clearbuffer
	lea	buffer128,a4
	moveq	#1,d2			block number default
* Send initial NAK
xreclp0	
	move.b	#nak,d1			send a NAK
	bsr	sendser
xreclp1a
	move.w	#12,d6			wait for block...
	bsr	waitser
	bpl.s	xrecok1			keep doing it, if nothing going
	bra.s	xreclp0
xrecok1	
	cmp.b	#soh,d0			start of block coming in?
	beq.s	xrecrcv

	cmp.b	#eot,d0			end of file?
	beq	xreceot

	cmp.b	#can,d0
	beq	xrcancel
	bra.s	xreclp1a

xrcancel:
	move.b	#can,d1
	bsr	sendser
	move.w	handle,-(sp)	;close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	moveq	#-1,d0
	rts

* Block coming in...
xrecrcv	
	move.w	#6,d6		;get block number
	bsr	waitser
	bmi	xrslowr

	cmp.b	d0,d2		;same?
	bne	xrbadblk	;was a bad, or previously-repeated block
	move.w	#6,d6		;get inverse block number
	bsr	waitser
	bmi	xrslowr
	not.b	d0
	cmp.b	d0,d2		;okay?
	bne	xrbadbl2	;definitely a bad block, not a repeat
				;Get the 128 data bytes
	move.w	#127,d4		;byte count
	clr.b	d3		;checksum
xrecrclp:
	move.w	#6,d6		;timeout for each character
	bsr	waitser
	bmi	xrecnop3	;missed a character, perhaps!?

	move.b	d0,(a4)+	;store it
	add.b	d0,d3		compute checksum
	dbra	d4,xrecrclp	loop back for more

	move.w	#6,d6
	bsr	waitser		get checksum
	bmi	xrcheckm	not good?

	cmp.b	d0,d3		checksum any good?
	bne	xrchecks

* Block was okay, so increment block number and go on...
	addq.w	#1,d2
	bsr	sendack

	movem.l	d0-a6,-(sp)
	move.l	#319,d0
	move.w	d2,d3
	subq.w	#1,d3
	mulu	d3,d0
	divu	baradd,d0
	move.l	baraddress,a0
	move.w	d0,20(a0)		;make meter small
	movem.w	savebounds,d0-d3
	objc_draw	genaddress,#meter,#3,d0,d1,d2,d3
writeblock:
	move.l	#buffer128,-(sp)
	move.l	#128,-(sp)
	move.w	handle,-(sp)
	move.w	#$40,-(sp)
	trap	#1
	add.l	#12,sp
	tst.l	d0
	bpl	wbover
	form_alert	#2,#writeerror
	move.w	int_out,d0
	cmpi.w	#2,d0
	beq	writeblock

	movem.l	(sp)+,d0-a6
	bra	xrcancel
wbover:
	movem.l	(sp)+,d0-a6
	bsr	clearbuffer
	lea	buffer128,a4		;write it out here
	bra	xreclp1a

* A character was missed from inside a block
xrecnop3
	bsr	clearbuffer
	lea	buffer128,a4	;reset it here
	bsr	waitfin
	bra	xreclp0

* Missed checksum
xrcheckm
xrchecks
	bsr	clearbuffer
	lea	buffer128,a4
	bsr	waitfin
	bra	xreclp0		and go back for more, with a Nak!

xrslowr	
	bsr	waitfin		Does not need any backtracking of a4
	bra	xreclp0

*-------------------------------------------------------------------------*
xrbadblk
	addq.b	#1,d1		check for previous block being repeated
	cmp.b	d1,d2		are they the same now?
	beq.s	xrbbb1
xrbadbl2
	bsr	waitfin		must have been a genuine bad block
	bra	xreclp0

* If previous block was repeated, then do an ACK to get it up to date
xrbbb1	
	bsr	sendack		and go back for more
	bra	xreclp1a	dont send NAK

*-------------------------------------------------------------------------*
xreceot	
	move.b	#ack,d1
	bsr	sendser
	move.w	handle,-(sp)	;close the file
	move.w	#$3e,-(sp)
	trap	#1
	addq.l	#4,sp
	moveq	#1,d0
	rts



*-------------------------------------------------------------------------*
sendack	
	move.b	#ack,d1
	bra	sendser

waitfin:
	move.w	#2,d6		wait until entire block is sent
	bsr	waitser
	bpl.s	waitfin
	rts


*********************************************************
*	Routines used by both xup and xdown here	*
*********************************************************
* Enter with d6 = to number of seconds to wait...
waitser	
	subq.w	#1,d6
waitser1
	move.w	#10000,d7		wait for char for one second
waitser2
	bsr	scanser
	dbpl	d7,waitser2
	
	bpl.s	retwait1
	
	dbra	d6,waitser1
retwait1
	rts

*-------------------------------------------------------------------------*
* Scans for input from the serial port, returns with d0=0 if nothing!
scanser	
	move.w	#1,-(sp)		Code for serial port
	move.w	#1,-(sp)		Code for Pending?
	trap	#13		Is a char waiting in serial port?
	addq.l	#4,sp		Correct Stack
	tst.l	d0		Yes...?
	bpl.s	skipser		Nope, snif!

	move.w	#1,-(sp)		Serial port code
	move.w	#2,-(sp)		Code for Get character
	trap	#13		Extended Bios function
	addq.l	#4,sp		Correct Stack
	rts			Dun!

skipser	
	moveq	#-1,d0		Signal no character there
	rts			.. and go back again

*-------------------------------------------------------------------------*
* Sends character in d1 to serial port
sendser	
	move.w	d1,-(sp)		Stack character to be sent
	move.w	#4,-(sp)		Code for SEND SERIAL
	trap	#1		GEMDOS Bios call
	addq.l	#4,sp		Correct Stack
	rts			Dun!


clearbuffer:
	lea	buffer128,a0
	moveq	#0,d0
	rept	32
	move.l	d0,(a0)+
	endr
	rts


	SECTION	DATA
mymenu:		dc.b	'  PFMasterST ',0
rsrcnamehi:	dc.b	'pfmsthi.rsc',0
rsrcnamemed:	dc.b	'pfmstmed.rsc',0
fromatari:	dc.b	'Transferring file from Atari to Portfolio',0
fromport:	dc.b	'Transferring file from Portfolio to Atari',0
	even
	SECTION	BSS
w_handle:	ds.w	1
savebounds:	ds.w	4
bdaddress:	ds.l	1
daddress:	ds.l	1
ap_id		ds.w 1
messagebuf	ds.b 16
message:	ds.b	16
mousex:		ds.w	1
mousey:		ds.w	1
dta:		ds.b	44
pathbuffer:	ds.b	64
genbuffer:	ds.b	64
*	enough space for 400 files in any directory
afilelist:
		ds.b	recsize*400
pfilelist:
		ds.b	recsize*400

selectedflag:	ds.b	1
	even
whichmachine:	ds.w	1
buffer128:	ds.b	128
handle:		ds.w	1
baraddress:	ds.l	1
baradd:		ds.w	1
savesizes:	ds.w	4
saveall:	ds.w	8
* if not linking then include the run-times
**************************************************************
	ds.l	1000			stack space
mystack	ds.w	1			(stacks go backwards)
**************************************************************
	include	aeslib.s
	include	vdilib.s
