;;; F.MAP   FDS bios loader

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

  .bank 0
  .org $0400
  jmp START
  .org $0404    
  jmp CONTROLLER
  
START:
  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo

  ldy #$00
LoadingBiosStrLoop:
  lda LoadingBiosStr, y
  sta printString, y      ;copy 32 text chars loading bios
  iny
  cpy #$20
  bne LoadingBiosStrLoop
  lda #$1B
  sta printY
  jsr PrintLine

  lda #$F8
  sta cursorY
  lda #$F8
  sta cursorX
  lda #$00
  sta scrollY           
  sta scrollYCounter    
  sta scrollYUp         
  sta scrollYDown       
  sta cursorYCounter
  sta cursorYUp         
  sta cursorYDown
  jsr RenderingOn  
  
  
  lda baseDirCluster
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3

  lda #'B'
  sta exMatch1
  lda #'I'
  sta exMatch2
  lda #'N'
  sta exMatch3

  jsr CardLoadDir    ; powerpak dir
  
  lda #'F'
  sta findEntry+0
  lda #'D'
  sta findEntry+1
  lda #'S'
  sta findEntry+2
  lda #'B'
  sta findEntry+3
  lda #'I'
  sta findEntry+4
  lda #'O'
  sta findEntry+5
  lda #'S'
  sta findEntry+6
  lda #' '
  sta findEntry+7
  


  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    ;;starting cluster of bios
  jsr ClusterToLBA

  lda #$00  ;;#$80 = wram     #$00 = prg ram
  sta destBank   ;;$0000 in prg ram
  sta PRGBANK
  jsr LoadBios





  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo

  lda #$0C
  sta temp
  jsr StrCopy    ;add loading game text
  lda #$1B
  sta printY
  jsr PrintLine
  
  lda #$F8
  sta cursorY
  lda #$F8
  sta cursorX
  lda #$00
  sta scrollY           
  sta scrollYCounter    
  sta scrollYUp         
  sta scrollYDown       
  sta cursorYCounter
  sta cursorYUp         
  sta cursorYDown

  jsr RenderingOn


  lda #$20
  sta destBank   ;;2nd half of prg
  sta PRGBANK
  jsr LoadDisk
  
  
  
  
  jsr RenderingOff



  lda prgSize
  lsr a
  lsr a
  sta prgSize  ;convert back to 16KB banks for size loading

  
  lda gameCluster
  sta wramCluster
  lda gameCluster+1
  sta wramCluster+1
  lda gameCluster+2
  sta wramCluster+2
  lda gameCluster+3
  sta wramCluster+3   ;;save it as wram for writing back to cf
  jsr SaveLast
  
  
  jsr LoadFPGA 
  jsr LoadMirroring
  jsr LoadPrgChrSize
  
  lda #$00
  sta BOOTENWR
  jmp [$FFFC]
  
  
  
  
  
  
  
  
LoadBios:

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

LoadBiosLoopCheckCluster:
  lda sourceCluster
  cmp #$FF
  bne LoadBiosLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+1
  cmp #$FF
  bne LoadBiosLoopLoadBank  ;cluster != last cluster
  
  lda fat32Enabled
  beq LoadBiosLoopDone   ;if fat 16, done checking
  
LoadBiosLoopCheckCluster32:
  lda sourceCluster+2
  cmp #$FF
  bne LoadBiosLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+3
  and #$0F
  cmp #$0F
  bne LoadBiosLoopLoadBank  ;cluster != last cluster

LoadBiosLoopDone:
  rts  ;cluster = FF FF (fat 16) or 0x0F FF FF FF  (fat 32)
  
LoadBiosLoopLoadBank:  
  lda temp
  sta destHi
LoadBiosLoopCheckBank:
  lda destHi
  cmp #$80
  bne LoadBiosLoop       ;check if next bank needed, destHi=80
  lda destBank
  clc
  adc #$01
  sta destBank
  sta PRGBANK
  lda #$60
  sta destHi
  jmp LoadBiosLoop  





OldLoadDisk:
  lda #$00
  sta sectorCounter
  sta destLo
  lda #$60
  sta destHi  
  
  lda gameCluster
  sta sourceCluster
  lda gameCluster+1
  sta sourceCluster+1
  lda gameCluster+2
  sta sourceCluster+2
  lda gameCluster+3
  sta sourceCluster+3
  
  jsr ClusterToLBA
  jsr CardReadSector16     ;get FDS header  16bytes
  
  lda #$00
  sta bankCounter
  sta destLo               ;reset to beginning of prgram
  lda #$60
  sta destHi  
OldLoadDiskLoop:
  lda #$00
  sta destLo
  
  jsr CardReadSector496    ;get 496 bytes
  
  lda destHi
  sta temp
  jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  lda temp
  sta destHi
  lda #$F0
  sta destLo
  jsr CardReadSector16     ;get 16 bytes  
OldLoadDiskLoopCheckBank:
  lda destHi
  cmp #$80
  bne LoadDiskLoop       ;check if next bank needed, destHi=80
  lda #$60
  sta destHi
  lda #$00
  sta destLo
  inc destBank
  inc bankCounter
OldLoadDiskLoopCheckDone:
  ;check if all prg loaded
  lda bankCounter
  cmp prgSize
  beq LoadDiskDone
  lda destBank
  sta PRGBANK
  jmp LoadDiskLoop
OldLoadDiskDone:
  rts




LoadDisk:
  lda #$00
  sta sectorCounter
  sta destLo
  lda #$60
  sta destHi  
  
  lda gameCluster
  sta sourceCluster
  lda gameCluster+1
  sta sourceCluster+1
  lda gameCluster+2
  sta sourceCluster+2
  lda gameCluster+3
  sta sourceCluster+3
  
  jsr ClusterToLBA
 ; jsr CardReadSector16     ;get FDS header  16bytes
  
  lda #$00
  sta bankCounter
  sta destLo               ;reset to beginning of prgram
  lda #$60
  sta destHi  
  lda #$00
  sta destLo
LoadDiskLoop:
  
  jsr CardReadSector    ;get 512 bytes
  
  lda destHi
  sta temp
  jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  lda temp
  sta destHi
;  lda #$F0
;  sta destLo
;  jsr CardReadSector16     ;get 16 bytes  
LoadDiskLoopCheckBank:
  lda destHi
  cmp #$80
  bne LoadDiskLoop       ;check if next bank needed, destHi=80
  lda #$60
  sta destHi
  lda #$00
  sta destLo
  inc destBank
  inc bankCounter
LoadDiskLoopCheckDone:
  ;check if all prg loaded
  lda bankCounter
  cmp prgSize
  beq LoadDiskDone
  lda destBank
  sta PRGBANK
  jmp LoadDiskLoop
LoadDiskDone:
  rts

  
LoadingBiosStr:
  ;    12345678901234567890123456789012
  .db "     Loading FDS BIOS...        "
  
  
SaveLast:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts
  lda #$00
  sta moduleReady


  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    
;-------------------------------------;

  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

  lda #'L'
  sta findEntry  
  lda #'A'
  sta findEntry+1       ;;find LAST.TXT in powerpak dir
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #' '
  sta findEntry+4
  sta findEntry+5
  sta findEntry+6
  sta findEntry+7


  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
  
  jsr ClusterToLBA
  
  
  ldx #$00
  ldy #$00
SaveLastGameLoop:         ;;game name
  lda gameName, x
  sta $0200, y
  iny
  inx
  cpx #32
  bne SaveLastGameLoop

  ldx #$00
SaveLastGameCLoop:         ;;game cluster
  lda gameCluster, x
  sta $0200, y
  iny
  inx
  cpx #4
  bne SaveLastGameCLoop
  
  ldx #$00
SaveLastSaveLoop:         ;;save name   ;;ALSO FDS file
  lda gameName, x
  sta $0200, y
  iny
  inx
  cpx #32
  bne SaveLastSaveLoop

  ldx #$00
SaveLastSaveCLoop:         ;;save cluster  ;;ALSO FDS file
  lda gameCluster, x
  sta $0200, y
  iny
  inx
  cpx #4
  bne SaveLastSaveCLoop

  ldx #$00
SaveLastGameGenieLoop:    ;;game genie chars 5x8
  lda gameGenie, x
  sta $0200, y
  iny
  inx
  cpx #40
  bne SaveLastGameGenieLoop

  
  
  lda #$00
  sta bankCounter
  sta sectorCounter
  sta sourceLo               ;reset to beginning of sector buffer
  lda #$02
  sta sourceHi    
  jsr CardWriteSector    ;write 512 bytes to file
  rts      
      
      
CONTROLLER:
  rts
  
  .org    $0800
  .incbin "fds3.bit"