 org $1FFD

begining
 JMP start


 include d:chset.src


 include d:terrain.src


title_memory = $5000
pause_memory = $6800
screen_memory = $7400
pmbase = $7800
play1 = pmbase+$0500
play2 = pmbase+$0600
play3 = pmbase+$0700
starfield = pmbase


display_list
 DB $30,$30,$70,$F0
         
; space
 DB $42
 DW viewport

 DB $42
 DW screen_memory
 DB $02,$02,$02,$02,$02,$02,$02,$02
 DB $02,$02,$02,$02,$02
           
; horizon
 DB $44
 DW horizon
               
; landscape
scroll_area
 DB $4F
 DW terrain_data
 DB $0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
 DB $0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
 DB $0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
 DB $0F,$0F,$0F,$0F,$0F,$0F,$00

; control panel
 DB $42
 DW control_panel
 DB $00,$02,$02,$00,$02

 DB $41
 DW display_list
 

start
 LDA #LOW busted
 STA $02
 LDA #HIGH busted
 STA $03
 LDA #$02
 STA $09
 LDA #$00
 STA $244
 STA toggle
 STA pointer
 STA $D4
 LDA #$01
 STA number_of_ships
 LDA #$03
 STA $D20F
 STA $D01D
 LDA #LOW kb_int
 STA $0208
 LDA #HIGH kb_int
 STA $0209
 LDA #$40
 STA $D20E
 LDA #HIGH pmbase
 STA $D407
 LDA #$80
 STA $D40E
 LDA #$3E
 STA $D400

 JSR clr_pause_display

 JSR set_up_letters

main_program
 JSR wait_for_start

 JSR vb_wait
 LDA #$00
 STA $D400

 LDA #LOW display_list
 STA $D402
 LDA #HIGH display_list
 STA $D403

 LDA #LOW dli
 STA $200
 LDA #HIGH dli
 STA $201

 LDA #$DE
 STA $D013
 LDA #$3C
 STA $D014
 LDA #$8C
 STA $D015
 JSR pm_init

 LDX #$04
loop1
 LDA colours,X
 STA $D016,X
 DEX
 BPL loop1

 JSR clr_screen

 LDA #$00
 STA released
 STA chr_offset
 STA $D208
 LDX #$02
horiz_init
 STA p1x,X
 DEX
 BPL horiz_init

 LDA #$FF
 STA left_frame
 STA right_frame
 STA shakin
 STA tons
   
 LDA #$78
 STA x_cursor
 STA y_cursor
          
 LDA #$10
 STA shields+$01
 STA shields+$02
 LDX #$04
loop70
 STA score,X
 DEX
 BPL loop70

 LDA #$11
 STA shields

 LDA #$04
 STA llength

 JSR vb_wait
 LDA #$3E
 STA $D400

set_level
 LDA number_of_ships
 STA alive_ships
 JSR init_fleet
 JSR vb_waste

loop3     
 JSR vb_wait
 JSR handle_ship
 JSR move_missiles
 JSR vb_waste

 JSR vb_wait 
 JSR move_land
 JSR move_cursor
 JSR fade_em
 JSR vb_waste
       
 JSR vb_wait
 JSR move_missiles
 JSR effects
 JSR play_keys
 JSR vb_waste

 JSR vb_wait  
 JSR move_land     
 JSR move_cursor
 JSR fade_em
 JSR vb_waste

 LDA alive_ships
 BNE loop3

 JSR vb_wait
 
 JSR up_wave
 LDA number_of_ships
 CMP #$FF
 BEQ set_level
 CLC 
 ADC #$01
 STA number_of_ships
 BNE set_level

colours
 DB $00,$0C,$00,$46,$00

llength
 DB $00

lcol_1
 DB $44

lcol_2
 DB $4A

up_wave
 LDX #$01
wave_loop
 LDA wave,X
 CLC
 ADC #$01
 CMP #$1A
 BEQ next_wave_digit
 STA wave,X
 RTS
next_wave_digit
 LDA #$10
 STA wave,X
 DEX
 BPL wave_loop
 RTS


init_fleet
 LDX #$00
init1loop
 JSR init_ship
 INX
 CPX number_of_ships
 BNE init1loop
 RTS


vb_wait
 LDA $D40B
 BNE vb_wait
 RTS


vb_waste
 LDA $D40B
 BEQ vb_waste
 RTS
                  

move_land
 LDX llength
 INX
 CPX #$05
 BNE set_llength
 LDA lcol_1
 PHA
 LDA lcol_2
 STA lcol_1
 PLA
 STA lcol_2
 LDX #$01
set_llength
 STX llength
 RTS


dli
 PHA
 TXA
 PHA
 TYA
 PHA

 LDX #$8B 
loop6
 STX $D40A
 STX $D018
 DEX
 CPX #$83
 BNE loop6

 LDA #$41
 LDX #HIGH character_set
 STA $D40A
 STA $D01B              
 STX $D409
         
 LDA starfield+$6F
 PHA
 LDX #$6E
loop4
 LDA starfield,X
 STA $D40A
 STA $D004
 STA starfield+$01,X
 STX $D012
 DEX
 BNE loop4
 PLA
 STA starfield+$01

 LDA #$00
 STA $D40A
 STA $D004

 LDA #$01
 STA $D40A
 STA $D01B
          
 LDX #$08
loop8
 STX $D40A
 DEX
 BNE loop8

 LDA lcol_1
 LDY lcol_2
 STA $D017
 STY $D018
 LDX llength
lloop_1
 STX $D40A
 DEX
 BNE lloop_1
 STA $D018
 STY $D017
 LDX #$05
lloop_2
 STX $D40A
 DEX
 BNE lloop_2
 STA $D017
 STY $D018
 LDX #$06
lloop_3
 STX $D40A
 DEX
 BNE lloop_3
 STA $D018
 STY $D017
 LDX #$07
lloop_4
 STX $D40A
 DEX 
 BNE lloop_4
 STA $D017
 STY $D018
 LDX #$08
lloop_5
 STX $D40A
 DEX
 BNE lloop_5
 LDX llength
 CPX #$04
 BEQ lskip_1
 STA $D018
 STY $D017
lloop_6
 STX $D40A
 INX
 CPX #$04
 BNE lloop_6
lskip_1
 LDA #$0C
 STA $D40A
 STA $D017
 LDX #$8B
 LDA #$E0
 STA $D40A
 STX $D018
 STA $D409
 DEX

loop7
 STX $D40A
 STX $D018
 DEX
 CPX #$83
 BNE loop7

 LDA #$00
 STA $D40A
 STA $D017

 LDX #$CB
loop9
 STX $D40A
 STX $D018
 STX $D40A
 DEX
 CPX #$C3
 BNE loop9

 LDA #$0C
 STA $D40A
 STA $D017

 LDX #$8B
loop11
 STX $D40A
 STX $D018
 DEX
 CPX #$83
 BNE loop11

 PLA
 TAY
 PLA
 TAX
 PLA
 RTI       


viewport
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0  
 DB 214,246,233,229,247
 DB 240,239,242,244,194
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

control_panel                    
 DB 0,0,0,0,0,0,0,0,0,0,0,0
 DB 214,227,239,238,244,242,239,236
 DB 128,240,225,238,229,236,194
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0

 DB 0,0,0,0,0,0,0
 DB 51,'core',0,0,0,0,0
 DB 51,'hields',0,0,0,0,0
 DB 55,'ave'
 DB 0,0,0,0,0,0,0

 DB 0,0,0,0,0,0,0
 DB 16,16,16,16,16,0,0,0,0,0
 DB 0,17,16,16,0,5,0,0,0,0,0,0
 DB 0,16,17,0
 DB 0,0,0,0,0,0,0

 DB 0,0,0,0,0,0,0,0,0
 DB 48,'rogrammed',0,34,'y',0
 DB 48,'aul',0,44,'ay'
 DB 0,0,0,0,0,0,0,0,0

score = control_panel+87
shields = control_panel+98
wave = control_panel+110


evaluate_address

; take the coordinates x,y in $F0,$F1
; and return the screen address in
; $CC,$CD

 TXA
 PHA

; first evaluate 40*y

 LDA #$00
 STA $CD
 LDA $F1

 ASL A   ; 2*y
 ROL $CD      
 ASL A   ; 4*y
 ROL $CD      
 ASL A   ; 8*y
 ROL $CD

 STA $CE ; save 8*y
 LDX $CD
 STX $CF

 ASL A   ; 16*y
 ROL $CD
 ASL A   ; 32*y
 ROL $CD

 CLC     ; add 32*y and 8*y
 ADC $CE                   
 STA $CC
 LDA $CD
 ADC $CF
 STA $CD

; now add in x
 
 LDA $CC
 CLC
 ADC $F0
 STA $CC
 BCC skip2
 INC $CD

skip2

; and add screen memory

 LDA $CC
 CLC
 ADC #LOW screen_memory
 STA $CC
 LDA $CD
 ADC #HIGH screen_memory
 STA $CD

 PLA
 TAX

; and return

 RTS


draw_big_ship

; draw a big space ship at
; coordinates x,y specified in $F0,
; $F1

 JSR evaluate_address

 LDA #$09      
 CLC
 ADC chr_offset
 LDY #$05
loop20
 STA ($CC),Y
 SEC
 SBC #$01
 DEY
 BPL loop20

 LDA #$0F 
 CLC
 ADC chr_offset
 LDY #$2D
loop21
 STA ($CC),Y
 SEC
 SBC #$01
 DEY     
 CPY #$27
 BNE loop21

 RTS


draw_medium_ship

; draw a medium space ship at
; coordinates x,y specified in $F0,
; $F1

 JSR evaluate_address

 LDA #$13
 CLC
 ADC chr_offset
 LDY #$03
loop22
 STA ($CC),Y
 SEC
 SBC #$01
 DEY
 BPL loop22

 LDA #$17      
 CLC
 ADC chr_offset
 LDY #$2B
loop23
 STA ($CC),Y
 SEC
 SBC #$01
 DEY
 CPY #$27     
 BNE loop23

 RTS


draw_small_ship

; draw a small space ship at
; coordinates x,y specified in $F0,
; $F1

 JSR evaluate_address

 LDA #$1A
 CLC
 ADC chr_offset
 LDY #$02
loop24
 STA ($CC),Y
 SEC
 SBC #$01
 DEY
 BPL loop24

 RTS


erase_big_ship
 LDA #$00


over_big_write
 PHA
 JSR evaluate_address
 PLA
 LDY #$05
loop30
 STA ($CC),Y
 DEY
 BPL loop30
 LDY #$2D
loop31
 STA ($CC),Y
 DEY
 CPY #$27
 BNE loop31
 RTS


erase_middle_ship
 LDA #$00


over_middle_write
 PHA
 JSR evaluate_address
 PLA
 LDY #$03
loop32
 STA ($CC),Y
 DEY
 BPL loop32
 LDY #$2B
loop33
 STA ($CC),Y
 DEY
 CPY #$27
 BNE loop33
 RTS

 
erase_small_ship
 LDA #$00

over_small_write
 PHA
 JSR evaluate_address
 PLA
 LDY #$02
loop34
 STA ($CC),Y
 DEY
 BPL loop34
 RTS


init_ship
 LDA $D20A
 AND #$3F
 CMP #$23
 BPL init_ship
 STA ship_x,X
 LDA #$0E
 STA ship_y,X
 LDA #$FF
 STA ship_direction,X
 LDA #$20
 STA ship_stage,X
 LDA $D20A
 AND #$7F
 STA ship_counter,X
 RTS


wipe_ship
 BNE test1
 JMP erase_small_ship
test1
 CMP #$01
 BNE test2
 JMP erase_medium_ship
test2
 JMP erase_big_ship


draw_ship
 BNE test3
 JMP draw_small_ship
test3
 CMP #$01
 BNE test4
 JMP draw_medium_ship
test4
 JMP draw_big_ship


handle_ship
 LDX #$00
 STX wings

main_loop
 LDA ship_stage,X
 CMP #$03
 BMI move_ship

 AND #$10
 BNE dying
        
 LDA ship_stage,X
 AND #$20
 BEQ next_ship

 DEC ship_counter,X
 BNE next_ship
 LDA #$00
 STA ship_stage,X
 LDA #$09
 STA ship_counter,X

next_ship
 INX 
 CPX number_of_ships
 BNE main_loop
 RTS

dying
 DEC ship_counter,X
 BMI dead
    
 LDA #$17
 STA chr_offset
 LDA ship_x,X
 STA $F0
 LDA ship_y,X
 STA $F1
 LDA ship_stage,X
 AND #$03
 JSR draw_ship
 LDA #$00
 STA chr_offset
 JMP next_ship

dead
 LDA ship_x,X
 STA $F0
 LDA ship_y,X
 STA $F1
 LDA ship_stage,X
 AND #$03
 JSR wipe_ship
 LDA #$03
 STA ship_stage,X
 DEC alive_ships
 JMP next_ship


move_ship
 LDA ship_x,X
 STA $F0
 LDA ship_y,X
 STA $F1
 LDA ship_stage,X
 JSR wipe_ship

 DEC ship_counter,X
 BNE skip10

 LDA #$09
 STA ship_counter,X
 INC ship_stage,X
 LDA ship_stage,X
 CMP #$03
 BMI skip10
           
 JSR down_shields
 LDA #$04
 STA shakin
 JSR init_ship
 JMP next_ship   

skip10
 LDA ship_y,X
 CLC
 ADC ship_direction,X
 STA ship_y,X
 STA $F1
 STA wings
 BNE skip11
 LDA #$01
 STA ship_direction,X

skip11
 LDA $D20A
 CMP #$19
 BCS skip12
 LDA ship_x,X
 BEQ draw_it
 DEC ship_x,X
 BPL draw_it

skip12
 LDA $D20A
 CMP #$19
 BCS draw_it
 LDA ship_x,X
 CMP #$22
 BEQ draw_it
 INC ship_x,X

draw_it
 LDA ship_x,X
 STA $F0
 LDA ship_stage,X
 JSR draw_ship
 JMP next_ship


move_cursor
 LDA $D010
 BNE no_fire

 LDA released
 BEQ proceed

 LDA #$00
 STA released

try_left
 LDA left_frame
 BPL try_right
 JMP fire_left_missile

try_right
 LDA right_frame
 BPL proceed
 JMP fire_right_missile

no_fire
 STA released

proceed
 LDY #$0F
 LDX y_cursor
 LDA #$00
loop50
 STA play1,X
 INX
 DEY
 BPL loop50

 LDA $D300
 AND #$0F
 STA $CC
 
 LDX toggle
 BEQ try0
     
 LDX #$00
 STX $EF
 LSR A
 ROL $EF   
 LSR A
 ROL $EF
 LDA $CC
 AND #$FC
 ORA $EF
 STA $CC
 
try0
 LSR $CC
 BCS try1
 LDA y_cursor
 SEC
 SBC #$08
 STA y_cursor

try1
 LSR $CC
 BCS try2
 LDA y_cursor
 CLC
 ADC #$08
 STA y_cursor

try2
 LSR $CC
 BCS try3
 LDA x_cursor
 SEC
 SBC #$08
 STA x_cursor

try3
 LSR $CC
 BCS check1
 LDA x_cursor
 CLC
 ADC #$08
 STA x_cursor

check1
 LDA x_cursor
 CMP #$30
 BCS check2
 LDA #$30
 STA x_cursor
 BPL check3

check2
 CMP #$C9
 BCC check3
 LDA #$C8
 STA x_cursor

check3
 LDA y_cursor
 CMP #$28
 BCS check4
 LDA #$28
 STA y_cursor
 BPL draw_cursor

check4
 CMP #$B1
 BCC draw_cursor
 LDA #$B0
 STA y_cursor
 
draw_cursor
 LDY #$0F
 LDX y_cursor
loop51
 LDA cross_hair,Y
 STA play1,X
 INX
 DEY
 BPL loop51

 LDA x_cursor
 STA $D001
 STA p1x
 
 RTS

cross_hair
 DB 0,84,146,146,16,16,16,238
 DB 238,16,16,16,146,146,84,0

x_cursor = $F2
y_cursor = $F3


fire_left_missile
 LDA x_cursor
 SEC
 SBC #$30
 LSR A
 LSR A
 LSR A
 STA left_x_inc

 LDA #$B0
 SEC
 SBC y_cursor
 LSR A
 LSR A
 LSR A
 STA left_y_dec

set_left_fire
 LDA #$08
 STA left_frame
 LDA #$30
 STA x_left
 LDA #$B0
 STA y_left
 LDA #$04
 STA tons
 JMP draw_left_missile


move_left_missile
 LDA left_frame
 BPL left_is_live
 RTS

left_is_live
 LDY #$0F
 LDX y_left
 LDA #$00
loop60
 STA play2,X
 INX
 DEY
 BPL loop60

 LDA x_left
 CLC
 ADC left_x_inc
 STA x_left

 LDA y_left
 SEC
 SBC left_y_dec
 STA y_left

 DEC left_frame
 BMI left_burned_out


draw_left_missile
 LDY #$0F
 LDX y_left
loop61
 LDA photon,Y
 AND $D20A
 STA play2,X
 INX
 DEY
 BPL loop61
 
 LDA x_left
 STA $D002
 STA p2x

left_burned_out
 RTS

left_frame = $F4
left_x_inc = $F5
left_y_dec = $F6
x_left = $F7
y_left = $F8


fire_right_missile
 LDA #$C8
 SEC
 SBC x_cursor
 LSR A
 LSR A
 LSR A
 STA right_x_dec

 LDA #$B0
 SEC
 SBC y_cursor
 LSR A
 LSR A
 LSR A
 STA right_y_dec

 LDA #$08
 STA right_frame
 LDA #$C8
 STA x_right
 LDA #$B0
 STA y_right
 LDA #$04
 STA tons
 JMP draw_right_missile


move_right_missile
 LDA right_frame
 BPL right_is_live
 RTS

right_is_live
 LDY #$0F
 LDX y_right
 LDA #$00
loop62
 STA play3,X
 INX
 DEY
 BPL loop62

 LDA x_right
 SEC
 SBC right_x_dec
 STA x_right

 LDA y_right
 SEC
 SBC right_y_dec
 STA y_right

 DEC right_frame
 BMI right_burned_out
      

draw_right_missile
 LDY #$0F
 LDX y_right
loop63
 LDA photon,Y
 AND $D20A
 STA play3,X
 INX
 DEY
 BPL loop63

 LDA x_right
 STA $D003
 STA p3x

right_burned_out
 RTS
    
right_frame = $F9
right_x_dec = $FA
right_y_dec = $FB
x_right = $FC
y_right = $FD
released = $FE
              
photon
 DB 24,60,126,126,126,255,255,255
 DB 255,255,255,126,126,126,60,24


move_missiles
 JSR test_left_missile
 JSR test_right_missile
 JSR move_left_missile
 JMP move_right_missile


check_missile_hit
 SEC
 SBC #$28
 LSR A
 LSR A
 LSR A 
 STA y_missile

 LDA x_missile
 SEC
 SBC #$30
 LSR A
 LSR A

 SEC
 SBC ship_x,X
 CLC
 ADC #$01
 BMI no_hit
                
 PHA
 LDA ship_stage,X
 TAY
 PLA
 CMP x_ranges,Y
 BPL no_hit

 LDA y_missile
 SEC
 SBC ship_y,X
 CLC
 ADC #$01
 BMI no_hit

 CMP y_ranges,Y
 BPL no_hit

a_hit
 LDA #$08
 STA hitz
 SEC
 RTS

no_hit
 CLC
 RTS

x_ranges
 DB $03,$04,$06

y_ranges
 DB $01,$02,$02

x_missile = $E0
y_missile = $E1


test_left_missile
 LDA left_frame
 BEQ left1test
 RTS

left1test
 LDX #$00

left1loop
 LDA ship_stage,X
 CMP #$03
 BMI left2test

left1next
 INX
 CPX number_of_ships
 BNE left1loop
 RTS    

left2test
 LDA x_left
 STA x_missile
 LDA y_left
 JSR check_missile_hit
 BCC left_miss

left_hit
 JSR up_shields
 JSR up_score
 LDA ship_stage,X
 ORA #$10
 STA ship_stage,X
 LDA #$08
 STA ship_counter,X
 RTS

left_miss
 JMP left1next

test_right_missile
 LDA right_frame
 BEQ right1test
 RTS

right1test  
 LDX #$00

r1loop
 LDA ship_stage,X
 CMP #$03
 BMI right2test

r1next
 INX
 CPX number_of_ships
 BNE r1loop
 RTS

right2test
 LDA x_right
 STA x_missile
 LDA y_right
 JSR check_missile_hit
 BCC right_miss

right_hit
 JSR up_shields
 JSR up_score
 LDA ship_stage,X
 ORA #$10
 STA ship_stage,X
 LDA #$08
 STA ship_counter,X
 RTS   

right_miss
 JMP r1next

chr_offset = $FF


fade_em
 LDX #$00

fade_loop
 LDA $D20A
 AND #$03
 TAY
 LDA character_set+$0020,X
 AND mask,Y
 STA character_set+$00D8,X
 INX
 CPX #$B6
 BNE fade_loop

 RTS

mask
 DB $00,$0F,$F0,$FF


number_of_ships = $E2
alive_ships = $E3


up_shields

 LDA shields
 CMP #$11
 BEQ done1

 LDA number_of_ships
 SEC
 SBC #$01
 AND #$F8
 LSR A
 LSR A
 LSR A
 CMP #$04
 BMI found_boost
 LDA #$03

found_boost
 TAY
 LDA lo_boosts,Y
 STA lo_add
 LDA hi_boosts,Y
 STA hi_add

 LDA shields+$02
 CLC
 ADC lo_add
 CMP #$1A
 BMI skip80

 INC hi_add
 SEC
 SBC #$0A

skip80
 STA shields+$02
 
 LDA shields+$01
 CLC
 ADC hi_add
 CMP #$1A
 BMI skip81
           
 LDA #$11
 STA shields
 LDA #$10
 STA shields+$01
 STA shields+$02

done1
 RTS

skip81
 STA shields+$01
 RTS

lo_add = $E4
hi_add = $E5

lo_boosts
 DB $02,$05,$00,$00

hi_boosts
 DB $00,$00,$01,$02


up_score
 LDY #$04
 LDA score,Y
 CLC
 ADC #$05
 CMP #$1A
 BMI done3

loop83
 SEC
 SBC #$0A
 STA score,Y

 DEY
 BMI overflow

 LDA score,Y
 CLC
 ADC #$01
 CMP #$1A
 BPL loop83

done3
 STA score,Y
 RTS

overflow
 RTS


down_shields
 LDY #$02
 LDA shields,Y
 SEC
 SBC #$04
 CMP #$10
 BPL done2

loop82
 CLC
 ADC #$0A
 STA shields,Y

 DEY 
 BMI no_shields

 LDA shields,Y
 SEC
 SBC #$01
 CMP #$10
 BMI loop82

done2
 STA shields,Y
 RTS

no_shields
 LDA #$10
 STA shields
 STA shields+$01
 STA shields+$02

game_over
 JSR time_to_die
 JSR off_sound
 LDX #$03
 LDA #$00
off_saves
 STA freqs,X
 STA dists,X
 DEX
 BPL off_saves
 LDX #$00
 TXS
 JMP main_program
 
 include d:prog2.src


ship_x

ship_y = ship_x+$0100

ship_direction = ship_y+$0100

ship_stage = ship_direction+$0100

ship_counter = ship_stage+$0100


 end begining 
