;vbi.asm


vbi
  lda #low dli1
  sta 512
  lda #high dli1
  sta 513
  lda pauseflag
  bne :jnscroll
  lda #0
  sta scrollflag+1
  lda scrollflag
  sta oscrollflag
  bne *+2+3
:jnscroll
  jmp :noscroll
  bpl scroll1
  lda #255
  sta scrollflag+1
scroll1
  clc
  lda scrollpos
  adc scrollflag
  sta scrollpos
  sta bytepos
  lda scrollpos+1
  adc scrollflag+1
  sta scrollpos+1
  sta bytepos+1
;boundry check
  lda scrollpos+1
  bmi :out1
  bne :okbn1
  lda scrollpos
  cmp #12
  bcs :okbn1
:out1
  lda #12
  sta scrollpos
  lda #0
  sta scrollpos+1
  sta fishspeed ;&&
  beq :okbn2 ;always
:okbn1
  sec
  lda scrollpos
  sbc #low [1022*4-48*4]
  lda scrollpos+1
  sbc #high [1022*4-48*4]
  bcc :okbn2
  lda #low [1022*4-48*4]
  sta scrollpos
  lda #high [1022*4-48*4]
  sta scrollpos+1
  lda #0
  sta fishspeed
:okbn2
  lda scrollpos
  and #3
  sta :tmpvbi
  sec
  lda #3
  sbc :tmpvbi
  sta HSCROL
  lsr bytepos+1
  ror bytepos
  lsr bytepos+1
  ror bytepos
:update
  ldx #21*3
  ldy #21
:lpvbi1
  clc
  lda adrtabl,y
  adc bytepos
  sta scrolltab+1,x
  lda adrtabh,y
  adc bytepos+1
  sta scrolltab+2,x
  dex
  dex
  dex 
  dey
  bpl :lpvbi1
  lda #0
  sta scrollflag
:noscroll
;animate waves
  inc wavecounter
  lda wavecounter
  and #3
  bne :nodraw
  lda wavecounter
  and #63
  tax
  ldy #0
:lp88
  lda wavetab1,x
  sta wave1,y
  lda wavetab2,x
  sta wave2,y
  inx
  iny
  cpy #4
  bne :lp88
:nodraw
;alternate pmbase
  lda wavecounter
  and #1 ;alternates every vbi
  bne *+5
  jmp :base2
***********************************
;on first, third, etc vbi, draw
;fish and enable "pmmem"1
:base1
  lda #high pmmem
  sta 54279
  lda bigflag ;0=norm 1=big
  lsr a ;set carry for later add
  lda bigflag
  sta SIZEP0
  sta SIZEP1
  adc #1;1=norm 3=big
  sta SIZEP2
  lda fishx
  sta HPOSP0
  sta HPOSP2
  clc
  adc #8
  ldy bigflag ;if big,2nd player is farther right
  beq *+4
  adc #8
  sta HPOSP1
  lda noeraseflag
  bne :noer
  ldx #16
  stx :cntr
  ldy ofishy
  lda #0
:lpdfish
  sta play0,y
  sta play1,y
  sta play2,y
  ldx obigflag
  beq :not1big
  sta play0+1,y
  sta play1+1,y
  sta play2+1,y
  sta play0+2,y
  sta play1+2,y
  sta play2+2,y
  iny ! iny
:not1big
  iny
  dec :cntr
  bne :lpdfish
:noer
  ldx #16
  stx :cntr
  lda fishd
;multiply by 16 
  asl a ! asl a ! asl a ! asl a
  tax
;x will be 0 to 16
;add 0,16,0,32
  stx :tmpvbi
  lda deadflag
  beq :readtrig
  lda #1
  bne :pick1
:readtrig
  lda 644
  eor #1
  ora autotail
:pick1
  lsr a ! ror a ! ror a ! ror a ;*32
  clc
  adc :tmpvbi
  tax 
  ldy bigflag
  sty obigflag
  ldy fishy
  sty ofishy
  lda deadflag
  bne *+5
  jmp :lpd2fish

  include DEAD.ASM

;draw alive fish
:lpd2fish
  lda fishdata1,x
  sta play0,y
  lda fishdata2,x
  sta play1,y
  lda fishdata3,x
  sta play2,y
  lda bigflag
  beq :not2big
  lda fishdata1,x
  sta play0+1,y
  sta play0+2,y
  lda fishdata2,x
  sta play1+1,y
  sta play1+2,y
  lda fishdata3,x
  sta play2+1,y
  sta play2+2,y
  iny ! iny
:not2big
  iny
  inx
  dec :cntr
  bne :lpd2fish
;set fish colors (alive)
  lda #198-32
  sta COLPM0
  sta COLPM1
  lda #202-32
  sta COLPM2
:donefish
;move baby fish 1 & 2
  lda baby1x
  cmp #1
  beq :nm12
  lda baby2x
  cmp #1
  beq :nm12
  ldx #83
:lpbaby
  lda missile+2,x
  sta missile,x
  inx
  bne :lpbaby
  lda #0
  sta missile+255
  lda baby1x
  beq :db1
  inc baby1x
  beq :db1
  inc baby1x
  beq :db1
  inc baby1x
  beq :db1
  inc baby1x
:db1
  lda baby2x
  beq :db2
  inc baby2x
  beq :db2
  inc baby2x
  beq :db2
  inc baby2x
  beq :db2
  inc baby2x
:db2
:nm12
  ldx baby1x
  stx HPOSM1
  inx
  inx
  stx HPOSM0
  ldx baby2x
  stx HPOSM3
  inx
  inx
  stx HPOSM2
  jmp :donepm
***********************************
;second, fourth vbi draw crab (players 0 and 1)
:base2
  lda #high pmmem2
  sta 54279
;set crab colors
  lda #68-16
  sta COLPM0
  sta COLPM1
;and piranha colors
  lda #150-32
  sta COLPM2
  sta COLPM3
  lda #0
  sta SIZEP2
;set crab position
  lda crabx
  sta HPOSP0
  lda bigcflag
  sta SIZEP0
  sta SIZEP1
  asl a
  asl a
  asl a
  clc
  adc #8
  adc crabx
  sta HPOSP1
;set piranha position
  clc
  lda pirx
  sta HPOSP2
  adc #8
  sta HPOSP3
;erase old crab
  ldy ocraby
  ldx #24
  lda obigcflag
  beq *+4
  ldx #24*2
  lda #0
:lpercr
  sta play0b,y
  sta play1b,y
  iny
  dex
  bne :lpercr 
;erase old piranha
  ldy opiry
  ldx #24
  lda #0
:lperpi
  sta play2b,y
  sta play3b,y
  iny
  dex
  bne :lperpi 
;draw new crab
  lda #16
  sta :cntr
  lda wavecounter
  and #4+8 ! asl a ! asl a ! asl a
  tax
  ldy craby
  sty ocraby 
:lpdrcr
  lda crabdata,x
  sta play0b,y
  lda crabdata+16,x
  sta play1b,y
  lda bigcflag
  beq :no3big
  lda crabdata,x
  sta play0b+1,y
  lda crabdata+16,x
  sta play1b+1,y
  iny
:no3big
  iny
  inx
  dec :cntr
  bne :lpdrcr
;draw new piranha
  lda #24
  sta :cntr
  lda wavecounter
  and #8 ! asl a ! asl a ! asl a
  ldy pird
  bpl :pirleft
  ora #128
:pirleft
  tax
  ldy piry
  sty opiry 
:lpdrpi
  lda pirdata,x
  sta play2b,y
  lda pirdata+32,x
  sta play3b,y
  iny
  inx
  dec :cntr
  bne :lpdrpi
;move baby fish 3 & 4
  lda baby3x
  cmp #1
  beq :nm34
  lda baby4x
  cmp #1
  beq :nm34
  ldx #83
:lp2baby
  lda missile2+2,x
  sta missile2,x
  inx
  bne :lp2baby
  lda #0
  sta missile2+255
  lda baby3x
  beq :db3
  inc baby3x
  beq :db3
  inc baby3x
  beq :db3
  inc baby3x
  beq :db3
  inc baby3x
:db3
  lda baby4x
  beq :db4
  inc baby4x
  beq :db4
  inc baby4x
  beq :db4
  inc baby4x
  beq :db4
  inc baby4x
:db4
:nm34
  ldx baby3x
  stx HPOSM1
  inx
  inx
  stx HPOSM0
  ldx baby4x
  stx HPOSM3
  inx
  inx
  stx HPOSM2
:donepm
***********************************
  jsr dosound

;animate fins
  lda wavecounter
  and #16
  lsr a
  clc
  adc #7
  tax
  ldy #7
:l001
  lda rdata1,x
  sta rh1,y
  sta rh2,y
  lda ldata1,x
  sta lh1,y
  sta lh2,y
  dex
  dey
  bpl :l001

;exitvbi

:exitvbi equ *+1
  jmp $FFFF

:tmpvbi db 0 ;temp vbi storage
:tmp2vbi db 0 ;ditto
:cntr db 0;used in vbi routine

