Scheme A PERM & the Routine Changing Sequence    Date: 1950-51
     
    Written by Alan Turing

    Ferranti Mark I Programming Manual, first edition is dated 1950
    and already describes (and lists) PERM and Scheme A

    from Programming the mark I: Early Programming Activity at the 
    University of Manchester (author: Martin Campbell-Kelly)

        Scheme A              Spring 1951   By A.Turing
        Scheme B              Spring 1952   Brooker and Glennie
        TELEINPUT (Scheme C)  Early 1953    Brooker

    Ferranti Mark I Programming Manual, first edition + errata sheet
    Alan Turing Collection (https://turingarchive.kings.cam.ac.uk/)
    amt-b-31-57-3.pdf
    p106
    
    Ferranti Mark I Programming Manual, first edition
    Computer History Museum (https://computerhistory.org/)
    102724592-05-01-acc.pdf
    p109
     
    Explanation on how to use Scheme A can be found in Programming Manual 
    In First edition: p54-p59 (section The permanent information PERM)
    In Second edition: p53-p76 (section The preparation of a proble,: Programming: Scheme A)


    [Routine PERM & RCS ] 

    [Col : S  Track 1L]                 [Col C K  Track 1R]       
                                               
    | //// | / | //// |                 |      | / |      |                   
    | E/// | E | ///@ |                 |      | E |      |
    | //// | @ | //// |                 |      | @ |      |                  
    | @/// | A | ///: |                 |      | A |      |                  
    | //// | : | //// |                 |      | : |      |                  
    | :/// | S | ///# |                 |      | S |      |                  
    | //// | I | //// |                 |      | I |      |                  
    | #/// | U | ///T |                 |      | U | $$// |                  
    | //// | # | //// |                 |      | # | H:/C |                  
    | T/// | D | $$$$ | const -1        |      | D | VS/F |                  
    | //// | R | $$$$ |                 |      | R | L:/J |                  
    | /E// | J | //// |                 |      | J | MK/A |
    | //// | N | FS/V | hoot            |      | N | XK/: |
    | /@// | F | KS/P |                 |      | F | $ST/ |
    | //// | C | VKTA | RCS Entry       |      | C | UKTR |
    | /:// | K | JS/L |                 |      | K | MK/S |
    | //// | T | VSTF |                 |      | T | MKQO |
    | /#// | Z | CK/H |                 |      | Z | //QT |                  
    | //// | L | $S/: |                 |      | L | $SQB |                  
    | /T// | W | /ET/ |                 |      | W | FS/P |__                  
    | //// | H | /ATN |                 |      | H |      |
    | //E/ | Y | MK/S |                 |      | Y |      |  Pseudo                
    | //// | P | Y:/C |                 |      | P |      |  accumulator            
    | //@/ | Q | MK/N |                 |      | Q |      |__              
    | //// | O | VSTJ |                 |      | O |      |              
    | //:/ | B | RSTR |                 |      | B |      |__              
    | //// | G | Y:TN |                 |      | G |      |              
    | //#/ | " | KS/H |                 |      | " |      | Special              
    | //// | M | VKT/ |                 |      | M |      | Working
    | //T/ | X | VS/P |__               |      | X |      | Space
    | //// | V | TEUN | cue (default    |      | V |      |
    | ///E | $ | @/EZ | cue is WRITE)   |      | $ |      |
                  
    [End]
                         
    The Routine changing sequence is entered
    by instruction N S / P

       - The cue to obey is set in [VS, $S]
       - Load AL with return link 
       - Issue NS/P instr, 
    
    (from Ferranti Mark I Programming Manual, second edition 
     Computer History Museum, 102724593-05-01-acc.pdf
     p57, section True and false cues, Scheme A)

     Cue decription     

         true cue  = tt ck MGIN   (if digit 39=0)
         false cue = tt ck nnTK   (if digit 39=1)

         Binary Digits 0..9 (tt, first 2 chars): 

         control transfer number. One less than the number of 
         line at witch the routine starts (the routine entry point)

         Binary Digits 0..19 (ck, following 2 chars): check characters 

         ck = [/E]-[/A] of routine entered.       
         The ck chars allows to ensure the loaded routine is the expected one. if
         ck in cue does not match the calculated ck of loaded routine then the cue
         can be pointing to wrong track/the loaded routine is not the expected one.
         In this case, execution stops with continuous hoot.

         if digit 39=0 -> true cue. Then:
            Digits 20..39 (MGIN, last 4 chars) = Magnetic instr lo load routine   
 
         if digit 39=1 -> false cue. Then:
            Digits 20..29 (nn, following 2 chars) = line in directory that contains 
                                                    Magnetic instr lo load routine    
            Digits 30..39 (TK, following 2 chars) = track containing directory (bit 39 
                                                    set to zero)

            A directory track can hold up to 64 directory lines.
            typical track to use as dir is TR=EZ (track 33L)


     RCS call

         Subroutine call sequence:

                  cc/T  Load AL with cue of routine to call
                  VSTA  Store it in [VS]
                  aa/T  Load AL with return link
             ss   NS/P  call RCS
             ss+1 ...   execution will continue here when subrourine
                        returns (with NS/P instr)

             cc   ttck  40bit cue (ttck MMMM) for 
             cc+1 MMMM  subrourine

             aa   ssck 40bit cue (ssck NNNN) for current 
             aa+1 NNNN routine, just ss set as return address

         A subroutine call needs 8 short lines

         On entry of subroutine, AL holds the return cue. So first instr 
         should be VSTA that stores the return link as new cue. 

                  VSTA  save return address to caller
                  ...
                                    
                  NS/P return to caller

         If the subroutine calls a sub-subroutine, it is important to 
         preserve the return cue:


                  bbTA  save return address to caller (not on [VS])
                  ...
                  cc/T  Load AL with cue of routine to call
                  VSTA  Store it in [VS]
                  aa/T  Load AL with return link back to this routine
                  NS/P  call RCS
                  ...

                  bb/A  restore return address (the link)
                  VSTA  as cue to return to to caller
                  NS/P  return to caller


         If CK characters suplied on cue are not the ones calculated by
         RCS, a contonuous hoot stop will occur. The calculated correct CK chars
         can be viewed in debug trace of simulator

            DBG(...)> CPU DATA: Fetch 0183 (QS): 393724 (MK/N) at Tube S2R line 23 

            DBG(...)> CPU CMD: Exec MK/N (addr=0508 func=/N) A=A+D*S unsigned 
            ...

            DBG(...)> CPU DATA: ... AL: 1031799769010 (LXEV /V/V) AM: 00015 (K/// ////) 

                                                         ^^ 
                 These are the calculated CK chars ------++ 


S U M M A R Y
    
    Normal uses of pages                                       
    
    Column   Page   Use
    
    /        0      Instructions
    E
    
    @        1      Instructions
    A
    
    :        2      PERM
    S
    
    I        3      free (Reserve for PERM or)
    U               free (systematic or unsystematic Working Space)
    
    #        4      free (Syst. W.S.)
    D
    
    R        5      free (Reserve syst.W.S.)
    J
    
    N        6      free (Reserve syst. or)
    F               free (unsyst. W.S.)
    
    C        7      free (Unsyst. W.S.)
    K               PERM GK-VK spec.
    
    N S / P  Enters routine changing sequence
    K S / P  Enters coninuous hoot


		
Annotated program

      =Addr=  Instr Notes
      174 CS  VKTA  RCS (Routine Changing Sequence) Entry: Save AL [VK, $K]=AL=return link, A=0
      175 KS  JS/L  Stop if L switch set
      176 TS  VSTF  AL=-[VS,$S=cue to call], sign extend to AM
      177 ZS  CK/H  if AM>=0 goto 488  (transfer if is a false cue)
      178 LS  $S/:  true cue: exec mag instr from cue to load the routine from drum
      179 WS  /ET/  AL=[/E] = @LWP H:/C of loaded routine
      180 HS  /ATN  Sub [/A] -> now AL=[/E]-[/A] -> check chars of loaded routine
      181 YS  MK/S  [MK, XK]=check char of loaded routine in MK
      182 PS  Y:/C  D=//E/ //// 
      183 QS  MK/N  shift to setup calculated check -> AL=..CK ....
      184 OS  VSTJ  XOR Check char calc with check chars in cue (CK chars from cue in [VS])
      185 BS  RSTR  AND $$$$ //// to keep only lower 20 bits
      186 GS  Y:TN  sub //E/ 
      187 "S  KS/H  if (A>=0) then goto continuous hoot: if CK in cue not same as CK in loaded routine -> not the expected rutine -> hoot
      188 MS  VKT/  AL=[VK, $K] to restore return link in AL
      189 XS  VS/P  goto [VS=cue] goto to entry point in new loaded routine given by the cue
      
      487 UK  $$// 
      488 #K  H:/C  D=//// //E/. false cue = TTCK NNTK where NN=line in dir, TK=dir track
      489 DK  VS/F  Mult to shift: ///# V//T --> AM= /#V/ /T$$ = CKNN TK$$
      490 RK  L:/J  add base mag instr to access directory
      491 JK  MK/A  [XK] = mag instr tr=TK, func=0, tube=0
      492 NK  XK/:  Mag Instr at [XK]: Read from Track TK Left to Tube S0
      493 FK  $ST/  AL=NNTK .... from saved destination address
      494 CK  UKTR  AND $$// -> keep first two chars
      495 KK  MK/S  [MK]=NN// -> the entry to take inside directory track
      496 TK  MKQO  B7=[MK]=NN
      497 ZK  //QT  B7=[//+B7]=[tube S0 + B7]=read into B7 the dir entry NN from tube S0
      498 LK  $SQB  [$S]=B7 save the dir entry -> is the maginstr from dir
      499 WK  FS/P  goto 176
