;;; Q.MAP   question screen

            ;PowerPak
            
            ; Save to CF card?   
            ;          > Yes
            ;            No

  .inesprg 1 ;01 x 16k prg bank
  .ineschr 0	;no chr bank
  .inesmir 1	; VERTICAL mirroring   
  .inesmap 0        

  .include "variables.h"
  .include "loader.fns"

  .bank 0
  .org $0400
  jmp START
  .org $0404    
  jmp CONTROLLER

  
  
START:
  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo  
  
  jsr LoadLast
  jsr ClearLine
    
  lda #$88
  sta cursorY
  ldx #$10
  ldy #$40  ;;string index
  lda wramCluster
  bne AutoSaveMenu
  lda wramCluster+1
  bne AutoSaveMenu
  lda wramCluster+2
  bne AutoSaveMenu
  lda wramCluster+3
  bne AutoSaveMenu
NoAutoSaveMenu:
  ldy #$30  ;;string index
  lda #$98
  sta cursorY
AutoSaveMenu:
  ldx #$10
  jsr CopyString   ;;autosave line
  lda #$11
  sta printY
  jsr PrintLine
AutoSaveMenuDone:

  jsr ClearLine
  ldx #$00
AutoSaveName:
  lda saveName, x  
  sta printString+1, x
  inx
  cpx #30
  bne AutoSaveName  
  lda #$12
  sta printY
  jsr PrintLine
      
  jsr ClearLine
  ldy #$20
  ldx #$10        ;;choose file line
  jsr CopyString
  lda #$13
  sta printY
  jsr PrintLine
  
  jsr ClearLine
  lda #'N'
  sta printString+$10
  lda #'o'
  sta printString+$11
  lda #$14
  sta printY
  jsr PrintLine

  jsr ClearLine   ;;;save ram line
  ldy #$10
  ldx #$06
  jsr CopyString
 
  lda #$80
  sta mapperData+$0F  ;;save wram chip selected
  
  ;;check for fds header
  lda #$20
  sta PRGBANK      ;;$40 banks, $20 = second half
  lda $6000
  cmp #'F'
  bne QScreenOn  ;;not fds
  lda $6001
  cmp #'D'
  bne QScreenOn  ;;not fds
  lda $6002
  cmp #'S'
  bne QScreenOn  ;;not fds
  
  lda #'F'
  sta printString+$B
  lda #'D'
  sta printString+$C
  lda #'S'
  sta printString+$D 
    
  lda #$20
  sta mapperData+$0F    ;;FDS disk in 2nd half of prg chip

  
  
QScreenOn:
  lda #$10
  sta printY
  jsr PrintLine

  lda #$78
  sta cursorX
    
  lda #$4C
  sta $0404      ;set controller vector to JMP controllers


  jsr RenderingOn
  jmp Forever
 
  
  
  
  



  
  
NextButton:    ;;88 -> -> 98 -> A0
  lda cursorY
  cmp #$A0
  beq NoToYesDown
  jmp MoveDown
NextButtonDone:
  rts

NoToYesDown:
  lda #$80
  sta cursorY  ;;jump to 80, above autosave
MoveDown:
  
  lda scrollYCounter    ;no scrolling while scrolling;
  bne NextButtonDone
  lda cursorYCounter
  bne NextButtonDone
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  lda #$08
  sta cursorYCounter
  lda cursorY
  cmp #$88
  bne MoveDownExtraDone
MoveDownExtra:
  lda #$10
  sta cursorYCounter
MoveDownExtraDone:
  lda #$01
  sta cursorYDown
  lda #$00
  sta cursorYUp
  ;jsr ScrollDown
  jmp NextButtonDone
  
    

PrevButton:      ;;A0 -> 98 -> -> 88  
  lda cursorY
  cmp #$88
  beq YesToNoUp
  jmp MoveUp
PrevButtonDone:
  rts

YesToNoUp:
  lda #$A8
  sta cursorY  ;;jump to A8, below No
MoveUp:
  lda scrollYCounter    ;no scrolling while scrolling
  bne PrevButtonDone
  lda cursorYCounter
  bne PrevButtonDone
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter  
  lda #$08
  sta cursorYCounter
  lda cursorY
  cmp #$98
  bne MoveUpExtraDone
MoveUpExtra:
  lda #$10
  sta cursorYCounter
MoveUpExtraDone:
  lda #$01
  sta cursorYUp
  lda #$00
  sta cursorYDown
  jmp PrevButtonDone
  





ChooseButton:
  ;;if auto save, save file then go to intro
  lda cursorY
  cmp #$88
  beq LoadModuleAutoSave
  
  ;if yes, go to b.map to display .sav/.fds files
  lda cursorY
  cmp #$98
  beq LoadModuleSave
  
  ;if no, go to i.map to display intro         
  lda cursorY
  cmp #$A0
  beq LoadModuleIntro
  rts
  
               
LoadModuleAutoSave:
  lda wramCluster
  bne LoadModuleAutoSaveContinue
  lda wramCluster+1
  bne LoadModuleAutoSaveContinue
  lda wramCluster+2
  bne LoadModuleAutoSaveContinue
  lda wramCluster+3
  bne LoadModuleAutoSaveContinue
  rts
;;
LoadModuleAutoSaveContinue:
  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo
  jsr ClearLine
  ldy #$00
  ldx #$0D
  jsr CopyString
  lda #$13
  sta printY
  jsr PrintLine
  
  lda #$F0
  sta cursorY
  sta cursorX
  
  jsr RenderingOn
  jsr SaveFile
  jmp LoadModuleIntro

;LoadModuleS:
;  lda #'S'         ;;save battery, .sav
;  sta findEntry
;  jmp ChooseButtonDone



LoadModuleSave:
  ;;check for fds header
  lda #$20
  sta PRGBANK      ;;$40 banks, $20 = second half
  lda $6000
  cmp #'F'
  bne LoadModuleB  ;;not fds
  lda $6001
  cmp #'D'
  bne LoadModuleB  ;;not fds
  lda $6002
  cmp #'S'
  bne LoadModuleB  ;;not fds
  jmp LoadModuleD  ;;FDS header found

LoadModuleB:
  jsr ClearFindEntry
  lda #'B'         ;;choose battery, .sav
  sta findEntry
  jmp ChooseButtonDone

LoadModuleD:
  jsr ClearFindEntry
  lda #'D'  
  sta findEntry    ;;save fds, .fds
  jmp ChooseButtonDone

  
LoadModuleIntro:
  jsr ClearFindEntry
  lda #'I'  
  sta findEntry
  jmp ChooseButtonDone




ChooseButtonDone:
;  lda #' '
;  sta findEntry+1
;  sta findEntry+2
;  sta findEntry+3
;  sta findEntry+4
;  sta findEntry+5
;  sta findEntry+6
;  sta findEntry+7
  
  jmp CardLoadModule      ;load options module

  
  
      
CONTROLLER:
  jsr ReadJoystick
  lda joystick
  bne ReadyCheck
  lda #$01
  sta moduleReady
  rts

ReadyCheck:
  lda moduleReady
  bne ReadyCheckDone
  rts
ReadyCheckDone:
  
FPGACheck:
  lda joystick
  and #%00100000    ;;select = test  
  bne FPGACheckDone
  
  lda $2002         ;clear 2006 latch
  lda #$3F
  sta $2006
  lda #$00
  sta $2006
  ldx #$20
WritePaletteBlue:
  lda #$3A    ;;green bg
  sta $2007
  dex
  bne WritePaletteBlue


  
FPGACheckDone

ACheck:
  lda #$00
  sta moduleReady

  lda joystick
  and #$F0
  beq NorthCheck    ;;A B SEL START
  jsr ChooseButton
  jmp CheckDone
  




NorthCheck:
  lda joystick
  and #$0A
  beq SouthCheck
  
  ;handle up/left button
  jsr PrevButton
  jmp CheckDone

  
SouthCheck:
  lda joystick
  and #$05
  beq CheckDone
  
  ;handle down/right button
  jsr NextButton
  
CheckDone:
  rts
  




LoadLast:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts
  lda #$00
  sta moduleReady


  lda #'T'
  sta exMatch1+0     ;.TXT
  sta exMatch3+0     ;.TXT

  lda #'X'
  sta exMatch2+0     ;.TXT

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

  ldy #$04
LoadLastBaseLoop:
  lda baseDirCluster-1, y
  sta sourceCluster-1, y
  dey
  bne LoadLastBaseLoop
;  lda baseDirCluster+1
;  sta sourceCluster+1
;  lda baseDirCluster+2
;  sta sourceCluster+2
;  lda baseDirCluster+3
;  sta sourceCluster+3
  jsr CardLoadDir        ; powerpak dir

  jsr ClearFindEntry
  lda #'L'
  sta findEntry  
  lda #'A'
  sta findEntry+1       ;;find LASTNES.TXT in powerpak dir
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #'N'
  sta findEntry+4       ;;find LASTNES.TXT in powerpak dir
  lda #'E'
  sta findEntry+5
  lda #'S'
  sta findEntry+6


  jsr DirFindEntry   
      
  ldy #$04    
LoadLastCopyLoop:      
  lda tempEntry+$1C-1, y
  sta sourceCluster-1, y
  dey
  bne LoadLastCopyLoop
  
  ;lda tempEntry+$1D
  ;sta sourceCluster+1
  ;lda tempEntry+$1E
  ;sta sourceCluster+2
  ;lda tempEntry+$1F
  ;sta sourceCluster+3      ;copy to source cluster
  
  lda #$00
  sta destLo             ;;load first sector into ram buffer
  lda #$02
  sta destHi
  jsr ClusterToLBA
  jsr CardReadSector
    
  ldx #$00
  ldy #$00
LoadLastSaveLoop:         ;;save name 
  lda $0224, y
  sta saveName, x
  iny
  inx
  cpx #32
  bne LoadLastSaveLoop

  ldx #$00
LoadLastSaveCLoop:         ;;save cluster 
  lda $0224, y
  sta wramCluster, x
  iny
  inx
  cpx #4
  bne LoadLastSaveCLoop
  
  rts





SaveFile:
;;load wram cluster into destcluster
  lda wramCluster
  bne SaveFileInit
  lda wramCluster+1
  bne SaveFileInit
  lda wramCluster+2
  bne SaveFileInit
  lda wramCluster+3
  bne SaveFileInit
  jmp SaveFileDone
  
SaveFileInit:

  ldy #$04
SaveFileInitLoop:
  lda wramCluster-1, y
  sta sourceCluster-1, y
  dey
  bne SaveFileInitLoop
  ;lda wramCluster+1
  ;sta sourceCluster+1
  ;lda wramCluster+2
  ;sta sourceCluster+2
  ;lda wramCluster+3
  ;sta sourceCluster+3

  jsr ClusterToLBA
  
  lda mapperData+$0F   ;;fds or wram
  sta PRGBANK
  sta destBank

SaveFileInitDone:
  lda #$00
  sta bankCounter
  sta sectorCounter
  sta sourceLo               ;reset to beginning of wram
  lda #$60
  sta sourceHi  
SaveFileLoop:
  lda #$00
  sta sourceLo
  jsr CardWriteSector    ;write 512 bytes
  lda sourceHi
  sta temp
  jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  

SaveFileLoopCheckCluster16:
  lda sourceCluster
  cmp #$FF
  bne SaveFileLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+1
  cmp #$FF
  bne SaveFileLoopLoadBank  ;cluster != last cluster
  
  lda fat32Enabled
  beq SaveFileDone   ;if fat 16, done checking
  
SaveFileLoopCheckCluster32:
  lda sourceCluster+2
  cmp #$FF
  bne SaveFileLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+3
  and #$0F
  cmp #$0F
  bne SaveFileLoopLoadBank  ;cluster != last cluster
  
  jmp SaveFileDone  ;fat 32, done checking
  
    
SaveFileLoopLoadBank:     
  lda temp
  sta sourceHi
SaveFileLoopCheckBank:
  lda sourceHi
  cmp #$80
  bne SaveFileLoop       ;check if next bank needed, destHi=80
  
  ;go to next bank
  ;reset sourceHi
  ;jmp SaveWRamLoop
  lda destBank
  clc
  adc #$01
  sta destBank
  sta PRGBANK
  lda #$60
  sta sourceHi
  jmp SaveFileLoop
  
  
  
SaveFileDone:
  rts




CopyString:
  lda #$10
  sta temp
CopyStringLoop:
  lda Strings, y
  sta printString, x
  inx
  iny
  dec temp
  lda temp
  bne CopyStringLoop
  rts
  

Strings:
  ;    12345678901234567890123456789012
Strings00:  .db "Saving...       "
Strings10:  .db "Save RAM to CF? "
Strings20:  .db "Choose File...  "
Strings30:  .db "No AutoSave     "
Strings40:  .db "AutoSave to:    "

  
ReadJoystick:
  ; Strobe controller
  lda #$01
  sta $4016
  lda #$00
  sta $4016
  sta joystick 
   
  ; Read all 8 buttons
  ldy #$02
joysetup:
  ldx #$08
joyloop1:
  pha
   
  ; Read next button state and mask off low 2 bits.
  ; Compare with $01, which will set carry flag if
  ; either or both bits are set.
  lda $4015, y    ;;4017, 4016
  and #$03
  cmp #$01
   
  ; Now, rotate the carry flag into the top of A,
  ; land shift all the other buttons to the right
  pla
  rol a
   
  dex
  bne joyloop1   ;;read 8 buttons
  ora joystick
  sta joystick   ;;save buttons
  dey
  bne joysetup   ;;read other controller
  rts


  