	.SYSID <	.TITLE MTF >,<114>
/
/COPYRIGHT (C) 1975
/DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS.
/
/THIS SOFTWARE IS FURNISHED UNDER A LICENSE FOR USE ONLY
/ON A SINGLE COMPUTER SYSTEM AND MAY BE COPIED ONLY WITH
/THE INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS
/SOFTWARE, OR ANY OTHER COPIES THEREOF, MAY NOT BE PRO-
/VIDED OR OTHERWISE MADE AVAILABLE TO ANY OTHER PERSON
/EXCEPT FOR USE ON SUCH SYSTEM AND TO ONE WHO AGREES TO
/THESE LICENSE TERMS.  TITLE TO AND OWNERSHIP OF THE
/SOFTWARE SHALL AT ALL TIMES REMAIN IN DEC.
/
/THE INFORMATION IN THIS DOCUMENT IS SUBJECT TO CHANGE
/WITHOUT NOTICE AND SHOULD NOT BE CONSTRUED AS A COM-
/MITMENT BY DIGITAL EQUIPMENT CORPORATION.
/
/DEC ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY
/OF ITS SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DEC.
	.EJECT
/ 010	OCT. 3, 73 JUDIE ZENSEN
/	011	SK	17-JAN-74	BUG FIXES IN MTTRAN ROUTINE TO ENABLE
/					PROPER OPERATION WITH MTDUMP V4A
/
/	012	J. K. -- 28-MAY-74	FIX TO WRITE PROPER EOF (DEPENDING ON PREVIOUS TRAN).
/	013	JMW 7/MAY/75		IGNORE .SEEK AND .ENTER (SPR 15-989);
/					REMOVE CONDITIONALS; UPDATE COPYRIGHT.
/ 113	09-JUN-75 (RCHM)	MAKE XVM CHANGES.
/ 114	31-JUL-75 (JMW)		RETURN ALL OF MAGTAPE STATUS IN .SCOM+21.
/
/
MTSF=707341   /SKIP ON MAG TAPE DONE OR MAG TAPE ERROR FLAG.
MTCR=707321   /SKIP ON MAG TAPE CONTROL READY.
MTTR=707301   /SKIP ON SELECTED MAG TAPE TRANSPORT READY.
MTAF=707322   /CLEAR MAG TAPE DONE AND ERROR FLAGS (ALSO STATUS
                /AND COMMAND REGISTERS, IF CONTROL IS READY).
MTCM=707324   /IOR AC 0-5,9-11 TO COMMAND REGISTER; TRANSFER
                /AC 6-8 TO COMMAND REGISTER 6-8.
MTLC=707326   /TRANSFER AC 0-11 TO COMMAND REGISTER.
MTVS=707342   /IOR STATUS REGISTER TO AC.
MTRS=707352    /READ STATUS.
MTVC=707302   /IOR COMMAND REGISTER TO AC.
MTRC=707312    /READ COMMAND.
MTGO=707304   /SET GO.
.SCOM=100
.MED=3          /MONITOR ERROR DIAGNOSTIC.
IDX=ISZ
EXTERR=.SCOM+37		/DISPATCH TO EXTENDED ERR HANDLING FOR DOS
	.TITLE	CAL DISPATCHER
/
/
/
	.GLOBL MTF.,MTBSIZ
MTF.    DAC MTCALP      /SAVE POINTER TO CAL.
        DAC MTARGP      /AND TO ARGUMENT LIST.
        ISZ MTARGP      /BUMP TO UNIT AND FUNCTION.
        LAC* MTARGP     /NO ERROR, GET FUNCTION WANTED THIS TIME.
        AND (17         /EXTRACT FUNCTION.
	DAC ERRFNC	/SAVE IN CASE OF ERROR.
        TAD (MTDISP     /FORM DISPATCH ADDRESS.
        DAC MTDISP      /MTDISP POINTS TO CAL SERVICER.
/CHECK IF WAIT OR PREVIOUS I/O STILL GOING ON BEFORE UPDATING
/STATUS TABLE POINTER. FILE SPACING COMMANDS COULD STILL BE
/IN EFFECT.
        LAC* MTDISP     /PICK UP CAL FUNCTION POINTER.
        SAD MTJDBR      /SKIP IF NOT .WAIT
        JMP MTWAIT      /.WAIT OR .WAITR DISPATCH NOW.
MTGET1	LAC MTIOSW	/NOT .WAIT, CHECK I/O UNDERWAY
        SZA             /SKIP IF I/O IS COMPLETE.
        JMP MTCALR      /I/O INCOMPLETE, RETURN TO CAL.
        LAC* MTARGP     /GET UNIT.
        ISZ MTARGP      /MTARGP POINTS TO CAL+2 AFTER EXECTUTION.
        RTL             /MOVE UNIT NUMBER TO 15-17.
        RTL             /..
        AND (7          /THROW AWAY JUNK.
        TAD (MTDSTB     /FIND ENTRY IN DRIVE STATUS TABLE.
        DAC MTTPTR      /SAVE LOCATION OF TABLE ENTRY WANTED.
        JMP* MTDISP     /GO TO FUNCTION.
MTDISP  XX              /POINTER TO FUNCTION.
        JMP MTINIT      / 1 -- INIT
        JMP MTOPER      / 2 -- OPER (FSTAT, DLETE, RENAM)
        JMP MTIGNR      / 3 -- SEEK -- IGNORE.  (JMW: 013)
        JMP MTIGNR      / 4 -- ENTER -- IGNORE. (013: JMW)
        JMP MTERR6      /5 -- CLEAR
        JMP MTCLOS      / 6 -- CLOSE
        JMP MTMTAP      / 7 -- MTAPE
        JMP MTREAD      /10 -- READ
        JMP MTWRIT      /11 -- WRITE
        JMP MTDBR       /12 -- WAIT, WAITR
        JMP MTTRAN      /13 -- TRAN
	.TITLE .INIT
/INITIALIZE MAG TAPE UNIT.
/CALLING SEQUENCE:
/       CAL+DIRECTION(8)+DAT(9-17)
/       UNIT(0-2)+1
/       RESTART         /MTARGP POINTS HERE NOW.
/       BUFSIZ
/WHERE DIRECTION IS 0 FOR INPUT, 1 FOR OUTPUT.
MTINIT  ISZ MTARGP      /POINT TO BUFSIZ
        LAC MTBSIZ      /255(10).
        DAC* MTARGP     /UPDATE BUFSIZ.
        ISZ MTARGP      /POINT TO RETURN.
MTSETP  CAL+45          /CALL SETUP.
MTLBH	16		/..
MTCOMD  MTSF            /MAG TAPE DONE OR ERROR FLAG.
MTSTAT  MTINT           /GO HERE ON INTERRUPTS.
/SETUP 7 0R 9 CHANNEL DEFAULT OPTIONS.
MTAC    LAC* (.SCOM+4   /BIT 6 - 0=7 CHAN, 1=9 CHAN.
	AND (4000	/
	SNA		/
	JMP MTONCE	/7 CHANNEL DEFAULT ALREADY SETUP.
        LAW -10         /9 CHANNEL, RESET ALL OF MTDSTB.
MTEMP   DAC MTROT8              /STATUS WORD TO 9 CHANNEL.
MTWRDS  LAC (MTDSTB     /GET ADDR OF STATUS TABLE.
	DAC MTDISP
MTIOM	LAC* MTDISP
	TAD (20100	/UPDATE TO 9-CHANNEL, DUMP MODE <012>
MTERCA	DAC* MTDISP
MTERWC  ISZ MTDISP       /BUMP ADDR TO NEXT STATUS WORD.
MTOUT   ISZ MTROT8      /BUMP COUNTER.
        JMP .-5         /
MTONCE  LAC .+2         /DON'T CALL .SETUP AGAIN.
        DAC MTSETP      /..
        JMP .+1         /..
        LAC* MTCALP     /GET CAL.
        AND (1000      /EXTRACT TRANSFER DIRECTION.
        TAD (2000      /INSERT .INIT-PERFORMED INDICATOR.
        CLL!RAR
        JMS MTROT8      /MOVE IO DIRECTION INDICATOR
        RTR             /TO BIT 17.
        DAC MTEMP
        LAC* MTTPTR     /GET STATUS WORD AND CLEAR OUT PREVIOUS
        AND MT.MED      /.INIT AND DIRECTION.
        XOR* MTTPTR     /
        TAD MTEMP       /UPDATE WITH .INIT AND DIRECTION.
MTDBR0  DAC* MTTPTR     /SAVE STATUS WORD.
MTDBR   DBR             /ALL DONE, DEBREAK.
        XCT .+1
        JMP* MTARGP     /AND RETURN TO CALLER.
	.TITLE .OPER
/
/.FSTAT IS NECESSARY FOR OTS ROUTINES.
/ONLY FSTAT IS ALLOWED, DLETE AND RENAM ARE ERRORS.
/
MTOPER	LAC* MTCALP
	AND (3000	/BITS 7-8 = 3 IF FSTAT
	SAD (3000
	SKP
	JMP MTERR6
	LAC* MTARGP	/SET BITS 0-2 OF CAL+2 FOR NON FILE ORIENTED.
	AND (77777
	DAC* MTARGP
	CLA		/RETURN WITH AC=0 FOR FILE NOT FOUND.
MTIGNR=.			/(JMW: 013) COME HERE TO IGNORE .SEEK AND .ENTER.
	IDX MTARGP	/BUMP TO EXIT
	JMP MTDBR
	.TITLE .MTAPE
/EXECUTE MTAPE COMMAND.
/CALLING SEQUENCE:
/       CAL+SUBFUNCTION(5-8)+DAT(9-17)
/       UNIT(0-2)+7
/WHERE SUBFUNCTION IS:
/0-7:   MANIPULATIVE REQUEST.
/10-17: PARITY/DENSITY/TRACK-COUNT SETTING.
MTMTAP	JMS MTSTAL	/WAIT FOR DRIVE TO FREE UP
        LAC* MTCALP     /PICK UP THE CAL.
        JMS MTROT8      /MOVE THE SUBFUNCTION CODE TO 14-17.
        RTR             /..
        RAR             /..
        AND (17
        TAD (MTAPDS+1   /START OF SUBFUNCTION DISPATCH TABLE.
        DAC MTAPDS      /SAVE DISPATCH ADDRESS.
        LAC MTARGP
        DAC MTPC
        LAC* MTAPDS     /PICK UP MTAPE FUNCTION.
        SPA             /SKIP IF FUNCTION 10-17.
        XCT* MTAPDS     /JMP TO FUNCTIONS 0-7.
        LAC* MTTPTR     /10-17, PICK UP CURRENT ENTRY IN MTDSTB.
        AND (717477      /CLEAR FORMAT BITS, INCLUDING DUMP MODE <012>
        TAD* MTAPDS     /INSERT NEW VALUES.
        JMP MTDBR0      /UPDATE TABLE AND RETURN.
  
/MTAPE FUNCTION DISPATCH TABLE.
MTAPDS  XX              /POINTER TO TABLE ENTRY.
/GROUP I -- MANIPULATIVE REQUESTS.
        JMP MTRWDF      /00 -- REWIND
        JMP MTERR6      /01 -- UNDEFINED
        JMP MTBSP       /02 -- BACKSPACE
        JMP MTBSPF      /03 -- BACKSPACE FILE
        JMP MTWEOF      /04 -- WRITE EOF
        JMP MTFSP       /05 -- SPACE FORWARD
        JMP MTFSPF      /06 -- SPACE FORWARD FILE
        JMP MTEOTF      /07 -- SPACE TO LOGICAL EOT
/GROUP II -- FORMAT DIRECTIVES.
MTZERO  0               /EVEN, 200 BPI, 7-CHANNEL.
  100             /EVEN, 556 BPI, 7-CHANNEL.
MTL200  200             /EVEN, 800 BPI, 7-CHANNEL.
	20300	/EVEN, 800 BPI, 9-CHANNEL, DUMP MODE <012>
MTPAR   40000           /ODD,  200 BPI, 7-CHANNEL.
MTSTND  40100           /ODD,  556 BPI, 7-CHANNEL.
        40200           /ODD,  800 BPI, 7-CHANNEL.
MTFORM  60300           /ODD,  800 BPI, 9-CHANNEL, DUMP MODE <012>
/DEFAULT VALUES SETUP.
MTDFLT=MTFORM-1         /DEFINE DEFAULT AS 7-CHANNEL.
	.EJECT
/PERFORM MTAPE FUNCTIONS
/
/REWIND TAPE (MTAPE 00)
/
MTRWDF  LAC     (1000  /REWIND COMMAND
        TAD*    MTTPTR  /PLUS THIS UNIT
        MTLC            /PUT IN COMMAND REGISTER
        JMS     MTCMOV  /CONTINUE MOVING (START REWIND)
        JMP     MTRET1  /
/
/BACKSPACE (MTAPE 02)
/
MTBSP   JMS     MTBKWD  /MOVE BACKWARD
        JMP     MTRET1  /
/
/BACKSPACE A FILE (MTAPE 03)
/
MTBSPF	LAC (-2		/SETUP TO BACKSPACE 2 EOF'S
	DAC MTCNT	/
	JMS MTBKWD	/BACKSPACE TAPE
        SPA             /SKIP UNLESS ERRORS
	JMP MTBSF2	/CHECK TYPE OF ERROR
MTBSF1	JMS MTCMOV	/CONTINUE BACKWARDS
        SMA             /SKIP IF ERRORS
	JMP MTBSF1	/CONTINUE BACK
MTBSF2	AND (110000	/CHECK IF EOF OR BOT
	SNA		/
	JMP MTBSF1	/IGNORE OTHER ERRORS
	AND (100000	/CHECK IF BOT
	SZA		/
	JMP MTRET1	/BOT, RETURN TO MAINLINE
	ISZ MTCNT	/BUMP EOF COUNT
	JMP MTBSF1	/ANOTHER EOF TO GO YET
	JMS MTESTL	/2 EOF'S, SETTLE TAPE DOWN
	JMP MTFSP	/GO FORWARD 1 RECORD
/
/WRITE A TAPE MARK, END FILE (MTAPE 04)
/
MTWEOF  LAC*    MTTPTR  /GET DRIVE
        AND     (40000  /ENSURE EVEN PARITY
        XOR*    MTTPTR  /
        TAD     (5000  /INSERT WRITE EOF COMMAND
	JMP MTWRBB-1	/PERFORM FROM MAINLINE WRITE
/
/FORWARD SPACE ONE RECORD (MTAPE 05)
/
MTFSP   JMS     MTFWD   /MOVE FORWARD
	AND (4000	/CHECK IF AT EOT
	SZA
	JMP MTER65	/PRINT IOPS 65 ERROR
        JMP     MTRET1  /EXIT, FINISHED
/
/FORWARD SPACE A FILE (MTAPE 06)
/
MTFSPF  JMS     MTFWD   /MOVE FORWARD
        SPA             /SKIP UNLESS ERRORS
	JMP MTFSF3	/CHECK IF EOF OR EOT
MTFSF1	LAC (-1	/RESET 2 EOF'S COUNTER
	DAC MTCNT	/
MTFSF2  JMS     MTCMOV  /CONTINUE FORWARD
        SPA             /SKIP UNLESS ERRORS
	SKP		/CHECK IF EOF OR EOT
	JMP MTFSF1	/KEEP GOING FORWARD
MTFSF3	AND (14000	/CHECK IF EOF OR EOT
	SNA		/
	JMP MTFSF1	/IGNORE OTHER ERRORS
	AND (4000	/CHECK IF EOT
	SZA		/
	JMP MTER65	/PRINT IOPS 65 ERROR
MTLEOT	NOP		/
	JMP MTRET1	/EOF, RETURN TO MAINLINE
/
/MOVE TO LOGICAL END-OF-TAPE (MTAPE 07)
/
MTEOTF	LAC (-1	/SETUP TO FIND TO CONSECT EOF'S
	DAC MTCNT	/
	LAC (JMP .+3	/FORM JMP PATCH IN MTFSF3
	DAC MTLEOT	/OVERLAY NOP
        JMP     MTFSPF  /SPACE FORWARD A FILE
        ISZ     MTCNT   /INCREMENT FILE COUNT
        SKP             /FOUND SECOND FILE
        JMP     MTFSF2  /RESUME SEARCH
        LAC     MTNOP   /GET NOP
        DAC     MTLEOT  /RESTORE NOP, NOW BACKSPACE
        JMS     MTESTL  /WAIT FOR TRANSPORT READY
        JMP     MTBSP   /BACK UP
/INITIATE TAPE MOTIONS
/
/MOVE TAPE IN BACKWARD DIRECTION
MTBKWD  0               /
        LAW     -1      /SET UP WORD COUNT
        DAC*    MTWCR   /GO BACK ONE RECORD
        LAC     (7000  /GET BACKSPACE COMMAND
        TAD*    MTTPTR  /PLUS UNIT
        JMS     MTRDWR  /GO BACKSPACE
        SKP!RTL         /ILLEGAL, MOVE BOT TO SIGN
        JMP*    MTBKWD  /MOTION ACCOMPLISHED
        SMA             /SKIP IF BOT
        JMP     MTIOP4  /ILLEGAL, UNIT NOT AVAILABLE
	LAC MTSTAT	/GET STATUS
        JMP*    MTBKWD  /BOT RETURN
/MOVE TAPE IN FORWARD DIRECTION
MTFWD   0               /
        LAW     -1      /SET UP WORD COUNT
        DAC*    MTWCR   /
        DAC     MTCNT   /EOF COUNTER FOR LOGICAL EOT
        LAC     (6000  /SPACE FORWARD COMMAND
        TAD*    MTTPTR  /PLUS UNIT
        JMS     MTRDWR  /GO DO IT
        JMP     MTIOP4  /DRIVE NOT AVAILABLE
        JMP*    MTFWD   /MOTION ACCOMPLISHED
/CONTINUE MOVING
MTCMOV  0               /
        LAW     -1      /SET UP WORD COUNT
        DAC*    MTWCR   /
        LAC     (.+3    /SET UP RETURN FROM INTERRUPT
        DAC     MTRDWR  /
        JMP     MTRDW1        /GO
        SKP!RTL         /ILLEGAL, MOVE BOT TO SIGN
        JMP*    MTCMOV  /MOTION ACCOMPLISHED
        SMA             /SKIP IF BOT
        JMP     MTIOP4  /NOT AVAILABLE
	LAC MTSTAT	/GET STATUS
        JMP*    MTCMOV  /BOT RETURN
	.TITLE .READ
/READ MAG TAPE.
/CALLING SEQUENCE:
/      CAL+MODE(6-8)+DAT(9-17)
/      UNIT(0-2)+10
/      LINE BUFFER ADDR
/      WORD COUNT
MTREAD=.
	LAW -31		/SETUP READ ERROR COUNTER
	DAC MTCNT	/
	LAC* MTCALP	/GET DATA MODE
        CLL!RAR
        JMS MTROT8
        RTR
        AND (17
	SNA		/CHECK IF BINARY MODE
	JMP MTMDOK	/LEGAL MODE
	SAD (2		/CHECK IF ASCII MODE
	JMP MTMDOK	/LEGAL MODE
	JMP MTERR7	/ILLEGAL MODE
MTMDOK  LAC* MTTPTR	/GET LAST STATUS OF THIS UNIT
        RTR		/CHECK IF READY
        CML             /INVERT LINK.
        SZL!SPA         /SKIP IF INPUT .AND. INIT PERFORMED.
        JMP MTERR6
        LAC* MTARGP	/GET BUFFER ADDR
        DAC MTLBH
	AND (700000)		/(RCHM-113) EXTRACT EXTENDED MEMORY BITS.
	SZA			/(RCHM-113) ARE ANY OF THEM ON?
	JMP MTER67		/(RCHM-113) YES, GIVER ERROR MESSAGE.
	LAC MTLBH		/(RCHM-113) FETCH USER BUFFER ADDRESS.
	TAD (-1
        DAC* MTCAR      /GIVE TO CURRENT ADDRESS REGISTER.
	DAC MTERCA	/SAVE IN CASE OF READ ERROR.
        ISZ MTARGP      /BUMP TO WORD COUNT
        LAC* MTARGP     /AND GET THAT.
        DAC* MTWCR      /PUT IT IN WORD COUNT.
	DAC MTERWC	/SACE IN CASE OF READ ERROR.
        DAC MTWRDS      /AND SAVE IN CASE OF ERROR.
        ISZ MTARGP      /BUMP TO RETURN.
        LAC MTARGP
        DAC MTPC
	JMS MTSTAL      /WAIT FOR CONTROL AND TRANSPORT.
	LAC* MTTPTR	/CLEAR DUMP MODE BIT <012>
	AND (757777	/<012>
	DAC* MTTPTR	/AND SAVE COMMAND WORD <012>
	AND (300	/GET DENSITY <012>
	XOR (300	/CHECK FOR 9-CHANNEL <012>
	SNA!CLA	/SKIP IF 7-CHANNEL <012>
	LAC (20000	/ELSE SET DUMP MODE BIT <012>
	XOR* MTTPTR	/COMBINE WITH COMMAND WORD <012>
	DAC* MTTPTR	/SAVE FOR WRITING EOF <012>
	TAD (2000	/SET UP READ COMMAND <012>
        DAC MTCOMD      /SAVE IN CASE OF TAPE ERROR.
MTAGNR  JMS MTRDWR      /GO READ A RECORD.
        JMP MTIOP4      /ILLEGAL.
/
/COME HERE ON READ COMPLETION, TAPE STATUS IN AC
RERROR	SMA		/SKIP IF ERROR FLAG IS SET.
	JMP MTCSCK	/NO ERROR, CALC CHECKSUM
	AND (20600	/EXTRACT PAR, BT OR DL
	SZA		/
	JMP MTRAGN	/REAL READ ERR, TRY TO REREAD 10 TIMES
	LAC MTSTAT	/GET STATUS
	AND (14000	/EXTRACT EOF OR EOT BITS
	SZA		/
	JMP MTENDT	/EOF OR EOT REACHED
	LAC MTSTAT	/GET STATUS
	AND (1000	/EXTRACT RECORD LENGTH BIT
	SZA		/
	JMP MTRDLN	/RECORD LENGTH INCORRECT
MTCSCK  JMS MTCCS	/CALC CHECKSUM ERROR
	SNA!CLA		/SKIP ON CS ERROR
	JMP MTRET1
	SKP		/
MTOVRE	TAD (20		/SET OVERFLOW BITS
	TAD (20		/SET CHECKSUM BITS
MTPARE	TAD (20		/SET PARITY BITS
	XOR* MTLBH	/SET VALIDITY BITS IN HEADER WORD
	DAC* MTLBH
	JMP MTRET1	/
/
/REAL READ ERROR (PAR,BT,DL)
/TRY REREADING 25 TIMES
MTRAGN	ISZ MTCNT	/BUMP ERROR COUNTER
	JMP MTRSET	/NOT 25 TRIES, RESET FOR READ AGAIN
	LAC MTSTAT	/GET STATUS
	AND (20000	/EXTRACT PARITY BIT
	SZA		/
	SKP		/SET PARITY BIT IN HEADER
	JMP MTER65	/PRINT UNRECOVERABLE ERROR - (BT,DL)
	JMP MTPARE
MTRSET	JMS MTESTL	/SETTLE TAPE DOWN
	JMS MTBKWD	/BACK OVER BAD SPOT, TRY AGAIN
	JMS MTESTL	/IGNORE ANY ERRORS, SETTLE TAPE DOWN
	LAC MTERCA	/RESTORE CURRENT ADDRESS
	DAC* MTCAR	/
	LAC MTERWC	/RESTORE WORD COUNT
	DAC* MTWCR	/
	LAC MTCOMD	/RESTORE LAST READ COMMAND
	JMP MTAGNR	/
MTENDT	AND (4000	/CHECK IF EOT
	SZA		/
MTEOT	LAC (1		/BUILD EOT HEADER FLAG
	TAD (2005	/BUILD EOF HEADER FLAG
	DAC* MTLBH	/RETURN HEADER WORD
	ISZ MTLBH	/
	CMA		/COMPLEMENT AC FOR CHECKSUM
	DAC* MTLBH	/STORE CS
	ISZ* MTLBH	/2'S COMPLEMENT
	ISZ MTLBH	/
	DZM* MTLBH	/RETURN 0 FOR FORTRAN 3RD WORD
	JMP MTRET1	/
MTRDLN	LAC* MTWCR	/CHECK IF LONG OR SHORT RECORD. <012>
	SZA		/
	JMP MTRET1	/SHORT RECORD, ACCEPT IT
	JMP MTOVRE	/LONG RECORD, BUFFER OVERFLOW
	.TITLE .WRITE
/WRITE MAG TAPE.
/CALLING SEQUENCE:
/       CAL+MODE(6-8)+DAT(9-17)
/       UNIT(0-2)+11
/       LINE BUFFER ADDRESS
/       WORD COUNT -- IGNORED
MTWRIT=.
	LAW -31		/SETUP WRITE ERROR COUNTER.
	DAC MTCNT	/
	LAC* MTCALP	/GET DATA MODE
        CLL!RAR
        JMS MTROT8
        RTR
        AND (17
	SNA		/CHECK IF BINARY MODE
	JMP MTMOK	/LEGAL MODE
	SAD (2		/CHECK IF ASCII MODE
	JMP MTMOK	/LEGAL MODE
	JMP MTERR7	/ILLEGAL MODE
MTMOK   LAC* MTTPTR	/GET LAST STATUS OF THIS UNIT
        RAR		/CHECK IF READY
        SNL		/SKIP IF OUTPUT AND .INIT PERFORMED
        JMP MTERR6
        JMS MTSTAL	/WAIT FOR CONTROL AND TRANSPORT
        LAC* MTARGP	/GET BUFFER ADDR
        DAC MTLBH
	AND (700000)		/(RCHM-113) EXTRACT EXTENDED MEMORY BITS.
	SZA			/(RCHM-113) ARE ANY OF THEM ON?
	JMP MTER67		/(RCHM-113) YES, GIVE BAD MEMORY ERROR.
	LAC MTLBH		/(RCHM-113) FETCH USER BUFFER ADDRESS.
	TAD (-1
        DAC* MTCAR	/GIVE TO CURRENT ADDR REGISTER
	DAC MTERCA	/SAVE IN CASE OF A WRITE ERROR.
        ISZ MTARGP	/BUMP PAST WORD COUNT
        ISZ MTARGP	/BUMP TO RETURN
        LAC MTARGP
        DAC MTPC
        LAC* MTLBH	/GET HEADER WORD 0
        JMS MTROT8	/EXTRACT WORD COUNT
        RTR
        AND (776                /EXTRACT COUNT.
        SNA!CMA         /DON'T ACCEPT ZERO COUNT.
        JMP MTDBR               /..
	TAD (1		/MAKE TWO'S COMPLEMENT.
        DAC* MTWCR      /COUNT TO WORD COUNT REGISTER.
	DAC MTERWC	/SAVE IN CASE OF A WRITE ERROR.
	JMS MTCCS	/CALC CHECKSUM
	CMA
	ISZ MTLBH	/BUMP TO CHECKSUM WORD
	DAC* MTLBH	/STORE CHECKSUM
	ISZ* MTLBH	/2'S COMPLEMENT
	LAC MTLBH	/RESTORE ORIGINAL POINTER IN CASE OF
	TAD (-1		/REWRITE OR SETTING OF HEADER WORD
	DAC MTLBH
	LAC* MTTPTR	/CLEAR DUMP MODE BIT <012> 
	AND (757777	/<012>
	DAC* MTTPTR	/AND RESTORE <012>
	AND (300	/GET DENSITY <012>
	XOR (300	/ZERO IF 9-CHANNEL TRANSPORT <012>
	SNA!CLA	/SKIP IF 7-CHANNEL <012>
	LAC (20000	/ELSE SET DUMP MODE BIT <012>
	XOR* MTTPTR	/COMBINE WITH COMMAND WORD <012>
	DAC* MTTPTR	/AND SAVE <012>
	TAD (4000	/SET UP WRITE COMMAND <012>
	DAC	MTCOMD	/SAVE FOR RETRY
MTWRBB  JMS MTRDWR
        JMP MTIOP4	/ILLEGAL
/
/COME HERE ON WRITE COMPLETION. STATUS COMES BACK IN AC.
WERROR	SMA		/SKIP IF ERROR FLAG.
        JMP MTRET1      /RETURN IF NO ERRORS.
        AND (4000      /CHECK IF E.O.T.
        SZA
	JMP MTEOT	/RETURN EOT FLAG IN HEADER WORD
	LAC MTSTAT	/GET STATUS
	AND (20600	/CHECK IF REAL WRITE ERROR
	SNA		/
	JMP MTRET1	/IGNORE ANY OTHER ERR FLAG
/
/WRITE ERROR, TRY REWRITTING 25 TIMES
	ISZ MTCNT	/BUMP ERROR COUNTER
	JMP MTSETW	/NOT 25 TRIES YET
/
/CAN'T WRITE ON THIS SPOT OF TAPE, SKIP 6 INCHES TRY AGAIN
	LAW -31		/RESET ERROR COUNTER
	DAC MTCNT	/
	JMS MTWSET	/SETUP TO REWRITE
	TAD (10000	/ADD IN SKIP BAD TAPE BIT
	JMP MTWRBB	/REEXECUTE FULL WRITE SEQUENCE
MTSETW	JMS MTWSET	/SETUP TO REWRITE
	JMP MTWRBB	/REEXECUTE FULL WRITE SEQUENCE
MTWSET	0		/
	JMS MTESTL	/SETTLE TAPE DOWN
	JMS MTBKWD	/BACK OVER BAD SPOT, TRY AGAIN
	JMS MTESTL	/SETTLE TAPE DOWN
	LAC MTERCA	/RESTORE CURRENT ADDR
	DAC* MTCAR	/
	LAC MTERWC	/RESTORE WORD COUNT
	DAC* MTWCR	/
	LAC MTCOMD	/RESTORE WRITE COMMAND
	JMP* MTWSET	/
	.TITLE .TRAN
/CALLING SEQUENCE:
/CAL+9 TRACK DUMP(6)+DIR(7-8)+DAT SLOT(9-17)
/13
/BLOCK
/BUF ADDR
/WORD COUNT
/
/
MTTRAN	JMS MTSTAL	/WAIT FOR UNIT READY
	LAW -31		/SETUP ERR COUNTER
	DAC MTCNT
	ISZ MTARGP	/BUMP TO START ADDR
	CLA!CMA
	TAD* MTARGP	/2'S COMP
	DAC* MTCAR	/C.A.
	DAC MTERCA	/SAVE IN CASE OF ERROR
	TAD (-1
	DAC MTLBH
	ISZ MTARGP	/BUMP TO WORD COUNT
	LAC* MTARGP
	DAC* MTWCR	/W.C.
	DAC MTERWC	/SAVE IN CASE OF ERROR
	ISZ MTARGP	/BUMP TO RETURN
	LAC MTARGP
	DAC MTPC
	LAC* MTTPTR	/GET UNIT STATUS
	AND (300	/9 CHANNEL? <012>
	XOR (300 	/<012>
	SZA		/SKP IF 9 TRACK
	JMP MTRAN7	/7 TRACK
	LAC* MTCALP	/GET CAL TO CHECK BIT 6 FOR
	AND (4000	/TRUE 9 TRACK DUMP
	CLL!RTL	/MOVE 9-CHANNEL BIT TO DUMP MODE POSITION <012>
	XOR (20000	/REVERSE IT <012>
	DAC MTIOM
	LAC* MTTPTR	/ <012>
	AND (757777	/CLEAR DUMP MODE BIT <012>
	XOR MTIOM	/SET PROPER DUMP MODE BIT <012>
	DAC* MTTPTR	/STORE IT FOR WRITING EOF <012>
MTRAN7	LAC* MTCALP	/GET CAL
	AND (3000	/EXTRACT FUNCTION BITS
	SNA!CLL		/SKIP IF NOT READ FORWARD
	JMP MTRANI	/INPUT
	SAD (1000	/SKIP IF NOT WRITE FORWARD
	SKP!RTL		/WRITE FORWARD
	JMP MTERR6	/ILLEGAL FUNCTION
	TAD* MTTPTR
	DAC	MTCOMD	/SAVE FOR RETRY
MTRANW	JMS MTRDWR	/GO AND WRITE
	JMP MTIOP4
	SMA		/CHECK FOR I/O ERROR
	JMP MTRET1	/TRANSFER OK
	LAC MTSTAT	/GET STATUS
	AND (4000	/EXTRACT EOT BIT
	SZA
	JMP MTER44	/UNRECOVERABLE ERROR
	ISZ MTCNT	/BUMP ERR COUNT
	JMP .+2		/NOT 25 TRIES YET
	JMP .+3		/GO ERASE BAD TAPE AND RETRY
	JMS GOBACK
	JMP MTRANW	/ALL RESET, TRY WRITE AGAIN
	LAW -31		/TRIED 25 REWRITES, ERASE, SKIP
	DAC MTCNT	/AND DO IT ALL OVER AGAIN
	JMS GOBACK
	TAD (10000	/ADD IN SKIP FUNCTION
	JMP MTRANW
/
MTRANI	LAC* MTTPTR
	TAD (2000	/INCLUDE FUNCT.
	DAC	MTCOMD	/SAVE FOR RETRY
MTRANR	JMS MTRDWR
	JMP MTIOP4
	SMA		/CHECK FOR I/O ERROR
	JMP MTRET1	/TRANSFER OK
	LAC MTSTAT	/GET STATUS
	AND	(366600	/LENGTH ERROR?
	SNA		/POSSIBLE FOR USER TO REQUEST
	JMP MTRET1	/SHORTER COUNT THEN REC SIZE.
	ISZ MTCNT	/BUMP ERR COUNTER
	JMP .+2
	JMP MTER65	/UNRECOVERABLE READ ERROR
	JMS GOBACK
	JMP MTRANR
/
/
GOBACK	0		/BACKSPACE AND RESET CA AND WC REG'S
	JMS MTESTL	/SETTLE TAPE DOWN
	JMS MTBKWD	/BACKSPACE 1 RECORD
	JMS MTESTL	/SETTLE DOWN TAPE AGAIN
	LAC MTERCA	/GET CA
	DAC* MTCAR
	LAC MTERWC	/GET WC
	DAC* MTWCR
	LAC MTCOMD	/GET LAST COMMAND OF THIS UNIT
	JMP* GOBACK
	.TITLE .CLOSE
/EXECUTE CLOSE ON MAG TAPE
/CALLING SEQUENCE:
/      CAL+DAT SLOT(9-17)
/      UNIT(0-2)+6
MTCLOS=.
	LAC MTARGP	/SETUP RETURN ADDRESS.
	DAC MTPC
	JMS	MTSTAL
	LAC* MTTPTR
	RTR
	CMA!CML
	SZL		/SKIP IF INIT?
	JMP MTERR6	/CLOSE & NO INIT??
	SPA		/SKIP IF OUTPUT
	JMP MTRET1	/CLEAR BUSY FLAG & RETURN
	JMP MTWEOF	/WRITE EOF MARKS
	.TITLE .WAIT
/EXECUTE WAIT, WAITR ON MAG TAPE.
/CALLING SEQUENCE
/       CAL+CODE(8)+DAT(9-17)
/       12
/       NON-BUSY RETURN (WAIT) OR POINTER TO BUSY RETURN (WAITR)
/       NON-BUSY RETURN (WAITR)
/WHERE CODE IS 0 FOR WAIT, 1 FOR WAITR.
/FIRST, FIND OUT WHETHER WAIT OR WAITR IS WANTED.
MTWAIT  ISZ MTARGP      /BUMP TO RETURN
        LAC* MTCALP     /WAIT OR WAITR
        AND (1000      /EXTRACT BIT 8. 
        XOR MTSKIP      /FORM NOP OR SKP.
        DAC MTWTSW      /SKIP IF WAIT, NOP IF WAITR.
/SET UP PROPER LINK, EXTEND MODE, MEMORY PROTECT BITS IN .WAITR RETURN.
        LAC* MTARGP     /GET RETURN ADDRESS.
        AND (77777      /EXTRACT 15 BITS.
        DAC MTEMP1      /SAVE TEMPORARILY.
        LAC MTARGP      /NOW GET POINTER.
        AND (700000      /EXTRACT L, EM, MP.
        ADD MTEMP1      /PLUS .WAITR BUSY RETURN.
        DAC MTEMP1      /AND SAVE FOR LATER.
/NOW SEE IF IO IS COMPLETE.
        LAC MTIOSW      /IO STILL UNDERWAY? (MTIOSW .NE. 0 IF SO.)
        SNA             /SKIP IF IO STILL NOT FINISHED.
        JMP MTDONE      /ALL FINISHED.
        LAC MTEMP1      /ASSUME WAITR.
MTWTSW  XX              /SKIP IF WAIT.
        DAC MTCALP      /MTCALP=C(MTARGP) IF WAITR.
MTCALR  DBR             /DEBREAK FROM CAL LEVEL.
        XCT .+1
        JMP* MTCALP     /RETURN TO CAL OR WAITR ADDRESS.
/COME HERE ON IO COMPLETE. RETURN TO CAL+2 IF WAIT, TO CAL+3 IF WAITR.
MTDONE  XCT MTWTSW      /IO FINISHED. SKIP IF WAIT, NOP IF WAITR.
        ISZ MTARGP      /WAITR, BUMP TO NON-BUSY RETURN.
	LAC MTSTAT
MTJDBR  JMP MTDBR       /THEN RETURN THRU MTARGP.
/
MTSKIP	SKP
	.TITLE GENERAL ROUTINES
/SUBROUTINE MTSTAL, WAIT FOR CONTROL AND TRANSPORT READY.
/CALLING SEQUENCE:
/       JMS MTSTAL
/       RETURN IF CONTROL AND UNIT READY.
MTSTAL  0               /ENTRY TO WAIT FOR MAG TAPE DEVICES.
        MTCR            /CONTROL READY?
        JMP MTCALR      /NO, RETURN TO CALLING SEQUENCE
        LAC* MTTPTR     /CONTROL READY, GET UNIT.
	XOR (400	/TURN OFF INTERRUPT
        MTLC            /LOAD COMMAND REGISTER.
        MTTR            /TRANSPORT READY?
        JMP .+3         /NO, GO FIND OUT WHY NOT.
        DZM MTTRSW      /TRANSPORT READY, ZERO NOT-READY COUNTER.
        JMP* MTSTAL     /AND EXIT TO CALLER.
        MTRS            /IS UNIT REWINDING?
        RAL             /GET INDICATOR.
        SPA             /SKIP IF UNIT IS NOT REWINDING.
        JMP MTCALR      /REWINDING, RETURN.
        LAC MTTRSW      /ARE WE IN THE PROCESS OF COUNTING?
        SNA             /YES, COUNT AGAIN.
        JMP MTSETC      /NO, SET UP COUNT.
        ISZ MTTRSW      /COUNT THIS TIME THROUGH.
        JMP MTCALR      /RETURN IF COUNT UNSATISFIED.
/COME HERE ON COUNT COMPLETE. MEANS UNIT MUST NOT BE AVAILABLE.
        JMP MTIOP4      /GO PUBLISH IOPS 4 ERROR.
MTSETC  LAW -1750               /1000(10).
        DAC MTTRSW      /SET UP COUNTER.
        JMP MTCALR      /THEN RETURN TO CALLER.
MTTRSW  0               /COUNTER FOR 1000 TIMES THRU WAIT LOOP.
/SUBROUTINE MTROT8.
/
MTROT8  0
        XCT* MTROT8
        XCT* MTROT8
        XCT* MTROT8
        JMP* MTROT8
/
/SUBROUTINE MTESTL, WAIT FOR TAPE TO SETTLE DOWN AFTER
/A CHANGE IN DIRECTION OF TAPE MOVEMENT
MTESTL	0
	LAW -6000	/8MS DELAY SEEMS TO BE ENOUGH FOR TAPE
	DAC MTTRSW	/TO SETTLE DOWN.
	MTTR		/CHECK IF TRANSPORT READY.
	SKP		/NOT YET.
	JMP* MTESTL	/READY TO GO, RETURN TO CALLER
	ISZ MTTRSW	/BUMP DELAY COUNTER
	JMP .-4
	LAC (JMP MTESTL+3	/SETUP IOPS4 RETURN HERE
	JMP MTIOP4+1
/SUBROUTINE MTIOR, PERFORM INCLUSIVE OR.
/CALLING SEQUENCE (IOR WORD1 AND WORD2):
/       LAC WORD1               /ENTER WITH FIRST WORD IN AC.
/       JMS MTIOR               /GO TO MTIOR.
/       AND WORD2               /THIS INSTRUCTION IS XCT'D.
/       DAC RESULT      /RETURN FROM MTIOR WITH RESULT IN AC.
MTIOR   0               /ENTRY TO INCLUSIVE OR.
        DAC MTEMP1      /SAVE TEMPORARILY.
        CMA             /INVERT.
        XCT* MTIOR      /EXTRACT BITS NEEDED FOR WORD2 INCLUSION.
        XOR MTEMP1      /THEN REINSERT WORD1.
        ISZ MTIOR               /BUMP TO RETURN.
        JMP* MTIOR      /THEN EXIT TO CALLER.
MTEMP1  0               /TEMPORARY STORAGE.
/
/SUBROUTINE TO CALC CHECKSUM
/
MTCCS	0
	LAC MTLBH	/GET POINTER TO W0
	DAC MTROT8	/SAVE FOR INDEXING
	LAC MTERWC	/GET WORD COUNT
	DAC MTCNT	/SAVE FOR INDEXING
	CLA
MTCCS1	TAD* MTROT8	/ADD UP DATA
	ISZ MTROT8	/BUMP TO NEXT DATA WORD
	ISZ MTCNT	/DECREMENT COUNT
	JMP MTCCS1
	JMP* MTCCS
	.TITLE DRIVE STATUS TABLE
/DRIVE STATUS TABLE.
/BITS 0-2:   UNIT NUMBER (0-7).
/BIT 3:      PARITY FOR THIS UNIT (1=ODD, 0=EVEN).
/BIT 4:      CORE DUMP; MUST ALWAYS BE ZERO.
/BIT 5:      WRITE EXTENDED IRG; MUST ALWAYS BE ZERO.
/BITS 6-8:   COMMAND:
/     000: NO OP
/     001: REWIND
/     010: READ
/     011: READ/COMPARE
/     100: WRITE
/     101: WRITE EOF
/     110: SPACE FORWARD
/     111: SPACE REVERSE
/BIT 9:      INTERRUPT ON; SHOULD ALWAYS BE ONE.
/BITS 10-11: DENSITY FOR THIS UNIT. 10-11 ON MEANS 9-CHANNEL.
/BITS 12-15: UNUSED.
/BIT 16:     0=NO INIT, 1=INIT PERFORMED.
/BIT 17:     0=INPUT, 1=OUTPUT.
MTDSTB=.
        .REPT 10,100000
        040600
/DEFAULT VALUE AT ASSEMBLY TIME WILL BE ODD PARITY, 800 BPI,
/AND 7 CHANNEL MAG TAPE.
	.TITLE ERROR HANDLING ROUTINES
/ERROR RETURNS.
MTERR7	ISZ MTERNO
MTERR6	ISZ MTERNO
	ISZ MTERNO
	LAC MTCALP
	DAC* MT.MED
	LAC MTERNO
	JMP SETERR
MTER44	LAW 44		/PRINT CAL ALREADY IN MT.MED WITH IOPS.
	JMP SETERR
MTER65	LAC MTSTAT	/PRINT STATUS WITH IOPS.
	DAC* MT.MED
	LAW 65		/PRINT UNRECOVERABLE IOPS ERROR
	JMP SETERR
MTER67	LAW 67			/(RCHM-113) FETCH ERROR NUMBER.
	JMP SETERR		/(RCHM-113) GO PRINT ERROR MESSAGE.
MTIOP4	LAC (JMP RETRY
	DAC ERLOOP
	JMS SETFNC
	LAW 4
	DAC MTERNO
	JMP EROUT
/
RETRY	LAC (NOP
	DAC ERLOOP
	JMS MTESTL
	JMP MTCALR
/
SETERR	DAC MTERNO
	JMS SETFNC
ERLOOP	NOP
	LAC MTERNO
EROUT	CLL
	JMS* (EXTERR
	JMP ERLOOP
	LAW -3
	.SIXBT 'MTF'
	40
ERRFNT	0
/
SETFNC	0
	LAC ERRFNC	/CHANGE FUNCTION TO SIXBIT TO BE PRINTED.
	LRSS 3
	ALS 3
	LLS 3
	TAD (406060
	DAC ERRFNT
	JMP* SETFNC
/
MTERNO	0
ERRFNC	0
	.TITLE INTERRUPT SERVICE
/MAG TAPE INTERRUPT
MTINT   SKP             /SKIP IF PIC ENTRY.
        JMP MTAPI               /API ENTRY.
        DAC MTAC                /SAVE AC.
        LAC* MTZERO     /GET PC.
        DAC MTPC                /SAVE IT, TOO.
        LAC (ION                /GET ION CODE.
        JMP MTSETI      /AND ENTER MAIN PROCESSOR.
MTAPI   DAC MTAC                /SAVE AC
        LAC MTINT               /GET PC.
        DAC MTPC                /SAVE IT.
        LAC MTNOP
MTSETI  DAC MTION               /SET UP EXIT INTERRUPT CONDITION.
        LAC MTSKIP      /RESTORE PIC ENTRY.
        DAC MTINT               /..
        MTRS            /READ STATUS
	DAC* (.SCOM+21		/(JMW:113) RETURN ALL OF STATUS TO .SCOM+21.
	AND	(777600	/CIEAR JUNK
        DAC MTSTAT      /SAVE STATUS
	MTAF		/CLEAR MAG TAPE STATUS REG.
        SMA             /SKIP IF ERROR.
        JMP MTISVC      /ERROR, GO DETERMINE ERROR TYPE.
        AND MTPAR               /EXTRACT ILLEGAL BIT.
        SNA             /SKIP IF ILLEGAL.
MTISVC  ISZ MTRDWR      /BUMP MTRDWR TO SUCCESSFUL-TRANSFER RETURN.
MTION   NOP             /OR ION, IF WE GOT HERE VIA A PIC INTERRUPT.
        LAC MTSTAT      /PICK UP STATUS AGAIN.
        JMP* MTRDWR
/
/
/STATUS REGISTER CONDITION CODES
/  BIT         FUNCTION
/  00          ERROR FLAG
/  01          TAPE REWINDING
/  02          BEGINNING OF TAPE (BOT)
/  03          ILLEGAL COMMAND
/  04          PARITY ERROR
/  05          END OF FILE (EOF)
/  06          END OF TAPE (EOT)
/  07          READ/COMPARE ERROR
/  08          RECORD LENGTH ERROR
/  09          DATA LATE
/  10          BAD TAPE
/  11          MAG TAPE FLAG
	.TITLE MTRDWR; PERFORM ACTUAL I/O
/COME HERE TO PERFORM ALL ACTUAL MAG TAPE IO OPERATIONS, VIA
/"JMS MTRDWR".
MTRDWR  MTRET1          /ENTRY TO PERFORM MAG TAPE IO.
        DAC MTIOSW      /TURN ON IOSW.
        MTLC            /COMMAND FROM AC TO COMMAND REGISTER.
MTRDW1	LAC	MTAC	/PIDK UP INTERRUPT AC.
        MTGO            /TURN ON GO.
MTIRET  DBR             /DEBREAK, EITHER FROM LEVEL 4 OR LEVEL 1.
        XCT .+1      /RETURN TO MAIN PROGRAM.
	JMP* MTPC
/EXIT FROM INTERRUPT SERVICE WHEN NO MORE IO REQUIRED.
MTRET1  DZM MTIOSW      /TURN OFF I/O UNDERWAT.
        LAC MTAC                /RESTORE AC.
        JMP MTIRET      /THEN GET OUT.
	.TITLE VARIABLES, ECT.
/COMMAND REGISTER BITS 6-8.
MTWCR   32              /WORD COUNT.
MTCAR   33              /CURRENT ADDRESS.
MTBSIZ  377              /STANDARD BUFFER SIZE (255 DECIMAL).
MTCALP	0	/STORAGE FOR CAL POINTER
MTARGP	0	/STORAGE FOR ARG. IN CAL POINTER
MTIOSW	0	/I/O BUSY SWITCH
MTNOP	NOP
MTPC	0		/
MTCNT	0		/
MTTPTR	0		/
MT.MED  .MED
        .END
