effects
 LDX shakin
 BMI no_shakin

 LDA shake_lines,X
 STA display_list
 LDA shake_freq,X
 STA $D200
 STA freq1
 LDA shake_dist,X
 STA $D201
 STA dist1
 DEC shakin

no_shakin
 LDA wings
 STA $D202
 STA freq2
 STA $D203
 STA dist2

 LDX tons
 BMI no_tons

 LDA pho_freq,X
 STA $D204
 STA freq3
 LDA pho_dist,X
 STA $D205
 STA dist3
 DEC tons

no_tons
 LDX hitz
 BMI no_hitz

 LDA hit_freq,X
 STA $D206
 STA freq4
 LDA hit_dist,X
 STA $D207
 STA dist4
 DEC hitz

no_hitz
 RTS


shakin = $E6
wings = $E7
tons = $E8
hitz = $E9


shake_lines
 DB $30,$20,$40,$10,$50

shake_freq
 DB $00,$80,$B0,$D0,$E0

shake_dist
 DB $00,$82,$84,$86,$88

pho_freq
 DB 0,160,140,120,48

pho_dist
 DB 0,$8A,$8C,$8E,$C6

hit_freq
 DB 0,240,230,220,210,200,190,180
 DB 170

hit_dist
 DB $00,$88,$8A,$8C,$8C,$8E,$8E,$8E
 DB $8C


freqs

freq1
 DB 0
freq2
 DB 0
freq3
 DB 0
freq4
 DB 0


dists

dist1
 DB 0
dist2
 DB 0
dist3
 DB 0
dist4
 DB 0


kb_int
 LDA $D209
 STA the_key
 PLA
 RTI

the_key = $EA


wait_for_start

 JSR set_hi_up
 JSR try_hi_score

key_reset 
 LDA #$FF
 STA the_key

press_wait 
 LDA $D01F
 CMP #$06 ; start ?
 BNE try_a_key
 LDX #$00
 BEQ go_level

try_a_key
 LDA the_key
 CMP #$2D ; toggle ?
 BNE no_toggle

 LDA toggle
 EOR #$FF
 STA toggle
 JMP key_reset

no_toggle
 CMP #$12 ; continue ?
 BEQ go_there

 LDX #$05
key_loop
 CMP lvl_keys,X ; level ?
 BEQ go_level
 DEX
 BNE key_loop
 JMP press_wait

go_level
 LDA lvl_number,X
 STA number_of_ships
 LDA lvl_lo,X
 STA wave+$01
 LDA lvl_hi,X
 STA wave

go_there
 LDA #$FF
 STA the_key
 JMP off_sound

toggle = $EB
pointer = $EC

lvl_keys
 DB $FF,$1F,$1E,$1A,$18,$1D

lvl_number
 DB 1,10,20,30,40,50

lvl_lo
 DB $11,$10,$10,$10,$10,$10

lvl_hi
 DB $10,$11,$12,$13,$14,$15 


play_keys
 LDA the_key
 CMP #$1C
 BNE no_esc

 JMP game_over

no_esc
 CMP #$2D
 BNE dont_toggle

 LDA toggle
 EOR #$FF
 STA toggle
 LDA #$FF
 STA the_key
 RTS

dont_toggle
 CMP #$21
 BNE no_space
    
 JSR off_sound
 JSR vb_wait
 LDA #LOW dl_pause
 STA $D402
 LDA #HIGH dl_pause
 STA $D403
 LDA #$40
 STA $D01B
 LDA #$E0
 STA $D01A
 LDX #$02
 LDA #$00
pm_erase
 STA $D001,X
 DEX
 BPL pm_erase
 LDA #$FF
 STA the_key   

 LDA #$05
 STA $D208
 LDA #$A6
 STA $D201
 STA $D205
 LDA #$80
 STA $D200
 LDA #$40
 STA $D204

pause_loop 
 JSR wiggle
 LDA the_key
 CMP #$21
 BNE pause_loop

 JSR vb_wait
 LDA #LOW display_list
 STA $D402
 LDA #HIGH display_list
 STA $D403
 LDA #$01
 STA $D01B
 LDA #$00
 STA $D01A
 LDX #$02
pm_draw
 LDA p1x,X
 STA $D001,X
 DEX
 BPL pm_draw
 LDA #$FF
 STA the_key
 LDA #$00
 STA $D208
 JSR on_sound

no_space
 RTS
            
p1x = $D0
p2x = $D1
p3x = $D2


off_sound
 LDX #$07
 LDA #$00
off_loop
 STA $D200,X
 DEX
 BPL off_loop
 RTS


on_sound
 LDX #$03
 LDY #$06
on_loop
 LDA freqs,X
 STA $D200,Y
 LDA dists,X
 STA $D201,Y
 DEX        
 DEY
 DEY
 BPL on_loop
 RTS


clr_screen
 LDA #LOW screen_memory
 STA $DE
 LDA #HIGH screen_memory
 STA $DF
 LDX #$04
 JMP rub_memory


clr_pause_display
 LDA #LOW pause_memory
 STA $DE
 LDA #HIGH pause_memory
 STA $DF
 LDX #$08
 JMP rub_memory


dl_pause
 DB $70,$70,$70
 DB $4F
 DW pause_memory
 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,$0F,$0F
 DB $0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
 DB $0F,$0F,$0F,$0F,$0F,$0F,$0F,$0F
 DB $0F,$0F
 DB $41
 DW dl_pause


wiggle
 JSR vb_wait
 LDX #$0D

wig1
 LDA pos_lo,X
 STA $ED
 LDA pos_hi,X
 STA $EE
 LDA chars_lo,X
 STA wig2+$01
 LDA chars_hi,X
 STA wig2+$02

 TXA
 PHA

 LDX #$00
 LDY #$00

wig2
 LDA $FFFF,X
 STA ($ED),Y
 LDA $ED
 CLC
 ADC #$28
 STA $ED
 BCC skip90
 INC $EE

skip90
 INX
 CPX #$18
 BNE wig2

 PLA
 TAX
 DEX
 BPL wig1

 LDX #$0D
 
wig3
 LDA updates-$01,X
 STA updates,X
 DEX
 BNE wig3

 LDX pointer
 CPX #$30
 BNE cont_sequence
 LDX #$00
 STX pointer

cont_sequence
 LDA sequence,X
 STA updates
 INC pointer

 LDX #$0D

wig4
 LDA updates,X
 BMI going_down

going_up
 LDA pos_lo,X
 CLC
 ADC updates,X
 STA pos_lo,X
 BCC lets_resume
 INC pos_hi,X
 BNE lets_resume

going_down
 LDA pos_lo,X
 CLC
 ADC updates,X
 STA pos_lo,X
 BCS lets_resume
 DEC pos_hi,X

lets_resume
 DEX
 BPL wig4

 RTS


updates
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0

pos_lo
 DB $3D,$3E,$3F,$40,$41,$42,$43,$44
 DB $45,$46,$47,$48,$49,$4A

pos_hi
 DB $6A,$6A,$6A,$6A,$6A,$6A,$6A,$6A
 DB $6A,$6A,$6A,$6A,$6A,$6A

u1 = $28
u2 = $50
u3 = $78
d1 = $D8
d2 = $B0
d3 = $88

sequence
 DB 0,0,0
 DB u1,u1,u1,u1,d1,d1,d1,d1
 DB 0,0
 DB u1,u1,u1,u2,u2,u3
 DB d3,d2,d2,d1,d1,d1
 DB 0,0,0
 DB u1,u2,u3,d3,d2,d1
 DB 0,0
 DB u1,u1,u1,u1,u1,u1,d3,d2,d1
 DB 0,0,0                             
chars_lo
 DB LOW char1,LOW char2,LOW char3
 DB LOW char4,LOW char5,LOW char6
 DB LOW char7,LOW char8,LOW char9
 DB LOW char10,LOW char11
 DB LOW char12,LOW char13
 DB LOW char14

chars_hi
 DB HIGH char1,HIGH char2,HIGH char3
 DB HIGH char4,HIGH char5,HIGH char6
 DB HIGH char7,HIGH char8,HIGH char9
 DB HIGH char10,HIGH char11
 DB HIGH char12,HIGH char13
 DB HIGH char14

char1
 DB 0,0,0,0
 DB 136,153,170,187,204,221,238,255
 DB 255,238,221,204,187,170,153,0
 DB 0,0,0,0

char2
 DB 0,0,0,0
 DB 136,153,170,187,0,13,14,0
 DB 15,14,13,12,187,170,153,0
 DB 0,0,0,0

char3
 DB 0,0,0,0
 DB 136,153,170,187,204,13,14,255
 DB 255,238,221,204,187,170,153,0
 DB 0,0,0,0

char4
 DB 0,0,0,0
 DB 136,153,170,187,192,13,14,0
 DB 15,14,13,12,187,170,153,0
 DB 0,0,0,0

char5
 DB 0,0,0,0
 DB 136,153,170,187,204,13,14,15
 DB 15,14,13,12,187,170,153,0
 DB 0,0,0,0

char6
 DB 0,0,0,0
 DB 136,153,170,187,12,13,14,15
 DB 15,14,13,0,187,170,153,0
 DB 0,0,0,0

char7
 DB 0,0,0,0
 DB 136,153,170,187,12,13,14,15
 DB 15,14,13,12,187,170,153,0
 DB 0,0,0,0

char8
 DB 0,0,0,0
 DB 136,153,170,187,0,13,14,0
 DB 255,238,221,0,187,170,153,0
 DB 0,0,0,0

char9
 DB 0,0,0,0
 DB 136,153,170,187,12,221,238,15
 DB 15,14,13,12,187,170,153,0
 DB 0,0,0,0

char10
 DB 0,0,0,0
 DB 136,153,170,187,0,13,14,0
 DB 15,14,13,0,187,170,153,0
 DB 0,0,0,0

char11
 DB 0,0,0,0
 DB 136,153,170,187,12,221,238,255
 DB 255,238,221,12,187,170,153,0
 DB 0,0,0,0

char12
 DB 0,0,0,0
 DB 136,153,170,187,0,13,14,15
 DB 15,14,13,0,187,170,153,0
 DB 0,0,0,0

char13
 DB 0,0,0,0
 DB 136,153,170,187,204,13,14,15
 DB 15,14,13,204,187,170,153,0
 DB 0,0,0,0

char14
 DB 0,0,0,0
 DB 136,153,170,187,204,221,238,255
 DB 255,238,221,204,187,170,153,0
 DB 0,0,0,0
                      

set_up_letters
 LDA #LOW title_memory
 STA $DE
 LDA #HIGH title_memory
 STA $DF
 LDX #$0B
 JSR rub_memory

 LDX #$05
here
 LDA lo_letters,X
 STA $DC
 LDA hi_letters,X
 STA $DD
 LDA lo_goes,X
 STA $DE
 LDA hi_goes,X
 STA $DF
      
 TXA
 PHA

 CMP #$04
 BMI skip96

 LDX #$0F
 BPL loop99

skip96
 LDX #$27

loop99
 LDY #$05

get1
 LDA ($DC),Y
 STA ($DE),Y
 DEY
 BPL get1

 LDA $DC
 CLC 
 ADC #$06
 STA $DC
 BCC skip97
 INC $DD

skip97
 LDA $DE
 CLC 
 ADC #$28
 STA $DE
 BCC skip95
 INC $DF

skip95
 DEX
 BPL loop99

 PLA
 TAX
 DEX
 BPL here

 RTS

lo_letters
 DB LOW s_letter
 DB LOW t_letter
 DB LOW a_letter
 DB LOW r_letter
 DB LOW rid_letters
 DB LOW er_letters

hi_letters
 DB HIGH s_letter
 DB HIGH t_letter
 DB HIGH a_letter
 DB HIGH r_letter
 DB HIGH rid_letters
 DB HIGH er_letters
 
lo_goes
 DB $58,$0E,$14,$6A,$17,$1D

hi_goes
 DB $50,$50,$50,$50,$57,$57


set_hi_up
 JSR off_sound
 JSR vb_wait
 LDA #LOW hi_dlist
 STA $D402
 LDA #HIGH hi_dlist
 STA $D403
 LDA #LOW hi_dli
 STA $0200
 LDA #HIGH hi_dli
 STA $0201
 LDA #$D0
 STA $D018
 LDA #$8C
 STA $D019
 LDA #$05
 STA $D208
 LDA #$AA
 STA $D201
 STA $D203
 LDA #$A6
 STA $D205
 STA $D207
 LDA #$00
 STA decay
 LDA #$27
 STA note
 LDA #$01
 STA left_to_right
 JSR pm_init
 LDA #$00
 STA left_to_right
 RTS

hi_dlist
 DB $70,$70,$F0
 DB $4F
 DW title_memory
 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,$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,$0F,$0F
 DB $0F,$0F,$0F,$0F
 DB $70
 DB $42
 DW scores_memory
 DB $70,$06,$70,$06,$70,$06
 DB $70,$06,$70,$06,$70
 DB $02
 DB $41
 DW hi_dlist


scores_memory
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 DB 40,41,39,40,0
 DB 51,35,47,50,37,51
 DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0

hi_line
 DB 0,0,0
 DB 97,110,97,108,111,103,0,0,0
 DB 16,17,16,16,16
 DB 0,0,0
hi_start = hi_line+12

 DB 0,0,0
 DB 97,110,97,108,111,103,0,0,0
 DB 16,16,21,16,16
 DB 0,0,0

 DB 0,0,0
 DB 97,110,97,108,111,103,0,0,0
 DB 16,16,18,21,16
 DB 0,0,0

 DB 0,0,0
 DB 97,110,97,108,111,103,0,0,0
 DB 16,16,17,16,16
 DB 0,0,0

 DB 0,0,0
 DB 97,110,97,108,111,103,0,0,0
 DB 16,16,16,21,16
 DB 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


hi_dli
 PHA
 TXA
 PHA

 LDX #$E0
 LDA #$41
 STA $D40A
 STX $D01A
 STA $D01B

 LDX #$3C

dli8loop
 LDA starfield,X
 STA $D40A
 STA $D004
 STX $D012
 DEC starfield,X
 TXA
 LSR A
 BCC dli98skip
 DEC starfield,X
dli98skip
 DEX
 BNE dli8loop

 STX $D40A
 STX $D01A
 LDA #$01
 STA $D01B

 LDX #$6D

dli9loop
 TXA
 ASL A
 STA $D40A
 STA $D017
 CLC
 ADC #$40
 STA $D016
 LDA starfield+$3C,X
 STA $D004
 STX $D012
 DEC starfield+$3C,X
 TXA
 LSR A
 BCC dli99skip 
 DEC starfield+$3C,X
dli99skip
 DEX
 BNE dli9loop
    
 STX $D004

 DEC decay
 BPL decayin

 LDX note
 CPX #$28
 BNE ok_note
 LDX #$00

ok_note
 LDA pitch,X
 STA $D200
 CLC
 ADC #$01
 STA $D202
 LSR A
 STA $D204
 CLC 
 ADC #$01
 STA $D206
 INX
 STX note
 LDA #$07
 STA decay

decayin
 PLA
 TAX
 PLA
 RTI
     
decay = $DB
note = $DC

pitch
 DB 72,110,74,111,72,110,74,111
 DB 64,126,66,127,64,126,66,127
 DB 72,110,74,111,72,110,74,111
 DB 64,126,66,127,64,126,66,127
 DB 64,74,84,94,62,72,82,92


rub_memory
 LDY #$00
 TYA
rub_loop
 STA ($DE),Y
 INY
 BNE rub_loop
 INC $DF
 DEX
 BNE rub_loop
 RTS


pm_init
 LDA #LOW play1
 STA $DE
 LDA #HIGH play1
 STA $DF
 LDX #$03
 JSR rub_memory

 LDX #$00
loop5
 LDA $D20A
 LDY left_to_right
 BNE is_skip
 CMP #$2F
 BCC loop5
 CMP #$CF
 BCS loop5  
is_skip
 STA starfield,X
 LDA #$01
 STA pmbase+$0300,X
 INX
 BNE loop5
 RTS   

left_to_right
 DB $00


time_to_die
 LDA #$C8
 STA $D3

loop88
 STA $D40A
 LDA $D20A
 AND #$70
 STA display_list
 LDY #$40
exploding 
 LDX #$03
blowing
 LDA $D20A
 STA $D004,X
 STA $D012,X
 DEX
 BPL blowing
 STA $D00C
 STA $D011
 AND #$07
 TAX
 LDA $D20A
 AND #$EF
 STA $D200,X
 DEY
 BNE exploding
 DEC $D3
 BNE loop88

 LDA #$30
 STA display_list
 LDA #$00
 STA $D00C
 RTS


try_hi_score
 LDA $D4
 BEQ first_game

 LDA #LOW hi_start
 STA $D5
 LDA #HIGH hi_start
 STA $D6
 
 LDX #$00

hs1loop
 LDY #$00

hs2loop
 LDA ($D5),Y
 CMP score,Y
 BCC a_new_hi
 BNE hs1skip

 INY
 CPY #$05
 BNE hs2loop

hs1skip
 LDA $D5
 CLC
 ADC #$14
 STA $D5
 BCC hs2skip
 INC $D6

hs2skip
 INX
 CPX #$05
 BNE hs1loop
 RTS

a_new_hi
 JMP add_high_score

first_game
 INC $D4
 RTS


add_high_score
 STX $D9
 LDA #LOW [hi_line+60]
 STA $D5
 LDA #HIGH [hi_line+60]
 STA $D6

 LDA #$04
 SEC
 SBC $D9
 TAX

ah1loop
 BEQ ah1skip
 LDA $D5
 CLC
 ADC #$14
 STA $D7
 LDA $D6
 ADC #$00
 STA $D8

 LDY #$13

ah2loop
 LDA ($D5),Y
 STA ($D7),Y
 DEY
 BPL ah2loop

 LDA $D5
 SEC
 SBC #$14
 STA $D5
 BCS ah2skip
 DEC $D6

ah2skip
 DEX
 BPL ah1loop

ah1skip
 LDA #LOW [hi_line+$03]
 STA $D5
 LDA #HIGH [hi_line+$04]
 STA $D6
 LDA $D9
 ASL A
 ASL A
 PHA
 CLC
 ADC $D5
 STA $D5
 BCC ah3skip
 INC $D6

ah3skip
 PLA
 ASL A
 ASL A
 CLC
 ADC $D5
 STA $D5
 BCC ah4skip
 INC $D6

ah4skip
 LDY #$07

ah3loop
 LDA name_buffer,Y
 TAX
 LDA name_characters,X
 STA ($D5),Y
 DEY
 BPL ah3loop

 LDA $D5
 CLC
 ADC #$09
 STA $D7
 LDA $D6
 ADC #$00
 STA $D8

 LDY #$04

ah4loop
 LDA score,Y
 STA ($D7),Y
 DEY
 BPL ah4loop

 LDY #$00

en1loop
 JSR delay

 LDA #$FF
 STA ($D5),Y

 JSR delay
 
 LDA name_buffer,Y
 TAX
 LDA name_characters,X
 STA ($D5),Y

 LDA $D010
 BEQ en5skip

 LDA $D300
 AND #$0F
 
 CMP #$07
 BNE en1skip

 INY
 CPY #$08
 BMI en1loop
 LDY #$00
 BPL en1loop

en1skip
 CMP #$0B
 BNE en2skip

 DEY
 BPL en1loop
 LDY #$07
 BPL en1loop

en2skip
 CMP #$0D
 BNE en3skip

 LDA name_buffer,Y
 SEC
 SBC #$01
 BPL en4skip
 LDA #$24
 BPL en4skip

en3skip
 CMP #$0E
 BNE en1loop

 LDA name_buffer,Y
 CLC
 ADC #$01
 CMP #$25
 BMI en4skip
 LDA #$00

en4skip
 STA name_buffer,Y
 JMP en1loop

en5skip
 LDA $D010
 BEQ en5skip
 RTS


delay
 JSR vb_wait
 JSR vb_waste
 JSR vb_wait
 JSR vb_waste
 JSR vb_wait
 JMP vb_waste


name_characters
 DB 0

a_char SET 97
 ECHO 26
 DB a_char
a_char SET a_char+$01
 	ENDM

a_char SET 80
 ECHO 10
 DB a_char
a_char SET a_char+$01
 	ENDM


name_buffer
 DB 0,0,0,0,0,0,0,0


busted
 LDA #$00
 STA $D400
 STA $D20E
 STA $D40E
 STA $D208
 LDA #$03
 STA $D20F

 LDA #LOW blister
 STA $D402
 LDA #HIGH blister
 STA $D403
 LDA #HIGH pmbase
 STA $D409
 LDA #$0C
 STA $D017
 LDA #$00
 STA $D018

 LDA #$23
 STA $D400

 LDA #$0E
 STA $D201
 STA $D203
 LDX #$01
 STX $D200
 INX
 STX $D202

bl1loop
 LDX #$7F
bl2loop
 LDA $D20A
 STA pmbase,X
 DEX
 BPL bl2loop
 BMI bl1loop


blister

 ECHO 14
 DB $42
 DW bline
 DB $02
 	ENDM

 DB $42
 DW bline

 DB $41
 DW blister
       

bline
 DB 0,1,2,3,4,5,6,7
 DB 8,9,10,11,12,13,14,15
 DB 0,1,2,3,4,5,6,7
 DB 8,9,10,11,12,13,14,15
 DB 0,1,2,3,4,5,6,7
 DB 8,9,10,11,12,13,14,15
 DB 8,9,10,11,12,13,14,15
 DB 0,1,2,3,4,5,6,7
 DB 8,9,10,11,12,13,14,15
 DB 0,1,2,3,4,5,6,7
 DB 8,9,10,11,12,13,14,15 
 DB 0,1,2,3,4,5,6,7

 include d:letters.src
