;
;	NAME:  FNDSPD
;
;	FUNCTION:  This program is run to determine what speed an incoming
;	terminal is set at.  It does this by varying the port speed, asking
;	the terminal operator to enter Carriage Returns, and then trying to
;	read them.  If it finds a valid CR character, it assumes that it is
;	at the right speed.  The only serial I/O board supported is the
;	AM300.  The only speeds supported are 300 and 1200.  The line is
;	is originally set to 1200 baud.
;
;	AUTHOR:  Tom Dahlquist
;
;	HISTORY:
;	  When   Who What
;	04/26/84 TAD Written.
;
	SEARCH	SYS
	SEARCH	SYSSYM
	SEARCH	TRM

	VMAJOR=1
	VMINOR=0
	VEDIT=0

FNDSPD:	PHDR	-2,PV$RSM!PV$WSM,PH$REU!PH$REE
	GETIMP	4,A0
	CLR	@A0

	JOBIDX	A5			; A5 -> JCB...
	MOV	JOBTRM(A5),A5		; A5 -> TDB...
	MOV	2(A5),A1		; A1 -> IDV control block...
	SUB	#4,A1			; A1 -> IDV name...
	CMM	@A1,AM300		; right board?
	BEQ	BRDOK			; br if so, else
	TYPECR	<?Only AM300 ports supported.>
	EXIT
BRDOK:	ORW	#T$IMI,@A5		; set image mode...
	MOVW	#10,D0			; start at 1200..
	CALL	SETSPD
CLRLP:	TCKI				; check for input...
	BNE	WAIT			; go wait for some if none, else
	KBD				; clear out buffer.
	BR	CLRLP
WAIT:	KBD				; wait for a character...

;
; Loop changing the baud rate and waiting for a CR.
;

LOOP:	NOTW	@A0			; flip switch...
	BEQ	1$
	MOVW	#10,D0			; set 1200 baud...
	BR	2$
1$:	MOVW	#6,D0			; set 300 baud...
2$:	CALL	SETSPD

	TYPECR	<Hit a Carriage Return and wait-->
	MOVW	#2,D5			; for loop...

ILOOP:	SLEEP	#10000.			; wait 1 second,
TCKI:	TCKI				; anything in buffer?
	BNE	ILPE			; go sleep again if not, else
	KBD				; get it and see if it's
	CMPB	D1,#15			; a CR...
	BEQ	EXIT			; if so, all done...
	BR	TCKI			; else see if any more.
ILPE:	DBF	D5,ILOOP		; do this N times, then fall thru
	JMP	LOOP			; and do the switch again.

EXIT:	TYPE	<Speed set at >
	TSTW	@A0
	BEQ	1$
	TYPE	<1200>
	BR	2$
1$:	TYPE	<300>
2$:	TYPECR	< baud.>
	EXIT

SETSPD:	SUPVR				; set supervisor state and
	SVLOK				; disable interrupts.

	MOV	T.IHW(A5),A4		; A4 -> I/O port...
	MOV	T.IHM(A5),D3		; get port number
	ANDB	#7,D3			; for MUX reg...
	ORB	#10,D3			; set baud rate change bit...
	MOVB	D3,4(A4)		; and send to MUX reg.
	MOVB	D0,@A4			; send baud rate.
	ANDB	#^C10,D3		; turn off baud rate change bit...
	MOVB	D3,4(A4)		; select ASTRO...
	MOVB	#11,1(A4)		; set standard stuff...
	MOVB	#205,@A4
	MOVB	#20,4(A4)		; enable interrupts...
	SVUNLK				; enable interrupts...
	SSTS	2(A0)			; get current processor status,
	ANDW	#157777,2(A0)		; turn off supervisor state, and
	LSTS	2(A0)			; reload it.
	RTN

AM300:	RAD50	/AM300/

	END