;;;;  D.MAP    SAVE FDS DISPLAY, show .fds, do scrolling


  .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:
  
  lda #$1B
  sta printY
  lda #$07
  sta temp
  jsr StrCopy  
  
;--------------------------------------;  
  lda #'F'
  sta exMatch1
  sta exMatch1+2
  sta exMatch1+3
  lda #'D'
  sta exMatch2
  sta exMatch2+2
  sta exMatch2+3
  lda #'S'
  sta exMatch3
  sta exMatch3+2
  sta exMatch3+3
  lda #'f'
  sta exMatch1+1
  lda #'d'
  sta exMatch2+1
  lda #'s'
  sta exMatch3+1
;-------------------------------------;

  lda rootDirCluster
  sta sourceCluster
  lda rootDirCluster+1
  sta sourceCluster+1
  lda rootDirCluster+2
  sta sourceCluster+2
  lda rootDirCluster+3
  sta sourceCluster+3
  jsr CardLoadDir        ; root dir

;  jsr DirSort

  jsr DirPrintDir
  jmp Forever
   
  
;---------------------------------------;




NextDir:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts
  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:  
  lda #$1B
  sta printY
  lda #$07
  sta temp
  jsr StrCopy
  jsr CardLoadDir

;  jsr DirSort

  jsr DirPrintDir
  lda #$00
  sta moduleReady
  lda #$4C
  sta $0404      ;module ready, set controller vector to jmp
  rts


NextModule:
  jsr DirGetEntry       ;get selected entry
  
  lda tempEntry+$1B     ;check if its a directory
  cmp #$01
  beq NextDir

  ldx #$00
CopySaveNameLoop:
  lda tempEntry, x
  sta saveName, x
  inx
  cpx #$1C               ;copy 28 chars
  bne CopySaveNameLoop
  
  
NextModuleQ:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts

  lda tempEntry+$1C
  sta wramCluster
  lda tempEntry+$1D
  sta wramCluster+1
  lda tempEntry+$1E
  sta wramCluster+2
  lda tempEntry+$1F
  sta wramCluster+3      ;copy to save ram cluster
  
  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo


  ldy #$00
SavingFDSStrLoop:
  lda SavingFDSStr, y
  sta printString, y      ;copy 32 text chars loading bios
  iny
  cpy #$20
  bne SavingFDSStrLoop
  
  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



  
  jsr SaveFDS
  
  
NextModuleDone:
  jsr ClearFindEntry
  lda #'I'
  sta findEntry
  jmp CardLoadModule      ;load intro module
  

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

CONTROLLER:
  jsr ReadJoystick
  ;check each button
  ; when pressed, go to next module
  lda joystick
  and oldButtons
  sta heldButtons
  lda joystick
  sta oldButtons

  lda joystick
  bne ReadyCheck
  lda #$01
  sta moduleReady

ReadyCheck:
  lda moduleReady
  bne ACheck
  jmp CheckDone
  
ACheck:
  lda joystick
  and #$80
  beq BCheck
  jmp NextModule
  
BCheck:
  lda joystick
  and #$40
  beq SelectCheck
  jmp NextModule

SelectCheck:
  lda joystick
  and #$20
  beq StartCheck
  jmp NextModule
  
StartCheck:
  lda joystick
  and #$10
  beq NorthCheck
  
  ;  came from question q.map, go back to question q.map  
StartNextModuleQ:
  
  
StartNextModuleDone:
  jsr ClearFindEntry
  lda #'Q'
  sta findEntry
  jmp CardLoadModule      ;load question module
  
  

NorthCheck:
  lda joystick
  and #$08
  beq SouthCheck
  
  ;handle up button
  lda scrollYCounter    ;no scrolling while scrolling;
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  lda #$01
  sta speedScroll
  lda #$08
  sta speedCounter
  jsr ScrollUp
  jmp CheckDone

SouthCheck:
  lda joystick
  and #$04
  beq WestCheck
  
  ;handle down button
  lda scrollYCounter    ;no scrolling while scrolling
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  lda #$01
  sta speedScroll
  lda #$08
  sta speedCounter  
  jsr ScrollDown
  jmp CheckDone

WestCheck:
  lda heldButtons
  and #$02
  beq EastCheck
  
  ;handle left button
  lda scrollYCounter    ;no scrolling while scrolling
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  jsr CalcScrollSpeed
  jsr ScrollUp
  jmp CheckDone

EastCheck:
  lda heldButtons
  and #$01
  beq NoButtons
  
  ;handle right button
  lda scrollYCounter    ;no scrolling while scrolling;
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  jsr CalcScrollSpeed
  jsr ScrollDown
  jmp CheckDone

NoButtons:
  lda #$00
  sta scrollHoldTimer

CheckDone:
  rts
  

StartButton:
  rts
  
  
  
  
  
CalcScrollSpeed:
  inc scrollHoldTimer
  lda scrollHoldTimer
  cmp #40
  bcc ScrollSlow
;  cmp #120
;  bcc ScrollMedium
;ScrollFast:
  dec scrollHoldTimer
;  lda #$08
;  sta speedScroll
;  lda #$01
;  sta speedCounter
;  rts
ScrollMedium:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  rts
ScrollSlow:
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  rts

  
  
  
  
  
  
  
  
SaveFDS:
;;load wram cluster into destcluster
  lda wramCluster
  bne SaveFDSInit
  lda wramCluster+1
  bne SaveFDSInit
  lda wramCluster+2
  bne SaveFDSInit
  lda wramCluster+3
  bne SaveFDSInit
  jmp SaveFDSDone
  
SaveFDSInit:
  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 #$20
  sta destBank    ;;2nd half of prg
  sta PRGBANK

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

SaveFDSLoopCheckCluster16:
  lda sourceCluster
  cmp #$FF
  bne SaveFDSLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+1
  cmp #$FF
  bne SaveFDSLoopLoadBank  ;cluster != last cluster
  
  lda fat32Enabled
  beq SaveFDSDone   ;if fat 16, done checking
  
SaveFDSLoopCheckCluster32:
  lda sourceCluster+2
  cmp #$FF
  bne SaveFDSLoopLoadBank  ;cluster != last cluster
  
  lda sourceCluster+3
  and #$0F
  cmp #$0F
  bne SaveFDSLoopLoadBank  ;cluster != last cluster
  
  jmp SaveFDSDone  ;fat 32, done checking
  
  
  
    
SaveFDSLoopLoadBank:     
  lda temp
  sta sourceHi
SaveFDSLoopCheckBank:
  lda sourceHi
  cmp #$80
  bne SaveFDSLoop       ;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 SaveFDSLoop
  
  
  
SaveFDSDone:
  rts


SavingFDSStr:
  ;    12345678901234567890123456789012
  .db "      Saving FDS disk...        "




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


  




