ùúùú  6:  #ò  #ò  #ò  #ò  #ò  #ò  #ò  #ò  #ò;----------------------------------------------------------------------------
;	door main programm coded by galaxy the one and only ! ;)
;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;	all needed librarys for the door enter more for funktions
;----------------------------------------------------------------------------
forbid			=		-132
wait			=		-318
waitport		=		-384
addport			=		-354
getmsg			=		-372
putmsg			=		-366
allocsignal		=		-330
remport			=		-360
freesignal		=		-336
replymsg		=		-378
findtask		=		-294
findport		=		-390
;----------------------------------------------------------------------------

;----------------------------------------------------------------------------
;	here are all other defined offsets
;----------------------------------------------------------------------------
nodes			=		10
;----------------------------------------------------------------------------

start:
;----------------------------------------------------------------------------
;	save inputs
;----------------------------------------------------------------------------
	move.l	(a0),addressave
	move.l	d0,datasave

;----------------------------------------------------------------------------
;	find the task and save the address for later use
;----------------------------------------------------------------------------
	sub.l	a1,a1
	move.l	4.w,a6
	jsr	findtask(a6)
	move.l	d0,taskaddress

;----------------------------------------------------------------------------
;	check if it was started from shell
;----------------------------------------------------------------------------
	move.l	d0,a4
	tst.l	172(a4)
	bne.b	main

;----------------------------------------------------------------------------
;	if started from workbench, then wait for port and recive the message
;----------------------------------------------------------------------------
	lea	92(a4),a0
	move.l	4.w,a6
	jsr	waitport(a6)

	lea	92(a4),a0
	move.l	4.w,a6
	jsr	getmsg(a6)
	move.l	d0,wbstartup

main:
;----------------------------------------------------------------------------
;	here is the mainroutine with all branches. better code it with
;	an overview instead of speed and confused!
;----------------------------------------------------------------------------
	bsr.b	findnode
	bsr.b	findaeport
	bsr.b	getsignal
	bsr.b	addanewport
	bsr.w	johnhodgemessagestructure
	bsr.w	sendtext
	bsr.w	shutdoordown

checkwb:
;----------------------------------------------------------------------------
;	if started from workbench, you must reply wbstartup before exit
;----------------------------------------------------------------------------
	tst.l	wbstartup
	beq.b	exit

	move.l	4.w,a6
	jsr	forbid(a6)

	move.l	wbstartup(pc),a1
	move.l	4.w,a6
	jsr	replymsg(a6)

exit:
;----------------------------------------------------------------------------
;	exit back to amiexpress
;----------------------------------------------------------------------------
	rts

findnode:
;----------------------------------------------------------------------------
;	find the node and put nodenumber behind the ports
;----------------------------------------------------------------------------
	lea	aedoorportnode(pc),a0
	lea	doorreplyportnode(pc),a1
	lea	addressave(pc),a2

	cmp.b	#nodes,(a2)
	beq.b	nomorenodes

	move.b	(a2),(a0)
	move.b	(a2),(a1)

nomorenodes:
	rts

findaeport:
;----------------------------------------------------------------------------
;	find aedoorport and save address
;----------------------------------------------------------------------------
	lea	aedoorport(pc),a1
	move.l	4.w,a6
	jsr	findport(a6)
	move.l	d0,aedoorportaddress

	tst.l	d0
	beq.b	checkwb
	rts

getsignal:
;----------------------------------------------------------------------------
;	get a signal for the replyport
;----------------------------------------------------------------------------
	moveq	#-1,d0
	move.l	4.w,a6
	jsr	allocsignal(a6)
	move.b	d0,allocsignl

	cmp.b	#255,d0
	beq.b	checkwb
	rts

addanewport:
;----------------------------------------------------------------------------
;	add the doorreplyport to the open port list
;----------------------------------------------------------------------------
	lea	messageportstructure(pc),a1
	move.l	4.w,a6
	jsr	addport(a6)
	move.l	d0,doorreplyportaddress

	tst.l	d0
	beq.b	checkwb
	rts

johnhodgemessagestructure:
;----------------------------------------------------------------------------
;	register this shitty door into the list of open doors
;----------------------------------------------------------------------------
	lea	doorreplyport(pc),a1

	lea	messagestructure(pc),a2

	bsr.b	copytext
	bsr.b	putmsgroutine
	rts

sendtext:
;----------------------------------------------------------------------------
;	send messages to the node
;----------------------------------------------------------------------------
	lea	codername(pc),a1

	lea	messagestructure(pc),a2
	move.l	#1,220(a2)
	move.l	#4,224(a2)

	bsr.b	copytext
	bsr.b	putmsgroutine

	bsr.b	waitforport
	rts

putmsgroutine:
;----------------------------------------------------------------------------
;	put the message to the aedoorport
;----------------------------------------------------------------------------
	move.l	aedoorportaddress(pc),a0
	lea	messagestructure(pc),a1
	move.l	4.w,a6
	jsr	putmsg(a6)
	rts

copytext:
;----------------------------------------------------------------------------
;	copy text into the messagestructure
;----------------------------------------------------------------------------
	moveq	#0,d0
	add.w	#20,a2

contcopy:
	addq.b	#1,d0
	cmp.b	#199,d0
	beq.b	maxbytesreached

	move.b	(a1)+,(a2)+
	tst.b	(a1)
	bne.b	contcopy

maxbytesreached:
	rts

shutdoordown:
;----------------------------------------------------------------------------
;	deregister the node, remove the port and free the signal
;----------------------------------------------------------------------------
	lea	messagestructure(pc),a0
	move.l	#2,224(a0)

	bsr.b	putmsgroutine

	bsr.b	waitforport

	lea	messageportstructure(pc),a1
	move.l	4.w,a6
	jsr	remport(a6)

	lea	messageportstructure(pc),a0
	move.l	#$ffffffff,16(a0)
	move.l	#$ffffffff,20(a0)
	moveq	#0,d0
	move.b	allocsignl(pc),d0
	move.l	4.w,a6
	jsr	freesignal(a6)
	rts

waitforport:
;----------------------------------------------------------------------------
;	wait for the signals and get the message
;----------------------------------------------------------------------------
	moveq	#0,d1
	move.b	allocsignl(pc),d1
	moveq	#1,d0
	asl.l	d1,d0
	move.l	4.w,a6
	jsr	wait(a6)

	lea	messageportstructure(pc),a0
	move.l	4.w,a6
	jsr	getmsg(a6)
	rts

;----------------------------------------------------------------------------
;	here is the messageportstructure
;----------------------------------------------------------------------------
messageportstructure:
	dc.l	0			;ln_succ	;auto created
	dc.l	0			;ln_pred	;auto created
	dc.b	4			;ln_type	;messageport = 4
	dc.b	0			;ln_pri		;priority = 0
	dc.l	doorreplyport		;ln_name	;name of the port
	dc.b	0			;mp_flags	;flags

allocsignl:
	dc.b	0			;mp_sigbit	;sigbits

taskaddress:
	dc.l	0			;mp_sigtask	;sigtasks
	dc.l	0			;mp_msglist	;auto created
	dc.l	0			;\  fuck i realy don't know what to
	dc.l	0			; > put here but without this the
	dc.w	0			;/  machine will reset

;----------------------------------------------------------------------------
;	here is the messagestructure
;----------------------------------------------------------------------------
messagestructure:
	dc.l	0			;ln_succ	;auto created
	dc.l	0			;ln_pred	;auto created
	dc.b	5			;ln_type	;message = 5
	dc.b	0			;ln_pri		;priority = 0
	dc.l	0			;ln_name	;name of the message
	dc.l	messageportstructure	;mn_replyport	;name of the replyport
	dc.w	236			;mn_length	;length of the message
	dcb.b	200,0			;info buffer	;string maximum length
	dc.l	0			;int data	;r/w & result indicator
	dc.l	1			;int command	;command send from door
	dc.l	-1			;int nodeid	;reserved
	dc.l	0			;int linenum	;reserved

;----------------------------------------------------------------------------
;	here stands what the door gets with it's start!
;----------------------------------------------------------------------------
addressave:
	dc.l	0

datasave:
	dc.l	0

;----------------------------------------------------------------------------
;	here is the wbstartup structure address if using tooltypes
;----------------------------------------------------------------------------
wbstartup:
	dc.l	0

;----------------------------------------------------------------------------
;	aedoorport. address and node id
;----------------------------------------------------------------------------
aedoorport:
	dc.b	"AEDoorPort"

aedoorportnode:
	dcb.b	2,0			;buffer for node number

aedoorportaddress:
	dc.l	0

;----------------------------------------------------------------------------
;	doorreplyport. address and node id
;----------------------------------------------------------------------------
doorreplyport:
	dc.b	"DoorReplyPort"

doorreplyportnode:
	dcb.b	2,0			;buffer for node number

	even				;don't remove this even
doorreplyportaddress:
	dc.l	0

;----------------------------------------------------------------------------
;	codername ;)
;----------------------------------------------------------------------------
codername:
	dc.b	"Start Door Coded",10
	dc.b	"By Galaxy The One And Only",0
