	.title	rsxovr	reload overlays for rsx ted
	.enabl	gbl
;
;
;	Copyright (C) 1978 1979 1980 1981 1982 1983   Change Software, Inc.
;	
;	
;	This software is furnished under a license and may
;	be  used  and  copied  only in accordance with the
;	terms of such license and with  the  inclusion  of
;	the  above copyright notice.  This software or any
;	other copies thereof may not be provided or other-
;	wise made available to any other person.  No title
;	to and ownership of the software is hereby  trans-
;	ferred.
;	
;	The information in this  software  is  subject  to
;	change  without notice and should not be construed
;	as a commitment by the author.
;
;	

	.psect	rsxovr	,ro,i,lcl,rel,con
;
;	note: must be in the root


	.macro	global	list
	.globl	list
	.endm	global


	global	<$$$wrk>		; start of the lowcore r/w
					; asect, defined in EDEFS.MAC
	.if ne	,resovr	
	.ift
sts	=	regsav
debug	=	0

	.psect	rsxovr
;	parameter blocks for resident overlay mapping (readonly)


l.main:	.byte	4,0			; main code, command dispatching
	.rad50	/$MAIN/			; also all commands other than
	.word	sts			; vtedit

l.vted:	.byte	4,0			; reslib overlay for screen edit
	.rad50	/$VTEDI/
	.word	sts

l.sche:	.byte	4,0			; to remap to vtedit help
	.rad50	/$MAIN/
	.word	sts

l.edsc:	.byte	4,0			; same as l.vted
	.rad50	/$VTEDI/
	.word	sts

	.iff

l.main:
l.vted:
l.sche:
l.edsc:	.word	0

	.endc


	.macro	load	name
	.if ne	,resovr
	.ift
	mov	name	,r0
	call	ovfind
	bcc	10$
	call	overr
10$:
	.endc
	.endm


	.sbttl	remap (for reslib) or reload overlay

;	These entry points  are placed  in the  root so  that
;	overlays that reside in the same region but differing
;	segments  can call  each other and then remap back to
;	the caller, as in:
;
;	.name	cotre1
;	.fctr	cotre1-*(vtedit,vthelp)
;
;	thus the screen editor can call up to the root (here)
;	to  remap to  the vteditor help code, do the help and
;	then remap back to the vt editor upon return.


$edit::	load	#l.main
	call	edit
	return

	global	<edit>

$cmrel::tst	(sp)+
	load	#l.main
	call	cmreld
	return

$vtrel::tst	(sp)+
	load	#l.vted
	call	vtreld
	return

$edsco::load	#l.edsc
	call	edscop
	return

$vte::	load	#l.vted
	call	vte
	return

$schel::load	#l.sche
	call	schelp
	return

	global	<cmreld,vtreld,edscop,vte,schelp>



	.if ne	,resovr
	.ift

overr:	mov	sts	,r2
	mov	sts+2	,r3
	clr	r4
	bisb	@#$dsw	,r4
	halt				; a mean thing to do, but it
					; will force a core dump.



ovfind::mov	r2	,-(sp)		; save a temp today
	mov	#20.	,r2		; retry 20 times on map failure

10$:	call	ovfin0			; try to map to a new segment
	bcc	100$			; it worked
	cmpb	#ie.upn	,@#$dsw		; no small buffers? (rsts)
	bne	110$			; no, we will have to die.
	.if eq	,rsx11m!rt11		; take a short nap and then
	.ift				; try to remap
	mov	#1	,xrb+0		; RSTS here
	.sleep				; sleeping is simple
	.iff				; RSX ?
	.mcall	mrkt$s	,wtse$s		; for RSX, issue a marktime
	mrkt$s	#20,#1,#2		; and then wait for the event
	wtse$s	#20			; wait......
	.endc				; for RSTS or RSX
	sob	r2	,10$
	br	110$			; complete failure, die

100$:	clc				; it worked
	mov	(sp)+	,r2
	return

110$:	sec				; it failed
	mov	(sp)+	,r2
	return				; die

	global	<$dsw	,ie.upn>

	.endc				; if ne, RESOVR


	.sbttl	map to a new reslib segment

	.if ne	,resovr
	.ift

	.if eq	,rsx11m
	.ift
	ws.wrt	=	0
	rs.wrt	=	0
	.endc

	t$rnme	=	14
	t$rwdb	=	20

	.mcall	atrg$s	,craw$s	,map$s	,qiow$s
	.enabl	lsb

ovfin0::mov	r2	,-(sp)		; save temp registers
	mov	r3	,-(sp)		;  save temp registers
	mov	r4	,-(sp)		;   save temp registers
	mov	r5	,-(sp)		;    save temp registers
	mov	@#n.ovpt,r2		; get address of mapping info
	mov	n.szsg(r2),r4		; size of seg descript (22 (8))
	mov	n.stbl(r2),r2		; get address of segment list
	clr	r5			; for error reporting
10$:	tst	@r2			; any more segments left to see?
	beq	200$			; yes, error
	mov	r2	,r3
	call	dump
	cmp	t$rnme(r2),2(r0)	; check for a match on seg name
	bne	20$			; error
	cmp	t$rnme+2(r2),4(r0)	; check for remaining match
	beq	30$			; got it
20$:	add	r4	,r2		; no match try again
	br	10$

30$:	sub	#30	,sp		; create setup for attach call
	mov	sp	,r3		; a dynamic buffer to use
	mov	#10	,r4		; number of words to copy
	mov	#att	,r1		; address of prototype params
40$:	mov	(r1)+	,(r3)+		; now copy it
	sob	r4	,40$		; simple
	mov	sp	,r3		; point back to it
	bis	#rs.wrt	,r.gsts(r3)	; set write access
	mov	#1	,r5
	call	dump
$atrg::	atrg$s	r3			; and attach to it
	bcs	110$			; oops
	mov	@r3	,r4		; reslib id
	mov	t$rwdb(r2),r2		; wdb address
	mov	#10	,r1		; copy wdb to temp area @ stack
50$:	mov	(r2)+	,(r3)+		; an attempt for r/o code ????
	sob	r1	,50$		; next word of the wdb please
	mov	sp	,r3		; point back to copy of the wdb
	mov	r4	,w.nrid(r3)	; copy the reslib id word now
	mov	w.nsiz(r3),w.nlen(r3)	; map the entire region
	clr	w.nsts(r3)		; readonly access please
	bis	#map.32	,w.nsts(r3)	; set correct mapping offset
	bis	#ws.wrt	,w.nsts(r3)
	mov	#2	,r5		; debug
	call	dump
$craw::	craw$s	r3			; create the window
	bcs	110$			; oops, we will have to die
	clr	w.nsts(r3)		; readonly access for map$
	bis	#map.32	,w.nsts(r3)	; set correct mapping offset
	bis	#ws.wrt	,w.nsts(r3)
	mov	r4	,w.nrid(r3)	; reset reslib id to be safe
	mov	#3	,r5		; debug
	call	dump
$map::	map$s	r3			; at last, map the segment
	bcs	110$			; really now!

100$:	add	#30	,sp		; pop stack and exit
	clc
	br	210$			; bye
110$:	add	#30	,sp		; error exit
200$:	mov	r5	,r0
	asl	r5			; print a message
	mov	@#$dsw	,-(sp)		; save this
	qiow$s	#io.wlb,#5,#20,,,,<erm(r5),#erml>
	mov	(sp)+	,@#$dsw		; for error reporting
	sec

210$:	mov	(sp)+	,r5		; pop temps now please
	mov	(sp)+	,r4		;  one more time
	mov	(sp)+	,r3		;   and again
	mov	(sp)+	,r2		;    and again .....
	return				; bye

	.dsabl	lsb
	.endc				; if ne, resovr


	.if ne	,resovr
	.ift

erm:	.word	10$,20$,30$,40$
10$:	.asciz	/?Can't find segment  /<cr><lf>
20$:	.asciz	/?ATRG (TEDRES) failed/<cr><lf>
30$:	.asciz	/?CRAW (TEDRES) failed/<cr><lf>
40$:	.asciz	/?MAP  (TEDRES) failed/<cr><lf>
erml	=	. - 40$


att:	.word	0
	.word	0
	.rad50	/TED/
	.rad50	/RES/
	.word	0
	.word	0
	.word	rs.red			; readonly access please
	.word	0

	call	$load

	global	<$load>
	global	<rs.red	,w.nsiz	,w.nlen	,w.nrid	,w.nsts>
	global	<map.32	,n.ovpt	,n.stbl	,n.szsg>
	global	<io.wlb>

	.endc				; if ne, RESOVR

	.sbttl	print octal value passed on sp (for debugging)


	.if ne	,resovr
	.ift

	.mcall	qiow$s

	.if eq	,debug
	.ift
dump:	return
	.iff
dump:
mapdmp::nop
	mov	r0	,-(sp)
	mov	r1	,-(sp)
	mov	r3	,r0
	mov	#10	,r1
10$:	mov	(r0)+	,-(sp)
	call	prtoct
	call	pcrlf
	sob	r1	,10$
	mov	(sp)+	,r1
	mov	(sp)+	,r0
	return

	.iftf				; always get this code in

prtoct::save	<r0,r1,r2,r3>		; save the scratch regs.
	mov	2+<4*2>(sp),r3
	sub	#10	,sp
	mov	sp	,r2
	mov	#6	,r0		; do it 6 times
	add	r0	,r2		; do it backwards
	call	10$			; simple
	qiow$s	#io.wlb,#5,#20,,,,<r2,#6>
	add	#10	,sp
	unsave	<r3,r2,r1,r0>
	mov	(sp)+	,(sp)
	return

10$:	mov	r3	,r1		; get the number
	bic	#177770	,r1		; leave low order 3 bits on
	movb	100$(r1),-(r2) 		; move an octal digit
	ash	#-3	,r3		; shift three bytes
	bic	#160000	,r3		; zap propagated sign bits
	sob	r0	,10$		; go convert next digit
	return

100$:	.ascii	\01234567\		; ascii/octal digits
	.even

pcrlf:	qiow$s	#io.wlb,#5,#20,,,,<#100$,#2>
	return

100$:	.byte	15,12
	.endc				; if ne, DEBUG

	.endc				; if ne, RESOVR
	.end
