.Title	JREAD - Reconstructed JREAD/JWRITE Routines
.Nlist	bex,toc
.Enabl	lc

.Sbttl	V1	16-Jan-88

op.wr=42	;WRITE, MSCP opcode
op.rd=41	;READ, ibid

errbyt=52	;Monitor Error Byte


JWRITC::
	mov	#op.wr,r3
	br	jc
JREADC::
	mov	#op.rd,r3
jc:
	mov	14(r5),r4	;Completion routine address
	br	$wrkck

JWRITE::
	mov	#op.wr,r3
	br	j
JREAD::
	mov	#op.rd,r3
j:
	mov	#1,r4		;Code for no waiting
	br	$wrkck

JWRITW::
	mov	#op.wr,r3
	br	jw
JREADW::
	mov	#op.rd,r3
jw:
	clr	r4		;Code for wait-mode I/O

$WRKCK::
	mov	(r5)+,r2	;Number of Fortran Arguments
	cmpb	#5,r2
	bgt	10$

	mov	10(r5),r1	;Address of MSCP Buffer Area
	cmp	#-1,r1		;If -1, get one from stack
	beq	10$

	add	#236,r1		;Point to the end of it
	clr	-(sp)		;Value to add to stack at end to correct it
	br	20$
10$:
	mov	sp,r1		;Get area from stack for MSCP buffer
	sub	#236,sp
	mov	#236,-(sp)
20$:
	mov	r3,-(r1)	;MSCP Opcode
	mov	r4,-(r1)	;Completion Routine Address/0/1
	mov	r1,r2		;Hold pointer to this area.

	mov	@6(r5),r3	;Channel
	clr	-(r1)		;(for address for CSTAT)
	mov	r3,-(r1)
	bis	(pc)+,(r1)
	.byte	0,27		;EMT value for CSTAT
	mov	r1,r0
	sub	#14,r1		;Need 7 words for CSTAT information
	mov	r1,2(r0)	;Address thereof
	emt	375		;.CSTAT
	bcc	30$

	mov	#2,r0		;Channel not in use.
	br	40$
30$:
	mov	12(r1),r4	;Rad50 name of device
	cmp	#^rDU,r4
	beq	50$

	mov	#3,r0		;Illegal device
40$:	jmp	error

50$:
	swab	r3		;Channel,,0
	bisb	10(r1),r3	;Channel,,Unit
	swab	r3		;Unit,,Channel
	mov	r2,r1		;Back pointing to where opcode & comp went
	mov	r4,-(r1)	;^rDU
	mov	r1,r0
	sub	#10,r1		;Room for DSTATUS
	mov	r1,-(sp)
	emt	342		;.DSTATUS
	bcc	60$

	mov	#3,r0
	jmp	error
60$:
	mov	r2,r1
	clr	-(r1)
	movb	#372,r0
	swab	r0
	bis	#377,r0		;377,,372
	mov	r0,-(r1)
	clr	-(r1)
	clr	-(r1)
	clr	-(r1)
	mov	(pc)+,-(r1)
	.byte	0,32		;Code for SPFUN
	bisb	r3,(r1)		;Channel number
	mov	r1,r0
	sub	#40,r1
	mov	r1,4(r0)
	emt	375		;.SPFUN
	bcc	70$

	movb	@#errbyt,r0
	jmp	error
70$:
	swab	r3
	clr	r0
	bisb	r3,r0
	asl	r0
	asl	r0
	cmp	(r0)+,(r0)+
	add	r0,r1
	mov	(r1)+,r4
	movb	(r1),-(sp)
	bic	#177400,(sp)
	mov	r2,r1
	mov	2(r1),r3
	movb	#371,r0
	swab	r0
	bis	#377,r0
	mov	r0,-(r1)
	mov	@(r5)+,r2
	mov	(r5)+,-(r1)
	clr	-(r1)
	mov	#1,-(r1)
	mov	(pc)+,-(r1)
	.byte	0,32		;SPFUN Code
	mov	r1,r0
	mov	r2,-152(r0)

	mov	#64,r2
80$:	clr	-(r1)
	dec	r2
	bne	80$

	mov	r1,4(r0)
	mov	r1,-154(r0)
	mov	#60,(r1)+
	mov	#0,(r1)+
	add	#60,r1
	mov	#60,(r1)+
	mov	#0,(r1)+

	mov	r4,4(r1)
	mov	(r5)+,r4
	mov	(r4)+,34(r1)
	mov	(r4)+,36(r1)
	add	(sp)+,36(r1)
	bcs	error1

	bis	@(r5)+,(r0)
	mov	-152(r0),r4
	asl	r4
	mov	r4,14(r1)
	mov	r3,10(r1)
	mov	-154(r0),r3
	emt	375			;.SPFUN
	bcc	90$

	movb	@#errbyt,r0
	br	error
90$:
	movb	16(r3),r0
	bne	error1

	mov	20(r3),r0
	clc
	ror	r0
	br	home

error1:
	mov	#1,r0
error:
	com	r0
home:
	add	(sp)+,sp
	return

.end
