	.title	system

	;
	;	This must be the first input file
	;	to the task builder (linker).
	;

	.include "os.mac"


	.asect

. = 40
	.word	ops			;program entry point

. = 600
cmdbuf:					;command file buffer
					;600 - 1577
					;stack limit at 2000 (RT-11)
. = 1000
cmdfil:					;command file name

	.psect	$syst

.if ne rt$sys

	; SERVER			CLIENTS
	; suspnd.mac	TSX:1,RT:1	suspnd.mac	TSX:1,RT:1
	; termio.mac	TSX:0,RT:1	termio.mac	TSX:1,RT:1
	; endrvr.mac	TSX:5,RT:5	skdrvr.mac	TSX:1,RT:1
	; svdrvr.mac	TSX:0,RT:2	cldrvr.mac	TSX:0,RT:2
	;				slunio.mac	TSX:0,RT:5
	;
	; plus extra elements

	qelements  = 16.	; queue elements required (RT-11 only)
	qsize	   = 16.*10.	; allocation size in words

qset:

.endc

	.page
	.sbttl	Initializer and Finalizer Code

	; The initializer allocates the queue
	; elements required by RT-11.

	.psect	$init

	.psect	$init$

.if ne rt$sys
	.word	rtqset			;initalizer routine
.endc

	.psect	$init.


	; The finalizer is not required.

	.psect	$finl

	.psect	$finl$

	.psect	$finl.


	.page
	.sbttl	Operating System Check

	;	This code:
	;	(1)	verifies that the program
	;		is running under the proper
	;		operating system.

	.psect	$syst

	.mcall	.exit,	.print,	.close

.if ne rt$sys
filnam:	.blkw	4			;copied command file name
.endc

ops:	mov	#gval,r0		;.gval
	emt	375
	tst	r0

.if ne rt$sys
	bmi	1$
	mov	#gvalxm,r0		;.gval
	emt	375
	bit	#010000,r0		;RT-11 Mapped System  ?
	bne	2$			;Yes - OK
.endc
.if ne ts$sys
	bmi	2$
.endc

1$:	.print	#opsmsg			;error message
	clr	r0
	.exit
	
2$:

	.page
	.sbttl	Stack Allocation Check

	;	This code:
	;	(1)	checks the stack limit (RT-11 only)

.if ne rt$sys
	cmp	@#42,#2000		;lowest stack position allowed
	bhis	3$
	.print	#sacmsg			;report error
	clr	r0
	.exit


	.page
	.sbttl	Foreground / System Job Check for RT-11

	;	This code:
	;	(1)	checks if argv: is suppressed
	;	   -->>	no,  finished
	;	(2)	checks if background job
	;	   -->> yes, enable argv:, finished
	;	(3)	checks if command file given
	;	   -->> no,  enable argv:, finished
	;	(4)	command file found and read ?
	;	   -->> no,  enable argv:, finished
	;	(5)	copy command line to $$cbuf
	;	   -->>	argv: suppressed, finished

	;	An external program must supply the
	;	code as shown here to enable the
	;	processing described above.  This
	;	code provides a method of starting
	;	system and foreground jobs which require
	;	command line arguments which cannot
	;	normally be supplied through a .com file.
	;	If the command file cannot be found
	;	then the processing reverts to requiring
	;	an argv: command line input.
	;
	;		.asect
	;
	;	. = 1000
	;
	;	cmdfil:	.rad50	"TCPRTELNTCMD"
	;
	;		.psect	$syst
	;
	;	$$narg:	.word	1
	;
	;	where the device must be "TCP"
	;

3$:	cmp	#1,$$narg		;special argv: suppressed ?
	bne	8$			;no, finished

	mov	#gtjb,r0		;.gtjb
	emt	375

	tst	jobnum			;background ?
	beq	7$			;yes, enable argv:

	mov	#cmdfil,r0		;copy file name
	mov	#filnam,r1
	mov	(r0)+,(r1)+
	mov	(r0)+,(r1)+
	mov	(r0)+,(r1)+
	mov	(r0)+,(r1)+

	cmp	#<^rTCP>,filnam		;file name given ?
	bne	7$			;no,  enable argv:

	mov	#lookup,r0		;.lookup
	emt	375
	bcc	4$

	mov	#<^rSY >,filnam		;check system device

	mov	#lookup,r0		;.lookup
	emt	375
	bcc	4$

	mov	#<^rDK >,filnam		;check default device

	mov	#lookup,r0
	emt	375
	bcs	7$			;file not found, enable argv:

4$:	mov	#readw,r0
	emt	375
	bcs	7$			;file error, enable argv:

	mov	#$$cbuf,r2		;destination
	mov	#cmdbuf,r1		;source
	mov	#$$cend-$$cbuf-1,r0	;maximum count
5$:	cmpb	(r1),<12>		;accept only one line
	beq	6$
	cmpb	(r1),<15>
	beq	6$
	movb	(r1)+,(r2)+
	beq	8$
	sob	r0,5$
6$:	clrb	(r2)+
	br	8$

7$:	clr	$$narg
8$:	.close	#0			;close file
.endc

	jmp	@#$$main		;program startup



gval:	.byte	0,34			;.gval
	.word	372			;rt-11 / tsx-plus

.if ne rt$sys
gvalxm:	.byte	0,34			;.gval
	.word	300			;rt-11 Mapped System

gtjb:	.byte	1,20			;.gtgb
	.word	jobnum
	.word	-1

jobnum:	.blkw	12.			;job  parameters

lookup:	.byte	0,1			;.lookup
	.word	filnam
	.word	0

readw:	.byte	0,10			;.readw
	.word	0
	.word	cmdbuf
	.word	256.
	.word	0
.endc



.if ne rt$sys
opsmsg:	.asciz	"This program requires a mapped RT-11 system."<12><15>
sacmsg:	.asciz	"Stack Limit less than 2000(8)."
.endc
.if ne ts$sys
opsmsg:	.asciz	"This program requires TSX-Plus."<12><15>
.endc
.even

.if ne rt$sys
	.blkw	<qsize>-<<.-qset>/2>	;complete allocation for qset
.endc


	.page
	.sbttl	Queue Element Allocation

	;	RT-11 requires this code to allocate
	;	the necessary queue elements.
	;
	;	TSX-Plus does not require queue element
	;	allocation within the program.

	.psect	c$code

	.mcall	.qset

.if ne rt$sys
rtqset:	.qset	#qset,#qelements
	rts	pc
.endc

	.end
