 ORG $2000
;                     
;  character set data 
;                     
chset
 DB $00,$00,$00,$00,$00,$00,$00,$00
 DB $01,$07,$17,$5F,$5F,$5F,$5F,$5F
 DB $55,$FF,$FF,$FF,$FF,$FF,$FF,$FF
 DB $50,$F4,$F4,$FD,$D5,$55,$45,$41
 DB $5F,$5D,$5D,$5D,$5D,$5F,$5F,$5F
 DB $5F,$57,$67,$97,$57,$5F,$5F,$5F
 DB $51,$55,$55,$55,$55,$D5,$F5,$FD
 DB $5F,$1F,$1F,$07,$07,$01,$01,$05
 DB $5F,$5F,$5F,$5F,$5F,$5D,$54,$55
 DB $FD,$FD,$FD,$F4,$F4,$50,$00,$00
 DB $05,$1F,$1F,$7F,$57,$55,$51,$41
 DB $55,$FF,$FF,$FF,$FF,$FF,$FF,$FF
 DB $40,$D0,$D4,$F5,$F5,$F5,$F5,$F5
 DB $45,$55,$55,$55,$55,$57,$5F,$7F
 DB $F5,$D5,$D9,$D6,$D5,$F5,$F5,$F5
 DB $F5,$75,$75,$75,$75,$F5,$F5,$F5
 DB $7F,$7F,$7F,$1F,$1F,$05,$00,$00
 DB $F5,$F5,$F5,$F5,$F5,$75,$15,$55
 DB $F5,$F4,$F4,$D0,$D0,$40,$40,$50
 DB $01,$07,$07,$1F,$1D,$15,$14,$14
 DB $55,$FF,$FF,$FF,$55,$55,$55,$14
 DB $40,$D0,$D0,$F4,$74,$54,$14,$14
 DB $75,$75,$75,$75,$75,$7D,$7F,$7F
 DB $14,$55,$55,$55,$55,$55,$55,$FF
 DB $5D,$5D,$5D,$5D,$5D,$7D,$FD,$FD
 DB $7F,$7F,$5F,$47,$47,$41,$50,$54
 DB $FF,$FF,$FF,$FF,$FF,$55,$00,$00
 DB $FD,$FD,$F5,$D1,$D1,$41,$05,$15
 DB $01,$07,$07,$1F,$1D,$1D,$1D,$1D
 DB $55,$FF,$55,$55,$55,$41,$55,$00
 DB $40,$D0,$D0,$F4,$74,$74,$74,$74
 DB $75,$74,$75,$74,$75,$74,$75,$75
 DB $55,$55,$55,$55,$55,$55,$55,$00
 DB $5D,$1D,$5D,$1D,$5D,$1D,$5D,$5D
 DB $7D,$7F,$5F,$47,$47,$41,$50,$54
 DB $55,$FF,$FF,$FF,$FF,$55,$00,$00
 DB $7D,$FD,$F5,$D1,$D1,$41,$05,$15
 DB $3C,$6A,$BF,$F5,$5F,$FB,$6E,$3C
 DB $3C,$6A,$BF,$F5,$5F,$FB,$6E,$3C
 DB $3C,$6A,$BF,$F5,$5F,$FB,$6E,$3C
 DB $3C,$6A,$BF,$F5,$5F,$FB,$6E,$3C
 DB $AA,$AA,$AA,$AA,$AA,$AA,$AA,$AA
 DB $00,$3F,$3F,$3F,$3F,$3F,$3F,$00
 DB $00,$00,$0F,$0F,$0F,$0F,$00,$00
 DB $00,$00,$00,$05,$05,$00,$00,$00
 DB $00,$7C,$EE,$EE,$EE,$EE,$7C,$00
 DB $00,$38,$78,$38,$38,$38,$FE,$00
 DB $00,$7C,$EE,$1C,$38,$70,$FE,$00
 DB $00,$FE,$1C,$38,$1C,$EE,$7C,$00
 DB $00,$1C,$3C,$7C,$EC,$FE,$1C,$00
 DB $00,$FE,$E0,$FC,$0E,$EE,$7C,$00
 DB $00,$7C,$E0,$FC,$EE,$EE,$7C,$00
 DB $00,$FE,$0E,$1C,$38,$70,$70,$00
 DB $00,$7C,$EE,$7C,$EE,$EE,$7C,$00
 DB $00,$7C,$EE,$7E,$0E,$1C,$78,$00
 DB $00,$7C,$E0,$7C,$0E,$FE,$7C,$00
 DB $00,$E0,$E0,$E0,$E0,$E0,$FE,$00
 DB $00,$38,$FE,$38,$38,$3E,$1E,$00
 DB $00,$7C,$FE,$E6,$E0,$E0,$E0,$00
 DB $00,$7C,$EE,$FE,$E0,$FE,$7C,$00
 DB $00,$FC,$FE,$E6,$E6,$E6,$E6,$00
 DB $00,$7E,$F6,$F6,$7E,$0E,$7C,$00
 DB $00,$E0,$E0,$FC,$EE,$EE,$EE,$00
 DB $00,$F8,$78,$78,$78,$78,$FC,$00
 DB $00,$EE,$EE,$EE,$7C,$38,$10,$00
 DB $00,$7C,$E0,$E0,$E0,$FC,$7C,$00
 DB $00,$7C,$EE,$EE,$EE,$FE,$7C,$00
 DB $00,$05,$1F,$1F,$7F,$7F,$7F,$7F
 DB $00,$01,$47,$DF,$DD,$FF,$FF,$FF
 DB $00,$40,$D0,$D0,$F4,$74,$74,$74
 DB $7F,$7F,$7F,$1F,$1F,$1F,$1F,$07
 DB $FF,$FF,$FF,$FF,$FF,$FF,$FF,$FF
 DB $F4,$F4,$F4,$D0,$D0,$D0,$D0,$40
 DB $07,$07,$01,$01,$00,$00,$00,$00
 DB $FF,$FF,$FD,$FD,$74,$10,$00,$00
 DB $40,$40,$00,$00,$00,$00,$00,$00
 DB $01,$05,$15,$15,$55,$55,$56,$54
 DB $55,$55,$55,$55,$55,$55,$56,$90
 DB $80,$60,$58,$58,$56,$56,$56,$56
 DB $58,$58,$54,$16,$15,$15,$05,$05
 DB $58,$64,$64,$46,$45,$65,$55,$55
 DB $96,$56,$56,$58,$58,$58,$60,$60
 DB $06,$00,$01,$05,$05,$01,$00,$00
 DB $66,$44,$11,$99,$55,$55,$10,$00
 DB $60,$00,$00,$60,$60,$80,$00,$00
 DB $02,$1A,$29,$AA,$99,$A5,$65,$A5
 DB $9A,$A9,$AA,$FF,$7F,$5D,$5D,$5D
 DB $A0,$9A,$A9,$E6,$5A,$55,$56,$56
 DB $95,$A5,$AD,$6F,$1F,$3F,$3F,$3F
 DB $9D,$9D,$7F,$F3,$FF,$F7,$D1,$40
 DB $96,$96,$5E,$FD,$FF,$FF,$FF,$7F
 DB $3D,$3D,$3F,$0F,$0F,$03,$00,$00
 DB $00,$00,$40,$D1,$F7,$FF,$FF,$00
 DB $1F,$1F,$7F,$FC,$FC,$F0,$C0,$00
 DB $01,$07,$1F,$1F,$7F,$7D,$75,$75
 DB $55,$FF,$FF,$FF,$FF,$FF,$7D,$BE
 DB $40,$D0,$F4,$F4,$FD,$7D,$5D,$5D
 DB $75,$75,$7D,$7F,$7F,$7F,$7D,$74
 DB $BE,$7D,$FF,$FF,$FF,$55,$00,$00
 DB $5D,$5D,$7D,$FD,$FD,$FD,$7D,$1D
 DB $7D,$7F,$7F,$7F,$7F,$7F,$1D,$04
 DB $00,$55,$FF,$FF,$FF,$F7,$D1,$40
 DB $7D,$FD,$FD,$FD,$FD,$FD,$F4,$50
 DB $01,$05,$05,$17,$15,$15,$55,$5D
 DB $5D,$75,$D5,$75,$5D,$55,$69,$AA
 DB $40,$50,$50,$54,$54,$54,$75,$5D
 DB $75,$D6,$76,$56,$56,$56,$D6,$75
 DB $AA,$8A,$82,$82,$92,$92,$A2,$AA
 DB $57,$9D,$95,$95,$95,$95,$97,$5D
 DB $55,$55,$15,$17,$15,$05,$05,$01
 DB $AA,$69,$55,$55,$D7,$DD,$75,$75
 DB $55,$55,$54,$54,$54,$50,$50,$40
 DB $40,$50,$55,$54,$15,$15,$15,$05
 DB $14,$55,$55,$55,$96,$96,$55,$55
 DB $01,$05,$55,$15,$54,$54,$54,$50
 DB $05,$01,$01,$2A,$2A,$AA,$AA,$A2
 DB $69,$82,$55,$55,$AA,$AA,$AA,$AA
 DB $50,$40,$40,$A8,$A8,$AA,$AA,$8A
 DB $A2,$92,$52,$40,$03,$03,$23,$2A
 DB $AA,$AA,$AA,$FF,$FF,$C3,$C3,$82
 DB $8A,$86,$85,$01,$C0,$C0,$C8,$A8
run_address
 LDA #$FF
 STA $0244
 LDA #$00
 STA just_loaded
 LDX #$FF
set_up_loop
 LDA #$01
 STA pm_area+$0300,X
 LDA $D20A
 STA starfield,X
 DEX
 BNE set_up_loop
code_start
 LDA #$00
 STA $D20E
 STA $D40E
 JSR off_video
 LDA #$E0
 STA $D409
 LDX #$04
b1_loop
 LDA hi_colours,X
 STA $D016,X
 DEX
 BPL b1_loop
 LDA #HIGH pm_area
 STA $D407
 LDA #$01
 STA $D01D
 LDA #$1D
 STA $D012
 LDA #LOW hi_dlist
 STA $D402
 LDA #HIGH hi_dlist
 STA $D403
 LDA #LOW dli1
 STA $0200
 LDA #HIGH dli1
 STA $0201
 JSR init_looney_tune
 LDA #$80
 STA $D40E
 JSR on_video
 LDA #$36
 STA $D400
 LDA just_loaded
 BEQ first_game
;                      
;  test for high score 
;                      
 LDA #LOW [score_starts+$0C]
 STA $CB
 LDA #HIGH [score_starts+$0C]
 STA $CC
 LDX #$00
hs1_loop
 LDY #$00
hs2_loop
 LDA ($CB),Y
 CLC
 ADC #$1D
 CMP scrn_start+$03E1,Y
 BMI a_new_high_score
 BNE hs1_skip
 INY
 CPY #$07
 BNE hs2_loop
hs1_skip
 LDA $CB
 CLC
 ADC #$14
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 INX
 CPX #$05
 BNE hs1_loop
 BEQ wait_for_start
a_new_high_score
 JSR add_high_score
 JMP wait_for_start
first_game
 INC just_loaded
wait_for_start
 LDA $D01F
 CMP #$06
 BNE wait_for_start
 JSR release_wait
;                   
;  the main program 
;                   
start
 LDA #$00
 LDX #$08
quiet_loop
 STA $D200,X
 DEX
 BPL quiet_loop
 JSR initialise
main_loop
 JSR consol_keys
 JSR vb_wait
 JSR perform_fire
 JSR collision_handling
 JSR handle_monsters
 JSR draw_player
 LDX #$07
m_loop1
 LDA $D20A
 AND m_data,X
 STA chset+$0128,X
 STA chset+$0130,X
 STA chset+$0138,X
 STA chset+$0140,X
 DEX
 BPL m_loop1
 JSR consol_keys
 JSR vb_wait
 JSR fire_test
 JSR perform_fire
 JSR collision_handling
 JSR handle_monsters
 JSR read_stick
 LDA stick_direction
 CMP #$FF
 BEQ continue
 LDA #$04
 STA frame
 JSR draw_player
 LDA stick_direction 
 BNE m_skip1
 JSR move_right     
 JMP continue
m_skip1
 CMP #$01
 BNE m_skip2
 JSR move_left
 JMP continue
m_skip2
 CMP #$02
 BNE m_skip3
 JSR move_down
 JMP continue
m_skip3
 CMP #$03
 BNE m_skip4
 JSR move_up
 JMP continue
m_skip4
 CMP #$04
 BNE m_skip5
 JSR move_down
 JSR move_right
 JMP continue
m_skip5
 CMP #$05
 BNE m_skip6
 JSR move_up
 JSR move_left
 JMP continue
m_Skip6
 CMP #$06
 BNE m_skip7
 JSR move_down
 JSR move_left
 JMP continue
m_skip7
 JSR move_up
 JSR move_right
continue
 JSR draw_player
 JMP main_loop

release_wait
 LDX #$04
debounce_loop
 LDA $D01F
 CMP #$07
 BNE release_wait
 DEX
 BPL debounce_loop
 RTS

consol_keys
 LDA $D01F
 CMP #$03
 BNE ck1_skip
 JSR no_noise
 JSR release_wait
ck1_loop
 LDA $D01F
 CMP #$03
 BNE ck1_loop
 JSR release_wait
 RTS
ck1_skip
 CMP #$06
 BNE ck2_skip
 JSR no_noise
 JSR release_wait
 LDX #$00
 TXS
 JMP code_start
ck2_skip
 RTS         

no_noise
 LDA #$00
 LDX #$05
nn1_loop
 STA $D200,X
 DEX
 DEX
 BPL nn1_loop
 RTS

just_loaded = $E5
m_data
 DB $3C,$7E,$FF,$FF,$FF,$FF,$7E,$3C
hi_colours
 DB $8C,$0E,$00,$4A,$00
hi_dlist
 DB $70,$70,$70,$B0,$46
 DW hi_scrn_start
 DB $70,$07,$30,$06,$30,$70,$07
 DB $70,$06,$70,$06,$70,$06
 DB $70,$06,$70,$06
 DB $70,$02,$00,$02,$00,$02,$41
 DW hi_dlist
hi_scrn_start
 DB 0,0,33,46,33,44,47,39,0
 DB 48,50,37,51,37,46,52,51,0,0,0
 DB 0,0,'floyd',0,'the',0
 DB 'droid',0,0,0
 DB 0,0,0,'goes',0,'blastin'
 DB 71,0,0,0,0
 DB 0,0,0,0,168,169,167,168,0
 DB 179,163,175,178,165,179
 DB 0,0,0,0,0
score_starts
 DB 0,240,236,225,249,243,239,230
 DB 244,0,0,0,16,16,16,17,16,16,16,0
 DB 0,240,236,225,249,243,239,230
 DB 244,0,0,0,16,16,16,16,21,16,16,0
 DB 0,240,236,225,249,243,239,230
 DB 244,0,0,0,16,16,16,16,18,21,16,0
 DB 0,240,236,225,249,243,239,230
 DB 244,0,0,0,16,16,16,16,17,18,21,0
 DB 0,240,236,225,249,243,239,230
 DB 244,0,0,0,16,16,16,16,16,21,16,0
 DB 0,0,0,0,0,0,0,35,'opyright',0
 DB 8,35,9,17,25,24,22,0
 DB 48,'laysoft',0,0,0,0,0,0,0
 DB 0,0,0,0,0,0,0,0,0,48,'rogrammed'
 DB 0,'by',0,48,'aul',0,44,'ay'
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0
 DB 0,0,48,'ress',0,179,180,161,178
 DB 180,0,'to',0,'begin'
 DB 0,'game',0,0,0,0,0,0,0,0

dli1
 PHA
 TXA
 PHA
 TYA
 PHA
 LDX #$A0
 LDY rainbow
dli1loop
 LDA starfield,X
 STA $D40A
 STA $D004
 STY $D018
 INY
 DEC starfield,X
 TXA
 AND #$01
 BEQ dli1skip
 DEC starfield,X
dli1skip
 DEX
 BNE dli1loop
 STX $D004
 LDY #$02
dli12loop
 LDA line_colours,Y
 LDX #$07
dli13loop
 STA $D40A
 STA $D018
 SEC
 SBC #$01
 DEX
 BPL dli13loop
 STA $D40A
 DEY
 BPL dli12loop
 INC rainbow
 JSR looney_tune
 PLA
 TAY
 PLA      
 TAX
 PLA
 RTI

pm_area = $3800
starfield = $3C00
line_colours
 DB $CA,$8A,$1A
rainbow = $E7

dli2
 PHA
 TXA
 PHA
 LDA #$0E
 LDX #$AA
 STA $D40A
 STX $D018
 STA $D017
 DEX
dli2loop
 STA $D40A
 STX $D018
 DEX
 CPX #$A2
 BNE dli2loop
 LDA #$86
 LDX #$CC
 STA $D40A
 STA $D017
 STX $D018
 PLA
 TAX
 PLA
 RTI
                
init_looney_tune
 LDA #$63
 STA $D208
 LDA #$03
 STA $D20F
 LDA #$C6
 STA $D201
 LDA #$00
 STA ltdelay
 STA the_note
 STA screach
 STA phase
 STA bass
 STA percuss
 RTS

looney_tune
 INC ltdelay
 LDA ltdelay
 AND #$01
 BEQ ltexit
 LDA screach
 STA $D200
 INC screach
 LDA $D20A
 AND #$3F
 STA $D204
 LDA percuss
 STA $D205
 CLC
 ADC #$04
 CMP #$09
 BCC lt1skip
 LDA #$00
lt1skip
 STA percuss
 LDA bass
 ORA #$A0
 STA $D203
 STA $D207
 LDX the_note
 LDA notes,X
 STA $D202
 CLC
 ADC #$01
 STA $D206
 LDA bass
 CLC
 ADC #$03
 CMP #$0D
 BCC lt2skip
 LDA #$00
lt2skip
 STA bass
 LDA phase
 CLC
 ADC #$14
 STA phase
 BVS ltnext
ltexit
 RTS
ltnext 
 INX
 CPX #$13
 BMI lt3skip
 LDX #$00
lt3skip
 STX the_note
 RTS

ltdelay = $E8
the_note = $E9
phase = $EA
screach = $EB
bass = $EC
percuss = $ED

notes
 DB 80,100,80,120,80,100,80,120
 DB 100,120,100,140,120,160,140
 DB 160,180,160,200

initialise
;                                  
;  set up the character set, clear 
;  the screen area, set up the     
;  display list, initialise the    
;  player, missiles and monsters   
;                                  
 LDA #$00
 STA $D40E
 JSR off_video
 LDA #HIGH chset
 STA $D409
 LDA #LOW scrn_start
 STA $CD
 LDA #HIGH scrn_start
 STA $CE
 LDA #$00
 LDX #$03
i_loop1
 LDY #$00
i_loop2
 STA ($CD),Y
 INY
 BNE i_loop2
 INC $CE
 DEX
 BPL i_loop1
 LDX #$27
i_loop3
 LDA text,X
 STA scrn_start+$03C0,X
 DEX
 BPL i_loop3        
 LDA #LOW d_list
 STA $D402
 LDA #HIGH d_list
 STA $D403
 LDA #LOW dli2
 STA $0200
 LDA #HIGH dli2
 STA $0201
 LDA #$80
 STA $D40E
 LDX #$04
i_loop4
 LDA colours,X
 STA $D016,X
 DEX
 BPL i_loop4
 LDA #$12
 STA play_x
 LDA #$0A
 STA play_y
 LDA #$00
 STA frame
 JSR draw_player
 LDX #$03
i_loop5
 LDA #$FF
 STA b_directions,X
 LDA #$00
 STA first_shot,X
 DEX
 BPL i_loop5
 STA alive_monsters
 STA init_monsters
 JSR handle_monsters
 LDA #$00
 STA $D208
 LDA #$03
 STA $D20F
 JSR on_video
 RTS      

play_x = $D0
play_y = $D1
frame = $DB
d_list
 DB $70,$70
 DB $44
 DW scrn_start
 DB $04,$04,$04,$04,$04,$04,$04
 DB $04,$04,$04,$04,$04,$04,$04
 DB $04,$04,$04,$04,$04,$04,$04
 DB $04,$84,$02
 DB $41
 DW d_list
text
 DB $37,$39,$3A,$3B,$3C,$3D,$39,$3E
 DB $00,$2D,$2D,$2E,$2D
 DB $00,$00
 DB $38,$3B,$40,$3B,$3F
 DB $00,$2D,$2D,$2D,$2D
 DB $00,$00
 DB $37,$41,$42,$3A,$3B
 DB $00,$2D,$2D,$2D,$2D,$2D,$2D,$2D
colours
 DB $0E,$86,$CC,$38,$00

vb_wait
;                              
;  wait for the vertical blank 
;  period                      
;                              
 LDA $D40B
 CMP #$74
 BNE vb_wait
 RTS

off_video
 LDA $D40B
 BNE off_video
 STA $D400
 RTS

on_video
 LDA $D40B
 BNE on_video
 LDA #$22
 STA $D400
 RTS

read_stick                     
;                               
;  read joystick in port #0 and 
;  return the direction in      
;  stick_direction              
;                               
 LDA $D300
 AND #$0F
 LDX #$07
r_loop1
 CMP joy_values,X
 BEQ r_skip1
 DEX
 BPL r_loop1
 LDX #$FF
r_skip1
 STX stick_direction
 RTS

stick_direction = $D6
joy_values
 DB $07,$0B,$0D,$0E
 DB $05,$0A,$09,$06
           
fire_test
;                               
;  test for the player firing a 
;  missile and activate a new   
;  missile appropriately        
;                               
 LDX #$03
test_missile_loop
 LDA b_directions,X
 CMP #$FF
 BEQ available_missiles
 DEX
 BPL test_missile_loop
 RTS
available_missiles
 LDA $D010
 BEQ trigger_pressed
 RTS
trigger_pressed
 STX $CD
 JSR read_stick
 LDX $CD
 LDA stick_direction
 CMP #$FF
 BNE fire_missile
 RTS
fire_missile
 STA b_directions,X
 INC first_shot,X
 CMP #$00
 BNE t_skip1
 LDA play_x
 CLC
 ADC #$02
 STA miss_x,X
 LDA play_y
 CLC
 ADC #$01
 STA miss_y,X
 RTS
t_skip1
 CMP #$01
 BNE t_skip2
 LDA play_x
 STA miss_x,X
 LDA play_y
 CLC
 ADC #$01
 STA miss_y,X
 RTS
t_skip2
 CMP #$02
 BNE t_skip3
 LDA play_x
 CLC
 ADC #$01
 STA miss_x,X
 LDA play_y
 CLC
 ADC #$02
 STA miss_y,X
 RTS
t_skip3
 CMP #$03
 BNE t_skip4
 LDA play_x
 CLC
 ADC #$01
 STA miss_x,X
 LDA play_y
 STA miss_y,X
 RTS
t_skip4
 CMP #$04
 BNE t_skip5
 LDA play_x
 CLC
 ADC #$02
 STA miss_x,X
 LDA play_y
 CLC
 ADC #$02
 STA miss_y,X
 RTS
t_skip5
 CMP #$05
 BNE t_skip6
 LDA play_x
 STA miss_x,X
 LDA play_y
 STA miss_y,X
 RTS
t_skip6
 CMP #$06
 BNE t_skip7
 LDA play_x
 STA miss_x,X
 LDA play_y
 CLC
 ADC #$02
 STA miss_y,X
 RTS
t_skip7
 LDA play_x
 CLC
 ADC #$02
 STA miss_x,X
 LDA play_y
 STA miss_y,X
 RTS

perform_fire
;                                  
;  move any active missiles in the 
;  appropriate direction           
;                                  
 LDX #$03
next_missile
 LDA b_directions,X
 CMP #$FF
 BNE live_missile
return
 DEX
 BPL next_missile
 RTS
live_missile
 LDA first_shot,X
 BNE move_missile
 LDA miss_x,X
 STA x_pos
 LDA miss_y,X
 STA y_pos
 JSR form_address
 LDA #$00
 LDY #$00
 STA ($CB),Y
move_missile
 LDA #$00
 STA first_shot,X
 LDA b_directions,X
 BNE p_skip1
 INC miss_x,X
 LDA miss_x,X
 BPL m_sound
p_skip1
 CMP #$01
 BNE p_skip2
 DEC miss_x,X
 LDA #$28
 SEC
 SBC miss_x,X
 BPL m_sound
p_skip2
 CMP #$02
 BNE p_skip3
 INC miss_y,X
 LDA miss_y,X
 BPL m_sound
p_skip3
 CMP #$03
 BNE p_skip4
 DEC miss_y,X
 LDA #$18
 SEC
 SBC miss_y,X   
 BPL m_sound
p_skip4
 CMP #$04
 BNE p_skip5
 INC miss_x,X
 INC miss_y,X
 LDA miss_y,X
 BPL m_sound
p_skip5
 CMP #$05
 BNE p_skip6
 DEC miss_x,X
 DEC miss_y,X
 LDA #$18
 SEC
 SBC miss_y,X
 BPL m_sound
p_skip6
 CMP #$06
 BNE p_skip7
 DEC miss_x,X
 INC miss_y,X
 LDA miss_y,X
 BPL m_sound
p_skip7
 INC miss_x,X
 DEC miss_y,X
 LDA #$18
 SEC
 SBC miss_y,X
m_sound
 LSR A
 STA $D200
 LDA #$8A
 STA $D201
range_check
 LDA miss_x,X
 BMI out_of_range
 CMP #$28
 BPL out_of_range
 STA x_pos
 LDA miss_y,X
 BMI out_of_range
 CMP #$18
 BPL out_of_range
 STA y_pos
 JSR form_address
 TXA
 CLC
 ADC #$A5
 LDY #$00
 STA ($CB),Y
 JMP return
out_of_range
 LDA #$00
 STA $D201
 LDA #$FF
 STA b_directions,X
 JMP return

draw_player
;                                
;  draw the player at the screen 
;  position specified in play_x  
;  & play_y with the specified   
;  frame                         
;                                
 LDA play_x
 STA x_pos
 LDA play_y
 STA y_pos
 JSR form_address 
 LDA #$02
 STA $CD
 LDA frame
 ASL A
 ASL A
 ASL A
 CLC
 ADC frame
 TAX
d_loop1
 LDY #$00
d_loop2
 LDA droid_data,X
 STA ($CB),Y
 INX
 INY
 CPY #$03
 BNE d_loop2
 LDA $CB
 CLC
 ADC #$28
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 DEC $CD
 BPL d_loop1
 RTS

droid_data
 DB $01,$02,$03,$04,$05,$06
 DB $07,$08,$09
 DB $0A,$0B,$0C,$0D,$0E,$0F
 DB $10,$11,$12           
 DB $13,$14,$15,$16,$17,$18
 DB $19,$1A,$1B
 DB $1C,$1D,$1E,$1F,$20,$21
 DB $22,$23,$24
 DB $00,$00,$00,$00,$00,$00
 DB $00,$00,$00

form_address
;                              
;  return the store address in 
;  locations $CB & $CD of the  
;  screen positions specified  
;  in x_pos and y_pos          
;                              
 LDA #LOW scrn_start
 STA $CB
 LDA #HIGH scrn_start
 STA $CC
 LDA y_pos
 STA $CD
 LDA #$00
 STA $CE
 LDY #$02
f_loop1
 ASL $CD
 ROL $CE
 DEY
 BPL f_loop1
 LDA $CB
 CLC
 ADC $CD
 STA $CB
 LDA $CC
 ADC $CE
 STA $CC
 LDY #$01
f_loop2
 ASL $CD
 ROL $CE
 DEY
 BPL f_loop2
 LDA $CB
 CLC
 ADC $CD
 STA $CB
 LDA $CC
 ADC $CE
 STA $CC
 LDA $CB
 CLC
 ADC x_pos
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 RTS
    
x_pos = $D4
y_pos = $D5
       
move_right
 LDA #$00
 STA frame
 LDA play_x
 CMP #$25
 BPL right_skip
 INC play_x
right_skip
 RTS
       
move_left
 LDA #$01
 STA frame
 LDA play_x
 BEQ left_skip
 DEC play_x
left_skip
 RTS

move_down
 LDA #$02
 STA frame
 LDA play_y
 CMP #$15
 BPL down_skip
 INC play_y
down_skip
 RTS

move_up
 LDA #$03
 STA frame
 LDA play_y
 BEQ up_skip
 DEC play_y
up_skip
 RTS
  
handle_monsters
;                           
;  update all the monsters, 
;  activating a new set if  
;  necessary                
;                           
 INC tick_tock
 LDA alive_monsters
 BNE move_monsters
 LDA init_monsters
 CMP #$FF
 BEQ h_skip1
 INC init_monsters
h_skip1
 LDX #$00
h_loop1
 JSR activate_new_monster
 INX
 CPX init_monsters
 BNE h_loop1
 STX alive_monsters
 STX new_activation
 JSR up_level
move_monsters
 LDX #$00
more_monsters_loop
 LDA m_directions,X
 CMP #$04
 BMI ss4
 JMP non_active_monster
ss4
 LDA new_activation
 BEQ ss1
 JMP a_new_set_of_monsters
ss1
 LDA m_delays,X
 BEQ ss2
 JMP sleeping_monster
ss2
 LDA #$04
 STA monsta
 JSR draw_monster
 LDA tick_tock
 AND #$01
 BEQ force_movement
 LDA init_monsters
 CMP #$14
 BCC display_monster
force_movement
 LDA m_directions,X
 BNE u_skip1
 INC m_xpositions,X
 LDA m_xpositions,X
 BNE test_on_screen
u_skip1
 CMP #$01
 BNE u_skip2
 DEC m_xpositions,X
 LDA m_xpositions,X
 JMP test_on_screen
u_skip2
 CMP #$02
 BNE u_skip3
 INC m_ypositions,X
 LDA m_ypositions,X
 BNE test_on_screen
u_skip3
 DEC m_ypositions,X
 LDA m_ypositions,X
test_on_screen
 STA $D204
 LDA #$26
 STA $D205
 LDA m_xpositions,X
 BMI is_off_screen
 CMP #$26
 BPL is_off_screen
 LDA m_ypositions,X
 BMI is_off_screen
 CMP #$16
 BMI display_monster
is_off_screen
 LDA #$00
 STA $D205
 JSR reactivate_monster
display_monster
 LDA m_frames,X
 STA monsta
 JSR draw_monster
 JMP h_skip2
non_active_monster
 CMP #$0D
 BEQ h_skip2
 AND #$0E
 SEC
 SBC #$04
 LSR A
 STA monsta
 BNE just_died_skip
 LDA #$00
 STA $D205
 BPL still_dying
just_died_skip
 CMP #$04
 BNE still_dying
 DEC alive_monsters
 LDA #$00
 STA $D203         
 BPL skip_sound
still_dying
 ASL A
 ASL A
 ASL A
 ASL A
 STA $D202
 LDA #$2E
 STA $D203
skip_sound
 JSR draw_monster
 INC m_directions,X
h_skip2
 INX
 CPX init_monsters
 BEQ ss3
 JMP more_monsters_loop
ss3
 RTS
a_new_set_of_monsters
 LDA #$00
 STA new_activation
 BEQ display_monster
sleeping_monster
 DEC m_delays,X
 JMP display_monster

alive_monsters = $DC
init_monsters = $DD
new_activation = $DE
tick_tock = $E4

activate_new_monster
;                            
;  activate a new monster as 
;  specified in register X   
;                            
 LDA $D20A
 AND #$07
 CLC
 ADC #$05
 STA m_frames,X
reactivate_monster
;                               
;  special entry point allowing 
;  for the reactivation of an   
;  existing monster, again      
;  specified in register X      
;                               
 LDA $D20A
 AND #$03
 STA m_directions,X
 BNE a_skip1
 LDA #$00
 STA m_xpositions,X
 LDA $D20A
 AND #$1F
 STA m_ypositions,X
 BPL bounds_check
a_skip1
 CMP #$01
 BNE a_skip2
 LDA #$25
 STA m_xpositions,X
 LDA $D20A
 AND #$1F
 STA m_ypositions,X
 BPL bounds_check
a_skip2
 CMP #$02
 BNE a_skip3
 LDA $D20A
 AND #$3F
 STA m_xpositions,X
 LDA #$00
 STA m_ypositions,X
 BPL bounds_check
a_skip3
 LDA $D20A
 AND #$3F
 STA m_xpositions,X
 LDA #$15
 STA m_ypositions,X
bounds_check
 LDA m_xpositions,X
 CMP #$26
 BMI a_skip4
 AND #$25
 STA m_xpositions,X
a_skip4
 LDA m_ypositions,X
 CMP #$16
 BMI a_skip5
 AND #$15
 STA m_ypositions,X
a_skip5
 LDA $D20A
 AND #$3F
 STA m_delays,X
 RTS

draw_monster
;                                
;  draw the monster specified in 
;  register X with the frame     
;  specified in monsta           
;                                
 STX $E0
 LDA m_xpositions,X
 STA x_pos
 LDA m_ypositions,X
 STA y_pos
 JSR form_address
 LDA #$02
 STA $CD
 LDA monsta
 ASL A
 ASL A
 ASL A
 CLC
 ADC monsta 
 TAX
row_loop
 LDY #$02
column_loop
 LDA evil_monster_data,X
 STA ($CB),Y
 INX
 DEY
 BPL column_loop
 LDA $CB
 CLC
 ADC #$28
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 DEC $CD
 BPL row_loop
 LDX $E0
 RTS
 
monsta = $DF
evil_monster_data
 DB $29,$29,$29,$29,$29,$29
 DB $29,$29,$29
 DB $AA,$AA,$AA,$AA,$AA,$AA
 DB $AA,$AA,$AA
 DB $2B,$2B,$2B,$2B,$2B,$2B
 DB $2B,$2B,$2B
 DB $2C,$2C,$2C,$2C,$2C,$2C
 DB $2C,$2C,$2C
 DB $00,$00,$00,$00,$00,$00
 DB $00,$00,$00
 DB $C5,$C4,$C3,$C8,$C7,$C6
 DB $CB,$CA,$C9
 DB $4E,$4D,$4C,$51,$50,$4F
 DB $54,$53,$52
 DB $57,$56,$55,$5A,$59,$58
 DB $5D,$5C,$5B
 DB $60,$5F,$5E,$63,$62,$61
 DB $66,$65,$64
 DB $E9,$E8,$E7,$EC,$EB,$EA
 DB $EF,$EE,$ED
 DB $72,$71,$70,$75,$74,$73
 DB $78,$77,$76
 DB $E0,$DF,$DE,$E3,$E2,$E1
 DB $E6,$E5,$E4
 DB $D7,$D6,$D5,$DA,$D9,$D8
 DB $DD,$DC,$DB   

collision_handling
;                               
;  determine whether any of the 
;  currently active monsters    
;  have collided with either a  
;  bullet or the player         
;                               
 LDX #$00
c_main_loop
 LDA m_directions,X
 CMP #$04            
 BPL next_monster
 LDA m_xpositions,X
 STA x_pos
 LDA m_ypositions,X
 STA y_pos
 JSR form_address
 LDA #$02
 STA $CD
next_row
 LDY #$02
next_column 
 LDA ($CB),Y
 AND #$7F
 BEQ no_collision
 CMP #$29
 BPL no_collision
 CMP #$25
 BMI player_collision_only
 SEC
 SBC #$25
 TAY
 LDA #$FF
 STA b_directions,Y
 LDA #$00
 STA $D201
 BEQ both_collisions
player_collision_only
 LDA m_frames,X
 CMP #$05
 BNE no_heart
 JSR up_strength
 JMP both_collisions
no_heart
 JSR down_strength
both_collisions
 LDA #$04
 STA m_directions,X
 JSR up_score
next_monster
 INX
 CPX init_monsters
 BNE c_main_loop
 RTS
no_collision
 DEY
 BPL next_column
 LDA $CB
 CLC
 ADC #$28
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 DEC $CD
 BPL next_row
 BMI next_monster                     
down_strength
;                               
;  decrement the strength total 
;  by 1                         
;                               
 LDY #$03
ds_1loop
 LDA scrn_start+$03C9,Y
 SEC
 SBC #$01
 CMP #$2D
 BMI ds_1skip
 STA scrn_start+$03C9,Y
 CPY #$03
 BNE ds_2skip
ds_2loop
 LDA scrn_start+$03C9,Y
 CMP #$2D
 BNE ds_2skip
 DEY
 BPL ds_2loop
 BMI death
ds_2skip
 RTS
ds_1skip
 LDA #$36
 STA scrn_start+$03C9,Y
 DEY
 BPL ds_1loop
 RTS
death
 LDA #$00
 STA $D201
 STA $D203
 LDA #$AE
 STA $D205
 LDA #$1F
 STA $E1
dth_1loop
 LDA #$FF
 STA $E2
dth_2loop
 LDA play_x
 STA x_pos
 LDA play_y
 STA y_pos
 JSR form_address
 LDX #$02
dth_3loop
 LDY #$02
dth_4loop
 LDA $D20A
 STA $D204
 AND #$83
 CLC
 ADC #$29
 STA ($CB),Y
 DEY
 BPL dth_4loop
 LDA $CB
 CLC
 ADC #$28
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 DEX
 BPL dth_3loop
 DEC $E2
 BNE dth_2loop
 DEC $E1
 BNE dth_1loop
 LDX #$00
 STX $D205
 TXS
 JMP code_start

up_strength
;                               
;  increment the strength total 
;  by 2                         
;                               
 LDA scrn_start+$03CC
 CLC
 ADC #$02
 CMP #$37
 BPL ps_1skip
 STA scrn_start+$03CC
 RTS
ps_1skip             
 SEC
 SBC #$0A
 STA scrn_start+$03CC
 LDY #$02
ps_1loop
 LDA scrn_start+$03C9,Y
 CLC
 ADC #$01
 CMP #$37
 BMI ps_2skip
 LDA #$2D
 STA scrn_start+$03C9,Y
 DEY
 BPL ps_1loop
 RTS
ps_2skip
 STA scrn_start+$03C9,Y
 RTS

up_level
;                              
;  increment the level display 
;                              
 LDY #$03
ul_loop1                       
 LDA scrn_start+$03D5,Y
 CLC
 ADC #$01
 CMP #$37
 BMI ul_skip1
 LDA #$2D
 STA scrn_start+$03D5,Y
 DEY
 BPL ul_loop1
 RTS
ul_skip1
 STA scrn_start+$03D5,Y
 RTS

up_score
;                              
;  increment the score display 
;                              
 LDY #$06
us_loop1
 LDA scrn_start+$03E1,Y
 CLC
 ADC #$01
 CMP #$37
 BMI us_skip1
 LDA #$2D
 STA scrn_start+$03E1,Y
 DEY
 BPL us_loop1
 RTS
us_skip1
 STA scrn_start+$03E1,Y
 RTS

add_high_score
;                               
;  make a high score entry for  
;  the table position passed in 
;  register X                   
;                               
 STX $E3
 LDA #LOW [score_starts+60]
 STA $CB
 LDA #HIGH [score_starts+60]
 STA $CC
 LDA #$04
 SEC
 SBC $E3
 TAX
ah1_loop
 BEQ ah1_skip
 LDA $CB
 CLC 
 ADC #$14
 STA $CD
 LDA $CC
 ADC #$00
 STA $CE
 LDY #$13
ah2_loop
 LDA ($CB),Y
 STA ($CD),Y
 DEY
 BPL ah2_loop
 LDA $CB
 SEC
 SBC #$14
 STA $CB
 LDA $CC
 SBC #$00
 STA $CC
 DEX
 BPL ah1_loop
ah1_skip
 LDA #LOW score_starts
 STA $CB
 LDA #HIGH score_starts
 STA $CC
 LDA $E3
 ASL A
 ASL A
 PHA
 CLC
 ADC $CB
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 PLA
 ASL A
 ASL A
 CLC
 ADC $CB
 STA $CB
 LDA $CC
 ADC #$00
 STA $CC
 LDY #$08
ah3_loop
 LDA name_buffer-1,Y
 TAX
 LDA name_characters,X
 STA ($CB),Y
 DEY
 BNE ah3_loop
 LDA $CB
 CLC
 ADC #$0C
 STA $CD
 LDA $CC
 ADC #$00
 STA $CE
 LDY #$06
ah4_loop
 LDA scrn_start+$03E1,Y
 SEC
 SBC #$1D
 STA ($CD),Y
 DEY
 BPL ah4_loop
;                 
;  now enter name 
;                 
 LDA $CB
 CLC
 ADC #$01
 STA $CD
 LDA $CC
 ADC #$00
 STA $CE
 LDY #$00
en1_loop
 JSR vb_delay
 LDA #$7F
 STA ($CD),Y
 JSR vb_delay
 LDA name_buffer,Y
 TAX
 LDA name_characters,X
 STA ($CD),Y
 LDA $D010
 BEQ en5_skip
 JSR read_stick
 LDA stick_direction
 BNE en1_skip
 INY
 CPY #$08
 BMI en1_loop
 LDY #$00
 BPL en1_loop
en1_skip
 CMP #$01
 BNE en2_skip
 DEY
 BPL en1_loop
 LDY #$07
 BPL en1_loop 
en2_skip
 CMP #$02
 BNE en3_skip
 LDA name_buffer,Y
 SEC
 SBC #$01
 BPL en4_skip
 LDA #$24
 BPL en4_skip
en3_skip
 CMP #$03
 BNE en1_loop
 LDA name_buffer,Y
 CLC
 ADC #$01
 CMP #$25
 BMI en4_skip
 LDA #$00
en4_skip
 STA name_buffer,Y
 JMP en1_loop
en5_skip
 LDA $D010
 BEQ en5_skip
 RTS

vb_delay
 LDX #$02
vb1_loop
 LDA $D40B
 BNE vb1_loop
vb2_loop
 LDA $D40B
 BEQ vb2_loop
 DEX
 BPL vb1_loop
 RTS

name_characters
 DB 0
character SET 225
 ECHO 26
 DB character
character SET character+$01
 	ENDM
character SET 208
 ECHO 10
 DB character
character SET character+$01
 	ENDM

name_buffer
 DB 0,0,0,0,0,0,0,0

;               
;  some equates 
;                           
m_frames

m_directions = m_frames + $0100

m_xpositions = m_directions + $0100

m_ypositions = m_xpositions + $0100

m_delays = m_ypositions + $0100

b_directions = m_delays + $0100

first_shot = b_directions + $04

miss_x = first_shot + $04

miss_y = miss_x + $04

scrn_start = miss_y + $04

 END run_address
