;The name in the VMI at $58 MUST be SONIC2____VM
;Otherwise, SA2 will not write a chao.

;Variables
;----------------------------------------------
;Don't know when or why the flash bank wouldn't be 0
;But safety is good

p3_pressed              =       $4      ; 1 Byte (For LibKCommon)
p3_last_input           =       $5      ; 1 Byte (For LibKCommon)
chaobyte		=	$6	;To check if there is a chao in slot 1
Step			=	$7	;Where to go next
VerifyResult		=	$8	;Result of verification
FlashBank		=	$7D	;7D must be used to set flash bank
FlashAddress1		=	$7E	;First byte of flash address must be set here
FlashAddress2		=	$7F	;Second byte of flash address must be set here

;Constants
;----------------------------------------------

T_BTN_SLEEP	equ	7	;These are all used to determine which bit in the value is 1
T_BTN_MODE	equ	6	;is bit 6 1
T_BTN_B1	equ	5	;is bit 5 1
T_BTN_A1	equ	4	;and so on
T_BTN_RIGHT1	equ	3	
T_BTN_LEFT1	equ	2
T_BTN_DOWN1	equ	1
T_BTN_UP1	equ	0

;Prepare application
;----------------------------------------------
	.org	$00
	jmpf start

	.org    $03
	reti

	.org    $0b
	reti

	.org    $13
	reti

	.org    $1b
	jmpf    t1int

	.org    $23
	reti

	.org    $2b
	reti	

	.org    $33
	reti

	.org    $3b
	reti	

	.org    $43
	reti

	.org    $4b
	clr1    p3int,0
	clr1 p3int,1
	reti

	.org    $130
t1int:
	push ie
	clr1 ie,7
	not1	ext,0
	jmpf	t1int
	pop	ie
	reti

;-----------------------------------------------------------------------------

	.org $105	;Application will go here after BIOS flash write call
	jmpf StepCheck

	.org $115	;After BIOS flash verify call
	jmpf StepCheck

	.org $125 	;After BIOS flash read call
	jmpf StepCheck

;CHAO HEADER: THIS NEEDS TO BE HERE FOR SONIC ADVENTURE 2 TO READ/WRITE CHAO
;-----------------------------------------------------------------------------
;The data slots pointed to need to be at addresses evenly divisible by $200.
;Slot 1 [chao] must be before slot 2 [egg].
;Simplest solution is to just put slot 1, 2, and items one after another.

	.org $1E0
	.byte "CHAO2ACCEPT"		;String for SA2 to recognize chao
	.byte $00, $00, $00, $00, $00
	.byte $00, $0E			;Pointer to chao slot 1 at E00
	.byte $00, $12			;Pointer to chao slot 2 at 1200
	.byte $00, $16			;Pointer to inventory at 1600
	.byte $00, $18			;Pointer to CA2 "story data" at 1800
	.byte $00, $18			;Pointer to CA2 "remaining data" at 1800
;Although we don't use this Chao Adventure 2 data, we must point to where it could go.
;Otherwise, a chao cannot be dropped off into this program.
	.byte $00, $00, $00
	.byte $01			;Language setting, 01 is English
	.byte $00			;If NOT 00, VMS file is deleted after chao withdrawn
	.byte $00			;"Empty file" if not 00?

;Header
;----------------------------------------------	
	.org $200
	.text 16 "SA2 BLUEINATOR"			;Short name
	.text 32 "SA2 CHAO BLUEINATOR"			;Long name
	.string 16 "me"			
	.include icon "./blue2.png"			;Waterbear generates icon

;Required files
;----------------------------------------------
.include "./lib/libperspective.asm" 
.include "./lib/libkcommon.asm"
.include "./lib/sfr.i"

;Main program
;----------------------------------------------
start:	;Leave this as-is
	clr1 ie,7
	mov #$A1,ocr	;Set to 81 before writes to set clock to 1/6 RC
	mov #$09,mcr
	mov #$80,vccr
	clr1 p3int,0
	clr1 p1,7
	mov #$ff,p3
	set1 ie,7

;Initialize variables

	mov #$FF, p3	;No buttons pressed
	mov #$00, Step	;Current step is 0 when first loading the program

;Since each BIOS call jumps to locations that cannot be changed, we need to keep track of where we just were so that we can jump to the correct location after each BIOS call.
;The variable Step is used for this.
;StepCheck will then send us to the correct location in the program after each BIOS call.
StepCheck:
	;We need to store the value of the acc register to a variable right away.
	;This is because we need to use the acc register right after verifying writes.
	st VerifyResult			;Store acc register to VerifyResult
	ld Step				;Store current step in acc register
	be #$00, JumpToFirstRead	;If starting the program, go to JumpToFirstRead
	be #$01, JumpToCheckByte	;If from FirstRead, go to JumpToCheckByte
	be #$02, JumpToWrite		;If from ReadJewel, go to JumpToWrite
	be #$03, JumpToVerStart		;If from WriteJewel, go to JumpToVerStart
	be #$04, JumpToVerCheck		;If from VerJewelStart, go to JumpToVerCheck
	jmpf Error			;I sure hope nothing else happened

JumpToFirstRead:
	jmpf FirstRead		;jmpf can "reach" farther than be

JumpToCheckByte:
	jmpf CheckByte	;If be can't reach target label, be to a label to jmpf to the target

JumpToWrite:
	jmpf Write

JumpToVerStart:
	jmpf VerStart

JumpToVerCheck:
	jmpf VerCheck

;PART 1: Loading to prepare to check the chao slot
	
FirstRead:	;Check on startup if there is a chao in slot 1
	;E00-E80 in flash will be loaded to 80-FF in RAM.
	;The first byte in the chao slot is what kind of chao is present.
	;If it's 00, there is no chao and we will go to the NO CHAO screen.
	;Otherwise, we will proceed to the title screen.
	mov #$01, Step		;Step is 1, go to CheckByte after this
	mov #$00, FlashBank	;Set flash bank to 0
	mov #$0E, FlashAddress1	;Reading from E00 in flash
	mov #$00, FlashAddress2	;Reading from E00 in flash
	not1 ext, 0		;Jump to the specified BIOS function
	jmpf $120		;$120 in BIOS is flash read call

;PART 2: Checking if there is a chao

CheckByte:
	ld $80				;First byte of the chao slot is at $80 now, load to acc
	be #$00, NoChaoStart		;If $80 is 00, there is no chao
	jmpf TitleStart			;Otherwise, go to the title screen

NoChaoStart:
	P_Draw_Background_Constant NoChaoImg
	P_Blit_Screen
NoChaoLoop:
	callf Get_Input
	ld p3			;Need to get inputs to enable sleep and mode
	jmpf NoChaoLoop		;Trapped forever... go get a chao!

TitleStart:
	P_Draw_Background_Constant TitleImg
	P_Blit_Screen
TitleLoop:
	callf Get_Input
	ld p3				;Load button input to acc register
	bp acc, T_BTN_A1, TitleLoop	;If this bit is 1, A is not pressed, loop
	jmpf Read			;Otherwise A is pressed, read section of flash

;PART 3: Reading the chao's color and texture bytes
;The bytes for the chao's color and texture are at A7 and A8 in the chao slot.
;We need the data we're not editing in this section in RAM.
;Otherwise, the data we are not trying to edit will be wiped.

Read:
	mov #$02, Step		;Step is 2, go to Write after this
	mov #$0E, FlashAddress1	;Reading from E80 in flash
	mov #$80, FlashAddress2	;Reading from E80 in flash
	not1 ext, 0		;Jump to the specified BIOS function
	jmpf $120		;$120 in BIOS is flash read call

;PART 4: Writing the new values for the chao's color and texture
;We edit these values in RAM, and write the altered data that is now in RAM to that section.

Write:
	;Chao slot addresses and RAM addresses line up here by coincidence.
	;Flash read/write is in chunks of 128 bytes, $80 aligned.
	;If we were editing at the E00-E7F chunk instead of the E80-CFF chunk...
	;mov $14, $A7 would be writing to $27 in the chao slot.
	mov #$03, Step			;Step is 3, go to VerStart next
	mov #$14, $A7			;Write color to be BLUE at A7 in chao slot
	mov #$00, $A8			;*unjewels your chao* at A8 in chao slot
	mov #$81, ocr			;Set to 81 before writes to set clock to 1/6 RC
	not1 ext, 0			;Jump to the specified BIOS function
	jmpf $100			;$100 in BIOS is flash write call

;STEP 5: Verifying the flash write
;If something went wrong, we will go to an error screen instead of the CHAO IS BLUE screen.
;This BIOS call checks if what is in RAM matches what is in flash.
;If it does, acc is 00. Otherwise, it's another value.

VerStart:
	mov #$04, Step			;Step is 4, go to VerCheck after this
	not1 ext, 0			;Jump to the specified BIOS function
	jmpf $110			;$110 in BIOS is flash verify call

VerCheck:
	ld VerifyResult		;Load VerifyResult into acc register
	be #$00, ChaoIsBlue	;If verification passes, go to ChaoIsBlue
	jmpf Error		;Otherwise, uh oh

ChaoIsBlue:
	mov #$A1,ocr	;No need to waste battery on a screen we never leave
	P_Draw_Background_Constant ChaoIsBlueImg
	P_Blit_Screen
WOOHOO:
	callf Get_Input
	ld p3			;Need to get inputs to enable sleep and mode
	jmpf WOOHOO

Error:
	mov #$A1,ocr	;No need to waste battery on a screen we never leave
	P_Draw_Background_Constant ErrorImg
	P_Blit_Screen
UhOh:
	callf Get_Input
	ld p3		;Need to get inputs to enable sleep and mode
	jmpf UhOh

goodbye:	
	not1 ext,0
	jmpf $1F0

;Include images
;----------------------------------------------
NoChaoImg:
	.include sprite "./nochaoimg.png"

TitleImg:
	.include sprite "./titleimg.png"

ChaoIsBlueImg:
	.include sprite "./chaoisblueimg.png"

ErrorImg:
	.include sprite "./error.png"

;Include empty chao slots
;----------------------------------------------
	.org $E00
	.include bytes "2EMPTY"		;It's a bunch of 00s for an empty chao slot
	.org $1200
	.include bytes "2EMPTY"		;Empty: The exciting sequel [this is the egg slot]
	.org $1600
	.include bytes "items"		;A stunning lack of items
	.org $1800
	.include bytes "filler"		;If this isn't here, things don't work
;More specifically, SA2 NEEDS space to write $1200 bytes of data.
;If this isn't here, it will refuse to write a chao.
;It also wants to write $6000 more bytes. Thankfully, if there isn't enough space left to do this, it will just give up and write the chao anyway.
;We'd normally pad to fill in the last block here, but the filler data should finish it evenly.
	;.cnop	0,$200		;Pad to fill block