;	@com.wudsn.ide.asm.mainsourcefile=PACMAN.ASM

;
VBSUBS LDA ATSEQU
 BNE VBATTR
 JMP CKOPTS
VBATTR CMP #1
 BNE CKATS2
 LDX #0
 TXA
CLRAOP STA OPTSCN+$50,X
 STA OPTSCN+$150,X
 DEX
 BNE CLRAOP
 LDX #$0F
BPMLP1 LDA BPMSG1,X
 STA OPTSCN+$11A,X
 DEX
 BPL BPMLP1
 LDX #$11
BPMLP2 LDA BPMSG2,X
 STA OPTSCN+$141,X
 DEX
 BPL BPMLP2
 LDA #$FF
 STA ATTIMR
 INC ATSEQU
 RTS
CKATS2 CMP #2
 BNE CKATS3
 LDA ATTIMR
 BEQ CAS2ER
 JMP DECATM
CAS2ER LDA #0
 LDX #$11
C2ERLP STA OPTSCN+$119,X
 STA OPTSCN+$141,X
 DEX
 BPL C2ERLP
 LDA #$7F
 STA ATTIMR
 INC ATSEQU
 RTS
CKATS3 CMP #3
 BNE CKATS4
 LDA ATTIMR
 BEQ CS3SCN
 JMP DECATM
CS3SCN LDX #0
 TXA
CLRASC STA PATSCN,X
 STA PATSCN+$100,X
 INX
 BNE CLRASC
 INC ATSEQU
 RTS
CKATS4 CMP #4
 BNE CKATS5
 LDA #<ADLIST
 STA SDLSTL
 STA DLISTL
 LDA #>ADLIST
 STA SDLSTH
 STA DLISTH
 LDA #<ADLIV
 STA VDSLST
 LDA #>ADLIV
 STA VDSLST+1
 LDX #7
SETACL LDA ACOLRS,X
 STA PCOLR0,X
 DEX
 BPL SETACL
 LDA #$7F
 STA ATTIMR
 INC ATSEQU
 RTS
CKATS5 CMP #5
 BNE CKATS6
 LDA ATTIMR
 BEQ C4NICK
 JMP DECATM
C4NICK LDX #19
ASTART LDA CHNKTX,X
 STA PATSCN+$28,X
 DEX
 BPL ASTART
 INC ATSEQU
 RTS
CKATS6 CMP #6
 BNE CKATSQ
 LDX #0
 TXA
 JSR INITPL
 LDX #$7F
INIAP0 STA $80,X
 DEX
 BPL INIAP0
 JSR ATINIT
 INC ATSEQU
 LDA #ADELAY
 STA ATTIMR
 RTS
CKATSQ LDA ATSEQU
 CMP #$16
 BCC CKATMR
 JMP SEQATM
CKATMR LDA ATTIMR
 BNE DECATM
 JSR SEQATR
 INC ATSEQU
 LDA #ADELAY
 STA ATTIMR
 RTS
DECATM DEC ATTIMR
 RTS
;
CKOPTS LDY OPTION
 BNE TSTOPT
 JMP VBGAME
TSTOPT CPY #1
 BNE VBOPT2
;
; BUILD TITLE SCREEN
;
 JSR OPTTTL
 LDX #0
 TXA
OPSCCL STA OPTSCN,X
 STA OPTSCN+$100,X
 STA PACMAZ+$300,X
 DEX
 BNE OPSCCL
 STA COLOR1
 LDX #$0D
PLOGOL LDA OALOGO,X
 STA OPTSCN+$157,X
 DEX
 BPL PLOGOL
 INX
PTTLLP TXA
 CLC
 ADC #$C1
 STA OPTSCN+$18,X
 TXA
 ADC #$CD
 STA OPTSCN+$2C,X
 INX
 CPX #$0C
 BNE PTTLLP
VBOP1I INC OPTION
VBOP1X RTS
;
; TIME OUT TITLE SCREEN
;
VBOPT2 CPY #2
 BNE VBOPT3
 LDA ATCLOK
 CMP RTCLOK+2
 BNE VBOP1X
 LDA #7
 STA ATCLOK
 BNE VBOP1I
;
; BUILD OPTION SCREEN
;
VBOPT3 LDA RTCLOK+2
 BNE OPTSCR
 DEC ATCLOK
 BNE OPTSCR
 LDA #1
 STA ATSEQU
OPTSCR LDA #0
 LDX #$11
OPTCL2 STA OPTSCN+$119,X
 STA OPTSCN+$141,X
 DEX
 BPL OPTCL2
 LDX NUMPLY
 INX
 TXA
 ORA #$50
 STA OPTSCN+$54
 LDA NUMPLY
 BNE PMOPT3
 CPY #3
 BNE SAME1P
 JSR SET1PL
SAME1P LDA #$12
 BNE PMOPT4
PMOPT3 CPY #3
 BNE SAME2P
 JSR SET2PL
SAME2P LDA #$11
PMOPT4 STA OPTSCN+$90
 LDX #$0A
PMO4LP LDA OPLGAM,X
 STA OPTSCN+$92,X
 ORA #$40
 STA OPTSCN+$56,X
 DEX
 BPL PMO4LP
 JSR OPTTTL
 LDX #$0F
PMO5LP LDA OPRSEL,X
 STA OPTSCN+$7A,X
 LDA OPROP1,X
 STA OPTSCN+$F2,X
 LDA OPRST1,X
 STA OPTSCN+$156,X
 DEX
 BPL PMO5LP
 LDX DIFOPT
 LDA OPTFRT,X
 STA OPTSCN+$C6
 CLC
 ADC #1
 STA OPTSCN+$C7
 LDX #$10
PMO7LP LDA OPROP2,X
 STA OPTSCN+$105,X
 DEX
 BPL PMO7LP
 LDX #8
PMO9LP LDA OPRST2,X
 STA OPTSCN+$16D,X
 DEX
 BPL PMO9LP
 RTS
;
VBGAME LDA ATTIMR
 BEQ VBGAM1
 LDA RTCLOK+2
 AND #7
 BNE VBGAM1
 DEC ATTIMR
VBGAM1 LDA PAUSEF
 BEQ VBGAM2
 JMP CLRAUD
VBGAM2 JSR FLSHXU
 LDA RSTRTF
 BEQ TSTGMO
 CMP #2
 BEQ VTUNES
TSTGMO LDA GMOVRF
 BEQ TSTRRK
 LDA NUMPLY
 BEQ VFLS1O
 JSR FLS2ON
VFLS1O JMP FLS1ON
TSTRRK LDA RRFLAG
 BEQ TSTVRD
 JMP RERACK
TSTVRD LDA READYF
 BNE VREADY
 LDA INTROF
 BNE VINTRO
VSQUIT RTS
VTUNES INC INTROF
 LDA #0
 STA ATRACT
 STA RSTRTF
 JMP READY1
VINTRO LDA RTCLOK+2
 AND #3
 BNE VSQUIT
VSONGS LDX NOTCNT
 CPX #$40
 BEQ VSTART
 LDA HINOT1,X
 STA AUDF1
 LDA LONOT1,X
 STA AUDF2
 LDA #$A8
 STA AUDC1
 LDA #$A4
 STA AUDC2
 INC NOTCNT
 CPX #$28
 BNE VSQUIT
 JMP READY2
VSTART INC READYF
VSTRT1 INC M1STAT
 JSR READY3
VREADY LDA RESETF
 BEQ VCONTN
 CMP #1
 BNE RESET2
 JSR VRESET
 LDA SWAPPF
 BNE VSQUIT
VRSTGM LDA GMOVRF
 BNE VSQUIT
CRESET INC RESETF
 LDA #$40
 STA RESETT
CRESTX RTS
;
RESET2 LDA RESETT
 BEQ VSTRT2
 DEC RESETT
VGONE1 RTS
VSTRT2 LDA #0
 STA RESETF
 BEQ VSTRT1
VCONTN JSR BLINKR
 LDA VFIZST
 BEQ VPLAYR
 JSR VFIZZL
 JMP FIZZIE
VPLAYR JSR EYONLY
 LDA VFREEZ
 BEQ VFRUIT
 JMP VGULPR
VFRUIT JSR FRUITY
 JSR DOTTST
 LDA RRFLAG
 BNE VPLYUD
VCOLLS JSR COLCHK
 LDA VFREEZ
 BNE VGONE2
 LDA VFIZST
 BEQ VPLYUD
VGONE2 RTS
VPLYUD JSR FLITEC
 LDA VFREEZ
 BNE VGONE2
 JSR VEATER
 JSR GOBBLE
 JSR SKIRTS
;
; SPEED OF PACMAN
;
SPDPAC LDA PACADV
 BEQ PACREG
 LDA #0
 BEQ SPDPC1
PACREG LDA PACDLY
 BNE SPDPC1
 LDX #4
 JSR SPDSEQ
SPDPC1 STA PACCNT
 LDA #0
 STA PACADV
 JSR PMSTIK
 LDA #0
 STA PACDLY
 JSR MUNCHY
 LDA RTCLOK+2
 AND #$03
 BNE UDMONS
 JSR STRTMN
UDMONS JSR CHSSEQ
 LDA PACADV
 BNE VSUBSX
 LDX #3
;
; SPEED HANDLER FOR MONSTERS
;
; X REG = 0-3 FOR MONSTERS 1-4
;
SPDMON LDA M1STAT,X
 AND #$7F
 BEQ NXMSPD
 LDA RTCLOK+2
 AND #7
 BNE SPMOK1
 LDA M1TIMR,X
 BEQ SPMOK1
 DEC M1TIMR,X
SPMOK1 LDA M1STAT,X
 AND #$3F
 BEQ NXMSPD
SPMOK2 LDA M1STAT,X
 ASL
 BMI NXMSPD
SPMOK3 LDA M1VPOS,X
 CMP #$74
 BNE SPDBLU
 LDA M1HPOS,X
 CMP #$A7
 BCS SPDSLO
 CMP #$52
 BCC SPDSLO
SPDBLU LDA M1STAT,X
 BPL SPDREG
SPDSLO JSR SPDSEQ
 CMP #0
 BNE NXMSPD
 LDY M1DELY,X
 CPY #1
 BNE MONDLA
 STA M1DELY,X
 BEQ SPDUPD
MONDLA LDA #1
 STA M1DELY,X
 BNE NXMSPD
SPDREG JSR SPDSEQ
 CMP #0
 BNE NXMSPD
SPDUPD JSR MONSTR
NXMSPD DEX
 BPL SPDMON
VSUBSX RTS
;
; ***** PACMAN ATTRACT MODE *****
;
ATINIT LDA #$11
 STA GPRIOR
 LDA #$E0
 STA PMHPOS
 LDA #$EC
 STA M1HPOS
 LDA #$F5
 STA M2HPOS
 LDA #$FE
 STA M3HPOS
 LDA #$07
 STA M4HPOS
 LDA #$8C
 STA PMVPOS
 STA M1VPOS
 STA M2VPOS
 STA M3VPOS
 STA M4VPOS
 LDA #$2A
 STA PACCLR
 RTS
;
;
; SEQUENCINC OF ATTRACT DISPLAY
;
SEQATR LDA ATSEQU
 CMP #7
 BNE ATSEQ8
 LDA #$05
 STA PATSCN+61
 LDA #$06
 STA PATSCN+81
 RTS
;
ATSEQ8 CMP #8
 BNE ATSEQ9
 LDX #0
 LDY #80
 JMP ATSHOW
ATSEQ9 CMP #9
 BNE ATSEQA
 LDX #10
 LDY #90
 JMP ATSHOW
ATSEQA CMP #$0A
 BNE ATSEQB
 LDA #$45
 STA PATSCN+101
 LDA #$46
 STA PATSCN+121
 RTS
ATSEQB CMP #$0B
 BNE ATSEQC
 LDX #20
 LDY #120
 JMP ATSHOW
ATSEQC CMP #$0C
 BNE ATSEQD
 LDX #30
 LDY #130
 JMP ATSHOW
ATSEQD CMP #$0D
 BNE ATSEQE
 LDA #$85
 STA PATSCN+141
 LDA #$86
 STA PATSCN+161
 RTS
ATSEQE CMP #$0E
 BNE ATSEQF
 LDX #40
 LDY #160
 JMP ATSHOW
ATSEQF CMP #$0F
 BNE ATSQ10
 LDX #50
 LDY #170
 JMP ATSHOW
ATSQ10 CMP #$10
 BNE ATSQ11
 LDA #$C5
 STA PATSCN+181
 LDA #$C6
 STA PATSCN+201
 RTS
ATSQ11 CMP #$11
 BNE ATSQ12
 LDX #60
 LDY #200
 JMP ATSHOW
ATSQ12 CMP #$12
 BNE ATSQ13
 LDX #70
 LDY #210
 JMP ATSHOW
ATSQ13 CMP #$13
 BNE ATSQ14
 LDX #7
ATSQDL LDA AM10PT,X
 STA PATSCN+325,X
 LDA AM50PT,X
 STA PATSCN+365,X
 DEX
 BPL ATSQDL
 RTS
ATSQ14 CMP #$14
 BNE ATSEQX
 LDA #$84
 STA PATSCN+261
 LDX #19
ACOPLP LDA COPMSG,X
 STA PATSCN+400,X
 DEX
 BPL ACOPLP
ATSEQX RTS
;
SEQATM JSR FLS1DT
 LDA ATSEQU
 CMP #$16
 BNE ATSQ17
 LDA RTCLOK+2
 AND #$0F
 BNE ABNKON
 LDA #0
 STA PATSCN+261
 STA PATSCN+365
 BEQ CHASEL
ABNKON CMP #8
 BNE CHASEL
 LDA #$84
 STA PATSCN+261
 STA PATSCN+365
CHASEL JSR SKIRTS
 LDA RTCLOK+2
 AND #1
 BNE CHASL1
 LDA #1
 BNE CHASL2
CHASL1 LDA PMHPOS
 CMP #$48
 BEQ CHACLS
 CMP #$70
 BEQ CHACLS
 CMP #$98
 BEQ CHACLS
 BNE CHSLFT
CHACLS LDX #3
CHSCLP DEC M1HPOS,X
 DEX
 BPL CHSCLP
CHSLFT LDX #3
CHSLLP LDA #4
 JSR MONHND
 DEX
 BPL CHSLLP
 LDA #0
CHASL2 STA PACCNT
 LDA PMHPOS
 CMP #$38
 BEQ STOPLF
 JSR PACLF
 RTS
STOPLF INC ATSEQU
 RTS
ATSQ17 CMP #$17
 BNE ATSQ18
 LDA #$80
 STA M1STAT
 STA M2STAT
 STA M3STAT
 STA M4STAT
 LDA #$84
 STA PCOLR0
 STA PCOLR1
 STA PCOLR2
 STA PCOLR3
 LDA #$14
 STA GPRIOR
 LDA #$FF
 STA GLPCNT
 LDA #0
 STA VFREEZ
 STA PATSCN+261
 STA HITCLR
 INC ATSEQU
 LDA ATSEQU
ATSQ18 CMP #$18
 BNE ATSQ19
 LDA VFREEZ
 BNE DECFRZ
 LDX GLPCNT
 CPX #3
 BEQ ATDONE
 INX
 JSR CHASER
 JSR COLCHK
 RTS
DECFRZ LDA VGLPC2
 CMP #$30
 BNE DCGLPF
 LDX GULPED
 LDA #0
 STA HPOSP0,X
 STA VFREEZ
 LDA #$2A
 STA PACCLR
 SEC
 LDA PMHPOS
 SBC #4
 STA PMHPOS
 RTS
DCGLPF DEC VGLPC2
 RTS
ATDONE LDA #0
 STA ATSEQU
 LDA #7
 STA ATCLOK
ATSQ19 RTS
 ; 
CHASER JSR SKIRTS
 LDA RTCLOK+2
 AND #3
 BNE CHASRP
CHSRML LDA #8
 JSR MONHND
 INX
 CPX #4
 BNE CHSRML
CHASRP LDA RTCLOK+2
 AND #1
 BNE CHASR1
 LDA #1
 BNE CHASR2
CHASR1 LDA #0
CHASR2 STA PACCNT
 JSR PACRT
 RTS

;
ATSHOW LDA #9
 STA TEMLOC
ATSHWL LDA CHARTX,X
 STA PATSCN,Y
 INX
 INY
 DEC TEMLOC
 BPL ATSHWL
 RTS
;
FLS1DT LDA RTCLOK+2
 AND #$0F
 BNE FLS1DO
 LDA #0
 STA PATSCN+365
 RTS
FLS1DO CMP #8
 BNE FLS1DX
 LDA #$84
 STA PATSCN+365
FLS1DX RTS
;
; ATTRACT MODE DLI ROUTINE
;
ADLIV PHA
 TXA
 PHA
 TYA
 PHA
 LDX DLICNT
 BNE N1ADLI
 LDA #>AMCSET
 STA WSYNC
 STA CHBASE
 BNE ADLIXX
N1ADLI CPX #1
 BNE N2ADLI
 LDA #$4A
 EOR COLRSH
 AND DRKMSK
 TAX
 LDA #$D8
 EOR COLRSH
 AND DRKMSK
 STA WSYNC
 STX COLPF1
 STA COLPF2
 BNE ADLIXX
N2ADLI CPX #2
 BNE N3ADLI
 LDA PACCLR
 EOR COLRSH
 AND DRKMSK
 LDX ACOLR1; $3A
 STA WSYNC
 STA COLPF3
 STX COLPF2
 BNE ADLIXX
N3ADLI CPX #3
 BNE N4ADLI
 LDA #$0C
 EOR COLRSH
 AND DRKMSK
 LDX ACOLR2; $44
 STA WSYNC
 STA COLPF0
 STX COLPF1
 BNE ADLIXX
N4ADLI LDA #>PACCHR
 LDX ACOLR4; $DA
 LDY ACOLR3; $2A
 STA WSYNC
 STA CHBASE
 STX COLPF0
 STY COLPF3
ADLIXX INC DLICNT
 PLA
 TAY
 PLA
 TAX
 PLA
 RTI
;
; COLLISION CHECK
;
COLCHK LDX #0
 LDY #1
COLLLP LDA M1STAT,X
 ASL
 BMI NXCOLL
 TYA
 BIT M1PL
 BEQ NXCOLL
 BIT M2PL
 BEQ NXCOLL
 LDA PMVPOS
 CMP M1VPOS,X
SBCMXV BCS PMBLMX
 SEC
 LDA M1VPOS,X
 SBC PMVPOS
 BNE TSTMXC
PMBLMX SBC M1VPOS,X
TSTMXC CMP #5
 BCC STRCOL
NXCOLL TYA
 ASL
 TAY
 INX
 CPX #4
 BNE COLLLP
COLCX1 JMP COLCKX
STRCOL LDA M1STAT,X
 BMI ZAPGST
 JMP PMDEAD
ZAPGST LDA #$42
 STA M1STAT,X
 STX GULPED
 INC GLPCNT
 SEC
 LDA PMHPOS
 SBC #4
 STA HPOSM3
 CLC
 LDY #2
REPOSL ADC #2
 STA HPOSM0,Y
 DEY
 BPL REPOSL
 ADC #2
 STA HPOSP0,X
 LDA #$DC
 STA PACCLR
 STA PCOLR0,X
 LDY ATSEQU
 BNE CKOFFS
 STA COLOR3
CKOFFS LDA PMVPOS
;
 CMP M1VPOS,X
 BEQ NOOFFS
 BCC OFFDWN
 LDA #$FE
 BMI STROFF
OFFDWN LDA #2
STROFF CLC
 ADC PMVPOS
;
NOOFFS STA SCNSC1
 STA SCNSC2
 LDA #(>PMADDR)+3
 STA SCNSC1+1
 TXA
 CLC
 ADC SCNSC1+1
 ADC #1
 STA SCNSC2+1
 LDA GLPCNT
 ASL
 TAY
 LDA BLSADD,Y
 STA PIXGET
 INY
 LDA BLSADD,Y
 STA PIXGET+1
 LDY #$0F
STBSCL LDA (PIXGET),Y
 STA (SCNSC1),Y
 LDA BLUSC0,Y
 STA (SCNSC2),Y
 DEY
 BPL STBSCL
 LDA #0
 STA AUDC2
 INC VFREEZ
 LDA #$84
 STA VGLPC2
 LDA #2
 STA VGLPC1
 LDA #$14
 STA GPRIOR
 LDA ATSEQU
 BEQ CKGPS0
 JMP CLRHIT
CKGPS0 LDA GLPCNT
 BNE CKGPS1
 LDA #2
 BNE UDGPSC
CKGPS1 CMP #1
 BNE CKGPS2
 LDA #4
 BNE UDGPSC
CKGPS2 CMP #2
 BNE CKGPS3
 LDA #8
 BNE UDGPSC
CKGPS3 LDA #1
 STA SCOREX+2
 LDA #6
UDGPSC STA SCOREX+3
 JMP PSCORE
PMDEAD LDA PMSTAT
 ORA #$80
 STA PMSTAT
 JSR CLRAUD
 LDA #1
 STA MSTILL
 STA VFIZST
 LDA #$60
 STA VFIZSQ
COLCKX LDA #2
 BIT M2PF
 BEQ CLRHIT
 LDX PLYNUM
 LDA MAZCT1,X
 CMP #$0C
 BCC LOWFSI
 LDA #$0C
LOWFSI TAX
 LDA FSINDX,X
 TAX
 LDY #0
FSLOOP LDA FS0100,X
 STA PACMAZ+$1F1,Y
 INX
 INY
 CPY #5
 BNE FSLOOP
 LDA #1
 STA FRSCRF
 LDA #$40
 STA FRSCRT
 LDA #0
 STA FRUFLG
 STA FRUTMR
 STA FRUTMR+1
 LDA #1
 STA GOBBLD
 LDA #$78
 STA GOBBLF
 LDX PLYNUM
 LDA MAZCT1,X
 CMP #$0C
 BCC LOWFRS
 LDA #$0C
LOWFRS ASL
 TAX
 LDA FRSTAB,X
 STA SCOREX+2
 INX
 LDA FRSTAB,X
 STA SCOREX+3
 JSR PSCORE
CLRHIT STA HITCLR
 RTS
;
VRESET JSR BLNKON
 LDA SWAPPF
 BEQ VRSET1
 LDX RESETT
 BEQ VSWAP1
 DEC RESETT
 RTS
VSWAP1 CMP #1
 BNE VSWAP2
;
 LDX #0
LODP1L LDA P1SAVE,X
 STA PACMAZ,X
 LDA P1SAVE+$100,X
 STA PACMAZ+$100,X
 LDA P1SAVE+$200,X
 STA PACMAZ+$200,X
 LDA P1SAVE+$300,X
 STA PACMAZ+$300,X
 INX
 BNE LODP1L
;
 JSR FLS2ON
 LDA #0
 STA PLYNUM
 BEQ VSWAPX
VSWAP2 CMP #2
 BNE VSWAP3
;
 LDX #0
LODP2L LDA P2SAVE,X
 STA PACMAZ,X
 LDA P2SAVE+$100,X
 STA PACMAZ+$100,X
 LDA P2SAVE+$200,X
 STA PACMAZ+$200,X
 LDA P2SAVE+$300,X
 STA PACMAZ+$300,X
 INX
 BNE LODP2L
;
 JSR FLS1ON
 LDA #1
 STA PLYNUM
 BNE VSWAPX
VSWAP3 CMP #3
 BNE VSWAP4
 JSR BLNKON
 JMP RST1PG
VSWAPX LDA #3
VSWPX1 STA SWAPPF
 RTS
VRSET1 LDA NUMPLY
 BEQ RST1PG
RST2PG LDA PLYNUM
 BNE RST2P2
RST2P1 LDA XPACP2
 BEQ RST1PG;PLAYER 2 DEAD
 LDA XPACP1
 BNE SWAP12;GO SWAP PLAYERS
 LDA #2
 STA SWAPPF;SHOW #1 GAME OVER
 LDA #$60
 STA RESETT
 BNE VGMEND
SWAP12 JSR SAVEP1
 LDA #$40
 STA RESETT
 LDA #2
 BNE VSWPX1
RST2P2 LDA XPACP1
 BEQ RST1PG;PLAYER 1 DEAD
 LDA XPACP2
 BNE SWAP21;GO SWAP PLAYERS
 LDA #1
 STA SWAPPF;SHOW #2 GAME OVER
 LDA #$60
 STA RESETT
 BNE VGMEND
SWAP21 JSR SAVEP2
 LDA #$40
 STA RESETT
 LDA #1
 STA SWAPPF
VGGONE RTS
RST1PG LDX PLYNUM
 LDA XPACP1,X
 BEQ VGMEND; GAME IS OVER
 JSR SETUP
 JSR READY1
 LDA NUMPLY
 BEQ VSWAP4
 LDA #$40
 STA RESETT
 LDA #4
 STA SWAPPF
 RTS
VSWAP4 LDA #0
 STA SWAPPF
 JMP READY2
VGMEND LDA #$AC
 LDX #0
GMOVLP STA PACMAZ+$1ED,X
 CLC
 ADC #1
 INX
 CPX #$0E
 BNE GMOVLP
 LDA #$44
 STA COLOR3
 STA COLPF3
 JSR PLAYRS
 LDA SWAPPF
 BNE VGGONE
THEEND LDX #$2A
 JSR CKHIGH
 LDX #$47
 JSR CKHIGH
 LDA #0
 STA RESETF
 LDA #1
 STA GMOVRF
 LDA #$E2
 STA ATTIMR
 JSR FLS1ON
 JMP BLNKON
;
CKHIGH LDY #0
CKHILP LDA TEXT,X
 CMP TEXT+$39,Y
 BEQ CHINXT
 BCC CKHIGX
 BCS STORHI
CHINXT INX
 INY
 CPY #6
 BNE CKHILP
 RTS
STORHI LDA TEXT,X
 STA TEXT+$39,Y
 INX
 INY
 CPY #6
 BNE STORHI
CKHIGX RTS
;
; TUNNEL LOGIC
;
TUNNEL LDX #4
CTMSKL LDA M1VPOS,X
 CMP #$74
 BNE NXTMSK
 JSR MSKTUN
NXTMSK DEX
 BPL CTMSKL
 RTS
;
MSKTUN STA TUNLOC
 TXA
 CMP #4
 BNE TUNPLY
 LDA #$FF
TUNPLY CLC
 ADC #(>PMADDR)+4
 STA TUNLOC+1
 LDA #$FF
 STA TUNMSK
 LDA M1HPOS,X
 STA TUNCNT
 CMP #$C0
 BCC TNMSK2
 LDA #$C0
TNMSL1 CMP TUNCNT
 BEQ STTMSK
 DEC TUNCNT
 ASL TUNMSK
 BCS TNMSL1
 LDA M1HPOS,X
 CMP #$CA
 BCC STTMSK
 LDA M1DIRT,X
 CMP #8
 BNE STTMSK
 LDA #$2A
 STA M1HPOS,X
 CPX #4
 BNE STTMSK
 LDA #$8F
 BNE TFXPAC
TNMSK2 CMP #$39
 BCS TNMSKX
TNMSL2 LDA #$38
 CMP TUNCNT
 BEQ STTMSK
 INC TUNCNT
 LSR TUNMSK
 BCS TNMSL2
 LDA M1HPOS,X
 CMP #$2A
 BNE STTMSK
 LDA M1DIRT,X
 CMP #4
 BNE STTMSK
 LDA #$CA
 STA M1HPOS,X
 CPX #4
 BNE STTMSK
 LDA #$B7
TFXPAC STA PACSCN
STTMSK LDY #$0F
STTMSL LDA (TUNLOC),Y
 AND TUNMSK
 STA (TUNLOC),Y
 DEY
 BPL STTMSL
TNMSKX RTS
;
