;;;;  B.MAP    SAVE RAM DISPLAY, show .sav .SAV games, 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 #'S'
  sta exMatch1
  sta exMatch1+2
  sta exMatch1+3
  lda #'A'
  sta exMatch2
  sta exMatch2+2
  sta exMatch2+3
  lda #'V'
  sta exMatch3
  sta exMatch3+2
  sta exMatch3+3
  lda #'s'
  sta exMatch1+1
  lda #'a'
  sta exMatch2+1
  lda #'v'
  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

  lda powerOn
  beq NextModuleQ
  ;if powerOn = 1
  ;  came from options o.map, save wramCluster go to o.map
  ;if powerOn = 0
  ;  came from question q.map, go to save s.map
NextModuleO:
  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 LoadBatteryRam
  
  lda #'O'
  sta findEntry
  jmp NextModuleDone
  
  
NextModuleQ:
  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
  lda #'S'
  sta findEntry
  
  
NextModuleDone:
  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 next 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
  
  ;start pressed = CANCEL
  lda powerOn
  beq StartNextModuleQ
  ;if powerOn = 1
  ;  came from options o.map, go back to o.map
  ;if powerOn = 0
  ;  came from question q.map, go back to question q.map
StartNextModuleO:
  lda #'O'
  sta findEntry
  jmp StartNextModuleDone
  
StartNextModuleQ:
  lda #'Q'
  sta findEntry
  
  
StartNextModuleDone:
  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
  
  

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
  

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


;StartButton:
;  rts




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  





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








  
  
  
  
