;************************************
;*                                  *
;*            PAC-MAN               *
;*                                  *
;*   Developed for Atari Inc. by    *
;*  Roklan Corp. This Information   *
;*  is confidential and is not for  *
;*  sale or distribution.           *
;*                                  *
;*                                  *
;*                                  *
;*            10/03/82              *
;*                                  *
;*          DISK VERSION            *
;*                                  *
;*          REVISION 3.0            *
;*                                  *
;************************************
;

; Adapted version for MADS, 2018-07-28 JAC!

 .def ORIGINAL=1		  ;Original source version
 .def ROKLAN=2			  ;Roklan binary disk version
 .def ATARI82=3			  ;Atari disk version, intermissions 3 & 4 are broken
 .if .not .def VERSION		  ;Default is not specified on assembler command line
   .def VERSION=ORIGINAL
 .endif

  .if .not .def INTERMISSION_DEMO ;Default is not specified on assembler command line
    .def INTERMISSION_DEMO=0	  ;Complete level by eating one dot 
  .endif
 
 ICL "SYSTEXT.ASM"
;
; LIST I
;
;*******  ZERO PAGE VARIABLES  ******
;
 ORG $0018
;
DLICNT .DS 1 ;DISPLAY LIST INT. COUNT
OPTION .DS 1 ;OPTION MODE FLAG
NUMPLY .DS 1 ;NUMBER OF PLAYERS
DIFOPT .DS 1 ;DIFFICULTY OPTION
RSTRTF .DS 1 ;RESTART FLAG
ATCLOK .DS 1 ;ATTRACT COUNTDOWN CLOCK
ATTIMR .DS 1 ;ATTRACT MODE TIMER
ATSEQU .DS 1 ;ATTRACT MODE SEQUENCE #
;
GMOVRF .DS 1 ;GAME OVER FLAG
READYF .DS 1 ;READY FLAG
INTROF .DS 1 ;INTRO FLAG
SWAPPF .DS 1 ;SWAP PLAYER FLAG
RESETF .DS 1 ;RESET FLAG
RESETT .DS 1 ;RESET TIMER
RRFLAG .DS 1 ;RERACK FLAG
RRSEQU .DS 1 ;RERACK SEQUENCE #
RRTIMR .DS 1 ;RERACK TIMER
RRFLCT .DS 1 ;RERACK FLASH COUNT
TEMLOC .DS 1 ;TEMPORARY STORAGE
PLYNUM .DS 1 ;PLAYER # 0=1 1=2
XPACP1 .DS 1 ;EXTRA PACMAN PLAYER 1
XPACP2 .DS 1 ;EXTRA PACMAN PLAYER 2
BPACP1 .DS 1 ;BONUS PACMAN PLAYER 1
BPACP2 .DS 1 ;BONUS PACMAN PLAYER 2
MAZCT1 .DS 1 ;MAZE COUNT PLAYER 1
MAZCT2 .DS 1 ;MAZE COUNT PLAYER 2
BIGDT1 .DS 1 ;BIG DOT STATUS #1
BIGDT2 .DS 1 ;BIG DOT STATUS #2
DTCTL1 .DS 1 ;DOTS EATEN CTR LSB #1
DTCTL2 .DS 1 ;DOTS EATEN CTR LSB #2
DTCTM1 .DS 1 ;DOTS EATEN CTR MSB #1
DTCTM2 .DS 1 ;DOTS EATEN CTR MSB #2
SCNSC1 .DS 2 ;SCREEN SCORE 1 ADDRESS
SCNSC2 .DS 2 ;SCREEN SCORE 2 ADDRESS
PIXGET .DS 2 ;P/M PIX GET ADDRESS
PIXPUT .DS 2 ;P/M PIX PUT ADDRESS
PACCLR .DS 1 ;ATTRACT PACMAN COLOR
;
 ORG $0043
;
FRUTP1 .DS 1 ;FRUIT COUNTER #1
FRUTP2 .DS 1 ;FRUIT COUNTER #2
BCOUNT .DS 1 ;BOUNCE TIMER COUNT
SAVCNS .DS 1 ;SAVE CONSOL DATA
TUNLOC .DS 2 ;USED FOR TUNNEL LOGIC
;
 ORG $005A
M1DELY .DS 1 ;MONSTER 1 DELAY
M2DELY .DS 1 ;MONSTER 2 DELAY
M3DELY .DS 1 ;MONSTER 3 DELAY
M4DELY .DS 1 ;MONSTER 4 DELAY
ACOLR1 .DS 1 ;ATTRACT COLOR 1 - $3A
ACOLR2 .DS 1 ;ATTRACT COLOR 2 - $44
ACOLR3 .DS 1 ;ATTRACT COLOR 3 - $2A
ACOLR4 .DS 1 ;ATTRACT COLOR 4 - $DA
INTCNT .DS 1 ;INTERMISSION COUNTER
INTCLK .DS 1 ;INTERMISSION CLOCK
 ORG $0080
;
PACSCN .DS 2 ;PACMAN SCREEN MEM ADDR
PACBYT .DS 1 ;PACMAN BYTE COUNTER
PVSAVE .DS 1 ;PACMAN VPOS SAVE
PHSAVE .DS 1 ;PACMAN HPOS SAVE
M1VPOS .DS 1 ;MONSTER 1 VERTICAL POSIT
M2VPOS .DS 1 ;MONSTER 2 VERTICAL POSIT
M3VPOS .DS 1 ;MONSTER 3 VERTICAL POSIT
M4VPOS .DS 1 ;MONSTER 4 VERTICAL POSIT
PMVPOS .DS 1 ;PACMAN VERTICAL POSITION
M1HPOS .DS 1 ;MONSTER 1 HORIZ POSITION
M2HPOS .DS 1 ;MONSTER 2 HORIZ POSITION
M3HPOS .DS 1 ;MONSTER 3 HORIZ POSITION
M4HPOS .DS 1 ;MONSTER 4 HORIZ POSITION
PMHPOS .DS 1 ;PACMAN HORIZONTAL POSIT
M1DIRT .DS 1 ;MONSTER 1 DIRECTION
M2DIRT .DS 1 ;MONSTER 2 DIRECTION
M3DIRT .DS 1 ;MONSTER 3 DIRECTION
M4DIRT .DS 1 ;MONSTER 4 DIRECTION
PMODIR .DS 1 ;PACMAN OLD DIRECTION
SCOREX .DS 6 ;PLAYER SCORE FOR TEXT
CARRYB .DS 1 ;CARRY BIT FOR ABOVE
PAUSEF .DS 1 ;PAUSE FLAG
WHINEY .DS 1 ;CHASE WHINE DELTA
FRUTMR .DS 2 ;FRUIT TIMER (10 SECS)
FRUFLG .DS 1 ;FRUIT DISPLAY FLAG
FRUCLR .DS 1 ;FRUIT COLOR
FRSCRF .DS 1 ;FRUIT SCORE FLAG
FRSCRT .DS 1 ;FRUIT SCORE TIMER
NOTCNT .DS 1 ;NOTE COUNT FOR TUNES
VSAVER .DS 1 ;VERTICAL POSIT SAVE
HSAVER .DS 1 ;HORIZONTAL POSIT SAVE
PACMAP .DS 1 ;PACMAN VERT MAP COUNTER
PACCNT .DS 1 ;PACMAN MOTION COUNT
PACADV .DS 1 ;PACMAN ADVANCE-TURNING
PACDLY .DS 1 ;PACMAN DELAY-EATING DOTS
PMSTAT .DS 1 ;PACMAN STATUS
PMSEQU .DS 1 ;PACMAN SEQUENCE (MOUTH)
PMNDIR .DS 1 ;PACMAN NEW DIRECTION
CHASET .DS 1 ;CHASE TIMER
MSTILL .DS 1 ;MONSTERS STILL FLAG
MSKIRT .DS 1 ;MONSTER SKIRT FLAG
M1SPSQ .DS 1 ;MONSTER 1 SPEED SEQUENCE
M2SPSQ .DS 1 ;MONSTER 2 SPEED SEQUENCE
M3SPSQ .DS 1 ;MONSTER 3 SPEED SEQUENCE
M4SPSQ .DS 1 ;MONSTER 4 SPEED SEQUENCE
PMSPSQ .DS 1 ;PACMAN SPEED SEQUENCE
M1SPCT .DS 1 ;MONSTER 1 SPEED COUNT
M2SPCT .DS 1 ;MONSTER 2 SPEED COUNT
M3SPCT .DS 1 ;MONSTER 3 SPEED COUNT
M4SPCT .DS 1 ;MONSTER 4 SPEED COUNT
PMSPCT .DS 1 ;PACMAN  SPEED COUNT
M1PIDX .DS 1 ;MONSTER 1 PATTERN INDEX
M2PIDX .DS 1 ;MONSTER 2 PATTERN INDEX
M3PIDX .DS 1 ;MONSTER 3 PATTERN INDEX
M4PIDX .DS 1 ;MONSTER 4 PATTERN INDEX
M1PCNT .DS 1 ;MONSTER 1 PATTERN COUNT
M2PCNT .DS 1 ;MONSTER 2 PATTERN COUNT
M3PCNT .DS 1 ;MONSTER 3 PATTERN COUNT
M4PCNT .DS 1 ;MONSTER 4 PATTERN COUNT
M1THPS .DS 1 ;MONSTER 1 TARGET HPOS
M2THPS .DS 1 ;MONSTER 2 TARGET HPOS
M3THPS .DS 1 ;MONSTER 3 TARGET HPOS
M4THPS .DS 1 ;MONSTER 4 TARGET HPOS
M1TVPS .DS 1 ;MONSTER 1 TARGET VPOS
M2TVPS .DS 1 ;MONSTER 2 TARGET VPOS
M3TVPS .DS 1 ;MONSTER 3 TARGET VPOS
M4TVPS .DS 1 ;MONSTER 4 TARGET VPOS
M1TIMR .DS 1 ;MONSTER 1 TIMER
M2TIMR .DS 1 ;MONSTER 2 TIMER
M3TIMR .DS 1 ;MONSTER 3 TIMER
M4TIMR .DS 1 ;MONSTER 4 TIMER
M1STAT .DS 1 ;MONSTER 1 STATUS
M2STAT .DS 1 ;MONSTER 2 STATUS
M3STAT .DS 1 ;MONSTER 3 STATUS
M4STAT .DS 1 ;MONSTER 4 STATUS
M1SSEQ .DS 1 ;MONSTER 1 START SEQUENCE
M2SSEQ .DS 1 ;MONSTER 2 START SEQUENCE
M3SSEQ .DS 1 ;MONSTER 3 START SEQUENCE
M4SSEQ .DS 1 ;MONSTER 4 START SEQUENCE
M1VDIR .DS 1 ;MONSTER 1 VERT CHOICE
M2VDIR .DS 1 ;MONSTER 2 VERT CHOICE
M3VDIR .DS 1 ;MONSTER 3 VERT CHOICE
M4VDIR .DS 1 ;MONSTER 4 VERT CHOICE
M1HDIR .DS 1 ;MONSTER 1 HORZ CHOICE
M2HDIR .DS 1 ;MONSTER 2 HORZ CHOICE
M3HDIR .DS 1 ;MONSTER 3 HORZ CHOICE
M4HDIR .DS 1 ;MONSTER 4 HORZ CHOICE
VCHASF .DS 1 ;CHASE FREQ
VCHASD .DS 1 ;DIRECTION 1=INC 2=DEC
VCHASS .DS 1 ;CHASE SOUND START
VFLITF .DS 1 ;FLIGHT FREQ
VFLITD .DS 1 ;DIRECTION SAME
VFLITV .DS 1 ;FLIGHT VOLUME
VFLITS .DS 1 ;FLIGHT SOUND START
VFREEZ .DS 1 ;FREEZE FLAG
VGLPC1 .DS 1 ;GULP COUNT1
VGLPC2 .DS 1 ;GULP COUNT2
GULPED .DS 1 ;LAST MONSTER EATEN
GLPCNT .DS 1 ;GULP COUNT
FIZZLE .DS 1 ;FIZZLE FLAG 1=FIZL
FIZPTR .DS 1 ;FIZZLE ADDR POINTER
FIZTIM .DS 1 ;FIZZLE TIMER
VFIZST .DS 1 ;FIZZLE STATUS
VFIZSQ .DS 1 ;FIZZLE SEQUENCE #
VFIZBS .DS 1 ;FIZZLE FREQ BASE
VFIZFQ .DS 1 ;FIZZLE CURRENT FREQ
VFIZCT .DS 1 ;FIZZLE COUNTER
TWEETR .DS 1 ;TWEET SOUND FLAG
TWEETF .DS 1 ;TWEET SOUND FREQ
EATERF .DS 1 ;EAT DOT SOUND FLAG
EATERC .DS 1 ;EAT DOT SOUND COUNT
EATERT .DS 1 ;EAT DOT TOGGLE
GOBBLD .DS 1 ;GOBBLE DIRCTN (FRUITS)
GOBBLF .DS 1 ;GOBBLE FREQUENCY
BLINKT .DS 1 ;BLINK TIMER
FLSHUP .DS 1 ;FLASH 1UP 2UP TIMER
FLASHT .DS 1 ;FLASH TIMER
FLASHC .DS 1 ;FLASH COUNT
FLITMR .DS 1 ;FLIGHT TIMER
TUNMSK .DS 1 ;TUNNEL BIT MASK
TUNCNT .DS 1 ;TUNNEL ITERATION COUNT
;
; EJECT
;
 ORG $0600
;
PACBUF .DS 16 ; PACMAN IMAGE BUFFER
MONBUF .DS 16 ; MONSTER IMAGE BUFFER
;
INTRDL EQU $0680;INTERMISSION DL
INTMOD EQU $06C0;INTERMISSION MODE
INTSEQ EQU $06C1;INTERMISSION SEQ
;
;
;****   SYSTEM VARIABLES    ****
;
BDELAY EQU 1  ;BOUNCE DELAY
FDELAY EQU $FF;FRUIT TIMER VALUE
ADELAY EQU $30;ATTRACT TIMER DELAY
;
GAMMEM EQU $0800; GAME MEMORY
;
PACMAZ EQU GAMMEM+$0C00
P1SAVE EQU GAMMEM+$1000
P2SAVE EQU GAMMEM+$1400
OPTCHR EQU GAMMEM+$1800
OPTSCN EQU GAMMEM+$1C00
PMADDR EQU GAMMEM+$2000
TEXT   EQU GAMMEM+$2000
PATSCN EQU GAMMEM+$2800
AMCSET EQU GAMMEM+$2C00

	OPT F+
;
; EJECT
;
 ORG $3FFD
;
 JMP INIT
;
 ICL "PACDAT1.ASM"
 
;
; EJECT
;
INIT LDX #9
INITL1 LDA HISCTX,X
 STA TEXT+$0F,X
 DEX
 BPL INITL1
 LDA #0
 LDX #$27
INITL2 STA DLICNT,X
 DEX
 BPL INITL2
 LDX #$7F
INIPZL STA $80,X
 DEX
 BPL INIPZL
;
 INC OPTION
 LDX RTCLOK+2
 DEX
 STX ATCLOK
;
; RELOCATE OPTION CHAR SET
;
 LDX #0
OCLOOP LDA PACTTL,X
 STA OPTCHR+8,X
 INX
 BNE OCLOOP
;
; BUILD ATTRACT MODE CHAR SET
;
ANCHRL LDA CHRORG,X
 STA AMCSET,X
 LDA CHRORG+$100,X
 STA AMCSET+$100,X
 INX
 BNE ANCHRL
 LDX #$1F
ANCDTL LDA ATCHRS,X
 STA AMCSET+$18,X
 DEX
 BPL ANCDTL
;
; ENABLE KEYSCAN & SET VBI VECTOR
;
PACINI SEI
 LDA POKMSK
 ORA #$40
 STA POKMSK
 STA IRQEN
 CLI
 LDY #<VBLANK
 LDX #>VBLANK
 LDA #7
 STA SKCTL
 JSR SETVBV;GO SET VBLANK VECTOR
 LDA #$C0;ENABLE DLI AND VBI
 STA NMIEN
 LDA #$3E
 STA SDMCTL;ENABLE P/M GRAPHICS
 LDA #$3
 STA GRACTL
 LDA #>PMADDR
 STA PMBASE
 LDA #<INIT
 STA DOSVEC
 LDA #>INIT
 STA DOSVEC+1
 LDA #<INITAD
 STA DOSINI
 LDA #>INITAD
 STA DOSINI+1
;
REINIT LDA #0;NEW GAME VECTOR
 LDX #$1F
REINLP STA GMOVRF,X
 DEX
 BPL REINLP
 TXS
 JSR CLRAUD
;
; NOW INITIALIZE PLAYER SCREENS
;
 JSR P1INIT
 JSR SAVEP1
 JSR SAVEP2
;
 LDA OPTION
 BNE LOOP
;
PACGAM LDA RTCLOK+2
PACGML CMP RTCLOK+2
 BEQ PACGML
 LDA #<DLIST
 STA SDLSTL
 STA DLISTL
 LDA #>DLIST
 STA SDLSTH
 STA DLISTH
 LDA #<DLIV; DLI VECTOR
 STA VDSLST
 LDA #>DLIV
 STA VDSLST+1
 LDA #$11
 STA GPRIOR;LET MISSILES USE COLPF3
SETPAC JSR SETUP
 JSR NEWGAM
 LDA NUMPLY
 BEQ P1SCIN
 JSR SET2PL
 JMP GODOIT

P1SCIN JSR SET1PL
GODOIT LDA #2
 STA RSTRTF
;
LOOP LDA RRSEQU
 CMP #3
 BNE GOLOOP
 
 JSR INTMIS
 JSR SETUP
 INC RRSEQU
GOLOOP LDX  #3
LOOPLP LDA ACOLOR,X
 EOR COLRSH;ATTRACT COLORS THAT ARE
 AND DRKMSK;SWITCHED WITH DLI'S
 STA ACOLR1,X
 DEX
 BPL LOOPLP
 LDA OPTION
 BNE PCTRIG
 LDA KEYDEL
 BNE IGNORK
 LDA CH;    TEST FOR SPACE BAR PAUSE
 CMP #$21
 BNE IGNORK
 LDA PAUSEF
 BEQ FLIPON
 LDA #0
 BEQ STFLIP
FLIPON LDA #1
STFLIP STA PAUSEF
NOFLIP LDA #$FF
 STA CH
IGNORK LDA GMOVRF
 BEQ GAMCNS
 LDA ATTIMR;IF GAME OVER, TIME OUT
 BNE PCTRIG;AND GO TO OPTION SCREEN
 LDA #4
 BNE PCEXT1
PCTRIG LDA #$FF
 STA CH
 LDA STRIG0
 BEQ CSTART
 LDA STRIG1;TEST TRIGGER START
 BEQ CSTART
GAMCNS LDA CONSOL
 CMP #7;    NO BUTTONS PRESSED
 BEQ LOOP
CNSSWT STA SAVCNS
 LDX #BDELAY
 STX BCOUNT
CLOOP1 ORA CONSOL
 LDX BCOUNT;DEBOUNCE LOGIC
 BNE CLOOP1
 CMP SAVCNS
 BNE GAMCNS
CLOOP2 CMP CONSOL
 BEQ CLOOP2
 LDA SAVCNS
 CMP #6
 BEQ CSTART
 LDX ATSEQU
 BEQ CPROPT
 LDA #0
 STA ATSEQU
 LDA #4
 BNE PCEXT1
CPROPT LDX OPTION;IF OPTION SCREEN
 CPX #3;         THEN CHANGE PARAMS
 BCC PCEXIT
PRESSW CMP #3
 BEQ COPTON
 CMP #5
 BEQ CSELEC
 JMP LOOP
CSTART LDA #0
 STA GMOVRF;START FUNCTION-NEW GAME
 STA OPTION
 STA ATSEQU
 LDA #1
 STA RSTRTF
 JMP REINIT
CSELEC LDA NUMPLY
 BNE PCNSE1;PROCESS SELECT CHANGES
 LDA #1
 BNE PCNSE2
PCNSE1 LDA #0
PCNSE2 STA NUMPLY
PCEXIT LDA #3
PCEXT1 STA OPTION
 STA ATRACT
 LDA #7
 STA ATCLOK
 JMP LOOP
COPTON LDA DIFOPT
 CMP #2
 BCS PCNOP1;PROCESS OPTION CHANGES
 INC DIFOPT
 BNE PCEXIT
PCNOP1 CMP #$0C
 BEQ PCNOP2
 INC DIFOPT
 INC DIFOPT
 BNE PCEXIT
PCNOP2 LDA #0
 STA DIFOPT
 BEQ PCEXIT
;
; DISPLAY LIST INTERRUPT VECTOR
;
DLIV PHA
 TXA
 PHA
 LDX DLICNT;DLI COUNT
 BNE DLI2ND
 LDA #>PACCHR
 STA WSYNC
 STA CHBASE
 BNE DLIXIT
DLI2ND CPX #1
 BNE DLI3RD
 LDA ACOLR4;-$DA PLAYER 1-2 MSG
 LDX ACOLR1;-$3A LT ORANGE COLOR
 STA WSYNC
 STA COLPF1
 STX COLPF2
 BNE DLIXIT
DLI3RD CPX #2
 BNE DLI4TH
 LDA FRUCLR; FRUIT COLOR
 EOR COLRSH
 AND DRKMSK
 STA WSYNC
 STA COLPF1
 BNE DLIXIT
DLI4TH CPX #3
 BNE DLI5TH
 LDA ACOLR2
 STA WSYNC
 STA COLPF1
 BNE DLIXIT
DLI5TH CPX #4
 BNE LSTDLI
 LDA ACOLR3;YELLOW COLOR
 LDX ACOLR4
 STA WSYNC
 STX COLPF0
 STA COLPF3
 BNE DLIXIT
LSTDLI LDX #3
 LDA #0
DLICLP STA COLPM0,X
 DEX
 BPL DLICLP
 STA COLPF3
DLIXIT INC DLICNT
 PLA
 TAX
 PLA
 RTI
;
; DLI VECTOR FOR OPT & TITLE SCREENS
;
OPDLIV PHA
 TXA
 PHA
 TYA
 PHA
 LDX DLICNT
 BNE OPDLI1
 LDY ACOLR2
 LDA ACOLR1
 LDX #>OPTCHR
 STA WSYNC
 STX CHBASE
 STA COLPF2
 STY COLPF1
 JMP OPDLIX
OPDLI1 LDA OCINDX,X
 STA WSYNC
 STA CHBASE
OPDLIX INC DLICNT
 PLA
 TAY
 PLA
 TAX
 PLA
 RTI
;
; VERTICAL BLANK INTERRUPT VECTOR
;
VBLANK LDA #0
 STA DLICNT
 DEC BCOUNT
 JSR VBSUBS
 LDA ATSEQU
 BNE VBEXIT
 LDA VFREEZ
 BNE VBEXIT
 JSR TUNNEL
VBEXIT JMP XITVBV; EXIT VECTOR
;
OPTTTL LDA #0
 LDX #7
CHRDWR STA HPOSP0,X
 STA AUDF1,X
 DEX
 BPL CHRDWR
 LDA #<ODLIST
 STA SDLSTL
 STA DLISTL
 LDA #>ODLIST
 STA SDLSTH
 STA DLISTH
 LDA #<OPDLIV
 STA VDSLST
 LDA #>OPDLIV
 STA VDSLST+1
OCOLRS LDY #3
OCLRLP LDA OCOLOR,Y
 STA COLOR0,Y
 DEY
 BPL OCLRLP
INITAD RTS
;
SET1PL LDX #6
 LDA #0
S1PLL1 STA TEXT+$20,X
 STA TEXT+$47,X
 STA TEXT+$2A,X
 DEX
 BPL S1PLL1
SETXPL JSR FLS1ON
 LDA #$10
 STA TEXT+$2E
 STA TEXT+$2F
 RTS
;
SET2PL LDX #6
 LDA #0
S2PLL1 STA TEXT+$2A,X
 STA TEXT+$47,X
 DEX
 BPL S2PLL1
 JSR FLS2ON
 LDA #$10
 STA TEXT+$4B
 STA TEXT+$4C
 BNE SETXPL

;
; EJECT
  ICL "PAC1.ASM"
; EJECT
  ICL "PAC2.ASM"
; EJECT
  ICL "PAC3.ASM"
; EJECT
  ICL "PAC4.ASM"
; EJECT

  ICL "PACDAT2.ASM"
; EJECT

:$28c .BY 0	;Fill gap with zeros

;
 
 ORG $7400
;
 ICL "PACDAT3.ASM"
;

;:$922 .BY 0	;Fill gap with zeros

 END INIT
