	.TITLE	ACSV$	C register save and restore with AST support
	.IDENT	"V1.3"
	.ENABL	LC
;+
;
; Index		C register save and restore with RSX AST's
; Index		C program execution environment with RSX AST's
;
; Usage
;
;	jsr	r5,csv$
;	...
;	jmp	cret$
;
; Description
;
;	     C program Run-time Environment with RSX AST's
;
;	Each C subroutine starts with a call to CSV$ and exits by
;	jumping to CRET$.  Upon exit, the stack need not be equal
;	to its value on entrance. This version preserves R0, so
;	that AST's will work.
;
;	During the execution of all C subroutines, register R5
;	points to the current "environment."  Within a subroutine,
;	it appears as follows:
;
;		 _______________
;		|		|
;	  SP ->	| 1st loc. var. | -10(R5)	C$AUTO-2(R5)
;		|_______________|
;		|		|
;		| Saved R2	|  -6(R5)
;		|_______________|
;		|		|
;		| Saved R3	|  -4(R5)
;		|_______________|
;		|		|
;		| Saved R4	|  -2(R5)
;		|_______________|
;		|		|
;	  R5 ->	| Saved R5	|
;		|_______________|
;		|		|
;		| Return add.	|  +2(R5)
;		|_______________|
;		|		|
;		| First arg.	|  +4(R5)	C$PMTR+0(R5)
;		|_______________|
;		|		|
;		| Second arg.	|  +6(R5)	C$PMTR+2(R5)
;		|_______________|
;
;	Within a subroutine, Registers R0-R4 and the top of the
;	stack, (sp) are available for use. Registers R0 and R1
;	are not preserved by subroutines and may be used to pass
;	a return value.
;
;	R5 must not be modified by a subroutine.  All variable
;	addressing must be done by offsets to R5.  Subroutine
;	arguments must be accessed by reference to C$PMTR.
;	Subroutine local variables must be accessed by reference
;	to C$AUTO.  This permits modification of calling sequences
;	without rewriting all subroutines.
;
;	CSV$ refers to global symbol $$main to call the run-time startup
;	program from the (RSX) library.
;
;	An RSX AST routine must call astset() immediately upon entry
;	to save R0 and R1. These registers are restored by astx(),
;	which must be used to exit from an AST service routine.
;	See the documentation of the CX library, CX.DOC.
;
; [end]
;-
;
; Edit history
; 000001 RBD		From old CSV.MAC.
; 000002 11-Mar-82 MM	Added Whitesmith's entries
; V1.3   19-Oct-82 RBD	Changed P-sections for new compiler. Documentation.
;
; If C$PMTR/C$AUTO are undefined, just define them
;
.iif	ndf	C$PMTR	C$PMTR	=  4	;formal[n] @ c$pmtr+<n*2>(r5)
.iif	ndf	C$AUTO	C$AUTO	= -6	;local[n]  @ c$auto-<n*2>(r5)
	.globl	C$PMTR, C$AUTO
.iif	ne	C$PMTR-4	.error	Bad definition of C$PMTR
.iif	ne	C$AUTO+6	.error	Bad definition of C$AUTO
;
; By defining C$PMTR and C$AUTO as local symbols, the task
; builder need not do so much work

	.GLOBL	$$MAIN	;Call start from the library

	.PSECT	C$CODE							;03
;
; save R4-R2 and make a one-word temp slot on the stack.
;
C$SAV::				;WHITESMITH'S SUBROUTINE ENTRY CALL	;02
CSV$::
	SUB	#10,SP		;MAKE ROOM FOR STACK FRAME
	MOV	R5,(SP)		;PUT ENTRY POINT IN TEMP SLOT
	MOV	SP,R5		;R5 --> TEMP SLOT
	TST	(R5)+		;R5 --> R2 SAVE SLOT
	MOV	R2,(R5)+	;SAVE R2; R5 --> R3 SAVE SLOT
	MOV	R3,(R5)+	;SAVE R3; R5 --> R4 SAVE SLOT
	MOV	R4,(R5)+	;SAVE R4; R5 --> R5 SAVE SLOT
	JMP	@(SP)		;ENTER ROUTINE
;
; pop R2-R4 and restore stack linkage registers.
;
C$RET::				;WHITESMITH'S SUBROUTINE EXIT		;02
CRET$::
	MOV	R5, R2
	MOV	-(R2), R4
	MOV	-(R2), R3
	MOV	-(R2), R2
C$RETS::			;WHITESMITH'S "NO RESTORE" EXIT		;02
	MOV	R5, SP
	MOV	(SP)+, R5
	RTS	PC
	.END
