
EM_Jet_List   IDNT                       ; title  EM Jet List Builder

        SECTION   Sect_EM_Jet_List,4,C   ; Declare a noncommon code section.

***************************************************************************
*  EM Jet List Builder Routine for the L1 Data Block    Rev. 18-APR-1992  *
*                                                                         *
*                                                                         *
*      Registers used in this routine:                                    *
*                                                                         *
*   A0  Base Address of EM Reference Set 0 Jet Masks.                     *
*   A1  Base Address of EM Reference Set 1 Jet Masks.                     *
*   A2  Base Address of EM Reference Set 2 Jet Masks.                     *
*   A3  Base Address of EM Reference Set 3 Jet Masks.                     *
*   A4  Pointer to the Raw EM Jet List working area.                      *
*   A5  Pointer to Trigger Tower Phi Index Relative Address Offset Table. *
*   A6  Temporary pointer to data.                                        *
*                                                                         *
*   D0  Temporary storage of a Reference Set 0 Mask of Jet Patterns data. *
*   D1  Temporary storage of a Reference Set 1 Mask of Jet Patterns data. *
*   D2  Temporary storage of a Reference Set 2 Mask of Jet Patterns data. *
*   D3  Temporary storage of a Reference Set 3 Mask of Jet Patterns data. *
*   D4  Temporary storage,  Bit Offset during HitProcess.                 *
*   D5  Storage flag to indicate if any of the Jet Masks had set bits.    *
*   D6  Flag to indicate if any SpecTrigs fired that depended on a RefSet.*
*   D7  Temporary storage                                                 *
*                                                                         *
***************************************************************************

***************************************************************************
*                                                                         *
*     Initialization section for the EM Jet List Builder                  *
*                                                                         *
***************************************************************************

Begin_EM_Jet_List   CLR.L  JetListCNT     ; Zero the count of the number of
                                          ; items in the EM Jet List.

        MOVEA.L   #BAEMM0,A0    ; EM Mask Ref 0 Base Adrs ---> register A0.
        MOVEA.L   #BAEMM1,A1    ; EM Mask Ref 1 Base Adrs ---> register A1.
        MOVEA.L   #BAEMM2,A2    ; EM Mask Ref 2 Base Adrs ---> register A2.
        MOVEA.L   #BAEMM3,A3    ; EM Mask Ref 3 Base Adrs ---> register A3.


        MOVEA.L   #EtIndexList,A6         ; Base Adrress of the Et Index List
                                          ; that will be used to order the
                                          ; Raw EM Jet List stored in reg A6.
        FOR.B     D7 = #1 TO #18  DO.S    ; Clear the memory locations where
          CLR.L   (A6)+                   ; the Et Index List is going
        ENDF                              ; to be built.


        MOVEA.L   #RawEMList,A4           ; Base Adrs of the Raw EM Jet List.

        MOVEA.L   #RelPhiAdrs,A5          ; Base Adrs of the table of relative
                                          ; phi offsets is loaded into A5.
***************************************************************************
*                                                                         *
*   Build D6 so that it indicates if any Specific Triggers fired on this  *
*   event that depended on a given EM Reference Set.                      *
*                                                                         *
*       Register D6 bits 31..24 will be FF if for this event at least     *
*   one Specific Trigger fired that depended on EM Ref Set 3.             *
*       Register D6 bits 23..16 will be FF if for this event at least     *
*   one Specific Trigger fired that depended on EM Ref Set 2.             *
*       Register D6 bits 15...8 will be FF if for this event at least     *
*   one Specific Trigger fired that depended on EM Ref Set 1.             *
*       Register D6 bits 7...0 will be FF if for this event at least      *
*   one Specific Trigger fired that depended on EM Ref Set 0.             *
*                                                                         *
*   If no specific Trigger fired that depended on a given EM Ref Set      *
*   then that section of D6 will be 00.                                   *
***************************************************************************

        CLR.L    D6         ; Clear register D6.  D6 will hold the flags that
                            ; showed if any if any Spec Trigs fired that
                            ; depended on EM Ref Set 0, 1, 2, or 3.

        MOVEA.L  #BAPrgFire+16,A6     ; Load register A6 with a pointer
                                      ; to the longword above the Prog
                                      ; AND Fired data for EM RefSet 3.

        TST.L  -(A6)                  ; Set Condition Codes according to the
                                      ; Longword which indicates Spec Trig's
                                      ; that fired and depended on EM RefSet 3.
        BEQ.S   NoEMRef3        ; Branch if NO Spec Trigs fired on this event
                                ;   that depended on EM Ref Set 3.
        SNE      D6             ; Store 1's if a Trg depended on EM Ref Set 3.

NoEMRef3   LSL.L   #8,D6        ; Shift the EM Ref Set 3 flag up by 8.

        TST.L  -(A6)                  ; Set Condition Codes according to the
                                      ; Longword which indicates Spec Trig's
                                      ; that fired and depended on EM RefSet 2.
        BEQ.S   NoEMRef2        ; Branch if NO Spec Trigs fired on this event
                                ;   that depended on EM Ref Set 2.
        SNE      D6             ; Store 1's if a Trg depended on EM Ref Set 2.

NoEMRef2   LSL.L   #8,D6        ; Shift the EM Ref Set 2 flag up by 8.

        TST.L  -(A6)                  ; Set Condition Codes according to the
                                      ; Longword which indicates Spec Trig's
                                      ; that fired and depended on EM RefSet 1.
        BEQ.S   NoEMRef1        ; Branch if NO Spec Trigs fired on this event
                                ;   that depended on EM Ref Set 1.
        SNE      D6             ; Store 1's if a Trg depended on EM Ref Set 1.

NoEMRef1   LSL.L   #8,D6        ; Shift the EM Ref Set 1 flag up by 8.

        TST.L  -(A6)                  ; Set Condition Codes according to the
                                      ; Longword which indicates Spec Trig's
                                      ; that fired and depended on EM RefSet 0.
        BEQ.S   NoEMRef0        ; Branch if NO Spec Trigs fired on this event
                                ;   that depended on EM Ref Set 0.
        SNE      D6             ; Store 1's if a Trg depended on EM Ref Set 0.


NoEMRef0   IF.L  D6 <EQ> #0  THEN.S   ; If for this event no Spec Trigs fired
             JMP  OrderEMList         ; that depended on a Trigger Tower being
           ENDI                       ; above an EM Ref Set then skip looking
                                      ; through all the EM Jet Masks.

***************************************************************************
*                       EM Tower Processor                                *
*                                                                         *
*  This Tower Processor routine handles sorting through the Trigger       *
*  Towers by testing the EM Jet Masks and looking for candidates for the  *
*  EM Jet List.                                                           *
*                                                                         *
*  Branch to the HitProcessor only if there is an Jet Mask for an EM      *
*  Ref Set Set which shows at least one Trigger Tower that is above       *
*  threshold AND at least one Spec Trig fired that depended on there      *
*  being a Trigger Tower over threshold in that Ref Set.                  *
*                                                                         *
***************************************************************************

    MOVEA.L   #EtIndexList,A6           ; Base Adrress of the Et Index List
                                        ; that will be used to order the
                                        ; Raw EM Jet List stored in reg A6.

    FOR.B  EtaIndex = #1 TO #20  DO     ; Loop over the tower Eta Index.

      CLR.L    EtaSign          ; At this Eta Index first look at the Positive
                                ; Eta Trigger Tower,  i.e. EtaSign = zero.

        MOVE.L   (A3),D3        ; Get next Ref 3 EM Mask, increment pointer.
        SNE      D5             ; Store 1's if EM Ref 3 Mask is not all 0's.
        LSL.L    #8,D5          ; Shift the Ref 3 Mask all 0's flag up by 8.

        MOVE.L   (A2),D2        ; Get next Ref 2 EM Mask, increment pointer.
        SNE      D5             ; Store 1's if EM Ref 2 Mask is not all 0's.
        LSL.L    #8,D5          ; Shift the Ref 2 Mask all 0's flag up by 8.

        MOVE.L   (A1),D1        ; Get next Ref 1 EM Mask, increment pointer.
        SNE      D5             ; Store 1's if EM Ref 1 Mask is not all 0's.
        LSL.L    #8,D5          ; Shift the Ref 1 Mask all 0's flag up by 8.

        MOVE.L   (A0),D0        ; Get next Ref 0 EM Mask, increment pointer.
        SNE      D5             ; Store 1's if EM Ref 0 Mask is not all 0's.

                               ; D5 now contans flags which indicate if any
                               ; of these EM Jet Masks contain any set bits.
                               ; If D5 is all zeros then there were no bits
                               ;   set in any of the EM Jet Masks.
                               ; If D5 bits 31-24 are all 1's then at least
                               ;   one bit was set in this Ref 3 EM Jet Mask.
                               ; If D5 bits 23-16 are all 1's then at least
                               ;   one bit was set in this Ref 2 EM Jet Mask.
                               ; If D5 bits 15-8 are all 1's then at least
                               ;   one bit was set in this Ref 1 EM Jet Mask.
                               ; If D5 bits 7-0 are all 1's then at least
                               ;   one bit was set in this Ref 0 EM Jet Mask.

        AND.L    D6,D5       ; AND D5, the flags that show which Ref Sets had
                             ; at least one Trigger Tower above threshold
                             ; with D6 the flags that show which RefSets had:
                             ;    at least one Specific Trigger programmed
                             ;    to depend on these Ref Sets  AND
                             ;    at least one of these Spec Trig's fired.
                             ; Store this in register D5.

        BEQ.S NoPosHit       ; Branch around the JSR HitProcessor if
                             ; none of the Jet Masks at the current eta
                             ; contain a hit and a Spec Trig fired that
                             ; depends on that hit.  I.E. branch if the
                             ; "mask" in D5 is all zero's.

        IF.L  JetListCNT <GT> #MaxJLCNT THEN.S  ; Exit if the number of items
          JMP   OrderEMList                     ; in the Jet List is greater
        ENDI                                    ; than MaxJLCNT and flag that
                                                ; the Jet List is incomplete.

        JSR EMHitProc        ; Jump to the HitProcessor because there is
                             ; one or more Trigger Towers at this eta that
                             ; participated in making the decision to trigger
                             ; and there are less the MaxJLCNT entries in
                             ; the Raw Jet List table so far.


NoPosHit  ADDQ.B  #1,EtaSign    ; At this Eta Index now look at the Negative
                                ; Eta Trigger Tower,  i.e. EtaSign = one.

       MOVE.L  OffPosNeg(A3),D3   ; Get next Ref 3 EM Mask, increment pointer.
       SNE     D5                 ; Store 1's if EM Ref 3 Mask is not all 0's.
       LSL.L   #8,D5              ; Shift the Ref 3 Mask all 0's flag up by 8.

       MOVE.L  OffPosNeg(A2),D2   ; Get next Ref 2 EM Mask, increment pointer.
       SNE     D5                 ; Store 1's if EM Ref 2 Mask is not all 0's.
       LSL.L   #8,D5              ; Shift the Ref 2 Mask all 0's flag up by 8.

       MOVE.L  OffPosNeg(A1),D1   ; Get next Ref 1 EM Mask, increment pointer.
       SNE     D5                 ; Store 1's if EM Ref 1 Mask is not all 0's.
       LSL.L   #8,D5              ; Shift the Ref 1 Mask all 0's flag up by 8.

       MOVE.L  OffPosNeg(A0),D0   ; Get next Ref 0 EM Mask, increment pointer.
       SNE     D5                 ; Store 1's if EM Ref 0 Mask is not all 0's.

          ADDQ.L   #4,A3       ; Increment the Address Registers A3 through A0
          ADDQ.L   #4,A2       ; so that they are pointing to the next long
          ADDQ.L   #4,A1       ; work so that they are ready for the next
          ADDQ.L   #4,A0       ; positive eta  (it looks like auto increment).

                               ; D5 now contans flags which indicate if any
                               ; of these EM Jet Masks contain any set bits.
                               ; If D5 is all zeros then there were no bits
                               ;   set in any of the EM Jet Masks.
                               ; If D5 bits 31-24 are all 1's then at least
                               ;   one bit was set in this Ref 3 EM Jet Mask.
                               ; If D5 bits 23-16 are all 1's then at least
                               ;   one bit was set in this Ref 2 EM Jet Mask.
                               ; If D5 bits 15-8 are all 1's then at least
                               ;   one bit was set in this Ref 1 EM Jet Mask.
                               ; If D5 bits 7-0 are all 1's then at least
                               ;   one bit was set in this Ref 0 EM Jet Mask.

        AND.L    D6,D5       ; AND D5, the flags that show which Ref Sets had
                             ; at least one Trigger Tower above threshold
                             ; with D6 the flags that show which RefSets had:
                             ;    at least one Specific Trigger programmed
                             ;    to depend on these Ref Sets  AND
                             ;    at least one of these Spec Trig's fired.
                             ; Store this in register D5.

        BEQ.S NoNegHit       ; Branch around the JSR HitProcessor if
                             ; none of the Jet Masks at the current eta
                             ; contain a hit and a Spec Trig fired that
                             ; depends on that hit.  I.E. branch if the
                             ; "mask" in D5 is all zero's.

        IF.L  JetListCNT <GT> #MaxJLCNT THEN.S  ; Exit if the number of items
          JMP   OrderEMList                     ; in the Jet List is greater
        ENDI                                    ; than MaxJLCNT and flag that
                                                ; the Jet List is incomplete.

        JSR EMHitProc        ; Jump to the HitProcessor because there is
                             ; one or more Trigger Towers at this eta that
                             ; participated in making the decision to trigger
                             ; and there are less the MaxJLCNT entries in
                             ; the Raw Jet List table so far.

NoNegHit  ENDF               ; End of Eta Index Loop.



                             ; Finished scanning all the Jet Masks and
                             ; have found less than the Max number of
                             ; items for the Jet List so now order it.
  JMP  OrderEMList           ; Jump to the routine that orders the EM list.


***************************************************************************
*                                                                         *
*  This is the routine to process a "Hit" in the EM Jet Mask.             *
*                                                                         *
***************************************************************************

;  If processing reaches this routine then at least one of the Reference Sets
;  will have a hit (i.e. a Trigger Tower over threhold for a Ref Set and at
;  least one Specific Trigger that depends on that Reference Set has fired
;  for this event.

EMHitProc   BTST.L  #3,D5               ; Examine D5 to see which Ref Sets
            BEQ.S   NoneEMRef0          ; have hits that participated in the
            JSR     StrWithEMR0         ; trigger.  Start with EM Ref Set 0.
NoneEMRef0  BTST.L  #11,D5              ; Now test for EM Ref Set 1.
            BEQ.S   NoneEMRef1
            JSR     StrWithEMR1
NoneEMRef1  BTST.L  #19,D5              ; Now test for EM Ref Set 2.
            BEQ.S   NoneEMRef2
            JSR     StrWithEMR2
NoneEMRef2  BTST.L  #27,D5              ; Now test for EM Ref Set 3.
            BEQ.S   NoneEMRef3
            JSR     StrWithEMR3
NoneEMRef3  RTS                         ; All EM Ref Sets Checked so return.


***************************************************************************
*                                                                         *
*            This is the routine to process "Hits" that were              *
*            first found in a Jet Mask for EM Reference Set 0.            *
*                                                                         *
*   Note:  The offset loaded into D4 is not the Bit Number of the most    *
*          significant of the set bits in D0;  rather it is the offset    *
*          to the most significant set bit.  e.g.  If Bit Number 31 is    *
*          set in D0 then an offset of 0 is loaded into D4,  if Bit       *
*          Number 2 is set then an offset of 29 is loaded into D4.        *
*                                                                         *
***************************************************************************

StrWithEMR0   BFFFO   D0{0:32},D4     ; Load the offset (i.e. 0...31) of the
              BEQ.S   EMR0NoHit       ; most significant of the set bits in D0
              JSR     EMRnHit         ; into D4.  Then JSR to EMR0Hit. If no
              JMP     StrWithEMR0     ; bit is set in D0 then return. Note the
EMR0NoHit     RTS                     ; offset is not the same as Bit Number.

***************************************************************************
*                                                                         *
*            This is the routine to process "Hits" that were              *
*            first found in a Jet Mask for EM Reference Set 1.            *
*                                                                         *
***************************************************************************

StrWithEMR1   BFFFO   D1{0:32},D4     ; Load the offset (i.e. 0...31) of the
              BEQ.S   EMR1NoHit       ; most significant of the set bits in D1
              JSR     EMRnHit         ; into D4.  Then JSR to EMR1Hit. If no
              JMP     StrWithEMR1     ; bit is set in D1 then return. Note the
EMR1NoHit     RTS                     ; offset is not the same as Bit Number.

***************************************************************************
*                                                                         *
*            This is the routine to process "Hits" that were              *
*            first found in a Jet Mask for EM Reference Set 2.            *
*                                                                         *
***************************************************************************

StrWithEMR2   BFFFO   D2{0:32},D4     ; Load the offset (i.e. 0...31) of the
              BEQ.S   EMR2NoHit       ; most significant of the set bits in D2
              JSR     EMRnHit         ; into D4.  Then JSR to EMR2Hit. If no
              JMP     StrWithEMR2     ; bit is set in D2 then return. Note the
EMR2NoHit     RTS                     ; offset is not the same as Bit Number.

***************************************************************************
*                                                                         *
*            This is the routine to process "Hits" that were              *
*            first found in a Jet Mask for EM Reference Set 3.            *
*                                                                         *
***************************************************************************

StrWithEMR3   BFFFO   D3{0:32},D4     ; Load the offset (i.e. 0...31) of the
              BEQ.S   EMR3NoHit       ; most significant of the set bits in D3
              JSR     EMRnHit         ; into D4.  Then JSR to EMR3Hit. If no
              JMP     StrWithEMR3     ; bit is set in D3 then return. Note the
EMR3NoHit     RTS                     ; offset is not the same as Bit Number.

***************************************************************************
*                                                                         *
*              Subroutine to add a "hit" first found in                   *
*          EM Ref Set 0, 1, 2, or 3 to the Raw EM Jet List.               *
*                                                                         *
*          This routine also adds an entry to the EtIndexList             *
*     that is used to order the Raw EM Jet List into the EM Jet List      *
*                                                                         *
***************************************************************************

EMRnHit    MOVE.L    #31,D7              ; We arrive here with D4 containing
           SUB.L      D4,D7              ; the offset to the most significant
           MOVE.L     D7,D4              ; set bit of the Jet Mask.  What is
                                         ; needed in D4 is the Bit Number of
                                         ; the most sig set bit in the Jet Msk.

           CLR.L   D7                    ; Clear register D7 for building the
                                         ; merged list (i.e. combination from
                                         ; all Ref Sets) of Spec. Trigs. that
                                         ; fired and depended on this Trigger
                                         ; Tower being above threshold.

           BCLR.L  D4,D0                   ; Test if this Trig Tower was
           BEQ.S   Tst0now1                ; above threshold for EM Ref Set 0.
           OR.L    BAPrgFire,D7            ; If so, OR it to the merged list
                                           ; and clear it from Ref Set 0.

Tst0now1   BCLR.L  D4,D1                   ; Test if this Trig Tower was
           BEQ.S   Tst1now2                ; above threshold for EM Ref Set 1.
           OR.L    BAPrgFire+4,D7          ; If so, OR it to the merged list
                                           ; and clear it from Ref Set 1.

Tst1now2   BCLR.L  D4,D2                   ; Test if this Trig Tower was
           BEQ.S   Tst2now3                ; above threshold for EM Ref Set 2.
           OR.L    BAPrgFire+8,D7          ; If so, OR it to the merged list
                                           ; and clear it from Ref Set 2.

Tst2now3   BCLR.L  D4,D3                   ; Test if this Trig Tower was
           BEQ.S   AllMerg                 ; above threshold for EM Ref Set 3.
           OR.L    BAPrgFire+12,D7         ; If so, OR it to the merged list
                                           ; and clear it from Ref Set 3.


AllMerg    MOVE.L  D7,(A4)+              ; Store this merged list of the Spec
                                         ; Trigs that depend on this Trigger
                                         ; Tower in the first longword of this
                                         ; EM Jet List entry in the Raw EM Jet
                                         ; List work area.

           JSR   RelVAXAdrs              ; Calculate the VAX Byte offset to the
                                         ; EM Et data for this Trigger Tower.
                                         ; It will be returned in register D7.
           MOVE.L  D7,(A4)+              ; Store this VAX Byte offset (relative
                                         ; Address) of this Trigger Tower in
                                         ; second longword of this EM Jet List
                                         ; entry in the Raw Jet List work area.

           JSR   AddIndex                ; Run the routine that will add an
                                         ; entry to the EtIndexList that will
                                         ; be used to order the raw EM Jet Lst.

           ADDI.L  #1,JetListCNT         ; Increment the counter for the
                                         ; number of items in the Jet List.

           RTS                           ; The Jet List Entry has been added
                                         ; to the Raw Jet List work area.


***************************************************************************
*                                                                         *
*       This is the routine to Order the EM Jet List.                     *
*                                                                         *
***************************************************************************
;
;  This routine orders the Et Index List and uses the pointers in it to
;  order the Raw EM Jet List thus making the Final Form EM Jet List.
;
;  This routine makes the "header" for the EM Jet List (i.e. the first
;  longword which indicates if the list is complete and how many entries
;  are in it),  it also orders the list in decreasing Et energy, and it
;  writes it to the section of MVME-214 memory where the VBD will read it.
;

OrderEMList    IF.L   JetListCNT <EQ> #0  THEN.S   ; If there are no entries
                 JMP  NoEMList                     ; in the Raw EM Jet List
               ENDI                                ; then make just a header.

         IF.L   JetListCNT <EQ> #1   THEN.S    ; If there is only 1 entry in
           JMP  SkipOrdering                   ; the Raw EM Jet List then
         ENDI                                  ; there is no need to order it.

         IF.L   JetListCNT <GT> #MaxJLCNT  THEN.S    ; If there are more than
           MOVE.L   #MaxJLCNT,D6                     ; MaxJLCNT entries in the
         ELSE.L                                ; Raw EM Jet List then set the
           MOVE.L   JetListCNT,D6              ; list length to MaxJLCNT when
         ENDI                                  ; calling the routing to order
                                               ; the Et Index list.

         MOVEA.L    #EtIndexList,A0            ; Base Adrress of the Et Index
                                               ; list.  This list will be
                                               ; ordered bythe sorting routine.

         JSR        Begin_Sort_EM_Jet_List     ; D6 holds the number of entries
                                               ; in the Et Index List and A0
                                               ; holds base adrs of this list.


SkipOrdering    MOVEA.L  #EtIndexList,A0    ; Base Adrress of the Et Index
                                            ; list.

         MOVEA.L   #RawEMList,A1          ; Put the base address of the Raw
                                          ; EM Jet List into register A1.

         MOVEA.L   #EMJetListBA,A2        ; Put the base address of the final
                                          ; form (i.e. ordered) EM Jet List
                                          ; into register A2.

         MOVE.L    JetListCNT,D0          ; Get bits 0...7 of the list header.
                                                     ; If there are more than
         IF.L   JetListCNT <GT> #MaxJLCNT  THEN.S    ; MaxJLCNT entries in the
           Move.L  #MaxJLCNT,D0                      ; list then set the list
           ORI.L   #$00008000,D0                ; length = MaxJLCNT and set the
         ENDI                                   ; incomplete flag in the final
                                                ; form Jet List header.
         MOVE.L    D0,(A2)+               ; Write out the header into the list.

                                                     ; From here on JetListCNT
         IF.L   JetListCNT <GT> #MaxJLCNT  THEN.S    ; is only used to control
           MOVE.L  #MaxJLCNT,JetListCNT              ; the number of entries
         ENDI                                   ; written into the Final Form
                                                ; EM Jet List,
                                                ; so keep it <= MaxJLCNT.


         FOR.L   D7 = #1 TO JetListCNT  DO.S    ; Write the entries into EM JL
            MOVE.L    (A0)+,D0                  ; Get 1st entry form Index list
            ANDI.L    #$0000001F,D0             ; Mask everything but the Index
            MOVE.L     (A1,D0.L*8),(A2)+        ; Copy the Spec Trigs Fired
            MOVE.L    4(A1,D0.L*8),(A2)+        ; Copy the VAX relative adrs
         ENDF

         FOR.L   D7 = JetListCNT TO #MaxJLCNT  DO.S    ; Pack out the rest of
            CLR.L     (A2)+                            ; the MaxJLCNT entries
            CLR.L     (A2)+                            ; in the final form EM
         ENDF                                          ; Jet List with zeros.

         RTS                              ; The ordered final form EM Jet list
                                          ; has been built so now return.


NoEMList   MOVEA.L   #EMJetListBA,A2   ; Put the base address of the final form
                                       ; (i.e. ordered) EM Jet List into A2.

           CLR.L     (A2)+             ; "Write" the header for this EM Jet
                                       ; List that has zero entries.

           FOR.L   D0 = #1 TO #MaxJLCNT  DO.S   ; "Write" the "entries" into
              CLR.L   (A2)+                     ; the Jet List. Spec Trg Fired
              CLR.L   (A2)+               ; Mask and VAX 32bit relative byte
           ENDF                           ; address of the EM Trigger Tower Et.

           RTS                         ; The Final Form (i.e. ordered) EM Jet
                                       ; List with no entries has been written.


***************************************************************************
*                                                                         *
*          Calculate the Relative BYTE Address in VAX memory              *
*              of the EM Et data for this Trigger Tower                   *
*                                                                         *
***************************************************************************
; This routine calculates the Relative BYTE Address (offset) for the EM Et
; data for this Trigger Tower with respect to the data for the Trigger Tower
; at location eta=1, phi=1.
;
; This relative byte address consists of 3 parts:
;    an offset for the EtaSign
;                                    EtaSign offset = EtaSign * 640
;
;    an offset for the EtaIndex
;                                    EtaIndex offset = EtaIndex * 32
;
;    and an offset that depends on the PhiIndex of this Trigger Tower.
;
; Note that the EtaSign and the EtaIndex may be used directly but that the
; PhiIndex must be dirived by looking at which bit is set in register D4
; (i.e. which bit has been found set in the EM Jet Mask longword).
;
;
;  VME-VAX            byte     byte         VME-VAX            byte     byte
; data bit   Trig    offset   offset       data bit   Trig    offset   offset
; in an EM   Tower   to VAX   to VME       in an EM   Tower   to VAX   to VME
; Jet Mask    Phi    memory   memory       Jet Mask    Phi    memory   memory
; Longword   Index   locatn   locatn       Longword   Index   locatn   locatn
;------------------------------------     ------------------------------------
;   D00     phi 8      14       13           D16     phi 16     30       29
;   D01     phi 7      12       15           D17     phi 15     28       31
;   D02     phi 6      10        9           D18     phi 14     26       25
;   D03     phi 5       8       11           D19     phi 13     24       27
;   D04     phi 4       6        5           D20     phi 12     22       21
;   D05     phi 3       4        7           D21     phi 11     20       23
;   D06     phi 2       2        1           D22     phi 10     18       17
;   D07     phi 1       0        3           D23     phi 9      16       19
;
;   D08     phi 24     15       12           D24     phi 32     31       28
;   D09     phi 23     13       14           D25     phi 31     29       30
;   D10     phi 22     11        8           D26     phi 30     27       24
;   D11     phi 21      9       10           D27     phi 29     25       26
;   D12     phi 20      7        4           D28     phi 28     23       20
;   D13     phi 19      5        6           D29     phi 27     21       22
;   D14     phi 18      3        0           D30     phi 26     19       16
;   D15     phi 17      1        2           D31     phi 25     17       18
;

RelVAXAdrs   CLR.L    D7            ; Clear working register D7.
             MOVE.B   EtaIndex,D7   ; Move the EtaIndex into D7.
             SUBQ.L   #1,D7         ; Subtract 1 because EtaIndex runs between
                                    ; 1...20 and an index of 1 needs 0 offset.
             LSL.L    #5,D7         ; Multiply the  (EtaIndex - 1)  by 32.

          IF.B  EtaSign <EQ> #1  THEN.S   ; If EtaSign is 1 (i.e. negative
            ADDI.L  #640,D7               ; eta's),  then add an offset
          ENDI                            ; of 640

          ADD.L  (A5,D4.L*4),D7  ; Use Address Register Indirect with Index
                                 ; and Displacement addressing (scale is 4)
                                 ; to get the Phi offset from the Phi Offset
                                 ; Table.  Add this phi part to the VAX
                                 ; Address Byte offset to the EM Et data
                                 ; in register D7.

          RTS                ; Return, the relative VAX byte address is in D7.



***************************************************************************
*                                                                         *
*         This is the routine to add an entry to the EtIndexList          *
*             that will be used to order the Raw EM Jet List.             *
*                                                                         *
*    An entry in the EtIndexList has the following form:                  *
*       bits 31...16 are all zeros.                                       *
*       bits 15...8  the Et of the EM Trig Tower in the Raw EM Jet List.  *
*       bits  7...0  are an index number showing which entry in the       *
*                    Raw EM Jet List this EtIndexList entry represents.   *
***************************************************************************

AddIndex   SUB.L   (A5,D4.L*4),D7       ; D7 still contains the VAX memory
                                        ; address byte offset to the Et of the
                                        ; Trigger Tower that we are adding to
                                        ; the list.  Subtracts the the VAX
       ADD.L    $0080(A5,D4.L*4),D7     ; specific phi part of the ards offset.
                                        ; Now add the VME specific phi part to
                                        ; the remaining eta and eta sign parts.
       ADDI.L   #BASEMP,D7              ; Now add the Base Address to the EM
                                        ; Et data in the MVME-214.  Thus D7 now
                                        ; holds the VME address of the byte
                                        ; that contains the Et of the EM Trig
                                        ; Tower that we are adding to the list.

       CLR.L    D4                      ; Get the byte of data from the memory
       MOVE.B   (D7.L*1),D4             ; address stored in register D7.
                                        ; This is the Et for the Trigger Tower
                                        ; that is being added to the list.

       LSL.L    #8,D4                   ; Shift the Et byte to the left by 8.

       OR.L     JetListCNT,D4           ; "Add" the index number that shows
                                        ; which entry to the Raw EM Jet List
                                        ; this Et represents.

       MOVE.L   D4,(A6)+                ; Store this on the Et Index List.

       RTS                              ; The entry has been added to the
                                        ; Et Index list so return.

***************************************************************************
*                                                                         *
*     Constants Section for Jet List Builder                              *
*                                                                         *
*   Export the following symbols to other program modules:                *
*     Begin_EM_Jet_List    This is the label for the entry point          *
*                          for this routine.                              *
*                                                                         *
*   Import the following symbols are from other modules:                  *
*     BAPrgFire  This is the base address of the lists of SpecTrigs that  *
*                are programmed to use a given Ref Set and that fired.    *
*     BASEMP     This is the base address of the CTFE EM Et data.         *
*     Begin_Sort_EMJet_List  This is the entry point for the EM Jet       *
*                            List Sorter.                                 *
*     EMJetListBA    This is the base address of the final form ordered   *
*                    EM Jet List that the VBD will read.                  *
*     BAEMM0     This is the Base Address of EM Jet Mask Ref 0.           *
*     BAEMM1     This is the Base Address of EM Jet Mask Ref 1.           *
*     BAEMM2     This is the Base Address of EM Jet Mask Ref 2.           *
*     BAEMM3     This is the Base Address of EM Jet Mask Ref 3.           *
*                                                                         *
***************************************************************************

           XDEF  Begin_EM_Jet_List      ; Symbols that will be referenced
                                        ; externally to this Program Module.

           XREF  BAPrgFire,BASEMP             ; Symbols that are referenced
           XREF  Begin_Sort_EM_Jet_List       ; in this module but defined in
           XREF  EMJetListBA                  ; another module.
           XREF  BAEMM0,BAEMM1,BAEMM2,BAEMM3  ;


              ALIGN  4               ; Want Long Word alignment.

JetListCNT    DS.L   1               ; Count of the number of items in
                                     ;   the Jet List.

EtaSign       DS.L   1               ; Sign of the Trigger Tower Eta Index.
                                     ;   0 ---> Positive,    1 ---> Negative
                                     ; Access as a byte operand.

EtaIndex      DS.L   1               ; Trigger Tower Eta Index  0...20.
                                     ; Access as a byte operand.

RawEMList     DCB.L  100,$00000000   ; 100 longwords to store the Raw un-
                                     ;   ordered version of the EM Jet List.

EtIndexList   DCB.L  50,$00000000    ; 50 longwords to store a list of the
                                     ;   Et's of the "Jets" in the Raw Jet
                                     ;   List and an Index number to order.

;
;       Trigger Tower Phi Index Relative VAX Address Offset Table
;      -----------------------------------------------------------
;  This table is the map between the Bit Number (i.e. 0...31) of a bit
;  set in a EM Jet Mask longword (i.e. the Phi "address" of the Trigger Tower)
;  and the relative byte address offset of the EM Et data in VAX memory.

RelPhiAdrs   DC.L   14,12,10,8,6,4,2,0,15,13,11,9,7,5,3,1
             DC.L   30,28,26,24,22,20,18,16,31,29,27,25,23,21,19,17
;
;  End of the Trigger Tower Phi Index Relative VAX Address Offset Table.
;
;
;       Trigger Tower Phi Index Relative VME Address Offset Table
;      -----------------------------------------------------------
;  This table is the map between the Bit Number (i.e. 0...31) of a bit
;  set in a EM Jet Mask longword (i.e. the Phi "address" of the Trigger Tower)
;  and the relative byte address offset of the EM Et data in VME memory.

             DC.L   13,15,9,11,5,7,1,3,12,14,8,10,4,6,0,2
             DC.L   29,31,25,27,21,23,17,19,28,30,24,26,20,22,16,18
;
;  End of the Trigger Tower Phi Index Relative VME Address Offset Table.
;


MaxJLCNT      EQU       16               ; Maximum number of entries in the
                                         ; ordered Final Form EM Jet List.

OffPosNeg     EQU       80               ; Byte address offset between the
                                         ; Positive and Negative Eta CHTCR
                                         ; "Mask of Jet Patterns" data.

        END

