
Sort_EM_Jet_List   IDNT                          ; Title EM Jet List SORTER.

         SECTION   Sect_Sort_EM_Jet_List,4,C     ; Declare a noncommon
                                                 ; code section. Align to LW.

*************************************************************************
*    SORT EM Jet List                                     3-MAR-1991    *
*                                                                       *
*    This is the routine to SORT the EM Jet List.  What is actually     *
*    sorted are longwords that have an index number ( i.e. 0, 1, ...    *
*    14, 15, 16) in the LSByte and the Et of the Trigger Tower for      *
*    this EM Jet List entry in the next higher order byte (i.e. bits    *
*    8...15).   Bits 16...31 are zero.                                  *
*                                                                       *
*    This routine receives in register A0 the base address of the list  *
*    of longwords that are to be sorted.  The number of entries in the  *
*    list will be received in register D6.                              *
*                                                                       *
*    The entries are to be sorted so that the biggest is at the base    *
*    address end of the list and the smallest is that top of the list.  *
*                                                                       *
*                  Registers used in this routine:                      *
*     ---------------------------------------------------------------   *
*     A0   Base Address of the list, received from the calling routine. *
*     D0   Temporary storage of a list entry.                           *
*     D1   Temporary storage of a list entry.                           *
*     D5   Flag to show if any entries were swapped:                    *
*            0 --> no pairs swapped on this trip through the list.      *
*     D6   Received as the number of entries in the list and then       *
*          used as a counter for the number of trips through the list.  *
*     D7   used as a pointer to entries in the list.                    *
*                                                                       *
*     A1,A2,A3,A4,A5,A6  not used.                                      *
*     D2,D3,D4           not used.                                      *
*                                                                       *
*                                                                       *
*    Do not call this routine with fewer than 2 entries to sort or      *
*    with more than 16 entries.                                         *
*                                                                       *
*    Example of a sort:                                                 *
*                                                                       *
*         Before the Sort                    After the Sort             *
*      ---------------------              ----------------------        *
*       Address      Data                  Address       Data           *
*      ---------  ----------              ---------   ----------        *
*       BA + 14    0000F305                BA + 14     00001301         *
*       BA + 10    00004A04                BA + 10     00002100         *
*       BA + 0C    00004B03                BA + 0C     00004A04         *
*       BA + 08    0000E702                BA + 08     00004B03         *
*       BA + 04    00001301                BA + 04     0000E702         *
*       BA + 00    00002100                BA + 00     0000F305         *
*                                                                       *
*************************************************************************


*************************************************************************
*                                                                       *
*    Setup the loop counter and the data pointer for the first trip     *
*    through the list.   We receive D6 as the number of entries in      *
*    the list.  D7 is used in Address Register Inderict with Index      *
*    and Displacement addressing to get the data entries in the list.   *
*                                                                       *
*************************************************************************

Begin_Sort_EM_Jet_List    SUBQ.L    #2,D6     ; Subtract 2 from the number
                                              ; of entries in the list.

             LSL.L      #2,D6      ; Multiply this by 4 (because LW entries).
             CLR.L      D7         ; Point to start of the list.
             CLR.L      D5         ; Clear the flag that shows swapping.
             JMP        NextPair

*************************************************************************
*                                                                       *
*    Test if any list entries were swapped during the just completed    *
*    trip through the list  (if D5 equals 0 then no entries were        *
*    swapped and we can return to the calling routine).   Then test     *
*    if this is the last trip through the list and if so then return    *
*    to the calling routine.  If this was not the last trip then setup  *
*    loop counter and data pointer for another trip through the list.   *
*                                                                       *
*************************************************************************

LastPair     IF.L   D5 <EQ> #0  THEN.S    ; Test to see if any pairs swapped
               RTS                        ; during the last trip through the
             ENDI                         ; list. If none swapped then Return.

             IF.L   D6 <LE> #0  THEN.S    ; Test the # of trips through list.
               RTS                        ; All trips over so Return.
             ENDI

             SUBQ.L     #4,D6      ; Test one less pair this time through
             CLR.L      D7         ; the list. Point to start of the list.
             CLR.L      D5         ; Clear the flag that shows swapping.

*************************************************************************
*                                                                       *
*   This is the loop that goes through D6 entry pairs in the list       *
*   (starting from the base address) and swaps were necessary.  D5      *
*   is set non-zero if any swapping is done.                            *
*                                                                       *
*************************************************************************

NextPair     MOVE.L       (A0,D7.L*1),D0       ; Get lower adrs entry of pair.
NextSingle   MOVE.L      4(A0,D7.L*1),D1       ; Get higher adrs entry of pair.

             IF.L        D0 <LT> D1  THEN.S    ; Should these entries swap ?
               MOVE.L    D1,(A0,D7.L*1)        ; Store one entry of the swap.
               ADDQ.L    #1,D5                 ; Set flag to show a pair swap.
               MOVE.L    D0,4(A0,D7.L*1)       ; Store other entry of the swap.
                  IF.L   D7 <GE> D6  THEN.S    ; Is this the last entry pair of
                    JMP  LastPair              ;   this trip through the list ?
                  ENDI
               ADDQ.L    #4,D7                 ; Increment Pointer to entries.
               JMP       NextSingle
             ENDI


             IF.L        D7 <GE> D6  THEN.S    ; Is this the last entry pair of
               JMP       LastPair              ;   this trip through the list ?
             ENDI
             ADDQ.L      #4,D7                 ; Increment Pointer to entries.
             JMP         NextPair


*************************************************************************
*                                                                       *
*    Export the following symbols to other program modules:             *
*                                                                       *
*      Begin_Sort_EM_Jet_List     This is the label for the entry       *
*                                 point to this routine.                *
*************************************************************************

       XDEF     Begin_Sort_EM_Jet_List        ; Entry point for this routine.

       END

