;;;;  G.MAP    GAME GENIE codes, show .TXT games, do scrolling, load codes

  .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 #'T'
  sta exMatch1+0     ;.TXT
  sta exMatch1+1    
  sta exMatch1+2     

  lda #'X'
  sta exMatch2+2     
  sta exMatch2+0     
  sta exMatch2+1    

  lda #'T'
  sta exMatch3+0   
  sta exMatch3+2   
  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 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 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
  cmp #$01
  beq NextDir
  
  
  
  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 #$00
  sta destLo
  lda #$02
  sta destHi
  jsr ClusterToLBA
  jsr CardReadSector
  
  
FindAvailCode:
  ldx #$00
  lda gameGenie, x
  cmp #$10          ;;check for _ char at beginning of code #1
  beq CodeFound

  ldx #$08
  lda gameGenie, x
  cmp #$10          ;;check for _ char at beginning of line #2
  beq CodeFound

  ldx #$10
  lda gameGenie, x
  cmp #$10          ;;check for _ char at beginning of line #3
  beq CodeFound

  ldx #$18
  lda gameGenie, x
  cmp #$10          ;;check for _ char at beginning of line #4
  beq CodeFound

  ldx #$20          ;;replace code #5
  
CodeFound:

  lda #$10
  sta gameGenie, x     ;;clear code
  sta gameGenie+1, x
  sta gameGenie+2, x
  sta gameGenie+3, x
  sta gameGenie+4, x
  sta gameGenie+5, x
  sta gameGenie+6, x
  sta gameGenie+7, x
  

  ldy #$00
ReadLoop:
  lda $0200, 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 #$41
  bcc NextChar             ;;under A
  cmp #$5B
  bcc SaveCharAlphaJump  ;;between A and Z
  jmp NextChar

SaveCharAlphaJump:
  jsr SaveCharAlpha
  jmp NextGGChar

ReadLine:
  lda $0200, y
  cmp #$0D
  beq NextCode
  cmp #$0A
  beq NextCode
  cmp #$1A       ;;EOF
  beq ReadLoopDone
  iny
  cpy #$FF
  beq ReadLoopDone
  jmp ReadLine

NextCode:
  txa
  sta 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
  tax
  cpx #$28          ;;first char beyond last code
  beq ReadLoopDone
  jmp NextChar
  
NextGGChar:
  inx                   ;;next gg char
  cpx #40
  beq ReadLoopDone
NextChar:
  iny
  cpy #$FF              ;;next char in file
  beq ReadLoopDone
  jmp ReadLoop

ReadLoopDone:
  jsr ClearFindEntry
  lda #'O'
  sta findEntry  
  lda #' '
  jmp CardLoadModule      ;load options module
 



SaveCharAlpha:
  tya
  pha
  lda $0200, y     ;;get char from file
  sec
  sbc #$41         ;;subtract 'A'
  tay
  lda GGCHARS, y   ;;look up char
  sta gameGenie, x ;;put into codes
  pla
  tay
  rts
  
GGCHARS:
;      A    B    C    D    E    F    G    H    I    J    K    L    M    N  
  .db $00, $10, $10, $10, $08, $10, $04, $10, $05, $10, $0C, $03, $10, $0F
  ;;   A                   E         G         I         K    L         N 

;      O    P    Q    R    S    T    U    V    W    X    Y    Z
  .db $09, $01, $10, $10, $0D, $06, $0B, $0E, $10, $0A, $07, $02 
  ;;   O    P              S    T    U    V         X    Y    Z

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

CONTROLLER:
  jsr ReadJoystick
  ;check each button
  ; when pressed, go to next module

  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
  jmp NextModule

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 joystick
  and #$02
  beq EastCheck
  
  ;handle left button
  lda scrollYCounter    ;no scrolling while scrolling
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter  
  jsr ScrollUp
  jmp CheckDone

EastCheck:
  lda joystick
  and #$01
  beq CheckDone
  
  ;handle right button
  lda scrollYCounter    ;no scrolling while scrolling;
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  jsr ScrollDown
  jmp CheckDone
  
CheckDone:
  rts
  
  
  
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

  
  
  