;;;;;A.MAP - auto save name loading
;;            check config for save name

  .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

  
LoadConfig:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts
  lda #$00
  sta moduleReady
  
  
  
  
  LDA #'S'  
  sta findEntry   ;;store file name char
  LDA #'A'  
  sta findEntry+1   ;;store file name char
  LDA #'V'  
  sta findEntry+2   ;;store file name char
  LDA #'E'  
  sta findEntry+3   ;;store file name char
  LDA #'S'  
  sta findEntry+4   ;;store file name char
  LDA #' '  
  sta findEntry+5   ;;store file name char
  LDA #' '  
  sta findEntry+6   ;;store file name char
  LDA #' '  
  sta findEntry+7   ;;store file name char

  jmp SaveDirNameDone

  
  
  
  
  ;;;code from here down loads the savedir from config.txt instead of default
  


  lda #'T'
  sta exMatch1+0     ;.TXT
  sta exMatch1+1    
  sta exMatch1+2     
  sta exMatch1+3    

  lda #'X'
  sta exMatch2+0     ;.TXT
  sta exMatch2+1    
  sta exMatch2+2     
  sta exMatch2+3    

  lda #'T'
  sta exMatch3+0     ;.TXT
  sta exMatch3+1    
  sta exMatch3+2     
  sta exMatch3+3    
;-------------------------------------;

;;load powerpak dir (.txt)
  lda baseDirCluster
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3
  jsr CardLoadDir        ; powerpak dir

;;find config.txt
  jsr ClearFindEntry
  lda #'C'
  sta findEntry  
  lda #'O'
  sta findEntry+1       ;;find CONFIG.TXT in powerpak dir
  lda #'N'
  sta findEntry+2
  lda #'F'
  sta findEntry+3
  lda #'I'
  sta findEntry+4
  lda #'G'
  sta findEntry+5


  jsr DirFindEntry       
  lda tempEntry+$1C
  sta sourceCluster
  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
  

;;search for SAVEDIR=
  ldy #$00
FindS:
  lda $0200, y
  cmp #'S'
  bne FindNext
  lda $0201, y
  cmp #'A'
  bne FindNext
  lda $0202, y
  cmp #'V'
  bne FindNext
  lda $0203, y
  cmp #'E'
  bne FindNext
  lda $0204, y
  cmp #'D'
  bne FindNext
  lda $0205, y
  cmp #'I'
  bne FindNext
  lda $0206, y
  cmp #'R'
  bne FindNext
  lda $0207, y
  cmp #'='
  bne FindNext
  jmp SaveDirFound
FindNext:
  iny
  cpy #$00
  bne FindS
SaveDirNotFound:

;;;;;;;;;;;
;  SAVEDIR NOT FOUND
;;;;;;;;;;;;

  jmp NextModule


SaveDirFound:
  lda #$20
  sta searchEntry
  sta searchEntry+1
  sta searchEntry+2
  sta searchEntry+3
  sta searchEntry+4
  sta searchEntry+5
  sta searchEntry+6
  sta searchEntry+7

  jsr ClearFindEntry

  ldx #$00
SaveDirName:
  lda $0208, y
  cmp #$0D      ;;CR = stop
  beq SaveDirNameDone
  cmp #$0A      ;;LF = stop
  beq SaveDirNameDone
  cmp #$20      ;;space = stop
  beq SaveDirNameDone
  cmp #$00      ;;unknown = stop
  beq SaveDirNameDone
  cmp #$FF      ;;unknown = stop
  beq SaveDirNameDone

  sta findEntry, x   ;;store file name char
  iny
  inx
  cpx #$08            ;;only store 8 characters
  bne SaveDirName

SaveDirNameDone:


;;;;;;;;;;;
;  SAVEDIR FOUND
;;;;;;;;;;;;
  lda baseDirCluster
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3
    
  jsr CardLoadDir        ; load POWERPAK dir

  jsr DirFindEntry         ;save dir into tempEntry

  lda tempEntry+$1C
  sta sourceCluster
  lda tempEntry+$1D
  sta sourceCluster+1
  lda tempEntry+$1E
  sta sourceCluster+2
  lda tempEntry+$1F
  sta sourceCluster+3     ;;transfer save dir cluster

  lda #'S'
  sta exMatch1+0     ;.SAV
  sta exMatch1+1    
  sta exMatch1+2     
  sta exMatch1+3    

  lda #'A'
  sta exMatch2+0     ;.SAV
  sta exMatch2+1    
  sta exMatch2+2     
  sta exMatch2+3    

  lda #'V'
  sta exMatch3+0     ;.SAV
  sta exMatch3+1    
  sta exMatch3+2     
  sta exMatch3+3 
     
  jsr CardLoadDir        ; load save dir (.sav files)
    
  ldy #$00
CopyGameName:
  lda gameName, y       ;;copy the game name to search for
  sta searchEntry, y
  iny
  cpy #28
  bne CopyGameName
  
  jsr DirFindEntryLong   ;;do the search
  
  ;;back here means entry found
  jsr DirGetEntry  ;;reload to get full entry name

  ldy #$00
CopySaveName:
  lda tempEntry, y       ;;copy the save name we found
  sta saveName, y
  iny
  cpy #28
  bne CopySaveName
  
  lda tempEntry+$1C     ;;copy save cluster
  sta wramCluster
  lda tempEntry+$1D
  sta wramCluster+1
  lda tempEntry+$1E
  sta wramCluster+2
  lda tempEntry+$1F
  sta wramCluster+3

  jsr LoadBatteryRam

  
NextModule:
  jsr ClearFindEntry
  lda #'O'
  sta findEntry  
  jmp CardLoadModule      ;load options module



  

CONTROLLER:
  rts

  
DirFindEntryLong:
  lda #$00
  sta selectedEntry
  sta selectedEntry+1
    
  lda filesInDir
  bne DirFindEntryLongFiles
  lda filesInDir+1
  bne DirFindEntryLongFiles
  jmp DirFindEntryLongFailed
DirFindEntryLongFiles:

  
  ldy #$00
DirFindEntryLongReplaceNes
  lda searchEntry, y
  cmp #'.'
  bne DirFindEntryLongReplaceNesNext
  lda searchEntry+1, y
  cmp #'N'
  beq DirFindEntryLongReplaceNesFound    ;;found .N
  cmp #'n'
  beq DirFindEntryLongReplaceNesFound    ;;found .n
  
DirFindEntryLongReplaceNesNext:    ;;go to the next character
  iny
  cpy #28
  bne DirFindEntryLongReplaceNes
  jmp DirFindEntryLongReplaceNesDone
  
DirFindEntryLongReplaceNesFound:    ;;found .N or .s, erase it
  lda #$00
  sta searchEntry+1, y                  ;;erase the N/n
  iny
  cpy #28
  beq DirFindEntryLongReplaceNesDone  ;;dont erase past end of file name
  sta searchEntry+1, y                  ;;erase E/e
  iny
  cpy #28
  beq DirFindEntryLongReplaceNesDone  ;;dont erase past end of file name
  sta searchEntry+1, y                  ;;erase S/s
  
DirFindEntryLongReplaceNesDone:

;PrintSearchEntry:
;  ldy #$00
;PrintSearchEntryLoop:
;  lda searchEntry, y
;  sta printString, y
;  iny
;  cpy #$20
;  bne PrintSearchEntryLoop
  ;
;  lda #$0D
;  sta printY
;  jsr PrintLine
  



DirFindEntryLongLoop:
  jsr DirGetEntry
  
  ;;if .sav is in name, change to .nes

  ldy #$00
DirFindEntryLongReplaceSav
  lda tempEntry, y
  cmp #'.'
  bne DirFindEntryLongReplaceSavNext
  lda tempEntry+1, y
  cmp #'S'
  beq DirFindEntryLongReplaceFound    ;;found .S
  cmp #'s'
  beq DirFindEntryLongReplaceFound    ;;found .s
  
DirFindEntryLongReplaceSavNext:    ;;go to the next character
  iny
  cpy #28
  bne DirFindEntryLongReplaceSav
  jmp DirFindEntryLongReplaceSavDone
  
DirFindEntryLongReplaceFound:    ;;found .S or .s, erase it
  lda #$00
  sta tempEntry+1, y                  ;;erase the S/s
  iny
  cpy #28
  beq DirFindEntryLongReplaceSavDone  ;;dont erase past end of file name
  sta tempEntry+1, y                  ;;erase A/a
  iny
  cpy #28
  beq DirFindEntryLongReplaceSavDone  ;;dont erase past end of file name
  sta tempEntry+1, y                  ;;erase V/v
  
DirFindEntryLongReplaceSavDone:
      
  

DirFindEntryLongCompare:
  ;;check if entry matches, only look at first 28 chars
  ldy #$00
DirFindEntryLongCharLoop:
  lda tempEntry, y
  cmp searchEntry, y
  bne DirFindEntryLongNext
  iny
  cpy #28
  bne DirFindEntryLongCharLoop
  
DirFindEntryLongFound:
  rts  ;all 28 chars match

DirFindEntryLongNext:
  clc
  lda selectedEntry
  adc #$01
  sta selectedEntry
  lda selectedEntry+1            ;increment to next entry index
  adc #$00
  sta selectedEntry+1
  
  lda selectedEntry 
  cmp filesInDir
  bne DirFindEntryLongLoop
  lda selectedEntry+1
  cmp filesInDir+1
  bne DirFindEntryLongLoop
  
DirFindEntryLongFailed:
  jmp NextModule

  
  
LoadBatteryRam:

  lda wramCluster
  bne LoadWRamInit
  lda wramCluster+1
  bne LoadWRamInit
  lda wramCluster+2
  bne LoadWRamInit
  lda wramCluster+3
  bne LoadWRamInit
  jmp LoadWRamDone
  
LoadWRamInit:
  lda wramCluster
  sta sourceCluster
  lda wramCluster+1
  sta sourceCluster+1
  lda wramCluster+2
  sta sourceCluster+2
  lda wramCluster+3
  sta sourceCluster+3
  jsr ClusterToLBA

  lda #$80
  sta destBank
  sta PRGBANK

  lda #$00
  sta bankCounter
  sta sectorCounter
  sta destLo               ;reset to beginning of wram
  lda #$60
  sta destHi  
LoadWRamLoop:
  lda #$00
  sta destLo
  jsr CardReadSector    ;get 512 bytes
  lda destHi
  sta temp
  jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  

LoadWRamLoopCheckCluster:
  lda sourceCluster
  cmp #$FF
  bne LoadWRamLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+1
  cmp #$FF
  bne LoadWRamLoopLoadBank  ;cluster != last cluster
  
  lda fat32Enabled
  beq LoadWRamDone   ;if fat 16, done checking
  
LoadWRamLoopCheckCluster32:
  lda sourceCluster+2
  cmp #$FF
  bne LoadWRamLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+3
  and #$0F
  cmp #$0F
  bne LoadWRamLoopLoadBank  ;cluster != last cluster

LoadWRamDone:
  rts  ;cluster = FF FF (fat 16) or 0x0F FF FF FF  (fat 32)

LoadWRamLoopLoadBank:  
  lda temp
  sta destHi
LoadWRamLoopCheckBank:
  lda destHi
  cmp #$80
  bne LoadWRamLoop       ;check if next bank needed, destHi=80
  lda destBank
  clc
  adc #$01
  sta destBank
  sta PRGBANK
  lda #$60
  sta destHi
  jmp LoadWRamLoop  

