;;;;Q = question

;=== Include MemoryMap, VectorTable, HeaderInfo ===
.INCLUDE "mapper.inc"

;=== Global Variables ===
.INCLUDE "variables.asm"

Main:
  SEP #$20  ; 8 bit Accumulator, 16 bit X/Y
  REP #$10 

  wai
  
  stz Joy1Press
  stz Joy1Press+1  ;;reset input buttons  
  stz Joy1New
  stz Joy1New+1

  wai
  
  jsr LoadLast
  
  ClearLine 16
  ClearLine 17
  ClearLine 18
	SetCursorPos 16, 2
	PrintString "Save Battery RAM to CF card?"
	SetCursorPos 17, 16
  
  lda saveCluster
  bne AutoSaveMenu
  lda saveCluster+1
  bne AutoSaveMenu
  lda saveCluster+2
  bne AutoSaveMenu
  lda saveCluster+3
  bne AutoSaveMenu
  
  PrintString "No Auto Save"
  lda #$A0
  sta cursorY
  jmp AutoSaveMenuDone
  
AutoSaveMenu:  
  lda #$90
  sta cursorY
	PrintString "Save to:"
  
  SetCursorPos 18,3
  lda #<saveName
  sta strPtr
  lda #>saveName
  sta strPtr+1
  ldy #strPtr
  PrintString "%s"
AutoSaveMenuDone:
  
	SetCursorPos 19, 16
	PrintString "Choose file..."
  
	SetCursorPos 20, 16
	PrintString "Don't save"
  lda #$78
  sta cursorX
 	SetCursorPos 21,0
  
  
  
ForeverSQ:
  wai
StartCheck:
  lda Joy1New+1
  and #%00010000
  beq StartCheckDone

  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'I'
  sta findEntry+1
  jump jCardLoadModule;START = cancel, load intro module
StartCheckDone:

NorthCheck:
  lda Joy1New+1
  and #%00001000
  beq NorthCheckDone
  jsr PrevButton  ;;up pressed 
NorthCheckDone:

SouthCheck:
  lda Joy1New+1
  and #%00000100
  beq SouthCheckDone
  jsr NextButton  ;;down pressed 
SouthCheckDone:

ABXYCheck:
  lda Joy1New+1
  and #%11000000
  bne ABXYNextModule
  lda Joy1New
  and #%11110000
  bne ABXYNextModule;;ABXYlr = select game
  jmp ABXYCheckDone
ABXYNextModule:
  jsr ChooseButton
ABXYCheckDone:
  jmp ForeverSQ





NextButton:    ;;90 -> A0 -> A8
  lda cursorY
  clc
  adc #$08
  sta cursorY
  cmp #$B0
  bne NextButtonDone
  lda #$90
  sta cursorY
NextButtonDone:
  lda cursorY
  cmp #$98
  bne NextButtonDone2
  lda #$A0
  sta cursorY
NextButtonDone2:
  rts


PrevButton:    ;;A8 -> A0 -> 90
  lda cursorY
  sec
  sbc #$08
  sta cursorY
  cmp #$88
  bne PrevButtonDone
  lda #$A8
  sta cursorY
PrevButtonDone:
  lda cursorY
  cmp #$98
  bne PrevButtonDone2
  lda #$90
  sta cursorY
PrevButtonDone2:
  rts

  
  
  
ChooseButton:
  ;if auto save
  lda cursorY
  cmp #$90
  beq AutoSave
  
  ;if yes, go to sb.map to display .srm files
  lda cursorY
  cmp #$A0
  beq NextModuleSB
  
  ;if no, go to si.map to display intro
  lda cursorY
  cmp #$A8
  bne ChooseButtonDone
  lda #$01
  sta CONFIGWRITESTATUS;;turn off the battery info
  jmp NextModuleSI
ChooseButtonDone:
  rts



AutoSave:
  lda saveCluster
  bne AutoSaveContinue
  lda saveCluster+1
  bne AutoSaveContinue
  lda saveCluster+2
  bne AutoSaveContinue
  lda saveCluster+3
  bne AutoSaveContinue
  rts
  
AutoSaveContinue:
  jsr SaveFile
  
NextModuleSI:
  lda #$FF
  sta SpriteBuf1+17  ;;move title graphics off screen
	sta SpriteBuf1+21
	sta SpriteBuf1+25
	sta SpriteBuf1+29
	sta SpriteBuf2+1  ;;32x32 sprites

  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'I'
  sta findEntry+1
  wai
  jump jPrintClearScreen
  jump jLoadLogo
  jump jLoadRomVersion
  jump jCardLoadModule;load intro module

NextModuleSB:
  lda #$FF
  sta SpriteBuf1+17  ;;move title graphics off screen
	sta SpriteBuf1+21
	sta SpriteBuf1+25
	sta SpriteBuf1+29
	sta SpriteBuf2+1  ;;32x32 sprites

  lda #'I'
  sta nextModule  ;;go to intro when done
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'B'
  sta findEntry+1
  jump jCardLoadModule;load SNES list module






LoadLast:
  ;;load LAST.TXT file
  lda #'T'
  sta exMatch1
  sta exMatch1+1
  sta exMatch1+2
  sta exMatch1+3
  lda #'X'
  sta exMatch2
  sta exMatch2+1
  sta exMatch2+2
  sta exMatch2+3
  lda #'T'
  sta exMatch3
  sta exMatch3+1
  sta exMatch3+2
  sta exMatch3+3

  lda baseDirCluster   ;;powerpak dir start
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3


  jump jClearFindEntry   ;;find LASTSNES.TXT in POWERPAK dir
  lda #'L'
  sta findEntry
  lda #'A'
  sta findEntry+1
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #'S'
  sta findEntry+4
  lda #'N'
  sta findEntry+5
  lda #'E'
  sta findEntry+6
  lda #'S'
  sta findEntry+7

  jump jCardLoadDir    ; powerpak dir
  
  jump jDirFindEntry        ;;get first cluster
  lda tempEntry+$1C
  sta sourceCluster
  lda tempEntry+$1D
  sta sourceCluster+1
  lda tempEntry+$1E
  sta sourceCluster+2
  lda tempEntry+$1F
  sta sourceCluster+3

  lda #<sectorBuffer1
  sta destLo
  lda #>sectorBuffer1
  sta destHi                   ;;put into sector ram
  stz destBank
  stz sectorCounter
  stz bankCounter
  jump jClusterToLBA            ;sourceCluster -> first sourceSector
  lda #kDestWRAM
  sta destType
  jump jCardReadSector          ;sector -> wram
  
  ldx #$0000
  ldy #$0000
LoadLastGameLoop:         ;;game name and cluster
  lda sectorBuffer1, y
  sta gameName, x
  iny
  inx
  cpx #34
  bne LoadLastGameLoop
  
  ldx #$0000
LoadLastSaveLoop:         ;;save name and cluster
  lda sectorBuffer1, y
  sta saveName, x
  iny
  inx
  cpx #34
  bne LoadLastSaveLoop

  rts






SaveFile:
  lda #$FF
  sta cursorX
  lda #$F0
  sta cursorY

  lda #%00000001
  sta CONFIGWRITESTATUS   ;;no more battery flag

  lda saveCluster
  sta sourceCluster
  lda saveCluster+1
  sta sourceCluster+1
  lda saveCluster+2
  sta sourceCluster+2
  lda saveCluster+3      ;copy  save cluster
  sta sourceCluster+3      ; to source cluster

  lda #$00
  sta DMAWRITELO
  sta DMAWRITEHI
  lda #$F8
  sta DMAWRITEBANK
    
  ClearLine 16
  SetCursorPos 16, 0
  PrintString "            Saving..."

  lda #kSourceSDRAM
  sta sourceType

  jump jCardWriteFile
  
  SetCursorPos 16, 0
  PrintString "        Battery RAM saved"
  rts
