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

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

Main:
  SEP #$20     ; 8 bit Accumulator, 16 bit X/Y
  REP #$10
  
  stz Joy1Press
  stz Joy1Press+1  ;;reset input buttons  
  stz Joy1New
  stz Joy1New+1

  lda #'S'         ;;;SMC  SWC  SFC   files
  sta ex5Match1
  sta ex5Match1+1
  sta ex5Match1+2
  lda #'M'
  sta ex5Match2
  lda #'W'
  sta ex5Match2+1
  lda #'F'
  sta ex5Match2+2
  lda #'C'
  sta ex5Match3
  sta ex5Match3+1
  sta ex5Match3+2

  lda #'G'         ;;GD3 files
  sta ex5Match1+3
  lda #'D'
  sta ex5Match2+3
  lda #'3'
  sta ex5Match3+3


  lda #'S'         ;;SPC files
  sta ex5Match1+4
  lda #'P'
  sta ex5Match2+4
  lda #'C'
  sta ex5Match3+4
  

  lda rootDirCluster
  sta sourceCluster
  lda rootDirCluster+1
  sta sourceCluster+1
  lda rootDirCluster+2
  sta sourceCluster+2
  lda rootDirCluster+3
  sta sourceCluster+3
  
  

MainLoad:  
	jump jPrintClearScreen
  SetCursorPos 0 0
  jsr CardLoadDir    ; first dir
  jump jDirPrintDir

  stz Joy1New
  stz Joy1New+1  ;;reset input buttons  
  stz Joy1Press
  stz Joy1Press+1
    
Forever:
  wai
  
;  SetCursorPos 10 10
;  PrintHexNum cursorY
  
UpCheck:
  lda Joy1Press+1
  and #%00001000
  beq UpDone
UpPressed:
  lda cursorYCounter
  bne UpDone
  lda scrollYCounter
  bne UpDone
UpScroll:
  lda #$01
  sta speedScroll
  lda #$08
  sta speedCounter
  jump jScrollUp
UpDone:

DownCheck:
  lda Joy1Press+1
  and #%00000100
  beq DownDone
DownPressed:
  lda cursorYCounter
  bne DownDone
  lda scrollYCounter
  bne DownDone
DownScroll:
  lda #$01
  sta speedScroll
  lda #$08
  sta speedCounter
  jump jScrollDown
DownDone:

LeftCheck:
  lda Joy1Press+1
  and #%00000010
  beq LeftDone
LeftPressed:
  lda cursorYCounter
  bne LeftDone
  lda scrollYCounter
  bne LeftDone
LeftScroll:
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  jump jScrollUp
LeftDone:

RightCheck:
  lda Joy1Press+1
  and #%00000001
  beq RightDone
RightPressed:
  lda cursorYCounter
  bne RightDone
  lda scrollYCounter
  bne RightDone
RightScroll:
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  jump jScrollDown
RightDone:

LCheck:
  lda Joy1Press
  and #%00100000
  beq LDone
LPressed:
  lda cursorYCounter
  bne LDone
  lda scrollYCounter
  bne LDone
LScroll:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  jump jScrollUp
LDone:

RCheck:
  lda Joy1Press
  and #%00010000
  beq RDone
RPressed:
  lda cursorYCounter
  bne RDone
  lda scrollYCounter
  bne RDone
RScroll:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  jump jScrollDown
RDone:



ABXYCheck:
  lda Joy1New+1
  and #%11000000
  bne ABXYNextModule
  lda Joy1New
  and #%11000000
  bne ABXYNextModule      ;;ABXY = select game
  jmp ABXYCheckDone
ABXYNextModule:
  jsr NextModule
ABXYCheckDone:



StartCheck:
  lda Joy1New+1
  and #%00010000
  beq StartDone
  jmp NextModuleSI     ;;;START = cancel, go back to intro
StartDone: 

  
SelectCheck:        ;;SELECT = verify loaded file
  lda Joy1New+1
  and #%00100000
  beq SelectDone
  
  jump jDirGetEntry       ;get selected entry
  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 DMAWRITELO
  lda #$FE
  sta DMAWRITEHI
  lda #$FF
  sta DMAWRITEBANK   ;;read file so dma wraps, erasing header
  
  stz sectorCounter
  stz bankCounter

  jsr CardVerifyFile
SelectDone:
  jmp Forever

  
  
  
  
  
  
NextModule:
  jump jDirGetEntry       ;get selected entry
  
  lda tempEntry+$1B
  cmp #$01  
  bne NextModuleLoad
  jsr NextDir
  rts
  

NextModuleLoad:
  ldy #$00
CopyGameNameLoop:
  lda tempEntry, y
  sta gameName, y
  iny
  cpy #$1C               ;copy 28 chars
  bne CopyGameNameLoop
  
  stz gameName+$1C     ;;null terminate game name
  
  lda tempEntry+$1C
  sta gameCluster
  sta sourceCluster
  lda tempEntry+$1D
  sta gameCluster+1
  sta sourceCluster+1
  lda tempEntry+$1E
  sta gameCluster+2
  sta sourceCluster+2
  lda tempEntry+$1F
  sta gameCluster+3      ;copy to game cluster
  sta sourceCluster+3      ;copy to source cluster
  
  
  lda #<sectorBuffer1
  sta destLo
  lda #>sectorBuffer1
  sta destHi                   ;;put first sector into sector ram
  stz destBank
  stz sectorCounter
  stz bankCounter
  jump jClusterToLBA            ;sourceCluster -> first sourceSector
  lda #kDestWRAM
  sta destType
  jump jCardReadSector          ;sector -> wram

  lda sectorBuffer1+$05
  cmp #'S'
  bne NextModuleSO        ;;check for SPC
  lda sectorBuffer1+$06
  cmp #'P'
  bne NextModuleSO        ;;check for SPC
  lda sectorBuffer1+$07
  cmp #'C'
  bne NextModuleSO        ;;check for SPC
  jmp NextModuleSP        ;;SPC found, go to player
  
NextModuleSO:
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'A'
  sta findEntry+1
  jump jCardLoadModule      ;load auto save loading module
  
NextModuleSI:
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'I'
  sta findEntry+1
  wai
  jump jPrintClearScreen
  jump jLoadLogo
  jump jLoadRomVersion
  jump jCardLoadModule      ;return to options or save module
  
NextModuleSP:
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'P'
  sta findEntry+1
  jump jCardLoadModule      ;load SPC PLAYER module
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
CardVerifyFile:                 ;;sourceCluster already set  
  wai
  ClearLine 20
  ClearLine 21
  ClearLine 22
	SetCursorPos 20, 1
  PrintString "Verifying File\n"
  
  lda #$00
  sta bankCounter
  sta bankOffset
  sta bankOffset+1

  lda #$00
  sta DMAWRITEBANK
  sta DMAWRITELO
  sta DMAWRITEHI

  lda #$00
  sta gameSize
  sta gameSize+1
  jump jClusterToLBA            ;sourceCluster -> first sourceSector

CardVerifyFileLoop:
  clc
  lda gameSize
  adc #$01
  sta gameSize
  lda gameSize+1
  adc #$00
  sta gameSize+1            ;;keep track of game size


  jsr CardVerifySector

  lda destHi
  pha
  lda destLo
  pha
  jump jLoadNextSectorNum
  pla 
  sta destLo
  pla 
  sta destHi

CardVerifyFileLastSectorCheck:  
  lda #$0F
  sta temp
  lda #$FF
  sta temp+1                            ;;FAT32 last cluster = 0x0FFFFFFF
  sta temp+2
  sta temp+3

  lda fat32Enabled
  cmp #$01
  beq CardVerifyFileLastSectorCheck2        ;;if FAT32, last cluster mask done
  
  lda #$00
  sta temp
  sta temp+1                             ;;FAT16 last cluster = 0x0000FFFF
  
CardVerifyFileLastSectorCheck2:
  ;check if cluster = last cluster
  ; if match, jmp to last entry found
  lda temp
  cmp sourceCluster+3
  bne CardVerifyFileNextSector
  lda temp+1
  cmp sourceCluster+2
  bne CardVerifyFileNextSector
  lda temp+2
  cmp sourceCluster+1
  bne CardVerifyFileNextSector
  lda temp+3
  cmp sourceCluster+0
  bne CardVerifyFileNextSector
  jmp CardVerifyFileDone

CardVerifyFileNextSector:
  inc destHi
  inc destHi
;  inc bankCounter
;  lda bankCounter
;  cmp #$00                 ;;only load 256x512=128KB for modules
;  beq CardVerifyFileDone


  jmp CardVerifyFileLoop

CardVerifyFileDone:
  PrintString "Verify Passed"
  rts










CardVerifySector:
  ;PrintHexNum bankCounter
;	PrintString "jCardWaitNotBusy\n"
  jump jCardWaitNotBusy
  ;PrintHexNum bankCounter
;	PrintString "jCardWaitReady\n"
  jump jCardWaitReady
  ;PrintHexNum bankCounter
;	PrintString "jCardCheckError\n"
  jump jCardCheckError
  ;PrintHexNum bankCounter
;	PrintString "jCardLoadLBA\n"
  jump jCardLoadLBA

  lda #$00
  sta sourceBytes16
  lda #$02
  sta sourceBytes16+1
  
;  PrintHexNum bankCounter
;	PrintString "jCardWaitNotBusy\n"
  jump jCardWaitNotBusy
;  PrintHexNum bankCounter
;	PrintString "jCardWaitReady\n"
  jump jCardWaitReady
;  PrintHexNum bankCounter
;	PrintString "jCardCheckError\n"
  jump jCardCheckError
  
  lda #$20
  sta CARDCOMMAND           ;send card read sector command
  nop
  nop
 ; jump jCardWaitNotBusy
 ; jump jCardWaitReady
;  PrintHexNum bankCounter
;	PrintString "jCardCheckError\n"
  jump jCardCheckError
;  PrintHexNum bankCounter
;	PrintString "jCardWaitDataReq\n"
  jump jCardWaitDataReq
  
  jsr CardVerifyBytesToSDRAM
    
CardVerifySectorCheckDone:
;  PrintHexNum bankCounter
;	PrintString "jCardCheckError\n"
  jump jCardCheckError
CardVerifySectorDone:
  rts  




CardVerifyBytesToSDRAM:         ;use DMA, no increment source or dest
  ldx sourceBytes16
CardVerifyBytesToSDRAMLoop:
  lda $21FF
  sta errorCode
  lda CARDDATAREAD  ;;READ DATA BYTE
  sta errorCode+1
  cmp errorCode
  bne CardVerifyBytesToSDRAMLoopError

CardVerifyBytesToSDRAMLoopNext:
  lda bankOffset
  clc
  adc #$01
  sta bankOffset
  lda bankOffset+1
  adc #$00
  sta bankOffset+1
  lda bankCounter
  adc #$00
  sta bankCounter

  dex
  bne CardVerifyBytesToSDRAMLoop
  rts

CardVerifyBytesToSDRAMLoopError:
  phx
;	SetCursorPos 21, 0
  PrintString "Verify Error\n wanted $"
  PrintHexNum errorCode+1
  PrintString " read $"
  PrintHexNum errorCode
  PrintString " @$"
  PrintHexNum bankCounter
  PrintHexNum bankOffset+1
  PrintHexNum bankOffset
  PrintString "\n"
CardVerifyError:
;  wai
;  jmp CardVerifyError
  plx
  jmp CardVerifyBytesToSDRAMLoopNext





;dest = sector dest  0200
;sourceEntry = where to get entry in 0200-0400, when overflow, load next sector
;destBank =  which bank to select in prg ram
;destEntry = where to put entry in prg ram 6000-7FFF, when overflow, increment bank
CardLoadDir:  
  lda #>sectorBuffer1
  sta destHi                   ; sourceSector1 = where to put sector, where to read entry
  sta sourceEntryHi
  lda #<sectorBuffer1
  sta destLo
  sta sourceEntryLo
  stz destBank
  stz sourceEntryBank
  
  
  stz destEntryHi
  stz destEntryLo
  lda #$7F
  sta destEntryBank            ;$7F0000 - where to put entry


  stz filesInDir
  stz filesInDir+1
  stz sectorCounter
  stz temp
  stz temp+1
  stz selectedEntry
  stz selectedEntry+1
  stz lfnFound
    
  jump jClusterToLBA            ; sourceCluster -> first sourceSector
  
  lda fat32Enabled
  cmp #$01
  beq CardLoadDirReadSector   ; FAT32 go to read sector
  
  lda sourceCluster            ;FAT16  check if trying to load root dir
  cmp rootDirCluster
  bne CardLoadDirReadSector
  lda sourceCluster+1
  cmp rootDirCluster+1
  bne CardLoadDirReadSector
  lda sourceCluster+2
  cmp rootDirCluster+2
  bne CardLoadDirReadSector 
  lda sourceCluster+3
  cmp rootDirCluster+3
  bne CardLoadDirReadSector  
  

  sec
  lda clusterBeginLBA
  sbc fat16RootSectors          ; FAT16 sourceSector = root dir first sector =>  clusterLBABegin(4) - fat16RootSectors(1)
  sta sourceSector
  lda clusterBeginLBA+1
  sbc #$00
  sta sourceSector+1
  lda clusterBeginLBA+2
  sbc #$00
  sta sourceSector+2  
  lda clusterBeginLBA+3
  sbc #$00
  sta sourceSector+3
  
  
      
CardLoadDirReadSector:  
  lda #kDestWRAM
  sta destType
  jump jCardReadSector          ; put into dest
  jump jCardLoadDirClearEntryName   ;clear entry name

CardLoadDirLoop:
  ldy #$0000
  lda [sourceEntryLo], y            ; if name[0] = 0x00, no more entries
  cmp #$00
  beq CardLoadDirLastEntryFound
  jmp CardLoadDirCheckUnused
    
CardLoadDirLastEntryFound:
  rts

CardLoadDirCheckUnused:
  ldy #$0000
  lda [sourceEntryLo], y
  cmp #$E5                     ; if name[0] = 0xE5, entry unused, skip
  bne CardLoadDirCheckLongName
  lda #$00
  sta lfnFound
  jmp CardLoadDirNextEntry



CardLoadDirCheckLongName:
  ldy #$000B
  lda [sourceEntryLo], y            ; if flag = %00001111, long file name entry found
  and #$0F
  cmp #$0F
  bne CardLoadDirCheckShortName
  
  ldy #$0000
  lda [sourceEntryLo], y
  and #%10111111                 ;mask off "last entry" bit
  cmp #$01                     ; if index = 1 or 2, load name    //CHANGE ME  INDEX < 10
  beq CardLoadDirLongName1      ; else skip entry
  cmp #$02
  beq CardLoadDirLongName2
  
  jump jCardLoadDirClearEntryName
  
  jmp CardLoadDirNextEntry
  
  
      ;//CHANGE ME  USE MULTIPLY13 FUNCTION
CardLoadDirLongName1:
  ldx #$0000                     ; index 1 = chars 0-12
  jmp CardLoadDirLongName
CardLoadDirLongName2:
  ldx #$000D                     ; index 2 = chars 13-25
  jmp CardLoadDirLongName
  
  
  
CardLoadDirLongName:
  lda #$00
  sta tempEntry+$20                 ;use as char counter    //CHANGE ME
  ldy #$0001
CardLoadDirLongNameLoop1:
  lda [sourceEntryLo], y             ; loop thro 5 chars   1x3x5x7x9x
  cmp #$FF
  beq CardLoadDirLongNameLoop1FF
  sta tempEntry, x
CardLoadDirLongNameLoop1FF:
  inx
  iny
  iny
  inc tempEntry+$20                 ;use as char counter      //CHANGE ME
  lda tempEntry+$20                 ;use as char counter      //CHANGE ME
  cmp #$05
  bne CardLoadDirLongNameLoop1

  ldy #$000E
CardLoadDirLongNameLoop2:
  lda [sourceEntryLo], y             ; loop thro 6 chars   Ex10x12x14x16x18x
  cmp #$FF
  beq CardLoadDirLongNameLoop2FF  
  sta tempEntry, x
CardLoadDirLongNameLoop2FF:
  inx
  iny
  iny
  inc tempEntry+$20                 ;use as char counter      //CHANGE ME  
  lda tempEntry+$20                 ;use as char counter    //CHANGE ME
  cmp #$0B
  bne CardLoadDirLongNameLoop2
    
  ldy #$001C
CardLoadDirLongNameLoop3:
  lda [sourceEntryLo], y             ;  thro 2 chars   1Cx1Ex
  cmp #$FF
  beq CardLoadDirLongNameLoop3FF  
  sta tempEntry, x
CardLoadDirLongNameLoop3FF:
  inx
  iny
  iny
  lda [sourceEntryLo], y
  cmp #$FF
  beq CardLoadDirLongNameLoop4FF  
  sta tempEntry, x
CardLoadDirLongNameLoop4FF:

  lda #$01
  sta lfnFound
  jmp CardLoadDirNextEntry
  
CardLoadDirCheckShortName:
  ldy #$000B
  lda [sourceEntryLo], y                 ;if flag = volume id, skip
  and #$08
  cmp #$08
  bne CardLoadDirCheckHidden
  jump jCardLoadDirClearEntryName
  lda #$00
  sta lfnFound
  jmp CardLoadDirNextEntry


CardLoadDirCheckHidden:
  ldy #$000B
  lda [sourceEntryLo], y
  and #$02
  cmp #$02                                ; if flag = 0x02, hidden, skip
  bne CardLoadDirCheckDir
  lda #$00
  sta lfnFound
  jump jCardLoadDirClearEntryName           ;clear entry name  
  jmp CardLoadDirNextEntry
  
      
CardLoadDirCheckDir:
  ldy #$000B
  lda [sourceEntryLo], y                 ;if flag = directory, load entry
  and #$10
  cmp #$10
  bne CardLoadDirCheckEx1
  lda #$01
  sta tempEntry+$1B                      ;$1B = dir flag  //CHANGE ME
  jmp CardLoadDirShortName

CardLoadDirCheckEx1:
  ldx #$0000
  ldy #$0008
CardLoadDirCheckEx1Loop:
  lda [sourceEntryLo], y
  cmp ex5Match1, x
  beq CardLoadDirCheckEx2
  inx
  cpx #$0005
  bne CardLoadDirCheckEx1Loop
  lda #$00
  sta lfnFound
  jump jCardLoadDirClearEntryName    
  jmp CardLoadDirNextEntry          ;if extension doesnt match, skip

CardLoadDirCheckEx2:
  ldx #$0000
  ldy #$0009
CardLoadDirCheckEx2Loop:
  lda [sourceEntryLo], y
  cmp ex5Match2, x
  beq CardLoadDirCheckEx3
  inx
  cpx #$0005
  bne CardLoadDirCheckEx2Loop
  lda #$00
  sta lfnFound
  jump jCardLoadDirClearEntryName
  jmp CardLoadDirNextEntry          ;if extension doesnt match, skip

CardLoadDirCheckEx3:
  ldx #$0000
  ldy #$000A
CardLoadDirCheckEx3Loop:
  lda [sourceEntryLo], y
  cmp ex5Match3, x
  beq CardLoadDirShortName
  inx
  cpx #$0005
  bne CardLoadDirCheckEx3Loop
  lda #$00
  sta lfnFound
  jump jCardLoadDirClearEntryName
  jmp CardLoadDirNextEntry          ;if extension doesnt match, skip

CardLoadDirShortName:
  clc  
  lda filesInDir                      ;filesInDir++
  adc #$01
  sta filesInDir
  lda filesInDir+1
  adc #$00
  sta filesInDir+1
  
  lda lfnFound
  cmp #$01
  beq CardLoadDirLongShortNameFound
  
CardLoadDirShortNameFound:  
  ldy #$0000
CardLoadDirShortNameLoop:            ;if lfnFound = 0, copy short name
  lda [sourceEntryLo], y
  sta tempEntry, y
  iny
  cpy #$0008
  bne CardLoadDirShortNameLoop

CardLoadDirShortFileName:  
  lda tempEntry+$1B                      ;$1B = dir flag  //CHANGE ME
  cmp #$01
  beq CardLoadDirSaveEntry

  lda #'.'
  sta tempEntry+$8
  ldy #$0008
  lda [sourceEntryLo], y    ;//copy short file name
  sta tempEntry+$9
  iny
  lda [sourceEntryLo], y
  sta tempEntry+$A
  iny
  lda [sourceEntryLo], y
  sta tempEntry+$B  
  
  jmp CardLoadDirSaveEntry
  
  
      
CardLoadDirLongShortNameFound:    
      
CardLoadDirSaveEntry:
  ldy #$001A
  lda [sourceEntryLo], y                  ;copy clusterhilo to last 4 bytes of entry   ///CHANGE ME
  sta tempEntry+$1C
  iny
  lda [sourceEntryLo], y
  sta tempEntry+$1D
  ldy #$0014
  lda [sourceEntryLo], y
  sta tempEntry+$1E
  iny
  lda [sourceEntryLo], y
  sta tempEntry+$1F
  
  
  ldy #$0000
CardLoadDirSaveEntryLoop:                 ;copy entry to prg ram   //CHANGE ME  128 bytes
  lda tempEntry, y
  sta [destEntryLo], y                
  iny
  cpy #$0020
  bne CardLoadDirSaveEntryLoop
    
  clc
  lda destEntryLo                         ;destEntry += 32   //CHANGE ME  to 128 bytes
  adc #$20
  sta destEntryLo
  lda destEntryHi
  adc #$00
  sta destEntryHi
  
;  lda destEntryHi                        ;check if destEntry is overflowing, inc bank, destEntry=6000
;  cmp #$80
;  bne CardLoadDirClearTempEntryName
;  lda #$60
;  sta destEntryHi                        ;reset to $6000
;  lda #$00
;  sta destEntryLo
;  inc destBank                           ;increment bank num

  jump jCardLoadDirClearEntryName

;CardLoadDirClearTempEntryName:
;  ldy #$0000
;  lda #$00  
;  sta lfnFound  
;CardLoadDirClearTempEntryLoop:            ;clear entry name
;  sta tempEntry, y
;  iny
;  cpy #$0020
;  bne CardLoadDirClearTempEntryLoop
    
CardLoadDirNextEntry:
  clc                                 ;increment entry source address
  lda sourceEntryLo                        ;sourceEntry += 32 in 0200-0400
  adc #$20
  sta sourceEntryLo
  lda sourceEntryHi
  adc #$00
  sta sourceEntryHi  
  
  lda sourceEntryHi                    ;if source overflows, get next sector
  cmp #>sectorBuffer1+$02   ;;LAST CHANGE
  beq CardLoadDirNextSector
  
  jmp CardLoadDirLoop
    
CardLoadDirNextSector:    
  clc
  lda sourceSector  
  adc #$01
  sta sourceSector                   ;go to next sector num
  lda sourceSector+1
  adc #$00
  sta sourceSector+1
  lda sourceSector+2
  adc #$00
  sta sourceSector+2
  lda sourceSector+3
  adc #$00
  sta sourceSector+3
  
  
  ;  if fat32, goto CardLoadDirSectorInc
  lda fat32Enabled
  cmp #$01
  beq CardLoadDirSectorInc

  
  lda sourceCluster            ;FAT16  check if trying to load root dir
  cmp rootDirCluster
  bne CardLoadDirSectorInc
  lda sourceCluster+1
  cmp rootDirCluster+1
  bne CardLoadDirSectorInc
  lda sourceCluster+2
  cmp rootDirCluster+2
  bne CardLoadDirSectorInc 
  lda sourceCluster+3
  cmp rootDirCluster+3
  bne CardLoadDirSectorInc  

  clc
  lda sectorCounter              ;FAT16 root dir all sequential sectors
  adc #$01
  sta sectorCounter

  ; if sectorCounter = fat16RootSectors
  ;        goto CardLoadDirLastEntryFound
  clc
  lda sectorCounter
  cmp fat16RootSectors
  beq CardLoadDirLastEntryFoundJmp
  jmp CardLoadDirLoadNextSector      ;FAT16 skip cluster lookup when max root sectors not reached
  

CardLoadDirLastEntryFoundJmp:
  jmp CardLoadDirLastEntryFound       ;FAT16 max root sectors reached, all root entries found
      
  
CardLoadDirSectorInc:
  clc
  lda sectorCounter                   ;one more sector
  adc #$01
  sta sectorCounter
  cmp sectorsPerCluster                ;make sure cluster isnt overflowing      
  bne CardLoadDirLoadNextSector
  
  ;;;move to next cluster
  jump jNextCluster                      ;get cluster num into sourceCluster


CardLoadDirLastEntryCheck:  
  lda #$0F
  sta temp
  lda #$FF
  sta temp+1                            ;;FAT32 last cluster = 0x0FFFFFFF
  sta temp+2
  sta temp+3

  lda fat32Enabled
  cmp #$01
  beq CardLoadDirLastEntryCheck2        ;;if FAT32, last cluster mask done
  
  lda #$00                              ;;FAT16 last cluster = 0x0000FFFF
  sta temp
  sta temp+1
  
CardLoadDirLastEntryCheck2:
  ;check if cluster = last cluster
  ; if match, jmp to last entry found
  lda temp
  cmp sourceCluster+3
  bne CardLoadDirNextSectorNum
  lda temp+1
  cmp sourceCluster+2
  bne CardLoadDirNextSectorNum
  lda temp+2
  cmp sourceCluster+1
  bne CardLoadDirNextSectorNum
  lda temp+3
  cmp sourceCluster+0
  bne CardLoadDirNextSectorNum
  
  jmp CardLoadDirLastEntryFound


CardLoadDirNextSectorNum:
  jump jClusterToLBA                     ;sourceCluster -> first sourceSector
  lda #$00
  sta sectorCounter                    ;reset sector counter

CardLoadDirLoadNextSector:
  lda #<sectorBuffer1
  sta destLo                          ;reset sector dest
  sta sourceEntryLo                   ;reset entry source
  lda #>sectorBuffer1                   
  sta destHi
  sta sourceEntryHi
  stz destBank
  stz sourceEntryBank
  lda #kDestWRAM
  sta destType
  jump jCardReadSector
  jmp CardLoadDirLoop




NextDir:
  lda tempEntry+$1C
  sta sourceCluster
  lda tempEntry+$1D
  sta sourceCluster+1
  lda tempEntry+$1E
  sta sourceCluster+2
  lda tempEntry+$1F
  sta sourceCluster+3      ;load dir selected
  
  lda sourceCluster
  cmp #$00
  bne LoadNextDir
  lda sourceCluster+1       ;check if cluster = root dir
  cmp #$00
  bne LoadNextDir
  lda sourceCluster+2
  cmp #$00
  bne LoadNextDir
  lda sourceCluster+3
  cmp #$00
  bne LoadNextDir
  
  lda rootDirCluster
  sta sourceCluster
  lda rootDirCluster+1
  sta sourceCluster+1
  lda rootDirCluster+2
  sta sourceCluster+2
  lda rootDirCluster+3
  sta sourceCluster+3
  
  
LoadNextDir:  
  jump jPrintClearScreen
  SetCursorPos 0, 0
  jsr CardLoadDir
  jump jDirPrintDir
  rts
