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

;
; PAC-MAN GAME SUBROUTINES
;
GOBBLE LDX GOBBLD
 BEQ GOBBLX
 LDA GOBBLF
 LDY #$A5
 CPX #7
 BCS GOBBD2
 CMP #$B4
 BEQ SAMGOB
 CLC
 ADC #5
 BNE STRGOB
SAMGOB INC GOBBLD
 LDY #$A3
 BNE STRGOB
GOBBD2 CPX #9
 BNE GOBBD3
 INC GOBBLD
 BNE DECGOB
GOBBD3 LDY #$A3
DECGOB SEC
 CMP #$78
 BNE GOBDEC
 LDA #0
 STA GOBBLD
 TAY
 BEQ STRGOB
GOBDEC SBC #5
STRGOB STA GOBBLF
 STA $D200
 STY $D201
GOBBLX RTS
;
SKIRTS LDA RTCLOK+2
 AND #$0F
 BNE SKIRTX
 LDA MSKIRT
 BEQ ISKIRT
 LDA #0
 STA MSKIRT
 BEQ SKIRTX
ISKIRT INC MSKIRT
SKIRTX RTS
;
; RERACK WILL RESET MAZE AFTER
; A PLAYER HAS CLEARED ALL DOTS
;
RERACK LDA RRSEQU
 BNE TSTRRS
 JSR CLRAUD
 JSR DRAWIT
 LDA #$40
RRKXX1 STA RRTIMR
RRKXX2 INC RRSEQU
 RTS
TSTRRS CMP #1
 BNE TSTRR2
 LDA RRTIMR
 BNE DRRWTM; DEC RERACK WAIT TIMER
 LDA #0
 LDX #3
TSRR1L STA HPOSP0,X
 DEX
 BPL TSRR1L
 LDA #$0C
 STA COLOR0
 LDA #$07
 STA RRFLCT; RERACK FLASH COUNT
 LDA #$10
 BNE RRKXX1
DRRWTM DEC RRTIMR
 RTS
TSTRR2 CMP #2
 BNE TSTRR3
 LDA RRTIMR
 BNE DRRWTM; DEC RERACK COLOR TIMER
 DEC RRFLCT
 BEQ RRKXX2
ALTCLR LDA RRFLCT
 CLC
 LSR
 BCC ALTBLU
 LDA #$0C
 BNE SETRRC
ALTBLU LDA #$86
SETRRC STA COLOR0
 LDA #$10
 STA RRTIMR
 RTS
TSTRR3 CMP #3
 BNE TSTRR4
 LDA #0
 STA INTCNT
 LDA #>CHRORG
 STA CHBASE
TSTR3X RTS
TSTRR4 CMP #4
 BNE TSTRR5
 JSR NEWBRD
 LDX PLYNUM
 INC XPACP1,X
 INC MAZCT1,X
 JSR READY1
 INC RRSEQU
 LDA NUMPLY
 BEQ TSTR3X
 LDA #$30
 STA RRTIMR
 RTS
TSTRR5 CMP #5
 BNE TSTRR6
 LDA RRTIMR
 BNE DTMRR5
 INC RRSEQU
 RTS
DTMRR5 DEC RRTIMR
 RTS
TSTRR6 JSR READY2
 LDA #0
 STA RRFLAG
 STA RRSEQU
 LDA #2
 STA RESETF
 LDA #$40
 STA RESETT
 RTS
;
; GET READY TO PLAY
;
READY1 LDA #$A2
 LDX #0
REDYLP STA PACMAZ+$1EF,X
 CLC
 ADC #1
 INX
 CPX #$0A
 BNE REDYLP
 JSR PLAYRS
 LDX PLYNUM
 LDA MAZCT1,X
 CMP #6
 BCC SETRED
 CMP #$0A
 BCS SETRED
 LDY #$DA; SET UP FOR GREEN FRUITS
 BNE SETFRC
SETRED LDY #$44
SETFRC STY FRUCLR
 LDY #0
 CMP #6
 BCS HFRUTS
 STA TEMLOC
 LDA #$92
 STA PIXPUT
 LDA #(>PACMAZ)+3
 STA PIXPUT+1
 LDX #0
FRUTLP LDA FRUCHR,X
 STA (PIXPUT),Y
 INC PIXPUT
 CLC
 ADC #1
 STA (PIXPUT),Y
 CPX TEMLOC
 BEQ FSPLIT
FRUTS1 INX
 DEC PIXPUT
 DEC PIXPUT
 DEC PIXPUT
 BNE FRUTLP
HFRUTS CMP #18
 BCC HFRUT1
 LDA #18
HFRUT1 SEC
 SBC #6
 STA TEMLOC
 SEC
 LDA #<HIFRUT
 SBC TEMLOC
 STA PIXGET
 LDA #>HIFRUT
 SBC #0
 STA PIXGET+1
 LDX #0
HFRUTL LDA (PIXGET),Y
 STA PACMAZ+$386,X
 CLC
 ADC #1
 STA PACMAZ+$387,X
 INX
 INX
 INY
 CPY #$7
 BNE HFRUTL
FSPLIT RTS
;
READY2 LDX #$0B
 LDA #0
RED2LP STA PACMAZ+$14E,X
 DEX
 BPL RED2LP
 JSR DRAWIT
 LDX PLYNUM
 DEC XPACP1,X
;
UDXPACS LDX PLYNUM
 LDA XPACP1,X
 LDX #0
 LDY #$9B
 CMP #3
 BNE TWOPAC
 STY PACMAZ+$378
UDXPC2 STY PACMAZ+$376
UDXPC1 STY PACMAZ+$374
 RTS
TWOPAC CMP #2
 BNE ONEPAC
 JSR UDNPC2
 JMP UDXPC2
ONEPAC CMP #1
 BNE NOPACS
 JSR UDNPC1
 JMP UDXPC1
NOPACS STX PACMAZ+$374
UDNPC1 STX PACMAZ+$376
UDNPC2 STX PACMAZ+$378
 RTS
;
DRAWIT LDA #1
 STA MSTILL
 LDX #3
GREADL LDA M1DIRT,X
 JSR MONHND
 DEX
 BPL GREADL
 LDA #0
 STA MSTILL
 JMP PACSTP
;
READY3 LDX #$0D
 LDA #0
REDY3L STA PACMAZ+$14D,X
 STA PACMAZ+$1ED,X
 DEX
 BPL REDY3L
 RTS
;
FLSHXU LDA RTCLOK+2
 AND #$0F
 BNE FLSHXX
 LDA FLSHUP
 BNE FLSRST
 INC FLSHUP
 BNE FLSHER
FLSRST LDA #0
 STA FLSHUP
FLSHER LDA PLYNUM
 BEQ FLSPL1
 LDA FLSHUP
 BNE FLS2ON
 TAX
 TAY
 BEQ F2STOR
FLS2ON LDA #$12
 LDX #$35
 LDY #$30
F2STOR STA TEXT+$21
 STX TEXT+$22
 STY TEXT+$23
 RTS
FLSPL1 LDA FLSHUP
 BNE FLS1ON
 TAX
 TAY
 BEQ F1STOR
FLS1ON LDA #$11
 LDX #$35
 LDY #$30
F1STOR STA TEXT+4
 STX TEXT+5
 STY TEXT+6
FLSHXX RTS
;
; SET UP MONSTER AND PACMAN
; START POSITIONS
;
SETUP JSR INITPM;CLEAR P/M GRAPHICS
SETUP1 LDX #$7F
CLRPGZ STA $80,X
 DEX
 BPL CLRPGZ
 JSR SETCLR;INIT COLOR REGS
;
; SPEED INITIALIZATION
;
SPDINI LDX PLYNUM
 LDA MAZCT1,X
 CMP #6
 BCC LOWINI
 LDA #6
LOWINI TAY
 LDA PACSPD,Y
 TAX
 LDA SPEED1,X
 STA PMSPCT
 LDA MONSPD,Y
 TAY
 LDX #3
SPINIL LDA SPEED1,Y
 STA M1SPCT,X
 DEX
 BPL SPINIL
;
 LDX #$13
INDATL LDA INIDAT,X
 STA PACSCN,X
 DEX
 BPL INDATL
 LDY #0
 JSR MSTIMR
 STA HITCLR
 RTS
NEWGAM LDA #3
 STA XPACP1;DO AT GAME START
 STA XPACP2
 LDA DIFOPT
 STA MAZCT1
 STA MAZCT2
 LDX #1
 JSR NEWBD1
NEWBRD JSR P1INIT; DO AT SCREEN START
 JSR UDXPACS
 LDX PLYNUM
 LDA MAZCT1,X
 TAY
 BNE NEWRK2
 LDA RTCLOK+2
 BPL NEWRK2
NEWRK1 JSR MSTIMR
 JMP NEWBD0
NEWRK2 INY
 BNE NEWRK1
NEWBD0 LDX PLYNUM
NEWBD1 LDA #$0F
 STA BIGDT1,X
 LDA #0
 STA FRUTP1,X
 STA DTCTL1,X
 STA DTCTM1,X
 RTS
;
MSTIMR CPY #3
 BCC LDMSTM
 LDY #3
LDMSTM LDX #2
LDMSTL LDA STARTV,Y
 STA M2TIMR,X
 INY
 DEX
 BPL LDMSTL
 RTS
;
; VFIZZL IS THE FOLD-UP
; SEQUENCE FOR THE PACMAN
;
; VFIZST IS THE STATUS
;
; 0 = NO ACTION
; 1 = WIGGLE SKIRTS
; 2 = CLEAR MONSTERS & INIT SOUNDS
; 3 = SOUND FREQ INCREASING
; 4 = SOUND FREQ DECREASING
; 5 = FADE OUT SOUND
; 6 = SHOW BLANK SCREEN
;
VFIZZL LDA VFIZST
 CMP #1
 BNE INICLR
 LDA VFIZSQ
 BEQ NXTFSQ
 JSR SKIRTS
 LDX #3
UDMFLP LDA M1DIRT,X
 JSR MONHND
 DEX
 BPL UDMFLP
 DEC VFIZSQ
 RTS
NXTFSQ INC VFIZST
 LDA VFIZST
INICLR CMP #2
 BNE FIZCHK
 LDX #3
 LDA #0
MONCLR STA HPOSP0,X
 DEX
 BPL MONCLR
 LDA #$35
 STA VFIZFQ
 STA VFIZBS
 JSR CLRAUD
 STA VFIZCT
 STA FIZPTR
 INC FIZZLE
 LDA #$07
 STA FIZTIM
 LDA #3
 STA VFIZST
 BNE VFIZUP
FIZCHK CMP #3
 BEQ VFIZUP
 CMP #4
 BEQ VFIZDN
 CMP #5
 BEQ VFIZFZ
 CMP #6
 BEQ FZWAIT
VFIZUP LDA #$A8
 STA AUDC1
 LDA VFIZFQ
 STA AUDF1
 CLC
 ADC #8
 STA VFIZFQ
 INC VFIZCT
 LDA VFIZCT
 CMP #4
 BNE VBFIZX
 BEQ SVFIZS
VFIZDN LDA #$A8
 STA AUDC1
 LDA VFIZFQ
 STA AUDF1
 SEC
 SBC #8
 STA VFIZFQ
 DEC VFIZCT
 BNE VBFIZX
 LDA VFIZBS
 CMP #$6D
 BEQ VBFIZI
VFIZSW CLC
 ADC #4
 STA VFIZBS
 STA VFIZFQ
 LDA #3
SVFIZS STA VFIZST
 RTS
VFIZFZ LDA #$A8
 STA AUDC1
 LDA VFIZBS
 STA AUDF1
 SEC
 SBC #8
 STA VFIZBS
 CMP #$25
 BNE VBFIZX
 JSR CLRAUD
 LDA #$80
 STA VFIZCT
VBFIZI INC VFIZST
VBFIZX RTS
;
FZWAIT LDA VFIZCT
 BNE DECFCW
 INC RESETF
 RTS
DECFCW DEC VFIZCT
 RTS
;
; FIZZIE WILL DRAW THE PACMAN
; FOLDING UP IN SEQUENCE
;
FIZZIE LDA FIZZLE
 BEQ FIZZIX
 LDA PMVPOS
 STA PIXPUT
 LDA #(>PMADDR)+3
 STA PIXPUT+1
 LDA FIZTIM
 BEQ RSTFIZ
 DEC FIZTIM
 BPL STRFIZ
RSTFIZ LDA FIZPTR
 CMP #$0F
 BEQ CLRFIZ
 INC FIZPTR
 LDA #$07
 STA FIZTIM
STRFIZ LDA FIZPTR
 BEQ FIZSTR
 CMP #$0F
 BEQ EXPPAC
 TAX
 DEX
 LDA FIZIDX,X
 TAY
 DEY
 LDA FIZDAT,X
 STA (PIXPUT),Y
 RTS
FIZSTR LDY #$0C
 LDX #9
FIZSTL LDA PACDIE,X
 STA (PIXPUT),Y
 DEY
 DEX
 BPL FIZSTL
 RTS
EXPPAC LDY #$0F
 LDX #$0F
EXPPCL LDA PACEXP,X
 STA (PIXPUT),Y
 DEY
 DEX
 BPL EXPPCL
 RTS
CLRFIZ LDY #$0F
 LDA #0
CLRFZL STA (PIXPUT),Y
 DEY
 BPL CLRFZL
 STA FIZZLE
FIZZIX RTS
;
FLITEC LDA FLASHC
 BEQ NOFLIT
 LDA TWEETR
 BNE CKFLTM
 JSR VRVERB
CKFLTM LDA FLITMR
 BEQ FLSHSQ
 DEC FLITMR
 JMP SETFLC
NOFLIT LDA TWEETR
 BNE FIZZIX
 JMP VCHASE
FLSHSQ LDX PLYNUM
 LDA MAZCT1,X
 TAX
 LDA FLSTIM,X
 CMP FLASHC
 BNE NXTFLS
 LDX #3
RSCLLP LDA M1STAT,X
 BPL NXTRSC
 LDA COLORS,X
 STA PCOLR0,X
NXTRSC DEX
 BPL RSCLLP
 LDX #3
RSTCHL LDA M1STAT,X
 BPL NXRSTC
 CMP #$80
 BEQ RSTBOX
 AND #$BB
 BEQ NXRSTC
 BNE RSTCHS
RSTBOX LDA #0
 BEQ RSSTAT
RSTCHS LDA #$20
RSSTAT STA M1STAT,X
NXRSTC DEX
 BPL RSTCHL
 LDA #0
 STA FLASHC
 STA FLASHT
 STA VFLITS
 LDA #$A0
 STA CHASET
 RTS
NXTFLS LDA FLASHT
 BNE DECFTM
 INC FLASHC
 LDA #$18
 STA FLASHT
DECFTM DEC FLASHT
SETFLC LDA FLASHC
 LSR; ALTERNATE BLUE & WHITE
 BCC FWHITE
 LDY #$84; DARK BLUE
 BNE FSTORE
FWHITE LDY #$0C; WHITE
FSTORE LDX #3
FSTORL LDA M1STAT,X
 BPL NXFSTR
 TYA
 STA PCOLR0,X
NXFSTR DEX
 BPL FSTORL
FLITCX RTS
;
;
VRVERB LDA VFLITS
 BNE VFLRDY
 LDA #5
 STA VFLITV
 LDA #2
 STA VFLITD
 INC VFLITS
 LDA #$90
 STA VFLITF
VFLRDY LDA VFLITD
 CMP #1
 BNE VFLIDN
 LDA VFLITF
 CMP #$90
 BNE VFUPOK
 LDA VFLITV
 CMP #3
 BNE VFVDEC
 LDA #5
 STA VFLITV
VFVDEC DEC VFLITV
 LDA #2
 STA VFLITD
 BNE VFDNOK
VFUPOK CLC
 LDA VFLITF
 ADC #$10
 BNE VRBSTR
VFLIDN LDA VFLITF
 CMP #$40
 BNE VFDNOK
 LDA #1
 STA VFLITD
 BNE VFUPOK
VFDNOK SEC
 LDA VFLITF
 SBC #$10
VRBSTR STA VFLITF
 STA AUDF2
 LDA VFLITV
 ORA #$A0
VRVBX1 STA AUDC2
VRVRBX RTS
;
VTWEET LDA TWEETF
 BNE CTWEET
 LDA #$0C
 STA TWEETF
 BNE ITWEET
CTWEET CMP #$17
 BNE ITWEET
 LDA #$0C
 STA TWEETF
ITWEET INC TWEETF
STWEET LDA TWEETF
 STA AUDF2
 LDA #$A4
 BNE VRVBX1
;
VGULPR DEC VGLPC1
 BEQ DISTRT
 SEC
 LDA VGLPC2
 SBC #4
 STA VGLPC2
 CMP #$10
 BEQ GLPOFF
 STA AUDF1
 STA AUDF4
 LDA #$84
 STA AUDCTL
 LDA #$A4
 BNE GBRNCH
DISTRT LDA #2
 STA VGLPC1
 SEC
 LDA VGLPC2
 SBC #3
 STA VGLPC2
 STA AUDF1
 STA AUDF3
 STA AUDF4

 .if VERSION=ORIGINAL
   LDA #$A8
 .elseif VERSION=ROKLAN
   LDA #$A3
 .elseif VERSION=ATARI82
   LDA #$A3
 .else
   .error "Invalid version ", VERSION
 .endif

 STA AUDC3
GBRNCH STA AUDC4
 BNE VEATRS
GLPOFF JSR CLRAUD
 STA AUDCTL
 STA VFREEZ
 STA PACCNT
 STY GULPED
 LDA #$2A
 STA COLOR3
 LDA #$11
 STA GPRIOR
 LDX #3
RSTPCL LDA PCOLR0,X
 CMP #$DC
 BEQ RSTPLC
 DEX 
 BPL RSTPCL
 RTS
RSTPLC LDA #$0C
 STA PCOLR0,X
 STA HITCLR
 LDA #0
 STA PACCNT
 JSR PMSTIK
 INC PACADV
 JMP MUNCHY
;
VEATER LDA EATERF
 BEQ VEATRX
 LDX EATERC
 CPX #6
 BNE CEATER
 LDA #0
 STA EATERF
 STA EATERC
 BEQ VEATRS
CEATER LDA EATERT
 BNE EATER2
 LDA E1DATA,X
 JMP STREAT
EATER2 LDA E2DATA,X
STREAT INC EATERC
 STA AUDF1

 .if VERSION=ORIGINAL
   LDA #$A4
 .elseif VERSION=ROKLAN
   LDA #$A4
 .elseif VERSION=ATARI82
   LDA #$C2
 .else
   .error "Invalid version ", VERSION
 .endif
VEATRS STA AUDC1
VEATRX RTS
;
CLRAUD LDY #7
 LDA #0
CLRAUL STA AUDF1,Y
 DEY
 BPL CLRAUL
CLRAUX RTS
;
PLAYRS LDA NUMPLY
 BEQ CLRAUX
 LDA #$65
 LDX #0
PLYRLP STA PACMAZ+$14E,X
 CLC
 ADC #1
 INX
 CPX #$0C
 BNE PLYRLP
 LDA PLYNUM
 BEQ CLRAUX
 LDX #$71
 STX PACMAZ+$158
 INX
 STX PACMAZ+$159
 RTS
;