	.TITLE	READ BINARY, WRITE BINARY
/ 
/ 
/                   FIRST PRINTING, FEBRUARY 1974
/ 
/ THE INFORMATION IN THIS DOCUMENT IS SUBJECT TO 
/ CHANGE WITHOUT NOTICE AND SHOULD NOT BE CONSTRUED
/ AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
/ DIGITAL EQUIPMENT CORPORATION ASSUMES NO RESPON-
/ SIBILITY FOR ANY ERRORS THAT MAY APPEAR IN THIS
/ DOCUMENT.
/ 
/ THE SOFTWARE DESCRIBED IN THIS DOCUMENT IS FUR-
/ NISHED TO THE PURCHASER UNDER A LICENSE FOR USE ON
/ A SINGLE COMPUTER SYSTEM AND CAN BE COPIED (WITH
/ INCLUSION OF DIGITAL'S COPYRIGHT NOTICE) ONLY FOR 
/ USE IN SUCH SYSTEM, EXCEPT AS MAY OTHERWISE BE PRO-
/ VIDED IN WRITING BY DIGITAL.
/ 
/ DIGITAL EQUIPMENT CORPORATION ASSUMES NO RESPONSIBILITY
/ FOR THE USE OR RELIABILITY OF ITS SOFTWARE ON EQUIP-
/ MENT THAT IS NOT SUPPLIED BY DIGITAL.
/ 
/ COPYRIGHT (C) 1974, BY DIGITAL EQUIPMENT CORPORATION
/ 
/ 
        .EJECT
/
/	EDIT # 15
/
/ COPYRIGHT 1971,1972,1973 DIGITAL EQUIPMENT CORP., MAYNARD, MASS.
/
/ MAGTAPE I/O HANDLER TASK		4 APR 73	R. MCLEAN
/					M. HEBENSTREIT
/
/ READB READS BINARY RECORDS FROM MAGTAPE (MODES 0,1)
/
/ 	IOPS BINARY (MODE 0) IS READ WITH THE HEADER
/		WORD PAIR INCLUDED TO PROVIDE THE USER SOME ASSURANCE
/		THAT THE IMAGE OBTAINED IS CORRECT.
/	IMAGE BINARY HEADERS ARE COMPUTED BY THE HANDLER
/		AND RETURNED TO THE USER.
/
/	ALL BINARY MODES ARE READ IN ODD PARITY UNLESS A FORMAT
/		OVERRIDES THE DEFAULT PARITY.
/
/
	.GLOBL	MTRWB,FINREQ,FLSREQ,WFMT,MTEVAM,MTNODE,MTCAA,MTWCA,FMTDEN,FMTPAR
	.GLOBL	UNTTMP,MTEOTT,ERM12
/
/
WCA	32
CAA	33	/WORD COUNT ADDRESS
	.TITLE	PARAMETER DEFINITIONS
/
/	PARAMETER DEFINITIONS
/
R1=101
R2=102
R3=103
R4=104
R5=105
POOL=240	/POOL DEQUE LIST HEAD
PICK=120	/PICK A NODE
PDVL=252	/PHYSICAL DEVICE DEQUE LIST HEAD
NADD=107	/NODE ADD
SNAM=123	/SEARCH FOR A NAME
DQRQ=337	/DE-QUEUE A NODE FROM PDVL NODE
ALAD=325	/ATTACH LUN AND DEVICE
DLAD=332	/DETACH LUN AND DEVICE
VAJX=342	/VALADATE AND ADJUST
X10=10
X11=11
X12=12
/
.INH=705522
.ENB=705521
ECLA=641000
/
MTSF=707341	/SKIP ON ERROR FLAG OR MAGTAPE FLAG
MTCR=707321	/SKIP ON TAPE CONTROL READY
MTTR=707301	/SKIP ON TAPE TRANSPORT READY
MTAF=707322	/CLEAR THE STATUS AND COMMAND REGISTERS, EF AND MTF
		/IF TAPE CONTROL READ. IF NOT READY CLEAR EF AND MTF ONLY.
MTVM=707302	/OR COMMAND REGISTER INTO AC 0-11
MTCM=707324	/OR AC 0-5,9-11 INTO COMMAND REGISTER LOAD 6-8
		/INTO COMMAND REGISTER
MTLC=707326	/LOAD AC 0-11 INTO COMMAND REGISTER
MTVS=707342	/INCLUSIVELY OR STATUS REGISTER INTO AC 0-11
MTRS=707352	/READ THE CONTENTS OF STATUS REGISTER INTO AC 0-11
MTRC=707312	/READ THE CONTENTS OF COMMAND REGISTER INTO AC 0-11
MTGO=707304	/EXECUTE THE COMMAND REGISTER
/
EF=400000	/ERROR FLAG
RW=200000	/TAPE REWINDING
BT=100000	/BEGINNING OF TAPE
IC=040000	/ILLEGAL COMMAND
PE=020000	/PARITY ERROR
FI=010000	/EOF
ET=004000	/EOT
RE=002000	/READ-COMPARE ERROR
IL=001000	/RECORD LENGTH INCORRECT
DL=000400	/DATA REQUEST LATE
TB=000200	/BAD TAPE
EP=000000+1	/EVEN PARITY
OP=040000+1	/ODD PARITY
CD=020000+1	/CORE DUMP MODE
IG=010000	/INTER - RECORD GAP
NP=000000	/MT NOP
RI=001000	/REWIND
RD=002000	/READ
RC=003000	/READ - COMPARE
WR=004000	/WRITE
WF=005000	/WRITE END OF FILE
SF=006000	/SPACE FORWARD
SR=007000	/SPACE REVERSE
IE=000400	/INTERRUPT ENABLE
ID=000000	/INTERRUPT DISABLE
D2=000000+1	/DENSITY 200 BPI
D5=000100+1	/DENSITY 556 BPI
D8=000200+1	/DENSITY 800 BPI
D9=000300+1	/DENSITY 800 BPI 9 TRACK
/
MTRWB	0
	DAC	MODSAV	/SAVE THE MODE OF THE REQUEST
	SNL		/SEPARATE READ AND WRITE REQUEST'S
	JMP	WRITB	/WRITE A RECORD
	.TITLE	READ BINARY
/
/
/	READB	-- READS BINARY RECORD.  ENTERED WITH AC = -1
/		FOR AN IOPS BINARY RECORD.  ENTERED WITH AC = 0 
/		FOR AN IMAGE BINARY RECORD.
/
READB	LAC	(READB)	/CALCLULAE THE XR ADJUSTMENT
	AND	(070000)
	TCA
	DAC	XADJ
	TAD*	MTNODE	/PICK UP THE REQUESTOR'S NODE ADDRESS
	PAX		/SAVE IT IN XR
	DZM	ERR	/CLEAR THE ERROR IN READ FLAG
	LAC	MODSAV	/SETUP THE MODE
	RAL		/0=IOPS 1=IMAGE
	LAC	10,X	/PICK UP THE I/O ADDRESS
	DAC	BUFADR
	DAC*	(R3)	/SET UP TO VALADATE
	LAC*	MTNODE	/AND ADJUST
	DAC*	(R2)
	LAC	11,X	/PICK UP MAX NUMBER OF WORDS
	AAC	-2	/TEST THE SIZE OF THE BUFFER >2 ILLEGAL
	SPA!SNA
	JMP	ERM16	/BUFFER TOO SMALL ERROR
	SZL		/TWO LESS IF IMAGE BINARY
	AAC	2
	DAC*	(R4)	/SAVE FOR VAJX
	TCA		/MAKE WORD COUNT NEGATIVE
	DAC*	WCA	/SAVE IN WORD COUNT
	DAC*	MTWCA	/SAVE IN CASE OF RETRIES AND FOR INCORRECT RECORD LENGTH ERROR
	JMS*	(VAJX)	/FINALLY VALADATE AND ADJUST
	JMP	ERM30	/PROTECT ERROR
	LAC	MODSAV	/RESTORE THE LINK
	RAL
	LAC*	(R3)	/PICK UP THE BUFFER ADDRESS
	DAC	BUFADR	/SAVE IT
	AAC	-1	/SUBTRACT 1 FOR THE CURRENT ADDRESS POINTER
	SNL		/IF LINK IS SET WORD COUNT OK -- INCLUDE HEADER WORD PAIR
	AAC	2	/IMAGE BINARY -- SUBTRACT HEAD WORD PAIR FROM THE CURRENT ADDRESS POINTER
	DAC*	CAA	/SAVE IN CURRENT ADDRESS POINTER
	DAC*	MTCAA	/SAVE FOR RETRY'S
	LAC*	UNTTMP	/PICK UP THE UNIT NUMBER TO
	PAX		/TO DETERMINE PARITY AND DENSITY
	ALS	17	/PUT UNIT NUMBER IN BITS 0-2
	AND	(700000) /MAGTAPE COMMAND
	LMQ
	LAC*	MTEOTT,X /CHECK FOR EOT FLAGS
	SMA
	JMP	ERM21	/YES -- EOT CAN'T DO A READ
	LAC*	FMTDEN,X /PICK UP THE DENSITY
	SNA		/DEFAULT?
	LAC	(D8)	/YES -- 7 TRACK 800 BPI
	OMQ		/OR IT INTO COMMAND
	LMQ		/SAVE IT BACK IN MQ
	LAC*	FMTPAR,X /PICK UP THE PARITY OF THIS DRIVE
	SZA		/IS IT DEFAULT?
	JMP	RDN7T	/NO USE GIVEN FORMAT
	LACQ		/CHECK FOR 9 TRACK
	AND	(D9-1)	/MASK OFF DENSITY
	SAD	(D9-1)	/IS IT 9 TRACK?
	SKP		/YES ADD CORE DUMP MODE
	SKP!CLA		/NO -- ONLY ADD ODD PARITY
	LAC	(CD)	/PICK UP CORE DUMP MODE
	TAD	(OP)	/ADD ODD PARITY BITS
RDN7T	OMQ		/OR IT INTO COMMAND
	XOR	(RD!IE)	/OR IN READ AND INTERRUPT ENABLE
	JMS*	WFMT	/READ A RECORD
	LAC	BUFADR	/PICK UP THE BUFFER ADDRESS
	DAC*	(X11)	/SAVE FOR IMAGE MODE
	AAC	-1
	DAC*	(X10)	/SAVE FOR CHECKSUMMING
	LAC*	MTEVAM	/PICK UP THE READ EVENT VARIABLE ADDRESS
	SMA		/IS THER AN ERROR
	JMP	CKSUMA	/	CHECKSUM THE FILE 
	LLS	4	/LOOK AT THE ERROR STATUS
	SPA!RAL		/WAS IT AN END OF FILE?
	JMP	PARERR	/PARITY ERROR SET BITS
	SPA!RAL		/WAS THIS EOF?
	JMP	EOF	/YES -- GO RETURN AN END OF FILE HEADER AND AN END OF FILE EVENT VARIABLE
	RTL
			/LOOK FOR INCORRECT RECORD LENGTH
	SPA
	JMP	INCRCL	/YES -- CHECK TO SEE IF RECORD TOO LONG
	AND	(300000) /MASK OFF REMAINING VALID BITS
	SZA		/OTHER ERRORS?
	JMP*	ERM12	/YES RETURN -12
	JMP	CKSUMA	/NO --  CHECKSUM THE RECORDS
/
ERM16	LAW	-16
	JMP*	FLSREQ
/
/	EOF  -- END OF FILE REACHED RETURN HEADER WORD PAIR OF
/		1005,-1005 AND AN EVENT VARIABLE OF +2 TO INDICATE
/		TO THE USER THAT AN END OF FILE HAS BEEN FOUND
/
EOF	LAC	(1005)	/SET UP EOF INDICATOR
	DAC*	X10	/SET IN BUFFER
	TCA		/COMPLEMENT
	DAC*	X10	/STORE IN USER'S CHECKSUM
	LAC*	MTEVAM	/PICK UP THE STATUS OF THE READ
	AND	(ET)	/WAS IT AN EOT?
	SZA
	JMP	EOT	/YES REPORT EOT AND SET TABLE
	AAC	3	/RETURN EVENT VARIABLE OF +3
	JMP*	FLSREQ	/RETURN TO USER
/
CKSUMA	LAC	MODSAV	/PICK UP THE MODE (IMAGE OR IOPS)
	SNA		/IS IT IMAGE?
	JMP	SETV	/YES IMAGE
CKSUMB	LAC*	X10	/COMPUTE CHECKSUM AND HEADER WORD PAIR
	DAC	CKSUM	/SET UP THE CHECKSUM
	SWHA
	AND	(177)	/MASK OFF THE WORD COUNT
	TCA		/MAKE WORD COUNT NEGATIVE
	DAC	WPCK	/SAVE IT
	SNA		/AVOID ZERO CASE.
	JMP	ERM5
	TCA		/TWOS COMPLEMENT
	CLL!RAL		/CHECK FOR VALID WP COUNT. (TIMES 2 = WC).
	TAD*	MTWCA
	SMA!SZA
	JMP	ERM5
	LAC	CKSUM
	SKP		/START CHECKSUM ON SECOND WORD
	TAD*	X10
	TAD*	X10
	ISZ	WPCK	/INCREMENT WORD PAIR COUNT
	JMP	.-3	/FINISHED?
	SZA		/YES -- CHECKSUM ERROR?
	JMP	CKSMER	/YES REPORT AN ERROR IN THE HEADER WORD PINR
SETV	LAC	BUFADR	/SET UP THE HEADER
	TAD	XADJ
	PAX
	LAC	MODSAV	/PICK UP THE MODE OF THE REQUEST
	SNA		/IS IT IMAGE
	JMP	SETIMG	/NO -- COMPUTE THE HEADER WORD
	LAC	0,X	/YES -- PICK UP THE HEADER
	JMP	SETER	/GO SET THE STATUS IN THE HEADER
SETIMG	LAC*	MTWCA	/PICK UP THE WORD COUNT
CKIMBG	TCA		/MAKE IT POSITIVE
	AAC	2
	RAR!CLL		/MAKE IT A WORD PAIR COUNT
	SWHA		/PUT IT INTO THE WORDCOUNT POSITION
	IAC		/IMAGE = 1
SETER	AND	(777717) /MASK OFF THE VALIDITY BITS
	XOR	ERR	/ADD IN THE ERROR CONDITION
	DAC	0,X	/SET IT IN THE BUFFER
	JMP	ETCHK	/REQUEST FINISHED CHECK FOR EOT FLAG
/
PARERR	LAC	(20)	/ON PARITY ERROR DON'T CHECKSUM
	DAC	ERR	/SET UP HEADER FLAGS
	JMP	SETV	/SET IT IN USER'S BUFFER
/
CKSMER	LAC	(40)	/CHECKSUM ERROR
	DAC	ERR	/SET UP HEADER FLAGS
	JMP	SETV	/SET IT IN USER'S BUFFER
/
RECLNG	LAC	MODSAV	/CHECK THE MODE OF THE REQUEST
	SZA		/IS IT IMAGE?
	JMP	RECIOP	/NO -- MUST BI IOPS ASCII
	LAC*	MTWCA	/PICK UP THE WORD COUNT
	TCA
	RAR!CLL		/MAKE IT WORDPAIR COUNT
	IAC
	SWHA
	IAC		/ADD IMAGE BIT
	DAC*	X10	/PUT IT IN USER'S HEADER
RECIOP	LAC	(60)	/INCORRECT RECORD LENGTH
	DAC	ERR
	JMP	SETV
/
/	INCRCL	-- INCORRECT RECORD LENGTH ERROR  NOT AN ERROR IF
/		BUFFER TO LONG ONLY IF BUFFER IS TOO SHORT
/
INCRCL	LAC*	WCA	/CHECK THE WORD COUNT
			/TO DETERMINE IF RECORD SHORT
	SNA
	JMP	RECLNG	/IF NONZERO ALL OK
	PAL
	LAC	MODSAV	/PICK UP THE MODE OF THE REQUEST RECORD OK
	SZA		/IS IT IMAGE?
	JMP	CKSUMB	/NO
	LAC	BUFADR	/PICK UP THE ADDRESS OF THE BUFFER
	TAD	XADJ	/SET UP TO USE XR
	PAX
	PLA		/RESTORE THE COUNT
	TCA
	TAD*	MTWCA	/SUBTRACT THE SET UP COUNT
	JMP	CKIMBG	/GO COMPUTE CHECKSUM
/
/
/	ETCHK -- CHECKS FOR EOT ON FORWARD MOTION
/
ETCHK	LAC*	MTEVAM	/WAS THERE AN EOT?
	AND	(ET)
	SNA
	JMP*	FINREQ	/NO MUST BE OK
	LAC	(4)	/YES RETURN EOT FLAG
	JMP*	FLSREQ
/
	.TITLE	WRITE BINARY
/ WRITEB WRITES BINARY RECORDS FROM MAGTAPE (MODES 0,1)
/
/ 	IOPS BINARY (MODE 0) IS WRITTEN WITH THE HEADER
/		WORD PAIR INCLUDED TO PROVIDE THE USER SOME ASSURANCE
/		THAT THE IMAGE OBTAINED IS CORRECT.
/	IMAGE BINARY HEADERS ARE COMPUTED BY THE HANDLER
/		AND RETURNED TO THE USER.
/
/	ALL BINARY MODES ARE WRITTEN IN ODD PARITY UNLESS A FORMAT
/		OVERRIDES THE DEFAULT PARITY.
/
/
/
/
/
/
/	WRITEB	-- WRITES BINARY RECORD.  ENTERED WITH AC = -1
/		FOR AN IOPS BINARY RECORD.  ENTERED WITH AC = 0 
/		FOR AN IMAGE BINARY RECORD.
/
WRITB	LAC	(WRITB)	/SET UP THE XR ADJUSTMENT
	AND	(070000) / MASK OFF THE PAGE BITS
	TCA
	TAD*	MTNODE	/PICK UP THE REQUEST NODE ADDRESS
	PAX
	DAC	XADJ	/SAVE NODE XR POINTER IN XADJ !!!!!!
	LAC	10,X	/PICK UP THE BUFFER ADDRESS
	DAC*	(R3)	/SET UP FOR VALADATE AND ADJUST
	DAC	BUFADR	/SAVE THE BUFFER ADDRESS FOR 2ND VALADATE
	LAC*	MTNODE	/ADDRESS OF NODE
	DAC*	(R2)
	CLA!IAC		/VALADATE 1 WORD
	DAC*	(R4)
	JMS*	(VAJX)	/VALADATE
	JMP	ERM30	/PROTECT ERROR
	LAC	MODSAV	/PUT THE MODE IN THE LINK
	RAL
	LAC*	(R3)	/ADJUSTED ADDRESS
	AAC	-1	/SUBTRACT 1 FOR AUTO XR'S
	DAC*	(X10)
	DAC*	(X11)
	DAC*	(X12)	/SET UP RE-ENTRANT XR'S FOR CHECKSUMMING
	SNL		/SET UP THE WORD COUNT REGISTER
	AAC	2	/ADD 2 IF IMAGE BINARY
	DAC*	CAA
	DAC*	MTCAA	/SAVE IN CASE OF RETRIES
	LAC	BUFADR	/RESET R3 FOR VALADATE AND ADJUST
	DAC*	(R3)
	LAC*	X12	/PICK UP THE FIRST WORD
	SWHA		/LOOK AT THE WORD PAIR COUNT
	AND	(177)
	SNL		/IOPS BINARY?
	AAC	-1	/NO -- IMAGE BINARY
	TCA
	DAC	CKSUM	/SET UP CHECKSUM COUNT
	TCA
	CLL!RAL		/FIND WORD COUNT
	DAC*	(R4)	/SET WORD COUNT FOR VALADATE
	TCA		/COMPLEMENT WORD COUNT TO MAKE NEGATIVE
	DAC*	WCA
	DAC*	MTWCA	/SET UP ERROR RETRIES
	JMS*	(VAJX)	/VALADATE AND ADJUST
	JMP	ERM30	/PROTECT ERROR
	LAC	MODSAV	/GET THE MODE
	SNA		/READ TO START TRANSFER?
	JMP	MTSTRT	/YES -- IMAGE BINARY TRANSFER
	LAC	XADJ	/RESTORE THE XR
	PAX
	LAC*	X11	/NO -- MUST CHECKSUM FIRST
	AND	(777000) /RESET THE MODE INDICATOR
	XOR	7,X	/OR IN THE MODE
	DAC*	X10	/SET UP IN THE USER'S BUFFER
	DAC*	X10	/SET ALSO IN THE CHECKSUM SINCE THIS WILL CHANGE
	JMP	CKSMLP	/CHECKSUM THE REST OF THE BUFFER
	TAD*	X10
	TAD*	X10
CKSMLP	ISZ	CKSUM	/CHECK FOR END OF BUFFER
	JMP	.-3	/NOT END TRY AGAIN
	TCA		/COMPLEMENT CHECK SUM
	DAC*	X11	/STORE IN CHECKSUM IN USER'S BUFFER
MTSTRT	LAC*	UNTTMP	/PICK UP THE UNIT NUMBER BITS 15-17
	PAX		/SAVE IT IN XR
	ALS	17	/PUT UNIT INTO BITS 0-17
	AND	(700000) /MASK OFF UNIT
	LMQ		/STORE IT IN MQ
	LAC*	MTEOTT,X /CHECK THE END OF TAPE FLAGS
	SMA		/CAN FORWARD SPACING BE DONE?
	JMP	ERM21	/NO SET EV TO -21
	LAC*	FMTDEN,X /PICK UP THE DENSITY OF THIS REQUEST
	SNA		/IS IT DEFAULT?
	LAC	(D8)	/YES SET TO 7 TRACK 800 BPI
	OMQ		/OR IT INTO AC
	LMQ		/RESTORE SUM INTO MQ
	LAC*	FMTPAR,X /PICK UP THE PARITY OF THE UNIT
	SZA		/IS IT DEFAULT PARITY?
	JMP	NT9TRK	/NO -- USE GIVEN PARITY
	LACQ		/PICK UP THE DENSITY
	AND	(D9-1)	/MASK OFF DENSITY
	SAD	(D9-1)	/IS IT 9 TRACK?
	SKP		/YES ADD CORE DUMP MODE
	SKP!CLA		/NO ONLY ADD ODD PARITY
	LAC	(CD)	/PICK UP CORE DUMP MODE
	TAD	(OP)	/ADD ODD PARITY
NT9TRK	OMQ		/OR IT INTO COMMAND
	XOR	(WR!IE)	/WRITE INTERRUPT ENABLEED
	JMS*	WFMT	/START MAGTAPE REQUEST
	LAC*	MTEVAM	/WAS THE TRANSFER OK?
	SMA
	JMP*	FINREQ	/YES -- REQUEST COMPLETE
	AND	(ET)	/NO --OR MAYBE NOT -- EOT?
	SNA
	JMP*	ERM12	/FATAL ERROR EVENT VARIABLE -12
/
/	EOT -- FOUND DURING A READ OR WRITE
/		SET EOT FLAGS IN TABLE AND RETURN EV + 3
/
EOT	LAC*	UNTTMP	/FIND THE UNIT NUMBER TO SET UP THE
	PAX		/EOT TABLE
	DZM*	MTEOTT,X /INDICATE EOT FOUND NO FORWARD MOTIONS ACCEPTED
	LAC	(4)	/RETURN +3 IN USER'S EVENT VARIABLE
	JMP*	FLSREQ
/
ERM21	LAW	-21
	JMP*	FLSREQ
/
ERM30	LAW	-30
	JMP*	FLSREQ
/
ERM5	LAW	-5
	JMP*	FLSREQ
/
/
/
/	TEMP STORAGE
/
MODSAV	0	/MODE INDICATOR
XADJ	0	/XR ADJUSTMENT
BUFADR	0	/ADDRESS OF USER'S BUFFER
ERR	0	/STATUS OF CHECKSUM
CKSUM	0	/CHECKSUM COUNT
WPCK	0	/HEADER WORD PAIR COUNT
	.END	MTRWB
