;---------------
DestAdr         EQU $00001000
****************
* ST-loader    *
startste:         pea     jag_end-startste+256.w
                  pea     startste-256(pc)
                  clr.w   -(sp)
                  move.w  #74,-(sp)
                  trap    #1
                  lea     12(sp),sp

                  clr.l   -(sp)
                  move.w  #32,-(sp)
                  trap    #1
                  addq.l  #6,sp
                  move.l  d0,-(sp)
 moveq #TimeOutInit,d0
 moveq #0,d1
 add.l $4ba.w,d0
get_ti addq.w #1,d1
 cmp.l $4ba.w,d0
 bhi.s get_ti
 lea TimerInit(pc),a0
 move.w d1,(a0)+
 moveq #FirstTimeOutInit,d0
 moveq #0,d1
 add.l $4ba.w,d0
get_ti1 addq.w #1,d1
 cmp.l $4ba.w,d0
 bhi.s get_ti1
 move.w d1,(a0)


                  bsr.s   SendPRG2Jaggy
                  move.w  #32,-(sp)
                  trap    #1
                  addq.l  #6,sp

                  clr.w   -(sp)
                  trap    #1

* Routinen um vom STE/Falcon ber die JoyPad-Ports ein Programm
* an den Jaguar zu schicken
*
* (c) 1994 Bastian Schick
* Version 4  -  29.5.95


SendPRG2Jaggy:    sf $ffff9203.w
                  lea     jag_prg(pc),a0
                  move.l  #(jag_end-jag_prg)>>2,d2
                  move.l  #DestAdr,d1
                  bsr     WriteDumpL
                  beq.s   error_sp
                  move.w  #StartSub,d0
                  bsr   SendWord_STE
                  beq.s   error_sp
                  move.l  #DestAdr,d0
                  bsr     SendLong_STE
error_sp:         rts
                  ;>ENDPART
                  ;>PART 'Print'
Print:            movem.l d0-d2/a0-a2,-(sp)
                  move.l  7*4(sp),-(sp)
                  move.w  #9,-(sp)
                  trap    #1
                  addq.l  #6,sp
                  movem.l (sp)+,d0-d2/a0-a2
                  move.l  (sp)+,(sp)
                  rts
*************************
* transmission-routines *
*************************
StartSub          EQU $00000083
WriteLongs        EQU $00000088
FirstTimeOutInit  EQU 120
TimeOutInit       EQU 70


pushSendRegs    MACRO
                  movem.l d1-d3/d5-a2,-(sp)
                ENDM
pullSendRegs    MACRO
                  movem.l (sp)+,d1-d3/d5-a2
                ENDM

SendLong_STE:     pushSendRegs
                  lea     $FFFF9201.w,a0
                  lea     2(a0),a1
                  move.w  TimerInit(pc),d5
                  move.w  TimerInit1(pc),d7
                  moveq   #3,d3
SendLong_STE1:    btst    d3,(a0)
                  beq.s   __0              ; auf BUSY high warten
SendLong_STE2:    rol.l   #3,d0
                  moveq   #$10,d2          ; STROBE high
                  moveq   #9,d6            ; 10*3 Bit

SendLong_STE3:    moveq   #$E0,d1
                  and.w   d0,d1
                  rol.l   d3,d0
                  or.b    d2,d1
                  move.w  d5,d7
SendLong_STE4:    btst    d3,(a0)
                  beq.s   __1              ; auf BUSY high warten
                  move.b  d1,(a1)          ; Bit mit STROBE
;                  move.w  d5,d7
SendLong_STE6:    btst    d3,(a0)
                  bne.s   __2
                  sf (a1)                  ; STROBE low
                  dbra    d6,SendLong_STE3
                  moveq   #$C0,d1
                  and.w   d0,d1
                  or.b    d2,d1
                  move.w  d5,d7
SendLong_STE8:    btst    d3,(a0)
                  beq.s   __3              ; auf BUSY high warten
                  move.b  d1,(a1)          ; Bit mit STROBE
SendLong_STE10:   btst    d3,(a0)
                  bne.s   __4
                  sf (a1)                  ; STROBE low
                  moveq   #-1,d0
                  pullSendRegs
                  rts

__1:              dbra    d7,SendLong_STE4
                  bra     TimeOut2
__2:              dbra    d7,SendLong_STE6
                  bra     TimeOut2
__3:              dbra    d7,SendLong_STE8
                  bra     TimeOut2
__4:              dbra    d5,SendLong_STE10
                  bra     TimeOut2
__0:              dbra    d7,SendLong_STE1
                  moveq   #0,d0
                  pullSendRegs
                  rts

SendWord_STE      pushSendRegs
                  lea     $000004BA.w,a2
                  moveq   #FirstTimeOutInit,d7
                  add.l   (a2),d7
                  lea     $FFFF9201,a0

SendWord_STE2:    btst    #3,(a0)
                  bne.s   SendWord_STE3
                  cmp.l   (a2),d7
                  bhi.s   SendWord_STE2
                  moveq   #0,d0
                  pullSendRegs
                  rts

SendWord_STE3:    lea     2(a0),a1
                  rol.w   #8,d0
                  moveq   #$10,d2
                  moveq   #3,d3
                  moveq   #7,d6
SendWord_STE4:    moveq   #$C0,d1
                  and.w   d0,d1
                  rol.w   #2,d0
                  or.b    d2,d1

                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
SendWord_STE5:    btst    #3,(a0)
                  bne.s   SendWord_STE6    ; auf BUSY high warten
                  cmp.l   (a2),d7
                  bhi.s   SendWord_STE5
                  bra     TimeOut2

SendWord_STE6:    move.b  d1,(a1)          ; Bit mit STROBE
                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
SendWord_STE7:    btst    #3,(a0)
                  beq.s   SendWord_STE8    ; auf BUSY low warten
                  cmp.l   (a2),d7
                  bhi.s   SendWord_STE7
                  bra     TimeOut2
SendWord_STE8:    clr.b   (a1)             ; STROBE low

                  dbra    d6,SendWord_STE4
                  moveq   #-1,d0
                  pullSendRegs
                  rts
                  ;>ENDPART
                  ;>PART 'GetWord_STE'
GetWord_STE:      pushSendRegs
                  moveq   #FirstTimeOutInit,d7
                  add.l   $000004BA.w,d7
                  lea     $FFFF9201,a0
GetWord_STE1:     btst    #3,(a0)
                  beq.s   GetWord_STE2
                  cmp.l   $000004BA.w,d7
                  bhi.s   GetWord_STE1

                  moveq   #0,d0
                  pullSendRegs
                  rts

GetWord_STE2:     lea     2(a0),a1
                  moveq   #0,d0
                  moveq   #0,d2

                  moveq   #7,d6
GetWord_STE3:     st      (a1)             ; /Busy high
                  bsr     _WaitStrobeHigh
                  move.b  (a1),d1
                  sf      (a1)             ; /Busy low
                  add.b   d1,d1
                  roxl.w  #1,d0
                  add.b   d1,d1
                  roxl.w  #1,d0
                  bsr     _WaitStrobeLow
                  dbra    d6,GetWord_STE3
                  moveq   #-1,d1
                  pullSendRegs
                  rts
                  ;>ENDPART
                  ;>PART 'GetLong_STE'
GetLong_STE:      pushSendRegs
                  lea     $000004BA.w,a2
                  moveq   #20,d7
                  add.l   (a2),d7
                  lea     $FFFF9201,a0
                  sf      2(a0)
GetLong_STE1:     btst    #3,(a0)
                  beq.s   GetLong_STE2
                  cmp.l   (a2),d7
                  bhi.s   GetLong_STE1
                  sf      2(a0)
                  moveq   #0,d0
                  pullSendRegs
                  rts

GetLong_STE2:     lea     2(a0),a1
                  moveq   #0,d0
                  moveq   #0,d2
                  moveq   #9,d6
GetLong_STE3:     st      (a1)             ; Busy high
                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
GetLong_STE6:     btst    #3,(a0)
                  bne.s   GetLong_STE7
                  cmp.l   (a2),d7
                  bhi.s   GetLong_STE6
                  bra.s   TimeOut2
GetLong_STE7:     moveq   #$E0,d1
                  and.b   (a1),d1
                  sf      (a1)             ; Busy low
                  rol.l   #3,d0
                  or.b    d1,d0

                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
GetLong_STE8:     btst    #3,(a0)
                  beq.s   GetLong_STE9
                  cmp.l   (a2),d7
                  bhi.s   GetLong_STE8
                  bra.s   TimeOut2
GetLong_STE9:     dbra    d6,GetLong_STE3

                  st      (a1)
                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
GetLong_STE10:    btst    #3,(a0)
                  bne.s   GetLong_STE11
                  cmp.l   (a2),d7
                  bhi.s   GetLong_STE10
                  bra.s   TimeOut2

GetLong_STE11:    moveq   #$C0,d1
                  and.b   (a1),d1
                  sf      (a1)             ; Busy low
                  rol.l   #3,d0
                  or.b    d1,d0
                  rol.l   #1,d0

                  moveq   #TimeOutInit,d7
                  add.l   (a2),d7
GetLong_STE12:    btst    #3,(a0)
                  beq.s   GetLong_STE13
                  cmp.l   (a2),d7
                  bhi.s   GetLong_STE12
                  bra.s   TimeOut2
GetLong_STE13:    moveq   #-1,d1
                  pullSendRegs
                  rts
                  ;>ENDPART
                  ;>PART '_WaitBusyHigh'
_WaitStrobeHigh:  moveq   #60,d7
                  add.l   $000004BA.w,d7
_wait0_sw:        btst    #3,(a0)          ; 12
                  bne.s   _ok_wbh          ;  8    auf BUSY high warten
                  cmp.l   $000004BA.w,d7
                  bhi.s   _wait0_sw
                  bra.s   TimeOut
_ok_wbh:          rts
                  ;>ENDPART
                  ;>PART '_WaitBusyLow'
_WaitStrobeLow:   moveq   #60,d7
                  add.l   $000004BA.w,d7
_wait_bl:         btst    #3,(a0)          ; 12
                  beq.s   _ok_wbl          ;  8    auf BUSY high warten
                  cmp.l   $000004BA.w,d7
                  bhi.s   _wait_bl
                  ;>ENDPART
                  ;>PART 'TimeOut'
TimeOut:          addq.l  #4,sp
TimeOut2:         pullSendRegs
                  sf      $FFFF9203.w
                  pea     timeouttxt(pc)
                  bsr     Print
                  moveq   #0,d0
_ok_wbl:          rts
timeouttxt:          DC.B "TimeOut",13,10,0
                  EVEN

WriteDumpL:       movem.l d0-d2/a0-a2,-(sp)
                  sf      $FFFF9203.w

again_wdl:        move.w  #WriteLongs,d0
                  bsr     SendWord_STE     ; Commando
                  beq.s   exit_wdl

                  move.l  d1,d0
                  bsr     SendLong_STE     ; Start-Adress
                  beq.s   exit_wdl

                  move.l  d2,d0
                  bsr     SendLong_STE     ; Anzahl der Lang-Worte
                  beq.s   exit_wdl

                  sf      $FFFF9203.w

wait_wdl:         bsr     GetWord_STE
                  beq.s   exit_wdl1
                  cmp.w   #WriteLongs,d0
                  bne.s   exit_wdl
                  moveq   #0,d3
loop_wdl:         move.l (a0)+,d0
                  move.l  d0,d1

                  eor.l   d3,d1
                  add.l   d1,d3

                  bsr     SendLong_STE
                  beq.s   exit_wdl

                  dbra    d2,loop_wdl
                  sub.l   #$00010000,d2
                  bpl.s   loop_wdl
                  bsr     GetLong_STE
                  cmp.l   d0,d3
                  bne.s   error_wdl

                  moveq   #-1,d0
                  movem.l (sp)+,d0-d2/a0-a2
                  rts
exit_wdl1:        pea     timeout1(pc)
                  bra.s   cont_wdl

exit_wdl:         pea     timeouttxt(pc)
                  bra.s   cont_wdl
error_wdl:        pea     WriteDumpErr(pc)
cont_wdl:         bsr     Print
                  moveq   #0,d0
                  movem.l (sp)+,d0-d2/a0-a2
                  rts
timeout1:         DC.B "WriteDumpL-TimeOut",13,10,0
WriteDumpErr:     DC.B "Error in transmission !",13,10,0
                  EVEN

                  ;>ENDPART
                  ;>PART 'WriteDumpL(a0.l,d1.l,d2.l)'
_WriteDumpL:       movem.l d0-d2/a0-a2,-(sp)
                  sf      $FFFF9203.w

again_wdl2:        move.w  #WriteLongs,d0
                  bsr     SendWord_STE     ; Commando
                  beq.s   exit_wdl2

                  move.l  d1,d0
                  bsr     SendLong_STE     ; Start-Adress
                  beq.s   exit_wdl2

                  move.l  d2,d0
                  bsr     SendLong_STE     ; Anzahl der Lang-Worte
                  beq.s   exit_wdl2

                  sf      $FFFF9203.w

wait_wdl2:         bsr     GetWord_STE
                  beq.s   exit_wdl21
                  cmp.w   #WriteLongs,d0
                  bne.s   exit_wdl2
                  moveq   #0,d3
loop_wdl2:         move.l (a0)+,d0
                  move.l  d0,d1

                  eor.l   d3,d1
                  add.l   d1,d3

                  bsr     SendLong_STE
                  beq.s   exit_wdl2

                  dbra    d2,loop_wdl2
                  sub.l   #$00010000,d2
                  bpl.s   loop_wdl2
;>                  bsr     GetLong_STE
;>                  cmp.l   d0,d3
;>                  bne.s   error_wdl2

                  moveq   #-1,d0
                  movem.l (sp)+,d0-d2/a0-a2
                  rts
exit_wdl21:        pea     timeout1(pc)
                  bra.s   cont_wdl2

exit_wdl2:         pea     timeouttxt(pc)
                  bra.s   cont_wdl2
error_wdl2:        pea     WriteDumpErr(pc)
cont_wdl2:         bsr     Print
                  moveq   #0,d0
                  movem.l (sp)+,d0-d2/a0-a2
                  rts
                  EVEN
****************
jag_prg:
;--------------
