; Sprite Moving routine
; Modified for SEKA on 18-2-87

; Special AMIGA Equates

MaxSndLen:  equ  14000

AbsExecBase:  equ  4   ; the only absolute address !
Level3Int:  equ  $6c
ScrPlane:  equ  $40000  ; $35000
SndBuffer:  equ  $45000
BPlane1:  equ  SndBuffer+MaxSndLen ; BPLANE MUST LAST DATA SEGMENT
BPlane2:  equ  BPlane1+3440 ; 3440 = 80Bytes ; 43rows
PField1:  equ  BPlane2+4000
MusicBuffer: equ  PField1+7000

; AMIGA Hardware Addresses
JOY0DAT:   equ  $dff00a
INTREQR:   equ  $dff01e  ; Interrupt request read
DMACON:   equ  $dff096
INTENA:   equ  $dff09a
INTREQ:   equ  $dff09c
AUD0LC:   equ  $dff0a0
AUD0LEN:   equ  $dff0a4
AUD0PER:   equ  $dff0a6
AUD0VOL:   equ  $dff0a8
AUD1LC:   equ  $dff0b0
AUD1LEN:   equ  $dff0b4
AUD1PER:   equ  $dff0b6
AUD1VOL:   equ  $dff0b8

; Exec Library Vector Offsets
Alert:   equ  -108
Permit:   equ  -138
Forbid:  equ  -132
OpenLibrary:  equ  -408
CloseLibrary: equ  -414

; Graphics Library Vector Offsets
Text:   equ  -60
SetFont:   equ  -66
CloseFont:  equ  -78
InitRastPort: equ  -198
InitBitMap:  equ  -390
SetDrMd:   equ  -354
SetAPen:   equ  -342
MovePen:   equ  -240

; Diskfont Library Vectors
OpenDiskFont: equ  -30

Open:   equ  -30
Read:   equ  -42
Close:   equ  -36
  CODE
; 
;  Initialize the Libraries needed!
;
   move.l sp,initialSP  
   move.l AbsExecBase,a6  ; ExecLib Base
   lea  GfxName,a1   ; addr. of library name
   jsr  OpenLibrary(a6)  ; open it
   move.l d0,GfxBase   ; store base pointer for use
   move.l d0,d6    ; save for use
   beq  NoGfx    ; cannot open dos.lib

   lea  SndBuffer,a1
   lea  Sprites,a0
   move.l #S_end-Sprites,d0
lok1:   move.b (a0)+,(a1)+
   dbra d0,lok1
;
; setup the new copper list (will be performed in next VBlank)
;
   move.l AbsExecBase,a6
   jsr  Forbid(a6)   ; stop multitasking

   move.l GfxBase,a0
   add.l #$32,a0    ; point to LOFlist in GfxBase

   move.w #0,index+6
   move.w #21,index+4
   move.w #14,index+2
   move.w #7,index

   move.w #$4000,INTENA  ; Master Interrupt Enable OFF
   move.l Level3Int,OldVector ; store old interrupt vector
   move.l #Handler,Level3Int

   move.w #$c000,INTENA  ; 

   move.w #$0080,DMACON  ; stop Copper DMA
   move.l (a0),OldCopper  ; pointer to old copper list
   move.l #CopList,(a0)  ; my own Coppers..
   move.w #$80A0,DMACON  ; 
;
;  Moving Main Routine
;
mvrt:
   move.b $bfe001,d0   ; wait until mouse button
   and.l #$40,d0
   bne  mvrt  
;
;  reset all resources used!
;
   move.l GfxBase,a0
   add.l #$32,a0    ; point to LOFlist in GfxBase

   move.w #$0080,DMACON  ; stop Copper and Sound
   move.l OldCopper,(a0)  ; restore pointer
   move.w #$8180,DMACON  ; start again ...

   move.w #$4000,INTENA  ; stop interrupts
   move.l OldVector,Level3Int
   move.w #$c000,INTENA  ; start again

   move.l AbsExecBase,a6
   jsr  Permit(a6)   ; restart multitasking
;
;  Ready to give system back
;
   move.l GfxBase,a1
   jsr  CloseLibrary(a6)
   move.l initialSP,sp
   clr.l d0     ; E.T. phone home!!
ILLEGAL
;   rts
;
;  Display System ALERT
;
FontErr: move.l #$60000!$8022,d7
   bra.s DoAlert 
NoGfx:  move.l #$30000!$8002,d7 ; AG_OpenLib!AO_GfxLib
DoAlert: jsr  Alert(a6)
   move.l initialSP,sp
   rts       ; try to escape (without hope!)
;
;  ---------------------------------------------
;  Level 3 Interrupt Handler for VBlank & Copper
;  ---------------------------------------------
;
Handler: movem.l d0-d2/a0-a1,-(a7)
   move SR,-(sp)   ; don't modify registers
   move.w INTREQR,d0   ; get interrupts
   btst #5,d0    ; test for VBlank Interrupt
   bne.s VBlank    ; VBlank Handler
   bra  EndHandler 
;
; VBlank Handler Fine Scrolling Routine
;
VBlank:
   clr.l d2
   lea  xpos,a0
   lea  SndBuffer,a1
mvobj:
   clr.l d0
   move.w 0(a0,d2.w),d0
   lsr.w #1,d0
   bcs.s sethorz
   bclr #0,3(a1)
   bra.s lab1
sethorz: bset #0,3(a1)
lab1:
   move.b d0,1(a1)

   move.b d0,73(a1)
   move.w 8(a0,d2.w),d0
   move.b d0,(a1)
   add.w #17,d0
   move.b d0,2(a1)

   add.l #148,a1    ; point to next Sprite Block
   addq.w #2,d2    ; next sprite !
   cmp.w #6,d2
   bls  mvobj

;
;  Move Sprites
;
   movem.l a2-a3,-(sp)

   clr.l d2
   lea  index,a0
   lea  x_position,a1
   lea  y_position,a2
   lea  xpos,a3
npos:
   move.w 0(a0,d2.w),d0
   cmp.w #256,d0
   bcs.s lok2
   clr.w d0
lok2:  move.b 0(a1,d0.w),d1
   and.w #$ff,d1
   add.w #120,d1
   move.w d1,0(a3,d2.w)

   move.b 0(a2,d0.w),d1
   and.w #$ff,d1
   add.w #44,d1
   move.w d1,8(a3,d2.w)

   addq.w #1,d0
   move.w d0,0(a0,d2.w)

   addq.w #2,d2
   cmp.l #6,d2
   bls  npos

   movem.l (sp)+,a2-a3
;
;  End of Vertical Blank Handler Routine
;
EndHandler:
   move (sp)+,SR
   movem.l (sp)+,d0-d2/a0-a1
   DC.W $4ef9
OldVector: DC.L $0000

GfxName: DC.B 'graphics.library',0
   even

Sprites:
;
;  Sprite Data 4
;
   DC.W $6d60
   DC.W $7e00
   DC.W $00f8,$0000
   DC.W $0108,$00f0
   DC.W $02e8,$01f0
   DC.W $05e8,$03f0
   DC.W $0be8,$07f0
   DC.W $1668,$0ff0
   DC.W $2c68,$1ef0
   DC.W $5868,$3cf0
   DC.W $b868,$7cf0
   DC.W $bfe2,$7ffc
   DC.W $bffa,$7ffc
   DC.W $8062,$7ffc
   DC.W $ff6e,$00f0
   DC.W $0168,$00f0
   DC.W $0168,$00f0
   DC.W $0108,$00f0
   DC.W $01f8,$0000

   DC.W $c060,$d100  ; position control of Spr. Reuse
   DC.W $00f8,$0000
   DC.W $0108,$00f0
   DC.W $02e8,$01f0
   DC.W $05e8,$03f0
   DC.W $0be8,$07f0
   DC.W $1668,$0ff0
   DC.W $2c68,$1ef0
   DC.W $5868,$3cf0
   DC.W $b868,$7cf0
   DC.W $bfe2,$7ffc
   DC.W $bffa,$7ffc
   DC.W $8062,$7ffc
   DC.W $ff6e,$00f0
   DC.W $0168,$00f0
   DC.W $0168,$00f0
   DC.W $0108,$00f0
   DC.W $01f8,$0000
   DC.W $0000,$0000
;
;   Sprite Data 9
;
   DC.W $6d60,$7e00
   DC.W $1fe0,$0000
   DC.W $2010,$1fe0
   DC.W $5fe8,$3ff0
   DC.W $b074,$7ff8
   DC.W $b074,$78f8
   DC.W $b074,$78f8
   DC.W $b074,$78f8
   DC.W $bff4,$7ff8
   DC.W $5ff4,$3ff8
   DC.W $2074,$1ff8
   DC.W $1f74,$00f8
   DC.W $f174,$00f8
   DC.W $8f74,$70f8
   DC.W $b074,$7ff8
   DC.W $5fe8,$3ff0
   DC.W $2010,$1fe0
   Dc.W $1fe0,$0000

   DC.W $c060,$d100   ; position control
   DC.W $1fe0,$0000
   DC.W $2010,$1fe0
   DC.W $5fe8,$3ff0
   DC.W $b074,$7ff8
   DC.W $b074,$78f8
   DC.W $b074,$78f8
   DC.W $b074,$78f8
   DC.W $bff4,$7ff8
   DC.W $5ff4,$3ff8
   DC.W $2074,$1ff8
   DC.W $1f74,$00f8
   DC.W $f174,$00f8
   DC.W $8f74,$70f8
   DC.W $b074,$7ff8
   DC.W $5fe8,$3ff0
   DC.W $2010,$1fe0
   Dc.W $1fe0,$0000
   Dc.W $0000,$0000

; Sprite Data 1   (times 2)

   DC.W $6d60,$7e00
   DC.W $0fe0,$0000
   DC.W $1020,$0fc0
   DC.W $2fa0,$1fc0
   DC.W $5fa0,$3fc0
   DC.W $b3a0,$7fc0
   DC.W $aba0,$77c0
   DC.W $9ba0,$67c0
   DC.W $eba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0820,$07c0
   DC.W $0fe0,$0000

   DC.W $c060,$d100   ; position control
   DC.W $0fe0,$0000
   DC.W $1020,$0fc0
   DC.W $2fa0,$1fc0
   DC.W $5fa0,$3fc0
   DC.W $b3a0,$7fc0
   DC.W $aba0,$77c0
   DC.W $9ba0,$67c0
   DC.W $eba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0820,$07c0
   DC.W $0fe0,$0000
   DC.W $0000,$0000
;
;  last Sprite of 1 image
;
   DC.W $6d60,$7e00
   DC.W $0fe0,$0000
   DC.W $1020,$0fc0
   DC.W $2fa0,$1fc0
   DC.W $5fa0,$3fc0
   DC.W $b3a0,$7fc0
   DC.W $aba0,$77c0
   DC.W $9ba0,$67c0
   DC.W $eba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0820,$07c0
   DC.W $0fe0,$0000

   DC.W $c060,$d100   ; position control
   DC.W $0fe0,$0000
   DC.W $1020,$0fc0
   DC.W $2fa0,$1fc0
   DC.W $5fa0,$3fc0
   DC.W $b3a0,$7fc0
   DC.W $aba0,$77c0
   DC.W $9ba0,$67c0
   DC.W $eba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0ba0,$07c0
   DC.W $0820,$07c0
   DC.W $0fe0,$0000
   DC.W $0000,$0000
S_end:  equ  *
;  -------------------------------------
;  DISPLAY COPROCCESSOR INSTRUCTION LIST
;  -------------------------------------
;
CopList:
  DC.W $008e,$2c81    ; DIW Start (3781)
  DC.W $0090,$f4c1    ; DIW Stop (f6d1)
  DC.W $0092,$38  ; DDF Start FETCH 22 WORDS ($28)
  DC.W $0094,$d0  ; DDF Stop ($d8)
  DC.W $00e0,BPlane1/$10000,$00e2
LP1: DC.W [BPlane1&$ffff]+40
  DC.W $00e4,BPlane2/$10000,$00e6
LP2: DC.W [BPlane2&$ffff]+40
  DC.W $0108,00  ; modulo odd planes (34)
  DC.W $0100,$1200  ; 2 BitPlanes
  DC.W $0102,$0000
  DC.W $0104,$0024
  DC.W $0180,$000  ; Background color
  DC.W $0182,$000
  DC.W $01a2,$00f
  DC.W $01a4,$0af
  DC.W $01a6,$0dd

  DC.W $01aa,$00f
  DC.W $01ac,$0af
  DC.W $01ae,$0dd

  DC.W $0120,$0004   ;[SndBuffer+148*2]/$10000
  DC.W $0122,$5128   ;[SndBuffer+148*2]&$ffff

  DC.W $0124,$0004   ;[SndBuffer+148*1]/$10000
  DC.W $0126,$5094   ;[SndBuffer+148*1]&$ffff

  DC.W $0128,$0004   ;SndBuffer/$10000
  DC.W $012a,$5000   ;SndBuffer&$ffff

  DC.W $012c,$0004   ;[SndBuffer+148*3]/$10000
  DC.W $012e,$51bc   ;[SndBuffer+148*3]&$ffff
;
; End of Copper List
;
  DC.W $ffff,$fffe

;
;  Sprite Moving Datas
;
x_position:
  DC.B $50,$50,$51,$53,$56,$5a,$5e,$63,$68,$6e,$74,$7b,$82
  DC.B $89,$91,$98,$9f,$a7,$ad,$b4,$ba,$c0,$c5,$ca,$ce
  DC.B $d1,$d4,$d6,$d7,$d8,$d8,$d8,$d6,$d5,$d2,$d0,$cd
  DC.B $c9,$c5,$c1,$bd,$b9,$b5,$b1,$ad,$a9,$a6,$a2,$a0
  DC.B $9d,$9b,$99,$98,$97,$97,$97,$97,$98,$99,$9b,$9c
  DC.B $9e,$a0,$a2,$a4,$a6,$a8,$aa,$ab,$ac,$ad,$ae,$ae
  Dc.B $ae,$ad,$ac,$aa,$a8,$a6,$a3,$a0,$9c,$98,$94,$8f
  DC.B $8b,$86,$81,$7d,$78,$74,$70,$6c,$69,$66,$64,$62
  DC.B $61,$61,$61,$62,$64,$66,$6a,$6d,$72,$77,$7d,$83
  DC.B $89,$90,$97,$9f,$a6,$ae,$b6,$bd,$c4,$cb,$d1,$d7
  DC.B $dd,$e2,$e6,$e9,$ec,$ee,$ef,$ef,$ee,$ed,$eb,$e8
  DC.B $e4,$e0,$db,$d6,$d0,$ca,$c3,$bc,$b5,$ae,$a7,$a0
  DC.B $9a,$93,$8d,$87,$82,$7d,$79,$76,$73,$70,$6f,$6e
  DC.B $6d,$6d,$6e,$70,$71,$74,$76,$79,$7c,$80,$83,$87
  DC.B $8b,$8e,$92,$95,$98,$9b,$9d,$9f,$a1,$a2,$a3,$a3
  DC.B $a3,$a3,$a2,$a1,$a0,$9e,$9c,$9a,$98,$96,$94,$92
  DC.B $90,$8e,$8d,$8b,$8b,$8a,$8a,$8b,$8b,$8d,$8f,$91
  DC.B $94,$97,$9b,$9f,$a3,$a8,$ad,$b2,$b7,$bc,$c2,$c7
  DC.B $cc,$d0,$d4,$d8,$dc,$de,$e1,$e2,$e3,$e3,$e3,$e1
  DC.B $df,$dd,$d9,$d5,$d0,$cb,$c5,$be,$b7,$b0,$a9,$a1
  DC.B $99,$91,$8a,$82,$7b,$74,$6e,$68,$63,$5e,$5a,$57
  DC.B $54,$53,$52

y_position:
  DC.B $56,$52,$4f,$4b,$48,$44,$41,$3e,$3c,$39,$37,$36
  DC.B $35,$34,$33,$33,$33,$34,$35,$36,$37,$39,$3b,$3e
  DC.B $40,$43,$45,$48,$4b,$4e,$51,$53,$56,$59,$5b,$5d
  DC.B $5f,$61,$62,$63,$64,$65,$65,$66,$65,$65,$65,$64
  DC.B $63,$62,$61,$60,$5f,$5e,$5c,$5b,$5a,$59,$58,$57
  DC.B $57,$56,$56,$56,$56,$56,$57,$58,$58,$59,$5a,$5c
  DC.B $5d,$5e,$60,$61,$63,$64,$65,$66,$67,$68,$69,$69
  DC.B $69,$69,$69,$68,$67,$66,$64,$62,$60,$5e,$5c,$59
  DC.B $56,$53,$50,$4d,$4a,$47,$44,$41,$3f,$3c,$3a,$38
  DC.B $36,$34,$33,$32,$32,$32,$32,$32,$33,$35,$37,$39
  DC.B $3b,$3e,$41,$44,$47,$4b,$4e,$52,$56,$59,$5d,$61
  DC.B $64,$67,$6a,$6d,$6f,$72,$73,$75,$76,$77,$77,$77
  DC.B $77,$77,$76,$74,$73,$71,$6f,$6d,$6b,$68,$66,$63
  DC.B $60,$5e,$5b,$59,$56,$54,$52,$50,$4f,$4d,$4c,$4b
  DC.B $4b,$4a,$4a,$4a,$4b,$4b,$4c,$4c,$4d,$4e,$4f,$50
  DC.B $51,$52,$53,$54,$55,$56,$57,$57,$57,$57,$57,$57
  DC.B $56,$56,$55,$54,$52,$51,$50,$4e,$4c,$4b,$49,$48
  DC.B $46,$45,$43,$42,$41,$41,$40,$40,$40,$40,$41,$42
  DC.B $43,$44,$46,$48,$4a,$4d,$50,$53,$56,$59,$5c,$5f
  DC.B $62,$66,$69,$6c,$6f,$71,$74,$76,$78,$79,$7a,$7b
  DC.B $7c,$7c,$7b,$7b,$79,$78,$76,$74,$72,$6f,$6c,$69
  DC.B $65,$62,$5e,$5b

  DATA
GfxBase: blk.l 1,0
initialSP: blk.l 1,0
OldCopper: blk.l 1,0
Timer: blk.w 1,0
xpos:  blk.w 4,0
ypos:  blk.w 4,0
index:  blk.w 4,0
  END
