Initialize_CAMAC     IDNT                      ; Title Initialize CAMAC.

        SECTION  Sect_Initialize_CAMAC,4,C     ; Declare a noncommon code
                                               ; section. Align to Longwords.

*****************************************************************************
*                                                                           *
*   Initialize CAMAC                                Rev.  27-MAR-91    MT   *
*****************************************************************************

*****************************************************************************
*                                                                           *
*   Layout of the Addressing of the "Internal Registers" in the             *
*   CES CAMAC Branch Driver CBD 8210                                        *
*                                                                           *
*   All Internal Registers are found at:  Branch Adrs = ?,  CR=0,  N=29.    *
*                                                                           *
*   Address Layout:                                                         *
*                                                                           *
*     MSByte                                                   LSByte       *
*   ---------- ----------    ------- ---------- ---------- -------------    *
*   2 2    2 2 1    1 1 1    1 1 1 1 1    1                                 *
*   3 2    1 0 9    8 7 6    5 4 3 2 1    0 9 8 7    6 5 4 3 2    1    0    *
*   ___    _____    _____    _________    _______    _________    _    _    *
*         Branch    Crate     Station     Sub Adr     Function   16         *
*          Adrs      Adrs    N Address    A Addres   F Address   Bit        *
*                                                                           *
*            0        0         29           0         0...10     1    0    *
*                                                                           *
*   1 0    0 0 0    0 0 0    1 1 1 0 1    0 0 0 0    0 x x x x    1    0    *
*   __________ __________    _______ __________ __________ _____________    *
*        8         0            E        8           x           x          *
*                                                                           *
*                                                                           *
*   Thus the VME Addresses of the Internal Registers are:                   *
*                                                                           *
*     Internal         Function         VME                                 *
*     Register          Address       Address       Read-Write              *
*   -------------     ----------     ---------     ------------             *
*        CSR                0          80E802       Read-Write              *
*        ITF                4          80E812         Write                 *
*    INT. CONT.1            5          80E816       Read-Write              *
*    INT. CONT.1            1          80E806       Read-Write              *
*    INT. CONT.2            6          80E81A       Read-Write              *
*    INT. CONT.2            2          80E80A       Read-Write              *
*    INT. CONT.3            7          80E81E       Read-Write              *
*    INT. CONT.3            3          80E80E       Read-Write              *
*        CAR                8          80E822       Read-Write              *
*        BTB                9          80E826         Read                  *
*         BZ                9          80E826         Write                 *
*         GL               10          80E82A         Read                  *
*                                                                           *
*****************************************************************************

*****************************************************************************
*                                                                           *
*   Include the "system calls" to the 133A Bug Monitor.                     *
*****************************************************************************

       INCLUDE   68020_EQUATES.INC

*****************************************************************************
*                                                                           *
*  Clear the location that flags errors trying to talk to CAMAC.            *
*                                                                           *
*  Change the Bus Error Vector to point to our own Bus Error exception      *
*  handler.   This exception handler routing starts at BUSEREXHD.           *
*****************************************************************************

Begin_Initialize_CAMAC    CLR.L  IntCAMACError    ; Clear the Initialize CAMAC
                                                  ; Error Flag.  If this flag
                                                  ; .NEQ. zero than there has
                                                  ; been a CAMAC error.

       MOVE.L   #BUSEREXHD,$00000008            ; Point the Bus Error vector
                                                ; to our exception handler.

*****************************************************************************
*                                                                           *
*   Initialize the Crate                                                    *
*   INITIALIZE CRATE CONTROLLER WITH COMMANDS:  Z, C, REMOVE I;   CRATE 1   *
*****************************************************************************

* Z IS N_28,A_6,F_26, 16 BIT OPERATION

       MOVE   $81E46A,D0
       TST    $81E46A

* C IS N_28,A_9,F_26, 16 BIT OPERATION

       MOVE    $81E4EA,D0
       TST     $81E4EA

* REMOVE I WITH N_30,A_9,F_24,  16 BIT OPERATION

       MOVE    $81F4E2,D0
       TST     $81F4E2

*****************************************************************************
*                                                                           *
*  We are finished talking to CAMAC so Restore the normal 133A Bug          *
*  Bus Error Vector.                                                        *
*****************************************************************************

       MOVE.L  #$FFF149E2,$00000008          ; Restore normal 133A Bug
                                             ;  Bus Error vector.

***************************************************************************
*                                                                         *
*   Send a message to the 68K console to indicate whether or not the      *
*   CAMAC Initialization has been successful.                             *
***************************************************************************

       PEA.L     BlankLine
       SYSCALL   .WRITELN

       IF.L  IntCAMACError <EQ> #0  THEN.S
           PEA.L     CAMACIntOK                    ; CAMAC Error Flag is zero
           SYSCALL   .WRITELN                      ; so send an OK message.
       ELSE
           PEA.L     CAMACIntBad                   ; CAMAC Error Flag is not
           SYSCALL   .WRITELN                      ; zero so send a CAMAC
       ENDI                                        ; Init failed message.

       PEA.L     BlankLine
       SYSCALL   .WRITELN


       RTS                                   ; Now return from the Initialize
                                             ;  CAMAC Routine.


*************************************************************************
*  Bus Error Exception Handler for Initialize CAMAC                     *
*                                                                       *
*  Mask the rerun bit in the Special Status Word,  then set the CAMAC   *
*  Error Flag,  and then Return.                                        *
*************************************************************************

BUSEREXHD  MOVE.W  (SP)+,D0                ; pull the Status Register
           MOVE.L  (SP)+,D1                ; pull the Program Counter
           MOVE.W  (SP)+,D2                ; pull the Format - Vector Offset
           MOVE.W  (SP)+,D3                ; pull the Internal Register
           MOVE.W  (SP)+,D4                ; pull the Special Status Register

           ANDI.W  #$FEFF,D4               ; Mask the rerun bus cycle bit

           MOVE.W  D4,-(SP)                ; push the Special Status Register
           MOVE.W  D3,-(SP)                ; push the Internal Register
           MOVE.W  D2,-(SP)                ; push the Format - Vector Offset
           MOVE.L  D1,-(SP)                ; push the Program Counter
           MOVE.W  D0,-(SP)                ; push the Status Register

           MOVE.L  #$FF,IntCAMACError      ; Set the CAMAC Error Flag.

           RTE


***************************************************************************
*                                                                         *
*   Constants Section for  Initialize CAMAC                               *
*                                                                         *
***************************************************************************

         XDEF    Begin_Initialize_CAMAC       ; Export this symbol
                                              ; to other modules.

                 ALIGN   4                    ; Align to Long words.

IntCAMACError    DS.L   $00000000             ; Storage location for the CAMAC
                                              ; error flag.

BlankLine        DC.B    5,'     '
CAMACIntOK       DC.B   48,' CAMAC has been initialized without any errors. '
CAMACIntBad      DC.B   48,' There were errors during CAMAC initialization. '

                 END

