                            Output sort program
                            
                            Event Sort program
                            by Roberto Sancho Nov 2020
                            from specs at page 72 in book "PERT summary report phase 2"
                            
                            tapes input:
                                          09 program 
                                          03 event tape created in Run08
                                  output:
                                          01 sorted event file
                                  temporary:
                                          04-07 work tape (only for sort)
                            
                                  on line print:
                                             label of input tape(s) 
                                             label of output tape and total number of sorted events 
                            
                            stops: IC: 01901 (00 61 0000 0000 1901) -> unrecoverable read error
                                   IC: 01905 (00 61 0000 0000 1905) -> unrecoverable write error
                            
                                   IC: 00001 (00 61 0000 0000 0000) -> finished ok. 
                            
                            
                            Input tape tape event record (as written by Run08)                            

                            w1 = NNN S P1 TLTE SCHEDL   NNN=msg len=7+3*num pred+3*num succ, SCHEDL=Schedulled date (mm dd yy), TLTE=slack TL-TE (weeks foramt NNN.N), S=TSTS sign (1 if negative), P1=probability (TL >= TS >= TE)
                            w2 = 0000 P2 EEEEEEEEE R    EEE=event code, R=report code, P2=probability (TE >= TS)
                            w3 = Event TE               Expected time (float)
                            w4 = Sigma for TE           (float)
                            w5 = Event TL               Latest Allowable time (float)
                            w6 = Sigma for TL           (float)
                            w7 = TSTS TTTT RS PR AA BB  AA=num of predecesors  BB=num of successors  TTTT=completition date (if event completed, else 0000), TSTS=Schedulled completition time, RS=Resource Code (=Res.rate), PR=Spec Code (=Pref.Rate)
                            w8 = 00 xxxx SSSSSSSSS x    Predecessor/Successor w1 = event code (comes from transac w4)
                            w9 =                        Predecessor/Successor w2 = Transaction TE Expected time (float)
                            w10 =                       Predecessor/Successor w3 = Transaction Sigma (float) 
                            ... 

                            (w8 w9 w10 ) repeated AA + BB times
                            depending on how much AA BB pred/successors
                            
                            in input tape events are grouped in blocks. Max 200 words each
                            
LABEL--- PP OPCODE-- RRRR---- MM SSSS---- MM TTTT---- MM COMMENT
                          
                                                         init
                            
0008        57                                           Clear M4 M6 M8
         09 98                                           rewind tape 09 program
         03 98                                           rewind tape 03 
         03 94       TEMP1       TEMP1       0000        read tape 03 label
            68       *                       REREAD      if tape check Call Re-read
            TR       MT3LBL      TEMP2                   set the expected label in temp2
            80       TEMP1       0001                    print input label 
            80       ZERO        0002
            80                   0003
            80                   0004
            80                   0005
            80                   0006
            81                   0007                    print
            TR EQ    TEMP1       TEMP2       INIAOK      check if label is the expected one
            STOP                                         stop because tape label does not match the expected one

INIAOK      TR       MT4         OUTTAP                  Init to start writting to MT4
            TR       MT5         OUTTAP2                 and then to MT5
         04 98                                           rewind MT4 work tape
         05 98                                           rewind MT5 work tape
         01 98                                           rewind MT1 result sorted tape
            TR                               PHASE-A
     
                                                              phase a (Split) read input tape MT3, 
                                                                              write 1 event per block to alternate tapes MT4/MT5
        
PHASE-A     NOP
            TR       MT3         INTAP                   init input tapes A = MT3
            TR       ZERO        EOFTAP                  mark tape A as not reached end of file
            TR       ZERO        BL-INBUF                init to force read first block of input tape
            TR       MT4         OUTTAP                  init output tapes C/D = MT4/5
            TR       MT5         OUTTAP2
            TR       ZERO        NW-OUTB                 Init output buffer as empty 
            TR       ZERO        WOUTTAP                 number of written events

LA0         TR       ONE         NSEQIN                  init max number of events allowed to be read 
            57       0000        0000        0000        use tape INTAP tape number
         08 60       *                       READ-EV     call read event
            TR EQ    CODE1       ZERO        EOFIN       if eof (=code zero) terminate phase a
            57       0000        0000        0000        use INBUF
         08 60       *                       WRITE-EV    call write event
            57       0000        0000        9999
         08 60       *                       WRITE-EV    flush outbuf to tape
            TR       OUTTAP      TEMP1                   swap output tapes
            TR       OUTTAP2     OUTTAP                  
            TR       TEMP1       OUTTAP2    
            TR                               LA0         
                                                              end of phase-a
     
EOFIN    04 90                                           write end of tape in tape 04
         08 60       *                       CHKWRT      Check write
         05 90                                           write end of tape in tape 05
         08 60       *                       CHKWRT      Check write
            TR                               PHASE-B     end of phase a split, goto phase b sort
               
                                                              phase b (sort) read tape A and B, 
                                                                             sort merge seuences of n events to alternate output tapes C/D
     
PHASE-B     TR       ONE         NSEQ                    init sequence of already sorted events
            TR       MT4         INTAP                   init input tapes A/B = MT4/5 
            TR       MT5         INTAP2
            TR       MT6         OUTTAP                  init output tapes C/D = MT6/7
            TR       MT7         OUTTAP2
            TR       ZERO        NW-OUTB                 Init output buffer as empty 
LBSEQ00  04 98                                           rewind work tapes
         05 98
         06 98
         07 98
            TR       ZERO        EOFTAP                  mark tape A/B as not reached end of file
            TR       ZERO        EOFTAP2
            TR       ZERO        WOUTTAP                 mark nothing writen yet into tape C/D 
            TR       ZERO        WOUTTAP2    
            TR       ZERO        BL-INBUF                init to force read first block of input tape
            TR       ZERO        BL2INBUF           
            TR                               LBSEQ0

LBSEQ0      TR       NSEQ        NSEQIN                  init max number of events allowed to be read in this sequence
            TR       NSEQ        NSEQIN2
            TR NEQ   NW-OUTB     ZERO        LBSEQ0E     check if out buf is empty    
;           nop                                          **echo LBSEQ0
            TR                               LBSEQ0B

LBSEQ0E     STOP                                         safety. stop because out buf not empty -> has not been flushed 

LBSEQ0B     57       0000        0000        0000        use tape INTAP tape number
         08 60       *                       READ-EV     call read event
            57       0000        0000        0001        use tape INTAP2 tape number
         08 60       *                       READ-EV     call read event

LBSEQ1      NOP

;           nop                                          **echo LBSEQ1
;           tr       code1                               
;           nop                                          **echo code1:
;           tr       code2                               
;           nop                                          **echo code2:

            TR EQ    CODE1       ZERO        LBDUMP2     if code1 = 0 then dump tape 2
            TR EQ    CODE2       ZERO        LBDUMP1     if code2 = 0 then dump tape 1
            SUB      CODE2       CODE1                   if code2 >= code1 then write code1
            TR SGN   LBWR1       LBWR1       LBWR2       else write code2
LBDUMP1     NOP

;           tr       code1                               **echo LBDUMP1
;           nop                                          **echo code1:

            TR EQ    CODE1       ZERO        LBSTERM     tape 1 dumped, goto sequence terminated
            57       0000        0000        0000        use INBUF

;           nop                                          **echo write_CODE:

         08 60       *                       WRITE-EV    call write event
            57       0000        0000        0000        use tape INTAP
         08 60       *                       READ-EV     call read event
            TR                               LBDUMP1
LBDUMP2     NOP


;           tr       code2                               **echo LBDUMP2
;           nop                                          **echo code2:

            TR EQ    CODE2       ZERO        LBSTERM     tape 2 dumped, goto sequence terminated
            57       0000        0000        0001        use INBUF2
         08 60       *                       WRITE-EV    call write event
            57       0000        0000        0001        use tape INTAP2
         08 60       *                       READ-EV     call read event
            TR                               LBDUMP2
LBWR1       NOP

;           nop                                          **echo LBWR1

            57       0000        0000        0000        write code1
         08 60       *                       WRITE-EV    call write event
            57       0000        0000        0000        read next code1
         08 60       *                       READ-EV     call read event
            TR                               LBSEQ1
LBWR2       NOP

;           nop                                          **echo LBWR2

            57       0000        0000        0001        write code2
         08 60       *                       WRITE-EV    call write event
            57       0000        0000        0001        read next code2
         08 60       *                       READ-EV     call read event
            TR                               LBSEQ1
LBSTERM     NOP

;           nop                                          **echo LBSTERM

            57       0000        0000        9999
         08 60       *                       WRITE-EV    flush outbuf to tape
            ADD      EOFTAP      EOFTAP2                   
            TR EQ                N2          LBSWAP      if all blocks read from input tapes, goto swap in/out tapes

                                                         end of sequence but no end of tapes

            TR       OUTTAP      TEMP1                   swap output tapes
            TR       OUTTAP2     OUTTAP                  
            TR       TEMP1       OUTTAP2        
            TR       WOUTTAP     TEMP1                   swap number of events in out tapes
            TR       WOUTTAP2    WOUTTAP                  and continue
            TR       TEMP1       WOUTTAP2     LBSEQ0

LBSWAP      NOP
;           nop                                          **echo LBSWAP
            TR       OUTTAP      SRCTAP
         08 60       *                       WR-EOT      write eot on output tape
            TR       OUTTAP2     SRCTAP
         08 60       *                       WR-EOT      write eot on output tape 2
            TR EQ    WOUTTAP     ZERO        LBOK2       if nothing in tape 1 goto output tape 2 has all the sorted events
            TR EQ    WOUTTAP2    ZERO        LBOK1       if nothing in tape 1 goto output tape 1 has all the sorted events
            TR       INTAP       TEMP1                   swap input and output tapes
            TR       INTAP2      TEMP2                   
            TR       OUTTAP      INTAP
            TR       OUTTAP2     INTAP2
            TR       TEMP1       OUTTAP
            TR       TEMP2       OUTTAP2
            ADD      NSEQ        NSEQ        NSEQ        double sequence size = number of events processed at once  
            TR                               LBSEQ00     contine      
LBOK1       TR       OUTTAP      SRCTAP      PHASE-C     output tape 1 has the sorted records 
LBOK2       TR       OUTTAP2     SRCTAP      PHASE-C     output tape 2 has the sorted records                                                         

                                                              phase c (transcribe) copy tape SRC to MT1, with label
        
PHASE-C     ADD      RWINT-OP    SRCTAP      RW-INT      set inst ...  
RW-INT      00                                           ... to rewind SRC tape
         01 90       MT1LBLS     MT1LBLS                 write label for sorted tape in MT1
         08 60       *                       CHKWRT      Check write
            ADD      WOUTTAP     WOUTTAP2    TEMP1       Now TEMP1 has the total sorted events
            TR       ONE         NBLOCK                  init block number
            80       MT1LBLS     0001                    print output tape label 
            80       TEMP1       0002                    print number of sorted transactions in output tape
            80       ZERO        0003
            80                   0004
            80                   0005
            80                   0006
            81                   0007                    print
            ADD      LDINT-OP    SRCTAP      RB-INT      set inst ...  
RB-INT      00                                           ... to read SRC tape
            68       *                       REREAD      if tape check Call Re-read
            67                               EOFT        if end of file, transcribe finished
            TR                   TEMP1                   Temp1=start of block xx xx aaaa bbbb xxxx
         04 MASK                 FIELD-S     TEMP2       Temp2=bbbb=02 90 aaaa bbbb nnnn maked with 0000 0000 1111 0000 -> 0000 0000 bbbb 0000, then shift 4 right to get 0..0 bbbb
         08 MASK     TEMP1       FIELD-R                 REG1=02 90 aaaa bbbb nnnn maked with 0000 1111 0000 0000 -> 0000 aaaa 0000 0000, then shift 8 right to get 0..0 aaaa 
            SUB      TEMP2                   BL-INBUF    BL-INBUF=Block Len from Input tape to transcribe=bbbb-aaaa
         96 TR       BL-INBUF                            set instr ...
            ADD      NBLOCK                              ... to write to MT01 
            ADD      SVINT-OP                WB-INT      ... for same len as read
WB-INT      00                                           ... and blocki nblock
         08 60       *                       CHKWRT      Check write
            ADD      NBLOCK      ONE         NBLOCK      increment block number 
            TR                               RB-INT      
     
RWINT-OP 00 98       0000        0000        0000
LDINT-OP 00 94       INBUF       0000        0000
SVINT-OP 01 90       INBUF       INBUF       0000
     
EOFT     01 90       0000        0000        0000        write end of tape 01 
         08 60       *                       CHKWRT      Check write
         01 98                                           rewing tape 01 sorted data   
END         TR       STOP-OP     0001        0001        set stop in 0001, transfer to 0001: sucessfull program end                             
     


500                                                      variables
     
     
INBUF       RES      200                                 block read from input tape 02 or 03
INBUF2      RES      200

BL-INBUF                                                 block length
BL2INBUF                                                 block length for inbuf2 
NW-INBUF                                                 Number of words read from block
NW2INBUF                                                 Number of words read from block in inbuf 2

OUTBUF      RES      200                                 block to write in tape 04 or 05 (unordered)
NW-OUTB                                                  Number of words in block to write


INTAP                                                    input tape for phase B 
INTAP2
OUTTAP                                                   output tape = 0n 0..0 
OUTTAP2                                                  alternate output tape 
SRCTAP                                                   source tape for phase-c transcribe file to mt1
EOFTAP                                                   flag indicates if reached end of file on input tape A
EOFTAP2                                                   
WOUTTAP                                                  number of events in output tape 
WOUTTAP2

LEN                                                      event length
CODE                                                     event code as control word for sort
CODE1                                                    event code read from input tape A
CODE2

NSEQ                                                     number of events in sequence 
NSEQIN
NSEQIN2

NBLOCK                                                   number of block on final mt1 sorted tape

WW1         RES      200
WW2         RES      200

TEMP1
TEMP2
     
MT3LBL   39 08       0000        0000        0000        input tape label for computed event tape
MT1LBLS  39 09       0000        0000        0000        output event sorted tape label 
     
ZERO     00 00       0000        0000        0000
ONE      00 00       0000        0000        0001
N2       00 00       0000        0000        0002
N7       00 00       0000        0000        0002
N200     00 00       0000        0000        0200
     
MT1      01 
MT2      02 
MT3      03
MT4      04 
MT5      05
MT6      06 
MT7      07
     
FIELD-R  00 00       1111        0000        0000
FIELD-S  00 00       0000        1111        0000  
FIELD-T  00 00       0000        0000        1111
ONE-S    00 00       0000        0001        0000
CODFIELD 00 00       0011        1111        1110               
     
STOP-OP  00 61       0000        0000        0000        stop opcode
SETM-OP  00 57       0000        0000        0000        clear M468 and set M468 opcode
NOP-OP   00 60       0000        0000        0000        

                                                              read event from input tape
                                                              if M8=0000 use INTAP as input
                                                                   =0001     INTAP2 etc 
                                                              if BL-INBUF=0 -> force read a new block
                                                                 and init BL-INBUF/NW-INBUF/NSEQIN
                                                              use INBUF/WW1|INBUF2/WW2
                                                              set CODE1|CODE2 with code for read event, 
                                                                 zero if no data beacause reached end of 
                                                                 seq, or end of tape (if so, set EOFTAP/2)
     
READ-EV     ADD      MK-TR60                 LRRINEX     set and save exit routine
            TR EQ    NSEQIN  +M8 ZERO        LRBINND     if no blocks in sequence remaining to be read, return No Data      
            TR NEQ   EOFTAP  +M8 ZERO        LRBINND     if tape reached end of file mark, return No Data      
            SUB      NSEQIN  +M8 ONE         NSEQIN  +M8 ok, read to read new input event. decr 
            TR NEQ   BL-INBUF+M8 ZERO        RR-EV       if BL-INBUF not zero, goto read record, else continue to read first block
                 
                                                              read block                                                         
                                                              
RB-EV       ADD      RBIN1-OP+M8 INTAP   +M8 LRBIN1      set instr to ...
LRBIN1      00                                           ... read from INTAP/INPAT2 to INBUF/INBUF2
            68       *                       REREAD      if tape check Call Re-read
            67                               LRBIN4      if end of file goto ...
            TR                   TEMP1                   Temp1=start of block xx xx aaaa bbbb xxxx
         04 MASK                 FIELD-S     TEMP2       Temp2=bbbb=02 90 aaaa bbbb nnnn maked with 0000 0000 1111 0000 -> 0000 0000 bbbb 0000, then shift 4 right to get 0..0 bbbb
         08 MASK     TEMP1       FIELD-R                 REG1=02 90 aaaa bbbb nnnn maked with 0000 1111 0000 0000 -> 0000 aaaa 0000 0000, then shift 8 right to get 0..0 aaaa 
            SUB      TEMP2             
            ADD                  ONE         BL-INBUF+M8 BL-INBUF=Block Len from Input event block=bbbb-aaaa+1
            SUB      N200                                check Block Len from Input block 
            TR SGN   LRBIN2      LRBIN2                  transfer if 200 or less
            STOP                                         stop because input block too big (max 200w)
LRBIN2      TR       ZERO        NW-INBUF+M8             NW-INBUF=Number of words already processed from Input block
            TR                               RR-EV       goto process first event record in block
LRBIN4      TR       ONE         EOFTAP  +M8             set end of file flag
            TR                               LRBINND     then return no data


RBIN1-OP 00 94       INBUF       0000
RBIN2-OP 00 94       INBUF2      0000
RBIN3-OP    TR       INBUF   +M6 WW1     +M4
RBIN4-OP    TR       INBUF2  +M6 WW2     +M4
SETM46OP    53       0000        0000        0000        
     
LRBINND     TR       ZERO        CODE1   +M8 LRRINEX     return no data
     
                                                              read event record from from input tape block 
                 
RR-EV       SUB      BL-INBUF+M8 NW-INBUF+M8             REG1=words left to be read in input transact block. 
            TR SGN   LRRIN1      RB-EV                   If no words left, go to read next block
            STOP                                         Safety: stop because overflowing block rec
LRRIN1   96 TR       NW-INBUF+M8                         Shift num words read into field S
            ADD      SETM46OP                LRRIN2      set instr to ...
LRRIN2      00                                           ... set M6=(NW-INBUF+M8), M4=0000
            TR       RBIN3-OP+M8 LRRIN3                  set instr to ... 
            TR                   LRRIN6                  ... 
            TR                   LRRIN8                  ... 
LRRIN3      00                                           ... copy from input buf to rec ww1/2..
         13 TR                   LEN                     get event length
            SUB      LEN         N7                      check event len is 7..200
            TR SGN   LRRIN4
            STOP                                         because event len < 7
LRRIN4      SUB      LEN         N200                    check event size <= 200
            TR SGN               LRRIN5      LRRIN5
            STOP                                         safety: stop because event has more that 200 words
LRRIN5      50       0001        0001                    incr M4, M6
LRRIN6      00                                           copy from input buf to rec ww1/2..
            MASK                 CODFIELD    CODE1   +M8 get event code
            TR NEQ               ZERO        LRRIN7      check if event code read is zero
            STOP                                         stop because event code is zero
LRRIN7      50       0001        0001                    incr M4, M6
            SUB      LEN         N2          TEMP1            
LRRIN8      00                                           copy from input buf to rec ww1/2..
            50       0001        0001                    incr M4, M6           
            SUB      TEMP1       ONE         TEMP1       decr remaining len
            TR NEQ               ZERO        LRRIN8      loop if event not yet copied
            ADD      NW-INBUF+M8 LEN         NW-INBUF+M8 Number of words read from block += LEN
LRRINEX     00                                           exit routine

                                                              add event to outbuf, write to outtap when full
                                                              if M8=9999 -> A flush outbuf to tape
                                                              if M8=0000 -> add trabasction from WW1
                                                              if M8=0001 -> add trabasction from WW2
     
WRITE-EV    ADD      MK-TR60                 LWROUEX     set and save exit routine
            TR       NOP-OP      LWB2        
         08 58       0000        9999        WR-EV       goto write event if M8<>9999

;           tr       outtap
;           nop                                          **echo WRITE_TAPE:

            TR       LWROUEX     LWB2        WB-EV       goto flush block to tape and then exit
     
                                                         write output event block on tape

WB-EV    96 TR       NW-OUTB                             put in REG1 field S ... 
            TR EQ                ZERO        LWB2        if outbuf empty, skip tape write
            SUB                  ONE-S                   ... number of words to save 
            ADD      SVOUT-OP                            set instr ...
            ADD      OUTTAP                  LWB1        ... with save mem addr 
LWB1        00       0000        0000        0000        ... in tape OUTTAP
;           nop                                          **echo save_tape
         08 60       *                       CHKWRT      Check write
LWB3        TR       ZERO        NW-OUTB                 clear number of words in block buf
LWB2        NOP            
            TR                               LWR1        goto add event data to outbuf
     
SVOUT-OP 00 90       OUTBUF      OUTBUF      0000
SVOU3-OP    TR       WW1     +M4 OUTBUF  +M6
SVOU4-OP    TR       WW2     +M4 OUTBUF  +M6
SVOU5-OP 13 TR       WW1         LEN  
SVOU6-OP 13 TR       WW2         LEN  

                                                         write output event rec on outbuf

WR-EV       NOP

;           tr       code1   +m8
;           nop                                          **echo WRITE_EVENT:

            TR       SVOU5-OP+M8 LWR0                    set instr to ... 
LWR0        00                                           ... get event LEN 
            TR EQ                ZERO        LWR0A       check if len zero
            SUB                  N200                    check event size <= 200
            TR SGN               LWR1B       LWR1B
            STOP                                         safety: stop because event has more that 200 words
LWR0A       STOP                                         safety: stop because event has zero words        
LWR1B       ADD      NW-OUTB     LEN                     check if enough room in outbuf for event
            SUB      N200        
            TR SGN                           WB-EV       no room -> write block
LWR1        TR       SVOU3-OP+M8 LWR3                    set source event rec instr
         96 TR       NW-OUTB                             set instr to ...
            ADD      LEN                                 ... set M8 value to LEN
            ADD      SETM-OP                 LWR2        ... set M6 value to NW-OUTB
LWR2        00       0000        0000        0000        clear M4, set M6 to NW-OUTB, M8 to LEN
LWR3        TR       WW1     +M4 OUTBUF  +M6             copy from ww1/2.. to outbuf         
            50       0001        0001        0000        ... incr M4, M6 
         08 58       9999        0000        LWR3        ... decr M8, loop
            ADD      NW-OUTB     LEN         NW-OUTB     Number of words in block += LEN
            ADD      WOUTTAP     ONE         WOUTTAP     incr number of event in output tape
LWROUEX     00

                                                              write end of tape to SRCTAP

WR-EOT      ADD      MK-TR60                 LWREOTEX    set and save exit routine
            ADD      SVEOT-OP    SRCTAP      LWEOT1      set instr ...
LWEOT1      00                                           ... to write end of tape
         08 60       *                       CHKWRT      Check write
LWREOTEX    00


SVEOT-OP 00 90       0000        0000        0000



1990                                                     Library
     
                                                              Re-read routine. if error persist then stop 
                                   
REREAD      STOP                             REREAD      if read error, stop    
     
                                                              Check write routine. 
     
CHKWRT      ADD      MK-TR60                 LCHKEX      set and save exit transfer
            68                               LCHK1       if tape check goto lchk1
LCHKEX      00                                           if no tape chek, return to caller
LCHK1       STOP                             LCHK1       if write error, stop 
MK-TR60  00 59       9999        9140        0001   
          
