;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	RAM_TimeLeft = $7F8827

	;; Where to place the meter relative to the screen boarder
	MeterPositionX = $58
	MeterPositionY = $24
	Answers = $7F9A7B
	Correct =	 $7F9A7E	

	;; Possible values for the below variables:
	;;   (fast) $00, $01, $03, $07, $0f, $1f, $3f, $7f, $ff (slow)
	CountDownFrequency = $07
	CountUpFrequency = $00

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
;; INIT and MAIN routines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

DCB "INIT"
	LDA $7FAB10,x
 	AND #$04
	BEQ TAR	
	LDA $13BF
	STA $7F8889
	LDA #$04
	STA $13BF
	LDA #$01
	STA $1426
TAR:	PHB
        PHK
        PLB
	LDA #$00
	STA $7F9A7B	;7F9A7B-7D = answer submission digits
	STA $7F9A7C
	STA $7F9A7D
	STA $7F9B00	;current question
	STA $7F9B01	;answer ready to check flag
	STA $7F9A82	;points missed in one event variable
	TAY		;initialize on question 0
	LDA AnswerDigit1,y	;$7F9A7E-80 = correct answer digits
	STA $7F9A7E
	LDA AnswerDigit2,y
	STA $7F9A7F
	LDA AnswerDigit3,y
	STA $7F9A80

	LDA #$9F
	STA RAM_TimeLeft

        PLB


	;LDA #$00
	;STA $7F9B00

	;LDA #$9F
	;STA RAM_TimeLeft
	RTL

DCB "MAIN"
        PHB
        PHK
        PLB
        JSR MainSub
        PLB
        RTL
        
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sprite Main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PointLoss:
	dcb $00,$01,$03,$03,$05

AnswerDigit1:	dcb $00,$00,$00	;questions 0~2
		dcb $01,$00,$00	;3~5
		dcb $02,$01,$07	;6~8
		dcb $00		;9

AnswerDigit2:	dcb $01,$04,$02
		dcb $00,$00,$09
		dcb $01,$05,$02
		dcb $02

AnswerDigit3:	dcb $03,$08,$05
		dcb $00,$05,$02
		dcb $04,$03,$09
		dcb $02

AnswerSubmit:
	LDA $7F9B01
	BNE Check
	RTS
Check:
	LDA #$00
	STA $7F9B01

	LDA $7F9A7B	;answer given digit 1 (far left)
	CMP $7F9A7E	;correct answer digit 1
	BNE Wrong
	LDA $7F9A7C	;answer given digit 2
	CMP $7F9A7F	;correct
	BNE Wrong
	LDA $7F9A7D	;given 3
	CMP $7F9A80	;correct
	BNE Wrong
	LDA #$29
	STA $1DFC
	JSR Reset
	RTS
Wrong:
TimeUp:
	LDA $157C,x	;number of answers wrong
	INC
	;TAY
	;LDA PointLoss,y
	;STA $7F9A82
	;TYA
	STA $157C,x
	INC $1528,x	;# questions asked
	LDA #$2A
	STA $1DFC
	JSR Reset

	BRA NoSound

MainSub:
	LDA $7FAB10,x
 	AND #$04
	BEQ TARGET
	RTS
TARGET:	LDA RAM_TimeLeft	; Play warning sounds

	BEQ TimeUp
	CMP #$1C
	BEQ Sound
	CMP #$14
	BEQ Sound
	CMP #$0C
	BNE NoSound
Sound:	
	LDA #$2a
	STA $1DFC
NoSound:

	
Gfx:	
	JSR SubGfx		; Draw the sprite
NoGfx:	

	LDA $9D			; Return if sprites are locked
	BNE Return0
	LDA $13D4
	BNE Return0


CountDown:
	LDA $13
	AND #CountDownFrequency
	BNE SkipMeter

	LDA RAM_TimeLeft	
	DEC A	
	STA RAM_TimeLeft
	
	SkipMeter:	

		JSR AnswerSubmit	
		LDA $1a			; Position = Screen boundary
		CLC
		ADC #$80
		STA $E4,x		; This is done so the sprite stays on screen
		LDA $1b
		ADC #$00
		STA $14E0,x
		LDA $1c
		STA $D8,x
		LDA $1d
		STA $14D4,x
Return0:	
	RTS


KillMario:
NoCheck:

	RTS


Reset:
	LDA #$00	;reset all inputted digits to 0
	STA $7F9A7B
	STA $7F9A7C
	STA $7F9A7D

	LDA $157C,x	;4 wrong answers and done
	CMP #$04
	BEQ GameOver

	LDA $7F9B00
	INC A
	CMP #$0A	;used to be $10
	BEQ GameOver
	STA $7F9B00
	TAY
	LDA AnswerDigit1,y
	STA $7F9A7E
	LDA AnswerDigit2,y
	STA $7F9A7F
	LDA AnswerDigit3,y
	STA $7F9A80

	LDA #$9F
	STA RAM_TimeLeft
	RTS

GameOver:
	LDY $157C,x
	LDA PointLoss,y
	STA $7F9A82
	LDA #$05
	STA $71
	RTS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Graphics Routine
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	NumTiles = $09
	
SpriteTileDispY:	dcb $00,$00,$00,$00,$00,$18,$18,$18,$18
SpriteTileDispX:	dcb $01,$10,$20,$30,$40,$F8,$10,$20,$30
SpriteGfxProp:	dcb $00,$00,$00,$00,$00,$00,$00,$00,$00

Palettes:           dcb $09,$05,$0B
	
SubGfx:

	LDA $15F6,x		; NOTE: Reinstate to use the palette from the cfg file
	STA $02
	
	LDA RAM_TimeLeft	; Load address to be timed
	LSR
	LSR
	LSR
	STA $03
	
        LDY $15EA,x             ; get offset to sprite OAM                           

        PHX
        LDX #$00
	;LDX #NumTiles
GfxLoopStart:

        LDA #MeterPositionX
	CLC
	ADC SpriteTileDispX,x
        STA $0300,Y
	
	LDA #MeterPositionY
	CLC			;
	ADC SpriteTileDispY,x
        STA $0301,Y

	LDA $03
	JSR GetTile
        STA $0302,Y
	
        LDA $02
        ORA SpriteGfxProp,x
	ORA #$20
        STA $0303,Y
	
        INY    
        INY                     
        INY                     
        INY                     

	INX
	CPX #NumTiles
        BCC GfxLoopStart
	PLX

        LDY #$02
        LDA #NumTiles
        JSL $01B7B3
        RTS                       

GetPalette:
	LDY #$00
	LDA RAM_TimeLeft
	CMP #$55
	BCC LoadPalette
	INY
	CMP #$AA
	BCC LoadPalette
	INY
LoadPalette:
	LDA Palettes,y
	RTS	
		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Question0Tiles:	;6 + 7
	dcb $AA,$8C,$A6,$8E,$AA,$A4	;A6 = plus / A8 = minus / A4 = equals
Question1Tiles:	;8 x 6
	dcb $AA,$A0,$AC,$8C,$AA,$A4
Question2Tiles:	;34 - 9
	dcb $86,$88,$A8,$A2,$AA,$A4	;$80 = 0, $82 = 1... etc $A2 = 9
Question3Tiles:	;49 + 51
	dcb $88,$A2,$A6,$8A,$82,$A4
Question4Tiles:	;20 / 4
	dcb $84,$80,$AE,$88,$AA,$A4
Question5Tiles:	;46 x 2
	dcb $88,$8C,$AC,$84,$AA,$A4
Question6Tiles:	;856 / 4
	dcb $A0,$8A,$8C,$AE,$88,$A4
Question7Tiles:	;64 + 89
	dcb $8C,$88,$A6,$A0,$A2,$A4
Question8Tiles:	;9 x 9 x 9
	dcb $A2,$AC,$A2,$AC,$A2,$A4
Question9Tiles:	;8 + 9 + 5
	dcb $A0,$A6,$A2,$A6,$8A,$A4
;Answers:
;	dcb $80,$80,$80,$80,$80,$80,$80,$80,$80

AnswerTiles:
DEX
DEX
DEX
DEX
DEX
DEX
	LDA Answers,x
	ASL
	CLC
	ADC #$80
	CMP #$8F
	BCC NoAdd
	CLC
	ADC #$10
NoAdd:

INX
INX
INX
INX
INX
INX	
	JMP FinNoPull
	



GetTile:
	CPX #$08
	BEQ AnswerTiles
	CPX #$07
	BEQ AnswerTiles
	CPX #$06
	BEQ AnswerTiles
	PHY
	PHA
	LDA $7F9B00
	TAY
	PLA
	CPY #$00
	BEQ Question0
	CPY #$01
	BEQ Question1
	CPY #$02
	BEQ Question2
	CPY #$03
	BEQ Question3
	CPY #$04
	BEQ Question4
	CPY #$05
	BEQ Question5
	CPY #$06
	BEQ Question6
	CPY #$07
	BEQ Question7
	CPY #$08
	BEQ Question8
	CPY #$09
	BEQ Question9
	BRA Question0
TileFinish:	
	PLY
FinNoPull:
	RTS

Question0:
	LDA Question0Tiles,x
	BRA TileFinish
Question1:
	LDA Question1Tiles,x
	BRA TileFinish
Question2:
	LDA Question2Tiles,x
	BRA TileFinish
Question3:
	LDA Question3Tiles,x
	BRA TileFinish
Question4:
	LDA Question4Tiles,x
	BRA TileFinish
Question5:
	LDA Question5Tiles,x
	BRA TileFinish
Question6:
	LDA Question6Tiles,x
	BRA TileFinish
Question7:
	LDA Question7Tiles,x
	BRA TileFinish
Question8:
	LDA Question8Tiles,x
	BRA TileFinish
Question9:
	LDA Question9Tiles,x
	BRA TileFinish
	
CalculateTile:
	CPX #$00
	BEQ EndPiece
	CPX #NumTiles
	BEQ EndPiece

	PHA
	TXA
	DEC A
	ASL
	ASL
	ASL
	STA $00
	PLA

	SEC
	SBC $00
	BMI EmptyTile
	
	CMP #$08 	
	BCS FullTile

	AND #$07
	CLC
	ADC #$47
	CMP #$4B
	BCC Return
	CLC
	ADC #$0C
Return:	
	RTS

FullTile:
	LDA #$46
	RTS
EmptyTile:
	LDA #$47
	RTS
EndPiece:
	LDA #$56
	RTS
	