          REM   BALGOL COMPILER OUTPUTMEDIA ROUTINE FOR PAPER TAPE.
          REM   2018-07-31 P.KIMPEL, REVISED 2018-10-21
          REM
    PTU   DEFN  1               PAPER TAPE/TTY OUTPUT UNIT NR
          REM
    OTBL  NOP   *-*             ENTRY POINT
          LDB 8 OTBL            LOAD RETURN ADDRESS TO B
          STB 8 XBUN,04         STORE IN EXIT BUN ADDR
          STR 8 RSAVE           PRESERVE CALLER-S R REG
          LDR - 9998            LOAD STP WORD TO R
          REM
          REM   OUTPUT CARRIAGE CONTROL BEFORE PRINT
          REM
          BFR 8 SSB,31,22       SINGLE SPACE BEFORE AND AFTER
          BFR 8 FFB,31,33       SKIP CH.1 BEFORE
          BFR 8 DSB,31,44       DOUBLE SPACE BEFORE
          BFR 8 FFB,31,55       SKIP CH.2 BEFORE
          BFR 8 DSB,31,66       DOUBLE SPACE BEFORE/SINGLE AFTER
          BFR 8 FFB,31,77       SKIP CH.3 BEFORE
          BUN 8 SSB             ALL OTHER CASES, SINGLE SPACE BEFORE
          REM
    FFB   PWR 8 FF,PTU,1        OUTPUT FORM-FEED
          BUN 8 FMT
    DSB   PWR 8 CR,PTU,1        OUTPUT CARRIAGE-RETURN
    SSB   PWR 8 CR,PTU,1        OUTPUT CARRIAGE-RETURN
          REM
          REM   FORMAT LINE ACCORDING TO F-DIGIT IN STP WORD
          REM
    FMT   DEFN  *
          DLB - 9999,44,0       LOAD BUFFER ADDR TO B
          BFR 8 FMT2,41,22      FORMAT ALGOL STATEMENT
          BFR 8 FMT1,41,00      FORMAT MACHINE INSTRUCTION
          BFR 8 FMT3,41,44      FORMAT FORWARDS
          BFR 8 FMT5,41,88      FORMAT FIXUPS
          REM
    FMT4  DEFN  *               FORMAT 4 -- OUTPUT WORDS FROM BUFFER
          BUN 8 WBUF            BRANCH TO BUFFER WRITE
          REM
    FMT1  DEFN  *               FORMAT 1 -- MACHINE INSTRUCTION
          CAD - 0               FETCH 1ST WORD FROM BUFFER
          STP 8 PFX1X           OUTPUT INSTRUCTION PREFIX
          BUN 8 PFX1
          CAD - 1               FETCH 2ND WORD FROM BUFFER
          STP 8 IOUTX           FORMAT 7 HIGH-ORDER DIGITS
          BUN 8 IOUT
          CAD - 1               REFETCH 2ND WORD FROM BUFFER
          STP 8 AOUTX           OUTPUT THE ADDRESS FIELD
          BUN 8 AOUT
          PWR 8 SP1,PTU,1       OUTPUT A SPACE
          CAD - 2               FETCH 3RD WORD FROM BUFFER
          BFA 8 CCA,00,00       IF BLANK, DONT PRINT IT
          STA 8 SIGN2,00        STORE WITH SIGN-2 FOR ALPHA
          PWR 8 SIGN2,PTU,1     OUTPUT 3RD WORD
          BUN 8 CCA             BRANCH TO CARRIAGE CONTROL
          REM
    FMT2  DEFN  *               FORMAT 2 -- ALGOL STATEMENT
          CAD - 9999            FETCH ADDRESS WORD BEFORE BUFFER
          SRA   4               POSITION ADDRESS DIGITS
          STP 8 AOUTX           OUTPUT THE ADDRESS
          BUN 8 AOUT
          PWR 8 SP4,PTU,1       OUTPUT 4 SPACES
          BUN 8 WBUF            BRANCH TO BUFFER WRITE
          REM
    FMT3  DEFN  *               FORMAT 3 -- FORWARD ADDRESSES
          CAD - 0               FETCH 1ST WORD FROM BUFFER
          STP 8 PFX1X           OUTPUT INSTRUCTION PREFIX
          BUN 8 PFX1
          CAD - 1               FETCH 2ND WORD FROM BUFFER
          STP 8 IOUTX           FORMAT 7 HIGH-ORDER DIGITS
          BUN 8 IOUT
          PWR 8 SP5,PTU,1       OUTPUT 5 SPACES
          CAD - 2               FETCH 3RD WORD FROM BUFFER
          STA 8 SIGN2,00        STORE WITH SIGN-2 FOR ALPHA
          PWR 8 SIGN2,PTU,1     OUTPUT 3RD WORD
          BUN 8 CCA             BRANCH TO CARRIAGE CONTROL
          REM
    FMT5  DEFN  *               FORMAT 5 -- ADDRESS FIXUPS
          CAD - 0               FETCH 1ST WORD FROM BUFFER
          STP 8 PFX1X           OUTPUT INSTRUCTION PREFIX
          BUN 8 PFX1
          PWR 8 SP5,PTU,1       OUTPUT 5 SPACES
          PWR 8 SP5,PTU,1       OUTPUT 5 MORE
          CAD - 1               FETCH 2ND WORD FROM BUFFER
          STP 8 AOUTX           OUTPUT THE ADDRESS
          BUN 8 AOUT
          BUN 8 CCA             BRANCH TO CARRIAGE CONTROL
          REM
          REM   WRITE N WORDS FROM BUFFER ADDRESS, TRIMMING SPACES
          REM
    WBUF  DEFN  *
          STB 8 P+,04           STORE BUF ADDR IN PWR WORD
          STB 8 C+,04           STORE BUF ADDR IN CAD WORD
          LDB 8 OTBL            GET RETURN ADDR
          CAD - 9998            LOAD STP WORD TO A
          SRA   1               SHIFT BUF LEN IN A TO /33
          STA 8 P+,32           STORE BUF LEN INTO PWR WORD
    *B    SRA   1               SHIFT BUF LEN IN A TO /44
          STA 8 B-,44           STORE BUF LEN FOR DLB NEXT
          DLB 8 B-,44,1         LOAD BUF LEN MINUS 1 TO B
    *C    CAD - *-*             LOAD CURRENT LAST WORD OF BUFFER
          BFA 8 D+,00,00        BRANCH IF WORD IS ALL SPACES
          BUN 8 P+              IF NOT SPACES, WRITE BUFFER
    *D    DFL 8 P+,32,1         DECREMENT BUF LEN IN PWR WORD
          DBB 8 C-,1            DECREMENT B, LOOP IF NON-NEG
          BUN 8 CCA             BUFFER IS ALL SPACES, SKIP TO CC
    *P    PWR   *-*,PTU,*-*     WRITE THE TRIMMED BUFFER
          REM
          REM   CARRIAGE CONTROL AFTER PRINT
          REM
    CCA   DEFN  *
          LDB 8 OTBL            LOAD RETURN ADDRESS TO B
          LDR - 9998            LOAD STP WORD TO R
          BFR 8 FFA,31,11       SKIP CH.1 AFTER
          BFR 8 SSA,31,22       SINGLE SPACE BEFORE AND AFTER
          BFR 8 SSA,31,66       DOUBLE SPACE BEFORE/SINGLE AFTER
          BFR 8 FFA,31,99       SKIP CH.1 AFTER
          BUN 8 EXIT            ALL OTHER CASES, NO CC AFTER
          REM
    FFA   PWR 8 FF,PTU,1        OUTPUT FORM-FEED
          BUN 8 EXIT
    SSA   PWR 8 CR,PTU,1        OUTPUT CARRIAGE-RETURN
          REM
    EXIT  DEFN  *
          LDR 8 RSAVE           RESTORE CALLER-S R REG
          BOF 8 *+1             MAKE SURE OVERFLOW IS RESET
    XBUN  BUN   *-*             RETURN TO CALLER
          REM
          REM   OUTPUT INSTRUCTION PREFIX FROM A REG
          REM
    PFX1  DEFN  *
          PWR 8 SP5,PTU,1       OUTPUT 5 SPACES
          STP 8 AOUTX           OUTPUT THE ADDRESS
          BUN 8 AOUT
          PWR 8 SP3,PTU,1       OUTPUT 3 SPACES
    PFX1X BUN   *-*             RETURN TO CALLER
          REM
          REM   OUTPUT 4 LOW-ORDER DIGITS FROM A REG
          REM
    AOUT  DEFN  *
          SRT   4,900           SHIFT DIGITS TO R
          CAD 8 SP4             LOAD SP4 WORD FOR 02 CHAR
          IFL 8 AOUT,11,4       SET REPEAT COUNT
    *L    LSA   8               SET SIGN TO 8
          SLS   1               ROTATE IT TO THE /01 DIGIT
          SLT   1               SHIFT DIGIT FROM R
          DFL 8 AOUT,11,1       DECREMENT REPEAT COUNT
          BRP 8 L-              LOOP IF NOT DONE
          STA 8 SIGN2,00        STORE IN /00 OF OUTPUT WORD
          PWR 8 SIGN2,PTU,1     OUTPUT ADDRESS STRING
          BOF 8 *+1             MAKE SURE OVERFLOW IS RESET
    AOUTX BUN   *-*             RETURN TO CALLER
          REM
          REM   OUTPUT 7 HIGH-ORDER INSTRUCTION DIGITS FROM A REG
          REM
    IOUT  DEFN  *
          SRT   10              MOVE INSTRUCTION TO R
          CAD 8 SP1             LOAD SP1 WORD AS SKELETON
          SLT   0               COPY SIGN FROM R TO A
          SRS   1               SHIFT SIGN DIGIT TO /11
          LSA   8               SET NUMERIC ZONE
          SRS   1               SHIFT ZONE TO /11
          STA 8 SIGN2,00        STORE RESULT IN OUTPUT WORD
          PWR 8 SIGN2,PTU,1     OUTPUT SIGN+SPACE
          SLT   4               SHIFT CONTROL DIGITS TO A
          STP 8 AOUTX           OUTPUT CONTROL DIGITS
          BUN 8 AOUT
          CAD 8 SP4             LOAD SP4 WORD AS SKELETON
          LSA   8               SET SIGN TO 8
          SLS   1               ROTATE 1ST ZONE DIGIT TO /01
          SLT   1               SHIFT 1ST NUMERIC DIGIT TO /01
          LSA   8               SET SIGN TO 8
          SLS   1               ROTATE 2ND ZONE DIGIT TO /01
          SLT   1               SHIFT 2ND NUMERIC DIGIT TO /01
          SLA   2               ROTATE ZERO (SPACE) TO /02
          STA 8 SIGN2,00        STORE IN /00 OF OUTPUT WORD
          PWR 8 SIGN2,PTU,1     OUTPUT OP CODE DIGITS
    IOUTX BUN   *-*             RETURN TO CALLER
          REM
          REM   STORAGE
          REM
    SIGN2 CNST  20000000000     SIGN-2 WORD FOR PWR OUTPUT
    CR    CNST  21602020202     CARRIAGE-RETURN WORD
    FF    CNST  21615020202     FORM-FEED WORD
    SP1   CNST  20002020202     1 SPACE
    SP3   CNST  20000000202     3 SPACES
    SP4   CNST  20000000002     4 SPACES
    SP5   CNST  20000000000     5 SPACES
    RSAVE CNST  0               STORAGE FOR CALLER-S R REG
          FINI  OTBL
