$auto$=-2
.enabl lc
;15-aug-80 for Conroy C runtime
        .PSECT EDI07
        .TITLE EDI07
        .IDENT /V1/
        mutask=1 
        .enabl lc
        .mcall wtse$,mrkt$,dir$,callr
         
         
         
         
        .MACRO return
        rts pc
        .ENDM 
         
        .MACRO push this
        mov this,-(sp)
        .ENDM 
         
        .MACRO pop that
        mov (sp)+,that
        .ENDM 
         
        .if df mutask
        .MACRO pure
        .psect pur,ro
        .ENDM 
         
        .MACRO impure
        .psect ipur,rw
        .ENDM 
         
        .iff 
         
        .MACRO pure
        .ENDM 
        .MACRO impure
        .ENDM 
         
        .endc 
         
         
         
         
        .globl cursr2
        .globl ttynll
        .globl ceol
        .globl fixscn
        .globl bcom		;entry for	b command
        .globl bcom1		;alt entry for b command
        .globl smode		;variable	to determine screen/line mode
        .globl prescn
        .globl postscn
        .globl bottom		;the line # of	the bottom line
        .globl fpage
        .globl fdot
        .globl downscroll
        .globl upscroll
        .globl get.line
        .globl upit
        .globl downit
        .globl cursl		;the line the cursor is on
         
         
smode:  .word	0		;0= not screen	mode,	<>0 = in screen	mode
         
         
        esc	=	33
        sdel	=	123
        home	=	110
        bottom	=	24.
        insert	=	114
        delete	=	115
        ceol	=	113
        ceop	=	'J
        rightmost =	80.
        screen	=	1
        line	=	0
        blank	=	40
         
         
olddot: .word	0		;previous dot value
I:      .word	0		;local	index
K:      .word	0		;local	index
t:      .word   0               ;temp flag variable
flag1:  .word	0		;used by dot	checker
cursl:  .word	13.
cursc:  .word	1.
fpage:  .word	'@
fdot:   .word	'H
nocmd:  .word	0		;0= bypass the	regular	ued command
smacro: .word	0		;the a	and i commands	may be s	macros
        multadr:.word	0		;set to 0 if a	multi address cmd in process
         
         
mark:   mrkt$	30.,4,1
wait:   wtse$	30.
         
         
        pure 
         
         
         
         
         
         
prescn:  
        clr nocmd
	JSR PC,fixscn
	CMP smode,#screen		;IS smode = #screen
	BNE E7.1	;Not if NE
            mov	r0,-(sp)
	JSR PC,@pretbl(r0)
            mov	(sp)+,r0
	TST nocmd		;IS nocmd NE #0
	BEQ E7.4	;Not if EQ
                add #10,(sp)		;bump return past	the subroutine	call
E7.4=.			; 
E7.6=.
E7.1=.			; 
E7.3=.
        return 
postscn: 
	CMP smode,#screen		;IS smode = #screen
	BNE E7.7	;Not if NE
            mov	r0,-(sp)
	JSR PC,@psttbl(r0)
        MOV #bottom-2,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
JSR PC,TTYNLL
            mov	(sp)+,r0
E7.7=.			; 
E7.9=.
        return 
         
         
pretbl: .WORD	Apre		;APPEND
        .WORD	no.op		;B
        .WORD	Cpre		;CHANGE
        .WORD	Dpre		;DELETE
        .WORD	Epre		;EDIT
        .WORD	Fpre		;FACTS
        .WORD	Gpre		;GLOBAL
        .WORD	ERR2		;H
        .WORD	Ipre		;INSERT
        .WORD	Jpre		;JOIN
        .WORD	Kpre		;KOPY
        .WORD	no.op		;L
        .WORD	Mpre		;MOVE
        .WORD   no.op           ;N
        .WORD	Opre		;OPTIONS
        .WORD	Ppre		;PRINT
        .WORD	Qpre		;QUIT
        .WORD	Rpre		;READ
        .WORD	Spre		;SUBSTITUTE
        .WORD	no.op		;T
        .WORD	upre		;U
        .WORD	Vpre		;EXCLUDE
        .WORD	Wpre		;WRITE
        .WORD	Xpre		;WRITE WITH	NEW VERSION
        .WORD	NO.OP		;Y
        .WORD	Zpre		;Z COMMAND			
         
         
         
         
         
psttbl: .WORD	Apst		;APPEND
        .WORD	no.op		;B
        .WORD	Cpst		;CHANGE
        .WORD	Dpst		;DELETE
        .WORD	Epst		;EDIT
        .WORD	Fpst		;FACTS
        .WORD	Gpst		;GLOBAL
        .WORD	ERR2		;H
        .WORD	Ipst		;INSERT
        .WORD	Jpst		;JOIN
        .WORD	Kpst		;KOPY
        .WORD	no.op		;L
        .WORD	Mpst		;MOVE
        .WORD   no.op           ;N
        .WORD	Opst		;OPTIONS
        .WORD	Ppst		;PRINT
        .WORD	Qpst		;QUIT
        .WORD	Rpst		;READ
        .WORD	Spst		;SUBSTITUTE
        .WORD	no.op		;T
        .WORD	upst		;U
        .WORD	Vpst		;EXCLUDE
        .WORD	Wpst		;WRITE
        .WORD	Xpst		;WRITE WITH	NEW VERSION
        .WORD	NO.OP		;Y
        .WORD	Zpst		;Z COMMAND			
         
         
         
Apre:   				;APPEND 
        mov cursl,r1			;if its not an	s	macro, clear bottom
        br app1
        Ipre=.				;INSERT
        mov cursl,r1
        dec r1
app1:   clr smacro			;assume not a macro
	JSR PC,getc
        mov r0,peek			;and put it back
	CMP r0,#nl		;IS r0 NE #nl
	BEQ E7.10	;Not if EQ
            inc	smacro			;its	a	macro
	JSR PC,cmultadr
	JMP E7.13
E7.10=.			; 
            dec	r1
        MOV R1,-(SP)
        MOV #rightmost,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #sdel,R0
        CALL TTYCC
MOV #esc,R0
        CALL TTYCC
MOV #sdel,R0
        CALL TTYCC
MOV #esc,R0
        CALL TTYCC
MOV #ceop,R0
        CALL TTYCC
            inc	r1
            inc	r1
        MOV R1,-(SP)
        MOV #rightmost,-(SP)
        CALL CURSR2
JSR PC,TTYNLL
E7.13=.			; 
E7.12=.
        return 
Cpre:   				;CHANGE 
	TST oe		;IS oe NE #0
	BEQ E7.14	;Not if EQ
            jmp	spre1			;and its really	an s command
E7.14=.			; 
E7.16=.
cpre1:   
         
Dpre:   				;DELETE 
        return 
Epre:   				;EDIT 
        clr smode
        return 
Fpre:   				;FACTS 
        return 
Vpre:   				;EXCLUDE 
Gpre:   				;GLOBAL 
        clr smode			;out of screen mode
        mov #1,onum			;turn on line numbers
        return 
Jpre:   				;JOIN 
Kpre:   				;KOPY 
Mpre:   				;MOVE 
Opre:   				;OPTIONS 
        return 
         
         
         
         
Ppre:   				;PRINT 
	CMP adr1,adr2		;IS adr1 NE adr2
	BEQ E7.17	;Not if EQ
            clr	smode			;out	of	screen mode and	print
	JMP E7.20
E7.17=.			; 
            inc	nocmd			;we do it all
	JSR PC,nonzer
	MOV adr1,R0
	SUB dot,R0
	TST r0		;IS r0 LT #0
	BGE E7.21	;Not if GE
                neg r0
E7.21=.			; 
E7.23=.
            mov	dot,olddot		;save it
            mov	adr1,dot		;set new dot
	CMP r0,#24.		;IS r0 GT #24.
	BLE E7.24	;Not if LE
	JSR PC,bcom1
	JMP E7.27
E7.24=.			; 
	CMP adr1,olddot		;IS adr1 = olddot
	BNE E7.28	;Not if NE
	JSR PC,redisp
	JMP E7.31
E7.28=.			; 
	CMP adr1,olddot		;IS adr1 HI olddot
	BLOS E7.32	;Not if LOS
	JSR PC,upit
	JMP E7.35
E7.32=.			; 
	JSR PC,downit
E7.35=.			; 
E7.34=.
E7.31=.			; 
E7.30=.
E7.27=.			; 
E7.26=.
E7.20=.			; 
E7.19=.
        return 
         
         
         
upit:   				;scroll up until dot reaches adr1
        mov olddot,dot
	JSR PC,dist
	MOV #1,I	;Init loop index
E7.36=.
	CMP I,#24.	;done loop?
	BGT E7.37	;yes if GT
            add	#2,dot
	TST lmode		;IS lmode = #0
	BNE E7.38	;Not if NE
	CMP t,#2		;IS t GT #2
	BLE E7.41	;Not if LE
	BR E7.44	;Here IF t GT #2
E7.41=.
	BR E7.38		;Here if not
E7.44=.
                dir$ #mark          ;display is too slow
                dir$ #wait          ;must   give it some time
E7.38=.			; 
E7.40=.
	JSR PC,upscroll
	CMP dot,adr1		;IS dot HIS adr1
	BLO E7.45	;Not if LO
	JMP E7.37         ;break
E7.45=.			; 
E7.47=.
	INC I
	JMP E7.36		;back to top
E7.37=.
        return 
downit: 				;scroll down until dot gets	to adr1
        mov olddot,dot			;need this again
	JSR PC,dist
	MOV #1,I	;Init loop index
E7.48=.
	CMP I,#24.	;done loop?
	BGT E7.49	;yes if GT
            sub	#2,dot			;back	a line
	TST lmode		;IS lmode = #0
	BNE E7.50	;Not if NE
	CMP t,#2		;IS t GT #2
	BLE E7.53	;Not if LE
	BR E7.56	;Here IF t GT #2
E7.53=.
	BR E7.50		;Here if not
E7.56=.
                dir$ #mark          ;display is too slow
                dir$ #wait          ;must   give it some time
E7.50=.			; 
E7.52=.
	JSR PC,downscroll
	CMP dot,adr1		;IS dot LOS adr1
	BHI E7.57	;Not if HI
	JMP E7.49         ;break
E7.57=.			; 
E7.59=.
	INC I
	JMP E7.48		;back to top
E7.49=.
        return 
         
         
dist:    
        mov dot,t                       ;check if only moving 1 line
        sub adr1,t                      ;in which case adr1 differs from dot
	TST t		;IS t LT #0
	BGE E7.60	;Not if GE
            neg t                        ;or -2
E7.60=.			; 
E7.62=.
        rts pc
         
         
Qpre:   				;QUIT 
Rpre:   				;READ 
        return 
Spre:   				;SUBSTITUTE 
	TST oe		;IS oe NE #0
	BEQ E7.63	;Not if EQ
            jmp	cpre1
E7.63=.			; 
E7.65=.
spre1:   
	JSR PC,cmultadr
        return 
upre:   				;U 
Wpre:   				;WRITE 
Xpre:   				;WRITE WITH	NEW VERSION
        return 
Zpre:   				;Z COMMAND			
        clr smode
no.op:  return 
         
         
         
Apst:   				;APPEND 
Ipst:   				;INSERT 
	TST smacro		;IS smacro = #0
	BNE E7.66	;Not if NE
            mov	dot,adr1		;it requires this
            mov	dot,adr2		;and this
	JSR PC,bcom1
	JMP E7.69
E7.66=.			; 
            jmp	spst1
E7.69=.			; 
E7.68=.
        return 
Cpst:   				;CHANGE 
	TST oe		;IS oe NE #0
	BEQ E7.70	;Not if EQ
            jmp	spst1
E7.70=.			; 
E7.72=.
cpst1:   
        return 
Dpst:   				;DELETE 
        mov dot,adr1			;it requires this
        mov dot,adr2			;and this
	JSR PC,bcom1
        return 
Epst:   				;EDIT 
        return 
Fpst:   				;FACTS 
        callr	sfacts			;in screen mode
Gpst:   				;GLOBAL 
        return 
Jpst:   				;JOIN 
        return 
Kpst:   				;KOPY 
Mpst:   				;MOVE 
        jmp dpst			;new	display	needed
Opst:   				;OPTIONS 
Ppst:   				;PRINT 
Qpst:   				;QUIT 
        return 
Rpst:   				;READ 
        jmp dpst
        return 
Spst:   				;SUBSTITUTE 
	TST oe		;IS oe NE #0
	BEQ E7.73	;Not if EQ
            jmp	spst1
E7.73=.			; 
E7.75=.
spst1:  				;This is the sub entry
	TST multadr		;IS multadr NE #0
	BEQ E7.76	;Not if EQ
            jmp	dpst			;new display
	JMP E7.79
E7.76=.			; 
	JSR PC,redisp
	JSR PC,getc
            mov	r0,peek			;put it back
            bic	#40,r0			;make it upper	case
	CMP r0,#'P		;IS r0 = #'P
	BNE E7.80	;Not if NE
                mov #-1,peek		;clear	peek char
E7.80=.			; 
E7.82=.
E7.79=.			; 
E7.78=.
        return 
         
upst:   				;U 
	JSR PC,redisp
Vpst:   				;EXCLUDE 
Wpst:   				;WRITE 
Xpst:   				;WRITE WITH	NEW VERSION
Zpst:   				;Z COMMAND			
        return 
         
         
fixscn:  
	CMP smode,#screen		;IS smode = #screen
	BNE E7.83	;Not if NE
	MOV R0,-(SP)
MOV #esc,R0
        CALL TTYCC
MOV #'Y,R0
        CALL TTYCC
MOV #'6,R0
        CALL TTYCC
MOV #blank,R0
        CALL TTYCC
MOV #esc,R0
        CALL TTYCC
MOV #ceop,R0
        CALL TTYCC
JSR PC,TTYNLL
	MOV (SP)+,R0
E7.83=.			; 
E7.85=.
        return 
         
         
         
bcom:   				;The b command does	the BEGIN SCREEN mode
	MOV R1,-(SP)
	MOV R0,-(SP)
        MOV #1,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #'G,R0
        CALL TTYCC
        mov #2,r1
	MOV #1,I	;Init loop index
E7.86=.
	CMP I,#17.	;done loop?
	BGT E7.87	;yes if GT
	CMP I,#9.		;IS I GT #9.
	BLE E7.88	;Not if LE
                inc r1
	JMP E7.91
E7.88=.			; 
                add #8.,r1
E7.91=.			; 
E7.90=.
        MOV #1,-(SP)
        MOV R1,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #'F,R0
        CALL TTYCC
	INC I
	JMP E7.86		;back to top
E7.87=.
JSR PC,TTYNLL
	MOV (SP)+,R0
	MOV (SP)+,R1
	JSR PC,setfld
bcom1:
	MOV R0,-(SP)
	MOV R2,-(SP)
        mov #screen,smode		;into screen	mode
	TST adr2		;IS adr2 NE #0
	BEQ E7.92	;Not if EQ
            mov	adr2,dot		;set dot
E7.92=.			; 
E7.94=.
	MOV dot,r2
	SUB #24.,r2
MOV #esc,R0
        CALL TTYCC
MOV #'j,R0
        CALL TTYCC
JSR PC,TTYNLL
	MOV #1,I	;Init loop index
E7.95=.
	CMP I,#22.	;done loop?
	BGT E7.96	;yes if GT
        MOV I,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
	CMP i,cursl		;IS i = cursl
	BNE E7.97	;Not if NE
                mov fdot,r0
	JMP E7.100
E7.97=.			; 
                mov fpage,r0
E7.100=.			; 
E7.99=.
        CALL TTYCC
JSR PC,TTYNLL
	JSR PC,get.line
            mov	#lbuf1,r0		;its	put into lbuf1	null terminated
	JSR PC,ttystr
            add	#2,r2			;go to next location
	INC I
	JMP E7.95		;back to top
E7.96=.
	MOV (SP)+,R2
	MOV (SP)+,R0
        RTS PC
         
         
         
         
err2:   mov #2,r1
        jmp error
         
         
         
         
ttynll: jmp ttynl
         
         
         
get.line: 				;get line	but check	range	of dot
	MOV R0,-(SP)
        clr flag1		;assume ok
	CMP r2,zero		;IS r2 LOS zero
	BHI E7.101	;Not if HI
            inc	flag1
E7.101=.			; 
E7.103=.
         
	CMP r2,dol		;IS r2 HI dol
	BLOS E7.104	;Not if LOS
            inc	flag1
E7.104=.			; 
E7.106=.
         
	TST flag1		;IS flag1 = #0
	BNE E7.107	;Not if NE
            mov	(r2),r0		;contents of	pointer	to line
	JSR PC,getlin
             
	JMP E7.110
E7.107=.			; 
            movb #' ,lbuf1	;simulate a blank line
            clrb lbuf1+1	;and	null terminate it
E7.110=.			; 
E7.109=.
	MOV (SP)+,R0
        return 
upscroll:  
	MOV dot,R2
	ADD #18.,R2
	JSR PC,get.line
        MOV #BOTTOM-1,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
MOV #ESC,R0
        CALL TTYCC
MOV #CEOL,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FPAGE,r0
	JSR PC,ttycc
	MOV CURSL,R1
	DEC R1
        MOV R1,-(SP)
        MOV #RIGHTMOST,-(SP)
        CALL CURSR2
MOV #ESC,R0
        CALL TTYCC
MOV #SDEL,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FPAGE,r0
	JSR PC,ttycc
MOV #ESC,R0
        CALL TTYCC
MOV #SDEL,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FDOT,r0
	JSR PC,ttycc
MOV #ESC,R0
        CALL TTYCC
MOV #HOME,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #DELETE,R0
        CALL TTYCC
        MOV #BOTTOM-2,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
JSR PC,TTYNLL
        mov #lbuf1,r0
	JSR PC,ttystr
        MOV #bottom-1,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
JSR PC,TTYNLL
        return 
Downscroll:  
	MOV dot,R2
	SUB #24.,R2
	JSR PC,get.line
	MOV cursl,R1
	SUB #2,R1
        MOV R1,-(SP)
        MOV #rightmost,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #sdel,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FDOT,r0
	JSR PC,ttycc
MOV #esc,R0
        CALL TTYCC
MOV #sdel,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FPAGE,r0
	JSR PC,ttycc
MOV #esc,R0
        CALL TTYCC
MOV #home,R0
        CALL TTYCC
MOV #esc,R0
        CALL TTYCC
MOV #insert,R0
        CALL TTYCC
MOV #ESC,R0
        CALL TTYCC
MOV #'R,R0
        CALL TTYCC
mov FPAGE,r0
	JSR PC,ttycc
JSR PC,TTYNLL
        mov #lbuf1,r0			;address of line to go	on top
	JSR PC,ttystr
        MOV #bottom-2,-(SP)
        MOV #rightmost,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #sdel,R0
        CALL TTYCC
MOV #esc,R0
        CALL TTYCC
MOV #ceop,R0
        CALL TTYCC
JSR PC,TTYNLL
        return 
         
         
redisp:  
        push r0
        push r2
        MOV cursl,-(SP)
        MOV #1,-(SP)
        CALL CURSR2
MOV #esc,R0
        CALL TTYCC
MOV #ceol,R0
        CALL TTYCC
JSR PC,TTYNLL
        mov dot,r2
	JSR PC,get.line
        mov #lbuf1,r0
	JSR PC,ttystr
        pop r2
        pop r0
        return 
         
         
setfld:  
	MOV R0,-(SP)
	JSR PC,getcx
	CMP R0,#nl		;IS R0 NE #nl
	BEQ E7.111	;Not if EQ
            mov	r0,fpage		;and set page	function
E7.111=.			; 
E7.113=.
         
	JSR PC,getcx
	CMP R0,#nl		;IS R0 NE #nl
	BEQ E7.114	;Not if EQ
            mov	r0,fdot			;set	dot function
E7.114=.			; 
E7.116=.
	MOV (SP)+,R0
        return 
         
         
         
getcx:  				;get char, check for newline, correct case
	JSR PC,getc
	CMP R0,#nl		;IS R0 NE #nl
	BEQ E7.117	;Not if EQ
	CMP R0,#40		;IS R0 = #40
	BNE E7.120	;Not if NE
                mov #'@,r0		;and make it an '@'
E7.120=.			; 
E7.122=.
            bic	#60,r0			;map to upper	and clear protect bit
	JMP E7.123
E7.117=.			; 
            mov	r0,peek			;put	it back
E7.123=.			; 
E7.119=.
        return 
         
         
         
cmultadr: 				;check for multiple	addresses
        clr multadr			;assume single address
	CMP adr1,adr2		;IS adr1 NE adr2
	BEQ E7.124	;Not if EQ
            inc	multadr			;note	fact
E7.124=.			; 
E7.126=.
        return 
         
         
cursr2:  
MOV #esc,R0
        CALL TTYCC
MOV #'Y,R0
        CALL TTYCC
        mov (sp),r0                     ; get ret address
        mov 4(sp),(sp)                  ; replace tos with x cord
        mov r0,4(sp)                    ; replace 3os with return address
        pop r0                          ; pop x into r0
        add #31.,r0                     ; convert to teleray cord's
	JSR PC,ttycc
        pop r0                          ; pop y into r0
        add #31.,r0                     ; convert
	JSR PC,ttycc
        rts pc
         
        .end 
