         *= $0801
	!to "pong1_1.prg";
	
fast     = $03
slow     = $0c
maxy     = $07
miny     = $00
maxx     = $0f
minx     = $00
bsx      = $07
bsy      = $03
psy      = $02
predel   = $08
postdel  = $01

         !byte $0c,$08,$0a,$00,$9e,$32
         !byte $30,$36,$31,$00,$00,$00

         lda #$ff
         sta $c0   ; tape motor ctrl
         sta $dd03 ; dser port to output
         lda #$00
         sta $dd01 ; turn all drives off
         jsr laseroff
         lda #$00
         sta $d020
         sta $d021 ; pretty colors
         jsr init

main     lda $dc00
         and #$10
         beq quit

         jsr draw
         jsr joyin
         jsr moveball

         jmp main

quit     lda #$00
         sta $dd01
         rts

init     lda #bsx
         sta ballx
         lda #bsy
         sta bally
         lda #psy
         sta pady0
         sta pady1
         rts

joyin    lda $dc00 ; get joystick inputs
         and #$0f  ; and move pads
         cmp #$0e  ; accordingly
         beq j0up
         cmp #$0d
         beq j0down
j1       lda $dc01
         and #$0f
         cmp #$0e
         beq j1up
         cmp #$0d
         beq j1down
         rts
j0up     ldx pady0
         beq j1
         dex
         stx pady0
         jmp j1
j0down   ldx pady0
         cpx #(maxy-2)
         beq j1
         inx
         stx pady0
j1up     ldx pady1
         beq j1ok
         dex
         stx pady1
         rts
j1down   ldx pady1
         cpx #(maxy-2)
         beq j1ok
         inx
         stx pady1
j1ok     rts

moveball lda balldy  ; move the ball one
         clc         ; diagonal step and
         adc bally   ; check for
         sta bally   ; collisions
         beq ybounce
         cmp #maxy
         beq ybounce
moveb0   lda balldx
         clc
         adc ballx
         sta ballx
         beq p0bounce
         cmp #maxx
         beq p1bounce
         rts
ybounce  lda #$00
         sec
         sbc balldy
         sta balldy
         jmp moveb0
p0bounce ldx pady0
         jmp pbounce
p1bounce ldx pady1
pbounce  cpx bally
         beq bounce
         inx
         cpx bally
         beq bounce
         inx
         cpx bally
         beq bounce
lost     lda #bsy
         sta bally
         lda #psy
         sta pady0
         sta pady1
         lda #bsx
         sta ballx
lost0    lda $dc01
         and #$10
         bne lost0
bounce   lda #$00
         sec
         sbc balldx
         sta balldx
         rts

draw     lda padx0   ; draw the paddles
         sta posxl   ; and the ball
         lda pady0
         sta posyl
         ldx #$00
         jsr convert
         inx
         jsr convert
         jsr seekto
         ldx #predel
         jsr delay
         jsr laseron
         inc posyl
         inc posyl
         ldx #$01
         jsr convert
         jsr seekto
         ldx #postdel
         jsr delay
         jsr laseroff
         lda ballx
         sta posxl
         lda bally
         sta posyl
         ldx #$00
         jsr convert
         inx
         jsr convert
         jsr seekto
         ldx #predel
         jsr delay
         ldx #$80
         jsr pulse
         lda padx1
         sta posxl
         lda pady1
         sta posyl
         ldx #$00
         jsr convert
         inx
         jsr convert
         jsr seekto
         ldx #predel
         jsr delay
         jsr laseron
         inc posyl
         inc posyl
         ldx #$01
         jsr convert
         jsr seekto
         ldx #postdel
         jsr delay
         jsr laseroff
         rts

pulse    cpx #$00   ; pulse the laser
         beq pulse1 ; during 10*x us
         jsr laseron
pulse0   nop
         nop
         nop
         dex
         bne pulse0
         jsr laseroff
pulse1   rts

laseron  lda $01
         and #$df
         sta $01
         rts

laseroff lda $01
         ora #$20
         sta $01
         rts

home     lda #$00  ; seek to (0,0) and
         sta posxl ; clear variables
         sta posxh
         sta posyl
         sta posyl
         sta destl0
         sta desth0
         sta destl1
         sta desth1
         jmp seekto

convert  lda posl,x  ; convert pos*x to
         and #$07    ; dest*x coords for
         sta destl,x ; use with seekto
         lda posl,x
         ror 
         ror 
         ror 
         and #$1f
         sta desth,x
         lda posh,x
         rol 
         rol 
         rol 
         rol 
         rol 
         and #$e0
         ora posh,x
         sta posh,x
         rts

seekto   ldx #$01 ; move until dest* eq
         jsr seek ; ang*
         jsr seek
         dex
         lda #fast
         sta speed
         jsr seek
         jsr seek
         jsr output
         lda angh0
         cmp desth0
         bne seekto
         lda angh1
         cmp desth1
         bne seekto
         lda angl0
         cmp destl0
         bne seekto
         lda angl1
         cmp destl1
         bne seekto
         rts

seek     lda angh,x  ; move stepper x
         cmp desth,x ; one step closer
         bcc seekup  ; to dest if needed
         bne seekdn
         lda angl,x
         cmp destl,x
         bcc seekup
         beq seeknot
seekdn   dec angl,x
         lda angl,x
         bpl seekok
         lda #$07
         sta angl,x
         dec angh,x
         jmp seekok
seekup   lda angl,x
         adc #$01
         sta angl,x
         bit mask8
         beq seekok
         and #$07
         sta angl,x
         inc angh,x
seekok   lda #slow
         sta speed
seeknot  rts

output   ldx angl0    ; set the new
         lda anglt0,x ; coil positions
         ldx angl1    ; and wait until
         ora anglt1,x ; the stepper has
         sta $dd01    ; moved
         ldx speed
         jsr delay
         rts

delay    txa          ; wait for x ms
         ldx #$00
delay0   dex
         bne delay0
         tax
         dex
         bne delay
         rts

padx                ; pad position
padx0    !byte $00
padx1    !byte maxx
pady
pady0    !byte psy
pady1    !byte psy
ballx    !byte bsx  ; ball position
bally    !byte bsy
balldx   !byte $01  ; ball movement
balldy   !byte $ff

posl                ; user-setable
posxl    !byte $00  ; linear coordinates
posyl    !byte $00
posh
posxh    !byte $00
posyh    !byte $00
destl               ; destination coords
destl0   !byte $00  ; for seeking
destl1   !byte $00
desth
desth0   !byte $00
desth1   !byte $00
angl                ; current stepper
angl0    !byte $00  ; coordinates
angl1    !byte $00
angh
angh0    !byte $00
angh1    !byte $00
anglt0   !byte $01,$03,$02,$06
         !byte $04,$0c,$08,$09
anglt1   !byte $10,$30,$20,$60
         !byte $40,$c0,$80,$90
speed    !byte $00
mask8    !byte $08


