	.title	DUP
	.ident	/8.0.01/
	.sbttl	definitions
;
;	connect code from kermit set to do connect only to the PDP11/45
;	F. Borger	March 26, 1987
;
;
; This is the terminal emulator for Kermit-11 on native RSX.  It was
; rewritten because the original version developed on the RSTS emulator
; was unsuitable for native RSX.  It wouldn't work reliably at baud
; rates over 1200 on a moderately busy system.
;

	lun.ti	= 6			; ti is actually other line
	lun.co	= 5			; co is rsx ti
	ef.kil	= 11.			; event flag for kill qio
	ef.wri	= 12.			; event flag for writes
	ef.10t	= 13.			; event flag for mark times
	em.10t	= 10000			; event mask for mark times
	ef.rem	= 14.			; Remote event flag
	em.rem	= 020000		; Remote event flag mask
	ef.loc	= 15.			; Local event flag
	em.loc	= 040000		; Local event flag mask
	cu	= 103			; ascii C
	cl	= 143			; ascii c
	ctlbs	= 34			; control backslash

	.sbttl	dpb`s
	.psect	$idata	rw,d,lcl,rel,con

	.mcall	qio$	qiow$	qiow$s	dir$	wtlo$s
	.mcall	exit$s	rdaf$s	clef$	cmkt$	mrkt$
;
;	QIO DPB's
;
remread:		; Read up to 20 characters (10 tick timeout)
	qio$	<io.rne!tf.ral>,lun.ti,ef.rem,,remios,,<rembf,20.>
locread:
	qio$	<io.rne!tf.ral>,lun.co,ef.loc,,locios,,<locbf,1>
remwrite:
	qiow$	io.wal,lun.ti,ef.loc,,,,<locbf,1>
locwrite:
	qiow$	io.wal,lun.co,ef.wri,,,,<rembf,1>
meswrite:
	qiow$	io.wvb,lun.co,ef.wri,,,,<mess1,len1,40>
att42:
	qiow$	io.att,lun.ti,ef.rem	; attach remote
attco:
	qiow$	io.att,lun.co,ef.rem	; attach local
det42:
	qiow$	io.det,lun.ti,ef.rem	; detach remote
detco:
	qiow$	io.det,lun.co,ef.rem	; detach local
kil42:
	qiow$	io.kil,lun.ti,ef.kil	; Kill incoming I/O
kilco:
	qiow$	io.kil,lun.co,ef.loc	; Kill incoming I/O
;
;	save/restore terminal characteristics dpb's and buffers
;
savatt:					; save users term characteristics
	qiow$	sf.gmc,lun.co,ef.wri,,,,<savco,savlen>
savco:	.byte	TC.FDX,0		; Original setting for local
	.byte	TC.BIN,0
	savlen=.-savco
savti:	.byte	TC.FDX,0		; Original setting for remote
	.byte	TC.BIN,0
setatt:					; set users term characteristics
	qiow$	sf.smc,lun.co,ef.wri,,,,<setco,setlen>
setco:	.byte	TC.FDX,1		; new setting for local
	.byte	TC.BIN,1
	setlen=.-setco
resatt:					; restore users term characteristics
	qiow$	sf.smc,lun.co,ef.wri,,,,<savco,savlen>
;
;	mark time stuff
;
clerem:	clef$	ef.rem			;clear remote read flag
cle10t:	clef$	ef.10t			;clear 10 tick flag
can10t:	cmkt$	ef.10t			;cancle 10 tick mark time
mrk10t:	mrkt$	ef.10t,10.,1		;10. tick mark time

	.sbttl	buffers and misc data
;
;	QIO buffers and status blocks
;
rembf:	.blkb	256.			; Remote data buffer
locbf:	.blkb	2			; Local data buffer
locios:	.word	0,0			; local io status buffer
remios:	.word	0,0			; remote io status buffer
;
;	initializing messages
;
mess1:	.ascii	?Connecting to the PDP11/45?	; Print id
	len1=.-mess1
mess2:	.ascii	?Type CTRL-\ and then C to return to the 11/44?
	len2=.-mess2
	.even
;
;	misc data
;
prvchr:	.word	0			; last character sent
efbuf:	.blkw	4			; Event flags buffer

	.sbttl	Setup code
	.psect	$code
	.enabl	lsb
;
;	do an identifying message
;
DUP:	
	dir$	#meswrite		;write first message
	mov	#mess2,meswrite+q.iopl
	mov	#len2,meswrite+q.iopl+2	;update things
;
;	attach terminals
;	and save characteristics, then set new ones
;
	dir$	#meswrite		;write 2nd message
	dir$	#att42			; attach remote
	dir$	#attco			; attach local
	dir$	#savatt			; save current characteristics
	mov	#lun.ti,savatt+q.iolu	; change to remote lun
	mov	#savti,savatt+q.iopl	; and that save area
	dir$	#savatt			; save remote attributes too
	dir$	#setatt			; set fdx and binary at local
	mov	#lun.ti,setatt+q.iolu	; change lun on set code
	dir$	#setatt			; set remote too
;
;	prime the io and set up timeout
;
	dir$	#remread		; Prime incoming and outgoing streams
	dir$	#locread
	dir$	#mrk10t			; and set 15 tick timeout

	.sbttl	main loop
;
; Main loop, shuffle IO until escape sequence is detected
;
20$:
	wtlo$s	0,#<em.loc!em.rem!em.10t>	; wait for i/o or timeout
	rdaf$s	#efbuf				; Read the event flags
	bit	#em.rem,efbuf+0			; did we receive anything ?
	beq	25$				; br if not
	dir$	#can10t				; cancle mark time
	dir$	#cle10t				; clear the flag
	movb	remios,r5			; get stat of read in r5
	call	iocheck				; and check for errors
	bcs	100$				; scram if bad error occurred
	mov	remios+2,r0			; get size read
	clr	remios+2			; and clear buffer
	br	30$
						; Handle remote read timeout
25$:	bit	#em.10t,efbuf+0			; read time out ?
	beq	40$				; (no)
	dir$	#cle10t				; clear the mark time flag
	dir$	#kil42				; kill outstanding read
				; status will be io.kil, and partial read will
				; be read into buffer
	dir$	#clerem				; and clear that flag
	mov	remios+2,r0			; get size of read
	clr	remios+2			; clear indicator
;
;	do local terminal write
;
30$:	tst	r0				; check size of read
	beq	35$				; br if nothing there
	mov	r0,locwrite+q.iopl+2		; put size in dpb
	dir$	#locwrite
35$:	dir$	#remread			; re-issue read
	dir$	#mrk10t				; and re-issue mark time
;
; Handle characters on outgoing (Local input) stream
;
40$:	bit	#em.loc,efbuf+0			; Anything typed locally?
	beq	20$				; (no, loop back)
	movb	locbf,r1			; r1 = just typed character
	bic	#200,r1				; drop bit 7 if mark set (BDN)
	cmpb	r1,#cu				; upper case c ?
	beq	45$				; br if yes
	cmpb	r1,#cl				; 2nd char of escape ?
	bne	50$				; (no)
45$:	cmpb	prvchr,#ctlbs			; was prev char a ^\ ?
	bne	50$				; skip if not
	jmp	100$				; exit cpu
50$:	dir$	#remwrite			; Transmit character to remote
	movb	r1,prvchr			; and save it for next test
	dir$	#locread			; Re-issue local read
	jmp	20$				; Loop back

	.sbttl	Clean Up and Exit

;
; Exit CONNECT mode
;
100$:


rsxrst:	dir$	#kil42			; kill remote io read
	dir$	#kilco			; kill local read
	dir$	#det42			;detach remote
	dir$	#detco			;detach local
	dir$	#resatt			; restore local attributes
	mov	#lun.ti,resatt+q.iolu	; change lun of dir
	mov	#savti,resatt+q.iopl	; and storage area
	dir$	#resatt			; restore remote attributes
	exit$s				; bye

iocheck:mov	r0	,-(sp)		; insure this is saved
	tstb	r5			; sucessesful read qio ?
	bpl	180$			; yes
	mov	#200$,r0		; no point to allowable errors
170$:	cmpb	r5,(r0)+		; found allowable error ?
	beq	180$
	tstb	(r0)			; any more to try ?
	bne	170$			; yes, try for another
	sec				; failure, exit to command level
	br	190$			; bye
180$:	clc				; success, stay in connect code
190$:	mov	(sp)+	,r0		; restore old r0 please
	return

200$:	.byte	IE.BCC	,IE.DAO	,IE.IES	,IE.NOD	,IE.PES	,IE.VER	,0
	.even
	.end	DUP
