

	.STITL TELETYPE AND TYI
;THINGS TO DO --
;DIFFERENT TTY TYPES

	VERN==VERN+%FNAM2



.MACR	IFL	FLG,EXPR	;ONE LINE COND IF FLG .NE. 0
.XLIST
	.IFNZ	FLG
	.LIST
	EXPR
	.XLIST
	.ENDC
.LIST
.ENDM

.MACR	IFN	FLG,EXPR	;ONE LINE COND IF FLG=0
.XLIST
	.IFZ	FLG
	.LIST
	EXPR
	.XLIST
	.ENDC
.LIST
.ENDM

.MACR	TIPNTR	TOP,SZ,TMS	;MAKES MNUSRS+TMS  PNTRS
.XLIST
	.ZZZ==0
	.REPT	MNUSRS+TMS
		TOP+<SZ*.ZZZ>
		.ZZZ==.ZZZ+1
	.ENDR
.LIST
.ENDM


;VARIOUS FLAGS AND TELETYPE VARIABLES

	;FLAGS TTY STATUS
TYIDR==100000
TIDW==40000	;TYI DISK WRITE IN PROCESS
TIDR==20000	;TYI DISK READ IN PROCESS
TIEDR==10000	;EDIT DSK READ
TIEDW==4000
TIDTRA==TYIDR!TIDW!TIDR!TIEDR!TIEDW	;DISK TRANSFER
TIBR==2000	;^G (BREAK) TYPED
TIED==1000		;IN EDIT MODE
FCQF==200	;CNTRL Q

NTY==1
NFTY==0
NKLTY==0
NFKLTY==2*NTY
NDCTY==0
NFDCTY==2*<NTY+NKLTY>
NDPTY==0
NFDPTY==2*<NTY+NKLTY+NDCTY>
NDMTY==7
NFDMTY==2*<NTY+NKLTY+NDCTY+NDPTY>
NTTY==NTY+NKLTY+NDCTY+NDCTY+NDPTY+NDMTY

	.STITLE	TELETYPE BUFFERS AND VARIABLES
	;BUFFERS
.EVEN
	TITQSZ==6	;SIZE OF TEMP TELETYPE INPUT BUFFER,
TITQ:	.=.+<TITQSZ*NTTY>	;TEMP TYI INT BUFFERS
	TIBFSZ==100	;TYPE IN BUFFER+EDIT BUF SIZE
TIBF:	.=.+<TIBFSZ*NTTY>	;TYPE IN AND EDIT BUFFERS
	TOBFSZ==40	;TYPE OUT BUFFER SIZE
TOBF:	.=.+<TOBFSZ*NTTY>	;TYPE OUT BUFFERS
;TYPE OUT INFO
CHARNO:	.=.+<NTTY*2>
LINEL:
.XLIST
.REPT NTTY
	60.
.ENDR
.LIST

;BUFFER PNTRS
	;TEMP TYPE IN QUEUE
TITQT:	TIPNTR	TITQ,TITQSZ,1	;TEMP Q TOP AND BOTTOM
TITQP:	.=.+<2*NTTY>	;NXT TO BE PROCESSED
TITQL:	.=.+<2*NTTY>	;TEMP Q LAST IN
TITQN:	.=.+<2*NTTY>
	;TYPE IN BUFFERS
TIBT:	TIPNTR	TIBF,TIBFSZ,1	;TYI BUFFER TOP (DOUBLE AS EDIT BUF BOTTOM)
TIBP:	.=.+<2*NTTY>
TYIBP:	.=.+<2*NTTY>	;SAME AS ABOVE,USED BY TYI
TITN:	.=.+<NTTY*2>
TYITN:	.=.+<NTTY*2>
TIBL:	.=.+<2*NTTY>	;LAST+1 THESE PNTRS CHANGE IN EDIT MODE
TIPC:	.=.+<2*NTTY>
TISVD:	.=.+<2*NTTY>
	;EDIT BUFFERS
	EDBFSZ==40	;EDIT BUFFER SIZE
TIEBT:	TIPNTR	TIBF+EDBFSZ,TIBFSZ,0	;EDIT BUF TOP
TIEBP:	.=.+<2*NTTY>	;PNTR INITIALLY PAST BOTTOM
	;TYPE OUT BUFFERS
TOBT:	TIPNTR	TOBF,TOBFSZ,1	;TOP & BOTTOM
TOBFF:	.=.+<2*NTTY>
TOBP:	.=.+<2*NTTY>	;NEXT CHAR
TOTN:	.=.+<NTTY*2>
TOPC:	.=.+<NTTY*2>
TOSVC:	.=.+<NTTY*2>
TOSVD:	.=.+<NTTY*2>
TODING:	.=.+<NTTY*2>
TOSND:	.REPT	NTY
	CNSND
.ENDR
.REPT	NDMTY
	DMSND
.ENDR
	;DISK BUFFER POINTERS
TILD:	.=.+<NTTY*2>
TIHD:	.=.+<NTTY*2>
TIDP:	.=.+<NTTY*2>	;# OF WDS IN TYI DSK BUFFER USED BY INT ROUTINES
TYIDP:	.=.+<NTTY*2>	;DITTO USED BY TYI
TYIFDP:	.=.+<NTTY*2>	;# OF WDS TRANSFERREDFROM DSK
	;EDIT BUFFER
TIELD:	.=.+<NTTY*2>
TIEHD:	.=.+<NTTY*2>
TIEDP:	.=.+<NTTY*2>	;# OF WDS TRANSFERRED TO DSK
TIEFDP:	.=.+<NTTY*2>	;# OF WDS TRANSFERRED FROM

TTYBR:	.=.+<NTTY*2>
TTYEBR:	.=.+<NTTY*2>
	;TTY STATUS
TTYST:	.=.+<NTTY*2>

	;USER INDEX
TTYU:	.=.+<2*NTTY>

	DMBIT=.-NFDMTY
		;BAR BIT FOR TTY
	.ZZZ==1
.XLIST
.REPT	2*NDMTY
	.ZZZ
	.ZZZ==.ZZZ_1
.ENDR
.LIST

	.STITLE	CHAR DISPATCH TABLES

	;TYI DISPATCH TABLES
CTIS:		;SPECIAL CHARACTERS -- DTBL (CHAR) HAS FSF ON
	TICC	;^C COPIES NEXT CHAR
	TICD	;^D DELETES NEXT CHAR
	TICZ	;^G BREAK
	TICM	;^M CARRIAGE RETURN -- POSSIBLE END OF LINE
	TICN	;^N COPIES NEXT WORD
	TICQ	;^Q QUOTES NEXT CHARACTER
	TICR	;^R COPIES REST OF EDIT BUFFER
	TICS	;^S SKIPS (DELETES) NEXT WORD
	TICW	;^W DELETES PREVIOUS WORD
	TGTC2	;^X CLARIFY INPUT
	TICY	;^Y RECOVER PREVIOUS LINE
	TICZ	;^Z EMPTIES TYPE IN BUFFER
	TIDEL	;RBO RUBOUT

CTIQ:	;QUOTING CHARS
	TIOBR	;[
CTIUQ:	TICBR	;]
	TIOBR

		;TYO DISPATCH TABLE FOR CHARS WITH FOF
CTOS:	TOCB	;^B ECHOES AS SPACE
	TOCH	;^H BACKSPACE
	UTYO4	;^G BELL
	TOCM	;^M CARRIAGE RETURN
	TOCI	;^I TAB
	UTYO4	;^J LINEFEED
	UTYO4	;^K VERT TAB
	UTYO4	;^L FORMFEED


TINIT:	CLR	U
	MOV	#7,BRKCHR
	MOV	#SIMTST,E
	CMP	E,(E)+
	BNE	TINIT2
	MOV	#77,CLKBDM+2	;USING SIMULATOR
	MOV	#30,BRKCHR
	MOV	#TICZ,CTIS+22
	INC	U
TINIT2:	MOV	U,(PC)+
SIMTST:	SIMTST
	CLR	U
	CLR	E
TINIT1:	JSR	PC,TTYRST
	MOV	E,UTTY(U)
	MOV	U,TTYU(E)
	MOV	DTYIBF(U),TILD(E)
	MOV	DTYIBF+2(U),TIHD(E)
	MOV	DEDTBF(U),TIELD(E)
	MOV	DEDTBF+2(U),TIEHD(E)
	TST	(E)+
	ADD	#LUBLK,U
	CMP	E,NUSER2
	BLT	TINIT1

	MOV	#JTYI,TYI+2
	MOV	#JTYO,TYO+2
	MOV	#CRLF,.CRLF+2
.IFNZ NTY
	MOV	#CNSQ,TKBRV
	MOV	#CNINT,TPBRV
	MOV	#100,TKS
	MOV	#100,TPS
.ENDC
	MOV	#DMCA+NFDMTY,DMTBR
	MOV	#DMTT,TTP
	MOV	#DMINT,DMTBRV
	MOV	#CLKBDM,CLKBR1+2	;TYPE IN AT CLOCK LEVEL
	MOV	#10001,DMCSR	;TRANSMIT INTS ONLY
	JMP	NEXTUS


;ACS (IN GENERAL)
;E -- TTY*2
;D-- CHARACTER
;C -- CHARACTER DISPATCHING
;B&A -- VARIOUS HACKS, USUALLY NOT USED
;U -- NOT USED, HOPEFULLY WILL CONTAIN USER

;TTYRST --
;CALL THIS WITH TTY IN E TO RESET INDVIDUAL TTY
TTYRST:	MOV	TITQT(E),TITQP(E)	;TEMP Q PNTRS
	MOV	TITQT(E),TITQL(E)
TTYRS1:	MOV	TITQL(E),TITQP(E)
	CLR	TITQN(E)
TTYRS2:	BIS	#TIBR,TTYST(E)
	CMP	TOSND(E),#DMSND
	BNE	TTYRS4
	BIC	DMBIT(E),DMTRAN
	BIC	DMBIT(E),DMBAR
	CLR	DMWC(E)		;TYPE OUT STUFF
	MOV	TOBT(E),DMCA(E)
TTYRS4:	MOV	TOBT(E),TOBP(E)
	MOV	TOBT(E),TOBFF(E)
	CLR	CHARNO(E)
	CLR	TOPC(E)
	CLR	TOTN(E)
	CLR	TODING(E)
	BIT	#TIDTRA,TTYST(E)
	BNE	TTYRS3
	BIC	#TIBR,TTYST(E)
	CLR	TYIDP(E)
TTYRS3:	BIC	#TIED!277,TTYST(E)	;INPUT STUFF
	MOV	TIBT+2(E),TIBL(E)
	MOV	TIBT(E),TIBP(E)	;TYI BUF
	MOV	TIBT(E),TYIBP(E)
	MOV	TIBT+2(E),TIEBP(E)	;EDIT BUFFER
	CLR	TIDP(E)		;DSK WD CNT
	CLR	TIEDP(E)
	CLR	TYIFDP(E)
	CLR	TIEFDP(E)
	CLR	TITN(E)
	CLR	TTYBR(E)
	CLR	TTYEBR(E)
	RTS	PC

;EDITA--
;CALL TO START EDIT
EDITA:	BIS	#TIED,TTYST(E)
	MOV	TIEBT(E),TIBL(E)	;USE 1/2 TYI BUFFER FOR EDIT BUF
	MOV	TIEBT(E),TIEBP(E)
	RTS	PC

;EDITZ--
;CALL TO END EDIT
EDITZ:	BIC	#TIED,TTYST(E)
	MOV	TIBT+2(E),TIBL(E)	;USE EDIT BUF FOR TYI AGAIN
	RTS	PC

	.STITLE TYPE IN
;TISQ --
;MOVE CHARS FROM TUMBLE TABLE TO TEMPORARY TYPE IN QUEUES
TISQ:	BIC	#177600,D
TISQ1:	MOV	TTYU(E),U
	CMP	D,BRKCHR
	BEQ	BREAK
	CMP	FLSRES(U),#FRTYIH
	BNE	TISQ3
	CMP	TITQN(E),#TITQSZ
	BGE	DING
TISQ2:	MOVB	D,@TITQL(E)
	INC	TITQN(E)
	INC	TITQL(E)
	CMP	TITQL(E),TITQT+2(E)
	BLT	TISQ3
	MOV	TITQT(E),TITQL(E)
TISQ3:	RTS	PC

;BREAK
BREAK:	CMP	FLSRES(U),#FRTYIH
	BEQ	BREAK1
	BIS	#TIBR,TTYST(E)
	MOV	PC,BRAKE
	RTS	PC
BREAK1:	JSR	PC,TTYRS2
	BR	TISQ2


;BRAKR -- TO EXECUTE BREAK WHEN NOT IN TYIH
BRAKR:	TST	JDCSW
	BEQ	TISQ3
	MOV	UTTY(U),E
	JMP	TTYRS2

;DING --
DING:	INC	TODING(E)
	RTS	PC

;DMSQ -- DM11 TYPE IN
DMSQ:	BIT	#60000,DMCSR
	BNE	DMSQ4	;NXM OR SYS OVERRUN
	SPUSH	C
	SPUSH	D
	SPUSH	E
	SPUSH	U
	MOV	TTP,C
	BR	DMSQ3

DMSQ1:	BIC	#120200,(C)
	BIT	#40000,(C)
	BNE	DMSQ2	;BR IF NO STOP RCVD
	MOVB	1(C),E	;TTY*2
	MOVB	(C),D	;CHAR
	ADD	#NFDMTY,E
	JSR	PC,TISQ1
DMSQ2:	TST	(C)+	;INC TTP
	CMP	C,#DMTT+200
	BLT	DMSQ3
	MOV	#DMTT,C	;IF TTP AT END OF TT, RESET
DMSQ3:	TST	(C)
	BLT	DMSQ1

	MOV	C,(PC)+	;SAVE PNTR
TTP:	DMTT
	SPOP	U
	SPOP	E
	SPOP	D
	SPOP	C
	RTS	PC

DMSQ4:	HALT		;NXM OR SYS OVERRUN
	BIC	#60000,DMCSR
	RTS	PC



CNSQ:	JSR	A,GENSQ
	TKB
	NFTY

GENSQ:	SPUSH	D
	SPUSH	E
	SPUSH	U
	MOV	@(A)+,D
	MOV	(A),E
	JSR	PC,TISQ
	SPOP	U
	SPOP	E
	SPOP	D
	SPOP	A
	RTI

;TI2Q--
;FOR ALL USERS, EMPTY TEMP TYI BUFFER TO TYI BUF
TI2Q:	SPUSH	C
	SPUSH	D
	SPUSH	E
	CLR	E

TI2Q1:	MOV	TIPC(E),C	;TRY TO PROCESS ALL CHARS
	BIT	#TIDTRA,TTYST(E)
	BNE	TI2Q3	;BR IF DSK TRA OR TYO BUF FULL
	TST	TOPC(E)
	BNE	TI2Q3
	TST	C
	BNE	TI2Q2
	TST	TITQN(E)
	BLE	TI2Q3
	MOVB	@TITQP(E),D	;CHAR
	JSR	PC,TGTC	;PROCESS
	DEC	TITQN(E)
	INC	TITQP(E)
	CMP	TITQP(E),TITQT+2(E)
	BLT	TI2Q1
	MOV	TITQT(E),TITQP(E)
	BR	TI2Q1

TI2Q3:	TST	(E)+
	CMP	E,NUSER2	;THROUGH WITH ALL TTYS?
	BLT	TI2Q1	;BR IF NO
	SPOP	E
	SPOP 	D
	SPOP	C
	RTS	PC

TI2Q2:	MOV	TISVD(E),D
	CLR	TIPC(E)
	JSR	PC,(C)
	BR	TI2Q1


;TGTC --
;GET CHAR FROM 5 WD BUFFER & PROCESS INTO WD ASSEMBLY BUFFER
TGTC:	BIT	#FCQF,TTYST(E)
	BEQ	TGTC5
	JMP	TICPY	;YES, COPY CHAR
TGTC5:	BIT	#200,D	;QUOTED?
	BNE	TGTC2	;BR IF YES
	BITB	#FSF,DTBL(D)	;SPECIAL CHAR?
	BEQ	TGTC1
	MOVB	DTBL2(D),C	;YES
	JMP	@CTIS(C)	;DISPATCH

TGTC1:	BITB	#FQF,DTBL(D)	;QUOTING CHAR?
	BEQ	TGTC2
	MOVB	DTBL2(D),C	;YES
	JSR	PC,@CTIQ(C)	;DISPATCH

TGTC2:	JSR	PC,TIINB	;PUT CHAR IN BUFFER
TGTC3:	JSR	PC,UTYO1
	JSR	PC,@TOSND(E)
TGTC4:	RTS	PC


;SPECIAL CHAR ROUTINES

;^C -- COPY NEXT CHAR
TICC:	BIT	#TIED,TTYST(E)
	BNE	TICC1
	JMP	TIQM	;IF NOT EDITING JUST ECHO ^C
TICC1:	JSR	PC,GEDC	;GET EDIT CHAR
	BR	TGTC5

;^C -- DELETES NEXT CHAR
TICD:	BIT	#TIED,TTYST(E)
	BNE	TICD1
	JMP	TIQM	;IGNORE IF NOT EDITING
TICD1:	JSR	PC,GEDC	;REMOVE CHAR
	MOV	#4,D
	BR	TGTC3	;SUCCEED, ECHO ^C

;^M (C.R.) -- MAYBE END OF LINE
TICM:	TST	TTYBR(E)
	BGT	TGTC2	;IF NOT EOL
TICM1:	JSR	PC,TGTC2	;ECHO C.R.
	JSR	PC,TIDTSC
	MOV	TITN(E),TYITN(E)
	TST	TIDP(E)
	BLE	TICM4
	CMP	TIBP(E),TIBT(E)
	BLE	TICM2
	MOV	#TICM2,TIPC(E)
	JMP	TIDKO
TICM2:	MOV	TIDP(E),TYIDP(E)
	JSR	PC,TTYRS3
	JSR	PC,TIDTS1
	JSR	PC,TYIDKI
TICM3:	MOV	TTYU(E),D
	MOV	#FRTYIW,FLSRES(D)
	RTS	PC
TICM4:	JSR	PC,TTYRS3
	JSR	PC,TEDKO
	BR	TICM3

;^N -- COPY NEXT WORD
TICN:	BIT	#TIED,TTYST(E)
	BNE	TICN1
	JMP	TIQM

TICN2:	JSR	PC,TGTC5
	JSR	PC,TIDTSC
TICN1:	JSR	PC,GEDC	;GET NEXT CHAR IN ED BUF
	BIT	#200,D
	BNE	TICN2
	BITB	#WSF,DTBL(D)
	BEQ	TICN2
	JMP	TGTC5

;^Q -- QUOTE NEXT CHAR
TICQ:	BIS	#FCQF,TTYST(E)	;^Q FLAG ON
	BR	TGTC3	;ECHO BUT NOT IN BUFFER

;^R -- COPY REST OF EDIT BUFFER
TICR:	BIT	#TIED,TTYST(E)
	BNE	TICR1
	JMP	TIQM	;IF NOT EDITING, IGNORE
TICR1:	JSR	PC,GEDC
	JSR	PC,TGTC5	;SUCCEED, ECHO
	JSR	PC,TIDTSC
	BR	TICR1

;^S -- SKIP (DELETE) NEXT WORD
TICS:	BIT	#TIED,TTYST(E)
	BNE	.+6
	JMP	TIQM
	JSR	PC,TGTC3
TICS1:	JSR	PC,TIDTSC
	JSR	PC,GEDC
	BIT	#200,D
	BNE	TICS1
	BITB	#WSF,DTBL(D)
	BEQ	TICS1
	JMP	TGTC4

;TICW -- ^W RUBOUT TILL PREVIOUS SPACE
TICW1:	JSR	PC,TGTC3	;ECHO CHAR
	JSR	PC,TIDTSC
TICW:	JSR	PC,TIDEL1
	BIT	#200,D
	BNE	TICW1
	BITB	#WSF,DTBL(D)
	BEQ	TICW1
	JMP	TGTC3

;TICY	-- ^Y RECOVER PREVIOUS LINE
TICY:	BIT	#TIED,TTYST(E)
	BNE	TIQM
	JSR	PC,TICZ1
	JSR	PC,TIDTSC
	MOV	#137,D
	JSR	PC,TGTC3
	TST	TIEDP(E)
	BLE	.+6
	JSR	PC,TEDKI
	CLR	TTYBR(E)
	MOV	TIBT(E),TIBP(E)
	CLR	TIDP(E)
	CLR	TITN(E)
	JMP	EDITA

;^Z -- EMPTY TYI BUFFER
TICZ:	MOV	TTYU(E),C
	MOV	#FRTYIW,FLSRES(C)
	JSR	PC,TICZ1
	CLR	TTYBR(E)
	MOV	#15,@TIBT(E)
	MOV	#1,TYITN(E)
	JMP	TTYRS3

TICZ1:	MOV	P,CHARNO(E)
	JMP	TGTC3


;TIDTSC -- DID SPEC CHAR CAUSE DSK TRANS OR TYO BUF FULL?
TIDTSC:	TST	TOPC(E)
	BNE	TIDTS2
TIDTS1:	BIT	#TIDTRA,TTYST(E)
	BEQ	TIDTS3
TIDTS2:	MOV	D,TISVD(E)
	MOV	(P)+,TIPC(E)
TIDTS3:	RTS	PC

;TICPY -- QUOTE CHAR IN D
TICPY:	BIC	#FCQF,TTYST(E)	;^Q FLAG OFF
	BITB	#FSF!FQF!WSF,DTBL(D)
	BEQ	TICPY1
	CMP	D,#3
	BEQ	TICPY2
	CMP	D,#32
	BEQ	TICPY2
	CMP	D,#177
	BEQ	TICPY1
	BIS	#200,D	;TURN ON HIGH BYTE BIT
TICPY1:	JMP	TGTC2
TICPY2:	JMP	TGTC3

;TIOBR -- [
TIOBR:	INC	TTYBR(E)
	RTS	PC

;TICBR -- ]
TICBR:	TST	TTYBR(E)
	BLE	TIQM
	DEC	TTYBR(E)
	RTS	PC

;TIQM -- PRINTS ?
TIQM:	MOV	#'?,D
	JMP	TGTC3


;REGULAR CHAR ROUTINES
;TINB MOVES INTO BUFFER, TIDEL1 MOVES OUT

TIDEL:	JSR	PC,TIDEL1	;THIS ALSO WANTS TO BE SUBR
	JMP	TGTC3	;SUCCEED, ECHO CHAR

TIDEL1:	CMP	TIBP(E),TIBT(E)
	BLE	TIDEL5	;BR IF NO CHARS IN BUF
	DEC	TIBP(E)
	MOVB	@TIBP(E),D	;CHAR
	BIT	#200,D
	BNE	TIDEL3	;BR IF QUOTED
	BITB	#FQF,DTBL(D)
	BNE	.+4
TIDEL3:	RTS 	PC
	MOVB	DTBL2(D),C	;UNDO QUOTING ACTION
	JMP	@CTIUQ(C)

TIDEL5:	TST	TIDP(E)
	BLE	TIDEL6
	MOV	D,TISVD(E)
	SUB	#4,(P)
	MOV	(P)+,TIPC(E)
	JMP	TIDKI
TIDEL6:	TST	(P)+
	RTS	PC

TIINB:	CMP	TIBP(E),TIBL(E)
	BGE	TIINB2
	MOVB	D,@TIBP(E)	;CHAR IN BUF
	INC	TITN(E)
	INC	TIBP(E)
	CMP	TIBP(E),TIBL(E)
	BGE	TIINB1
	RTS	PC

TIINB1:	JMP	TIDKO

TIINB2:	HALT	;TIBP ZONKED
	RTS	PC


;GEDC -- GET EDIT CHAR (FROM EDIT BUF)
GEDC:	CMP	TIEBP(E),TIBT+2(E)	;ANYTHING IN BUFFER?
	BGE	TIDEL6
	CMPB	@TIEBP(E),#15	;IS NEXT CHAR C.R.?
	BEQ	GEDCA	;BR IF YES
GEDC1:	MOVB	@TIEBP(E),D	;CHAR IN D
	INC	TIEBP(E)
	BIT	#200,D
	BNE	GEDC2
	BITB	#FQF,DTBL(D)	;IS IT QUOTING?
	BEQ	GEDC2
 	MOVB	DTBL2(D),C
	ADD	EDQF(C),TTYEBR(E)
GEDC2:	CMP	TIEBP(E),TIBT+2(E)	;OUT OF BUF?
	BLT	GEDC3	;BR IF STILL STUFF IN BUF
	TST	TIEDP(E)	;IS THERE STUFF ON DSK?
	BNE	GEDC4	;BR IF YES
GEDC3:	RTS	PC

GEDC4:	JMP	TEDKI

		;NEXT CHAR C.R.
GEDCA:	TST	TTYEBR(E)
	BGT	GEDC1	;BR IF IN QUOTES
	MOV	TIBT+2(E),TIEBP(E)	;END OF EDIT LINE, PUT PNTR TO END OF EDIT BUFFER
	BR	TIDEL6


EDQF:	1	;[
	-1	;]


;UTYI -- USER TYI
;RETURNS CHAR IN CH
JTYI:	TST USER
	BGE .+6
	JMP STYI
	PUSH	E
	PUSH	U
	MOV	USER,U
	MOV	UTTY(U),E
	BIT	#TIDTRA,TTYST(E)
	BEQ	UTYI2
	MOV	#FRTYIW,FLSRES(U)
UTYI1:	JSR	PC,FLUSH	;IF DSKING,FLUSH USER
	BIT	#TIDTRA,TTYST(E)
	BNE	UTYI1
	JSR	PC,RUNME
UTYI2:	BIT	#TIBR,TTYST(E)
	BNE	UTYI3
	DEC	TYITN(E)
	BLT	UTYI4
	CMP	TIEDP(E),TYIFDP(E)
	BGE	.+6
	JSR	PC,TEDKO
	MOVB	@TYIBP(E),D
	INC	TYIBP(E)
	CMP	TYIBP(E),TIBL(E)	;USED UP BUFFER?
	BLT	UTYI3
	JSR	PC,TYIDKI
UTYI3:	POP	U
	POP	E
	RTS	PC

UTYI4:	HALT	;TYI ASKED FOR TOO MANY CHARS
	BR	.-2

.STITLE 	TYI DSK TRANSFER, START, & STOP ROUTINES

;DISK TRANSFER ROUTINES
;LOCS MUST BE FILLED AS FOLLOWS
;(A) START ROUTINE ADDRESS
;2(A) -WORD COUNT (NOT BYTE COUNT)
;4(A) CORE ADDRESS-2
;6(A) LOW ORDER OF DSK ADDR
;10(A) HIGH ORDER
;12(A) FREE WD STORE TTY HERE
;14(A) READ OR WRITE CODE (IN LOW BYTE)

;TIDCM -- COMMON CODE
;CALLED WITH JSR D,
TIDCM:	MOV	(D)+,.+6	;ZOT NXT INSTR
	MOV	2(E),4(A)
	SUB	#2,4(A)		;CORE ADDR-2
	MOV	E,12(A)		;USER
	BIS	(D)+,TTYST(E)
	TST	(D)+
	MOV	D,(A)
	MOV	-(D),14(A)
	SPOP	D
TIDCM1:	SPOP	A
	RTS	PC

;TSTAB
;TEST FOR ABORT (CALLED WITH JSR D,) AT BEGIN OF START ROUTINE
TSTAB:	SPUSH	E
	MOV	12(A),E
	BIT	#TIBR,TTYST(E)
	BEQ	TSTAB1
	CLR	14(A)	;IF BREAK FLAG, ABORT TRANS
	BIC	-10(D),TTYST(E)
TSTAB4:	BIT	#TIDTRA,TTYST(E)
	BNE	TSTAB1
	MOV	TTYU(E),D
	CMP	#FRTYIH,FLSRES(D)
	BNE	TSTAB3
	BIC	#TIBR,TTYST(E)
TSTAB1:	MOV	D,(A)	;STOP ROUTINE
TSTAB3:	SPOP	E
	SPOP	D
	RTS	PC

;TSTAB2
;CALLED AT START OF STOP ROUTINE
TSTAB2:	SPUSH	E
	MOV	12(A),E	;TTY
	BIC	-14(D),TTYST(E)
	BIT	#TIBR,TTYST(E)
	BNE	TSTAB4
	JMP	(D)

;TIDKO -- TTY
;FROM TYI BUFFER 
;TO END OF DSK TYI BUFFER
TIDKO:	SPUSH	A
	JSR	PC,SYDRBG
	MOV	TIBT(E),2(A)
	SUB	TIBP(E),2(A)
	BLT	.+10
	HALT
	JMP	TIDCM1
	ASR	2(A)
	SBC	2(A)		;-WD CNT
	MOV	TILD(E),6(A)
	MOV	TIHD(E),10(A)
	ADD	TIDP(E),6(A)	;LOW ORDER DSK
	ADC	10(A)		;HIGH ORDER
	JSR	D,TIDCM
	TIBT
	TIDW
	DWRTEC
	JSR	D,TSTAB		;START ROUTINE
	JSR	D,TSTAB2	;STOP ROUTINE
	SUB	2(A),TIDP(E)	;UPDATE # OF WDS (NOT BYTES) ON DSK
	SUB	2(A),TYIDP(E)
	MOV	TIBT(E),TIBP(E)
	JMP	TSTAB3

;TIDKI --
;FROM END OF DISK TYI
;TO CORE TYI
TIDKI:	SPUSH	A
	JSR	PC,SYDRBG
	MOV	TIBL(E),2(A)
	SUB	TIBT(E),2(A)
	CMP	2(A),TIDP(E)
	BLE	TIDKI1
	MOV	TIDP(E),2(A)
TIDKI1:	NEG	2(A)		;- WD CNT
	MOV	TILD(E),6(A)
	MOV	TIHD(E),10(A)
	ADD	2(A),6(A)	;DSK ADDR=DSK TOP+# OF WDS ON DSK-
	ADD	TIDP(E),6(A)	;# BEING TRANSFERRED
	ADC	10(A)
	JSR	D,TIDCM
	TIBT
	TIDR
	DREADC
	JSR	D,TSTAB		;START ROUTINE
	JSR	D,TSTAB2	;STOP ROUTINE
	ADD	2(A),TIDP(E)
	BGE	.+6
	HALT		;NEG WD CNT
	BR	.-2
	ASL	2(A)
	SUB	2(A),TIBP(E)	;CORE PNTR
	JMP	TSTAB3


;TIEDKI --
;FROM FRONT OF DSK EDIT BUF
;TO CORE EDIT BUFFER
TEDKI:	SPUSH	A
	JSR	PC,SYDRBG
	MOV	#<EDBFSZ/2>,2(A)
	CMP	2(A),TIEDP(E)
	BLE	TEDK1
	TST	TIEDP(E)
	BGT	.+10
	HALT
	JMP	TIDCM1
	MOV	TIEDP(E),2(A)
TEDK1:	NEG	2(A)		;NEG WD CNT
	MOV	TIELD(E),6(A)
	MOV	TIEHD(E),10(A)
	ADD	TIEFDP(E),6(A)	;LOW ORDER DSK
	ADC	10(A)
	JSR	D,TIDCM
	TIEBT
	TIEDR
	DREADC
	JSR	D,TSTAB		;START ROUTINE
	JSR	D,TSTAB2	;STOP ROUTINE
	ADD	2(A),TIEDP(E)	;# OF WDS ON DSK
	BGE	.+6
	HALT
	BR	.-2
	SUB	2(A),TIEFDP(E)	;# OF WDS OFF DSK
	MOV	TIEBT(E),TIEBP(E)
	JMP	TSTAB3


;TIEDKO --
;FROM CORE TYI
;TOEND OF DSK EDIT BUFFER
TEDKO:	SPUSH	A
	JSR	PC,SYDRBG
	MOV	#-<TIBFSZ/2>,2(A)	;NEG WD CNT
	MOV	TIELD(E),6(A)
	MOV	TIEHD(E),10(A)
	ADD	TIEDP(E),6(A)
	ADC	10(A)
	JSR	D,TIDCM
	TIBT
	TIEDW
	DWRTEC
	JSR	D,TSTAB		;START ROUTINE
	JSR	D,TSTAB2	;STOP ROUTINE
	SUB	2(A),TIEDP(E)
	BGE	.+4
	HALT
	JMP	TSTAB3

;TYIDKI
;FROM FRONT OF DSK TYI BUFFER
;TO CORE TYI BUFFER
TYIDKI:	SPUSH	A
	JSR	PC,SYDRBG
	MOV	#-<TIBFSZ/2>,2(A)	;NEG WD CNT
	MOV	TILD(E),6(A)
	MOV	TIHD(E),10(A)
	ADD	TYIFDP(E),6(A)	;LOW ORDER DKS
	ADC	10(A)		;HIGH ORDER
	JSR	D,TIDCM
	TIBT
	TYIDR
	DREADC
	JSR	D,TSTAB		;START ROUTINE
	JSR	D,TSTAB2	;STOP ROUTINE
	SUB	2(A),TYIFDP(E)
	ADD	2(A),TYIDP(E)
	MOV	TIBT(E),TYIBP(E)
	JMP	TSTAB3

.STITLE	TYPE OUT

;DMINT --
;DM11 TRANSMIT INTERRUPT
DMINT:	SPUSH	E
	MOV	#NFDMTY,E
DMINT1:	BIT	DMBIT(E),DMTRAN
	BEQ	DMINT5		;LINE WASN'T TRANSMITTING
	TST	DMWC(E)
	BLT	DMINT5	;TRANSMITTING
	BIC	DMBIT(E),DMTRAN
	CMP	DMCA(E),TOBT+2(E)
	BLT	DMINT2
	MOV	TOBT(E),DMCA(E)
DMINT2:	TST	TOPC(E)
	BNE	DMINT4
	TST	TOTN(E)
	BEQ	DMINT5
DMINT3:	JSR	PC,DMSND4
DMINT5:	TST	(E)+
	CMP	E,#NFDMTY+<2*NDMTY>
	BLT	DMINT1	;MORE TTYS TO SERVICE
	SPOP	E
	CMP	JDCSW,#1
	BGT	.+6
	JSR	PC,TI2Q
	RTI

DMINT4:	JSR	PC,TOENT
	BR	DMINT3

;DMSND --
;TRANSMIT CHARS FROM TOBE(E) TO TOBP(E) IN CIRC BUF
DMSND:	TST	DMWC(E)
	BLT	DMSND5
	CMP	DMCA(E),TOBT+2(E)
	BLT	DMSND4
	MOV	TOBT(E),DMCA(E)
DMSND4:	MOV	DMCA(E),DMWC(E)
	CMP	DMWC(E),TOBP(E)
	BLT	DMSND1
	SUB	TOBT+2(E),DMWC(E)	;MESSAGE WRAPS AROUND
	BR	DMSND2
DMSND1:	SUB	TOBP(E),DMWC(E)
DMSND2:	BGE	DMSND3
	ADD	DMWC(E),TOTN(E)
	BIS	DMBIT(E),(PC)+
DMTRAN:	0
	BIS	DMBIT(E),DMBAR
DMSND5:	RTS	PC
DMSND3:	HALT		;WC>=0, SND SHOULD NOT HAVE BEEN CALLED
	BR	.-6

CNINT:	SPUSH	E
	CLR	E
	JSR	PC,CNSND
	SPOP	E
	RTI

CNSND:	JSR	A,GNSND
	TPS
	TPB
	
GNSND:	BIT	#200,@(A)+
	BEQ	GNSND1
	TST	TOTN(E)
	BLE	GNSND1
	MOVB	@TOBFF(E),@(A)
	DEC	TOTN(E)
	INC	TOBFF(E)
	CMP	TOBFF(E),TOBT+2(E)
	BLT	GNSND1
	MOV	TOBT(E),TOBFF(E)
GNSND1:	SPOP	A
	RTS	PC


;UTYO -- USER TYO
;PUTS CHARS IN CIRC TYO BUFFER
JTYO:	TST USER
	BGE .+6
	JMP STYO
	PUSH	C
	PUSH	E
	PUSH	U
	MOV	USER,U
	MOV	UTTY(U),E
	BIT	#TIBR,TTYST(E)
	BEQ	.+10
UTYO11:	JSR	PC,TTYRS2
	BR	UTYO10
	TST	TOPC(E)
	BEQ	UTYO9
	MOV	#FRTYOW,FLSRES(U)
UTYO8:	JSR	PC,FLUSH	;FLUSH USER WITH FULL TYPE OUT BUFFER
	TST	TOPC(E)
	BNE	UTYO8
	JSR	PC,RUNME
UTYO9:	BIT	#TIBR,TTYST(E)
	BNE	UTYO11
	JSR	PC,UTYO1
	JSR	PC,@TOSND(E)
UTYO10:	POP	U
	POP	E
	POP	C
	RTS	PC


TOENT:	SPUSH	C
	SPUSH	D
	SPUSH	TOPC(E)
	CLR	TOPC(E)
	MOV	TOSVC(E),C
	MOV	TOSVD(E),D
	JSR	PC,@(P)+
	SPOP	D
	SPOP	C
	RTS	PC


UTYO1:	TST	TOPC(E)
	BEQ	.+6
	HALT		;TYO BUFFER FULL
	BR	.-2
	TST	TODING(E)
	BLE	UTYO1B
	MOV	D,C
	MOV	#7,D
UTYO1A:	JSR	PC,TOINB(E)
	DEC	TODING(E)
	BGT	UTYO1A
	MOV	C,D
UTYO1B:	BIC	#177600,D
	BITB	#FOF,DTBL(D)
	BEQ	UTYO2
	MOVB	DTBL2(D),C
	JMP	@CTOS(C)

UTYO2:	CMP	D,#40
	BGE	UTYO3
	MOV	D,C	;PRINT ^ AND CHAR + 100
	MOV	#'^,D
	JSR	PC,TOINB
	MOV	C,D
	ADD	#100,D
	JSR	PC,TOINB
	MOV	C,D
	ADD	#2,CHARNO(E)
	BR	UTYO5

UTYO3:	INC	CHARNO(E)
UTYO4:	JSR	PC,TOINB
UTYO5:	CMP	CHARNO(E),LINEL(E)
	BLT	UTYO6
TOCM:	CLR	CHARNO(E)
	MOV	D,C
	MOV	#15,D
	JSR	PC,TOINB
	MOV	#12,D
	JSR	PC,TOINB
	TST	SIMTST
	BNE	UTYO7
	CLR	D
	JSR	PC,TOINB
	JSR	PC,TOINB
	JSR	PC,TOINB
UTYO7:	MOV	C,D
UTYO6:	RTS	PC


;TOINB -- PUT CHAR IN TYO BUFFER
TOINB:	CMP	TOBP(E),TOBT+2(E)
	BLT	TOINB1
	MOV	TOBT(E),TOBP(E)
TOINB1:	MOVB	D,@TOBP(E)
	INC	TOTN(E)
	INC	TOBP(E)
	CMP	TOSND(E),#DMSND
	BEQ	TOINB4
	CMP	TOTN(E),#TOBFSZ
	BGE	TOINB2
	CMP	TOBP(E),TOBT+2(E)
	BLT	TOINB3
	MOV	TOBT(E),TOBP(E)
	RTS	PC

TOINB4:	CMP	TOBP(E),DMCA(E)
	BEQ	TOINB2
	CMP	TOBP(E),TOBT+2(E)
	BLT	TOINB3
	MOV	TOBT(E),TOBP(E)
	CMP	TOBP(E),DMCA(E)
	BNE	TOINB3
TOINB2:	MOV	(P)+,TOPC(E)
	MOV	C,TOSVC(E)
	MOV	D,TOSVD(E)
TOINB3:	RTS	PC

;CHAR ROUTINES

;^B -- ECHOES AS SPACE
TOCB:	MOV	#40,D
	INC	CHARNO(E)
	JSR	PC,TOINB
	MOV	#2,D
	BR	UTYO5

;^H -- BACKSPACE
TOCH:	TST	CHARNO(E)
	BEQ	UTYO6
	DEC	CHARNO(E)
	JMP	UTYO4

;^I -- TAB
TOCI:	MOV	#40,D
TOCI1:	INC	CHARNO(E)
	JSR	PC,TOINB
	BIT	#7,CHARNO(E)
	BNE	TOCI1
	MOV	#11,D
	JMP	UTYO5
