.Title	BASE - template programme for easier user programming
.enabl	lc
.nlist	bex,toc

.SBTTL	V2	 4-Aug-83	CVLLIB Routine
;Adjust for new IO2
.SBTTL	V1	13-Jun-82

		.If eq 1
Author:	C G Wilson, 71 Galatea St., Charleville 4470, Australia.

Provides a general-purpose I/O template for RT11 users, with double-buffered
I/O on output to channel 0 and on input sequentially from channels 3-10.

****************************************************************
Designed to be assembled with CMAC.MAC, and linked with IO2.OBJ.
****************************************************************

[IO2 requires globals area, errnam, and error.]
		.Endc

.Mcall	.settop,.qset,.wait,.close
					.psect
					.psect	mesage	con
Deftyp::.rad50	/DATLST      /	;Default input & output extensions

Vermes::.Asciz	/Base V1/
Errnam::.Ascii	/?Base-F-/<200>
					.psect
; Globals from IO2 I/O package:

.Globl	setibf,setobf,rdbuf,ouclrb,inbyt,oubyt
.Sbttl	Data Locations

spsav::	0			;For saving SP for re-runs
area::	.blkw	6		;Programmed request workspace.
numbuf:	1			;Number of blocks per I/O buffer

limit::	.limit			;Low and High Program Limits (2 words)
hlimit::0			;Limit after buffers allocated

qarea::.blkw	60.		;Queue area, allowing for XM
.SBTTL	Start - initialization section

START::
	mov	sp,spsav
	br	rerun
ERROR::
	bisb	#error$,@#userrb	;Error causes prang if command file
	.rctrlo				;Called by Call Error, .word Message
	.print	#errnam
ERROK::	.rctrlo				;Here if don't prang command file
	.print	@(sp)
RERUN::
	mov	spsav,sp
	.sreset
	.rctrlo
	.settop	limit+2

	.qset	#qarea,#6	;Allocate room for 6 queue elements

	mov	#1,numbuf	;Initially set for 1-block buffers

	.csigen	limit+2,#deftyp ;Free space
	mov	r0,hlimit

	mov	(sp)+,r5	;# of switches
	beq	endswt		;None specified


; Decode the next switch:

Nexswt:
	clr	r2		;In case no value.
	mov	(sp)+,r1	;b15 set if value specified, b14-8 file on
				; which switch specified, b7-0 ascii value
	bpl	5$		; of switch.
	mov	(sp)+,r2	;Value
5$:	bic	#40,r1		;In case lower case

	mov	#swtab,r4	;Table of <switch><routine>
6$:
	tst	(r4)		;Null = end of table.
	beq	illswt
	cmpb	r1,(r4)		;See if it's this switch
	beq	9$		;It is.
	cmp	(r4)+,(r4)+	;It isn't.  Try the next one.
	br	6$
9$:
	call	@2(r4)		;Call relevant routine
	dec	r5		;Any more switches?
	bne	nexswt		;Yes.
	jmp	endswt		;No.

Illswt:
	call	error
	ref	<"Illegal Switch.  Type /H for help"<0>>
.Sbttl	Switch Table & Help Message

;Entries in this table are:

;	.word	<upper case value of switch>
;	.word	<address of routine to handle that switch>
; The table is terminated by a null.

Swtab:
	.word	'H,help

	.word	0

Help:
	call	errok
	.word	hmes

	.psect	mesage	con
hmes:
.ascii	#Switches available are:#<cr><lf><lf>
.ascii	#/H	type this message#<cr><lf>
.byte	0
	.psect
.Sbttl	Switch decoding routines

;Each switch decoding routine is called with the following information:

; R1/	b15 set if value specified; b14-8 file on which switch specified;
;	low order byte (b 7-0) ascii value of switch with lower-case bit clear





Endswt:
	.wait	#0
	bcc	10$		;If no output channel
	.wait	#3		; or input channel,
	bcc	10$
	.print	#vermes		; just type ident.
	jmp	rerun
10$:
	mov	numbuf,r2	;Number of blocks per I/O buffer
	mov	hlimit,r1	;R1 has start of buffer space

	call	setibf		;Set up input buffers
	  bcs	nobuf
	call	setobf		;Set up output buffers
	  bcs	nobuf
	mov	r1,hlimit	;Check that we've got room!
	.settop	r1
	cmp	r0,r1
	  bne	nobuf

	jmp	proces

; **** Endfil **** Close output file, and set up for rerun.

Endfil::
	.wait	#0		;Don't close output file if there weren't one
	bcs	10$
	call	ouclrb
	.settop	limit+2		;Allow USR back in before closing output file.
	.close	#0
10$:	jmp	rerun


Nobuf:	call	error
	ref	</?-F-Insufficient Memory/<0>>
.Sbttl	Processing

Proces:

;
;
;
;
;Insert processing here.
;	inbyt - returns a byte into r0
;	oubyt - sends a byte from r0
;	ouclrb - sends current output buffer
;
	jmp	endfil		;Close output file, go back for next load.

.End	Start
