;<FOONEX>CHATTY.MAC;1 20-Mar-81 00:10:39, Edit by MMCM
; INTERFACES FOR CHAOSNET FROM NVT CODE

	SEARCH PROLOG,STENEX
	SEARCH MONSYM,MACSYM
	TITLE CHATTY

EXTERN CHAFLG,CTMSTM,CVTCLZ,CHNGTM,ASGPK1,RELPKM,SNDPK1
EXTERN CHPKIA,CHAQGF,CHATTW,CHPMXT,CHAVSO
EXTERN TTNETW,CKNNVT,NVTXGA,NVTCHS,TTCOBI
EXTERN NVTSTD,NVTSTP,TODCLK,NEGTM0,NEGCH1,DETCVT
EXTERN NVTRRR,NETTEO,NETTOO,NVTOPF,PBRCNT,TTECT,TTOCT,TTRLOB

INTERN CVTDET,CVTCAP,CHANEG,CHATCS,CVTUPI,PTCONN
INTERN CHKCVT

DEFINE RCALL (RTN,R) <		;;;USED FOR DEBUGGING: CALL ROUTINE WITH 'REASON'
IFN CHSDBG,<MOVX CX,R>
	CALL RTN
>

PTCONN:	POINT 18,TTNETW(T2),35	;CHAOSNET CONNECTION FOR THIS TTY

;IS THIS LINE A CHAOSNET TTY?
CHKCVT:	ACVAR <W1>
	MOVSI W1,NVTCHS
	TDNN W1,TTNETW(T2)
	 RET
	RETSKP

;CLOSE A CHAOSNET TTY CONNECTION
CVTDET:	SAVEQ
	LDB Q1,PTCONN
	JRST CVTCLZ

;CALLED ON INPUT WAIT
CVTCAP:	PUSH P,T2
	NOINT			; Protect any possible ilocks
	CALL CKNNVT		; If not new nvt
	 JRST CVTCA1		; Skip sending ga
	CALL NVTXGA		; SEND GA IF NEEDED
CVTCA1:	OKINT
	POP P,T2
	RET

;CHECK FOR OVERDUE NEGOTIATIONS FROM CHAOS BACKGROUND FORK
CHANEG:	MOVE T1,TODCLK		;GET NOW
	CAMG T1,CHNGTM		;IS OUR TIME RIPE?
	 RET			;NOPE, GET OUT
	ADD T1,[NEGTM0]		;ADD TIME OUT QUANITY
	MOVEM T1,CHNGTM		;SAVE TIME FOR NEXT CHECK
	JRST NEGCH1		;JOIN NVT CODE

;CHATCS - MOVE TTY OUTPUT TO NET BUFFERS
;T2/ LINE NUMBER

RCTOPT==7			;RCTE OPTION
WILOPT==10			;OFFSET FOR REQUESTS
CHATCS:	MOVEI T3,(1B<RCTOPT+WILOPT>)
	TDNE T3,NVTOPF(T2)
	CALL CKNNVT
	 JRST CHATC7		;NO RCTE
	LDB 3,PBRCNT		; NEED TO SEND RESET?
	SKIPE T3		;NEED TO SEND RESET?
	 CALL NVTRRR		;YES, TRY TO SEND
CHATC7:	SKIPGE XTTFLG##(2)	;IF SCREEN FREEZE IS ON FOR THIS LINE
	 RET			;THEN WAIT FOR USER TO UNFREEZE IT
CHATC8:	SKIPG TTNETW(T2)	;STILL CONNECTED?
	 JRST TTCOBI		;NO, JUST CLEAR BUFFER
	LDB Q1,PTCONN		;GET CONNECTION INDEX
	HRRZ FX,CHASTA(Q1)	;GET CONNECTION STATUS
	CAIE FX,.CSOPN		;STILL OPENED?
	 JRST TTCOBI		;NO, FORGET OUTPUT
IFN CHSDBG,<			;WHEN DEBUGGING,
	MOVE T4,TODCLK		; NOTE WE'VE DONE SOMETHING FOR THIS FELLOW
	MOVEM T4,CHATTM(Q1)	; AT THIS TIME WRT TTY OUTPUT
>;CHSDBG
	MOVE T1,TTOCT(T2)	;GET NUMBER OF BYTES TO BE SENT OUT
	ADD T1,TTECT(T2)	;INCLUDE THOSE IN ECHO BUFFER
	CAMLE T1,CHPMXT
	 MOVE T1,CHPMXT		;NO MORE THAN THIS AT A TIME THOUGH
	ADDI T1,3		;CONVERT TO WORDS, ROUNDING UP
	LSH T1,-2
	PUSH P,T1
	MOVEI Q2,.CODAT		;DATA PACKET
	PUSH P,T2		;SAVE TTY NUMBER
	SKIPG CHANOS(Q1)	;ROOM WITHIN CURRENT WINDOW SIZE FOR ANOTHER PACKET?
	 JRST CHATC9		;NO
	RCALL ASGPK1,63		;CAN ALLOCATE A PACKET NOW?
CHATC9:	 JRST [POP P,T2		;+++NO, FORGET IT (SHOULDN'T WE REMEMBER WE DIDN'T)?
	       POP P,T1
	       RET]
	MOVEI Q2,CHPKDT(FX)
	HRLI Q2,441000		;MAKE BYTE POINTER TO DATA PORTION OF PACKET
	POP P,T2		;GET BACK TTY NUMBER
	POP P,P1		;NO MORE THAN THIS AT A TIME THOUGH
	LSH P1,2
	SETZ T4,		;INIT CHAR COUNT
CHATC2:	SOJL P1,CHATC5		;COUNT DOWN LIMIT
	NOSKED
	SKIPE TTECT(2)
	 JRST [	CALL NETTEO
		JRST CHATC3]
	SKIPE TTOCT(2)
	 JRST [	CALL NETTOO
		JRST CHATC3]
	PUSH P,T3
	CALL TTRLOB
	POP P,T3
	OKSKED

CHATC5:	JUMPE T4,[RCALL RELPKM,1 ;DONE IF NO OUTPUT GENERATED
		RET]
	STOR T4,CPKNB,(FX)	;PACKET BYTE COUNT
	PUSH P,T2		;SAVE TTY NUMBER
	SETO T1,
	CALL SNDPK1		;QUEUE IT TO BE SENT OFF, BUT DON'T BLOCK
IFN CHSDBG,<			;WHEN DEBUGGING,
	MOVE T4,TODCLK		; NOTE WE'VE SUCCESSFULLY DONE SOMETHING
	MOVEM T4,CHATSM(Q1)	; AT THIS POINT
>;CHSDBG
	POP P,T2		;GET BACK TTY NUMBER
	SKIPN TTECT(T2)
	 SKIPE TTOCT(T2)	;SEE IF ANY MORE OUTPUT TO BE GENERATED
	 JRST CHATC8	
	RET

CHATC3:	OKSKED
	IDPB T1,Q2		;YES. PUT CHAR IN OUR PACKET
	AOJA T4,CHATC2

;CVTUPI - UNPACK MESSAGE INTO TTY BUFFERS

;ACCEPTS
;	T2/ NVT index
;	Q1/ CONNECTION POINTER

CVTUPI:	STKVAR<CVUPTT,CVUPKT>
	MOVEM T2,CVUPTT		;SAVE TTY NUMBER
CVTUPP:	HLRZ FX,CHAIBF(Q1)	;GET NEXT INPUT PACKET
	JUMPE FX,R		;NO MORE
	CALL CHPKIA		;HANDLE ACKNOWLEDGEMENT IF DATA PACKET
	MOVEI Q2,CHAIBF(Q1)
	CALL CHAQGF		;REMOVE PACKET FROM QUEUE
	SOS CHANBF(Q1)		;ONE LESS ON QUEUE
	MOVEM FX,CVUPKT		;SAVE PACKET
	SKIPL (FX)		;NON-DATA PACKET?
	 JRST CVTUPR		;YES, JUST FLUSH IT
	LOAD Q2,CPKNB,(FX)	;GET BYTE COUNT OF PACKET
	MOVEI FX,CHPKDT(FX)
	HRLI FX,441000		;BYTE POINTER TO DATA
CVTUPL:	SOJL Q2,CVTUPR		;COUNT CHARS LEFT IN PKT
	ILDB T1,FX		;GET CHARACTER
	MOVE T2,CVUPTT		;GET TTY NUMBER AGAIN
	LDB T3,NVTSTP		;GET THE CURRENT COMMAND STATE OF THIS
	SETZ T4,
	DPB T4,NVTSTP
	PUSH P,FX		;SAVE BYTE POINTER
	PUSH P,Q2		;AND BYTE COUNT
	CALL @NVTSTD(T3)	;DISPATCH ON IT
	POP P,Q2		;GET BACK BYTE COUNT
	POP P,FX		;AND BYTE POINTER
	JRST CVTUPL		;DO NEXT CHARACTER

CVTUPR:	MOVE FX,CVUPKT		;GET BACK PACKET
	RCALL RELPKM,2		;DONE WITH THIS PACKET
	MOVE T2,CVUPTT		;GET TTY AGAIN
	JRST CVTUPP		;GO SEE IF MORE TO COME

	END
