
; YAR'S RETURN 1K Compo Entry.  DASM-Compliant Source Code
; Written 2002 by Six of DLoC
; http://www.darklordsofchaos.com
; email six@darklordsofchaos.com
;
; 

gstate EQU $F6  
ymisdel EQU $F7
ymisfir EQU $F8   ;0 = ready, 1=onscreen
yardel EQU $F9  ;delay for yar animation
yarfrm EQU $FA  ;0 = basefrm, 1= 2ndfrm
yardir EQU $FB  ;0 = up, 1=r, 2=d, 3=l
misdel EQU $FC
enedel EQU $FD
collide EQU $FE
bcollid EQU $FF

 processor 6502    ;CPU to compile for is 6502
 org $0801    ;compile at $0801 (BASIC)
BASIC dc.b $0b,$08,$01,$00,$9e,$32,$30,$36,$34;basic tokens for 10 SYS 2064


 org $0810    
START
 sei     ;disable IRQ
 lda #$09    ;Color 9 for multicolor
 sta $0286    ;in cursor color
 jsr $e544    ;clear screen chokes on old ROM
 lda #$31    ;allow cset access
 sta $01     ;by hitting $01

 ldx #$00    ;x is my loop counter
sloop2
 lda $d180,x    ;get numbers from cset
 sta $2020,x    ;stash at my custom cset
 lda #$00    ;and put $00
 sta $0e80,x    ;where my sprites are going
 sta $0d80,x    ;
 sta $0c80,x    ;
 dex     ; decrement my counter
 bne sloop2    ; and loop until it's 0
drawforcefield
 lda #$37    ; Set $01 back
 sta $01     ;
 cli     ;and re-allow IRQ 
 txa     ; a=0
 ldy #$19    ; y=19 (rows high)
putter3
 ldx #$08    ; set counter to 8
putter sta $0407,x    ; store $00 at screeen
 dex     ; for 8 columns
 bne putter    ; loop if not 0
 pha     ; push my value
 lda #$28    ; self-modifying code
 clc     ; clear carry
 adc putter+$01    ; add
 sta putter+$01    ; store
 bcc putter2    ; and then
 inc putter+$02    ; inc if carry
putter2
 pla     ; pop my a back
 dey     ; y is my row counter
 bne putter3    ; and branch if not done
sloop4
 lda #$AA    ; draw my MC ff char
 sta $2000,y    
 lda #$ff    ; draw my shield char
 sta $2008,y    
 lda #$00    ; Why'd I do this?
 sta $2100,y
 lda sprdat,y    ;decompress my sprites
 sta $0d00,x
 lda sprdat+$08,y
 sta $0d40,x
 lda sprdat+$10,y
 sta $0c80,x
 lda sprdat+$18,y
 sta $0cc0,x
 lda sprdat+$20,y
 sta $0e00,x
 lda sprdat+$28,y
 sta $0e40,x
 lda sprdat+$30,y
 sta $0d80,x
 lda sprdat+$38,y
 sta $0dc0,x
 lda sprdat+$40,y
 sta $0ec0,x
 lda sprdat+$48,y
 sta $0ed8,x
 inx     ;x is my sprite index, 
 inx
 inx
 iny     ;y is my main index
 cpy #$08
 bne sloop4
 lda #$01    ;set score screenloc colrs
 sta $d84e
 sta $d84d
 sta $d829
 sta $d82a
 lda #$0C    
 sta $0f00
 sta $0f03
 lda #$19
 sta $d018
 lda #$ff
 sta $0e89
 sta $0e8c
 lda #$04    ;put my zero char on scores
 sta $044e
 sta $044d
 sta $0429
 sta $042a

setup2      ;setup2 recycled after kill
 lda #$18
 sta $d016
 sta $d000
 lda #$80
 sta $d001
 sta $d003
 sta $d005
 lda #$50
 sta $d002
 lda #$41
 sta $d004
 lda #$01
 sta $0600
 sta $0601
 sta $0628
 sta $0629
 sta yardir 
 lda #$ff
 sta $d015
 lda #$3a
 sta $07f9
 sta $07fb
 lda #$3b
 sta $07fa
 lda #$06
 sta $d010
 lda #$04
 sta $d01d
 sta $d017
 lda #$00
 sta $d006
 sta $d007
 sta $d021
 sta $d020
 sta ymisfir
 lda #$01
 sta gstate
loop      ; main game loop
 lda gstate
 cmp #$01
 bne loop2
 jsr updateyar
 jsr updatene
 jsr updatemis
 jsr updateymis
 jsr checkcollide
 inc $d023
 jmp loop
loop2
 cmp #$02
 bne loop
 jmp setup2
;=============================
checkcollide
 lda $d01e
 sta collide
 lda $d01f
 sta bcollid
 lda #$01
 bit collide
 beq checkcole1
 lda #$02
 bit collide
 beq checkcole1
 jsr incene
 lda $d01e
 lda $d01f
 lda #$02
 sta gstate
checkcole1
 lda #$04
 bit collide
 beq checkcole2
 lda #$08
 bit collide
 beq checkcole2
 jsr incyar
 lda $d01e
 lda $d01f
 lda #$02
 sta gstate
checkcole2
 lda #$08
 bit bcollid
 beq checkcolex
 lda #$08
 bit $d010
 beq checkcolex
 dec ymisfir
 jsr killymis
checkcolex 
 rts
incyar
 inc $042a
 lda $042a
 cmp #$0e
 bne incyar2
 lda #$04
 sta $042a
 inc $0429
incyar2

 rts
incene
 inc $044e
 lda $044e
 cmp #$0e
 bne incene2
 lda #$04
 sta $044e
 inc $044d
incene2
 rts
;==============================================
updatene
 dec enedel
 bne updateene3
 lda #$30
 sta enedel

udateene
 inc $d005
 lda $d005
 cmp #$a0
 bne updateene2
 lda #$ce
 sta udateene
updateene3
 rts
updateene2
 cmp #$60
 bne updateene3
 lda #$ee
 sta udateene
 rts
;===========================================
;updatebul
; lda bulfir
; beq updatebulx
; lda buldir
; bne updatebul2
; dec $d008
; beq killbul 
; rts
;updatebul2
; cmp #$01
; bne updatebul3
; inc $d009
; rts
;updatebul3
; cmp #$02
; bne updatebul4
; inc $d008
; beq killbul
; rts
;updatebul4
; dec $d009
; beq killbul
; rts
;updatebulx
; rts
;killbul
; dec bulfir
; rts

;============================================
updateymis ; Fire and track yar's missile
 lda ymisfir
 beq updateymisx
delymis
 dec ymisdel
 bne updateymisx
 lda #$10
 sta ymisdel

moveymis
 lda #$08
 bit $d010
 beq moveymis3

 lda $d006
 cmp #$60
 bne updateymis4

 jsr killymis

 dec ymisfir
 rts
updateymis4
 inc $d006
 rts
moveymis3
 inc $d006
 bne updateymisx
 lda #$08
 jsr d010it
updateymisx
 rts

killymis
 lda #$00
 sta $d006
 sta $d007
killymis2
 lda #$F7
 jsr d010it2
 rts 
;==========================================

updateyar
 dec yardel
 beq updateyar2
 jmp yarexit
updateyar2
 lda #$10
 sta yardel

Up
 lda #$01
 bit $dc00
 bne Dn 
 dec $d001
 lda $d001
 cmp #$27
 bne Dn
 lda #$fc
 sta $d001
 lda #$00
 sta yardir
Dn
 lda #$02
 bit $dc00
 bne Left
 inc $d001
 lda $d001
 cmp #$fc
 bne Left
 lda #$27
 sta $d001
 lda #$02
 sta yardir
Left
 lda #$04
 bit $dc00
 bne Right

 lda #$01
 bit $d010
 bne Left1

 lda $d000
 cmp #$18
 beq lfnomove

 dec $d000
 jmp lfnomove
Left1
 dec $d000
 bne lfnomove
 lda #$fe
 jsr d010it2
 lda #$ff
 sta $d000
lfnomove
 lda #$03
 sta yardir

Right
 lda #$08
 bit $dc00
 bne Button

 lda #$01
 bit $d010
 beq Right1

 lda $d000
 cmp #$4B
 beq rtnomove
 jmp Right2

Right1
 lda $d000
 cmp #$ff
 bne Right2

 lda #$01
 sta $d000
 jsr d010it
 jmp rtnomove
Right2
 inc $d000
rtnomove
 lda #$01
 sta yardir
Button
 lda #$10
 bit $dc00
 bne yaranim

 lda #$01
 bit bcollid
 beq Button2

 lda ymisfir
 bne yaranim

 inc ymisfir
 jsr killymis2

 lda $d001
 sta $d007
 lda #$00
 sta $d006
; jmp yaranim
Button2
; lda bulfir
; bne yaranim
; lda $d000
; sta $d008
; lda $d001
; sta $d009
; inc bulfir
; lda yardir
; sta buldir
yaranim
 ldx yardir
 lda yarspr,x
 clc
 adc yarfrm
 sta $07f8
 lda yarfrm
 bne yaranim1
 inc yarfrm
 bne yarexit
yaranim1
 dec yarfrm
yarexit
 rts
;======================================
updatemis
 dec misdel
 beq updatemis2
 rts
updatemis2
 lda #$40
 sta misdel

 jsr updatemis6
 jmp mismovy
updatemis6
 lda #$01
 bit $d010    
 beq misYARLO    ;If not set, Yar is LO
misYARHI
 lda #$02      
 bit $d010
 bne misSAME     ; if set, Yar and Missile are LO 
misYARHIMISLO
 jsr misright
 rts
misYARLO
 lda #$02
 bit $d010
 beq misSAME     ; If not set, Missile is LO
misYARLOMISHI
 jsr misleft
 rts
misSAME
 lda $d002
 cmp $d000
 bcs misSAME1
 jsr misright
 rts
misSAME1
 jsr misleft
 rts
;==========================================
mismovy
 lda $d003
 cmp $d001
 bcs mismov2
 inc $d003
 rts
mismov2
 dec $d003

misexit
 rts 
misleft
 lda #$02
 bit $d010
 beq mLeft2

 dec $d002
 bne mlfnomove
 lda #$ff
 sta $d002
 lda #$fd
 jsr d010it2
 rts
mLeft2
 lda $d002
 cmp #$16
 beq mlfnomove
 dec $d002
mlfnomove 
 rts 

misright
 lda #$02
 bit $d010
 beq mRight1

 lda $d002
 cmp #$4b
 beq mrtnomove
 inc $d002
 rts
mRight1
 lda $d002
 cmp #$ff
 bne mRight2
 lda #$02
 jsr d010it
mRight2
 inc $d002
mrtnomove
 rts
;=======================================
d010it
 ora $d010
 sta $d010
 rts
d010it2
 and $d010
 sta $d010
 rts
;=========================================
yarspr dc.b $34,$36,$32,$38
sprdat
 incbin "YAR.dat"

