; I T S MISCELLANEOUS DEVICES

IFN PTRP,[

SUBTTL	PAPER TAPE PUNCH ROUTINES

PTPO:	JSP Q,STDOPN
	7,,PTPUSR
	PTPA,,PTPB
	PTPWA,,PTPWB
	PTPI,,0
	PTPI,,0

BPIPUN:	JSP E,NBTOCH
	CAIN A,EOFCH
	POPJ P,
	JRST APIPU2

APIPUN:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)
	ANDI A,177
APIPU2:	CAIN A,14
	 JRST PUNFF
	TRO A,200
	JRST PIPUN3

PIPUN:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)
PIPUN3:	SKIPG PUNCC
	 JRST PUNW1
PIPUN2:	IDPB A,PUNIP
	HRRZ A,PUNIP
	CAIN A,PUNBE-1
	MOVEI A,PUNBO
	HRRM A,PUNIP
	SOS PUNCC
PUNST:	SKIPGE PUNNT
	POPJ P,	;PUNCH OUT OF TAPE
	CONSO PTP,7
	CONO PTP,10+PCHCHN
	POPJ P,

	PUSHJ P,PUNW2
PUNW1:	PUSHJ P,UFLS
	JRST PIPUN2

PUNW2:	SKIPG T,PUNCC
	POPJ P,
	CAIL T,PUNSIZ*2
	JRST POPJ1
	JRST POPJ3

PUNFF:	MOVEI T,55
	CAMLE T,PUNCC
	PUSHJ P,UFLS
	MOVSI B,-4
	PUSHJ P,PUNFF2
	MOVEI A,214
	PUSHJ P,PIPUN2
	MOVSI B,-50
PUNFF2:	MOVEI A,0
	PUSHJ P,PIPUN2
	AOBJN B,PUNFF2
	POPJ P,

WBPIPN:	JSP Q,WRDBT
WAPIPN:	MOVEI T,6
	CAMLE T,PUNCC
	PUSHJ P,UFLS
	SKIPGE C
	 SKIPA B,(C)
	  UMOVE B,(C)
	MOVSI E,-6
WPIPN2:	MOVEI A,2
	LSHC A,6
	PUSHJ P,PIPUN2
	AOBJN E,WPIPN2
	POPJ P,

PBRK1:	MOVEI A,PUNSIZ*4-4
	CAMG A,PUNCC
	JRST PUNSTP
	CONSZ PTP,100	;CHECK NO TAPE FLAG
	JRST PBRK2	;PUNCH OUT OF TAPE
	ILDB A,PUNOP
	DATAO PTP,A
	HRRZ A,PUNOP
	CAIN A,PUNBE-1
	MOVEI A,PUNBO
	HRRM A,PUNOP
	AOSA PUNCC
PUNSTP:	CONO PTP,0
	JRST LPBKRT

PBRK2:	SETOM PUNNT	;PTP OUT OF TAPE
	JRST PUNSTP

;PTP SLOW CLOCK ROUTINE
PTPCHK:	SKIPGE PUNNT
	CONSZ PTP,100
	POPJ P,
	HRRZM P,PUNNT
	JRST PUNST	;PTP HAS TAPE NOW

EBLK

PUNNT:	105	;-1 => PTP OUT OF TAPE
PUNCC:	PUNSIZ*4-4
PUNIP:	(1000)PUNBO-1
PUNOP:	(1000)PUNBO-1
PUNBO:	BLOCK PUNSIZ
PUNBE:

BBLK

PUNCLR:	MOVEI A,PUNSIZ*4-4
	MOVE B,PUNIP
	MOVEM A,PUNCC
	MOVEM B,PUNOP
	POPJ P,

PUNFIN:	MOVE T,PUNSIZ*4-4	;.CALL FINISH
	CAMLE T,PUNCC
	 PUSHJ P,UFLS
	POPJ P,

AFEED:	HRRZ A,(R)
	CAIL A,PTPI	;LOWEST PTP DEV #
	CAILE A,PTPWB	;HIGHEST PTP DEV #
	POPJ P,	;PTP NOT OPEN ON CHNL
	MOVEI A,0
	PUSHJ P,PIPUN3
	JRST POPJ1

SUBTTL	PAPER TAPE READER ROUTINES

	PTRCHX==10	;NUMBER OF CHARACTERS IGNORED AT END OF TAPE

PTRO:	PUSH P,[PTRO1]	;SO STDOPN WILL RETURN
	JSP Q,STDOPN
	7,,PTRUSR
	REDUAI,,REDBAI	;ASCII MODE
	REDUII,,REDBII	;IMAGE MODE
	REDUTI,,0	;ALL 8 HOLES MODE
	REDUTI,,0	;" " " "

		;RETURN FROM STDOPN
PTRO1:	POPJ P,		;OPEN LOST
	AOS (P)		;OPEN WON, MAKE RETURN SKIP
	SKIPE PTRUSE
	POPJ P,		;READER ALREADY OPEN, DON'T RESET
			;READER NOT ALREADY OPEN, SO
		;RESET PTR POINTERS AND START UP READER
	CONO PI,PTROFF	;INHIBIT INTERRUPTS
	MOVEI T,20	;BUSY FLAG
	PUSHJ P,PTRST	;START UP READER IF NOT ALREADY RUNNING
		;RESET PTR BUFFER POINTERS
PTRZAP:	MOVE A,[041000,,PTRBUF-1]	;BUFFER BYTE POINTER
	MOVEM A,PTRIP	;BUFFER INPUT (INTERRUPT) POINTER
	MOVEM A,PTROP	;BUFFER OUTPUT (IOT) POINTER
	MOVEI A,PTRCHX	;REINITIALIZE CHARACTER COUNT
	MOVNM A,PTRCC	;" " "
	CONO PI,PTRON	;RE-ENABLE INTERRUPTS
	JRST PTRSIW	;RESET STATUS OF IMAGE WORD AND RETURN

		;CLOSE CHANNEL ON WHICH PTR OPEN

PTRCLS:	CONO PI,PTROFF	;INHIBIT INTERRUPTS
	SOSL PTRUSE	;DECREMENT USE COUNTER
	JRST PTRONJ	;PTR OPENED ON MORE CHANNELS
	SETOM PTRUSR	;NO MORE OPENS ON READER, INDICATE NO USER
	SETOM PTRDIE	;EVENTUALLY TURN OFF READER
	JRST PTRZAP	;RESET BUFFER STATUS

PTRRS:	CONO PI,PTROFF	;PTR .RESET ROUTINE
	JRST PTRZAP

		;BLOCK ASCII INPUT

PTRBAI:	JSP E,INBTCH	;DO IT A CHARACTER AT A TIME

		;GET AN ASCII CHARACTER IN W

PTRUAI:
PTRCHA:	PUSHJ P,PTRCH	;GET CHARACTER - SKIPS IF EOF.
	TRZA W,200	;CLEAR 200 BIT
	 JRST POPJ1
	JUMPE W,PTRCHA	;IF RESULT ZERO THEN DON'T CALL IT ASCII
	CAIN W,177	;IF RUBOUT,
	 JRST PTRCHA	;THEN IGNORE IT
	POPJ P,		;ASCII, USE IT

PTRBII:	JSP W,WRDBTI	;BLOCK IMAGE INPUT.

		;UNIT IMAGE INPUT
		;END-OF-FILE GIVES SIXBIT /######/

PTRUII:	PUSHJ P,PTRCH	;GET CHARACTER
	 TRNE W,200
	  CAIA		;CALL IT BINARY IF 200 SET, OR IF EOF.
	   JRST PTRUII
	IDPB W,PTRIBP
	JUMPL W,PTRUI2	;IF REACHED EOF, SKIP 1 OR 2.
	SOSLE PTRICC	;IF WORD NOT FULL,
	 JRST PTRUII	;THEN GO BACK FOR MORE
	JRST PTRUI1	;ELSE RETURN THE WORD.

PTRUI2:	SOSG B,PTRICC
	 JRST PTRUI3
	CAIE B,5
	 JRST PTRUII
	JRST PTRUI4

PTRUI3:	AOS (P)		;IF WE REACHED EOF, SKIP TWICE.
PTRUI4:	AOS (P)		;REACHED EOF BEFORE WORD; SKIP ONCE.
PTRUI1:	MOVE W,PTRIW	;WORD FULL,
PTRSIW:	MOVEI B,6	;NOW REINITIALIZE POINTERS TO WORD
	MOVEM B,PTRICC	;CHARACTER COUNT OF WORD
	MOVE B,[440600,,PTRIW]	;POINTER TO WORD
	MOVEM B,PTRIBP
	POPJ P,

		;GET A CHARACTER IN W OR SKIP RETURNING -1,,EOFCH IF NONE AVAILABLE

PTRUTI:		;UNIT MODE ALL-HOLES INPUT.
PTRCH:	MOVEI T,20	;BUSY FLAG
	PUSHJ P,PTRCHW	;WAIT FOR A CHARACTER
	 PUSHJ P,UFLS
	SOSGE I,PTRCC	;DECREMENT CHARACTER COUNT
	 JRST PTRCH1	;BUFFER EMPTY
	MOVEI T,10	;DONE FLAG
	CAIGE I,REDSIZ-PTRCHX	;IF BUFFER LESS THAN 1/4-FULL,
	 PUSHJ P,PTRST	;THEN MAKE SURE READER RUNNING
	ILDB W,PTROP	;GET CHARACTER
	HRRZ B,PTROP	;RING THE BUFFER
	CAIL B,PTRBUF+REDSIZ-1
	 MOVEI B,PTRBUF
	HRRM B,PTROP
	POPJ P,

		;END-OF-FILE ENCOUNTERED DURING PTRCH

PTRCH1:	AOS PTRCC	;RESTORE CHARACTER COUNT TO ITS ORIGINAL STATE
	JRST UNIEOF

		;SKIP IF CHARACTER IN BUFFER OR END-OF-TAPE

PTRCHW:	SKIPG PTRCC	;IF CHARACTER IN BUFFER,
	CONSO PTR,400	;OR POWER IS OFF,
	JRST POPJ1	;THEN EXIT SKIPPING
			;OTHERWISE,
		;MAKE SURE READER RUNNING

PTRST:	CONSZ PTR,7
	CONSO PTR,30	;IF RUNNING,
	JRST .+2
	POPJ P,		;THEN THAT'S ALL
	CONO PTR,PTRCHN(T)	;NOT RUNNING, RESTART WITH SELECTED FLAG
	SETOM PTRDIE	;LET SLOW CLOCK FIGURE OUT IF THERE'S TAPE
	POPJ P,

		;PTR INTERRUPT ROUTINE

PTRBRK:	AOSN PTRSTP	;IF READER SHOULD BE STOPPED THIS BREAK,
	JRST PTRFUL	;THEN STOP IT (BUFFER FULL)
	DATAI PTR,A	;GET CHARACTER
	SETOM PTRDIE	;TELL SLOW CLOCK THAT BREAK HAS HAPPENED
	IDPB A,PTRIP	;PUT IN BUFFER
	HRRZ A,PTRIP	;RING THE BUFFER
	CAIN A,PTRBUF+REDSIZ-1
	MOVEI A,PTRBUF
	HRRM A,PTRIP
	AOS A,PTRCC	;TOTAL CHARACTER COUNT-PTRCHX
	CAIGE A,REDSIZ*4-10-PTRCHX
	JRST LPBKRT
	SETOM PTRSTP	;STOP READER AT NEXT BREAK
	JRST LPBKRT

PTRFUL:	CONO PTR,PTRCHN	;BUFFER FULL, STOP READER
	MOVEI A,1	;TELL SLOW CLOCK...
	MOVEM A,PTRDIE	;NOT TO TURN IT OFF
	JRST LPBKRT

		;SLOW CLOCK ROUTINE
		;CHECKS READER EVERY HALF-SECOND

PTRCHK:	AOS A,PTRDIE
	CAIN A,1	;IF UNINTENTIONALLY STOPPED FOR OVER HALF-SECOND
	CONO PTR,400	;THEN TURN THE READER OFF
	POPJ P,

EBLK
		;PTR VARIABLES

PTRDIE:	-264		;END-OF-TAPE DETECTION
PTRSTP:	0		;-1 => STOP READER AT NEXT BREAK (SET DURING BREAK WHEN BUFFER FULL)
PTRIP:	041000,,PTRBUF-1	;BUFFER INPUT (INTERRUPT) POINTER
PTROP:	041000,,PTRBUF-1	;BUFFER OUTPUT (IOT) POINTER
PTRBUF:	BLOCK REDSIZ	;READER BUFFER
PTRCC:	-PTRCHX		;NUMBER OF CHARACTERS IN BUFFER -PTRCHX

PTRIW:	0		;CURRENT WORD FOR IMAGE MODE
PTRIBP:	440600,,PTRIW	;POINTER TO CURRENT WORD
PTRICC:	0		;CHARACTER COUNT TO CURRENT IMAGE MODE WORD

BBLK

]		;END OF IFN PTRP

SUBTTL	LINE PRINTER ROUTINES

IFN NLPTP,[
NLPTO:	MOVE A,UTMPTR(U)
	SKIPGE NLUSR
	SKIPL TTYTYP-USRRCE(A)
	 JRST TPLO
	SETOM NLFREE
	MOVEI T,LPTBSZ
	CAME T,NLPTBR	;WAIT TILL LPT BUFFER IS EMPTY.
	 PUSHJ P,UFLS
	CONSZ NLPT,207	;AND LPT ISN'T STILL PRINTING LAST WD
	 PUSHJ P,UFLS
	PUSH P,[NLPTO1]
	JSP Q,STDOPN
	1,,NLUSR
	NLPTDN,,NLPTBN

NLPTO1:	POPJ P,		;LPT NOT AVAIL & NOT ASSIGNED.
			;LPT ASSIGNED, INIT IT.
	CONO NLPT,1000	;PUT LPT INTERFACE IN IMAGE MODE,
	DATAO NLPT,[23*2]	;TURN ON THE LPT'S MOTOR.
	MOVEI I,1000
	CONSZ NLPT,200	;WAIT TILL IT IS ON.
	 SOJG I,.-1
	AOS (P)
NLPTO3:	HRROI I,NLPTCF
	HRROI C,I
	PUSHJ P,BNLPTO	;CAN'T PCLSR SINCE BUFFER IS EMPTY.
	HRRZ I,NLPTBP
	HRLI I,010700
	MOVEM I,NLPTIP
	POPJ P,

NLPTCF:	15_29.+14_22.+177_15.+177_8+177_1
]

IFN GLPTP,[	;GOULD ELECTROSTATIC PRINTER ROUTINES

IFE DL10P,.ERR GOULD PRINTER WITHOUT A PDP11 TO RUN IT?

;OPEN ROUTINE

GLPTO:	SKIPE GLPTER
	 JRST OPNL7	;DEV NOT READY IF 11 UNHAPPY
	MOVEI Q,[1,,LPTUSR
		 GLPTDN,,GLPTBN]
	PUSHJ P,STDOPN
	 POPJ P,
	SETZM GLPGRF
	TLNE C,10	;BIT 3.4 => GRAPHICS MODE.
	 SETOM GLPGRF
	JRST POPJ1






;IOT ROUTINES

	SKIPA T,[SIOKT]	;SIOT HANDLER = UNIT HANDLER MINUS ONE
GLPTDO:	 MOVEI T,CHRKTO
	MOVE E,[440700,,5]
	JRST GLPTOO

GLPTBO:	MOVEI T,BLKT
	MOVE E,[444400,,1]
GLPTOO:	JSP B,(T)
	SETZ GLPMPP
	GLPMPC
	GLPBGB
	GLPBWW
	JRST 4,.
	SKIPE GLPTBC

;CLOSE ROUTINE

GLPCLS:	SKIPE GLPMPP		;IF THERE'S AN UN-SENT BUFFER,
	 PUSHJ P,GLPBWW		;BARK AT THE PDP11
	SKIPE GLPTBC		;WAIT FOR ALL BUFFERED STUFF TO BE SENT
	 PUSHJ P,UFLS
	PUSHJ P,GLPBG0		;AVOID IOCERR AT NEXT OPEN
	IRPS CH,,12 14 177 105
	 MOVE C,[SETZ I]	;AT CLOSE SEND FF, EOF
	 MOVEI I,CH		;THIS CAN'T PCLSR ONCE IT GETS A NEW BUFFER.
	 PUSHJ P,GLPTDO
	 TERMIN
	SETZM GLPGRF
	PUSHJ P,GLPBWW		;WRITE LAST BUFFER
	SETOM LPTUSR
	SETOM LPTUSE
	POPJ P,

;BUFFER GET ROUTINE

GLPBGB:	SKIPE GLPTER
	 JRST IOCER1		;DEV NOT READY IF 11 UNHAPPY ABOU IT.
	SKIPE GLPTBC
	 POPJ P,		;BUFFER BUSY
GLPBG0:	MOVEI TT,GLPTBF
	MOVEI J,GLPBSZ
	JRST SIOBGX

;BUFFER DISCARD ROUTINE

GLPBWW:	MOVE D,[450000,,GLPTBF-1]	;DL10 BYTE POINTER (7-BIT BYTES)
	SKIPE J,GLPGRF
	 HRLI D,340000		;GRAPHICS MODE => 8-BIT BYTES
	MOVEM D,GLPTBP
	SKIPN D,GLPMPP		;COMPUTE # CHARS IN BUFFER
	 POPJ P,
	JUMPL D,[SOJA D,GLPBW1]
	MOVEI TT,15		;INDEPENDENT OF E, BLOCK VS UNIT, ETC.
	TLNE D,760000
	 JRST [ IDPB TT,D ? JRST .-1 ]
GLPBW1:	MOVEI D,-GLPTBF+1(D)
	IMUL D,[4 ? 5]+1(J)	;GRAPHICS => 4, ELSE 5.
	MOVEM D,GLPTBC		;START THE PDP11
	SETZM GLPMPP		;FLAG THAT BUFFER IS NOT ACTIVE AT M.P. LEVEL
	SETZM GLPMPC
	POPJ P,

;NOTE:  TPL CODE HAS DELIBERATELY BEEN OMITTED.
;THE DL10 LIMITS THE BUFFER SIZE TO 256 WORDS.  WITH THIS SMALL
;A BUFFER THE SYSTEM JOB CAN'T DRIVE THE PRINTER AT FULL SPEED
;BECAUSE IT CAN ONLY IOT ONCE A SECOND OR SO.
;ANYWAY, THE NECESSARY TPL CODE IS HAIRY.
]	;END IFN GLPTP

IFN OLPTP,[

LPTLPP==60.

LPTO:	CAMN U,OLUSR
	JRST LPTO2	;ALREADY HAS IT
	MOVE A,UTMPTR(U)	;GET POINTER TO USER RESOURCE
			;(TTY DEPENDANT)
	SKIPL TTYTYP-USRRCE(A)
	JRST TPLO
LPTO2:	PUSH P,[LPTO1]
	JSP Q,STDOPN
	1,,OLUSR
	LPTDN,,LPTBN

LPTO1:	POPJ P,		;OPEN LOST
	SKIPE OLUSE	;OPEN WON
	JRST POPJ1	;NOT FIRST OPEN BY THIS USER
	AOS (P)
LPTRS2:	MOVEI A,15	;ENTRY FROM LPTRS, DUMP OUT CRUD ON THIS LINE THEN FORM FEED
	PUSHJ P,PILPT
	MOVEI A,14
	JRST PILPT

BLPTO:	MOVEI E,PILPT
	JRST NBTOCH

PILPT1:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)
	ANDI A,177
PILPT:	CAIN A,177
	POPJ P,		;IGNORE RUBOUTS
	MOVEI T,130.	;LPTCC COMPARED WITH 132. BY SYSTEM JOB BEFORE TRY OF .OPEN
	CAMLE T,LPTCC
	JRST LPTW1	;WAIT
LPTIM:	CAIGE A,40
	JRST LPTRC
	CAIL A,140
	SUBI A,40
LPTIM2:	AOSN LPTBSF
	PUSHJ P,LPC	;PRINT PREV STUFF AND SPACE OVER
	AOS T,LINEPOS
BADCOL:	CAIN T,-1	;PATCH NUM OF BAD COL IN HERE WHEN NECESSARY
	JRST LPTBC1
	CAILE T,120.
	JRST PILPT2
LPC1:	SUBI A,40
	IDPB A,LPTIP
	HRRZ A,LPTIP
	CAIN A,LPTBE-1
	MOVEI A,LPTBO
	HRRM A,LPTIP
	SOS LPTCC
	CONSO OLPT,7
	CONO OLPT,LPTCHN
	POPJ P,

LPTBC1:	PUSH P,A
	MOVEI A,40
	PUSHJ P,LPC1
	POP P,A
	JRST LPC1

LPTRC:	CAIN A,10
	JRST LPTBS	;BACK SPACE
	CAIN A,11
	JRST LPTTAB
	CAIN A,12
	JRST LPTLF
	CAIN A,14
	JRST LPTFF
	CAIN A,EOFCH
	JRST LPTEOF
	SETOM PCL
	CAIE A,15
	JRST LPTRCH
LPT.CR:	SETZM LINEPOS
	SETZM LPTBSF
	JRST LPC1

	PUSHJ P,LPTW2
LPTW1:	PUSHJ P,UFLS
	JRST LPTIM

LPTW2:	CAMLE T,LPTCC
	POPJ P,
	MOVE T,LPTCC
	CAIL T,<LPTBE-LPTBO>*2
	JRST POPJ1
	JRST POPJ3

LPTBS:	SKIPG LINEPOS
	POPJ P,	;AT BEG OF LINE, IGNORE
	SOS LINEPOS
	SETOM LPTBSF
	POPJ P,

LPTRCH:	CAIN A,33
	JRST LPTALT
	MOVE B,A
	MOVEI A,"^
	PUSHJ P,LPTIM2
	MOVEI A,100(B)
	JRST LPTIM2

LPTALT:	MOVEI A,"$
	JRST LPTIM2

PILPT2:	SOS LINEPOS
	POPJ P,

LPTEOF:	SKIPE OLUSR
	POPJ P,
	SUB P,[1,,1]	;SYSTEM JOB HACKING  SO CLOBBER TO EOF
	XCTR XRW,[CLEARM (C)]
	POPJ P,

LPTCLS:	SKIPLE OLUSE	;LPT CLOSE, IS THIS THE ONLY CHANNEL LPT OPEN ON
	JRST LPTCL3
	SKIPN LINEPOS	;THIS IS LAST CLOSE, CHECK TO SEE IF LOSER LEFT POSITION SCREWED
	SKIPE LPTPPS
	PUSHJ P,LPTCL2	;INCREMENT TO NEXT PAGE
	MOVEI T,SCRTPC
	IORM T,SUPCOR	;LOOK FOR TPL FILES TO PRINT
	SETOM OLUSR	;DO THIS FIRST SO DONT NEED CLOCK OFF LPT HACKED BY ONE JOB AT A TIME
LPTCL3:	SOS OLUSE	;DECREMENT USE COUNT
	POPJ P,

LPTCL2:	MOVEI A,100.
	CAMLE A,LPTCC
	POPJ P,	;NO ROOM FORGET IT
	MOVEI A,15
	PUSHJ P,LPTIM
	MOVEI A,14
	JRST LPTIM
LPTTAB:	PUSHJ P,LPTS
	LDB J,[300,,LINEPOS]
	JUMPE J,CPOPJ
	JRST .-3

LPTS:	MOVEI A,40
	JRST LPTIM2

LPTLF:	AOS J,LPTPPS	;LINE FEED, INCREMENT LINE #
	CAIL J,LPTLPP	;END OF PAGE?
LPTFF:	SETZM LPTPPS	;END OF PAGE CLEAR OUT PAGE POS (ENTRY FOR FF)
	PUSHJ P,LPC
	JRST LPC1

LPC:	MOVE J,LINEPOS	;CR AND SPACE BACK OVER TO LINPOS
	MOVE B,A
	MOVEI A,15
	AOSN PCL
	SKIPE LINEPOS
	PUSHJ P,LPT.CR
	CLEARM LPTBSF	;IN CASE WAS ALREADY AT BEG OF LINE
	SOJL J,LPC2
	PUSHJ P,LPTS
	JRST .-2

LPC2:	MOVE A,B
	POPJ P,

EBLK

LPTCC:	<LPTBE-LPTBO>*5-5
LINEPOS:	0	;LINE POSITION # CHARS PRINTED THIS LINE BEFORE CURRENT
LPTPPS:	0	;PAGE POSITION (# LINES PRINTED BEFORE CURRENT)

PCL:	0	;INCREMENTED BY LF OR FF, SETOMED BY OHTER CONTROL CHARS
	;PROBABLY UNNECESSARY BUT NOT WORTH FLUSHING
LPTBSF:	0	;-1 => BACKSPACES HAVE BEEN TYPED, CR AND SPACE OVER BEFORE MAKING INK

LPTIP:	700,,LPTBO-1
LPTOP:	700,,LPTBO-1

BBLK

LPTCN:	CAIN A,<15-40>&177
	JRST LPTCR	;CRR
	CAIN A,<12-40>&177
	MOVEI A,400	;LINE FEED
	CAIN A,<14-40>&177
	MOVEI A,401	;FORM FEED
	CAIE A,177	;SKIP IF LOOKS LIKE A LEFT ARROW
	JRST LPTCN1	;GO TO PRINT
	HRRZ A,LPTOP	;LOAD OUPUT POINTER
	CAIE A,LPTBE-1	;SKIP IF CURRENT PNTR = END OF BUFFER
	JRST LPENT3	;REAL LEFT ARR
	MOVE A,[350700,,LPTBO]
	MOVEM A,LPTOP
	JRST LPENT2
]

IFN NLPTP,[
NLPT1:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)
	MOVE B,NLPTIP
	TLNN B,760000
	JRST NLPTW	;MAY NOT BE ROOM
NLPTW1:	IDPB A,NLPTIP
	MOVE T,NLPTIP
	CAMN T,NLPTEP
	SUBI T,LPTBSZ
	HRRM T,NLPTIP
	TLNE T,760000
	POPJ P,
	SOSGE NLPTBR
	JRST 4,.
NLPTGO:	CONO PI,LPTOFF
	CONSO NLPT,7
	CONO NLPT,100+LPTCHN	;SET OUTPUT DONE
	JRST LPTONJ

NLPTW:	SKIPG NLPTBR
	PUSHJ P,UFLS
	JRST NLPTW1

EBLK

NLPTIP:	010700,,NLPTBO-1
NLPTBO:	BLOCK LPTBSZ
NLPTEP:	010700,,NLPTBO+LPTBSZ-1
NLPTPP:	NLPTBO	;OUTPUT PNTR (PI)
NLPTBR:	LPTBSZ
NLPTBP:	004400,,NLPTBO-1	;BLKT POINTER
NLPTBC:	LPTBSZ	;BLKT CNT = MIN(# WDS TILL END OF BUF,NLPTBR)
NLPTOP:	NLPTBO	;VALUE OF NLPTBP BEFORE BLKT CALL
NLFREE:	-1	;-1 => LPT IN USE RECENTLY.
		;0 => NOT IN USE SINCE LAST VSCLK.
		;1 => NOT SINCE NEXT-TO-LAST VSCLK.
		;=>2 => LPT MOTOR NOW OFF.

BBLK

BNLPTO:	MOVE B,NLPTBP
	MOVEM B,NLPTOP
	PUSH P,[BNLPT1]
	MOVE E,[444400,,1]
	JSP B,BLKT
	SETZ NLPTBP
	NLPTBC
	BNLPTG
	BNLPT1
	@J
	SKIPG NLPTBR

BNLPT1:	MOVE T,NLPTBP
	MOVE W,T
	CAMN T,[004400,,NLPTBO+LPTBSZ-1]
	MOVE T,[004400,,NLPTBO-1]
	MOVEM T,NLPTBP
	EXCH T,NLPTOP
	SUB T,W	;- # WORDS WRITTEN
	ADDB T,NLPTBR	;DECREASE ROOM
	JUMPL T,[JRST 4,.]
	JRST NLPTGO

BNLPTG:	MOVE TT,NLPTBP
	MOVE J,NLPTBR
	MOVEI T,NLPTBO+LPTBSZ-1
	SUBI T,(TT)	;# WORDS UNTIL END OF BUFFER
	CAMLE J,T
	MOVE J,T
	MOVEM J,NLPTBC
	JRST POPJ1

NLPTCL:	SKIPLE NLPTBR	;ENOUGH ROOM?
	PUSHJ P,NLPTO3
	SETOM NLFREE
	SETOM NLUSR
	SETOM NLUSE
	POPJ P,

NLPDCL:	MOVSI H,760000
	SKIPA A,[177]
NLPDC1:	PUSHJ P,NLPTW1
	TDNE H,NLPTIP
	JRST NLPDC1
	HRRZ A,NLPTIP
	CAIN A,NLPTBO+LPTBSZ-1
	MOVEI A,NLPTBO-1
	HRLI A,004400
	MOVEM A,NLPTBP
	MOVNI A,(A)
	ADDI A,NLPTBO+LPTBSZ-1
	MOVE B,NLPTBR
	CAMLE A,B
	MOVE A,B
	MOVEM A,NLPTBC
	JRST NLPTCL
]

IFN NTYP\OLPTP\NLPTP\PLTP\340P\PDP6P\STKP\PTRP,[

EBLK
LPTA:	0
LPTB:	0
LPTACS:	BLOCK 15

LPTBRK:	0
BBLK
	MOVEM A,LPTA
IFN NTYP,[		;A KLUDGE FOR THE PDP 11 ON DM
	CONI NTY,A
	TRNE A,7	;NO PIA
	 TRNN A,200000	;NO FLAG
	  CAIA
	   JRST NTYBRK	;AHH, FLAG AND PIA
]
	
IFN OLPTP,[
	CONSZ OLPT,7
	CONSZ OLPT,10
	JRST .+2
	JRST LPENT1	;LINE PRINTER
]
IFN NLPTP,[
	CONSZ NLPT,7
	CONSO NLPT,100	;DONE
	JRST .+2
	JRST NLPENT	;NEW LINE PRINTER
]
IFN 340P,[
	CONI DIS,A
	TRNN A,77
	JRST LPTBR2
	CONSZ DIS,7400
	JUMPL A,SRECYC	;SPECIAL DISPLAY CHNL
]
LPTBR2:	MOVEM B,LPTB
IFN PLTP,[
	CONSZ PLT,SDS+PDS
	JRST PLTBRK	;PLOTTER
]
IFN PTRP,[
	CONSZ PTR,7
	CONSO PTR,10
	JRST .+2
	JRST PTRBRK
	CONSZ PTP,10
	JRST PBRK1	;PAPER TAPE PUNCH
]
IFN PDP6P,[
	CONSO DSDEV,7
	JRST .+3
	CONSZ DSDEV,10
	JRST DSDBRK	;INTERRUPT FROM OTHER PROCESSOR
]
IFN STKP,[
	CONSZ STK,7
	CONSO STK,10
	JRST .+2
	JRST STKINT	;STANFORD KEYBOARD
]
LPBKRT:	MOVE A,LPTA
	JRST 12,@LPTBRK

LPTRT1:	MOVS A,[C,,LPTACS]
	BLT A,17
LPTRT3:	MOVE B,LPTB
	MOVE A,LPTA
	JRST 12,@LPTBRK
]	  ;END IFN OLPTP\ ETC.
.ELSE [
EBLK
LPTBRK:	0
BBLK
	JRST 4,.
]

IFN OLPTP,[
LPENT3:	MOVEI A,177
	JRST LPTCN1

LPTCR:	CONO OLPT,10+LPTCHN	;PRINT LINE
	AOS LPTCC
	JRST LPBKRT

LPENT1:	MOVEI A,<LPTBE-LPTBO>*5-5
	CAMG A,LPTCC
	JRST LPTSTP
LPENT2:	ILDB A,LPTOP
	TRNE A,100
	JRST LPTCN
LPTCN1:	DATAO OLPT,A	;OUTPUT CHARACTER
	AOS A,LPTCC
	CONSZ OLPT,10
	JRST LPBKRT
	CAIGE A,<LPTBE-LPTBO>*5-5
	JRST LPENT2
LPTSTP:	CONO OLPT,0	;CLEAR PIA
	MOVEI A,SCRTPC
	SKIPGE LPTUSR
	 IORM A,SUPCOR	;LPT NOW IDLE
	JRST LPBKRT

EBLK

LPTBO:	BLOCK LPTBSZ-1
	-1
LPTBE:

BBLK

STALPT:	MOVE E,LINEPOS
	DPB E,[130700,,D]
	DPB A,[60300,,D]	;MODE
	MOVE E,LPTCC
	CAIG E,150.
	TRO D,1_9.
	CAIL E,30000
	TRO D,1_<9.+1>
	POPJ P,

LPTRS:	CONO PI,LPTOFF
	MOVEI A,5*<LPTBE-LPTBO>-5
	MOVEM A,LPTCC
	MOVE A,[700,,LPTBO-1]
	MOVEM A,LPTIP
	MOVEM A,LPTOP
	SETZM LINEPOS
	CONO PI,LPTON
	JRST LPTRS2

]

IFN NLPTP,[
NLPENT:	MOVE A,NLPTBR
	CAIN A,LPTBSZ
	JRST NLPTST
	CAIL A,LPTBSZ
	JRST 4,.
	DATAO NLPT,@NLPTPP
	AOS A,NLPTPP
	CAIN A,NLPTBO+LPTBSZ
	MOVEI A,NLPTBO
	MOVEM A,NLPTPP
	AOS NLPTBR
	JRST LPBKRT

NLPTST:	CONO NLPT,200	;CLEAR DONE AND PIA
	MOVEI A,SCRTPC
	SKIPGE NLUSR
	 IORM A,SUPCOR	;LPT NOW IDLE
	JRST LPBKRT

NLPTRS:	CONO PI,LPTOFF
	MOVE A,[010700,,NLPTBO-1]
	MOVEM A,NLPTIP
	MOVEI A,NLPTBO
	MOVEM A,NLPTPP
	MOVE A,[004400,,NLPTBO-1]
	MOVEM A,NLPTBP
	MOVEM A,NLPTOP
	MOVEI A,LPTBSZ
	MOVEM A,NLPTBR
	MOVEM A,NLPTBC
	CONO PI,LPTON
	JRST NLPTO3
]

SUBTTL	STANFORD KEYBOARD

IFN STKP,{
;3.4 => DON'T HANG ON IOT IF NO CHAR (RETURN -1 INSTEAD)
;3.5 = 1 => DON'T DO CHAR CONVERSION
;    = 0 => CONVERT ACCORDING TO OTHER BITS
;3.6 = 1 => ITS MODE (CTRL DOES CTRL AND META IS 8TH BIT)
;    = 0 => STANFORD MODE (400 = META, 200 = CTRL, AND ALL THE
;           CHARS FROM 1-177 CAN BE GENERATED WITH TOP AND SHIFT.)
;3.7 = 1 => IF 3.5 = 0, 3.6 = 1, THEN DON'T INPUT META BIT

STKO:	TLNE C,3	;BLOCK OR OUTPUT
	JRST OPNL12	;MODE NOT AVAILABLE
	CONO PI,CLKOFF
	SKIPL STKUSR
	JRST OPNL10	;DEVICE NOT AVAILABLE
	MOVEM U,STKUSR
	AOSE STKUSE
	JRST 4,.	;CAN ONLY BE OPEN ONCE
	CONO PI,CLKON
	HRRZ A,UUAC(U)
	MOVE A,CHNBIT(A)
	ROT D,-2
	DPB D,[220400,,A]
	MOVEM A,STKMSK
	MOVE A,STKBP
	MOVEM A,STKIP
	MOVEM A,STKOP
	SETZM STKBC
	DATAI STK,A	;CLEAR DONE
	CONO STK,LPTCHN
	MOVEI A,STKDP
	MOVEM A,(R)
	JRST POPJ1

STKCLS:	CONO STK,0	;CLEAR PIA
	SOSL STKUSE
	JRST 4,.	;CAN ONLY BE OPEN ONCE
	SETOM STKUSR
	POPJ P,

STKRS:	CONO PI,LPTOFF
	MOVE A,STKBP
	MOVEM A,STKIP
	MOVEM A,STKOP
	SETZM STKBC
	JRST LPTONJ

STKI:	MOVE TT,STKMSK
	TLNE TT,1
	 JRST STKI1
	PCLT
	SKIPG STKBC
	 PUSHJ P,UFLS
STKI2:	ILDB A,STKOP
	SOS STKBC
	TLNN TT,2
	 JRST STKCV	;DO CONVERSION
STKI3:	MOVE W,A
	MOVE A,STKOP
	CAMN A,STKEP
	SUBI A,STKBL
	HRRM A,STKOP
	POPJ P,

STKI1:	SKIPLE STKBC
	JRST STKI2
	JRST STTYI3

STKINT:	MOVEM B,LPTB
	DATAI STK,B
	ANDI B,1777	;CURRENTLY 10 BITS
	MOVE A,STKBC
	CAIL A,STKBS
	JRST LPTRT3	;NO ROOM.  JUST IGNORE CHAR
	IDPB B,STKIP
	AOS STKBC
	MOVE A,STKIP
	CAMN A,STKEP
	SUBI A,STKBL
	HRRM A,STKIP
	MOVE A,STKUSR
	HRRZ B,STKMSK
	AND B,MSKST2(A)
	IORM B,IFPIR(A)
	JRST LPTRT3

EBLK
STKBL==40
STKBS==STKBL*3
STKIP:	001400,,STKIB-1
STKIB:	BLOCK STKBL
STKEP:	001400,,STKIB+STKBL-1
STKBP:	001400,,STKIB-1
STKOP:	001400,,STKIB-1
STKBC:	0
STKMSK:	0	;BIT FOR CHANNEL STK OPEN ON
		;3.1 => DON'T HANG ON IOTS MODE
		;3.2 => DON'T CONVERT CHARS
		;3.3 => ITS MODE
		;3.4 => DON'T INPUT META IN ITS MODE
BBLK

STKCV:	LDB B,[000600,,A]	;LOW 6 BITS
	CAIN B,33
	JRST STKCV1	;33
	CAIL B,35
	CAILE B,47
	JRST .+2
	JRST STKCV2	;35-47
	CAIL B,74
	CAILE B,77
	JRST .+2
	JRST STKCV3	;74-77
	TRNE A,400
	JRST STKCVT	;TOP IS ON
	CAIG B,32
	JRST STKCV4	;0-32
	CAIN B,34	;SKIP IF 50-73
	JRST STKCVB	;34
STKCVX:	TLNN TT,4	;SKIP ON ITS MODE
	JRST STKCVY	;STANFORD MODE
	ANDI A,1000	;META BIT
	LSH A,-2
	TLNN TT,10	;SKIP ON DON'T INPUT META BIT
	IORI B,(A)
	MOVE A,B
	JRST STKI3

STKCVY:	LDB D,[110100,,A]	;META BIT
	LSH D,8		;IN 400 POSITION
	ANDI A,200	;CTRL BIT
	IORI A,(D)
	IORI A,(B)
	JRST STKI3

STKCV1:	MOVEI B,15
	JRST STKCVX

STKCV2:	MOVEI Q,-35(B)	;35-47 (-35)
	IDIVI Q,5
	LDB B,STKBPT(J)
	JRST STKCVX

STKCV3:	CAIE B,74
	JRST STKCV5	;75-77
	MOVEI B,177
	JRST STKCVX

STKCV5:	MOVEI B,33
	JRST STKCVX

STKCVT:	CAIG B,32	;TOP IS ON
	JRST STKCT1	;0-32
	CAIN B,34
	JRST STKCT2	;34
	MOVEI Q,-50(B)	;50-73 (-50)
	MOVE D,B
	IDIVI Q,5
	LDB B,STKBP1(J)
	TRNE A,200	;SKIP IF CTRL IS OFF
	TLNN TT,4	;SKIP IF ITS MODE
	JRST STKCVX
	CAIL D,52
	CAILE D,72
	TRZ B,100	;50,51,73
	JRST STKCVX

STKCT1:	MOVE Q,B	;0-32 TOP ON
	MOVE D,B
	IDIVI Q,5
	LDB B,STKBP2(J)
	TRNE A,200	;SKIP IF CTRL IS OFF
	TLNN TT,4	;SKIP IF ITS MODE
	JRST STKCVX
	CAIN D,12
	TRZ B,100
	JRST STKCVX

STKCT2:	MOVEI B,16
	JRST STKCVX

STKCV4:	TLNN TT,4	;SKIP IF ITS MODE
	JRST STKCV6
	TRNE A,200
	JRST STKCVX	;ITS, CTRL
STKCV6:	TRO B,100
	TRNN A,100
	TRO B,40	;SHIFT WAS OFF
	JRST STKCVX

STKCVB:	MOVEI B,134
	TRNE A,200	;SKIP IF CTRL IS OFF
	TLNN TT,4	;SKIP IF ITS MODE
	JRST STKCVX
	TRZ B,100
	JRST STKCVX

DEFINE	STKM A,B,C,D,E
	A_29.+B_22.+C_15.+D_8+E_1
TERMIN

DEFINE	STKM1 TBL
	350700,,TBL(Q)
	260700,,TBL(Q)
	170700,,TBL(Q)
	100700,,TBL(Q)
	010700,,TBL(Q)
TERMIN

STKBPT:	STKM1 STKTBL
STKTBL:	STKM 12,0,0,40,0
	STKM 33,0,0,11,14
	STKM 13,0,0,0,0

STKBP1:	STKM1 STKTB1
STKTB1:	STKM 133,135,26,174,41
	STKM 5,77,17,60,36
	STKM 22,23,20,21,44
	STKM 45,32,30,1,136

STKBP2:	STKM1 STKTB2
STKTB2:	STKM 0,34,7,6,74
	STKM 100,76,33,75,47
	STKM 137,31,27,25,24
	STKM 173,176,4,43,35
	STKM 46,140,10,37,3
	STKM 42,2,0,0,0
};IFN STKP

SUBTTL	INTERFACE (?) TO DM PDP11 ON NTY (REALLY WANT A CAIOS)
IFN NTYP,[
IFN TK10P,	.ERR	YOU REALLY WILL LOSE WITH BOTH TTY AND 11 ON NTY
NTYOPN:	PUSHJ P,NTYOP1	;SET UP OPEN
	 POPJ P,	;OPEN LOST?
	SKIPE NTYUSE	;IF THIS IS FIRST OPEN, DO SOME INITIALIZING
	 JRST POPJ1
	CONO NTY,0	;STOP INTS
	SETOM NTYOAC
	MOVE A,[NTYIOI,,NTYIOR]
	BLT A,NTYORM	;INITIALIZE ALL THE POINTERS AND COUNTERS
	CONO NTY,620000+NTYCHN
	JRST POPJ1

NTYOP1:	ROT D,1		;MODE BACK, TO GET INPUT & OUTPUT
	JSP Q,STDOPN
	 3,,NTYUSR
	 NTYIT,,NTYOT
	 0,,0		;NTYBIT,,NTYBOT

NTYRS==CPOPJ

NTYCLS:	SOSL NTYUSE	;-1 MEANS FREE (USE STDCLS?)
	 POPJ P,
	SETOM NTYUSE
	SETOM NTYUSR
	POPJ P,

;MAIN PROGRAM ROUTINES FOR IOT/SIOT ...

NTYBI==IOCR10
NTYBO==IOCR10

NTYTO::
NTYOPT:	PCLT			;PUT OUTPUT INTO BUFFER
	SKIPG C
	 SKIPA A,(C)
	  UMOVE A,(C)		;FETCH THE USERS BYTE
	SKIPG NTYORM		;WAIT FOR ROOM
	 PUSHJ P,UFLS
	IDPB A,NTYOIP
	SOSGE A,NTYORM
	 JRST 4,.		;WENT BELOW ZRO?
	AOSN NTYOAC
	 CONO NTY,10+NTYCHN	;NOT ACTIVE, START PI LEVEL
	AOSGE NTYOIR
	 POPJ P,
	MOVE A,[NTYOII,,NTYOIR]	;RING AROUND THE ROSY
	BLT A,NTYOIP
	POPJ P,
NTYTI::
NTYIGT:	MOVEI T,NTYIBS*4
	PCLT
	CAMG T,NTYIRM
	 PUSHJ P,UFLS		;WAIT UNTIL A BYTE COMES IN
	ILDB W,NTYIOP
	AOS NTYIRM		;NOW MORE ROOM
	AOSGE NTYIOR		;RING?
	 POPJ P,
	MOVE B,[NTYIOI,,NTYIOR]	;REDO POINTERS
	BLT B,NTYIOP
	POPJ P,

;INTERRUPT ROUTINES

NTYBRK:	TRC A,2000		;DEVICE #
	TRNE A,7000
	 JRST NTYSEL		;STOPPED ON OTHER DEVICE
	TRNE A,40
	 JRST NTYIPT		;INPUT
	TRNE A,20
	 JRST NTYOGT		;OUTPUT
NTYRET:	CONO NTY,620000+NTYCHN	;RESTART
	JRST LPBKRT

NTYSEL:	TRC A,2000		;HAVE TO KNOCK DOWN SOME OTHER FLAG
	ANDI A,7000
	LSH A,3
	CONO NTY,400000+NTYCHN(A)
	CONSZ NTY,40
	 DATAI NTY,A		;CLEAR INPUT
	CONSZ NTY,20
	 CONO NTY,20+NTYCHN	;CLEAR OUTPUT
	AOS NTYERR
	JRST NTYRET

NTYOGT:	MOVEI A,NTYOBS*4
	CAMG A,NTYORM
	 JRST  [SETOM NTYOAC
		CONO NTY,20+NTYCHN
		JRST NTYRET]
	ILDB A,NTYOOP
	AOS NTYORM
	DATAO NTY,A
	AOSGE NTYOOR
	 JRST NTYRET
	MOVE A,[NTYOOI,,NTYOOR]
	BLT A,NTYOOP		;ZAP THE POINTER AND COUNT (RING BUF)
	JRST NTYRET

NTYIPT:	SOSG NTYIRM
	 CONO NTY,100+NTYCHN	;IF HDWE NOT IN, BUFFER WILL BE CLOBBERED
				;BUT ALSO, THE COUNT WILL BE NEG!!!
	DATAI NTY,A
	IDPB A,NTYIIP
	AOSGE NTYIIR
	 JRST NTYRET
	MOVE A,[NTYIII,,NTYIIR]
	BLT A,NTYIIP		;RING THE BUFFER POINTERS
	JRST NTYRET

;DATA AREAS FOR THE NTY INTERFACE TO DM PDP 11

NTYIBS==1000
NTYOBS==100

NTYIOI:	-NTYIBS*4	;STUFF TO INITIALIZE INPUT SIDE
	041000,,NTYIBF-1
NTYIII:	-NTYIBS*4
	041000,,NTYIBF-1
	NTYIBS*4

NTYOOI:	-NTYOBS*4	;INITIALIZES OUTPUT SIDE
	041000,,NTYOBF-1
NTYOII:	-NTYOBS*4
	041000,,NTYOBF-1
	NTYOBS*4

EBLK

NTYIOR:	-NTYIBS*4	;RING COUNTER, INPUT BUFFER, OUTPUT PART
NTYIOP:	041000,,NTYIBF-1 ;BYTE POINTER, "   "
NTYIIR:	-NTYIBS*4	;RING CTR, INPUT BUFFER, INPUT PART
NTYIIP:	041000,,NTYIBF-1 ;BYTE POINTER, "   "
NTYIRM:	NTYIBS*4	;ROOM EMPTY IN IN BUFFER

NTYOOR:	-NTYOBS*4	;RING CTR, OUTPUT BUFF, OUT PART
NTYOOP:	041000,,NTYOBF-1 ; BYT PTR,  " "
NTYOIR:	-NTYOBS*4	;RING CTR, OUT BUFF, IN PART
NTYOIP:	041000,,NTYOBF-1 ;BYT PTR, "  "
NTYORM:	NTYOBS*4	;ROOM IN THE OUTPUT BUFFER

NTYIBF:	BLOCK NTYIBS	;INPUT BUFFER
NTYOBF:	BLOCK NTYOBS	;OUTPUT BUFFER

NTYOAC:	-1		;FLAG, OUTPUT ACTIVE UNLESS -1
NTYUSR:	-1
NTYUSE:	-1

NTYERR:	0

	BBLK
];NTYP

SUBTTL	PDP-10 TO PDP-11 INTERFACE AND XGP

IFN TEN11P,[
EBLK
T11MP:	BLOCK 256.	;CONTENTS OF TEN11 MAP, -1 => CONFLICT PAGE
BBLK
			;ELSE 0 => AVAIL
			;4.9=1 => CONTENTS OF MAP WD EXCEPT 4.7-4.3 => USE COUNT
IFN XGP,XGPTP==376	;PAGE NUMBER IN 10-11 RESERVED FOR CLK LEVEL XGP HACKS

;PLACE WD IN A IN AVAIL 10-11 MAP ENTRY (RETURN IN RH B)
T11AD:	SKIPE TEN11F
	POPJ P,
	MOVE C,A
	JSP Q,T11FND	;SKIP UNLESS NO MAP ENTRIES AVAIL
	JRST T11A1	;WD ALREADY IN PAGE
	MOVEI C,0	;FIND FREE PAGE
	JSP Q,T11FND
	JRST T11A2
	POPJ P,	;NO FREE PGS AVAIL

T11A1:	LDB E,[350500,,T11MP(B)]
	CAIN E,37
	JRST T11FN2	;USAGE COUNT MAX FOR THAT ENTRY
	AOS E
	DPB E,[350500,,T11MP(B)]
	JRST POPJ1


T11A2:	MOVEM A,T11MP(B)
	MOVEM A,400000+T11CP*2000(B)	;STORE IN HARDWARE
	JRST POPJ1

T11FND:	MOVSI B,-256.
T11FN1:
IFN XGP,[	CAMN B,[-256.+XGPTP,,XGPTP]	;DON'T ALLOCATE SPECIAL XGP ENTRY TO OTHER USERS
	JRST T11FN2
]
	MOVE E,T11MP(B)
	TLZ E,174000	;USE COUNT BITS
	CAMN E,C
	JRST (Q)	;FOUND IT
T11FN2:	AOBJN B,T11FN1
	JRST 1(Q)	;CANT FIND IT


;DELETE REF TO T11 PAGE IN B
T11DL:	SKIPE TEN11F
	POPJ P,
	MOVE E,T11MP(B)
	AOJE E,CPOPJ	;REALLY CONFLICT PAGE
	LDB E,[350500,,T11MP(B)]
	SOJL E,T11DL1	;DELETING LAST REF TO PAGE?
	DPB E,[350500,,T11MP(B)]
	POPJ P,

T11DL1:	CLEARM T11MP(B)
	CLEARM 400000+T11CP*2000(B)
	POPJ P,
]

IFN TEN11P,[

;.CALL ADR
;	LOSE
;	WIN
;ADR/	SETZ
;	SIXBIT /T11MP/
;	ARG 1 VIRT PAGE #
;	ARG 2 DESIRED T11 MAP WORD

NT11M:	TDNE A,[-400]	;TEMP? TEN11 CALL A HAS USER PG
	 JRST OPNL33	;INVALID ARG - BAD USER PAGE #.
	SKIPE TEN11F
	 JRST OPNL7	;DEVICE NOT READY.
	PUSHJ P,UPLC
	MOVE W,A
	JUMPGE B,OPNL33
	TLZ B,174000	;DONT ALLOW ANYTHING IN USAGE COUNT BITS
	MOVE D,B
	LDB E,Q
	JUMPE E,NT11M1	;PAGE MUST BE FREE OR ABS, OTHERWISE ERROR
	CAIE E,-1
	 JRST OPNL13	;FILE ALREADY EXISTS
	LDB B,T
	ANDI B,PMRCM
	LDB E,[.BP <PMRCM-377> B]	;GET MOBY #
	ANDI B,377
	CAIN E,T11CPA_-18.
	PUSHJ P,T11DL
	MOVEI B,0
	DPB B,Q
	DPB B,T
NT11M1:	MOVE R,Q
	MOVE A,D
	PUSHJ P,T11AD
	 JRST OPNL37	;NO CORE AVAILABLE.
	ADDI B,200000+<<T11CPA_-18.>_8>	;ADDRESS CORRECT MOBY
	TLNE A,200000
	TRO B,400000	;WRITE PERMISSION
	DPB B,T
	MOVEI C,-1
	DPB C,R
	LSH W,10.
	ADDI W,2000
	CAMLE W,HUSRAD(U)
	MOVEM W,HUSRAD(U)
	JRST POPJ1
]
.ELSE NT11M:	JRST OPNL1	;NO SUCH DEVICE.
IFN XGP,[
;CLOCK LEVEL XGP CODE FOR FILLING IMAGE MODE BUFFERS
;ASSUMES XGP11P IS POINTER TO FIRST 11 RING NODE
;AND THAT THE 11 BUFFER RING STRUCTURE IS:

;	POINTER TO NEXT NODE OF BUFFER
;	USE WORD	(0=> EMPTY)
;	POINTER TO BUFFER (ASSUMED TO BE 1K 10 STYLE, 2K 11 STYLE)

;
;THE NODES AND THE BUFFERS MUST BEGIN ON EVEN PDP-11 WORDS
;I.E. 11 ADDRESSES ENDING WITH TWO ZEROES.

;THIS CODE USES A SPECIAL 10-11 INTERFACE ENTRY
;NAMELY, XGPTP, AND THREE EXEC PAGES, XGPC WHICH POINTS TO
;THE LOW 2K OF PDP-11 CORE, USED FOR THE RING NODE POINTERS
;XGPUB WHICH POINTS TO THE CURRENTLY HACKED USER PAGE
;AND XGPB WHICH POINTS PERMANENTLY TO THE 10-11 PAGE CORRESPONDING
;TO XGPTP

XGP11==1	;PDP-11 RUNNING XGP
XGPCA==XGPC*2000+400000	;EXEC ADR OF LOW PDP-11 CORE
XGPBA==XGPB*2000+400000	;EXEC ADR OF BUFFER IN PDP-11
XGPUBA==XGPUB*2000+400000	;EXEC ADR OF CURRENT PAGE IN USER'S MAP
XGNTPG==117.	;MAX # TIED DOWN PAGES (WOULD YOU BELIEVE...)
X11BFP==102	;LOC OF XGP BUFF HEAD IN 11 (PDP11 ADR 410)

XGPCLK:	PUSH P,A
	PUSH P,B
	MOVE A,XGPTM1	;TIME FIRST PAGE LOCKED IN CORE
	ADDI A,30.*60.	;1 MINUTE
	CAMGE A,TIME	;WIN?
	JSP Q,XGPFLS	;LOSE...
	SKIPL XGPINI	;DON'T HACK UNTIL MAIN PGM HAS PAGES TIED DOWN
	JRST POPBAJ
	SPM XGPSPM	;SAVE OLD MAP
XGPC1:	MOVE A,XGP11P	;GET POINTER TO FIRST PDP-11 BUFFER
	MOVE A,XGPCA(A)	;GET CONTENTS FROM PDP-11
	LDB B,[242000,,A]	;GET NEXT RING POINTER
	LDB A,[042000,,A]	;GET USE WORD
	JUMPN A,POPBAJ	;BUFFERS FULL
	TRNE B,3
	JSP Q,XGPFLS	;NEW BUF POINTER NOT ON EVEN WD BNDY
	LSH B,-2	;CONVERT TO 10 ADDRESS
	SKIPE B
	CAIL B,2000	;IS IT WITHIN CONTROL PAGE?
	JSP Q,XGPFLS	;FLUSH
	EXCH B,XGP11P	;SETUP NEXT POINTER, GET BACK OLD ONE
	MOVEM B,XGPOB	;SAVE OLD POINTER FOR HACKING BUFF FULL
	MOVE B,XGPCA+1(B)	;GET POINTER TO 11 BUFFER
	LDB B,[242000,,B]
	TRNE B,3
	JSP Q,XGPFLS
	LSH B,8		;SETUP 10-11 ENTRY
	IOR B,[600000+<XGP11_8>,,1777]	;R/W, PROT INFINITE
	MOVEM B,400000+2000*T11CP+XGPTP	;GRONK INTO 10-11 INTERFACE
	MOVEM B,T11MP+XGPTP	;FOR DEBUGGING ONLY
	ILDB A,XGP10P	;GET POINTER TO NEXT 10 PAGE
	CAIL A,TSYSM	;LOSE
	JRST 4,.	;MAIN PGM HAS POINTER TO NXM
	JUMPE A,XGPFIN	;HERE WE ARE DONE, I HOPE.  DEUS GRATIUS
	MOVE B,A	;SAVE MMSWP ENTRY # TO CLEAR OUT TIE DOWN
	IORI A,600000	;R/W
	DPB A,[.XGPUB+EXEUMP]	;GRONK EXEC MAP
	LPMR XGPSPM	;RESET ASSOC REGS
	MOVE A,[XGPUBA,,XGPBA]
	BLT A,XGPBA+1777	;DO THE STUFF!
	MOVSI A,-1
	ADDM A,MMSWP(B)	;CLEAR TIE DOWN, PHEW!
	SKIPGE MMSWP(B)	;STOP BUGGY LOSSAGE
	JRST 4,.
	PUSHJ P,XGPBIT	;GET TABLE INFO
	TDNN A,XGPMTB(B)
	JRST 4,.	;UNTIED PAGE NOT TIED DOWN
	ANDCAM A,XGPMTB(B)	;CLEAR THE BIT
	MOVEI A,0
	DPB A,[.XGPUB+EXEUMP]	;CLEAR EXEC PAGE MAP
	MOVE A,XGPOB	;GET OLD RING PTR
	MOVEI B,30	;CHANGE RH ONLY TO A 1
	MOVEM B,XGPCA(A)	;SET BUFFER FULL
	JRST XGPC1	;LOOP TO GET A NEW BUFFER



XGPFIN:	SETZM XGPINI
	JSP Q,XGPFLS

;MMSWP ENTRY IN B,
;RETURNS BIT IN A
;WD OF TABLE IN B
XGPBIT:	MOVE A,B
	IDIVI A,36.
	MOVNS B
	PUSH P,A
	MOVSI A,400000
	LSH A,(B)
	JRST POPBJ

;CALLED FROM CORE ALLOCATOR WHEN FLUSHING PAGES
;OF A JOB WHICH HAVE LH OF MMSWP NON ZERO.

XGPCFL:	CAME U,XGPUSR	;ARE WE CONCERNED?
	POPJ P,	;NOPE
	PUSH P,A
	PUSH P,B
	MOVE B,TT	;B GETS MMSWP INDEX
	PUSHJ P,XGPBIT	;GET INDEXES INTO XGPMTB
	TRNN A,XGPMTB(B)	;IS THIS A PAGE BEING HACKED BY XGP?
	JRST POPBAJ	;NOPE
			;FALLS THRU

;XGPFLS IS CALLED AT THE CLOCK LEVEL IF ANYTHING GOES WRONG WITH ANYTING
;AND AT THE END OF THE RUN.
;IT FREES THE WORLD, AND FLUSHES THE XGP USER

XGPFLS:	MOVEM Q,XFLSPC
	SETOM XGPUSR	;FLUSHES XGP, ALSO PREVENTS TIMING ERRORS IF XGPFLS CALLED
			;AT CLOCK LEVEL, SINCE IT WON'T BE ANY MORE
	PUSH P,C
	PUSH P,D
	CLEARB A,B
	MOVSI C,400000
	MOVSI D,-1
XGPFL1:	TDNN C,XGPMTB(B)
	JRST XGPFL3	;THIS PAGE NOT HACKED
	ADDM D,MMSWP(A)	;UNTIE THIS PAGE
	SKIPGE MMSWP(A)
	JRST 4,.
	ANDCAM C,XGPMTB(B)	;AND FLUSH THIS BIT
XGPFL3:	ROT C,-1	;MOVE ON TO NEXT BIT
	SKIPGE C	;BEG OF NEW WORD?
	AOS B	;YES, AOS XGPMTB INDEX
	CAIGE A,TSYSM-1	;FINISHED WITH ALL OF MMSWP ENTRIES?
XGPFL2:	AOJA A,XGPFL1	;NOPE, LOOP BACK FOR MORE FUN
	POP P,D
	POP P,C
	JRST POPBAJ


;MAIN PROGRAM BUFFER TIE DOWN ROUTINES
;.CALL FOO
;FOO:	SETZ
;	SIXBIT /XGPIM/
;	SETZ POINTER TO [# OF PAGES,,FIRST PAGE]

NXGPIM:	XCTR XRW,[MOVES B,(A)]	;GET POINTER, AND CHECK FOR WRITE
	JUMPL B,XCONT	;CONTINUING A PREVIOUS CALL
	SKIPN TEN11F
	SKIPL XGPUSR
	POPJ P,		;LOSE
	MOVEI C,X11BFP	;BUFF HEAD IN 11 DEFINED AS 1000
	MOVEM C,XGP11P
	MOVE C,XGPCA(C)	;GET BUFF POINTER FROM 11
	JUMPE C,CPOPJ	;LOSE, THE 10-11 IS LOSING OR PGM LOSING
	MOVE C,TIME
	MOVEM C,XGPTM1	;SET UP TIMEOUT ON PAGE LOCK
	SETZM XGPINI	;SAY NOT ACTIVE
	MOVEM U,XGPUSR	;AND BEING LOADED
	SETZM XGPTAB
	MOVE C,[XGPTAB,,XGPTAB+1]
	BLT C,XGPTAB+LXTAB-1	;CLEAR OUT OLD PAGE BUF TABLE
	MOVE C,[441400,,XGPTAB]
	MOVEM C,XGP10P	;SETUP BYTE POINTER TO PAGE LIST
XTIE:	TLO B,400000
	UMOVEM B,(A)
	HRRZ C,B
	HRRZ E,B	;C,E GET PAGE # NEXT TO BE TIED DOWN
	HLRZ D,B	;D GETS COUNT OF PAGES LEFT
	LSH C,10.
	UMOVE B,20(C)	;BRING IN THAT LOSING PAGE
	PUSHJ P,SWTL
	CIRPSW
	TRZ D,400000
	CAIL D,XGNTPG	;MAX # PAGES TIED DOWN
	JRST XLOSE
	EXCH E,A
	CAIL A,400
	JRST XLOSE	;PAGE OUT OF REASONABLE RANGE
	PUSH P,A
	PUSHJ P,UPLC	;FIND OUT PAGE MAP ENTRY
	LDB B,Q
	CAIN B,-1
	JRST XLOSE1
	LDB B,T
	ANDI B,PMRCM
	CAIL B,TSYSM
	JRST 4,.
	PUSHJ P,XGPBIT
	CONO PI,CLKOFF	;CLOCK OFF TO PREVENT PAGES 1/2 TIED DOWN
	TDNN A,XGPMTB(B)	;WE HAVE HIT THIS PAGE ALREADY
	CAME U,XGPUSR	;TIMEOUT OR OTHER GROSS LOSSAGE
	JRST XLOSE1	;DON'T TIE THIS ONE DOWN, AND FLUSH ALL THE REST
	IORM A,XGPMTB(B)	;SET BIT IN XGP MEM TABLE
	LDB B,T
	ANDI B,PMRCM
	IDPB B,XGP10P	;PUT IN BUFFER LIST
	MOVSI A,1
	ADDM A,MMSWP(B)	;TIE IT DOWN GOOD (FOR ALL YOU BONDAGE FREAKS)
	CONO PI,CLKON
	PUSHJ P,LSWPOP	;CIRPSW
	POP P,B
	SOJLE D,XBFIN	;WE ARE DONE TYING START XGPING
	AOS B
	HRL B,D
	MOVE A,E	;NOW, LOOP FOR MORE CRUFTIES, OR
XCONT:	CAME U,XGPUSR	;REENTER HERE IF THE MAD SWAPPER HAD US SWAPPED OUT
	POPJ P,
	JRST XTIE

XLOSE1:	CONO PI,CLKON	;CLEANUP AFTER LOSSAGE
	SUB P,[1,,1]
XLOSE:	PUSHJ P,LSWPOP	;CIRPSW
	PUSH P,A
	PUSH P,B
	JSP Q,XGPFLS	;FLUSH THE LAST VESTIGE OF THIS ABORTIVE ATTEMPT TO CAUSE WINNAGE

XBFIN:	SETOM XGPINI	;THE WORST IS OVER, SIT BACK AND RELAX
	MOVE A,[441400,,XGPTAB]
	MOVEM A,XGP10P
	JRST POPJ1	;TELL HIM HE MAY EVEN WIN SOMEDAY

XPLO:	CONO PI,CLKOFF
	PUSHJ P,XPLST
	JRST XPLO1
	CONO PI,CLKOFF
	PUSHJ P,XPLWT
	PUSHJ P,UFLS
	MOVE T,XPLU
	SKIPE IOCHNM(T)
	JRST XPLO2
	MOVE T,SV40(T)
	CAME T,[.SLEEP 17,]
XPLO2:	JRST OPNL10
	CONO PI,CLKON
	MOVNI A,1
	MOVSI B,(SIXBIT /XPL/)
	PUSHJ P,CLIO
	POPJ P,
	MOVE A,UTMPTR(U)
	MOVE B,XPLU
	MOVEM A,UTMPTR(B)
	JRST POPJ1

XPLO1:	PUSHJ P,UDELAY
	JRST XPLO

XPLST:	SKIPL T,XPLU
	PUSHJ P,XPLIN
	JRST POPJ1
	HRROI A,XPL
	CONO PI,UTCOFF
	PUSHJ P,NUJBST
	JRST UTCONJ
	JRST UTCOJ1

XPLWT:	SKIPGE T,XPLU
	POPJ P,
	PUSHJ P,XPLIN
	SKIPGE IOCHNM(T)
	JRST POPJ1
	MOVS Q,SV40(T)
	CAIN Q,(.SLEEP 17,)
	AOS (P)
	POPJ P,

XPLIN:	PUSH P,U
	CAML T,USRHI
	JRST XPLIN1
	HLRE U,UNAME(T)
	AOJN U,XPLIN1
	MOVSI U,(SIXBIT /XPL/)
	CAME U,JNAME(T)
XPLIN1:	AOS -1(P)
	POP P,U
	POPJ P,

;VARIABLES AREA
EBLK

XGPTAB:	BLOCK XGNTPG/3+1	;PAGE #'S OF XGP BUFS IN 12 BIT BYTES
LXTAB==.-XGPTAB
XGP11P:	0	;POINTER TO 11 RING STRUCTURE (10 STYLE ADR INTO XGPC)
XGPOB:	0	;TEMP FOR OLD XGP11P
XGP10P:	0	;BYTE POINTER INTO XGPTAB FOR GETTING 10 BUFFERS
XGPMTB:	BLOCK TSYSM/36.+1	;BIT TABLE FOR KEEPING
				;TRACK OF AOSES OF MMSWP
XGPUSR:	-1	;XGP USER INDEX, -1 IF NONE
XGPINI:	0	;IF -1, SIGNAL TO CLK CODE THAT ALL BUFFERS ARE NOW TIED  DOWN.
XFLSPC:	0	;PC OF LAST CALL TO XGPFLS
XGPSPM:	BLOCK 8	;STORAGE FOR PAGE MAP ENTRIES
XGPTM1:	0	;TIME FIRST PAGE TIED DOWN
XPL:	SIXBIT /XPL/	;FILE + JNAME OF XPL INTERPRETER
XPLU:	0	;INDEX OF LOADED XPL

BBLK
]

IFE XGP,NXGPIM:	JRST OPNL1	;NO SUCH DEVICE

SUBTTL	ROBOT ARM ROUTINES

IFN ARMP,[
NARMCH==11.

AARMOVE:	UMOVE A,(J)
	JUMPGE A,CPOPJ
	CAMGE A,[-100,,]
	JRST ILUUO
	CONO PI,CLKOFF
	CAMN U,ARMUSR
	JRST AARM1A
	SKIPL ARMUSR
	JRST ILUUO
;	CONO TIPDEV,0
;	CONI TIPDEV,C
;	JUMPE C,ILUUO	;DONT ALLOW ARM TO RUN WITHOUT TIPBRK
;	DATAI TIPDEV,C
;	TRNN C,1
;	JRST ILUUO	;WRIST DETENT OFF NORMAL- BARF
;	CONO TIPDEV,TIPBCH
	MOVEM U,ARMUSR
AARM1A:	CONO PI,CLKON
	MOVSI B,-NARMCH
AARM1:	SETZM ARMDF(B)
	SKIPN ARMF
	SETOM DESTIN(B)	;NOT PREV IN USE, INITIALIZE
	AOBJN B,AARM1
	SKIPN ARMF
	SETOM OMPXIF
	SKIPN ARMF
	PUSHJ P,IMXON	;START IMX
	SETOM ARMF	;ARM NOW IN USE
	SETOM DLSRCH
ARML:	UMOVE E,(A)	;PICK UP COMM
	LDB C,[220400,,E]
	JUMPE C,ARML1	;NO INDEX
	XCTR XRW,[HRRZ Q,(C)]
	ADD E,Q
	TLZ E,17
ARML1:	TLZE E,20
	XCTR XRW,[HRR E,(E)]
	LDB D,[360600,,E]	;CHNL
	LDB C,[300600,,E]	;OP
	CAIGE D,NARMCH
	CAIL C,NARMOP
	JRST ARMLOS
	PUSHJ P,@ARMOPT(C)
	AOBJN A,ARML
ADF1:	MOVSI D,-NARMCH
ADFL:	SKIPL A,ARMDF(D)
	JRST ADF2
	TLNE A,100000
	JRST .+3
	MOVE C,RMAXV(D)
	MOVEM C,MAXV(D)
ADF2:	AOBJN D,ADFL
	SETZM DLSRCH
	JUMPL B,POPJ1
	POPJ P,

ARMLOS:	SETZM DLSRCH
	JRST ILUUO

ARMOPT:	ASDEST	;0 SET DEST
	ASVEL	;1 SET VELOC
	ATDEST	;2 COMPARE COMPUTED POSN
	ATVEL	;3 COMPARE COMPUTED VELOC
	AADST	;4 COMPARE ACTUAL POS
	ARPOST	;5 READ CURRENT POSITION
NARMOP==.-ARMOPT

ARPOST:	LDB B,IMPXPP(D)	;READ POSITION
	UMOVEM B,(E)
	POPJ P,

ASDEST:	ANDI E,7777
	CAMGE E,DESMIN(D)
	MOVE E,DESMIN(D)
	CAMLE E,DESMAX(D)
	MOVE E,DESMAX(D)
	MOVEM E,DESTIN(D)
	MOVSI E,600000
ADFSET:	IORM E,ARMDF(D)
	POPJ P,

ASVEL:	ANDI E,7777
	CAMLE E,RMAXV(D)
	MOVE E,RMAXV(D)
	MOVEM E,MAXV(D)
	MOVSI E,500000
	JRST ADFSET

AADST:	LDB C,IMPXPP(D)	;COMPARE ACTUAL POSITION
	SUB C,DESTIN(D)
	JRST ATD1

ATDEST:	SKIPA C,DESTIN(D)	;COMPARE COMPUTED POSITION
ATVEL:	SKIPA C,VELOC(D)	;COMPARE COMPUTED VELOCITY
	SUB C,POST(D)
ATD1:	MOVMS C
	CAMLE C,E
	TLO B,400000
	POPJ P,

SERVO:	MOVSI A,-NARMCH
	AOSN OMPXIF
	JRST SERVI
SERV1:	MOVE T,DESTIN(A)
	SUB T,POST(A)
	SKIPN VELOC(A)
	JUMPE T,SERVX
SERV1A:	MOVN B,MAXV(A)
	MOVE C,VELOC(A)
	SUB C,MAXA(A)
	CAMGE C,B
	MOVE C,B
	MOVE B,VELOC(A)
	ADD B,MAXA(A)
	CAMLE B,MAXV(A)
	MOVE B,MAXV(A)
	CAMG T,B
	CAMGE T,C
	JRST SERV2
	MOVM D,T
	CAMLE D,MAXA(A)
	JRST SERV2
SERV3:	MOVEM T,VELOC(A)
	ADDB T,POST(A)
	IOR T,OMPXP(A)
	DATAO OMPX,T
SERVX:	AOBJN A,SERV1
SERVR:	JRST ARMSVR

SERV2:	SKIPGE T
	SKIPA D,C
	MOVE D,B
	SUB T,VELOC(A)
	MOVM E,D
	IMUL D,E
	IDIV D,MAXA(A)
	ASH D,-1
	CAMGE D,T
	SKIPA E,B
	MOVE E,C
	MOVE C,E
	XOR C,VELOC(A)
	JUMPGE C,SERV2A
	MOVE C,E
	XOR C,T
	JUMPGE C,SERV2A
	SKIPA T,VELOC(A)
SERV2A:	MOVE T,E
	JRST SERV3

SERVI:	LDB B,IMPXPP(A)	;INITIALIZE TO CURRENT ARM STATE
	MOVEM B,POST(A)
	SETZM VELOC(A)
	SKIPGE DESTIN(A)
	MOVEM B,DESTIN(A)
	IOR B,OMPXP(A)
	DATAO OMPX,B
	AOBJN A,SERVI
	JRST SERVR

AARMOFF:	CAME U,ARMUSR
	POPJ P,
	CONO TIPDEV,0
	SETZM ARMF
	PUSHJ P,IMXCL1
	SETOM ARMUSR
	POPJ P,

AARMRS:	CAME U,ARMUSR	;RESET ARM LOCK FLAGS
	POPJ P,
	MOVSI A,-NARMCH
AARMR1:	SKIPL ARMLOK(A)
	JRST AARMR2
	CONO PI,OMXOFF
	SETOM DESTIN(A)	;RELOAD ARM COMM FROM CURRENT POS
	CLEARM ARMLOK(A)
	SETOM OMPXIF
	CONO PI,OMXON
AARMR2:	AOBJN A,AARMR1
	POPJ P,


TIPBRK:	DATAI TIPDEV,B
	TRNE B,1	;WRIST DETENT
	JRST TIPBR2
	SETOM ARMLOK+ARMVRT	;UNHAPPINESS
TIPBR2:	EXCH B,TIPBR
	XOR B,TIPBR
	SKIPGE A,ARMUSR
	JRST TIPBR1
	ANDI B,7
	LSH B,ATPSFT
	IORM B,PIRQC(A)
TIPBR1:	JRST OMXRET


EBLK

;JOINT ASSIGN
;0-10 AMF SWING VERT HORIZ YAW TILT GRIP ROTATE EXTEND ROLL
;11-12 NEW VIDI FOCUS IRIS

ARMVRT==1	;CHNL # OF ARM VERT

DESMIN:	IRPS A,,0 41 233 0 0 0 0 0 0 0 0
	A
TERMIN
DESMAX:	IRPS A,,7777 7777 7323 7777 7777 7777 7777 7777 7777 7777 7777
	A
TERMIN
DESTIN:	BLOCK NARMCH	;-1 NOT COMMANDED   + VAL OF POSITION COMMAND
POST:	BLOCK NARMCH	;POSITION OF ARM AT PI LEVEL
VELOC:	BLOCK NARMCH
MAXV:	BLOCK NARMCH
ARMDF:	BLOCK NARMCH	;4.9 VARIABLE CHANGED 4.5 POS VAR 4.7 VEL 4.6 ACC
ARMLOK:	BLOCK NARMCH	;IF -1 AXIS LOCKED DO NOT ALLOW CHNG IN COMMAND

MAXA:	REPEAT NARMCH,14
RMAXV:	REPEAT NARMCH,140

OMPXP:	IRPS A,,2 3 4 6 7 12 11 10 5 32 31
	A,,
	TERMIN

IMPXPP:	IRPS A,,102 103 104 106 107 112 111 110 105 132 131
	301400-140000*<A-A/3*3>,,MPXBUF+A/3
	TERMIN

ARMUSR:	-1	;-1 OR INDEX OF USER DOING ARMOVE
OMPXIF:	0	;-1 => INITIALIZE DESTINS'S TO CURRENT ARM POSITION
ARMF:	0	;-1 ARM IN USE, 0 NO ARMOV EXECUTED
TIPBR:	0	;TIP BREAK REG LAST TIME READ IN
BBLK
]

SUBTTL	INPUT MULTIPLEXOR

IFN IMXP,[
APOTSE:	CONI MPX,T
	JUMPE T,ILUUO	;MPX NOT AVAIL
	MOVE C,B
APOTS1:	UMOVE A,(C)
	ANDI A,377	;GET IMPX CHNL
	JUMPE A,CPOPJ		;CHNL 0 = END OF LIST
	IDIVI A,3		;COMPUTE MPXBUF INDEX
	ADD A,MPXPT(B)		;FORM BYTE PTR
	MOVSI B,-NPOTCH		;SET UP TN DELETE PREVIOUS POT CONN
PTS1:	CAMN A,POTBPT(B)	;IS ENTRY FOR SAME CHNL
	SKIPG POTUSR(B)	;AND ACTIVE
	JRST PTS2		;NO,KEEP LOOKING
	CAME U,POTUSR(B)	;IS THIS USER THE ONE WHO MADE ENTRY
	JRST ILUUO		;NO,ILLEGAL
	PUSHJ P,NARK		;DEACTIVATE AND FREE ENTRY

PTS2:	AOBJN B,PTS1		;TRY NEXT ENTRY
	MOVEI B,400000		;MASK FOR DELETE ONLY
	XCTR XRW,[TDNE B,(C)]		;SKIP ON NEW ENTRY WANTED
	JRST PTSR		;TRY FOR NEXT BLOCK
	MOVSI B,-NPOTCH		;SET UP TO FIND FREE ENTRY
PTS3:	SKIPL POTUSR(B)		;SKIP ON FREE
PTS4:	AOBJN B,PTS3		;TRY OTHER SLOTS
	JUMPGE B,ILUUO		;NO SLOTS AVAIL
	CONO PI,CLKOFF		;PREVENT OTHER GRABBERS
	SKIPGE POTUSR(B)	;SKIP IF JUST TAKEN
	JRST PTS5		;OKAY FOR SURE
	CONO PI,CLKON		;REENABLE
	JRST PTS4		;TRY ANOTHER

PTS5:	SETZM POTUSR(B)		;MARK TAKEN
	AOS IMXNTO
	AOS POTCON		;INCREMENT NUMBER SLOTS IN USE
	PUSHJ P,IMXON		;START IMPX
	CONO PI,CLKON		;AND ENABLE
PTS6:	MOVEM A,POTBPT(B)	;SAVE PTR TO MPXBUF FOR MPX ROUTINE
	XCTR XRW,[HRRZ A,1(C)]		;GET USER ADR
	XCTRI XRW,[MOVES (A)]
	 JRST .+2
	  JRST PTSL1		;NO,LOSE
	XCTR XRW,[HLL A,1(C)]		;GET BYTE FIELDS
	TLNN A,777700		;IF NONE SPECIFIED
	TLC A,4400		;ASSUME FULL WORD
	TLZ A,77		;FLUSH INDEX,INDIRECT
	MOVEM A,POTVPT(B)	;SAVE AS USER MEM PTR FOR MPX ROUTINE
PTS7:	UMOVE A,(C)
	ANDI A,3	;GET FLOAT + ABS BITS
	MOVEI D,PUFLI		;ADDR OF FLOATING INCREMENTAL UPDATE
	TRNN A,1		;SKIP ON FLOATING
	MOVEI D,PUFXI		;ADDR OF FIXED INCREMENTAL UPDATE
	TRNE A,2		;SKIP ON NOT ABSOLUTE
	SOS D			;ABS ROUTINE STARTS ONE BEFORE INCREMENTAL
	MOVEM D,POTUPD(B)	;SAVE ROUTINE ADDR FOR MPX ROUTINE
PTS8:	XCTR XRW,[MOVN D,2(C)]		;GET LOWER LIMIT
	MOVNM D,POTOFS(B)	;SAVE AS OFFSET FOR MPX ROUTINE
	TRNE A,1		;SKIP ON FIXED
	JRST PTS9		;GO SET FLOATING PARAMS
	XCTR XRW,[ADD D,3(C)]		;UL-LL
	ASH D,-2		;SCALE MULTIPLIER

PTS9R:	MOVEM D,POTSCL(B)	;STORE SCALE FACTOR FOR MPX ROUTINE
	LDB D,POTVPT(B)		;GET CURRENT VARIABLE VALUE
	TRNE A,1		;SKIP ON FIXED
	JRST PTS10		;COMPUTE FLOATING INCR VALUE
	SUB D,POTOFS(B)		;D_VAL-LL FIXED
	MOVSI E,200000		;SET TO ROUND VALUE
	ASHC D,-25.		;SCALE TO GET 12 BIT VALUE
	DIV D,POTSCL(B)		;COMPUTE INCR VAL FOR VAR
PTS10R:	HRROM D,POTVAL(B)	;STORE INCR VALUE FOR MPX ROUTINE
	MOVEM U,POTUSR(B)	;STORE USER TO ACTIVATE MPX ROUTINE
PTSR:	ADDI C,4		;ADVANCE POINTER TO NEXT 4 WORD BLOCK
	JRST APOTS1		;GO SET MORE TABLE ENTRIES

PTS9:	FADR D,3(C)		;UL-LL IN FLOATING
	FSC D,-12.		;SCALE DOWN FOR POT RANGE
	JRST PTS9R		;RETURN WITH SCALE FACTOR

PTS10:	FSBR D,POTOFS(B)	;D_VAL-LL FLOATING
	FDVR D,POTSCL(B)	;COMPUTE INCR VAL FLOATING
	MULI D,400		;FIX INCR VAL
	TSC D,D			;WORK FOR BELOW LOWER LIMIT
	ASH E,-243(D)		;VALUE IN E
	MOVE D,E		;TO D LIKE FIXED
	JRST PTS10R		;RETURN WITH INCR VALUE


PTSL1:	PUSHJ P,NARK
	PUSHJ P,TPFLT
	JRST UUOTRO

;POTS UPDATE CALLED BY MPX ROUTINE

UPOTS:	SPM UPTPGT
	MOVSI A,-NPOTCH		;SET UP TO LOOP FOR ALL POT TABLE POSITIONS
	SETZM UPUSR		;INDICATE NO USER SET UP
UPOT1:	SKIPLE U,POTUSR(A)	;SKIP ON SLOT UNUSED OR IN PROCESS
	JRST UPOT2		;GO TRY TO UPDATE VAR
UPOTR:	AOBJN A,UPOT1		;TRY NEXT POT TABLE ENTRY
	LPMR UPTPGT	;RESTORE MAP
	JRST UPOTRT		;DONE

UPOT2:	CAMN U,UPUSR		;IS THIS USER LAST UPDATED
	JRST UPOT3		;YES, RELOCATION IS SETUP
	MOVE T,USTP(U)		;TEST TO SEE IF
	SKIPN UMAPS(U)	;MAP LOADED TO POINT TO OTHER USER
	TLNE T,BUCSTP+BCSTOP	;USER BEING MOVED
	JRST UPOTR		;YES DON'T UPDATE FOR HIM
	MOVEM U,UPUSR		;SAVE AS LAST USER SET UP
	LPMR UPGML(U)	;LOAD USERS MAP
UPOT3:	LDB B,POTBPT(A)		;GET CURRENT ABS POSITION
	HLRZ C,POTVAL(A)	;GET POS LAST TIME
	TRNE C,400000		;SKIP UNLESS FIRST TIME
	JRST UPOT3A
	MOVE T,B
	SUB T,C
	MOVMS T
	CAMG T,PTNOIS
	JRST UPOTR
UPOT3B:	HRLM B,POTVAL(A)	;SAVE FOR NEXT TIME
	SUB B,C			;GET AMOUNT OF INCREMENT
	CAIL C,5252		;SKIP IF NOT IN UPPER THIRD
	JUMPG B,UPOT4		;JUMP IF IN UPPER THIRD AND GOING UP
	CAIGE C,2525		;SKIP IF NOT IN LOWER THIRD
	JUMPL B,UPOT5		;JUMP IF IN LOWER THIRD AND GOING DOWN
UPOT6:	HRRE C,POTVAL(A)	;GET PREV INCREMENTAL VALUE
	ADD C,B			;UPDATE BY POT INCR
	CAILE C,7777		;IF NEW VALUE WOULD EXCEED 7777
	MOVEI C,7777		;CLAMP IT TO 7777
	CAIGE C,0		;IF NEW VALUE WOULD BE LESS THAN 0
	MOVEI C,0		;CLAMP IT TO 0
	HRRM C,POTVAL(A)	;STORE NEW INCR VALUE
	JSP B,@POTUPD(A)	;CALL ROUTINE TO COMPUTE NEW VARIABLE VALUE
	HRRZ T,POTVPT(A)	;STORE VAR IN USER MEM
	XCTRI XR,[MOVE B,(T)]
	 JRST .+2
	  JRST INARK
	MOVEI D,B
	HLL D,POTVPT(A)
	DPB C,D
	XCTRI XW,[MOVEM B,(T)]	;STORE VAR IN USER MEM
	 JRST UPOTR

INARK:	SETOM POTUSR(A)	;THIS POT A LOSER, FLUSH
	SOS POTCON
	SOS IMPXF
	SOSGE IMXNTO
	DATAO DSDEVN,[MPX]
	JRST UPOTR		;RETURN FOR MORE POTS

UPOT4:	HRREI C,-6(B)		;C_DELTA-6
	IMULI C,3		;C_DELTA*3-22
	CAMLE C,B		;IF POT TURNED UP FAST
	MOVEM C,B		;THEN BOOST INCREMENT
	JRST UPOT6		;RETURN TO STORE

UPOT5:	HRREI C,6(B)		;C_DELTA+6
	IMULI C,3		;C_DELTA*3+22
	CAMGE C,B		;IF POT TURNED DOWN FAST
	MOVEM C,B		;THEN DECREASE DECREMENT
	JRST UPOT6		;RETURN TO STORE

PUFXA:	HLRZ C,POTVAL(A)	;ABS ENTRY, USE DIRECT VALUE
PUFXI:	MUL C,POTSCL(A)		;INCR ENTRY, SCALE VALUE
	ASHC C,25.		;200000,,=FULL WORD
	ADD C,POTOFS(A)		;OFFSET VALUE
	JRST (B)		;RETURN WITH USER FIELD VALUE

PUFLA:	HLRZ C,POTVAL(A)	;ABS ENTRY FOR FLOAT PT
PUFLI:	TLC C,232000		;FLOAT POT VALUE
	FAD C,C			;NORMALIZE
	FMPR C,POTSCL(A)	;SCALE FLOATING VALUE
	FADR C,POTOFS(A)	;ADD OFFSET
	JRST (B)		;RETURN WITH NEW FLOATING VAR VALUE

UPOT3A:	MOVE C,B		;FIRST TIME USE CURRENT VALUE
	JRST UPOT3B


NARK:	SETOM POTUSR(B)	;TURN OFF AND FREE POT CHANNEL
	SOS POTCON	;DECREMENT # OF SLOTS IN USE
	JRST IMXCL1	;FLUSH THIS USE OF MPX AND MAYBE DEASSIGN MPXR

POTCLS:	MOVEI A,NARK	;ON CLOSE FLUSH ALL ENTRIES FOR USER
POTCLA:	MOVSI B,-NPOTCH	;SET UP TO TEST ALL ENTRIES FOR USER U
POTCL1:	CAMN U,POTUSR(B)	;SKIP IF DIFFERENT USER
	PUSHJ P,(A)	;FLUSH IT MAYBE
	AOBJN B,POTCL1	;TRY ALL SLOTS
	POPJ P,	;DONE

EBLK
POTUSR:	REPEAT NPOTCH,-1	;-1=FREE, 0=PROCESS, +=USER INDEX
POTBPT:	BLOCK NPOTCH		;BYTE PTR INTO MPXBUF
POTVPT:	BLOCK NPOTCH		;BYTE PTR INTO USER MEM INDEXED BY R
POTVAL:	BLOCK NPOTCH		;DIRECT VALUE,,INCR VALUE
POTUPD:	BLOCK NPOTCH		;ADDR OF ROUTINE TO COMPUTE NEW VALUE
POTSCL:	BLOCK NPOTCH		;SCALE FACTOR FOR VARIABLE
POTOFS:	BLOCK NPOTCH		;OFFSET FOR VARIABLE
MPXPT:	REPEAT 3,301400-140000*.RPCNT,,MPXBUF	;POINTERS FOR 12 BIT MPX BYTES
UPUSR:	0	;USER FOR WHOM RELOC SETUP AND NOT STOPPED
UPTPGT:	BLOCK 8	;SAVE MAP

POTCON:	-1	;<NUMBER OF POTS CONNECTED>-1

PTNOIS:	3	;IGNORE ANY CHANGE < OR = THIS AS POT NOISE
]

SUBTTL	340 DISPLAY

IFN 340P,[
;
; DISPLAY INTERRUPT ROUTINE
;
DRECYC:	0
;
BBLK
;
	MOVEM A,LPTA
	MOVE A,DRECYC
	MOVEM A,LPTBRK
	MOVE A,[B,,LPTB]
	BLT A,LPTB+17-B+1-1
	JRST DRECY1
SRECYC:	MOVE A,[B,,LPTB]
	BLT A,LPTB+17-B+1-1
	CONSZ DIS,2000
	JRST DSLTPB	;LIGHT PEN BREAK
	SKIPGE 340MD
	JRST SRCYRB	;RANDOM BREAK
	CONSZ DIS,5000
	JRST SRCY4
DRECY1:
;	DATAI 710,A
;	SUB A,LQTIM
;	CAIL A,32000./2
;	JRST DISF2	;MISSING CLOCK BREAK DUE TO DIS
	HLRE A,DBLKOP
	SKIPL A
	TDZA A,A
	MOVNS A
	ADDB A,DWDS	;CREDIT BACK WDS NOT USED
	JUMPL A,DISF2
	HRRZS DBLKOP
	SKIPE 340MD
	JRST SRCY3	;IDS OR DIS
SRCY1:	HRRZ A,LDISP
	JUMPE A,ENDLIS
	JSP B,DPFTCH
	JUMPL A,SRCY2A	;SINGLE BLKO PNTR
	MOVEM A,LDISP	;LIST
	HLRZS A
	JUMPE A,SRCY5	;THIS ENTRY NULL
	JSP B,DPFTCH
	SKIPGE LMODE
	JRST SRCY1A	;DSTRT
SRCY1C:	JUMPGE A,SRCY5
	MOVEM A,LUBLKP	;SAVE FOR DEBUGGING
	HLRO C,A
	MOVNS D,C	;+ COUNT
	LDB E,[1200,,A]	;WD WITHIN PG
	ADD E,D
	LSH E,-10.
	AOS E
	MOVE J,E	;SAVE # EXEC PGS REQD
	SUBI E,N340PB
	JUMPG E,SRCY2B	;CANT MAKE IT IN ONE WHACK
	MOVN Q,E	;SAVE MAX ACCEPT STARTING EXEC PG #
	HRLZS E
	HRRZ C,A
	CAIGE C,20
	JRST DISLOS
	ANDI C,776000	;USER PG #
	MOVNI R,1	;BEST STARTING PLACE SO FAR
SRCY2D:	CAMN C,340DA(E)	;LOOK FOR PG SET UP TO RIGHT USER PG
	JRST SRCY2F	;FOUND ONE
SRCY2I:	AOBJN E,SRCY2D
	HRRZ E,R
	JUMPGE R,SRCY2C	;PARTIALLY SET UP PLACE FOUND
	AOS 340DP
	CAML Q,340DP	;NONE AVAIL, IS RING PNTR IN ACCEPTABLE AREA?
	SKIPA E,340DP	;YES
	CLEARB E,340DP	;NO RESET
SRCY2C:	MOVE R,DDAD(E)
	LSH R,-10.
	DPB R,[121000,,A]	;SWITCH BLKO PNTR TO EXEC MD PG
	MOVEM A,LDBLKP
	MOVEM A,DBLKOP
	HLRES A		;FOR GROSS DISPLAY LISTS
	ADDM A,DWDS	;APPROX TO WDS TO DISPLAY
	IDIVI A,1000.
	SOS A	;QUOTIENT MAY BE ZERO
	MOVEM A,DISDIE
	MOVNI A,EWRT
	ADDM A,DWDS
	ADDI E,2	;OFFSET FOR POINTER PAGES
	MOVEI A,0
SRCY2E:	CAME C,A340P1(E)	;PG SET TO RIGHT ADR?
	JRST SRCY2J	;NO SET UP
SRCY2K:	ADDI C,2000
	AOS E
	SOJG J,SRCY2E	;SET UP ALL PGS IN BLOCK
	CONSZ DIS,5400
	CONO DIS,100\SDCHN_3\DISCHN
	JRST SRCYCX

SRCY2J:	JSP B,DDFTC
	MOVEI B,-2(E)
	MOVEM B,340DP
	JRST SRCY2K

SRCY2B:	JRST DISLOS	;TEMP

SRCY2F:	MOVE D,J	;FIRST PAGE MATCHES, DO ENOUGH ADDITIONAL PAGES MATCH?
	SOJLE D,SRCY2C	;ONLY ONE PAGE NEEDED.  WINS
	MOVEI TT,2000(C)
	MOVEI R,1(E)
SRCY2H:	CAME TT,340DA(R)
	JRST SRCY2G	;NOT SET UP FOR WHOLE BLOCK
	ADDI TT,2000
	AOS R
	SOJG D,SRCY2H
	JRST SRCY2C	;SET UP FOR WHOLE BLOCK.  WIN

SRCY2G:	HRRZ R,E	;SAVE BEST SO FAR
	JRST SRCY2I
DPFTCH:	HRRZ C,A
	CAIGE C,20
	JRST DISLOS
	TRZ C,1777
	MOVSI E,-N340PB-2
DPFTC2:	CAME C,A340P1(E)	;PG SET UP TO RIGHT ADR?
	JRST DPFTC1	;NO
	TRZ A,776000	;YES RETCH REL ADR
	XCTRI [MOVE A,@DPAP(E)]
	 JRST (B)	;NO FAULT
	JRST 4,.	;SHOULD HAVE BEEN VERIFIED WHEN LOADED

DPFTC1:	AOBJN E,DPFTC2	;LOOP ON ALL EXEC PNTR PAGES
	MOVE E,340PP	;NONE SET UP SO SELECT ONE TO SET UP
	AOS D,340PP	;INCR IT FOR NEXT TIME
	CAILE D,1
	CLEARM 340PP	;RESET
DDFTC:	SPM DPBS	;ENTRY FOR DATA PGS STORE MAP
	SKIPGE U,DISUSR
	JRST 4,.
	SKIPL CIRPSW
	JRST DDFTC2	;MAP MIGHT CHANGE
	SKIPGE A340P1(E)
	JRST DDFTC3
	SETOM A340P1(E)	;WIPE OUT OLD ENTRY
	LDB W,DPEP(E)	;PICK UP OLD PAGE
	TRZN W,600000	;NO ACCESS
	JRST 4,.
	ANDI W,PMRCM
	CAIL W,TSYSM
	JRST 4,.
	MOVEI R,0
	DPB R,DPEP(E)
	SKIPGE DDEXCF+340P1(E)
	JRST DDFTC3
	LDB R,[MUR,,MEMBLT(W)]
	CAIE R,MURUSR
	JRST 4,.
	MOVSI R,-1
	ADDM R,MMSWP(W)	;REDUCE COUNT OF EXEC REFS
DDFTC3:	MOVE W,C	;SET PAGE IN E TO PNT TO ADR IN C
	LSH W,-10.	;GET USR PG #
	ROT W,-1	;GET WD # AND SAVE WHICH HALF IN SIGN
	ADDI W,UPGMP(U)
	HLRZ R,(W)
	SKIPGE W
	HRRZ R,(W)	;PICKS UP PAGE ENTRY
	HLRZ T,UPGCP-UPGMP(W)
	SKIPGE W
	HRRZ T,UPGCP-UPGMP(W)
	TRNN R,600000
	JRST DDFTC1	;NO ACCESS OR SWAPPED OUT
	LDB Q,[PMRCAD,,R]
	CAIN T,-1
	JRST DDFTC4	;ABS PG
	CAILE Q,TSYSM
	JRST 4,.
	SKIPGE MEMBLT(Q)
	JRST DDFTC2	;CAN'T SET UP EXEC PG POINTING TO THIS ONE
	LDB TT,[MUR,,MEMBLT(Q)]
	CAIE TT,MURUSR
	JRST 4,.
	CLEARM DDEXCF+340P1(E)
	MOVSI TT,1
	ADDM TT,MMSWP(Q)	;AOS COUNT OF EXEC PGS
DDFTC5:	MOVEM C,A340P1(E)
	DPB R,DPEP(E)	;STORE USERS PG ENTRY IN EXEC MAP
	LPMR DPBS	;RESET ASSOC REG
	ANDI A,1777	;FLUSH USER ADR EXCEPT WD IN PAGE
	XCTRI [MOVE A,@DPAP(E)]	;REF PAGE TO MAKE SURE ITS THERE, ETC
	 JRST (B)
	JRST 4,.

DDFTC4:	SETOM DDEXCF+340P1(E)	;INDICATE PAGE POINTING TO ABS PAGE
	JRST DDFTC5

DDFTC1:	JUMPE R,DISLOS	;MAP COMPLETELY 0 MUST BE ILM
	JUMPE T,DISLOS	;ILM
	MOVEM U,DISSWI
	LDB W,[121000,,C]
	MOVEM W,DISSPG	;RQ PG IN
DDFTC2:	MOVNI A,3	;HANG FOR 3 60THS
	JRST DISF2A

SRCY1A:	JUMPGE A,SRCY1B
	MOVEI A,-1(A)
SRCY1D:	JSP B,DPFTCH
	SOJA A,SRCY1C

SRCY1B:	TLNN A,777000
	JRST SRCY5
	HLRZ A,LDISP
	JRST SRCY1D

SRCY3:	SOSGE DTIME
	JRST DISF	;LIMIT REPETITION RATE
	SKIPL NDSFMS	;SKIP ON FRAME HACK NOT ACTIVE
	JSP B,ENDFM1	;MAYBE STOP DISPLAY AFTER SPEC # OF FRAMES
	MOVE A,IDBLKP
	MOVEM A,DBLKOP
	CONO DIS,100\SDCHN_3\DISCHN
	SETOM DISDIE
	JRST SRCYCX

SRCY4:	CONO DIS,200\SDCHN_3\DISCHN	;EDGE GRONK DIS AND KEEP GOING
	JRST SRCYCX

DISF2:	MOVNI A,300.
DISF2A:	MOVEM A,DTIME
	SETOM DISON
	JRST DISF3

DISF:	SETOM DISON
DISF1:	MOVEI A,1
	MOVEM A,DTIME
DISF3:	CONO DIS,100
DISF4:	MOVEI A,DIS300-1
	MOVEM A,DBLKOP
SRCYCX:	MOVE A,[JSR DBLKPB]
SRCYB1:	MOVEM A,40+2*DISCHN
SRCYB2:	JRST LPTRT1

DIS300:	3000
	3000

DCRSTP:	SKIPE 340MD	;STOP DISPLAY FOR CORE SHUFFLING
	 POPJ P,	;BUFFER NOT IN USER CORE SO NO ACTION
DCRST1:	SETOM CDISOF
	CONO DIS,0
	POPJ P,

DCRRST:	SKIPE 340MD	;DISPLAY RESTART
	 POPJ P,
	SKIPL DISOFF
	 CONO DIS,SDCHN_3+DISCHN
	CLEARM CDISOF
	POPJ P,

DSLTPB:	DATAI DIS,LLTPSN
	AOS LTPCNT
	HRRZ A,LLTPSN
	ADDM A,LTPCY
	HLRZ A,LLTPSN
	ADDM A,LTPCX
	SKIPGE B,DISUSR
	JRST 4,.
	MOVEI A,%PILTP
	AND A,MSKST(B)
	IORM A,PIRQC(B)
	CONO DIS,@LTPDCN
	JRST SRCYCX

EBLK

DBLKP1:	0
DBLKPB:	0

BBLK
	MOVEM A,DBLKP1
	MOVE A,[BLKO DIS,DBLKOP]
	MOVEM A,40+2*DISCHN
	MOVE A,DBLKP1
	JRST 12,@DBLKPB

SRCYRB:	CONI DIS,A
	SKIPGE A
	CONO DIS,100	;WOULD ASSIGN IF IDLE
	MOVE A,[JSR DIGNOR]
	JRST SRCYB1

EBLK

DIGNOR:	0

BBLK
	CONO DIS,0
	DATAO DSDEV,[DSNDIS]
	JRST 12,@DIGNOR

ENDLIS:	SETOM DISDIE
	MOVE A,DROOT
	MOVEM A,LDISP
	SOSGE DTIME
	JRST DISF	;LIMIT REPETION RATE
	SKIPL NDSFMS
	JSP B,ENDFM1	;HACK FRAME HACK
	CONO DIS,100\SDCHN_3\DISCHN
	JRST SRCY1

ENDFM1:	SKIPE NDSFMS	;STOP DISPLAY IF SPEC # OF FRAMES UP ELSE RETURN
	SOSGE NDSFMS
	JRST ENDFM2	;STOP WORLD
	JRST (B)

ENDFM2:	SETOM DISOFF
	JRST DISF1

SRCY5:	MOVNI A,EWRT/2
	ADDB A,DWDS
	JUMPGE A,SRCY1
	JRST DISF2

DISLOS:	SKIPE 340MD
	JRST 4,.
	SKIPGE A,DISUSR
	JRST 4,DISF2A
	MOVEI B,%PIDIS
	IORM B,PIRQC(A)
	MOVSI A,(SETZ)
	JRST DISF2A	;DIE FOR A LONG TIME

SRCY2A:	SOSGE DTIME
	JRST DISF
	SKIPL NDSFMS
	JSP B,ENDFM1
	CONO DIS,100\SDCHN_3\DISCHN
	JRST SRCY1C


SUBTTL	340 DISPLAY - .DSTART, .DSTRTL

DISACR:	CAME U,DISUSR
	POPJ P,
	SKIPE 340MD	;COME HERE IF PAGE BEING FLUSHED + EXEC PAGE POINTING TO IT
	JRST 4,.	;SHOULD NOT BE SET UP TO USER PAGE
	CONO PI,UTCOFF
	SETOM A340P1-340P1(T)
	SKIPGE DDEXCF(T)
	JRST DISAC1
	ADDB J,MMSWP(TT)
	SKIPGE J
	JRST 4,.
DISAC1:	SKIPGE DISOFF
	JRST POPJ1
	PUSH P,T
	PUSHJ P,DISZAP
	POP P,T
	JRST POPJ1

;ROUTINES TO START 340 DISPLAY WITH DIS BUFF IN USERS CORE
ADSTART:	TDZA E,E	;CALL 10,
ADSTL:	MOVNI E,1	;CALL 12,
	MOVEI A,0
	PUSHJ P,ADSDV
	POPJ P,
	SETOM DISOFF
	CONO DIS,0
	CLEARM 340MD
	MOVEM E,LMODE
	HRRZ C,40
	MOVEM C,DROOT
	MOVEM C,LDISP
	SETZM DISOFF
	PUSHJ P,DISZAP
	JRST CLKOJ1

ANDIS:	CAME U,DISUSR
	POPJ P,
	UMOVE A,(J)
	JUMPL A,ANDIT1	;WAIT FOR COMPLETION OF LAST NDIS
	JUMPE A,ANDIT2	;RETURN COUNT LEFT FROM LAST NDIS
	MOVEM A,NDSFMS	;DISPLAY N TIMES AND THEN STOP
	SKIPL DISOFF
	JRST POPJ1
	SETOM DISDIE
	CLEARM DISOFF
	PUSHJ P,DISZAP
	JRST POPJ1

ANDIT1:	SKIPL DISOFF
	PUSHJ P,UFLS
	POPJ P,

ANDIT2:	MOVE A,NDSFMS
	JRST APTUAJ

SUBTTL	340 DISPLAY - .DSTOP, .DCLOSE, .LTPEN
ADSTOP:	CAMN U,DISUSR	;OPER 16
	SKIPGE DISOFF
	POPJ P,
ADCLB:	PUSH P,B
	PUSH P,C
	MOVSI C,-1
	SETOM DISOFF
	SETZM DBLINK
	CONO DIS,0
	MOVEI A,1
	MOVEM A,DTIME
	SETZM DISON
	SETOM DISDIE
	CONO PI,UTCOFF
	MOVSI A,-N340CL
ADCLB1:	SETOM A340P1(A)
	LDB B,DPEP(A)
	ANDI B,600000+PMRCM
	SKIPGE DDEXCF+340P1(A)
	JRST ADCLB2	;POINTING TO EXEC PG
	TRZE B,600000
	ADDM C,MMSWP(B)
ADCLB2:	AOBJN A,ADCLB1
	CLEARM 340PP
	CLEARM 340DP
	CLEARM EXEUMP+.340P1
	REPEAT N340PB/2,CLEARM EXEUMP+.DPG0+.RPCNT
	MOVE A,[JSR DIGNOR]
	MOVEM A,40+2*DISCHN
	CONO PI,UTCON
	POP P,C
	JRST POPBJ

ADCL1:	SOSE DISNTO
	POPJ P,
	JRST AIDS7

ADCLOSE:	CAME U,DISUSR	;OPER 15
	POPJ P,
	SKIPLE 340MD
	JRST AIDS8
AIDS7:	PUSHJ P,ADCLB
	PUSHJ P,ADCL2
	JFCL
AIDS9:	SETOM 340MD
	SETOM DISUSR
	DATAO DSDEV,[DSNDIS]	;DEASSIGN DIS
	POPJ P,

AIDS8:	MOVE A,U	;FLUSH CHNLS IN CASE OF .DCLOSE FROM DIS OR IDS
	PUSHJ P,CHSCAA
	PUSHJ P,ADSDV4
	JRST AIDS7

ALTPEN:	MOVE C,B
	CAME U,DISUSR	;CALL 14,
	JRST ALTPP2
	XCTR XR,[SKIPL A,(C)]
	JRST ALTPP1
	TLNN A,377777
	JRST .+3
	SKIPN LTPCNT
	PUSHJ P,UFLS
ALTPP1:	CONO PI,LPTOFF	;INHIBITS LPEN BREAKS
	MOVE A,LDISP
	XCTRI XW,[MOVEM A,4(C)]
	 SKIPA A,DBLKOP
	  JRST ALTPPL
	TRZ A,400000
	LDB B,[121000,,A]
	LDB B,[121000,,A340P1-340P1(B)]
	DPB B,[121000,,A]
	XCTRI XW,[MOVEM A,5(C)]
	 SKIPA B,C
	  JRST ALTPPL
	HRLI B,LLTPSN
	XCTRI XBW,[BLT B,3(C)]
	 JRST .+2
	  JRST ALTPPL
	SETZB A,LTPCNT
	MOVEM A,LTPCY
	MOVEM A,LTPCX
	JRST LPTONJ

ALTPP2:	XCTR XW,[SETZM (C)]
	XCTR XW,[SETZM 1(C)]
	POPJ P,

ALTPPL:	CONO PI,LPTON
	PUSHJ P,TPFLT
	JRST ALTPEN

SUBTTL	340 DISPLAY

ADSDV:		;A HAS NEW 340MD
	CONO PI,CLKOFF
	CONI DIS,T	;DIS AVAILABLE
	JUMPN T,ADSDV1	;YES
	CONO DIS,0
	CONI DIS,T
	JUMPE T,ADSDVN	;DIS NOT AVAIABLE
ADSDV1:	SKIPL T,DISUSR
	CAMN U,DISUSR
	JRST ADSDVY	;YES
	CAME U,UMASTER
	JRST ADSDVM	;MAYBE
ADSDV2:	SKIPGE A,DISUSR	;TAKE AWAY DISPLAY
	JRST ADSDV3
	PUSHJ P,RPCCK
ADSDV7:	SKIPL DISUSR
	SKIPG 340MD
	JRST ADSDV3
	PUSH P,R
	PUSHJ P,CHSCAA	;CHANGE 340 CHANNELS TO NUL DEV
	PUSHJ P,ADSDV4
	POP P,R
	SETZM DISNTO
ADSDV3:	MOVE A,340MD
ADSDVY:	SETZM DBLINK
	SETOM NDSFMS	;TURN OFF FRAMER HACK
	CAME A,340MD
	JUMPE A,ADSDV5	;SAME USER CLOBBERING HIS IDS OR DIS
ADSDV6:	PUSHJ P,ADCLB
	MOVEM U,DISUSR
ADCL2:	SKIPGE A,340BF
	JRST CLKOJ1
	LSH A,-10.
	PUSHJ P,MEMR
	SETOM 340BF
	JRST CLKOJ1

ADSDV4:	HRRZ Q,(R)
	CAIG Q,DN340L
	CAIGE Q,DN340B
	POPJ P,		;NOT A 340 CH
	CAIL Q,DN340C
	SKIPA T,[NLODN]	;UNIT MODE
	MOVEI T,NLBDN	;BLOCK MODE
	SOS DISNTO
	HRRM T,(R)
	POPJ P,

ADSDV5:	SKIPGE 340MD
	JRST ADSDV6
	MOVE A,DISUSR
	JRST ADSDV7

ADSDVM:	CAMN T,UMASTER
	JRST ADSDVN	;CURRENT USER UMASTER
	MOVE T,UTMPTR(U)
	MOVE T,TTYTYP-USRRCE(T)	;GET BITS FOR NEW GUY IN LH OF T
	HRR T,DISUSR
	HRR T,UTMPTR(T)
	HLR T,TTYTYP-USRRCE(T)
	TRNE T,%TT3HP
	JRST ADSEQ1	;CURRENT USER HAS HIGH PRIORITY
	TRNE T,%TT340
	JRST ADSEQ2	;CURRENT USER NEAR 340
	TLNN T,%TT340+%TT3HP	;GIVE TO NEW GUY IF HE IS EITHER
	JRST ADSEQ
ADSWIN:	JRST ADSDV2

ADSEQ1:	TLNN T,%TT3HP
	JRST ADSDVN	;NO
ADSEQ:	SKIPL TTYTBL(U)	;MAYBE
	JRST ADSWIN
ADSDVN:	JRST CLKONJ

ADSEQ2:	TLNE T,%TT3HP
	JRST ADSWIN
	TLNN T,%TT340
	JRST ADSDVN
	JRST ADSEQ

SUBTTL	340 DISPLAY AS ASCII DEVICE

DISO:	PUSHJ P,AIDS1
	JRST OPNL10	;NOT AVAILABLE
	AOS DISNTO
	SETOM 340DMD
	LDB B,[20200,,D]
	AOS B
	LSH B,4
	MOVEM B,CSCALE
	LDB TT,[40200,,CSCALE]
	MOVNS TT
	MOVEI T,170.
	LSH T,(TT)
	TRZ T,7
	MOVEM T,DISCHS	;CHARS THAT WILL FIT THIS SIZE
	MOVEI T,88.
	LSH T,(TT)
	MOVEM T,DISLNL	;LINES THAT WILL FIT
	PUSHJ P,DSIZAP
	MOVSI T,-1777
	HRR T,340BF
	SOS T
	MOVEM T,IDBLKP
	SETZM DISOFF
	PUSHJ P,DISZAP
	SETOM 340T
	JSP Q,OPSLD3
	DN340C,,DN340B
	DN340H,,DN340W

340FF:	SKIPL 340T
	JRST 340FF1
	MOVE A,TIME
	MOVEM A,340T
340FF1:	MOVE T,340T
	ADDI T,30.*3
	CAML T,TIME
	PUSHJ P,UFLS
	SETOM 340T
	PUSHJ P,ADCLB
	PUSHJ P,DSIZAP
	JRST DISZAP

DSIZAP:	SETZM DBLOAT
	SETZM 340XMD
	SETZM DBLINK
	MOVE T,340BF
	MOVEI A,34117
	IOR A,CSCALE
	MOVEM A,(T)
	PUSH T,[221700060000]
	HRLI T,0600
	MOVEM T,340BFP
	MOVE A,[403737403737]
	MOVEI B,1(T)
	HRLS B
	AOS B
	MOVEM A,1(T)
	BLT B,1775(T)
	MOVE A,DISLNL
	MOVEM A,DISLNS
	MOVE A,DISCHS
	MOVNM A,DISCHC
	MOVEI T,(HRRZ I,(A))
	HRLM T,AIDC1
	POPJ P,


340B:	MOVEI E,340D
	JRST NBTOCH

340C:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)
	ANDI A,177
340D:	CAIN A,^C
	POPJ P,
	CAIN A,^L
	JRST 340FF
	CAIN A,^T
	JRST DSIZAP
	SKIPE DISLNS
	SKIPGE DBLOAT
	POPJ P,
	CAIN A,^B
	JRST BLNKON
	CAIN A,^E
	JRST BLNKOF
	SKIPE 340XMD
	PUSHJ P,340D1	;ENTER CHR MOD INF NOT ALREADY
340E:	EBLK
AIDC1:	HRRZ\HLRZ I,DISTBL(A)
	BBLK
	TRNN I,-100
	JRST 340FC	;NORMAL CHR
	TRNN I,-200
	JRST 340K	;FAKE OUT 340 NON-SPACING CHRS
	PUSHJ P,(I)	;ROUTINE DISPATCH (MAY SKIP OR SKIP TWICE)
	JRST 340E	;WRONG CASE OR SOMETHING, TRY AGAIN
340F:	IDPB I,340BFP
340F2:	MOVE J,340BFP
	ANDI J,1777
	CAIL J,1776
	SETOM DBLOAT
	POPJ P,

340FC1:	SKIPL DISCHC	;DISPLAY IF COUNT NOT OUT BUT DONT INCR COUNT
340FC:	AOSG DISCHC	;INCR COUNT + DISPLAY IF NOT OUT
	JRST 340F
	JRST 340F2

340HD1:	SKIPN 340XMD	;ESCAPE TO PARAM MODE
	PUSHJ P,340H1
340D1:	MOVEI I,74117	;PARAM WD
	IOR I,CSCALE
	PUSHJ P,340F	;STORE PARAM WD
	MOVEI I,6
	DPB I,[300600,,340BFP]	;SWITCH TO CHR MODE
	SETZM 340XMD
	POPJ P,

340K:	MOVEI I,40	;SPACE TO MAKE SPACING
	PUSHJ P,340FC1
	XCT AIDC1	;GET NON-SPACING LETTER AND DISPLAY
	JRST 340FC

340W:	JSP Q,WRDBT
	XCTR XRW,[MOVSS (C)]
	PUSHJ P,340H
	XCTR XRW,[MOVSS (C)]
340H:	SKIPGE DBLOAT
	POPJ P,
	SKIPN 340XMD
	PUSHJ P,340H1	;ENTER HALF WD MODE IF NOT ALREADY
	UMOVE I,(C)	;PICK UP LOSERS HALF WD
	JRST 340F

340H1:	MOVEI I,37	;IN CHR MODE, EXCAPE
	PUSHJ P,340F
	LDB I,[360600,,340BFP]
	JUMPE I,340H2
	CAIE I,22
	JRST 340H1
340H2:	MOVEI A,22
	DPB A,[300600,,340BFP]
	SETOM 340XMD
	POPJ P,

BLNKON:	MOVE A,DBLINK
	CAIL A,DBLNKN
	POPJ P,
	PUSHJ P,340HD1
	MOVE B,340BFP
	AND B,[770060,,1777]	;ADR OF PARAM WD TO BLINK INTEN OF
	TLO B,300+B
	MOVE A,DBLINK
	MOVEM B,DBLNKB(A)
	AOS DBLINK
	MOVEI T,15.
	PUSHJ P,CLQADD
	BLINKB
	JRST 340F2

BLNKOF:	PUSHJ P,340HD1
	JRST 340F2

BLNKBR:	SKIPN A,DBLINK
	JRST CLQRET	;BLINKING TURNED OFF
	SKIPL B,340BF
	SKIPGE CDISOFF
	JRST BLNKB3
	MOVE C,DBLNKF
BLNKB2:	DPB C,DBLNKB-1(A)
	SOJG A,BLNKB2
	SETCMM DBLNKF	;COMPLEMENT FLAG
BLNKB3:	MOVEI T,15.	;1/4 SEC
	MOVEI C,BLINKB
	JRST CLQREE

SUBTTL	340 DISPLAY

AIDS1:	MOVEI A,2
	PUSHJ P,ADSDV	;DETERMINE IF THIS LOSER SHOULD GET DISPLAY
	POPJ P,
AIDS3:	PUSHJ P,TCALL
	JRST IOMQ
	JRST AIDS2	;NO MEM AVAIL
	MOVEI B,MUDISB
	MOVE T,A
	LSH T,10.
	MOVEM T,340BF
	DPB B,[MUR,,MEMBLT(A)]
	MOVEI B,2
	MOVEM B,340MD
	JRST POPJ1

AIDS2:	SKIPG MEMFR
	 JRST AIDS9
	PUSHJ P,LOSSET
	    AIDS9
	PUSHJ P,UDELAY
	PUSHJ P,LSWDEL
	JRST AIDS3

IFN 0,[
;INTERPRETIVE DISPLAY COMPILER

IDISO:	PUSHJ P,AIDS1
	JRST OPNL10	;NOT AVAIL
	AOS DISNTO
	SETZM 340DMD
	SETOM AISWS
	HRLI T,442200
	MOVEM T,340BFP
	MOVEI Q,10135	;TURN OFF LP SCALE 1 INT 5
	IDPB Q,340BFP
	MOVEI T,1_4
	MOVEM T,CSCALE
	MOVEM T,VSCALE
	MOVEM T,ISCALE
	MOVSI A,(MOVE)	;DONT HACK CHR'S PER LINE
	MOVEM A,DISCHS
	MOVNM A,DISCHC
	CONO DIS,500
	MOVEI A,DN340I
	JRST OPNSLT


340I:	MOVEM C,AIDPCP
	UMOVE C,(C)

AIDL:	UMOVE R,(C)
AIDL2:	TRNE R,1
	JRST AIDL1	;NOT CHR MODE
	MOVEI Q,(HRRZ I,(A))
	HRLM Q,AIDC1
	MOVE Q,CSCALE
	IORI Q,60100
	PUSHJ P,AIDPY
	MOVE J,[220600,,Q]
AIDCL:	MOVE B,[440700,,R]
AIDC3:	ILDB A,B
EBLK
AIDC1:	HRRZ\HLRZ I,DISTBL(A)
BBLK
	TRNN I,-100
	JRST AIDC5
	TRNN I,-200
	JRST AIDC7
	PUSHJ P,(I)
	JRST AIDC1
AIDC5:	IDPB I,J
	TLNN J,770000
	PUSHJ P,AIDC2
	TLNE B,760000
	JRST AIDC3
	AOS C
	UMOVE R,(C)
	MOVE I,UEXIT
	CAMN I,[JRST ONEFLS]
	JRST AIDC6
	TRNN R,1
	JRST AIDCL
AIDC6:	MOVEI I,37
	IDPB I,J
	TLNE J,770000
	JRST .-2
	PUSHJ P,AIDC2

AIDL1:	MOVE B,UEXIT
	CAMN B,[JRST ONEFLS]
	JRST AIDI
	LDB B,[30300,,R]
	AOJA C,@AIDTB(B)

AIDC7:	MOVEI I,40
	PUSHJ P,AIDC4
	XCT AIDC1
	JRST AIDC5

AIDI:	MOVE R,AIDPCP
	UMOVEM C,(R)
	SOS UUOH
	JRST UUOE1

AIDTB:	AIER1
	AIPT
	AIVC
	AIINC
	AIOP
	AIVC
	REPEAT 2,AIER1

AIPT:	LDB I,[251600,,R]	;PICK UP Y
	SUB I,DWDB
	MOVEI TT,220000
	DPB I,[1200,,TT]
	LSH I,-10.
	ANDI I,17
	MOVEM I,YFLD
	LDB I,[61600,,R]	;X
	SUB I,DWDL
	MOVEI E,0
	TRNE R,2
	TRO E,2000	;INTENSIFY
	DPB I,[1200,,E]
	LSH I,-10.
	ANDI I,17
	MOVEM I,XFLD
	MOVEI Q,20000
	PUSHJ P,AIDPY
	MOVE Q,TT
	PUSHJ P,AIDPY
	MOVE Q,E
	PUSHJ P,AIDPY
	JRST AIDL

AIDC4C:	SKIPL DISCHC
	POPJ P,
	JRST AIDC4
]
AIDC4B:	AOSLE DISCHC
	POPJ P,
AIDC4:
IFN 0,[
	SKIPGE 340DMD
	JRST AIDC4A	;DIS
	IDPB I,J
	TLNE J,770000
	POPJ P,
AIDC2:	PUSHJ P,AIDPY
	MOVEI Q,0
	MOVE J,[220600,,Q]
	POPJ P,
]
AIDC4A:	IDPB I,340BFP
	POPJ P,
IFN 0,[
AIDPY:	HRLS Q
	DATAI DIS,AIDPIC
	DATAO DIS,Q
AIDP1:	MOVEI H,50
AIDP1A:	CONSZ DIS,DVEF+DHEF	;EDGE FLAGS
	JRST DEGFS
	CONSO DIS,200
	SOJG H,AIDP1A
	JUMPE H,AIER2
	CONSZ DIS,DVEF+DHEF
	JRST DEGFS
	SKIPN XFLD
	SKIPE YFLD
	JRST AIDP2
DEG4B:	HRRZ H,340BFP
	SUB H,340BF
	CAIL H,1777
	JRST AIER3
	IDPB Q,340BFP
AIDP2:	MOVEI Q,0
	POPJ P,

DEGFS:	DATAI DIS,DEGVCC
	CONI DIS,H
	LSH H,-15.
	ANDI H,7
	CAIN H,4	;VECTOR
	JRST DEGVC
	SKIPN XFLD
	SKIPE YFLD
	JRST DEG5A
	MOVEI Q,400000
	CAIN H,3
	MOVEI Q,373737
	PUSH P,H
	PUSHJ P,DEG4B	;LEAVING INSERT APPROPRIATE ESCAPE INSTEAD
	POP P,H
DEG5A:	CONO DIS,200
	MOVEI TT,50.
	CONSO DIS,DVEF+DHEF+200
	SOJG TT,.-1
	JUMPE TT,AIER2
	CONSO DIS,200
	JRST DEG5A	;RIDE OUT TO FINAL EDGE
	DATAI DIS,TT
	XOR TT,AIDPIC
	TRNN TT,1000
	JRST DEG5B
	HRR TT,AIDPIC
	TRNE TT,1000
	AOS XFLD
	TRNN TT,1000
	SOS XFLD
DEG5B:	TLNN TT,1000
	JRST DEG4
	HLL TT,AIDPIC
	TLNN TT,1000
	SOS YFLD
	TLNE TT,1000
	AOS YFLD
	JRST DEG4

DEG5:	MOVE TT,DEGVCC
	CONSZ DIS,DVEF
	JRST DEG1
DEG3:	CONSO DIS,DHEF
	JRST DEG4
	TRNE TT,1000
	SOS XFLD
	TRNN TT,1000
	AOS XFLD
DEG4:	MOVEI TT,0
	DPB TT,[44000,,XFLD]
	DPB TT,[44000,,YFLD]
	CONSO DIS,200
	CONO DIS,200	;CONTINUE
	MOVEI TT,100.
	CONSO DIS,200+DVEF+DHEF
	SOJG TT,.-1
	JUMPE TT,AIER2
	SKIPN XFLD
	SKIPE YFLD
	JRST AIDP1	;NOT COMING ON
DEG4F:	DATAI DIS,TT
	CAIN H,4
	MOVE TT,DEGVCC
	PUSH P,Q	;COMING ON PRODUCE SETPOINT
	PUSH P,H
	MOVEI Q,20100
	IOR Q,@DEGSCT(H)
	PUSHJ P,DEG4B
	HLRZ Q,TT
	IORI Q,220000
	PUSHJ P,DEG4B
	HRRZ Q,TT
	CONI DIS,H
	LSH H,-15.
	DPB H,[150300,,Q]
	MOVE TT,(P)
	CAIN TT,4
	JRST DEGVC1
DEG4D:	SUB P,[2,,2]
	JRST AIDP1

DEGSCT:	[0]
	[0]
	[0]
	CSCALE
	VSCALE
	VSCALE
	ISCALE
	[0]

DEG1:	TLNE TT,1000
	SOS YFLD
	TLNN TT,1000
	AOS YFLD
	JRST DEG3

DEGVC:	SETOM DEGVCF
	SKIPN XFLD
	SKIPE YFLD
	JRST DEG5	;NOT GOING OFF
	CLEARM DEGVCF
	HRRZ A,AIDPIC
	HLRZ B,AIDPIC
	PUSHJ P,DEG4C
	CONSZ DIS,DVEF
	SUBI B,2
	CONSZ DIS,DHEF
	SUBI A,2
	SKIPGE A
	MOVEI A,0
	SKIPGE B
	MOVEI B,0
	DPB B,[100700,,Q]
	DPB A,[700,,Q]
	TRO Q,400000
	PUSHJ P,DEG4B
	JRST DEG5

DEGVC1:	SKIPN XFLD
	SKIPE YFLD
	SKIPLE DEGVCF
	JRST .+2
	JRST DEG4D	;VECTOR TOTALLY OUT OF IT
	IORI Q,100000
	PUSHJ P,DEG4B
	POP P,H
	POP P,Q
	DATAI DIS,A
	HLRZ B,A
	HRRZS A
	PUSHJ P,DEG4C
	DPB B,[100700,,Q]
	DPB A,[700,,Q]
	MOVE A,DEGVCC
	MOVEM A,AIDPIC
	JRST AIDP1

DEG4C:	HRRZ I,DEGVCC
	SUB A,I
	HLRZ I,DEGVCC
	SUB B,I
	MOVMS A
	MOVMS B
	CAIGE A,1000
	JRST .+3
	MOVNS A
	ADDI A,2000
	CAIGE B,1000
	JRST .+3
	MOVNS B
	ADDI B,2000
	LDB I,[40200,,VSCALE]
	MOVNS I
	ASH A,(I)	;GET X INC IN A
	ASH B,(I)	;Y IN B
	POPJ P,

AIINC:	MOVE Q,ISCALE
	IORI Q,140100
	PUSHJ P,AIDPY
	SETOM DDINCI
	MOVEI Q,0
	MOVE B,[200400,,Q]
AIIN1:	MOVE D,[440600,,R]
AIIN8:	ILDB T,D
	LDB E,[200,,T]	;LOAD COUNT
	JUMPE E,AIIN5	;NULL INCREMENT
AIIN7:	TRNN T,4	;SKIP ON INTENSIFY
	JRST AIIN2
	SKIPG DDINCI	;SKIP ON SET TO INTENSIFY
	JRST AIIN3

AIIN7A:	LDB H,[30300,,T]
	MOVE H,DINCT(H)
	IDPB H,B
	PUSHJ P,AIIN6	;MAYBE START NEW WORD
	SOJG E,AIIN7

AIIN5:	TLNE D,700000
	JRST AIIN8
	UMOVE R,(C)
	LDB H,[30300,,R]
	TRNE R,1
	CAIE H,3	;INC MODE
	TROA Q,400000
	AOJA C,AIIN1
	PUSHJ P,AIIN6A
	JRST AIDL2

AIIN6:	TLNE B,770000
	POPJ P,

AIIN6A:	PUSHJ P,AIDPY
	MOVEI Q,0
	SETOM DDINCI
	MOVE B,[200400,,Q]
	POPJ P,

AIIN2:	SKIPN DDINCI
	JRST AIIN7A	;INTENSITY AGREES
AIIN3:	SKIPL DDINCI
	PUSHJ P,AIIN6A
	HRRZM P,DDINCI
	TRNE T,4
	TROA Q,200000
	CLEARM DDINCI
	JRST AIIN7

AIVC:	MOVE Q,VSCALE
	IORI Q,100100
	LDB A,[251700,,R]	;DY
	LDB B,[61700,,R]	;DX
	TRNE A,40000
	ORCMI A,77777
	TRNE B,40000
	ORCMI B,77777
	MOVM E,A
	MOVM TT,B
	MOVE H,VSCALE
	LSH H,-4
	ANDI H,3
	MOVNS H
	LSH E,(H)
	LSH TT,(H)
	JUMPN E,.+2
	JUMPE TT,AIDL
	PUSHJ P,AIDPY
AIVCL:	MOVE H,E
	MOVE J,TT
AIVC3:	CAIG J,177
	CAILE H,177
	JRST AIVC2
	SUB E,H
	DPB H,[100700,,Q]
	SKIPGE A
	TRO Q,1_<8+7>
	SUB TT,J
	DPB J,[700,,Q]
	SKIPGE B
	TRO Q,1_7
	TRNE R,1_1
	TRO Q,200000
	JUMPN E,.+3
	JUMPN TT,.+2
	TRO Q,400000
	PUSH P,A
	PUSH P,B
	PUSH P,E
	PUSH P,TT
	PUSHJ P,AIDPY
	POP P,TT
	POP P,E
	POP P,B
	POP P,A
	JUMPN E,AIVCL
	JUMPN TT,AIVCL
	JRST AIDL

AIVC2:	LSH J,-1
	LSH H,-1
	JRST AIVC3

AIOP:	LDB B,[60300,,R]
	JRST @AIOPT(B)

AIOPT:	AIER5
	AIPSJ
	AIPPJ
	AIJMP
	AIPSH
	AIIPOP	;5
	AIPS
	AIER5
	AIER5

AIPSJ:	XCTR XRW,[AOS D,43]	;PICK UP PDL PNTR AND INCR
	XCTR XRW,[MOVEM C,(D)]	;STORE RETURN PC
AIJMP:	HLRZ C,R
	JRST AIDL

AIPPJ:	UMOVE D,43
	XCTR XRW,[HRRZ C,(D)]
	JUMPE C,AIEXT	;POPJ INTO 0 START SIGNAL
	XCTR XRW,[SOS 43]
	JRST AIDL

AIEXT:	SKIPL AISWS
	JRST AIDST3
AIDST:	MOVEI Q,3000
	PUSHJ P,DEG4B
	MOVE A,340BF
	ADD A,[-2000-1,,-1]
	MOVEM A,IDBLKP
	MOVEM A,DBLKOP
	MOVEI A,1
	MOVEM A,DTIME
	SETZM DISOFF
	CONO DIS,1100\DISCHN\SDCHN_3
AIDST3:	POPJ P,

AIPSH:	HLRZ TT,R
	UMOVE TT,(TT)
	XCTR XRW,[AOS D,43]
	UMOVEM TT,(D)
	JRST AIDL

AIIPOP:	HLRZ TT,R
	XCTR XRW,[SOS D,43]
	UMOVE E,1(D)
	UMOVEM E,(TT)
	JRST AIDL

AIPS:	LDB A,[140600,,R]
	HLRZ B,R
	CAIL A,AIMXP
	JRST AIER7
	ANDI B,3
	LSH B,4
	XCT AIPST(A)
	JRST AIDL

AIPST:	JRST AIER7
	HLLEM R,AISWS	;START MODE
	MOVEM B,CSCALE
	MOVEM B,ISCALE
	MOVEM B,VSCALE
	JRST AIPSA
	HLRM R,DWDL
	HLRM R,DWDB
AIMXP==.-AIPST

AIPSA:	MOVEM B,CSCALE
	MOVEM B,ISCALE
	MOVEM B,VSCALE
	JRST AIDL
]
EBLK
XFLD:	0	;HIGH ORDER BITS OF X COOR REG (LIGHT=0)
YFLD:	0	;  ... Y
CDISOF:	0	;DISPLAY OFF IN CORE ALLOC
340BF:	-1	;POINTER TO 1K BLOCK FOR 340 BF (MEM ADR)
340BFP:	0	;POINTER TO 340BF
	DBLNKN==10.
DBLINK:	0	;0=IDLE ;+N=NUM OF ENTRIES
DBLNKF:	0	;BLINK FLAG, -1 MEANS START BLINK OFF
DBLNKB:	BLOCK DBLNKN	;BYTE POINTER TO PARAMETER HW TO CHANGE
BLINKB:	0	;CLOCK QUEUE BLOCK
	-1
	JRST BLNKBR
AIDPCP:	0	;USER LOCN WHERE TO STORE BACK PC IF NECC
CSCALE:	0	;SCALE FOR CHR WRDS
ISCALE:	0	;SCALE FOR INCREMENT
VSCALE:	0	;SCALE FOR VECTOR WDS
DWDL:	0	;14 BIT COOR OF LH 340
DWDB:	0	;14 BIT COOD OF BOT 340
DEGVCF:	0	;EDGE FLAG FOR VECT -1 UNLESS GOING AWAY
DEGVCC:	0	;COORD AT EDGE
DDINCI:	0	;-1 CURRENT INTENSITY NOT COMMITED (IN INCREMENT MODE)
	;+ => 1 - => 0
AISWS:	0	;SELECT DISPLAY START MODE -1 STARTS DISPLAY AFTER IOT
AIDPIC:	0	;COORDINATES AT START OF AIDPY

LLTPSN:	0	;. TO .+3 BLTED TO USER
LTPCNT:	0
LTPCY:	0
LTPCX:	0
LTPDCN:	200\SDCHN_3\DISCHN

LDISP:	0	;DISPLAY LINK POINTER
LMODE:	0	;LISP LINK MODE FLAG
DISOFF:	-1	;-1 IF DIS NOT IN USE OR STOPPED DUE TO FRAME HACK
DTIME:	0

340MD:	-1	;MODE IN WHICH DISPLAY OPEN
		;-1 NOT DISPLAYING 0 .DSTART ETC 2 AS DIS OR IDS
340DMD:	0	;0 IDS -1 DIS
340XMD:	0	;IN DIS, 0=CH 1=IMAGE
DBLOAT:	0
340T:	0	;TIMER FOR DIS PAGE
LUBLKP:	0	;LAST USER BLKO PNTR SET UP AT IN LEVEL, FOR DEBUGGING

;START 340 EXECPG AREA

A340P1:	-1	;USER ADR THAT EXEC PG SET UP FOR
A340P2:	-1
340DA:	REPEAT N340PB,-1
N340CL==.-A340P1	;THIS BLOCK MUST BE CONSEC
340DP:	0	;0=> N30PB-1 PNTR TO DATA PGS
340PP:	0	;PNTR TO 340P1 OR P2 ALTNERATELY

DPAP:	400000+340P1*2000(A)	;EXEC ADR TO REF PAGE
	400000+340P2*2000(A)
DDAD:	REPEAT N340PB, 400000+<DPG0+.RPCNT>*2000(A)

DPEP:	.340P1+EXEUMP	;BYTE PNTR TO EXEC MAP PAGE ENTRY
	.340P2+EXEUMP
DDEP:	REPEAT N340PB, CONC .DPG,\.RPCNT,+EXEUMP

DPBS:	BLOCK 8	;SAVE PAGE BOX AT PI DIS LVL

DROOT:	0	;DIS RESTART POINTER TO LOSER CORE
DBLKOP:	0	;TEMP DIS BLKO PTR
LDBLKP:	0	;LAST
DISON:	0	;-1 IF DIS STOPPED AFTER SPECIAL INT OR BLKO OVERFLOW
		;IE RESTART AFTER PASSAGE OF TIME
DISDIE:	0	;0	;-1 IF DIS HASNT DIED
DISTPR:	0	;-1 TO TURN OFF DISPLAY
IDBLKP:	0	;BLKO PNTR IF IN INTERP DISPLAY MODE
NDSFMS:	-1	;-1 NOT HACKING FRAME HACK ELSE NUMBER OF FRAMES REMAINING
DISNTO:	0	;# TIMES DIS OR IDS OPEN
DWDS:	0	;- # WDS SENT TO DIS-EWRT PER TRIP THRU SRECYC
		;RESET TO MDISWD EVERY 1/30
		;IF <0 DISPLAY STOPS FOR 5 SEC -300.->DTIME

DISCHS:	0	;LENGTH OF LINE IN CURRENT SIZE
DISCHC:	0	;-# CHR POS REMAINING
DISLNL:	0	;# OF LINES WILL FIT CURRENT SIZE
DISLNS:	0	;# LINES REMAINING
BBLK

DEFINE LC A
A,,DISLC
TERMIN

DEFINE UC A
DISUC,,A
TERMIN

DEFINE AC A
A,,A
TERMIN



ADISCR:	MOVE I,DISCHS
	MOVNM I,DISCHC
	MOVEI I,34
	JRST POPJ1

DISALF:	SOS DISLNS
	MOVEI I,33
	JRST POPJ1

DSATB:	MOVEI I,40	;B
	PUSHJ P,AIDC4B
	LDB I,[300,,DISCHC]
	SKIPG DISCHC
	JUMPN I,DSATB
	JRST POPJ2

DISBLB:	MOVEI I,50	;DOWN ARROW
	PUSHJ P,AIDC4B
	TRO A,100	;CONVERT TO UC LETTER
DISUC:	SKIPA I,[HRRZ I,35(A)]	;SWITCH TO UC
DISLC:	MOVE I,[HLRZ I,36(A)]	;SWITCH TO LC
	PUSHJ P,AIDC4	;STORE CASE CODE
	HLLM I,AIDC1
	POPJ P,

ADISBS:	MOVEI I,72	;BACKSPACE
	SOS DISCHC
	JRST POPJ1

DISRUB:	MOVEI I,"X&77	;LC X
	PUSHJ P,AIDC4
	MOVEI I,72	;340 BACKSPACE
	PUSHJ P,AIDC4
	MOVEI A,"O	;YES, A
	POPJ P,	;LOOP BACK

DISTBL:	POPJ2,,POPJ2	;IGNORE 0
	REPEAT 6,LC DISBLB
	LC 63	;BELL
	LC ADISBS
	AC DSATB
	AC DISALF
	LC DISBLB
	LC DISBLB
	AC ADISCR
	REPEAT 33-16,LC DISBLB
	LC 47	;ALT MD (RT ARROW)
	REPEAT 4,LC DISBLB
	AC 40
	REPEAT 133-41,UC <.-DISTBL>&77
	LC 53
	LC 52	;BACKSLASH
	LC 54
	LC 100+67
	LC 60
	LC 100+66
	REPEAT 173-141,LC <.-DISTBL-140>
	LC 55
	LC 62	;VERTICAL BAR=174
	LC 56
	LC 43	;TILDE=176
	LC DISRUB
IFN .-DISTBL-200,PRINTX /DISTBL LOSS/


DINCT:	10
	2
	14
	3
	12
	16
	17
	13



REPEAT NDOPL, CONC AIER,\.RPCNT+1,:	JSP D,AIER

AIER:	HRRZ R,UUAC(U)
	ADDI R,IOCHNM(U)
	MOVEI D,1+1-AIER1(D)
	LSH D,6+18.
	IORM D,IOCHST-IOCHNM(R)
	MOVE T,AIDPCP
	UMOVEM C,(T)
	JRST IOCERR
]

IFN VIDP,[
;
SUBTTL	NEW VIDISECTOR ROUTINES
;
TVCO:	HLRZ D,C
	SKIPA Q,[TVCOBK]
NVIDI:	MOVEI Q,NVDOBK
	PUSH P,R
	PUSH P,[NVIDI2]
	JRST STDOP1

NVIDI2:	JRST POPAJ
	POP P,R
	PUSHJ P,NVDGET
	JRST NVIDI3
NVIDI1:	MOVEM A,(R)
	CAIN Q,TVCOBK
	AOS NTVCOS
	CAIN Q,TVCOBK
	TLNE C,1	;ONLY DO IF TVC OUTPUT OPEN
	PUSHJ P,TVCNOS
	JRST POPJ1

NVIDI3:	PUSHJ P,OPNL25
	JRST NVDCLS

NVDOBK:	1,,NVDUSR
	NVIDOP,,BNVIDO

BNVIDI:	JSP Q,WRDBT
NVIDIT:	SKIPGE NVDIFL
	PUSHJ P,UFLS
	UMOVE B,(C)
	MOVEM B,NVDCOR
	CONO PI,OMXOFF
	SETOM NVDIFL
	PUSHJ P,NVDVS1	;TURNS OMPCH ON
	SKIPGE NVDIFL
	PUSHJ P,UFLS
	MOVE B,NVDCOR
	UMOVEM B,(C)
	POPJ P,

RNVDIB:	JSP Q,WRDBT
RNVDIU:	SKIPN NVDCNT	;TVC INPUT
	JRST RNVDO2
	XCTR XRW,[MOVES (C)]	;MAKE SURE GOING TO WIN
	MOVE T,NVDOPT
	CAMN T,NVDVPT
	PUSHJ P,UFLS
	MOVE B,(T)
	SOS NVDCNT
	UMOVEM B,(C)
	AOS T,NVDOPT
	CAIL T,NVDBUF+NVDLNG
	MOVEI T,NVDBUF
	MOVEM T,NVDOPT
	POPJ P,

TVCOBK:	3,,NVDUSR
	DNVDIU,,DNVDOU
	DNVDIB,,DNVDOB

TVCCLS:	SOSGE NTVCOS
	PUSHJ P,TVCINI	;TRY TO AVOID GARBAGE PNT PART WAY VIDISECTED
NVDCLS:	SOSGE NVDUSE
	PUSHJ P,VIDFLS
	POPJ P,

TVCNOS:	LDB A,[251700,,C]	;TVC OPEN ROUTINE
TVCN1:	TRZ A,703000	;DONT LET LOSER FOOL AROUND
	IORI A,NVDCHN_15.
	LDB B,[20300,,A]	;DCO
	JUMPE B,TVCO3
	CAIN B,7
	JRST TVCO6	;DCO DISABLED FOR DCL=7
	IMUL B,[-100]
TVCO4:	ADDI B,1300
TVCO7:	MOVEM B,NVDDK	;DARK VALUE CORRECT FOR DCO
	MOVE TT,A
	XOR TT,TVCONO
	TRNE T,340
	JRST TVCO5	;CHANGE IN VIDI SELECTION STATUS
TVCO5A:	MOVEM A,TVCONO
	POPJ P,

TVCO3:	LDB T,[200,,A]	;CONF
	CAIN T,3
	MOVNI B,100	;IF CONF=3 & DCO=0 VALUE 100 LESS THAN PREDICTED ABOVE
	JRST TVCO4

TVCO5:	TRZ A,700000	;CLEAR CHNL OUT OF TVCONO
	CONO NVDX,(A)	;START RLYS
	MOVEI T,2
	SKIPL TVQBLK+1	;WAIT IF PREV RQ NOT SATISFIED
	PUSHJ P,UFLS
	PUSHJ P,CLQADD	;RQ LATER TRANSFER TO VIDD1
	TVQBLK
	JRST TVCO5A

TVCO6:	LDB T,[200,,A]	;CONF
	IMUL T,[-200]	;CALCULATE WHEN OVF HAPPENS
	ADDI T,2000
	JRST TVCO7

TVCINI:	CONO PI,OMXOFF
	SETZM NVDCNT
	MOVE T,NVDIPT
	MOVEM T,NVDVPT
	MOVEM T,NVDOPT
	MOVE T,NVDBKR
	CAIN T,1
	SETOM NVDBKR
	JRST OMXONJ

VIDFLS:	SKIPL NVDUSE	;SKIP IF OPENS ON NVD, .VSCAN
	POPJ P,
	DATAO DSDEV,[DSNVID]	;DEASSIGN VIDI
	SETOM NVDUSR
	POPJ P,

NVDGET:	CONO NVDX,@TVCONO	;TRY TO ASSIGN BAT
	CONI NVDX,TT
	JUMPE TT,CPOPJ
	JRST POPJ1


NVDBRK:	MOVE T,NVDBKR
	JRST NVTAB(T)	;DISPATCH ON REASON FOR BREAK

	JRST NEWORK	;STARTUP OF SOME NEW FUNCTION
NVTAB:	JRST SCNB1	;NEW VSCAN POINT
	JRST TVB1	;NEW TVC POINT
	JRST INVD1	;NEW NVD PNT
;	JRST DRET	;NEW DAEMON POINT

TVB1:	DATAI NVDX,@NVDVPT	;READ IN OF TV POINT
	CONI NVDX,T
	LSH T,-11.
	DPB T,[220200,,@NVDVPT]	;READ ALL RELAVENT INFO FROM BAT
	TRNE T,3
	JRST TVB2	;DCO OR OVFL
	MOVE T,@NVDVPT
	ANDI T,1777
	CAMLE T,NVDDK
	JRST TVB2	;DUE TO NOISE/PROB DARKER THAN DCO
TVB3:	AOS T,NVDVPT
	CAIL T,NVDBUF+NVDLNG
	MOVEI T,NVDBUF
	MOVEM T,NVDVPT	;INCREMENT TO NEXT POINT
	JRST NEWORK

TVB2:	MOVE T,NVDDK
	DPB T,[1200,,@NVDVPT]
	DPB T,[240600,,@NVDVPT]	;PUT IN FLOATING PART
	LSH T,-6
	DPB T,[330400,,@NVDVPT]
	JRST TVB3

TVNEXT:	MOVE T,NVDVPT
	CAMN T,NVDIPT
	JRST TVCFIN
	MOVEI T,1	;SET UP FOR NEXT TV POINT
	MOVEM T,NVDBKR
	MOVE T,NVDVPT
	HLRZ T,@NVDVPT
	CONO NVDX,@TVCONO
	DATAO NVDX,T
	EXCH T,VIDXOL
	SUB T,VIDXOL
	MOVMM T,VIDTEM
	HRRZ T,@NVDVPT
	DATAO NVDY,T
	EXCH T,VIDYOL
	SUB T,VIDYOL
	MOVMS T
	CAMGE T,VIDTEM
	MOVE T,VIDTEM
	CAIL T,1000
	JRST TVB5
	DATAO NVDT,[-4]
NVDBR4:	JRST OMXRET

TVB5:	TLC T,232000	;CALCULATE SETTLING TIME
	FAD T,T
	LDB T,[330400,,T]
	DATAO NVDT,SETLL-10.(T)
	JRST NVDBR4

SETLL:		;SETTLING TIME IN 10 MICROSEC
	-6	;DEFLECTING 512 - 1023 
	-10	;1024 - 2047
	-16	;2048 - 4095
	-26	;4096 - 8191
	-45	;8192 - 16383
	-70	;16384

RNVDOB:	JSP Q,WRDBT
RNVDOU:	MOVEI T,NVDLNG-1	;TO PREVENT HANGUP AT PI ON WRAPAROUND
	CAMG T,NVDCNT
	JRST RNVDO2
	UMOVE B,(C)
	MOVEM B,@NVDIPT
	AOS NVDCNT
	AOS T,NVDIPT
	CAIL T,NVDBUF+NVDLNG
	MOVEI T,NVDBUF
	MOVEM T,NVDIPT
	CONO PI,OMXOFF
	SETOM TVFLG
NVDVS1:	MOVE T,TVCONO
	SKIPGE NVDBKR
	CONO NVDX,2000(T)
	JRST OMXONJ

RNVDO2:	POP P,T	;CALLED FROM RNVDI ALSO
	ANDI T,-1
	CAIE T,WRDBRT
	JRST IOCER9
	POPJ P,

NEWORK:;	SKIPGE DAEMFL
;	JRST DNEXT
	SKIPL SCNUSR	;DEFLECT FOR NEW VSCAN POINT
	JRST SCNB2
	SKIPGE NVDIFL	;NVD
	JRST INVD2
	SKIPGE TVFLG	;DEFLECT FOR NEW TVC POINT
	JRST TVNEXT
	SETOM NVDBKR	;NO MORE CROCKS FOR VIDISECTOR TO DO
	MOVE T,TVCONO
	TRZ T,703000	;SAVE STATE OF VIDI SELECT
	CONI NVDX,A
	CONO NVDX,(T)
	SKIPN A
	DATAO DSDEV,[DSNVID]	;RE DEASSIGN VIDI IF WAS DEASSIGNED 
			;(CONO DID CLEAR PIA, DONE, AND SET MODE FLOPS THO)
	JRST OMXRET

TVCFIN:	SETZM TVFLG
	JRST NEWORK

INVD1:	DATAI NVDX,NVDCOR
	CONI NVDX,T
	LSH T,-11.
	DPB T,[220200,,NVDCOR]
	SETZM NVDIFL
	JRST NEWORK

INVD2:	MOVEI T,2
	MOVEM T,NVDBKR
	CONO NVDX,@TVCONO
	HLRZ T,NVDCOR
	DATAO NVDX,T
	HRRZ T,NVDCOR
	DATAO NVDY,T
	DATAO NVDT,[-7]
	JRST NVDBR4

SUBTTL	NEW VIDISECTOR ROUTINES - .VSTST

AVSTST:	UMOVE A,(J)
	CAME U,NVDUSR
	JRST AVST4
	JUMPE A,AVST1	;READ CURRENT STATE
	JUMPL A,AVST2
AVST3:	SKIPL SCNUSR	;HANGUP TILL VSCAN FINISHED
	PUSHJ P,UFLS
AVST2:
SCNSTP:	CONO PI,OMXOFF	;FLUSH VSCAN ENTIRELY
	SKIPGE SCNUSR
	JRST OMXONJ	;ALREADY FINISHED
	SETOM SCNUSR
	SOSGE NVDUSE
	SETOM NVDUSR
	SKIPN NVDBKR	;IGNORE VSCAN POINT IN PROGRESS IF ANY
	SETOM NVDBKR
VSLS2:	PUSH P,A
	PUSH P,B
	PUSH P,W
	MOVSI B,-1
	MOVSI W,-2
SCNS2:	LDB A,VEXT(W)
	TRNN A,600000
	JRST SCNS4
	ANDI A,PMRCM
	ADDM B,MMSWP(A)
SCNS4:	MOVEI A,0
	DPB A,VEXT(W)
	AOBJN W,SCNS2
	MOVE A,USER
	SPM UPGML(A)
	LPMR UPGML(A)
	POP P,W
	POP P,B
	POP P,A
	PUSHJ P,VIDFLS
	JRST OMXONJ

AVST4:	JUMPE A,ILUUO
	POPJ P,

AVST1:	SKIPGE SCNUSR
	POPJ P,
	MOVE A,NVDPTR
	SUBI A,400000+VSB1*2000
	JRST APTUAJ

;CHECK TO SEE IF MEM PROTECT STILL OK
SCNACR:	CAMN U,SCNUSR
	POPJ P,
	AOS (P)
	JRST SCNSTP

;STOP SCAN
SCNSTC:	CONO NVDX,0
	POPJ P,

;RESTART SCAN AFTER STOP FOR SHUFFLING
SCNRST:	CONO NVDX,NVDCHN_17
	POPJ P,


SUBTTL	NEW VIDISECTOR ROUTINES - .VSCAN

AVSCAN:	XCTR XRW,[MOVES (C)]
	XCTR XRW,[MOVES 12(C)]
	SKIPL SCNUSR
	PUSHJ P,UFLS
	CONO PI,CLKOFF
	CAME U,NVDUSR
	SKIPGE NVDUSE
	AOSA NVDUSE
	JRST ILUUO
	MOVEM U,NVDUSR
	CONO PI,CLKON
	PUSHJ P,NVDGET
	JRST VSLOS
A.VS1:	SETZM BATMXA
	XCTR XRW,[HLRZ A,2(C)]
	MOVEM A,NVDCNX
	MOVEM A,NVDCN3
	XCTR XRW,[HRRZ A,2(C)]
	MOVEM A,NVDCNY
IRPS XY,,X Y P X Y P,A12,,1 1 1 2 2 2,N,,3 6 11 4 7 12,YX,,X X X Y Y Y
	UMOVE A,N(C)
	IDIV A,NVDCN!YX
	MOVEM A,BATD!XY!A12
TERMIN
IRP XY,,[P,X,Y]ADR,,[[[1,,]],5(C),10(C)]
	MOVE A,BATD!XY!1
	ADD A,BATD!XY!2
	ASH A,-1
IFE .IRPCNT,	ADD A,ADR
IFN .IRPCNT,	XCTR XRW,[ADD A,ADR]
	MOVEM A,BAT!XY!1
	MOVEM A,BAT!XY!2
	IFN .IRPCN,[IDIV A,BATP1
	MOVEM A,NVID!XY]
TERMIN
	XCTR XRW,[HRRZ B,1(C)]
	LDB A,[121000,,B]
	CAIL A,376
	JRST VSLOS
	MOVEI W,0
	PUSHJ P,VSMS1
	MOVEI W,1
	PUSHJ P,VSMS1
	SPM UPGML(U)
	LPMR UPGML(U)
	HRRZ R,B
	ANDI R,776000
	ADDI R,4000-2000
	XCTR XRW,[HLRO D,1(C)]
	MOVE A,NVDCNX
	IMUL A,NVDCNY
	JUMPLE A,VSLOS	;SIZE OF RASTOR
	MOVNS D		;PLUS COUNT
	CAMLE A,D
	JRST VSLOS	;WON'T FIT
	CAML A,R
	JRST VSLOS	;WON'T FIT IN EXEC PGS
	ADD A,B
	MOVEM A,BATMXA
	LDB A,[121000,,A]
	PUSHJ P,UPLC
	LDB A,T
	TRC A,600000
	TRCE A,600000	;SKIP ON R/W/F
	JRST VSLOS	;TOP PAGE LOSES
	ANDI B,1777
	ADDI B,400000+VSB1*2000
	MOVEM B,NVDPTR
	XCTR XRW,[HRRZ A,(C)]
	PUSHJ P,TVCN1
	MOVE A,[JRST SCNG1]
	MOVEM A,SCNB1A
	CONO PI,OMXOFF
	MOVEM U,SCNUSR
	MOVE A,TVCONO
	SKIPGE NVDBKR
	CONO NVDX,2000(A)
	CONO PI,OMXON
	XCTR XRW,[SKIPL (C)]
	POPJ P,
	JRST AVST3

VSLS3:	PUSHJ P,LSWPOP
VSLOS:	PUSHJ P,VSLS1
	JRST ILUUO


VPGLOS:	PUSHJ P,VSLS1
	PUSHJ P,TPFLT
	JRST UUOTRO

VSLS1:	SOSGE NVDUSE
	SETOM NVDUSR
	CONO PI,OMXOFF
	JRST VSLS2

VSMS1:	PUSHJ P,SWTL
	CIRPSW
	LDB A,[121000,,B]
	ADD A,W
	MOVEM A,VSMPG
	PUSHJ P,UPLC
	CONO PI,CLKOFF
	LDB A,Q
	JUMPE A,VSMS2	;NO ACCESS
	CAIN A,-1
	JRST VSLS3
	LDB A,T
	TRNN A,600000
	JRST VSMS3	;SWAPPED OUT
	TRNN A,400000
	JRST VSLS3	;RD ONLY
	TRO A,200000
VSMS2:	DPB A,T		;CHANGE R/W/F TO R/W
	LDB TT,VEXT(W)
	DPB A,VEXT(W)
	MOVSI Q,1
	ANDI A,PMRCM
	SKIPE A
	ADDM Q,MMSWP(A)
	JUMPE TT,VSLS4
	ANDI TT,PMRCM
	MOVSI Q,-1
	ADDM Q,MMSWP(TT)
VSLS4:	PUSHJ P,LSWPOP
	JRST CLKONJ

VSMS3:	PUSHJ P,LSWPOP
	MOVE A,VSMPG
	SOSGE NVDUSE
	SETOM NVDUSR
	PUSHJ P,CWAIT
	SKIPL VIDSWI
	MOVEM U,VIDSWI
	MOVEM A,VIDSPG
	SKIPL VIDSWI
	PUSHJ P,UFLS
	SUB P,[1,,1]
	JRST AVSCAN
SCNB1:	DATAI NVDX,@NVDPTR
	CONI NVDX,T
	LSH T,-11.
	DPB T,[220200,,@NVDPTR]
	TRNE T,3
	JRST SCNB1C	;OVF OR DCO?
	MOVE T,@NVDPTR
	ANDI T,1777
	CAMG T,NVDDK	;OR VALUE GREATER THAN DCO LEVEL
	JRST NEWORK
SCNB1C:	MOVE T,NVDDK
	DPB T,[1200,,@NVDPTR]
	DPB T,[240600,,@NVDPTR]	;PUT IN FLOATING PART
	LSH T,-6
	DPB T,[330400,,@NVDPTR]
	JRST NEWORK

SCNB2:	CONO NVDX,@TVCONO	;SETUP FOR NEXT VSCAN POINT
	DATAO NVDX,NVIDX
	DATAO NVDY,NVIDY
	MOVE T,NVIDX
	EXCH T,VIDXOL
	SUB T,VIDXOL
	MOVMM T,VIDTEM
	MOVE T,NVIDY
	EXCH T,VIDYOL
	SUB T,VIDYOL
	MOVMS T
	CAMGE T,VIDTEM
	MOVE T,VIDTEM
	CAIL T,1000
	JRST SCNT5
	DATAO NVDT,[-4]
SCNT4:	CLEARM NVDBKR
	EBLK
SCNB1A:	JRST SCNG1	;OR AOS (AOSA) NVDPTR
	BBLK
	JRST SCNS1	;HOMO LINEAR FROB
SCNB1B:	SOSG NVDCNX	;SIMPLE MINDED
	JRST SCNB3
IRPC X,,XY
	MOVE T,BATD!X!1
	ADDB T,BAT!X!1
	HLRZM T,NVID!X
TERMIN
SCNBX:	JRST OMXRET

SCNT5:	TLC T,232000	;CALCULATE SETTLING TIME
	FAD T,T
	LDB T,[330400,,T]
	DATAO NVDT,SETLL-10.(T)
	JRST SCNT4

SCNB3:	SOSG NVDCNY
	JRST SCNB4
IRPC X,,XY
	MOVE T,BATD!X!2
	ADDB T,BAT!X!2
	MOVEM T,BAT!X!1
	HLRZM T,NVID!X
TERMIN

	MOVE T,NVDCN3
	MOVEM T,NVDCNX
	JRST SCNBX

SCNB4:	SKIPL NVDCNY	;WAIT FOR LAST DATAO TO RETURN A POINT
	JRST SCNBX
	SETOM SCNUSR
	SOSL NVDUSE
	JRST NEWORK
	SETOM NVDUSR
	DATAO DSDEV,[DSNVID]	;DEASSIGN BAT
	MOVSI B,-1
	MOVSI W,-2
SCNB4B:	LDB A,VEXT(W)
	TRNN A,600000
	JRST SCNB4A
	ANDI A,PMRCM
	ADDM B,MMSWP(A)
SCNB4A:	MOVEI A,0
	DPB A,VEXT(W)
	AOBJN W,SCNB4B
	JRST NEWORK

SCNS1:	MOVEM U,NVDTMU
	SOSG NVDCNX
	JRST SCNS3
IRPC X,,PXY
	MOVE T,BATD!X!1
	ADDB T,BAT!X!1
	IFN .IRPCN,[IDIV T,BATP1
	MOVEM T,NVID!X]
TERMIN

SCNBX1:	MOVE U,NVDTMU
	JRST SCNBX

SCNS3:	SOSG NVDCNY
	JRST SCNB4
IRPC X,,PXY
	MOVE T,BATD!X!2
	ADDB T,BAT!X!2
	MOVEM T,BAT!X!1
	IFN .IRPCN,[IDIV T,BATP1
	MOVEM T,NVID!X]
TERMIN
	MOVE T,NVDCN3
	MOVEM T,NVDCNX
	JRST SCNBX1

SCNG1:	MOVSI T,(AOSA)
	SKIPN BATDP1
	SKIPE BATDP2
	MOVSI T,(AOS)
	HRRI T,NVDPTR
	MOVEM T,SCNB1A
	TLNN T,(AOSA-AOS)
	JRST SCNS1
	JRST SCNB1B


SUBTTL	NEW VIDISECTOR ROUTINES

EBLK

NVDIPT:	NVDBUF	;POINTER TO INSERT IN BUF
NVDVPT:	NVDBUF	;POINTER TO WORD  BEING VIDISECTED
NVDOPT:	NVDBUF	;POINTER TO WITHDRAW FROM BUF
NVDCNT:	0	;NUMBER PTS IN BUF
NVDBUF:	BLOCK NVDLNG
VIDSTRT:	0
LBTCNO:	0	;LAST CONO TO BAT
NVDIFL:	0	;+=> RQ TO READ NVD PNT
NVDCOR:	0	;NVD DEV, COORDINATES TO PI, READ DATA FROM PI
NTVCOS:	-1	;# TVC OPENS

IRPC X,,XYP
	IRPC N,,12
		BAT!X!!N:	0
		BATD!X!!N:	0
	TERMIN
TERMIN

NVDCNX:	0
NVDCNY:	0
NVDCN3:	0
NVIDX:	0
NVIDY:	0
BATMXA:	0
NVDPTR:	0
NVDTMU:	0
SCNUSR:	-1	;USER IN CURRENT .VSCAN, -1 .VSCAN FREE
SCNREL:	0
TVCONO:	0	;CONO FOR TVC DEVICE AND VSCAN (NORM HAS CHNL BUT NOT IF IN VIDDLY)
NVDDK:	0	;-1=>VID VALUE CORRESP TO DCO
TVFLG:	0	;-1=>TVC WORK IN PROGRESS
NVDBKR:	-1	;REASON LAST POINT READ IN, DISPATCH ADDRESS FOR INTS
			;-1 NONE  0 VSCAN  1 TVC  2 NVD
TVQBLK:	0	;CLOCK QUEUE BLOCK
	-1
	JRST VIDD1
VIDXOL:	0	;OLD VIDI CORDS
VIDYOL:	0	;OLD VIDI CORDS
VIDTEM:	0	;LARGEST DELTA
VSMPG:	0
VEXT:	.VSB1+EXEUMP
	.VSB2+EXEUMP

BBLK

VIDD1:	MOVEI A,NVDCHN_15.
	IORB A,TVCONO	;RESTORE CHNLS TO TVCONO
	CONO NVDX,2000(A)	;CAUSE EVENTUAL VIDI INT TO SEE IF ANYTHING YO DO
	JRST CLQRET	;RETURN TO CLOCK ROUTINE
]

SUBTTL	VIDEO SWITCH AND VIDEO BUFFER ROUTINES

;SYSTEM CALLS FOR HACKING THE VIDEO SWITCH AND VIDEO BUFFERS.

IFE N11TYS,[
NVDBF==OPNL1
NVIDSW==OPNL1
]

IFN N11TYS,[

VBDFLS:	PUSH P,B
	PUSH P,T
	PUSH P,I
	PUSH P,Q
	PUSH P,C
	MOVSI B,-MXVBN	;FLUSH ALL BUFFERS ASSIGNED TO JOB IN U
VBDFL3:	CAMN U,VBAST(B)
	 JRST VBDFL1
VBDFL2:	AOBJN B,VBDFL3
	SKIPE NVDBA(U)
	 JRST 4,.
VBDFL6:	POP P,C
	POP P,Q
	POP P,I
	POP P,T
	POP P,B
	POPJ P,

VBDFL1:	JSP Q,11CM1
	 JRST VBDFL4	;LOST..
	PUSH P,B
	JSP Q,NVDBU2	;FLUSH BUFFER
	 JFCL		;PDP11 TIMED OUT
	 JFCL		;PDP11 GAVE NEGATIVE REPLY
	POP P,B
	JRST VBDFL2

VBDFL5:	CAMN U,VBAST(B)
VBDFL4:	 SETOM VBAST(B)
	AOBJN B,VBDFL5
	CLEARM NVDBA(U)
	JRST VBDFL6

;VIDBUF SYSTEM CALL - ASSIGN OR DEASSIGN VIDEO BUFFERS
NVDBF:	JSP Q,11CM1	;SET UP ACS, LOCK SWITCH.
	 JRST OPNL7	;LOST
	JUMPGE A,NVDBU1	;JUMP IF DEASSIGN COMMAND
	MOVSI C,2_<16.+4-18.>
	JSP Q,NVDCMD	;TELL PDP11 TO ASSIGN BUFFER.
	 JRST OPNL7	;PDP11 TIMED OUT
	 JRST OPNL6	;PDP11 GAVE NEGATIVE REPLY
	HRRZ A,C
	CAIL A,MXVBN
	 JRST OPNL6	;PDP11 HAS MORE VIDEO BUFFERS THAN WE DO
	AOS NVDBA(U)	;WON
	MOVEM U,VBAST(A)
	JRST POPJ1

NVDBU1:	MOVE B,A	;DEASSIGN VIDEO BUFFER
	CAIGE B,MXVBN	;CHECK THAT VIDEO BUFFER IS LEGAL
	 CAME U,VBAST(B) ;AND BELONGS TO THIS JOB
	  JRST OPNL33	;NOT LEGAL VIDEO BUFFER NO
	JSP Q,NVDBU2	;TELL PDP11 TO FREE THE BUFFER.
	 JRST OPNL7	;TIMED OUT
	 JRST OPNL23	;GAVE NEG REPLY
	JRST POPJ1	;WON

EBLK
;TABLE OF VIDEO BUFFER ASSIGNMENTS.
VBAST:	REPEAT MXVBN,-1	;-1 => FREE, ELSE USR IDX.
11CVSR:	-1 ? 0		;SWITCH TO LOCK TO PROTECT 10-TO-11 COMMAND VARS.
BBLK

;VIDSW SYSTEM CALL - SET VIDEO SWITCH.
NVIDSW:	JSP Q,11CM1	;LOCK SWITCH, ETC.
	 JRST OPNL7
	LSH A,4.+16.
	LSH B,4.+16.
	MOVEM A,1(I)
	MOVEM B,2(I)
	MOVSI C,1_<16.+4-18.>
	JSP Q,NVDCMD
	 JRST OPNL7	;TIMED OUT
	 JRST OPNL23	;PDP11 DIDN'T FEEL LIKE SWITCHING THE SWITCH
	JRST POPJ1

NVDBU2:	SETOM VBAST(B)	;GIVE DEASSIGN COMMAND TO 11
	LSH B,4.+16.
	MOVEM B,1(I)	;WHICH ONE TO DEASSIGN
	SOSGE NVDBA(U)
	 JRST 4,.
	MOVSI C,3_<16.+4-18.>
;GIVE COMMAND IN C TO PDP11, WAIT, RETURN (Q) IF 11 TIMES OUT,
;1(Q) IF 11 GIVES NEGATIVE REPLY AND 2(Q) IF SUCESSFUL
;IN ANY CASE, SWITCH IS UNLOCKED
;ASSUMES ARGS ALREADY SET UP, AND 11CVSR LOCKED.
NVDCMD:	MOVEM C,(I)	;STORE COMMAND-TYPE, TELLING PDP11 TO GO.
NVDCM1:	CAME C,(I)
	 JRST NVDCM2	;IT'S FINISHED, WE WIN.
	SOJG T,NVDCM1	;LOOP FOR A WHILE,
	PUSHJ P,LSWPOP
	JRST (Q)	;FAIL - 11 TIMED OUT

NVDCM2:	MOVE C,1(I)	;RETURNED ARG IN CASE OF ASSIGN VIDEO BUFFER
	LSH C,-4.-16.
	SKIPGE (I)	;PDP11 FINISHED: DID COMMAND SUCCEED?
	 AOS Q		;YES SKIP EXTRA TIME
	SETZM (I)
	PUSHJ P,LSWPOP
	JRST 1(Q)

;LOCK SWITCH AND SET UP.. RETURN TO (Q) IF FAILS (SWITCH NOT LOCKED IN THAT CASE)
;RETURN TO 1(Q) IF SUCEEDS

11CM1:	SKIPL TT11P
	 JRST (Q)
	PUSHJ P,SWTL
	 11CVSR
	LDB T,[061600,,TT11HA]
	LDB I,[061600,,400000+TTPG0*2000(T)]
	ADDI I,400000+TTPG0*2000	;I HAS 10-TO-11 COMMAND BFR ADDR.
	MOVSI T,1	;# TIMES TO LOOP WAITING FOR 11 TO ACKNOWLEDGE
11CM2:	SKIPG (I)
	 JRST 1(Q)	;11 READY FOR COMMAND.
	SOJG T,11CM2
	PUSHJ P,LSWPOP
	JRST (Q)	;TIMED OUT
]

SUBTTL	TABLET AND ROBOT CONSOLE

IFN TABP,[

ITAB:	IFE TABCLK,[
	SOSLE TABRTC
	JRST ITAB6
	SKIPGE TABUSR
	JRST ITAB7
	MOVE A,TABRTE
	MOVEM A,TABRTC
	CONO RBTCON,RTABC_3+TABCHN
]	IFN TABCLK,	CONO RBTCON,RTABC_3
	ROT A,36.*2
	DATAI RBTCON,A
	JUMPL A,ITAB	;TRY AGAIN
	LDB B,[400300,,A]
	CAME B,TABLSZ
	JRST ITAB2
	CAIN B,7
	JRST ITAB3	;PEN OUT OF CONTACT
	LDB B,[221400,,A]	;X
	SUB B,TABLSX
	MOVMS B
	CAML B,TABDTA
	JRST ITAB2
	LDB B,[1400,,A]
	SUB B,TABLSY
	MOVMS B
	CAML B,TABDTA
	JRST ITAB2
ITAB3:	AOS B,TABSMC
	CAIL B,77
	JRST ITAB2
ITAB5:	IFN TABCLK,[
	MOVE T,TABRTE
	MOVEI C,TABQBK
	SKIPL TABUSR
	JRST CLQREE
	JRST CLQRET
]	IFE TABCLK,[
	JRST OMXRET

ITAB6:	DATAI RBTCON,A	;FLUSH DATA
	JRST OMXRET

ITAB7:	CONO RBTCON,0
	JRST OMXRET
]
ITAB2:	MOVE B,TABSMC
	DPB B,[140600,,TABLST]
	SETZM TABSMC
	HRRZM A,TABLSY
	LDB B,[221400,,A]
	MOVEM B,TABLSX
	LDB B,[400300,,A]
	MOVEM B,TABLSZ
	EXCH A,TABLST
	JUMPE A,ITAB5	;JUST STARTING UP
	MOVE B,TABCC
	CAIL B,LTABBF
	JRST ITAB4	;FULL
	MOVEM A,@TABIP
	AOS TABCC
	AOS B,TABIP
	CAIL B,TABBFE
	MOVEI B,TABBUF
	MOVEM B,TABIP
	JRST ITAB5

ITAB4:	MOVSI B,(SETZ)	;SET DATA LOST
	IORM B,TABLST
	JRST ITAB5

]

IFN RBTCP,[

	;ROBOT CONSOLE READ SWITCHES
ARBTC:	UMOVE A,(J)
	CONO PI,TABOFF
	CONO RBTCON,RLTSWC_3+IFE TABCLK,TABCHN
	ROT A,36.*2
	DATAO RBTCON,A
	ROT A,36.*2
	DATAI RBTCON,A
	CONO PI,TABON
	UMOVEM A,(J)
	POPJ P,

;DATA FORM
;4.9 DATA LOST BEFORE THIS
;4.8-4.6 TAB "Z"
;4.5-4.4 USUSED
;4.3-3.1 TABLET X
;2.9-2.4 REPEAT COUNT
;2.4-1.1 TABLET Y

TABO:	PUSH P,[TABO1]
	JSP Q,STDOPN
	1,,TABUSR
	DTABUI,,DTABBI

TABO1:	POPJ P,		;OPEN LOST
	SKIPE TABUSE	;OPEN WON
	JRST POPJ1	;NOT FIRST OPEN
	LDB T,[250300,,C]
	AOS T
	MOVEM T,TABRTE	;TABLET RATE (IGNORE THIS MANY BETWEEN TAKES)
	LDB T,[300600,,C]
	MOVEM T,TABDTA	;DELTA FOR COORD TO BE DIFFERENT
	PUSHJ P,TABCLR
IFE TABCLK,	CONO RBTCON,RTABC_3+TABCHN
IFN TABCLK,[	PUSHJ P,CLQAD1	;START UP CLOCK LEVEL
	TABQBK
]	JRST POPJ1

BTABI:	UMOVE J,(C)	;INITIAL AOBJN PNT
	JSP Q,WRDBT
UTABI:	SKIPG TABCC	;TABLET .IOT
	JRST TABI1	;NO DATA AVAIL, SEE IF BEING ACCUMULATED
UTABI1:	MOVE A,@TABOP
	UMOVEM A,(C)
	AOS T,TABOP
	CAIL T,TABBFE
	MOVEI T,TABBUF
	MOVEM T,TABOP
	SOS TABCC
	POPJ P,

TABI1:	HRRZ T,(P)
	CAIN T,WRDBRT
	JRST TABI2
TABI3:	SKIPG TABCC
	PUSHJ P,UFLS
	JRST UTABI1

TABI2:	XCTR XRW,[CAMN J,(H)]
	JRST TABI3	;NO WDS TRANSFERRED
	SUB P,[1,,1]	;RETURN TO USER SOME HAVE BEEN TRANSFERRED
	POPJ P,

TABCLR:	CONO PI,TABOFF
	SETZM TABCC
	MOVEI T,TABBUF
	MOVEM T,TABIP
	MOVEM T,TABOP
	SETZM TABSMC
	SETZM TABLST
	CLEARM TABLSX
	CLEARM TABLSY
	CLEARM TABLSZ
IFE TABCLK,	SETZM TABRTC
	CONO PI,TABON
	POPJ P,
EBLK

TABBUF:	BLOCK LTABBF
TABBFE:
TABOP:	TABBUF	;OUTPUT PNTR (MP)
TABIP:	TABBUF	;INPUT PNTR (PI)
TABCC:	0	;ACTIVE WDS IN TAB BUFFER
TABSMC:	0	;COUNT OF TIMES "SAME" DATA RECEIVED FOR TAB
TABLST:	0	;LAST DATA
TABLSZ:	0	;Z OF LAST DATA
TABLSX:	0	;X
TABLSY:	0	;Y
TABDTA:	0	;DELTA FOR COORD TO BE DIFFERENT
TABRTE:	0	;TABLET RATE (TAKE EVERY N'TH ONE)
IFN TABCLK,[TABQBK:	0	;CLOCK QUEUE BLOCK
	-1
	JRST ITAB
]
IFE TABCLK,[
TABRTC:	0	;COUNT WITHIN TABRTE
]
	BBLK
]
IFN IMXP,[

SUBTTL	INPUT MULTIPLEXOR ROUTINES

IMPXO:	CONI MPX,T
	JUMPE T,OPNL25
	TRNN D,2
	JRST IMPXO2	;READ AT MN PRGM
	SKIPGE IMPXF	;IMAGE (READ AT PI)
	SETOM MPXWF
	PUSHJ P,IMXON
IMPXO2:	AOS IMXNTO
	HLL A,C
	JSP Q,OPSLD3
	IMXDN,,BIMXDN
	IMPXDN,,BIMPXD

IMXON:	AOSGE IMPXF
	JRST 4,.	;SHOULD NOT HAVE BEEN LESS THAN -1
	PUSHJ P,CLQAD1	;START CLOCK LEVEL
	MPXBLK
	POPJ P,

BIMPXS:	JSP Q,WRDBT
IMPXS:	SKIPGE MPXWF
	PUSHJ P,UFLS
	UMOVE A,(C)
	ANDI A,377
	CAILE A,LCHN
	JRST IMPXS1
AIMXW5:	IDIVI A,3
	LDB A,IMPXT(B)
	UMOVEM A,(C)
	POPJ P,

IMPXCL:	TRNE A,4	;SKIP ON WAS READING AT MP
IMXCL1:	SOS IMPXF	;WAS READING AT PI
	SOSGE IMXNTO
	DATAO DSDEVN,[MPX]
	POPJ P,

IMPXRS:	SETOM MPXWF
	POPJ P,

IMPXT:	301400,,MPXBUF(A)
	141400,,MPXBUF(A)
	1400,,MPXBUF(A)

BIMXS1:	JSP Q,WRDBT
IMPXS1:	UMOVE A,(C)
	TDZE A,[-400]
	JRST IOCER4
AIMXW4:	MOVE T,MPXWF
	CAILE A,LCHN
	MOVNI T,2	;CHNL NOT READ IN BY PI HACK
AIMXW2:	SKIPL IMXSW
	PUSHJ P,UFLS
	CAME T,MPXWF
	AOJGE T,AIMXW3	;PI FROB HAS CYCLED (MAYBE)
	AOSE IMXSW
	JRST AIMXW2	;READING IMX AT PI
	DATAO MPX,A	;SELECT CHNL
	CONO MPX,0	;START CONVERSION
	MOVEI B,62
	CONSO MPX,10
	SOJG B,.-1
	JUMPE B,IMPXFE
	XCTR XRW,[DATAI MPX,(C)]
IMX1:	SETOM IMXSW
	POPJ P,

AIMXW3:	SKIPGE MPXWF
	JRST AIMXW4	;IT WAS JUST SETOMMED
	JRST AIMXW5	;HAS BEEN READ IN BY PI HACK SO GOBBLE DATA

IMPXFE:	XCTR XRW,[SETOM (C)]
	JRST IMX1

IMPXSR:	AOSE IMXSW
	JRST MPXRT1	;USING IMX AT MN PRGM LVL
	MOVE T,[<-LCHN>/3-1,,MPXBUF-1]
	MOVEM T,MPXPTR	;SET UP BLKI POINTER
	MOVE T,[BLKI MPX,MPXPTR]
	MOVEM T,IMXLC	;SET UP DC INTERUPT LOCN
	MOVE T,[JSR MPXCYC]
	MOVEM T,IMXLC+1	;SET UP DC INT LOC+1
	DATAO MPX,[0]	;READ STARTING WITH CHNL 0
	CONO MPX,140+DCCHN
	MOVEI T,5	;12TH SEC
IFN ARMP,SKIPN ARMF
	SKIPL POTCON
	MOVEI T,2	;1/30 SEC, HAIRY POTS
	JRST .+2
MPXRT1:	MOVEI T,1	;LOST RETRY IN 1/60
	MOVEI C,MPXBLK
	SKIPL IMPXF
	JRST CLQREE	;RE-ENTER RQ
	JRST CLQRET	;RETURN

EBLK

OMXNTO:	-1
IMXNTO:	-1
IMPXF:	-1	;-1 IF MPX NOT RUNNING
MPXWF:	0
MPXBLK:	0	;CLOCK QUEUE BLOCK
	-1
	JRST IMPXSR
MPXPTR:	0
IMXST:	0
IMXSW:	-1	;-1 IF IMX AVAIL
IMXTBB:	LCHN
MPXBUF:	BLOCK <LCHN>/3+1
IMXTBE==.-1

IMXACS:	BLOCK 20
MPXCYC:	0

BBLK

	CONO MPX,0
	SETOM IMXSW
	AOS MPXWF
	MOVEM T,IMXST
	MOVE T,[JSR MPXCYC]
	MOVEM T,IMXLC
	MOVE T,IMXST
;	SKIPGE POTCON
;	SKIPE ARMF
;	JRST MPXCYG
	JRST 12,@MPXCYC

MPXCYG:	MOVEM 17,IMXACS+17
	MOVEI 17,IMXACS
	BLT 17,IMXACS+16
IFN ARMP,[
	SKIPE ARMF
	JRST SERVO
ARMSVR:]
	SKIPL POTCON
	JRST UPOTS
UPOTRT:	MOVSI 17,IMXACS
	BLT 17,17
	JRST 12,@MPXCYC
]
IFN OMXP,[

SUBTTL	OUTPUT MULTIPLEXOR ROUTINES

OMPXO:	CONI OMPX,T
	JUMPE T,OPNL25
	AOS OMXNTO
	HLL A,C
	JSP Q,OPSLD3
	OMPXDN,,BOMPXDN
	OMXDN,,BOMXDN

BOMXDS:
BOMPXS:	JSP Q,WRDBT
OMXDS:
OMPXS:	XCTR XRW,[DATAO OMPX,(C)]	;ZAP CHNL
	POPJ P,

OMPXCL:	SOSGE OMXNTO
	DATAO DSDEVN,[OMPX]
	POPJ P,

EBLK
OMXACS:	BLOCK 20

OMPXBK:	0
BBLK
	MOVEM 17,OMXACS+17
	MOVEI 17,OMXACS
	BLT 17,OMXACS+16
IFN VIDP,[
	CONSZ NVDX,700000
	CONSO NVDX,1000
	JRST .+2
	JRST NVDBRK
]
IFN ARMP,[
	CONSZ TIPDEV,10
	JRST TIPBRK
]
IFN TABP,[
	CONSZ RBTCON,7
	CONSO RBTCON,10
	JRST .+2
	JRST ITAB
]
;	MOVEI J,OMPCHN
;	JSP E,SPUR	;SPURIOUS INT
OMXRET:	MOVSI 17,OMXACS
	BLT 17,17
	JRST 12,@OMPXBK
]

SUBTTL	MORSE CODE SENDER (COD DEVICE)

IFN CODP,[
;	CODITC==CODBFL*5-1	;WHEN TO TRY TO INTERRUPT

CLKCOD:	SETOM TT	;USED AS A FLAG
	MOVE B,CODWD
	LSH B,-1
CLKCC3:	CAIN B,1
	JRST CLKCC1
	LDB A,[100,,B]
	EXCH A,LSTBIT
	CAME A,LSTBIT
	DATAO 70,LSTBIT
	MOVEM B,CODWD
	MOVE T,CODSPD
	MOVEI C,CODTIM
	JRST CLQTTR	;RE-ENTER OR NOT DEPENDING ON TT

CLKCC1:	MOVE A,CODCC
	CAIL A,CODBFL*5
	JRST CLKCC5
CLKCC8:	AOS CODCC
	ILDB B,CODPTR
	MOVE A,CODPTR
	CAMN A,[10700,,CODBUF+CODBFL-1]
	HRRI A,CODBUF-1
	HRRM A,CODPTR
	CAIL B,140
	JRST CLKCC2
	CAIGE B,40
	JRST CLKCC1
	MOVE B,CODTBL-40(B)
	JRST CLKCC3
CLKCC6:	MOVE B,CDRUB
	JRST CLKCC3
CLKCC2:	CAIN B,177
	JRST CLKCC6
	SUBI B,137
	MOVEM B,CODSPD
	JRST CLKCC1

CLKCC5:
;	CAIN A,CODITC
;	JRST CLKCC7
	MOVEI B,2
	SETZM TT	;INDICATE IDLE
	JRST CLKCC3

;CLKCC7:	MOVE A,CODUSR	;GET INDEX OF USER
;	MOVE B,CODITB	;GET CHANNELS OPEN MASK
;	AND B,MSKST2(A)	;FIND OUT WHAT CODE CHANNELS ENABLED,
;	MOVNM B,CODITD
;	AND B,CODITD
;	IORM B,IFPIR(A)	;AND REQUEST INTERRUPT ON LOWEST NUMBERED ONE
;	JRST CLKCC8	;SEND REMAINING CHARACTER(S) IN BUFFER

	;RETURN HERE FROM STDOPN
CODO1:	POPJ P,		;NON-SKIP RETURN
	AOS (P)		;DO NOT CHANGE TO JRST POPJ1 AT END
	MOVEI I,1	;INDICATE "IOPOP"

	;CODE DEVICE IOPUSH AND IOPOP ROUTINE (SETS INTERRUPT MASK)

CODIOP:	SUBI R,IOCHNM(U)	;GET "AC FIELD" (ROUTINES THAT NEED IT RESTORE R)
	MOVE A,CHNBIT(R)	;SET UP INTERRUPT BIT
	XCT CDPTBL(I)	;IORM OR ANDCAM TO CODITB
	POPJ P,

CDPTBL:	ANDCAM A,CODITB	;IOPUSH AND CLOSE
	IORM A,CODITB	;IOPOP AND OPEN

	;COD CLOSE ROUTINE
CODCLS:	MOVEI I,0	;TO SIMULATE IOPUSH
	PUSHJ P,CODIOP	;CLEAR RELEVANT INTERRUPT BIT
	CONO PI,CLKOFF	;CLOSE, USE STANDARD SEQUENCE
	SOSGE CODUSE
	SETOM CODUSR
	JRST CLKONJ

SEND:	SKIPGE C
	 SKIPA A,(C)
	  XCTR XRW,[MOVE A,(C)]	;UNIT OUTPUT
	CAIA
BSEND:	 JSP E,NBTOCH	;BLOCK OUTPUT
	ANDI A,177
	CAIN A,"S-100
	 JRST CODSSS
	SKIPE CODSSF
	 JRST CODSST
	SKIPG CODCC	;WAIT FOR ROOM
	 PUSHJ P,UFLS
	IDPB A,CODIPT
	MOVE A,CODIPT
	CAMN A,[10700,,CODBUF+CODBFL-1]
	 HRRI A,CODBUF-1
	HRRM A,CODIPT
	SOS CODCC
	MOVE T,CODSPD
	PUSHJ P,CLQADD	;START UP CLOCK LEVEL
	 CODTIM
	POPJ P,

CODO:	PUSH P,[CODO1]	;CODO1 SETS UP INTERRUPT BIT
	JSP Q,STDOPN
	1,,CODUSR
	CODDN,,CODBN


CODSSS:	SETOM CODSSF
	POPJ P,

CODSST:	SETZM CODSSF
	SUBI A,137
	JUMPLE A,CPOPJ
	MOVEM A,CODSPD
	POPJ P,

CODRS:	CONO PI,CLKOFF
	MOVEI A,CODBFL*5
	MOVEM A,CODCC
	MOVE A,CODIPT
	MOVEM A,CODPTR
	JRST CLKONJ

CODFIN:	MOVE T,CODBFL*5	;.CALL FINISH
	CAMLE T,CODCC
	 PUSHJ P,UFLS
	POPJ P,

EBLK
CODITB:	0	;MASK OF CHANNELS OPEN, 1.1=>0
CODITD:	0	;RANDOM SCRATCH LOCATION
CODWD:	2	;CURRENT WORD
CODTIM:	0	;CLOCK QUEUE BLOCK
	-1
	JRST CLKCOD
CODSPD:	3	;CODE SPEED (SET FROM CHARS >=140) WPM= 75./(CODSPD)
CODCC:	CODBFL*5
CODPTR:	440700,,CODBUF
CODIPT:	440700,,CODBUF
LSTBIT:	0	;FLUSH 60 CPS CHIRPS
CODSSF:	0

CODBUF:	BLOCK CODBFL
BBLK

DEFINE GCODE A
BIT=1
WRD=0
IRPC B,,[A]
IFSE B,-,[WRD=WRD+7*BIT
BIT=BIT_4]
IFSE B,.,[WRD=WRD+BIT
BIT=BIT_2]
IFSE B, ,[BIT=BIT_2]
TERMIN
WRD+4*BIT
TERMIN

CODTBL:	20
	GCODE -.-.-
	GCODE .-..-.
	GCODE -. .-.
	REPEAT 2,1
	GCODE . ...
	GCODE .----.
	REPEAT 2,GCODE -.--.-
	GCODE .-.-
	GCODE . ...
	GCODE --..--
	GCODE -....-
	GCODE .-.-.-
	GCODE -..-.
	IRP A,,[-----,.----,..---,...--,....-,.....,-....,--...,---..,----.]
	GCODE A
	TERMIN
	GCODE ---...
	GCODE -.-.-.
	27727	;K WITH LONG LAST DAH
	GCODE -...-
	GCODE ...-.-
	GCODE ..--..
	GCODE ...-.
	IRP A,,[.-,-...,-.-.,-..,.,..-.,--.,....,..,.---,-.-,.-..,--,-.,---,.--.,--.-
.-.,...,-,..-,...-,.--,-..-,-.--,--..]
	GCODE A
	TERMIN
	GCODE -.--.
	GCODE -.-..-..
	GCODE .-.-.
	GCODE .-...
	GCODE -.. .

CDRUB:	GCODE ........

EXPUNGE WRD,BIT

]
IFN PLTP,[

SUBTTL	CALCOMP PLOTTER ROUTINES

PLOT:	SKIPGE C
	 SKIPA A,(C)
	  UMOVE A,(C)	
PLOT1:	SKIPE A
	CAIN A,EOFCH
	POPJ P,
	SKIPG PLBFS
	JRST PLOTW1
PLOTW2:	IDPB A,PLPTR
	MOVE  A,PLPTR
	CAMN A,[600,,PLBUF+LPLBUF-1]
	HRRI A,PLBUF-1
	HRRM A,PLPTR
	SOS PLBFS
	AOSN PLON
	CONO PLT,SDS+PLTCHN
	POPJ P,

	PUSHJ P,PLOTW3
PLOTW1:	PUSHJ P,UFLS
	JRST PLOTW2

PLOTW3:	SKIPG T,PLBFS
	POPJ P,
	CAIL T,LPLBUF*5
	JRST POPJ1
	JRST POPJ3

BPLOT:	MOVEI E,PLOT1
	JRST NBTOCH

PLOTO:	MOVEI Q,PLOTOC
	PUSHJ P,STDOPN
	 POPJ P,
	AOS (P)
PLOTEL:	;TELL WHO'S USING THE PLOTTER
	BUG INFO,[PLOTTER USER],SIXBIT,UNAME(U),SIXBIT,JNAME(U)
	POPJ P,

PLOTOC:	1,,PLTUSR
	PLTDN,,PLTBN

PLTRS:	MOVEI A,LPLBUF*6
	MOVE B,PLPTR
	CONO PI,LPTOFF
	MOVEM A,PLBFS
	MOVEM B,PLIPTR
	JRST LPTONJ

PLTBRK:	MOVE A,PLBFS
	CAIN A,LPLBUF*6
	JRST PLSTP
	AOS PLBFS
	ILDB A,PLIPTR
	MOVE B,PLIPTR
	CAMN B,[600,,PLBUF+LPLBUF-1]
	HRRI B,PLBUF-1
	HRRM B,PLIPTR
	ANDI A,77
	LSH A,3
	TRO A,SDC+PDC+SD+PLTCHN
	TRNE A,PUP+PDN
	TRC A,SD#PD
	CONO PLT,(A)
	JRST LPTRT3


PLSTP:	CONO PLT,SDC+PDC
	SETOM PLON
	JRST LPTRT3

EBLK
PLBUF:	BLOCK LPLBUF
PLON:	-1
PLPTR:	440600,,PLBUF
PLIPTR:	440600,,PLBUF
PLBFS:	LPLBUF*6
IPL:	SIXBIT /IPL/	;FILE NAME 2 OF IPL JOB (USED FOR LOADING ASO JNAME OF IPL JOB)
IPLU:	0	;USER INDEX OF IPL JOB IF IT EXISTS OR -1 IF IPL BEING LOADED

;INTERPRET PLOTTER OPEN ROUTINE
BBLK
IPLO:	CONO PI,CLKOFF
	PUSHJ P,IPLST	;INITIATE LOADING OF IPL JOB
	 JRST IPLO1
	PUSHJ P,PLOTEL
	CONO PI,CLKOFF
	PUSHJ P,IPLWT	;WAIT FOR IPL JOB TO FINISH LOADING
	PUSHJ P,UFLS
	MOVE T,IPLU	;INDEX OF IPL JOB
	SKIPE IOCHNM(T)
	JRST IPLO2	;IPL JOB OPNS CLA ON CNNL 0
	MOVE T,SV40(T)
	CAME T,[.SLEEP 17,]	;IPL JOB DOES SLEEP 17, WHEN READY
IPLO2:	JRST OPNL10	;DEVICE NOT AVAILABLE
	CONO PI,CLKON
	MOVE T,IPLU
	MOVE A,UNAME(T)	;SET UP CLI OPEN
	MOVSI B,(SIXBIT /IPL/)
	PUSHJ P,CLIO
	POPJ P,
	MOVE A,UTMPTR(U)
	MOVE B,IPLU
	MOVEM A,UTMPTR(B)
	JRST POPJ1

IPLO1:	PUSHJ P,UDELAY
	JRST IPLO

IPLST:	SKIPL T,IPLU	;MAKE SURE IPL JOB NOT ALREADY BEING LOADED
	PUSHJ P,IPLIN	;MAKE SURE IPL JOB NOT ALREADY IN
	JRST POPJ1	;DONT LOAD
	HRROI T,IPL
	CONO PI,UTCOFF
	PUSHJ P,NUJBST
	JRST UTCONJ
	JRST UTCOJ1

;ROUTINE TO SEE IF IPL JOB WINS. CLKOFF OR CLKBRK IN PROGRESS
IPLWT:	SKIPGE T,IPLU	;CHECK IPL JOB LOADING FLAG
	POPJ P,	;IPL JOB STILL LOADING WAIT
	PUSHJ P,IPLIN	;SEE IF IPL JOB IN CORE
	SKIPE IOCHNM(T)	;SEE IF IPL JOB IN USE
	JRST POPJ1	;LOSE
	MOVE T,SV40(T)	;SEE IF IPL JOB READY
	CAMN T,[.SLEEP 17,]	;IPL JOB DOES .SLEEP 17, WHEN READY
	AOS (P)	;READY WIN
	POPJ P,	;NOT READY YET, WAIT

IPLIN:	PUSH P,U	;ROUTINE TO SEE IF IPL JOB IN. C(T)=C(IPLU), CLKOFF OR CLKBRK IN PROGRESS
	CAML T,USRHI
	JRST IPLIN1	;USER VARIABLES SLOT FLUSHED
	HLRO U,UNAME(T)
	AOJN U,IPLIN1	;UNAME OF IPL JOB IS -1 (IN LH)
	MOVSI U,(SIXBIT /IPL/)
	CAME U,JNAME(T)
IPLIN1:	AOS -1(P)	;IPL JOB NOT IN, SO SKIP
	POP P,U
	POPJ P,

]
;UTC MUST BE OFF!
NUJBST:	MOVEI TT,MXCZS	;ENTRY FROM ELSEWHERE TO DO SIMILAR THINGS
	SKIPL DEDTIM	;SKIP IF SYS DEAD
	CAMG TT,UTTYCT
	 POPJ P,	;RING BUFFER FULL, WAIT
	MOVEM T,@UTTYI
	AOS UTTYCT
	AOS TT,UTTYI
	CAIL TT,UTTYS+MXCZS
	MOVEI TT,UTTYS
	MOVEM TT,UTTYI
	SETOM 1(T)	;SET JOB LOADING FLAG
	JRST POPJ1

SUBTTL	PDP-6 AS INFERIOR

IFN PDP6P,[
;.IPDP CH,
;INTERRUPT THE PDP6, I/O CH SHOULD HAVE PDP6 OPEN AS INFERIOR
;THIS CALL DOESNT SKIP OR INTERRUPT TO THE USER

AIPDP:	MOVSI T,%CLS6
	SKIPGE A,(R)
	 TDNN T,CLSTB(A)
	  POPJ P,
	CONO PI,LPTOFF
	CONI DSDEV,T
	ANDI T,7
	CONO DSDEV,20(T)
	JRST LPTONJ

;PDP-6 DEVICE
;INTERRUPT FROM PDP6, MAYBE GENERATE IO CHANNEL USER INTERRUPT

DSDBRK:	CONO DSDEV,10	;TURN OFF FLAG
			;ALSO KEEP IT FROM INTERRUPTING FOR A WHILE
			;GETS TURNED ON AT DISMISS, SETOM PICLR AND SLOW CLK
	SKIPGE A,PDPISR
	 JRST LPTRT3	;NO ONE WANTS AN INT
	MOVE B,PDPMSK
	AND B,MSKST2(A)
	MOVNM B,PDPTMP
	AND B,PDPTMP
	IORM B,IFPIR(A)	;GENERATE USER INT
	JRST LPTRT3

		;PDP6 .OPEN ROUTINE (GET HERE FROM USR OPEN)

PDPO:	SKIPL PDP6UP	;MAKE IT EASY TO MARK PDP6 AS DOWN.
	 JRST OPNL10
	TLNN C,20
	 JRST PDPO1	;DON'T TRY TO ENABLE INTERRUPTS
	TLO C,400000	;TRY TO ENABLE INTERRUPTS
	SKIPL PDPISR
	 CAMN U,PDPISR
	  JRST PDPO1
	JRST OPNL12

PDPO1:	PUSH P,U
	PUSHJ P,PDPO2
	 JRST POPUJ
	AOS -1(P)
	POP P,U
	SKIPL (R)
	 JRST .+3
	  AOS PDPISE
	  MOVEM U,PDPISR;GOT IT AS INFERIOR
	PUSHJ P,LSWPOP	;RELEASE TREESW
	MOVEI I,1	;DO "IOPOP" INTO CHNL

PDPIOP:	SKIPL (R)
	 POPJ P,	;NOT OPEN TO INTERRUPT
	SUBI R,IOCHNM(U)
	MOVE A,CHNBIT(R)
	ADDI R,IOCHNM(U)	;RESTORE R
	XCT PDPIOT(I)
	POPJ P,

PDPIOT:	ANDCAM A,PDPMSK
	IORM A,PDPMSK

PDPCLS:	MOVEI I,0	;CLOSE
	PUSHJ P,PDPIOP	;=> DO "IOPUSH"
PDPCL1:	CONO PI,CLKOFF
	SOSGE PDPUSE
	 SETOM PDPUSR	;LAST CHANNEL CLOSED
	SKIPL (R)
	 JRST CLKONJ	;NOT INTERRUPT ENABLED THIS CHNL
	SOSGE PDPISE
	 SETOM PDPISR	;LAST CHANNEL CLOSED
	JRST CLKONJ


PDPO3:	MOVEI U,(A)	;MAKE U INDEX OF SUPERIOR
PDPO2:	SKIPL A,SUPPRO(U)	;GET POINTER TO SUPERIOR, SKIP IF TOP LEVEL
	 JRST PDPO3	;TRY AGAIN
	HLRZ D,C
	TLO C,200000	;SIGNAL PDP6 TO UBI ETC
	JSP Q,STDOPN
	 3,,PDPUSR
	 PDPUII,,PDPUIO
	 PDPBII,,PDPBIO

;.UCLOSE PDP6
PDPUCL:	MOVSI C,%CLS6	;GET PDP6 CLSTB BIT
	MOVEI R,IOCHNM(U)	;GET PNTR TO USER'S IO CHNL AREA
	HRLI R,-20	;MAKE AOBJN PNTR TO ALL CHNLS
	PUSHJ P,PDPCCK	;CHECK CHNL
	 PUSHJ P,PDPCZP	;CLOSE CHNL IF PDP6 OPEN ON IT
	AOBJN R,.-2	;REPEAT FOR ALL CHNLS
	HRLI R,-LUIOP/2	;MAKE AOBJN PNTR TO IO PDL (ADR ALREADY THERE)
	PUSHJ P,PDPCCK	;CHECK ENTRY FOR PDP6
	 PUSHJ P,PDPPZP	;CLEAR ENTRY IF PDP6 CHNL
	AOJ R,		;INCR TO SKIP IOCHST ENTRY
	AOBJN R,.-3	;REPEAT FOR WHOLE IO PDL
	POPJ P,		;ALL PDP6 CHNLS THIS JOB SHOULD NOW BE CLOSED

PDPCCK:	HRRZ B,(R)	;GET CLSTB INDEX FROM IOCHNM
	TDNN C,CLSTB(B)	;IF NOT PDP6 CHNL
	 AOS (P)	;SKIP CLOSING CHNL
	POPJ P,

;CLOSE PDP6 USER CHNL
PDPCZP:	PUSHJ P,PDPCLS	;ADJUST PDP6 VARS
	JRST STDCLX	;CLOBBER CHNL & RETURN

;CLEAR PDP6 IO PDL ENTRY
PDPPZP:	PUSHJ P,PDPCL1	;ADJUST PDP6 VARS (EXCEPT PDPMSK)
	SETZB A,(R)	;CLEAR SAVED IOCHNM
	DPB A,[4000,,1(R)]	;CLEAR SAVED IOCHST (SAVING CHNL # FOR .IOPDL)
	POPJ P,
PDPCLR:	OFFSET 20-.	;ROUTINE TO BE MOVED
	CONO PI,11577	;CLEAR PI
	CONO 655550	;CLEAR PROCESSOR
	DATAO 20,.+1	;RELEASE DEVICES
	SETZ		;CLEAR 0, ALSO BIT FOR DEASSIGNMENT (^)
	HRRZI 1,1	;BLT POINTER 0,,1
	BLT 1,41		;CLEAR AC'S, SELF, AND 41
PDPCLE==.
	OFFSET 0	;BACK TO NORMAL
EBLK

PDPTMP:	0	;TEMP FOR INT
PDPMSK:	0	;MASK OF INT BITS OF CHNLS OPEN

BBLK

PDPRST:	PUSHJ P,MP6LD
	MOVE C,[400020,,400021]
	XCTR XW,[CLEARM 400020]
	XCTR XBRW,[BLT C,437777]
	MOVE C,[PDPCLR,,400020]
	XCTR XBW,[BLT C,400000+PDPCLE-1]
	MOVE C,[JRST 20]
	UMOVEM C,400041
	PUSHJ P,UDELAY
	UMOVE A,400041
	JUMPE A,PDPRS2	;JUMP ON ROUTINE WON, DON'T HAVE TO CLEAR IT OUT
	XCTR XW,[CLEARM 400020]
	MOVE C,[400020,,400021]
	XCTR XBRW,[BLT C,400041]
PDPRS2:	JRST MPLDZ
];IFN PDP6P

SUBTTL	DL10MP SYSTEM CALL

IFN DL10P,[
;.CALL DL10MP
;  ARG 1 - PAGE #
;CREATES A READ/WRITE, UNENCACHED, ABSOLUTE PAGE AT THAT POINT
;IN THE USER'S MAP.  THE PAGE CONTAINS THE DL10 CONTROL AREA.
;  VAL 1 - AOBJN POINTER TO DL10 CONTROL AREA WITHIN PAGE
;  VAL 2 - POINTER TO 3 WORDS USED FOR PDP11 EXAMINE/DEPOSIT COMMANDS
;  VAL 3 - POINTER TO FIRST FREE WORD WITHIN DL10 CONTROL AREA

DL10MP:	TDNE A,[-400]
	 JRST OPNL33		;PAGE NUMBER TOO BIG
	PUSHJ P,UPLC
	LDB C,Q			;GET CIRC POINTER
	CAIE C,-1
	 JUMPN C,OPNL13		;PAGE SLOT ALREADY IN USE
	BUG INFO,[DL10MP BY],SIXBIT,UNAME(U),SIXBIT,JNAME(U)
	MOVEI C,600000+<DL10AR_-10.>	;SUITABLE PTW
	DPB C,T
	MOVEI C,-1		;ABS CIRC PNTR
	DPB C,Q
	CLRPGM (U)
	LSH A,10.		;USER'S VIRTUAL ADDRESS
	ADDI A,2000
	CAMLE A,HUSRAD(U)
	 MOVEM A,HUSRAD(U)
	SUBI A,2000-<DL10AR&1777>	;VIR ADDR OF START OF DL10 CONTROL AREA
	HRLI A,-100		;SIZE OF DL10 CONTROL AREA
	MOVEI B,DL10XD-DL10AR(A)
	MOVEI C,DLXEND-DL10AR(A)
	JRST POPJ1
];DL10P

SUBTTL	TRAP DEVICE

;.CALL TRPOPN
;  ARG 1 - <JOB> SPEC
;  ARG 2 - CHANNEL NUMBER IN THAT JOB TO BE OPENED AS TRAP DEVICE
;  ARG 3 - IOCHNM WORD (ONLY LH IS USED)
;  ARG 4 - IOCHST WORD
;THIS GIVES THE INFERIOR A TRAP DEVICE CHANNEL, WHICH GIVES
;A %PITRP INTERRUPT ON MOST OPERATIONS.  THE INFERIOR SIGNALS
;ITS DESIRE TO GET SUCH A CHANNEL BY OPENING THE TRAP DEVICE,
;WHICH ALSO GIVES A %PITRP INTERRUPT.
;TRPOPN IS ALSO USEFUL FOR STORING ERROR CODES INTO IOCHST OF AN ALREADY-OPEN TRAP CHANNEL

TRPOPN:	TDNE B,[-NIOCHN]
	 JRST OPNL14		;BAD CHANNEL NUMBER
	MOVE J,A
	JSP T,NCORUI
	 JFCL
IFN PDP6P,[
	CAIN J,-1
	 JRST OPNL34
];PDP6P
	JSP T,NCORWR
	 JRST OPNL31		;CAN'T MODIFY JOB
	ADDI B,IOCHNM(J)	;GET IOCHNM WORD ADDRESS
	HRRZ T,(B)
	CAIE T,DNTRAP
	 JUMPN T,OPNL13		;FILE ALREADY EXISTS (YOU MUST CLOSE THE CHANNEL FIRST)
	HRRI C,DNTRAP
	MOVEM C,(B)
	MOVEM D,IOCHST-IOCHNM(B)
	JRST LSWPJ1		;UNLOCK DIELOK, TAKE SUCCESS RETURN
