                 processor 6502
;Pong in the sideborder and screen

;Memory map:
                  ;$80-$ff Zero page

                  ;$7000--$ffff code

  include globals.s

  seg.u zp
  org $0080
zp_start:

  seg code
  org $7000

sprite_screen = $6000
sprite_base = $c0

 seg zp
savea: dc.b 0
savex: dc.b 0
savey: dc.b 0
char_x: dc.b 0
char_y: dc.b 0
char_spdx: dc.b 0
char_spdy: dc.b 0
char_addr: dc.b 0,0
char_tmp: dc.b 0
countdown: dc.b 0,0
 seg code

sprites:
  ds.b $400,$aa

;$7400
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00,$00

  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00,$00

  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00,$00

  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00,$00
;$7500
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00
  dc.b $aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00,$00

;Sprite on the other side:
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00

  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00

  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00

;$7600
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00

  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa
  dc.b $00,$aa,$aa,$00,$aa,$aa,$00,$aa,$aa,$00



start: subroutine
  sei
  lda #0
  sta $d015
  jsr init_sync
  lda #2
  sta $dd00

  sei
  lda #<irq1
  sta $fffe
  lda #>irq1
  sta $ffff
  ldx #$2e
.lo9:
  lda def_d000,x
  sta $d000,x
  dex
  bpl .lo9
  lda #$7f
  sta $dc0d
  lda $dc0d
  lda #$35
  sta $01
  cli
  jsr erase
  rts




  org ((* / $100) * $100) + $140

init_sync: subroutine
  lda #$3e
  sta $dc04
  lda #$00
  sta $dc05
  lda #$ff
.wait:
  cmp $d012
  bne .wait
  ldy $00
  ldy #$10
.another:
  lda $d012
  ldx #$08
.lo:
  dex
  bne .lo

  cmp ($00,x)
  cmp $d012
  beq .waste_one
.waste_one
  dey
  bne .another
  lda #$11
  sta $dc0e
  rts

sync:
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more
  rts
.more:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more2
  rts
.more2:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more3
  rts
.more3:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
sync2:
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more4
  rts
.more4:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
sync3:
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more5
  rts
.more5:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more6
  rts
.more6:
  cmp $00
  nop
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dey
  bne .more7
  rts
.more7:
  cmp $00
  nop
  stx $d016
  sta $d016,x
  dey
  stx $d016
  sta $d016
  cmp ($00,x)
  cmp $00
  jmp sync

  ds.b 7,0

irq1:
  sta savea
  stx savex
  sty savey
  sec
  lda #$2b
  sbc $dc04
  sta waste
waste = * + 1
  bcs .waste_def
  ldy #$a0
  ldy #$a0
  ldy #$a0
.waste_def:
  ldy $ea
  ldx #$1f
  cmp ($00,x)
  cmp ($00,x)
.lop1:
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp $00
  nop
  dec $d016
  inc $d016
  dex
  bne .lop1
  lda #$32
  sta $d009
  sta $d00d
  lda #$5c
  sta $d00b
  sta $d00f
  cmp ($00,x)
  lda #$f0
  ldy #sprite_base+$10
  dec $d016
  inc $d016
  sty sprite_screen+$3fc
  iny
  sty sprite_screen+$3fd
  ldy #sprite_base+$15
  sty sprite_screen+$3fe
  iny
  sty sprite_screen+$3ff
  ldy #$f0
  sta $d00a
  sty $d010
  lda #$08
  ldx #$00
  stx $d016
  sta $d016,x
  ldy #$48
  stx $d016
  sta $d016
  sty $d00e
  ldy #$52
  jsr sync
  stx $d016
  sta $d016
  ldy #$86
  sty $d009
  sty $d00d
  ldy #$b0
  sty $d00b
  sty $d00f
  ldy #sprite_base+$12
  sty sprite_screen+$3fc
  iny
  sty sprite_screen+$3fd
  ldy #sprite_base+$17
  sty sprite_screen+$3fe
  iny
  sty sprite_screen+$3ff
  nop
  stx $d016
  sta $d016
  nop
  nop
  ldy #$53
  jsr sync2
  stx $d016
  sta $d016
  ldy #$da
  sty $d009
  sty $d00d
  ldy #sprite_base+$14
  sty sprite_screen+$3fc
  ldy #sprite_base+$19
  sty sprite_screen+$3fe
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  stx $d016
  sta $d016,x
  nop
  stx $d016
  sta $d016
  nop
  nop
  ldy #$14
  jsr sync
  stx $d016
  sta $d016
  sty $d017
  ldy #$08
  jsr sync3
  stx $d016
  sta $d016
  ldy #$fa
  sty $d001
  sty $d003
  sty $d005
  sty $d007
  sty $d009
  sty $d00b
  sty $d00d
  sty $d00f
  ldy #$e8
  sty $d00a
  ldy #$18
  sty $d00e
  stx $d016
  sta $d016
  ldy #$33
  sty $d011
  ldy #$d0
  sty $d010
  ldy #sprite_base+$09
  sty sprite_screen+$3f9
  iny
  sty sprite_screen+$3fa
  iny
  sty sprite_screen+$3fb
  iny
  sty sprite_screen+$3f8
  iny
  sty sprite_screen+$3fd
  nop
  nop
  stx $d016
  sta $d016
  iny
  sty sprite_screen+$3ff
  iny
  sty sprite_screen+$3fe
  ldy #sprite_base+$08
  sty sprite_screen+$3fc
  ldy #$ff
  sty $d017
  ldy #$29
.lop3:
  cmp ($00,x)
  cmp ($00,x)
  cmp ($00,x)
  dec $d016
  inc $d016
  cmp ($00,x)
  cmp $00
  nop
  dey
  bne .lop3
  lda #$3b
  sta $d011
  lda #$08
  sta $d001
  sta $d003
  sta $d005
  sta $d007
  sta $d009
  sta $d00b
  sta $d00d
  sta $d00f
  ldx #$07
.lop4:
  lda def_07f8,x
  sta sprite_screen+$3f8,x
  dex
  bpl .lop4
  jsr MUSIC_PLAY
  inc $d019
  lda $dc0d
  ldy savey
  ldx savex
  lda savea
  rti

def_07f8:
  dc.b sprite_base+$04
  dc.b sprite_base+$01
  dc.b sprite_base+$02
  dc.b sprite_base+$03
  dc.b sprite_base+$00
  dc.b sprite_base+$05
  dc.b sprite_base+$07
  dc.b sprite_base+$06

  
erase_one:  subroutine
  lda char_y
  cmp #$04
  bcc .low_y
  cmp #$1d
  bcc .mid_y
.high_y:
  sbc #$1d
  ldy #>(sprites+$200)
  ldx #$00
  jmp .upper_lower_erase
.low_y:
  ldy #>(sprites)
  ldx #$0f
  jmp .upper_lower_erase

.mid_y:
  lda char_x
  cmp #$04
  bcc .low_x
  cmp #$2c
  bcc .mid_x
.high_x:
  sbc #$2c
  ldy #>(sprites+$541)
  ldx #<(sprites+$541)
  jmp .left_right_erase
.low_x:
  ldy #>(sprites+$400)
  ldx #<(sprites+$400)
  jmp .left_right_erase

.mid_x:
  sec
  sbc #$04
  tay
  lda char_y
  sbc #$04
  sta char_tmp
  asl
  asl
  adc char_tmp
  asl
  asl
  rol char_addr+1
  asl
  rol char_addr+1
  sta char_addr
  lda char_addr+1
  and #$03
  ora #>sprite_screen
  sta char_addr+1

;erase the character:
  lda #$bb
  sta (char_addr),y
  rts

.upper_lower_erase:
  sty char_addr+1
  stx char_addr
  sta char_tmp
  asl
  clc
  adc char_tmp
  asl
  asl
  adc char_addr
  sta char_addr
  lda char_x
  cmp #$18
  bcc .noinc
  sbc #$18
  inc char_addr+1
.noinc:
  ldx #$00
.lop4:
  cmp #$06
  bcc .noinc2
  sbc #$06
  inx
  bne .lop4
.noinc2:
  lsr
  ldy #$f0
  bcc .go2
  ldy #$0f
.go2
  clc
  adc char_addr
  sta char_addr
  txa
  asl
  asl
  asl
  asl
  asl
  asl
  ora char_addr
  sta char_addr
  tya
  ldy #$00
  ora (char_addr),y
  sta (char_addr),y
  ldy #$03
  sta (char_addr),y
  ldy #$06
  sta (char_addr),y
  ldy #$09
  sta (char_addr),y
  rts


.left_right_erase:
  sty char_addr+1
  stx char_addr
  lsr
  ldy #$f0
  bcc .go3
  ldy #$0f
.go3:
  sty char_tmp
  clc
  adc char_addr
  sta char_addr
  sec
  lda char_y
  sbc #$04
  cmp #$15
  bcc .noww
  inc char_addr+1
.noww:
  tax
  lda offset,x
  and #$c0
  clc
  adc char_addr
  sta char_addr
  bcc .nowr2
  inc char_addr+1
.nowr2:
  lda offset,x
  and #$3f
  tay
  txa
  ldx #$00
  cmp #$17
  bcc .nowr3
  inx
.nowr3:
  cmp #$18
  bcc .nowr4
  inx
  iny
  iny
  iny
.nowr4
  lda char_tmp
  ora (char_addr),y
  sta (char_addr),y
  jsr .erone
  jsr .erone
  jsr .erone
  dex
  bmi .rts
  jsr .erone
  dex
  bmi .rts
  jsr .erone
  jsr .erone

.erone:
  iny
  iny
  iny
  cpy #$3f
  bne .nowr6
  iny
.nowr6:
  sta (char_addr),y
.rts:
  rts

offset:
  dc.b $00,$0c,$18,$24,$30,$3c,$49,$55
  dc.b $61,$6d,$79,$86,$92,$9e,$aa,$b6
  dc.b $c3,$cf,$db,$e7,$f3,$00,$0c,$18
  dc.b $24,$00,$00,$00,$00,$00,$00,$00

def_d000:
  dc.b $b8,$08,$28,$08,$58,$08,$88,$08
  dc.b $f0,$08,$e8,$08,$48,$08,$18,$08
  dc.b $d0,$3b,$11,$00,$00,$ff,$c8,$ff
  dc.b $85,$01,$01,$00,$ff,$ff,$00,$00
  dc.b $01,$01,$0b,$0b,$0b,$0b,$0b,$01
  dc.b $01,$01,$01,$01,$01,$01,$01


erase:
  lda #0
  sta char_x
  lda #$21
  sta char_y
  lda #1
  sta char_spdx
  lda #$ff
  sta char_spdy
  lda #$0
  sta countdown+1
  sta countdown

erase_lop:

  ldy #0
  ldx #0
  lda #0
  jsr erase_one

  lda char_y
  clc
  adc char_spdy
  cmp #$21
  bne .nolower
  ldx #$ff
  stx char_spdy
.nolower:  
  cmp #$00
  bne .noupper
  ldx #$1
  stx char_spdy
.noupper:
  sta char_y

  ldy #0
  ldx #0
  lda #0
  jsr erase_one

  lda char_x
  clc
  adc char_spdx
  cmp #$2f
  bne .noright
  ldx #$ff
  stx char_spdx
.noright:  
  cmp #$00
  bne .noleft
  ldx #$1
  stx char_spdx
.noleft:
  sta char_x

  lda countdown
  cmp #$60
  bne .noend
  lda countdown+1
  cmp #$7
  beq end_now
.noend:

  inc countdown
  bne erase_lop
  inc countdown+1
  jmp erase_lop
end_now:
  sei
;slu:
;  lda #$2
;  sta $d020
;  sta $d021
;  jmp slu

  lda #$00
  sta $d020
  sta $d021
  sta $d011
  sta $d015
  sta $d01c
  rts

