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


;
; PAC-MAN GAME SUBROUTINES
;
; INITIALIZE PLAYER/MISSILE MEMORY
;
; THIS SUBR CLEARS P/M AREA
;
INITPM LDX #0
 TXA
 JSR INITPL
 LDA #$FE
 LDX #$F0
 JSR INITPL
 TXA
 RTS
INITPL STA PMADDR+$300,X
 STA PMADDR+$400,X
 STA PMADDR+$500,X
 STA PMADDR+$600,X
 STA PMADDR+$700,X
 INX
 BNE INITPL
 RTS
;
; SETCOLOR SUBROUTINE WILL
; INITIALIZE $2C0 - $2C7
; (COLOR REGISTERS)
;
SETCLR LDX #7
SETCLL LDA COLORS,X
 STA PCOLR0,X
 DEX
 BPL SETCLL
 RTS
;
;
P1INIT LDX #0
P1INIL LDA DATMAZ,X
 STA PACMAZ,X
 LDA DATMAZ+$100,X
 STA PACMAZ+$100,X
 LDA DATMAZ+$200,X
 STA PACMAZ+$200,X
 LDA DATMAZ+$300,X
 STA PACMAZ+$300,X
 INX
 BNE P1INIL
 RTS
;
SAVEP1 LDX #0
SAVP1L LDA PACMAZ,X
 STA P1SAVE,X
 LDA PACMAZ+$100,X
 STA P1SAVE+$100,X
 LDA PACMAZ+$200,X
 STA P1SAVE+$200,X
 LDA PACMAZ+$300,X
 STA P1SAVE+$300,X
 INX
 BNE SAVP1L
 RTS
;
SAVEP2 LDX #0
SAVP2L LDA PACMAZ,X
 STA P2SAVE,X
 LDA PACMAZ+$100,X
 STA P2SAVE+$100,X
 LDA PACMAZ+$200,X
 STA P2SAVE+$200,X
 LDA PACMAZ+$300,X
 STA P2SAVE+$300,X
 INX
 BNE SAVP2L
SAV2PX RTS

;
; PMSTIK SUBROUTINE WILL TEST
; JOYTICK INPUT AND DETERMINE
; IF DIRECTION CHOSEN IS VALID.
;   THE PACMAN WILL THEN MOVE
; IN THE PROPER DIRECTION WITH
; IT'S MOUTH OPENING AND CLOSING.
;
; THIS CODE IS CALLED DURING
; VBLANK AND INITIATES MOTION
; DURING ALTERNATE OCCURANCES
; OF VBLANK.  MOUTH ANIMATION
; IS PERFORMED EVERY VBLANK.
;
;
PMSTIK LDA PMSTAT
 BMI SAV2PX
PUDSTK LDX #4
 JSR MAZHND
 CLC
 LDA PMNDIR;SEE IF WE CHANGE DIR.
 BIT TEMLOC;IS IT VALID ?
 BEQ PMUDST;NO
 CMP PMODIR
 BEQ PUDSAM
 ORA PMODIR
 TAY
 AND #3
 BEQ PUDSAM
 TYA
 AND #$0C
 BEQ PUDSAM
 STY PACADV
PUDSAM LDA PMNDIR
 STA PMODIR
PMUDST LDX PLYNUM
 LDA STICK0,X
 EOR #$0F
 BEQ PMSAME

 ORG $5969
 .if VERSION=ORIGINAL 
   ICL "PAC3-$5969-ORIGINAL.ASM"
 .elseif VERSION=ROKLAN 
   ICL "PAC3-$5969-ROKLAN.ASM"
 .elseif VERSION=ATARI82
   ICL "PAC3-$5969-ATARI82.ASM"
  .else
   .error "Invalid version ", VERSION
 .endif

 STA PMNDIR
 BIT TEMLOC
 BEQ PMSAME
 STA PMODIR
PMSAME LDA PMODIR
 BIT TEMLOC
 BNE PACTST
 JMP PACOPN
PACTST CMP #1
 BEQ PACUP
 CMP #2
 BEQ PACDN
 CMP #4
 BNE PACRTV
 JMP PACLF
PACRTV CMP #8
 BEQ PACRT
 JMP PACSTP
PACUP LDA PACCNT
 BNE PACUPS
 DEC PMVPOS
 DEC PMVPOS
 LDA PACMAP
 BNE DECPMP
 LDA #3
 STA PACMAP
 BNE PACUPS
DECPMP DEC PACMAP
 BNE PACUPS
 LDA PMVPOS
 CMP PVSAVE
 BEQ PACUPS
 SEC
 LDA PACSCN
 SBC #$28
 STA PACSCN
 LDA PACSCN+1
 SBC #0
 STA PACSCN+1
PACUPS LDY #6;POINT TO PACTOP
 JMP MOVPAC
PACDN LDA PACCNT
 BNE PACDNS
 INC PMVPOS
 INC PMVPOS
 LDA PACMAP
 CMP #3
 BNE INCPMP
 LDA #0
 STA PACMAP
 LDA PMVPOS
 CMP PVSAVE
 BEQ PACDNS
 CLC
 LDA PACSCN
 ADC #$28
 STA PACSCN
 LDA PACSCN+1
 ADC #0
 STA PACSCN+1
 BNE PACDNS
INCPMP INC PACMAP
PACDNS LDY #8;POINT TO PACBOT
 JMP MOVPAC
 ORG $5A19	;Force alignment JAC!

PACRT LDA PACCNT
 BNE PACRTS
;
 LDA PMHPOS
 CMP #$CA
 BNE NORTTN
 LDA #$2A
 STA PMHPOS
 LDA #$8F
 STA PACSCN
;
NORTTN INC PMHPOS
 LDA PACBYT
 CMP #3
 BNE INCPBY
 LDA #0
 STA PACBYT
 LDA PMHPOS
 CMP PHSAVE
 BEQ PACRTS
 INC PACSCN
 BNE PACRTS
 INC PACSCN+1
 BNE PACRTS
INCPBY INC PACBYT
PACRTS LDY #2;POINT TO PACRGT
 BNE MOVPAC

PACLF LDA PACCNT
 BNE PACLFS
;
 LDA PMHPOS
 CMP #$2A
 BNE NOLFTN
 LDA #$CA
 STA PMHPOS
 LDA #$B7
 STA PACSCN
;
NOLFTN DEC PMHPOS
 LDA PACBYT
 BNE DECPBY
 LDA #3
 STA PACBYT
 BNE PACLFS
DECPBY DEC PACBYT
 BNE PACLFS
 LDA PMHPOS
 CMP PHSAVE
 BEQ PACLFS
 SEC
 LDA PACSCN
 SBC #1
 STA PACSCN
 LDA PACSCN+1
 SBC #0
 STA PACSCN+1
PACLFS LDY #4;POINT TO PACLFT
 BNE MOVPAC
PACOPN LDA PMODIR
 CMP #1
 BNE POPNDN
 LDY #6; UP
 BNE SETOPN
POPNDN CMP #2
 BNE POPNLF
 LDY #8; DOWN
 BNE SETOPN
POPNLF CMP #4
 BNE POPNRT
 LDY #4; LEFT
 BNE SETOPN
POPNRT CMP #8
 BNE PACSTP
 LDY #2
SETOPN LDA #$0A
 BNE STRPAC
PACSTP LDY #0;POINT TO PACDOT
 TYA
 BEQ STRPAC
MOVPAC LDX PMSEQU
 BNE PACSID
 INC PMSEQU
 BNE PACSTP
PACSID DEX
 LDA PACIDX,X
 CPX #2
 BNE INCPSQ
 LDX #$FF
 STX PMSEQU
INCPSQ INC PMSEQU
STRPAC TAX
 LDA PACADD,Y
 STA PIXGET
 INY
 LDA PACADD,Y
 STA PIXGET+1
 TXA
 CLC
 ADC PIXGET
 STA PIXGET
 LDA #0
 ADC PIXGET+1
 STA PIXGET+1
 LDY #9
PMBFLP LDA (PIXGET),Y
 STA PACBUF+3,Y
 DEY
 BPL PMBFLP
 LDA #<PACBUF
 STA PIXGET
 LDA #>PACBUF
 STA PIXGET+1
 LDA PMVPOS
 STA PIXPUT
 LDA #(>PMADDR)+3
 STA PIXPUT+1
 CLC
 LDA PMHPOS
 STA HPOSM3
 ADC #2
 STA HPOSM2
 ADC #2
 STA HPOSM1
 ADC #2
 STA HPOSM0
 LDY #$0F
PPLOOP LDA (PIXGET),Y
 STA (PIXPUT),Y
 DEY
 BPL PPLOOP
PMSIXX RTS

;
; MUNCHY SUBROUTINE EATS DOTS
;
MUNCHY LDA PACBYT
 BNE MUNCHX
 LDA PACMAP
 BNE MUNCHX
 LDA PMHPOS
 CMP PHSAVE
 BNE NWPREF
 LDA PMVPOS
 CMP PVSAVE
 BEQ PMSIXX
NWPREF LDA PMVPOS
 STA PVSAVE
 LDA PMHPOS
 STA PHSAVE
 LDY #0
 LDA (PACSCN),Y
 CMP #1
 BEQ EATSML
 CMP #2
 BNE MUNCHX
 TYA
 STA (PACSCN),Y	
 RTS
 
EATSML STA SCOREX+4
 JSR PSCORE
 LDA #1
 STA EATERF
 STA PACDLY
 LDA #0
 STA EATERC
 LDA EATERT
 BNE ZEATER
 LDA #1
 BNE SEATER
ZEATER LDA #0
SEATER STA EATERT
 LDA #0
 TAY
 STA (PACSCN),Y
INCDOT LDX PLYNUM
 INC DTCTL1,X
 BNE CHKMAX
 INC DTCTM1,X
CHKMAX LDX PLYNUM
  .if INTERMISSION_DEMO=1
:10 NOP
 .else
 LDA DTCTM1,X
 BEQ MUNCHX
 LDA DTCTL1,X
 CMP #4
 BNE MUNCHX
 .endif
 
 LDA #1
 STA RRFLAG
MUNCHX RTS
;
DOTTST LDX PLYNUM
 LDA BIGDT1,X
 STA TEMLOC
 LDX PMVPOS
 LDY PMHPOS
 LDA #1
 BIT TEMLOC
 BEQ DT2TST
 CPX #$3C
 BNE DT2TST
 CPY #$3A
 BEQ DOTFND
DT2TST ASL
 BIT TEMLOC
 BEQ DT3TST
 CPX #$3C
 BNE DT3TST
 CPY #$BE
 BEQ DOTFND
DT3TST ASL
 BIT TEMLOC
 BEQ DT4TST
 CPX #$A4
 BNE DT4TST
 CPY #$3A
 BEQ DOTFND
DT4TST ASL
 BIT TEMLOC
 BEQ MUNCHX
 CPX #$A4
 BNE MUNCHX
 CPY #$BE
 BNE MUNCHX
DOTFND EOR #$0F
 AND TEMLOC
 LDX PLYNUM
 STA BIGDT1,X
 LDA #5
 STA SCOREX+4
 JSR PSCORE
 LDA #1; SET UP FOR BLUE MONSTERS
 STA FLASHC
 LDA #$FF
 STA GLPCNT
 LDX PLYNUM
 LDA MAZCT1,X
 TAX
 LDA BLUTIM,X
 STA FLITMR
 LDX #3
SETFLL LDA M1STAT,X
 ASL
 BMI NXTFLL
 LDA M1STAT,X
 ORA #$80; SET STATUS = FLIGHT
 STA M1STAT,X
 LSR
 LSR
 LSR
 BCS NXTFLL
;
 AND #$3B
 BEQ NXTFLL
 LDA M1DIRT,X
 TAY
 LDA BLUREV,Y
 STA M1DIRT,X
;
NXTFLL DEX
 BPL SETFLL
 JMP INCDOT
;
; PSCORE WILL ADD ANY POINTS
; SCORED TO THE PLAYERS' SCORE
;
PSCORE LDA #0;PSCORE SUBR
 STA CARRYB
 SED
 LDA PLYNUM
 BEQ PSCOR1
 LDX #$4C
 BNE PSCORX
PSCOR1 LDX #$2F
PSCORX LDY #5
KSCORE CLC
 LDA TEXT,X
 AND #$0F
 ADC CARRYB
 ADC SCOREX,Y
 PHA
 AND #$10
 BEQ NOCARY
 LDA #1
NOCARY STA CARRYB
 PLA
 ORA #$10
 CMP #$10
 BNE STRSCR
 CPY #0
 BNE TESLED
 TYA
 BEQ STRSCR
TESLED LDA TEXT-1,X
 BNE NOLEAD
 LDA CARRYB
 BEQ STRSCR
NOLEAD LDA #$10
STRSCR STA TEXT,X
 DEX
 DEY
 BPL KSCORE
 CLD
 LDX #5
 LDA #0
CLRSCR STA SCOREX,X
 DEX
 BPL CLRSCR
CKBONS LDX PLYNUM
 LDA BPACP1,X
 BNE NOBONS
 CPX #0
 BNE CKBON2
 LDA TEXT+$2B
 BNE BONUSP
NOBONS RTS
CKBON2 LDA TEXT+$48
 BEQ NOBONS
BONUSP INC BPACP1,X
 INC XPACP1,X
 JMP UDXPACS
;
; MAZE HANDLER SUBROUTINE
;
; ENTRY:
; A REG VALUE EQUALS VPOS
; Y REG VALUE EQUALS HPOS
;
; EXIT:
; A REG VALUE EQUALS PERMISSIBLE
; DIRECTIONS FOR ANY OBJECT FROM
; ANY POSITION
;
; BIT 0 SET - UP OK
; BIT 1 SET - DN OK
; BIT 2 SET - RT OK
; BIT 3 SET - LF OK
;
; CARRY BIT IS SET IF
; DECISION POINT WAS REACHED
; OTHERWISE IT IS CLEARED.
;
MAZHND LDA M1HPOS,X
 STA HSAVER
 LDA M1VPOS,X
 STX TEMLOC
 LDX #9
MHORLP CMP VTABLE,X;SEARCH VPOS
 BEQ VRTFND;MATCH FOUND
 DEX
 BPL MHORLP;KEEP LOOKING
 LDA HSAVER;NONE FOUND SO TRY HPOS
 LDY #9
MVRTLP CMP HTABLE,Y;SEARCH HPOS
 BEQ HORFND;MATCH FOUND
 DEY
 BPL MVRTLP
VRTFND LDY #9;NOW WE CHECK HPOS TABLE
 LDA HSAVER;  TO SEE IF DECISION PT.
VRTFNL CMP HTABLE,Y
 BEQ CHOICE;YES - MAKE CHOICE
 DEY
 BPL VRTFNL
 LDA #$0C;NO - KEEP GOING
 CLC
 BCC MAZHNX
HORFND LDA #3; ONLY ONE MATCH FOUND
 CLC
 BCC MAZHNX
CHOICE TXA;NOW INDEX INTO TABLE
 ASL
 TAX
 LDA HTBADD,X
 STA PIXGET
 INX
 LDA HTBADD,X
 STA PIXGET+1
 LDA (PIXGET),Y
 SEC
MAZHNX LDX TEMLOC
 STA TEMLOC
 PHP
 CPX #4
 BEQ MAZHX1
 LDA M1DIRT,X
 TAY
 LDA TEMLOC
 AND REVTAB,Y
 STA TEMLOC
 LDA M1VPOS,X
 CMP #$64
 BNE MAZHX1
 LDA M1HPOS,X
 CMP #$76
 BEQ MASKIT
NEXTMU CMP #$82
 BNE MAZHX1
MASKIT LDA M1STAT,X
 BMI MASKSD
 LDA TEMLOC
 AND #$0E
 BNE MASKUP
MASKSD LDA #1
MASKUP STA TEMLOC
MAZHX1 LDA TEMLOC
 PLP
 RTS
;
VCHASE LDA #0
 STA VFLITS
 LDA VCHASS
 BNE VCHDIR
 LDA #$28
 STA VCHASF
 LDA #1
 STA VCHASS
 STA VCHASD
VCHDIR LDA VCHASD
 CMP #1
 BNE VCHADN
 LDA VCHASF
 CMP #$40
 BCC VCUPOK
 LDA #2
 STA VCHASD
 BNE VCDNOK
VCUPOK LDA WHINEY
 CLC
 ADC VCHASF
 BNE STRVCH
VCHADN LDA VCHASF
 CMP #$28
 BCS VCDNOK
 LDA #1
 STA VCHASD
 BNE VCUPOK
VCDNOK SEC
 LDA VCHASF
 SBC WHINEY
STRVCH STA VCHASF
 STA AUDF2
 LDA #$A1
 STA AUDC2
VCHASX RTS
;

BLINKR_JAC
 ORG $5D29	;JAC!

BLINKR LDA RTCLOK+2
 AND #$0F
 BEQ BLNKON
 CMP #8
 BEQ BLNKOF
 RTS
BLNKON LDY #2
 LDX PLYNUM
 LDA BIGDT1,X
 STA TEMLOC
 LDA #1
 BIT TEMLOC
 BEQ BKOND2
 STY PACMAZ+$7B
BKOND2 ASL
 BIT TEMLOC
 BEQ BKOND3
 STY PACMAZ+$9C
BKOND3 ASL
 BIT TEMLOC
 BEQ BKOND4
 STY PACMAZ+$283
BKOND4 ASL
 BIT TEMLOC
 BNE BKONX1
BKONXX RTS
;
BLNKOF LDY #0
 STY PACMAZ+$7B
 STY PACMAZ+$9C
 STY PACMAZ+$283
BKONX1 STY PACMAZ+$2A4
 RTS
;
;
; SPEED SEQUENCING FOR OBJECTS
;
; X REG = INDEX VALUE FOR OBJECT
;
; X=0 TO 3 FOR MONSTERS 1 - 4
; X=4 FOR PACMAN
;
; ON EXIT:
; A REG = 0 INDICATES UPDATE TIME
;
SPDSEQ DEC M1SPCT,X
 BEQ NXSPSQ
 LDA #$FF
 RTS
NXSPSQ LDA M1SPSQ,X
 CMP #3
 BNE INCSPS
 LDA #$FF
 STA M1SPSQ,X
INCSPS INC M1SPSQ,X
 LDY PLYNUM
 LDA MAZCT1,Y
 CMP #6
 BCC LOWSPD
 LDA #6
LOWSPD TAY
 CPX #4
 BNE MXSPSQ
 LDA PACSPD,Y
 BPL LDSPSQ
MXSPSQ LDA MONSPD,Y
LDSPSQ CLC
 ADC M1SPSQ,X
 TAY
 LDA SPEED1,Y
 STA M1SPCT,X
 LDA #0
 RTS
;
; CHASE SEQUENCING
;
CHSSEQ LDA RTCLOK+2
 AND #7
 BNE CHSSQ1
 LDA CHASET
 BEQ CHSSQ1
 DEC CHASET
CHSSQ1 LDX PLYNUM
 LDA DTCTM1,X
 BEQ CHMAD1
VRYMAD LDX #3
 JSR SETMAD
 LDA #5
 BNE STWHIN
CHMAD1 LDA DTCTL1,X
 CMP #$F0
 BCS VRYMAD
 CMP #$E0
 BCC SMLMAD
 LDX #2
 JSR SETMAD
 LDA #4
 BNE STWHIN
SMLMAD CMP #$B0
 BCC NOTMAD
 LDA #3
 BNE STWHIN
NOTMAD LDA #2
STWHIN STA WHINEY
CHSSQ2 LDX #3
CHSSQL LDA M1STAT,X
 CMP #8
 BEQ TSTCHS
 CMP #$10
 BEQ TSTCHS
 BNE NXCHSQ
TSTCHS JSR SEEPAC
NXCHSQ DEX
 BPL CHSSQL
 RTS
;
SETMAD LDA M1STAT,X
 CMP #8
 BEQ YESMAD
 CMP #$10
 BEQ YESMAD
 CMP #$20
 BNE NXSTMD
YESMAD LDA #2
 STA M1STAT,X
NXSTMD DEX
 BPL SETMAD
 STX CHASET
 RTS
;
FRUITY LDA FRSCRF
 BEQ TSTFRT
 LDA FRSCRT
 BNE DCFRST
 LDA #0
 STA FRSCRF
 LDX #4
CLFRSL STA PACMAZ+$1F1,X
 DEX
 BPL CLFRSL
 RTS
DCFRST DEC FRSCRT
DCFRSX RTS
TSTFRT LDA FRUFLG
 BNE DFRTMR
 LDX PLYNUM
 LDA DTCTL1,X
 TAY
 LDA FRUTP1,X
 BEQ FRTST1
 CMP #1
 BEQ FRTST2
 RTS
FRTST1 CPY #$50
 BEQ SETFRT
FRTST2 CPY #$A0
 BNE DCFRSX
SETFRT INC FRUTP1,X
 LDA MAZCT1,X
 CMP #$0C
 BCC LOWFRC
 LDA #$0C
LOWFRC TAX
 LDA FRUCHR,X
 STA PACMAZ+$1F3
 CLC
 ADC #1
 STA PACMAZ+$1F4
 LDA #1
 STA FRUFLG
 LDA #FDELAY
 STA FRUTMR
 LDA #2
 STA FRUTMR+1
 RTS
DFRTMR LDA FRUTMR+1
 BNE DCFRTM
 LDA FRUTMR
 BNE DCFRTM
 LDA #0
 STA PACMAZ+$1F3
 STA PACMAZ+$1F4
 STA FRUFLG
 RTS
DCFRTM DEC FRUTMR
 BNE FRUITX
 DEC FRUTMR+1
FRUITX RTS
