	.title SETLIN - SET serial LINe parameters
	.mcall .module
.module SETLIN,RELEASE=V01,VERSION=01,COMMENT=<SET SERIAL LINES>,AUDIT=YES
	.if  ne 0

	SETLIN is designed to run on rt-11 and will set the serial line
	parameters according to setlin's default values.  these default
	values may be modified and so will replace the existing values
	in setlin.mac OR the operator may specify their own output file

		Modification History
		--------------------
		v1.00  Jul-85  RWH/TAE  Initial version
	.endc
;
;	Version Header. This label MUST be the first code/data label
;	in the program.
;
vheadr:	.ascii	\Setlin Version 1.00  5-July-1985\<15><12><0>
	.even



	.page
;
	.enable lc
	.mcall	.close,.csispc,.enter,.exit,.fetch,.gtlin,.lookup
	.mcall	.print,.readw,.scca,.trpset,.ttyin,.ttyout,.writw
;
	.macro	type,text
	jsr	pc,tycr
	.asciz	@text@
	.even
	.endm
	.macro	typen,text
	jsr	pc,tynocr
	.asciz	@text@
	.even
	.endm
;
	chan	=	1			;channel number one, output
	jsw	=44
	errbyt	=52
;				powers of two (binary)
	bit0	=1			;octal equiv.,  1      -decimal equiv.
	bit1	=2			;               2
	bit2	=4			;               4
	bit3	=10			;               8
	bit4	=20			;               16
	bit5	=40			;               32
	bit6	=100			;               64
	bit7	=200			;               128
	bit8	=400			;               256
	bit9	=1000			;               512
	bit10	=2000			;               1024
	bit11	=4000			;               2048
	bit12	=10000			;               4096
	bit13	=20000			;               8192
	bit14	=40000			;               16384
	bit15	=100000			;               32768
;
SETLIN:
	bic #bit14, @#jsw		;convert all characters to uppercase
	clr modflg			;clear modification flag
	clr linbuf			;clear first 2 bytes of gtlin buffer
	.GTLIN #linbuf,#intro		;prompt for option
;
	cmpb linbuf, #033		;oper wants out?
	bne 3$				;branch if no
	jmp xit				;
3$:	cmpb linbuf, #000		;oper wants to quit?
	bne 8$				;branch if no
	jmp xit				;
;
8$:	cmp linbuf, #"DE		;oper want setlin default parameters?
	bne 9$				;branch if no
	call setdef			;setup setlin default parameters
	jmp xit				;all done
;
9$:	cmp linbuf, #"MO		;oper want to modify setlin defaults?
	beq 10$				;branch if yes
	jmp 100$			;
10$:
	call setcur			;display current parameters
	.PRINT #modhlp			;display how to get help
	call setmod			;accept new default parameters
	bne 15$				;branch if z bit is clear (finished)
	br 10$				;branch if z bit set
15$:
	tst modflg			;done any modifications?
	bne 20$				;branch if yes
	jmp xit				;
20$:	clr glinbf			;clear first 2 bytes of gtlin buffer
	.GTLIN #glinbf,#linp91		;GET RESPONSE RE MODIFICATION SAVE
	cmpb glinbf,#077		;wants help?
	bne 21$				;branch if no
	.PRINT #mhlp10			;help test on save modifications
	;
	jmp 20$				;
21$:	cmpb glinbf,#131		;want to save modifications?
	beq 22$				;branch if yes
	jmp xit				;
22$:	clr glinbf			;clear first 2 bytes of gtlin buffer
	clr glinbf+2			;
	clr glinbf+4			;

	call saver			;save modifications routine
	jmp xit				;all done
;
;
;
100$:	cmp linbuf, #"HE		;oper wants help?
	beq sethlp			;branch if yes
	cmp linbuf, #"  		;any characters?
	bne sethlp			;branch if yes, oper needs help
					;no characters so exit
;
xit:	.EXIT
	.page				;force page break
;
;
sethlp:
	.PRINT #vheadr			;version header text
	.PRINT #hlptx1			;display help & modify help text
	call mcont			;
	.PRINT #hlptx2			;display default help text
	call mcont			;
	.PRINT #hlptx3			;display default help text
	call mcont			;
	.PRINT #hlptx4			;display default help text
	jmp setlin			;
;
;
;
;			quietly set up default serial line parameters
setdef:
;;;;;;	.PRINT #deftx
	mov #1., r0			;
	call delay			;delay for a milli second
	clr linptr			;initialize serial line pointer
sdflp:	mov linptr, r0			;linptr to r0
	mov r0, curtln			;put current line number in storage
	movb xref(r0), r5		;partner line
	mov r5, partln			;put partner line number in storage
	asl r0				;shift left so r0 is word addressable
	asl r5				;
	cmp config(r0), #177777		;unknown (all ones) config for this line?
	beq 18$				;branch if yes
	mov csradr(r0), r1		;csr address to r1
	mov r0, -(sp)			;save r0
	.TRPSET #tarea,#trout		;setup trap to 4 routine
	mov (sp)+, r0			;restore r0
	mov r0, -(sp)			;save r0
	tst (r1)			;addr r1 points to exist?
	bcc 20$				;branch if yes
;;;;;;	.PRINT #nfound			;display not found message
	mov (sp)+, r0			;restore r0
18$:	jmp noconf			;
20$:	mov (sp)+, r0			;restore r0
	clrb uniqb			;clear byte (assume b.r. for both lines not unique)
	movb config+1(r0), baudhi	;mov hi byte (baud rate) to low byte
	asr baudhi			;shift right to
	asr baudhi			;drop off
	asr baudhi			;first
	asr baudhi			;four bits
	bic  #177760, baudhi		;and zero bits 4 and up
	mov baudhi, r2			;addr of rate required
	tstb unique(r2)			;is baud rate for current line unique?
	beq 30$				;branch if no
	bis #bit0, uniqb		;indicate current line is unique
30$:	cmp config(r5), #177777		;unknown (all ones) config for this line?
	bne 33$				;branch if no
	mov r1, -(sp)			;save r1, - current line csr addr
	mov r0, -(sp)			;sav r0
	mov csradr(r5), r1		;partner line csr addr to r1
	.TRPSET #tarea,#trout		;setup trap to 4 routine
	tst (r1)			;addr r1 points to (partner line) exist?
	bcc 31$				;branch if yes
	jmp 32$				;
31$:	bit #bit0, uniqb		;current line unique?
	beq 32$				;branch if no
	mov curtln, r3			;octal number to r3
	mov #defer5, r4			;ascii string addr to r4
	mov #1., r5			;number of digits
	call cvtoct			;current s/line number to output area
	mov partln, r3			;octal number to r3
	mov #defer6, r4			;ascii string addr to r4
	mov #1., r5			;number of digits
	call cvtoct			;partner s/line number to output area
	.PRINT #defer4			;warn partner line exists but not configured
	mov (sp)+, r0			;restore r0
	mov (sp)+, r1			;restore r1 (current line csr addr)
	jmp 70$				;go setup current line
32$:	mov (sp)+, r0			;restore r0
	mov (sp)+, r1			;restore r1 (current line csr addr)
33$:	movb config+1(r5), baudhi	;mov hi byte (baud rate) to low byte
	asr baudhi			;shift right to
	asr baudhi			;drop off
	asr baudhi			;first
	asr baudhi			;four bits
	bic  #177760, baudhi		;and zero bits 4 and up
	mov baudhi, r2			;addr of rate required
	tstb unique(r2)			;is baud rate for partner line unique?
	beq 40$				;branch if no
	bis #bit1, uniqb		;indicate partner line is unique
40$:	tstb uniqb			;either line unique?
	bne 45$				;branch if yes
	jmp 70$				;ok, do setting
45$:	cmpb uniqb, #3			;both lines unique?
	blt 60$				;branch if no
	clrb brtb			;clear baud rate table byte
	bit #bit7, config(r0)		;current line in baud rate table 1?
	beq 50$				;branch if yes
	bisb #bit0, brtb		;indicate current line in brt 2
50$:	bit #bit7, config(r5)		;partner line in baud rate table 1?
	beq 55$				;branch if yes
	bisb #bit1, brtb		;indicate partner line in brt 2
55$:	cmpb brtb, #0			;both lines in brt 1?
	beq 70$				;branch if yes
	cmpb brtb, #3			;both lines in brt 2?
	beq 70$				;branch if yes
	mov curtln, r3			;octal number to r3
	mov #defer2, r4			;ascii string addr to r4
	mov #1., r5			;number of digits to convert
	call cvtoct			;current s/line number to output area
	mov partln, r3			;octal number to r3
	mov #defer3, r4			;ascii string addr to r4
	mov #1., r5			;number of digits to convert
	call cvtoct			;partner s/line number to output area
	.PRINT #defer1			;announce both lines have unique baud rate
					;and differing baud rate tables
	jmp noconf			;skip this line and try next
60$:	bit #bit0, uniqb		;current line unique?
	beq 62$				;branch if no
;			make partner line same b.r.t. as current line
	mov config(r0), r2		;move current line to r2
	bic #177577, r2			;clear all bits except bit 7 (b.r.t. select)
	bis config(r5), r2		;set partner line bits in r2
	mov r2, config(r5)		;and load into expected area
	mov r1, -(sp)			;save r1 - current line csr addr
	mov csradr(r5), r1		;partner line csr addr to r1
	movb config+1(r5), xcsr+1(r1)	;
	movb config(r5), rcsr+1(r1)	;
	mov (sp)+, r1			;restore r1 - current line csr addr
	jmp 70$				;
;			make current line same b.r.t. as partner line
62$:	mov config(r5), r2		;move partner line mode to r2
	bic #177577, r2			;clear all bits except bit 7 (b.r.t. select)
	bis config(r0), r2		;set current line bits in r2
	mov r2, config(r0)		;and load into expected area
;			set current line
70$:	movb config+1(r0), xcsr+1(r1)	;move high byte (baud rate) to high byte
					;of transmit csr
	movb config(r0), rcsr+1(r1)	;move low byte (mode) to high byte of
					;receive csr
noconf:	inc linptr			;point to next serial line
	cmp linptr, #<csrade-csradr>/2	;done all lines?
	blt 80$				;branch if no
	rts pc				;
;
80$:	jmp sdflp			;do next line
	.page				;force page break
;
;			operator wants to modify setlin.sav
;			default serial line parameters
;			RCSR register (low byte of config word) indicates:
;					 -which of two baud rate tables to use
;					 (lines 0 and 3 only)
;					-number of stop bits
;					-parity enabled or not
;					 (if enabled, odd or even)
;					-character length
;			XCSR register (hi byte of config word) indicates:
;					 -baud rate
setcur:
	.PRINT #crlf			;do carriage return and line feed
	.PRINT #crlf			;
	.PRINT #modtxh
	clr linptr			;initialize serial line pointer
smodlp:	mov #modlin, r4			;ascii string addr to r4
	call blkrtn			;clear ascii message area
	mov linptr, r0			;linptr to r0
	mov linptr, r3			;octal number to r3
	mov #modlin, r4			;ascii string addr to r4
	mov #1., r5			;number of digits
	
	cmp r0, #8.			;doing line number eight?
	bne 2$				;branch if no
	movb #'8, (r4)			;move to area pointed to by r4
	br 3$				;
2$:	call cvtoct			;current s/line number to output area
3$:	asl r0				;shift left so r0 is word addressable
	mov csradr(r0), r1		;csr addr of current s/line to r1
	mov r1, r3			;csr addr to output area
	mov #modcsr, r4			;ascii string addr to r4
	mov #6., r5			;number of digits
	call cvtoct			;csr addr to output area
;
	cmp config(r0), #177777		;unknown (all ones) config for this line?
	bne 6$				;branch if no (known)
	jmp 55$				;config unknown
;
6$:	clr baudhi			;clear word
	clr modelo			;clear word
	movb config(r0), modelo		;mov low byte (mode) to low byte of word
	movb config+1(r0), baudhi	;mov hi byte (baud rate) to low byte
;
	asr baudhi			;shift right to
	asr baudhi			;drop off
	asr baudhi			;first
	asr baudhi			;four bits
	bic  #177760, baudhi		;and zero bits 4 and up
	mov #5, r3			;number of ascii char to move
	mov baudhi, r4			;addr of rate required
	movb tab5(r4), r4		;mov byte offset from table5 to r4
	mov #modbau, r5			;mov addr of destn ascii string to r5
	bit #bit7, modelo		;tst bit 7 to determine which baud rate
					;table to use
	bne 12$				;branch if using baud rate table 2

					;using baud rate table 1 (bit 7=0)
	add #brt1, r4			;add addr of start of b/r table1 to r4
	br 13$				;
12$:	add #brt2, r4			;add addr of start of b/r table2 to r4
13$:	call mmovb			;call multiple move byte routine
	movb modelo, r2			;contents of modelo to r2
	bit #bit0, r2			;bit zero set? (char len 8)
	bne 15$				;branch if yes
	movb #'7, moddb			;char len (7) to output area
	br 20$				;
15$:	movb #'8, moddb			;char len (8) to output area
20$:
	movb modelo, r2			;contents of modelo to r2
	bit #bit4, r2			;bit 4 set? (parity enabled)
	bne 40$				;branch if no
	bit #bit2, r2			;bit 2 set? (odd parity)
	bne 30$				;branch if no
	movb #'E, modpar		;even parity to
	movb #'v, modpar+1		;
	movb #'e, modpar+2		;output area
	movb #'n, modpar+3		;
	br 45$				;

30$:	movb #'O, modpar		;odd parity to
	movb #'d, modpar+1		;
	movb #'d, modpar+2		;output area
	br 45$				;

40$:	movb #'N, modpar		;no parity to
	movb #'o, modpar+1		;
	movb #'n, modpar+2		;output area
	movb #'e, modpar+3		;

45$:	movb modelo, r2			;contents of modelo to r2
	bit #bit6, r2			;bit 6 set? (2 stop bits)
	bne 50$				;branch if yes
	movb #'1, modsb			;1 stop bits to output area
	br 55$				;
50$:	movb #'2, modsb			;2 stop bits to output area

55$:	mov r0, -(sp)			;save r0
	.PRINT #modlin			;output to screen
	mov (sp)+, r0			;restore r0

	mov r0, -(sp)			;save r0
	.TRPSET #tarea,#trout		;setup trap to 4 area and routine
	mov (sp)+, r0			;restore r0

	mov r0, -(sp)			;save r0
	tst (r1)			;addr r1 points to exist?
	bcs 60$				;branch if no (carry bit set)
	.PRINT #found			;output found to screen
	br 65$				;
60$:	.PRINT #nfound			;output not found to screen

65$:	mov (sp)+, r0			;restore r0
	jmp modcnt			;

trout:	bis	#1,2(sp)		;set the carry bit in the PSW image
	rti				;return from trpset routine

mnocsr:	mov r0, -(sp)			;save r0
	.PRINT #modlin			;output to screen
	.PRINT #nfound			;output csr not found message to screen
	mov (sp)+, r0			;restore r0
	jmp modcnt			;try next csr line
;
mnocnf:	mov r0, -(sp)			;save r0
	.PRINT #modlin			;output to screen
	.PRINT #notset			;output to screen
	mov (sp)+, r0			;restore r0

modcnt:	inc linptr			;point to next serial line
	cmp linptr, #<csrade-csradr>/2	;done all serial lines?
	blt 72$				;branch if no
	jmp 80$				;done
72$:	jmp smodlp			;do next serial line
80$:
	rts pc
;
;
;			accept new default parameters on a csr line by line basis
;
setmod:	clr glinbf			;clear first 2 bytes of gtlin buffer
	.GTLIN #glinbf,#linp1		;GET CSR LINE TO MODIFY
	cmpb glinbf,#077		;needs help?
	bne ok1a			;branch if no
	.PRINT #mhlp1a			;display help for this question
	jmp mcont			;
ok1a:	cmpb glinbf,#033		;want to exit? all done?
	bne ok1b			;branch if no
	clz				;clear z bit
	jmp ret				;
ok1b:	cmpb glinbf,#'0			;line number greater or equal zero?
	bge ok1c			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok1c:	cmpb glinbf,#'8			;line number less or equal eight?
	ble ok1d			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok1d:	cmpb glinbf+1,#000		;only one digit number entered?
	beq ok1e			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok1e:	movb glinbf, linptr		;save number of csr line to modify
	sub #'0, linptr			;sub ascii 0 to make an octal number
;
	call defs			;routine to display default answers
;
	mov #6., r3			;number of bytes to mov
	mov #glinbf, r4			;addr of source
	mov #glinbf+1, r5		;addr of destn
	movb #'0, (r4)			;move zero to first byte of glinbf
	call mmovb			;call multiple move byte routine
	clr glinbf+6			;clear bytes 6 & 7 of gtlin buffer
	clr glinbf+8.			;clear bytes 8 & 9 of gtlin buffer
	clr glinbf+10.			;clear bytes 10 & 11 of gtlin buffer
	.GTLIN #glinbf+6,#linp2		;GET BAUD RATE REQUIRED
	cmpb glinbf+6,#077		;needs help?
	bne ok2a			;branch if no
	.PRINT #mhlp2a			;display help for this question
	jmp mcont			;
ok2a:	cmpb glinbf+6,#033		;want to escape?
	bne ok2b			;branch if no
	.PRINT #mhlpab			;display abort message
	clz				;clear z bit
	jmp ret				;
ok2b:	cmpb glinbf+11.,#000		;only five digit number entered?
	beq ok2c			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok2c:	cmpb glinbf+6,#000		;accept default?
	bne ok2c0a			;branch if no
	mov #5., r3			;number of bytes to move
	mov #linp2a, r4			;addr of source
	mov #glinbf, r5			;addr of destination
	call mmovb			;call multiple move byte routine
	jmp ok2c0e			;
ok2c0a:	mov #glinbf+6, r5		;addr of opers required baud
	clr r3				;zero r3
ok2c0b:	cmpb (r5)+, #000		;found null? (return)
	beq ok2c0c			;branch if yes
	inc r3				;add to counter
	br ok2c0b			;loop again
ok2c0c:	inc r3				;
	dec r5				;
	mov #glinbf+5, r4		;expected addr of baud rate
ok2c0d:	movb -(r5), -(r4)		;mov opers ans to expected area right justified
	sob r3, ok2c0d			;subtract and, if not zero, loop
ok2c0e:	mov #16., r2			;validate b/rate answer against table 1
ok2c1a:	mov #glinbf, r3			;address of gtlin buffer to r3
	movb tab5-1(r2), r4		;mov byte offset from table5 to r4
	add #brt1, r4			;add addr of start of b/r table1 to r4
	mov #5., r5			;counter for looping
ok2c1b:	cmpb (r3)+, (r4)+		;bytes equal?
	bne ok2c1c			;branch if no
	sob r5, ok2c1b			;subtract and, if not zero, loop
	bic #bit7, modelo		;match in b/rate table one
	jmp ok2d			;
ok2c1c:	sob r2, ok2c1a			;no match, advance & try next group of 5
					;not in table 1.
	mov #16., r2			;validate b/rate answer against table 2
ok2c2a:	mov #glinbf, r3			;address of gtlin buffer to r3
	movb tab5-1(r2), r4		;mov byte offset from table5 to r4
	add #brt2, r4			;add addr of start of b/r table2 to r4
	mov #5., r5			;counter for looping
ok2c2b:	cmpb (r3)+, (r4)+		;bytes equal?
	bne ok2c2c			;branch if no
	sob r5, ok2c2b			;subtract and, if not zero, loop
	bis #bit7, modelo		;match in b/rate table two
	jmp ok2d			;
ok2c2c:	sob r2, ok2c2a			;no match, advance & try next group of 5
					;not in table 2.
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok2d:
	dec r2				;decrement r2, the b/rate pointer
	mov r2, baudhi			;move to baudhi word
	asl baudhi			;shift left to
	asl baudhi			;get in
	asl baudhi			;correct bit position
	asl baudhi			;of low byte
;
	clr glinbf			;clear first 2 bytes of gtlin buffer
	.GTLIN #glinbf,#linp3		;GET BITS/CHAR (character length)
	cmpb glinbf,#077		;needs help?
	bne ok3a			;branch if no
	.PRINT #mhlp3a			;display help for this question
	jmp mcont			;
ok3a:	cmpb glinbf,#033		;want to escape?
	bne ok3b			;branch if no
	.PRINT #mhlpab			;display abort message
	clz				;
	jmp ret				;
ok3b:	cmpb glinbf+1,#000		;only one digit number entered?
	beq ok3c			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok3c:	cmpb glinbf,#000		;accept default?
	bne ok3d			;branch if no
	mov #1., r3			;number of bytes to move
	mov #linp3a, r4			;addr of source
	mov #glinbf, r5			;addr of destination
	call mmovb			;call multiple move byte routine
ok3d:	cmpb glinbf,#'7			;bits/char equal 7?
	bne ok3e			;branch if no
	bic #bit0, modelo		;clear bit0 in low byte
	bis #bit1, modelo		;set bit1 in low byte
	jmp ok3h			;
ok3e:	cmpb glinbf,#'8			;bits/char equal 8?
	bne ok3g			;branch if no
ok3f:	bis #bit0, modelo		;set bit0 in low byte
	bis #bit1, modelo		;set bit1 in low byte
	jmp ok3h			;
ok3g:	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok3h:
;
	clr glinbf			;clear first 2 bytes of gtlin buffer
	.GTLIN #glinbf,#linp4		;GET PARITY
	cmpb glinbf,#077		;needs help?
	bne ok4a			;branch if no
	.PRINT #mhlp4a			;display help for this question
	jmp mcont			;
ok4a:	cmpb glinbf,#033		;want to escape?
	bne ok4b			;branch if no
	.PRINT #mhlpab			;display abort message
	clz				;
	jmp ret				;
ok4b:	cmpb glinbf,#000		;accept default?
	bne ok4c			;branch if no
	mov #1., r3			;number of bytes to move
	mov #linp4a, r4			;addr of source
	mov #glinbf, r5			;addr of destination
	call mmovb			;call multiple move byte routine
ok4c:	cmpb glinbf,#'N			;no parity?
	bne ok4e			;branch if no
ok4d:	bis #bit4, modelo		;disable parity
	jmp ok4h			;
ok4e:	cmpb glinbf,#'E			;even parity?
	bne ok4f			;branch if no
	bic #bit4, modelo		;enabled parity
	bic #bit2, modelo		;enable even parity
	jmp ok4h			;
ok4f:	cmpb glinbf,#'O			;odd parity?
	bne ok4g			;branch if no
	bic #bit4, modelo		;enable parity
	bis #bit2, modelo		;enalbe odd parity
	jmp ok4h			;
ok4g:	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok4h:
;
	clr glinbf			;clear first 2 bytes of gtlin buffer
	.GTLIN #glinbf,#linp5		;GET NUMBER OF STOP BITS
	cmpb glinbf,#077		;needs help?
	bne ok5a			;branch if no
	.PRINT #mhlp5a			;display help for this question
	jmp mcont			;
ok5a:	cmpb glinbf,#033		;want to escape?
	bne ok5b			;branch if no
	.PRINT #mhlpab			;display abort message
	clz				;
	jmp ret				;
ok5b:	cmpb glinbf+1,#000		;only one digit number entered?
	beq ok5c			;branch if yes
	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok5c:	cmpb glinbf,#000		;accept default?
	bne ok5d			;branch if no
	mov #1., r3			;number of bytes to move
	mov #linp5a, r4			;addr of source
	mov #glinbf, r5			;addr of destination
	call mmovb			;call multiple move byte routine
ok5d:	cmpb glinbf,#'1			;one stop bit?
	bne ok5f			;branch if no
ok5e:	bic #bit6, modelo		;set one stop bit
	jmp ok5h			;
ok5f:	cmpb glinbf,#'2			;two stop bits?
	bne ok5g			;branch if no
	bis #bit6, modelo		;set two stop bits
	jmp ok5h			;
ok5g:	.PRINT #invald			;announce invalid response
	jmp mcont			;
ok5h:
	mov linptr, r0			;line pointer to r0
	asl r0				;shift left so r0 is word addressable
	movb modelo, config(r0)		;low byte to low byte
	movb baudhi, config+1(r0)	;low byte to high byte
	bis #bit11, config(r0)		;PBRE bit on
	call setdef			;setup serial line parameters
	mov #1., modflg			;indicate a modification made
	sez				;set z bit
	jmp ret				;
;
;
;
mcont:	clrb glinbf			;clear first byte of gtlin buffer
	.GTLIN #glinbf,#linp90		;await return key
	cmpb glinbf,#000		;get return?
	bne mcont			;branch if no
	sez				;set z bit
ret:	rts pc				;return
	.page				;force page break
	;
;===========================================================
;		called routines section
;
	;
	; CVTOCT - convert octal number to ascii string
	;
	;arguments:	r3 - octal number
	;		r5 - number of octal digits
	;result:	r4 - ascii string area
	;function:	convert octal number in r3 to ascii string pointed to
	;		by r4.
	;
CVTOCT:	add r5, r4			;point to end of ascii string for 1st #
lp:	dec r4				;number less one
	movb r3, (r4)			;copy 8 bits of octal no. into ascii string
	bicb #370, (r4)			;clear all but 3 low bits
	bisb #'0, (r4)			;add in '0' to make ascii char
	asr r3				;shift right
	asr r3				;next three
	asr r3				;bits to become low 3 bits
	sob r5, lp			;subtract and, if not zero, loop for next
	rts pc
	;
	;
	;
	; BLKRTN - clear an ascii string
	;
	;arguments:	r4 - start of ascii string area
	;results:	none
	;function:	byte by byte clear of an ascii string area.  halts upon
	;		encountering 0 or 200
	;
BLKRTN:	bitb #177, (r4)			;test for 0 or 200
	bne clrit			;branch if no
	rts pc				;return
clrit:	movb #' , (r4)+			;clear & increment pointer to next ascii
	br blkrtn			;loop to next
	;
	;
	;
	; MMOVB - multiple move byte
	;
	;arguments:	r3 - number of bytes to move
	;		r4 - start source addr
	;		r5 - start destn addr
	;result:	none
	;function:	move a number to bytes from source to destination
	;
MMOVB:	movb (r4)+, (r5)+		;mov byte, increment src & dest by one
	sob r3, mmovb			;subtract and, if not zero, loop for next
	rts pc				;return
	;
	;
	.page
;
;
	;DEFS - defaults
	;
	;arguments:	csr line number in linptr
	;result:	a line's existing values are displayed as the
	;		default answers
	;function
;
DEFS:	mov linptr, r0			;
	asl r0				;shift left so r0 is word addressable
	cmp config(r0), #177777		;unknown (all ones) config for this line?
	bne 6$				;branch if no (known)
	jmp 55$				;config unknown
;
6$:	clr baudhi			;clear word
	clr modelo			;clear word
	movb config(r0), modelo		;mov low byte (mode) to low byte of word
	movb config+1(r0), baudhi	;mov hi byte (baud rate) to low byte
;
	asr baudhi			;shift right to
	asr baudhi			;drop off
	asr baudhi			;first
	asr baudhi			;four bits
	bic  #177760, baudhi		;and zero bits 4 and up
	mov #5, r3			;number of ascii char to move
	mov baudhi, r4			;addr of rate required
	movb tab5(r4), r4		;mov byte offset from table5 to r4
	mov #linp2a, r5			;mov addr of destn ascii string to r5
	bit #bit7, modelo		;tst bit 7 to determine which baud rate
					;table to use
	bne 12$				;branch if using baud rate table 2

					;using baud rate table 1 (bit 7=0)
	add #brt1, r4			;add addr of start of b/r table1 to r4
	br 13$				;
12$:	add #brt2, r4			;add addr of start of b/r table2 to r4
13$:	call mmovb			;call multiple move byte routine
	movb modelo, r2			;contents of modelo to r2
	bit #bit0, r2			;bit zero set? (char len 8)
	bne 15$				;branch if yes
	movb #'7, linp3a		;char len (7) to output area
	br 20$				;
15$:	movb #'8, linp3a		;char len (8) to output area
20$:
	movb modelo, r2			;contents of modelo to r2
	bit #bit4, r2			;bit 4 set? (parity enabled)
	bne 40$				;branch if no
	bit #bit2, r2			;bit 2 set? (odd parity)
	bne 30$				;branch if no
	movb #'E, linp4a		;even parity to
	br 45$				;

30$:	movb #'O, linp4a		;odd parity to
	br 45$				;

40$:	movb #'N, linp4a		;no parity to

45$:	movb modelo, r2			;contents of modelo to r2
	bit #bit6, r2			;bit 6 set? (2 stop bits)
	bne 50$				;branch if yes
	movb #'1, linp5a		;1 stop bits to output area
	br 65$				;
50$:	movb #'2, linp5a		;2 stop bits to output area
	jmp 65$				;return
;
55$:	movb #'0, linp2a		;set up default
	movb #'9, linp2a+1		;for an
	movb #'6, linp2a+2		;as yet
	movb #'0, linp2a+3		;unconfigured
	movb #'0, linp2a+4		;line
	movb #'8, linp3a		;
	movb #'N, linp4a		;
	movb #'1, linp5a		;
;
65$:	return				;
;
;
	.page
	;
	; SAVER	routine which saves itself.
	;
SAVER:	mov	#outspc,r0		;clear down
	mov	#<linben-outspc>/2,r1	;and line buffers
10$:	clr	(r0)+
	sob	r1,10$

	typen	<Change Default name of >

	mov	#filnam,r0		;display default filename
	jsr	pc,tytext		;with no crlf following
	jsr	pc,yesno		;obtain oper's response
	bcs	30$			;branch if no

	jsr	pc,tycrlf
12$:	typen	<Enter new Filename >

	.gtlin	#filnam			;get new filename
	cmpb filnam,#077		;wants help?
	bne 30$				;branch if no
	.PRINT #mhlp11			;help test on save filename
	;
	jmp 12$				;


30$:	mov	sp,r5			;save sp in case of options
	.csispc	#outspc,#defext,#filnam	;rad50 of filnam in outspc
	mov	r5,sp

	typen	<Filename is >
	mov	#in1,r1			;get device name
	jsr	pc,typfil		;display back the new file name
	jsr	pc,tycrlf
	
	.fetch	#handlr,#in1		;get the handler
	bcc	25$			;branch if got it

23$:	type	<error fetching handler>
	jmp	quit

25$:	.lookup	#area,#chan,#in1	;see if output file exists
	bcs	40$			;branch if no.

	typen	<Output file exists, Delete it? >
	jsr	pc,yesno
	beq	40$			;branch if yes	
	jmp	quit

40$:	.close	#chan			;close it

	mov	#buf,r0			;clear output buffer
	mov	#256.,r2
60$:	clr	(r0)+
	sob	r2,60$

	mov	#mvtab,r0		;point to move table
	mov	#<mvtabe-mvtab>/4,r2	;length

70$:	mov	@(r0)+,@(r0)+		;save some word locations in blk zero
	sob	r2,70$

	br	mvtabe

mvtab:	.word	40,buf+40,42,buf+42,44,buf+44,46,buf+46,50,buf+50
	.word	360,buf+360,362,buf+362,364,buf+364,366,buf+366
mvtabe:

	mov	50,r1			;get file length
	bic	#777,r1			;kill all but uppers
	swab	r1			;make pages
	asr	r1			;make records
	inc	r1			;round up
	mov	r1,numblk		;count

	.enter	#area,#chan,#in1,r1	;make the file
	bcc	190$

	type	<Error opening output file>
	jmp	quit

190$:	clr	block			;clear write block counter

	.writw	#area,#chan,#buf,#256.,block	;write 1st block
	bcs	205$			;oops

	inc	block			;count
	mov	numblk,r3		;block counter
	dec	r3			;already done one
	mov	#vheadr,r2		;source pointer
	cmp	r2,#1000		;MUST be at 1000
	beq	200$			;ok

	type	<Can't save, non-standard load address.>
	br	quit

200$:	.writw	#area,#chan,r2,#256.,block	;write
	bcc	210$

205$:	type	<Error Writing File>
	br	quit

210$:	inc	block			;next block
	add	#512.,r2		;next record

	sob	r3,200$			;do next block

	type	<Finished>

	.close	#chan			;close output channel

quit:	rts pc				;and return to calling program




yesno:	typen	< (Y/N)? >
	.gtlin	#linbf
	bic	#177640,linbf		;force U/C and no parity
	cmpb	linbf,#'N		;no
	beq	10$

	cmpb	linbf,#'Y		;yes
	bne	yesno			;no, ask again

	clc
	rts	pc

10$:	sec
	rts	pc




typrad:	mov	r1,-(sp)
	mov	r0,r1			;put in right reg
	mov	#<50*50>,r0		;1st divisor
	jsr	pc,div			;divide
	movb	radstr(r0),r0		;convert
	jsr	pc,tychar		;print it

	mov	#50,r0			;2nd divide
	jsr	pc,div
	movb	radstr(r0),r0		;convert
	jsr	pc,tychar		;print

	movb	radstr(r1),r0		;3rd digit
	jsr	pc,tychar
	mov	(sp)+,r1
	rts	pc

;
;	R0 = R1/R0, R1 = remainder
;
div:	mov	r0,-(sp)		;use stack
	clr	r0			;counter

10$:	inc	r0			;count
	sub	(sp),r1			;subtract
	bhis	10$			;repeat

	add	(sp)+,r1		;correct
	dec	r0
	rts	pc

radstr:	.ascii	/ ABCDEFGHIJKLMNOPQRSTUVWXYZ$..0123456789/

typfil:	mov	(r1)+,r0		;type filespecs R1 -->
	jsr	pc,typrad
	typen	<:>
	mov	(r1)+,r0
	jsr	pc,typrad		;file name
	mov	(r1)+,r0
	jsr	pc,typrad
	typen	<.>
	mov	(r1)+,r0		;extension
	jsr	pc,typrad
	rts	pc

tycr:	mov	(sp)+,r0		;get ext address
	jsr	pc,tytext
	mov	r0,-(sp)
	jsr	pc,tycrlf
	mov	(sp)+,r0
sretn:	bit	#1,r0
	beq	10$
	inc	r0
10$:	jmp	@r0

;type a cr-lf

cr	=	15
lf	=	12

tycrlf:	mov	#cr,r0
	jsr	pc,tychar
	mov	#lf,r0
	jsr	pc,tychar
	rts	pc
	


;type out text, with no cr-lf

tynocr:	mov	(sp)+,r0
	jsr	pc,tytext
	br	sretn

;type out a character

tychar:	.ttyout	r0
	rts	pc

;type out the string pointed to by r0

tytext:	mov	r1,-(sp)
	mov	r0,r1

10$:	movb	(r1)+,r0
	beq	20$
	jsr	pc,tychar
	br	10$

20$:	mov	r1,r0
	mov	(sp)+,r1
	rts	pc

; OUTSPC: has 3 blks of 5 words each (O/P), & 6 blks of 4 words each (I/P)

outspc:
out1:	.blkw	5.			;output stuff
out2:	.blkw	5.
out3:	.blkw	5.
in1:	.blkw	4.
in2:	.blkw	4.
inrest:	.blkw	<39.-4-4-5-5-5>		;don't care
linbf:	.blkb	82.			;rest of the line
linben:
filnam:	.asciz	/DK:SETLIN.SAV/
	.even
defext:	.rad50	/SAV            /	;default type
	.even
area:	.blkw	10.

block:	.word	0			;current read block
numblk:	.word	0			;number of blocks
buf:	.blkb	512.			;buffer
;
;
;
	; DELAY - delay routine
dlayvl	=754.
DELAY:	mov r0, -(sp)			;save count on stack
	mov #dlayvl, r0			;
delms1:	sob r0, delms1			;subtract and br if not equal zero
	mov (sp)+, r0			;restore count to r0
	sob r0, delay			;subtract and br if not equal zero
	rts pc				;
;
;			end called routines section
;====================================================================
	.page
;
	.nlist	bin			;do not list binary code
;
linbuf:	.blkb	81.
	.even
linbue:	.word	0
intro:	.ascii	\Enter option (HELP/MODIFY) \<200>
	.even
crlf:	.ascii	<15><12>
	.asciz	\\
	.even
;
;
deftx:	.asciz	\Set up defaults\
	.even
defer1:	.ascii	<15><12>\Line pair baud rate incompatibility:  \
defer2:	.ascii	\  - \
defer3:	.asciz	\ .  Configuration NOT implemented.\
	.even
defer4:	.ascii	<15><12>\NOTE - line \
defer5:	.ascii	\ 's partner, line \
defer6:	.asciz	\ , not configured.\
	.even
;
;			low byte  -mode
;			high byte -baud rate
config:	.word	177777			;line 0, console (N1=0)
					;
	.word	177777			;line 1, N2=0
	.word	177777			;line 2, N2=0
	.word	177777			;line 3, N2=0
	.word 	177777			;line 0 (4), N1=1, N2=0
					;
	.word	177777			;line 1, N2=1
	.word	177777			;line 2, N2=1
	.word	177777			;line 3, N2=1
	.word	177777			;line 0 (4), N1=1, N2=1
;
;			serial line csr base address table
csradr:	.word	177560			;line 0, console (N1=0)
					;
	.word	176500			;line 1, N2=0
	.word	176510			;line 2, N2=0
	.word	176520			;line 3, N2=0
	.word 	176530			;line 0 (4), N1=1, N2=0
					;
	.word	176540			;line 1, N2=1
	.word	176550			;line 2, N2=1
	.word	176560			;line 3, N2=1
	.word	176570			;line 0 (4), N1=1, N2=1
					;
csrade:					;csr address table end
;
	.page
;			line pairs cross reference table/list
xref:	.byte 2,3,0,1,2,7,8.,5,6
;
;			baud rate uniqueness list
;				a 1 indicates two different rates for the sameposition
;				in baud rate tables 1 and 2
;				a 0 indicates two rates the same for the same position
;				in baud rate tables 1 and 2
;			i.e. first position brt1=00050 and
;			                    brt2=00075 therefore uniqueness is 1
unique:	.byte 1,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1
	.even
;
;			receive and transmit register offsets
	rcsr	=0			;mode (two words)
	rbuf	=rcsr+2			;................
	xcsr	=rbuf+2			;baud (two words)
	xbuf	=xcsr+2			;................
					;
linptr:	.word	0			;serial line pointer/counter
uniqb:	.byte	0
brtb:	.byte	0
curtln:	.word	0
partln:	.word	0
	.page				;force page break
					;
modflg:	.word	0
modtxh:	.asciz	\Csr # Base address Baud  Bits/char Parity Stop bits\
	.even
modlin:	.ascii	\      \
modcsr:	.ascii	\             \
modbau:	.ascii	\      \
moddb:	.ascii	\          \
modpar:	.ascii	\       \
modsb:	.ascii	\            \<200>
modend:
notset:	.asciz	\Not Configured\
found:	.asciz	\CSR Found\
nfound:	.asciz	\CSR NOT Found\
	.even
tarea:	.word	0,0
modelo:	.word	0
baudhi:	.word	0
tab5:	.byte	0,5.,10.,15.,20.,25.,30.,35.,40.,45.,50.,55.,60.,65.,70.,75.

;				RCSR<15>=0
brt1:	.ascii	\00050\
	.ascii	\00050\
	.ascii	\00110\
	.ascii	\00134\
	.ascii	\00200\
	.ascii	\00300\
	.ascii	\00600\
	.ascii	\01200\
	.ascii	\01200\
	.ascii	\01050\
	.ascii	\02400\
	.ascii	\02400\
	.ascii	\04800\
	.ascii	\07200\
	.ascii	\09600\
	.ascii	\38400\
;				RCSR<15>=1
brt2:	.ascii	\00075\
	.ascii	\00075\
	.ascii	\00110\
	.ascii	\00134\
	.ascii	\00150\
	.ascii	\00300\
	.ascii	\00600\
	.ascii	\01200\
	.ascii	\01200\
	.ascii	\02000\
	.ascii	\02400\
	.ascii	\02400\
	.ascii	\04800\
	.ascii	\01800\
	.ascii	\09600\
	.ascii	\19200\

;
;
;
modhlp:	.ascii	<15><12>
	.ascii	\Enter ? at any prompt for help text or \<15><12>
	.asciz	\press ESC key at any prompt to exit\<15><12>
	.even
;
glinbf:	.blkb	81.
	.even
linp1:	.ascii	\CSR # to modify: \<200>
linp2:	.ascii	<15><12>\Baud      [\
linp2a:	.ascii	\     ]: \<200>
linp3:	.ascii	<15><12>\Bits/char [\
linp3a:	.ascii	\ ]    : \<200>
linp4:	.ascii	<15><12>\Parity    [\
linp4a:	.ascii	\ ]    : \<200>
linp5:	.ascii	<15><12>\Stop bits [\
linp5a:	.ascii	\ ]    : \<200>
linp90:	.ascii	<15><12>\Press RETURN key to continue \<200>
linp91:	.ascii	<15><12>\Do you want to save this sessions modifications? [N] \<200>
linp92:	.ascii	\Enter filename: \<200>
;
;
mhlp1a:	.ascii	<15><12>
	.ascii	\>>> enter the number of the CSR line whose configuration\<15><12>
	.ascii	\    you wish to modify\<15><12>
	.asciz	\\
mhlp2a:	.ascii	<15><12>
	.ascii	\>>> enter five digit baud rate required\<15><12>
	.ascii	\    valid baud rates are: 00050\<15><12>
	.ascii	\                          00075\<15><12>
	.ascii	\                          00110\<15><12>
	.ascii	\                          00134\<15><12>
	.ascii	\                          00150\<15><12>
	.ascii	\                          00200\<15><12>
	.ascii	\                          00300\<15><12>
	.ascii	\                          00600\<15><12>
	.ascii	\                          01050\<15><12>
	.ascii	\                          01200\<15><12>
	.ascii	\                          01800\<15><12>
	.ascii	\                          02000\<15><12>
	.ascii	\                          02400\<15><12>
	.ascii	\                          04800\<15><12>
	.ascii	\                          07200\<15><12>
	.ascii	\                          09600\<15><12>
	.ascii	\                          19200\<15><12>
	.asciz	\                          38400\<15><12>
mhlp3a:	.ascii	<15><12>
	.ascii	\>>> either 7 or 8 are valid character lengths\<15><12>
	.asciz	\\
mhlp4a:	.ascii	<15><12>
	.ascii	\>>> None, Even or Odd are valid parity settings\<15><12>
	.asciz	\\
mhlp5a:	.ascii	<15><12>
	.ascii	\>>> either 1 or 2 are valid for number of stop bits\<15><12>
	.asciz	\\
mhlp10:	.ascii	<15><12>\>>> This sessions modification will be lost upon system\<15><12>
	.ascii	\    restart.  You may replace the existing default serial\<15><12>
	.ascii	\    line parameters in SETLIN.SAV with this sessions\<15><12>
	.ascii	\    modifications or create a new .SAV file\<15><12>
	.asciz	\\
mhlp11:	.ascii	<15><12>\>>> Enter a 1 to 6 alphabetic uppercase filename.\<15><12>
	.ascii	\    The device will be the system device and extension\<15><12>
	.ascii	\    will be SAV\<15><12>
	.asciz	\\
;
mhlpab:	.asciz	<15><12>\>>> Procedure aborted.  Last modification not saved\
invald:	.asciz	<15><12>\>>> Invalid response.  Try again\
;
	.even
	.page				;force page break
;
;
hlptx1:	.ascii	\******************************************************************\<15><12>
	.ascii	\SETLIN - An RT-11 macro program to set/alter values for the four\<15><12>
	.ascii	\serial lines on the WEBSTER COMPUTER CORPRATION's\<15><12>
	.ascii	\PCLV11-J (MFU) board.  Users should be familiar with the\<15><12>
	.ascii	\SETLIN.DOC file on the distribution media and the\<15><12>
	.ascii	\PCLV11-J manual.\<15><12>
	.ascii	<15><12>
	.ascii	\SETLIN overrides any initial serial lines values provided\<15><12>
	.ascii	\by the MFU boot PROM.  These lines may be located at one\<15><12>
	.ascii	\of two locations.  In addition the fourth line may be\<15><12>
	.ascii	\defined as the console for a total of nine locations.  The values\<15><12>
	.ascii	\for each line are the baud rate, number of characters, parity and\<15><12>
	.ascii	\number of stop bits.\<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.asciz	\******************************************************************\
;
hlptx2:	.ascii	\******************************************************************\<15><12>
	.ascii	\Invoking the SETLIN program\<15><12>
	.ascii	<15><12>
	.ascii	\1)  Manually at the RT-11 command level by entering: SETLIN or\<15><12>
	.ascii	\                                                     R SETLIN or\<15><12>
	.ascii	\                                                     RUN SETLIN to\<15><12>
	.ascii	\    alter the default values within the SETLIN program.\<15><12>
	.ascii	<15><12>
	.ascii	\2)  Via a command file to establish SETLIN's default values on\<15><12>
	.ascii	\    the PCLV11-J serial lines.  Enter: SETLIN DEF or\<15><12>
	.ascii	\                                       R SETLIN DEF or\<15><12>
	.ascii	\                                       RUN SETLIN DEF.\<15><12>
	.ascii	\    It is anticipated that this will form part of the RT-11\<15><12>
	.ascii	\    STARTS.COM file\<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.asciz	\******************************************************************\
;
hlptx3:	.ascii	\******************************************************************\<15><12>
	.ascii	\Modify the default values in SETLIN.SAV.\<15><12>
	.ascii	<15><12>
	.ascii	\enter: SETLIN or R SETLIN or RUN SETLIN\<15><12>
	.ascii	\receive: Enter option (HELP/MODIFY)\<15><12>
	.ascii	<15><12>
	.ascii	\enter: HELP\<15><12>
	.ascii	\receive: help text\<15><12>
	.ascii	<15><12>
	.ascii	\enter: MOD\<15><12>
	.ascii	\receive: display of SETLIN.SAV's existing default values.  Operator\<15><12>
	.ascii	\         selects CSR line to modify and responds to prompts.  Default\<15><12>
	.ascii	\         answers are enclosed in [ ] and operator presses RETURN to\<15><12>
	.ascii	\         accept default answer.  Modifications are implemented\<15><12>
	.ascii	\         immediately and operator is again invited to either select\<15><12>
	.ascii	\         another CSR line to modify or exit.  Upon exit operator may\<15><12>
	.ascii	\         save the sessions modifications as a new version of\<15><12>
	.ascii	\         SETLIN.SAV or to another .SAV file.\<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.asciz	\******************************************************************\
;
hlptx4:	.ascii	\******************************************************************\<15><12>
	.ascii	\Set the PCLV11-J serial lines to SETLIN.SAV's default values\<15><12>
	.ascii	<15><12>
	.ascii	\enter: SETLIN DEF or R SETLIN DEF or RUN SETLIN DEF\<15><12>
	.ascii	\receive: next command in command file or RT-11 dot.  The SETLIN\<15><12>
	.ascii	\         program does not produce any output when the default option\<15><12>
	.ascii	\         is invoked\<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.ascii	<15><12>
	.asciz	\******************************************************************\
;
	.list	bin
;
handlr:					;place to put handlers
;
	.END SETLIN
