;=== 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 #'T'         ;;;TXT
  sta exMatch1
  sta exMatch1+1
  sta exMatch1+2
  sta exMatch1+3
  lda #'X'
  sta exMatch2
  sta exMatch2+1
  sta exMatch2+2
  sta exMatch2+3
  lda #'T'
  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

  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 NextModuleSO     ;;;START = cancel, go back to options
StartDone: 
  jmp Forever

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

NextModuleLoad:
  SetCursorPos 18,0
  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 #<sectorBuffer1
  sta destLo
  lda #>sectorBuffer1
  sta destHi                   ;;put into sector ram
  stz destBank

  stz sectorCounter
  stz bankCounter
  jump jClusterToLBA            ;sourceCluster -> first sourceSector
  lda #kDestWRAM
  sta destType
  jump jCardReadSector          ;sector -> wram     lda gameCluster


  ldx #$0000
  ldy #$0000
ReadLoop:
  lda sectorBuffer1, y
  cmp #$20       ;;space - read to end of line, then next code 
  beq ReadLine  
  cmp #$0D       ;;CR
  beq NextCode
  cmp #$0A       ;;LF
  beq NextCode
  cmp #$1A       ;;EOF
  beq ReadLoopDone
  cmp #$30
  bcc NextChar   ;;under 0
  cmp #$3A
  bcc SaveCharNumJump  ;;between 0 and 9
  cmp #$41
  bcc NextChar             ;;under A
  cmp #$47
  bcc SaveCharAlphaJump  ;;between A and F
  jmp NextChar

SaveCharAlphaJump:
  jsr SaveCharAlpha
  jmp NextGGChar
SaveCharNumJump:
  jsr SaveCharNum
  jmp NextGGChar

ReadLine:
  ;PrintString "R"
  lda sectorBuffer1, y
  cmp #$0D
  beq NextCode
  cmp #$0A
  beq NextCode
  iny
  cpy #$0100
  beq ReadLoopDone
  jmp ReadLine

NextCode:
;  PrintString "N"
  stx temp
 ; PrintHexNum temp
  lda temp
  and #%00000111
  beq NextChar      ;;already at the beginning of a code, skip
  lda temp
  and #%11111000    ;;go to beginning of current code
  clc
  adc #%00001000    ;;go to beginning of next code
  sta temp
  ldx temp
  lda temp
  cmp #$28
  beq ReadLoopDone
  jmp NextChar
  
NextGGChar:
  inx                   ;;next gg char
  cpx #$0028        ;;40 gg chars = 5 codes x 8 chars
  beq ReadLoopDone
NextChar:
  iny
  cpy #$0100
  beq ReadLoopDone
  jmp ReadLoop

ReadLoopDone:
  jmp NextModuleSO

  
NextModuleSO:

;  lda Joy1New+1
;  and #%0001000
;  beq NextModuleSO

  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'O'
  sta findEntry+1
  jump jCardLoadModule      ;load options module
    
  
  
SaveCharAlpha:
  lda sectorBuffer1, y    ;;put character into codes
  sec
  sbc #$41    ;;subtract 'A'
  clc
  adc #$0A    ;;add 0-9
  sta gameGenie, x
  sta temp
;  PrintString " A"
;  PrintHexNum temp
  rts
  
SaveCharNum:
  lda sectorBuffer1, y    ;;put character into codes
  sec
  sbc #$30     ;;subtract '0'
  sta gameGenie, x
  sta temp
;  PrintString " C"
;  PrintHexNum temp
  rts
  
  