;;;;B = SRM DISPLAY
;=== 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
  stz Joy1New
  stz Joy1New+1
  
  lda #'S'         ;;;SRM
  sta exMatch1
  sta exMatch1+1
  sta exMatch1+2
  sta exMatch1+3
  lda #'R'
  sta exMatch2
  sta exMatch2+1
  sta exMatch2+2
  sta exMatch2+3
  lda #'M'
  sta exMatch3
  sta exMatch3+1
  sta exMatch3+2
  sta exMatch3+3
  
  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
  jump jCardLoadDir    ; root dir
  jump jDirPrintDir
  
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:
LCheck:
  lda Joy1Press
  and #%00010000
  beq LDone
LPressed:
  lda cursorYCounter
  bne LDone
  lda scrollYCounter
  bne LDone
LScroll:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  jump jScrollUp
LDone:

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:
RCheck:
  lda Joy1Press
  and #%00100000
  beq RDone
RPressed:
  lda cursorYCounter
  bne RDone
  lda scrollYCounter
  bne RDone
RScroll:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  jump jScrollDown
RDone:

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:
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 NextModuleSOI     ;;;skip saving the sram info
;StartDone: 
  jmp Forever
  
NextModule:
  jump jDirGetEntry       ;get selected entry
  
  lda tempEntry+$1B
  cmp #$01  
  bne NextModuleLoad
  jump jNextDir
  rts
  


NextModuleLoad:
  ldy #$0000
CopyGameNameLoop:
  lda tempEntry, y
  sta saveName, y
  iny
  cpy #30               ;
  bne CopyGameNameLoop
  
  stz saveName+$1C     ;;null terminate game name
  
  lda tempEntry+$1C
  sta saveCluster
  sta sourceCluster
  lda tempEntry+$1D
  sta saveCluster+1
  sta sourceCluster+1
  lda tempEntry+$1E
  sta saveCluster+2
  sta sourceCluster+2
  lda tempEntry+$1F
  sta saveCluster+3      ;copy to save cluster
  sta sourceCluster+3      ;copy to source cluster
  
NextModuleSOI:
  lda #%00000001
  sta CONFIGWRITESTATUS   ;;no more battery flag

  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda nextModule
  sta findEntry+1
  cmp #'I'
  beq NextModuleSI
  jmp NextModuleSOIDone
  
NextModuleSI:
  lda #$FF
  sta cursorX
  lda #$F0
  sta cursorY
  jump jPrintClearScreen
  jump jLoadLogo
  jump jLoadRomVersion

  lda #$00
  sta DMAWRITELO
  sta DMAWRITEHI
  lda #$F8
  sta DMAWRITEBANK
    
  ClearLine 18
  SetCursorPos 18, 0
  PrintString "            Saving..."

  lda #kSourceSDRAM
  sta sourceType

  jump jCardWriteFile
  
  SetCursorPos 18, 0
  PrintString "        Battery RAM saved"

  
NextModuleSOIDone:
  jump jCardLoadModule      ;load options or save module



  
  

