*         1415    280384
*
** HISCORE.S
** ASTEROIDS FOR THAT DAMN ATARI 3600, OR IS IT 7000, I FORGET. *
** WRONG!! IT'S THE ATARI 7800 PRO SYSTEM.  TA DA! *
** THIS FILE CONTAINS THE HANDLERS FOR THE INTERFACE TO THE HISCORE CART *
**  UPDATED TO HSC REV 7.X *

* HSCINIT -- INITIALIZE HIGH SCORE CART DATA.  DOES HSCSTAT.
* RETURNS > 0 IF NO HSC EXISTS, < 0 IF ENTRY NOT FOUND, ZERO IF OK
HSCINIT:
	JSR	HSCSETDM	;SET UP DIFF/MODE LEVEL ARGUMENT
HSCINIT2:
	LDA	#$C6		;CHECK EXISTANCE.
	CMP	$3900
	BNE	NOHSC
	LDA	#$FE
	CMP	$3904
	BNE	NOHSC

	LDX	#$0E		;COPY HSC ROM INTO RAM ARGUMENT BLOCK
HSCCOPY:
	LDA	HSCROM,X
	STA	HSCRAM,X
	DEX
	BPL	HSCCOPY

	LDA	TEMP		;INSTALL LEVEL ARGUMENT
	ASL
	ASL
	STA	HSCRAM+2

;	LDX	#L(HSCRAM)	;WHY BOTHER; HSC TRASHES X ANYWAY
	LDY	#HSCRAM >> 8
	JMP	HSCSTAT

NOHSC:
	LDA	#1		;POSITIVE NUMBER = NO HIGH SCORE
	RTS

* HSSUCK -- SUCK IN HIGH SCORE, IF AVAILABLE.  IF NOT ZAP IT OUT.
HSCSUCK:
	JSR	ZEROHS		;FIRST ZAP IT OUT
	JSR	HSCINIT		;SUCK IN HIGH SCORE, IF AVAILABLE
	BNE	NOHSC
	LDX	#3
SUCKHSIN:
	LDA	HSCRAM+$F,X
	STA	HISCORE,X
	DEX
	BPL	SUCKHSIN
	RTS

* HSCSHOW -- DISPLAY ONE DIFF/MODE LEVEL
HSCSHOW:
	JSR	HSCINIT2	;USE INIT2 SO NOT TO DISTURB TEMP VAL
	BNE	NOHSC

	LDX	HSCRAM+4	;COMPUTE DISPLAY TIMES FOR EACH LEVEL
	CPX	#5		;USE MINIMUM TIME IF >= 5 ENTRIES
	BCC	ZNHSOK
	LDX	#5
ZNHSOK:
	LDA	HSTIME,X	;TABLE LOOKUP FOR DISPLAY TIME
	STA	HSCRAM+$E

	LDA	TEMP		;TEMP CONTAINS THE DIFF/MODE LEVEL
	ASL			; SHIFT UP FOR ARG FORMAT
	ASL
	STA	HSCRAM+2

	JSR	HSCDM		;PUT UP OUR OWN DIFFICULTY/MODE NAMES
	JSR	ONTITLE		;PUT UP THE LOGO

	JSR	WAITLOAD	;WAIT FOR LOADER TO UPDATE
	LDA	#$60		;TURN OFF DMA NOW, SO WE DON'T GET
	STA	CTRL		; A GLITCHY SCREEN IN BETWEEN
	LDA	#$40		;TELL NMI HANDLER TO DO FANCY
	STA	NMICTRL		; ALTERNATING READ MODES ON CTRL

;	JSR	OLDBUTON	;GO BACK TO ONE BUTTON MODE
;	LDX	#L(HSCRAM)
	LDY	#HSCRAM>>8
	JSR	HSCATRCT
;	JSR	NEWBUTON	;REVERT TO WHATEVER MODE WAS BEFORE

	RTS

* HSCDOENT -- DO CALLS TO HSCENTER FOR ALL PLAYERS WHAT NEEDS IT
HSCDOENT:
	JSR	HSCINIT		;SET UP HSC
	BEQ	ZHSENTOK	;DOES THE HSC EXIST?
	BPL	NOHSC		;NO.  DO NOTHING.
ZHSENTOK:
	LDA	HSCRAM+2	;SET UP TEMP TO DM VALUE
	LSR
	LSR
	STA	TEMP

	JSR	HSCDM		;PUT UP DIFF/MODE INDICATORS
	JSR	ONTITLE		;PUT UP THE LOGO

	LDA	MODE		;SET UP ENTER DELAY FOR THIS MODE
	LSR
	BCC	ZNOWAIT		;ON EVEN MODES, DON'T WAIT HERE.
	LDA	#10		;ON ODD MODES, WAIT 10 SECONDS.  THERE
	STA	HSCRAM+$E	; IS ONLY ONE SCORE BEING ENTERED.
ZNOWAIT:
	LDA	MODE		;IF IN ONE PLAYER MODE JUST
	BMI	ZHSCENT		;  DO THIS ONE.

	LDX	#0		;PICK HIGHER OF THE TWO FIRST!
ZCOMPLOP:
	LDA	SCORE,X		; COMPARE EACH DIGIT OF P1 SCORE
	CMP	SCORE+4,X	;  AGAINST EACH DIGIT OF P2 SCORE
	BEQ	ZP2EQU		; P1 EQUALS P2, MOVE ON TO NEXT DIGIT
	BCS	ZPNGO		; P1 > P2, LET P1 ENTER
ZP2GO:				; P2 > P1, LET P2 ENTER
	INC	HSCRAM+2	; TOGGLE PLAYER BIT TO P2
	LDA	#SCORE2 & 255	; CHANGE TO P2 SCORE
	STA	HSCRAM+8
ZPNGO:

	LDA	MODE		;IN TEAMPLAY MODE, USE COMBINED SCORE
	CMP	#1
	BNE	ZHSCENT
	LDA	#COMBSCOR & 255
	STA	HSCRAM+8	;LOW ADDR OF SCORE PTR.  HI ADDR = 0 ZP

ZHSCENT:
	JSR	WAITLOAD	;WAIT FOR LOADER TO UPDATE
	LDA	#$60		;TURN OFF DMA NOW, SO WE DON'T GET
	STA	CTRL		; A GLITCHY SCREEN IN BETWEEN
	LDA	#$40		;TELL NMI HANDLER TO DO FANCY
	STA	NMICTRL		; ALTERNATING READ MODES ON CTRL

;	JSR	OLDBUTON	;GO BACK TO ONE BUTTON MODE
;	LDX	#L(HSCRAM)
	LDY	#HSCRAM >> 8
	JSR	HSCENTER
;	JSR	NEWBUTON	;REVERT TO WHATEVER MODE WAS BEFORE

	LDA	HSCRAM+$E	;CHECK DELAY TIME:
	BNE	ZHSCRTS		; IF NOT ZERO, WE'RE DONE.

	LDA	#10		;SET DELAY TIME TO FINAL DELAY.
	STA	HSCRAM+$E

	LDA	HSCRAM+2	;CHECK PLAYER BIT TO SEE WHICH PLAYER
	LSR
	BCC	ZP2GO		;IF WE JUST DID P1, THEN DO P2
ZP1GO:
	LDA	#SCORE & 255	;GO BACK TO P1 SCORE
	STA	HSCRAM+8
	DEC	HSCRAM+2	;GO BACK TO P1 JOYSTICK
	JMP	ZHSCENT

ZP2EQU:
	INX			; MOVE ON TO NEXT DIGIT
	CPX	#4		; 4 DIGITS OF SCORE TO COMPARE
	BEQ	ZPNGO		; IF WE HAVE DONE ALL COMPARES, DFLT P1
	BNE	ZCOMPLOP	; IF NOT, DO NEXT COMPARE

* HSCDM -- PUT UP DIFF/MODE ICONS ACCORDING TO VALUE OF TEMP
HSCDM:
	LDA	TEMP
	AND	#$03		;GET DIFF BITS
	CLC
	ADC	#10
	TAY
	LDX	#10
	JSR	DOICON
	LDA	TEMP
	LSR			;GET MODE BITS
	LSR
	BEQ	HSCZAPM		;ONLY DISPLAY TEAM AND COMPETITVE PLAY
	CLC
	ADC	#15
	TAY
	INX			;10+1=11
	JSR	DOICON
ZHSCRTS:
	RTS

HSCZAPM:
	LDA	#$FF		;ZAP OUT STATUS OF MODE ICON
	STA	STATUS+11
	RTS

* HSCSETDM -- SET DIFF/MODE LEVEL ARGUMENT BASED ON DIFF AND MODE
HSCSETDM:
	LDA	MODE		;COMPUTE DIFF/MODE LEVEL VALUE BYTE
	BPL	ZHSCMOK
	LDA	#0
ZHSCMOK:
	ASL
	ASL
	CLC
	ADC	DIFF
	STA	TEMP		;SAVE IT WHERE WE CAN INSERT LATER
	RTS

* GO TO OLD ONE-BUTTON MODE FOR WAT'S HISCORE CART
;OLDBUTON  LDA	#$FF
;ZOLDSTOR  STA	SWCHB
;	    RTS
* GO BACK TO PREVIOUS STATE
;NEWBUTON  LDA	ONEBUT
;	    BPL	ZOLDSTOR

; THIS TABLE IS COPIED INTO RAM WHEN HSC IS CALLED.  IT'S THE ARGUMENT BLOCK
;	    NEEDED BY THE HSC.

HSCROM:
	.DC.B	$49,$6C		; CARTRIDGE ID NUMBER

	.DC.B	$00		; DIFFICULTY LEVEL
				;  + PLAYER NUMBER (BOTTOM 2 BITS)

	.DC.B	$01		; CONTROLLER TYPE

	.DC.B	$00,$00		; GAME NAME; WE USE OUR OWN LOGO.

	.DC.B	$00,$00		; DIFFICULTY NAME; WE USE OUR OWN.

	.DC.W	SCORE		; POINTER TO THE SCORE

	.DC.W	HSCDLL		; POINTER TO DLL

	.DC.W	LOGOBARS	;SOUND ROUTINE. WE USE THIS TO TWINKLE
				;  OUR LOGO BARS WHILE OFF SCREEN.

	.DC.B	$00		; SECONDS TO WAIT BEFORE RETURNING
				;  (FOR EACH DIFFICULTY LEVEL)

* HSCDLL -- DLL FOR HSC DISPLAY.  MERELY A RE-ARRANGED VERSION OF DLLTAB W/DLIS
HSCDLL:
	.DC.B	$8F
	.DC.B	BLANKDL>>8,BLANKDL&255			;DLI + 16 BLANK LINES

	.DC.B	$4F
	.DC.B	(GAMEDL07+4)>>8,(GAMEDL07+4)&255	;NO DLI + MODE

	.DC.B	$4F
	.DC.B	(GAMEDL04+4)>>8,(GAMEDL04+4)&255	;NO DLI + TOP OF LOGO

	.DC.B	$4F
	.DC.B	(GAMEDL05+4)>>8,(GAMEDL05+4)&255	;NO DLI + BOTTOM OF LOGO

	.DC.B	$4F
	.DC.B	(GAMEDL11+4)>>8,(GAMEDL11+4)&255	;NO DLI + DIFF LEVEL

* HSTIME -- TIME TO DISPLAY EACH ENTRY FOR A GIVEN TOTAL NUMBER OF ENTRIES.
HSTIME:
	.DC.B	0,10,6,5,4,3
