;;;;O = OPTIONS

;=== 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 #$D5
  sta DMAWRITELO
  lda #$FF
  sta DMAWRITEHI
  lda #$40
  sta DMAWRITEBANK     ;;;destroy header  40FFC0
  lda #$00
  sta $21FF
  
  lda #$D5
  sta DMAWRITELO
  lda #$FF
  sta DMAWRITEHI
  lda #$00
  sta DMAWRITEBANK     ;;;destroy header  FFC0
  lda #$00
  sta $21FF

  lda #$D5
  sta DMAWRITELO
  lda #$7F
  sta DMAWRITEHI
  lda #$00
  sta DMAWRITEBANK     ;;;destroy header  7FC0
  lda #$00
  sta $21FF


;  lda #%00000011  ;battused, bootmode
;  sta CONFIGWRITESTATUS
  
  wai
  jump jPrintClearScreen

  SetCursorPos 5,8
  PrintString "Load Battery RAM"
  SetCursorPos 6,3

  lda #<saveName
  sta strPtr
  lda #>saveName
  sta strPtr+1
  ldy #strPtr
  PrintString "%s"

   
  SetCursorPos 9,8
  PrintString "Game Genie Codes"
  SetCursorPos 11,9
  ldy #$0000
  jump jGameGeniePrint
  SetCursorPos 13,9
  ldy #$0008
  jump jGameGeniePrint
  SetCursorPos 15,9
  ldy #$0010
  jump jGameGeniePrint
  SetCursorPos 17,9
  ldy #$0018
  jump jGameGeniePrint
  SetCursorPos 19,9
  ldy #$0020
  jump jGameGeniePrint
  
  
  
  SetCursorPos 22,8
  PrintString "Start Game"
  SetCursorPos 23,3
  lda #<gameName
  sta strPtr
  lda #>gameName
  sta strPtr+1
  ldy #strPtr
  PrintString "%s"

  lda #$B8
  sta cursorY   ;;
  lda #$38
  sta cursorX
  lda #$00
  sta cursorYCounter
  sta cursorYUp
  sta cursorYDown
  sta scrollY
  sta scrollYCounter     
  sta scrollYUp          
  sta scrollYDown     
  
  stz Joy1Press
  stz Joy1Press+1
  stz Joy1New
  stz Joy1New+1
 


ForeverSO:
  wai
  jsr ReadController
  jmp ForeverSO
  
  
  
  
LoadGameModule:
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'M'
  sta findEntry+1    
  jump jCardLoadModule      ;load mapper module
  
  

LoadGGLabelModule:
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'G'
  sta findEntry+1    
  jump jCardLoadModule      ;load mapper module





NextButton:
  lda cursorY
  cmp #$30
  bne NextCheckStart
;  jsr LoadBRAMModule   ;load battery ram
LoadBRAMModule:
  lda #'O'
  sta nextModule         ;;go back to options after picking bram file
  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'B'
  sta findEntry+1
  jump jCardLoadModule
;  rts

NextCheckStart:
  lda cursorY
  cmp #$B8
  bne NextCheckGGLabel
  jmp LoadGameModule   ;start game
;  rts

NextCheckGGLabel:
  lda cursorY
  cmp #$50
  bne NextCheckGameGenie
  jmp LoadGGLabelModule   ;load gg code file

  
NextCheckGameGenie:
  lda cursorY
  cmp #$60
  beq NextCheckGameGenieNext     ;game genie
  cmp #$70
  beq NextCheckGameGenieNext
  cmp #$80
  beq NextCheckGameGenieNext
  cmp #$90
  beq NextCheckGameGenieNext
  cmp #$A0
  beq NextCheckGameGenieNext
  jmp NextCheckDone
NextCheckGameGenieNext:  
  jump jGameGenieNextChar
;  jsr GameGenieNextChar
  rts

NextCheckDone:
  rts
  
  
PrevButton:
  lda cursorY
  cmp #$30
  bne PrevCheckStart
  jmp LoadBRAMModule   ;load battery ram
;  rts

PrevCheckStart:
  lda cursorY
  cmp #$B8
  bne PrevCheckGGLabel
  jmp LoadGameModule   ;start game
;  rts

PrevCheckGGLabel:
  lda cursorY
  cmp #$50
  bne PrevCheckGameGenie
  jmp LoadGGLabelModule   ;load gg code file
  
PrevCheckGameGenie
  lda cursorY
  cmp #$60
  beq PrevCheckGameGeniePrev     ;game genie
  cmp #$70
  beq PrevCheckGameGeniePrev
  cmp #$80
  beq PrevCheckGameGeniePrev
  cmp #$90
  beq PrevCheckGameGeniePrev
  cmp #$A0
  beq PrevCheckGameGeniePrev
  jmp PrevCheckDone
PrevCheckGameGeniePrev:
  jump jGameGeniePrevChar
 ; jsr GameGeniePrevChar
  rts

PrevCheckDone:
  rts



ReadController:

AXCheck:
  lda Joy1New+1
  and #%10000000
  bne AXNext
  lda Joy1New
  and #%10000000
  bne AXNext
  jmp AXCheckDone
AXNext:
  jsr NextButton
;  jmp CheckDone
AXCheckDone:

BYCheck:
  lda Joy1New+1
  and #%01000000
  bne BYPrev
  lda Joy1New
  and #%01000000
  bne BYPrev
  jmp BYCheckDone
BYPrev:
  jsr PrevButton
;  jmp CheckDone
BYCheckDone:
    
SelectCheck:
  lda Joy1Press+1
  and #%00100000
  beq SelectCheckUp       ;SELECT = show gg info
SelectCheckDown:
  jsr SelectButton
  jmp SelectCheckDone
SelectCheckUp:
  ClearLine 12
  ClearLine 14
  ClearLine 16
  ClearLine 18
  ClearLine 20
SelectCheckDone:

  
StartCheck:
  lda Joy1New+1
  and #%00010000
  beq StartCheckDone

  jump jClearFindEntry
  lda #'S'
  sta findEntry
  lda #'I'
  sta findEntry+1
  wai
  jump jPrintClearScreen
  jump jLoadLogo
  jump jLoadRomVersion
  jump jCardLoadModule      ;START = cancel, load intro module
StartCheckDone:



NorthCheck:
  lda Joy1New+1
  and #%00001000
  bne NorthStart
  jmp SouthCheck
  
  ;handle up button
  ;cursorY = cursorY-16
  
  ;;if at start game (B8)
  ;;  go to game genie 5 (A0)
  ;;if at game genie 5 (A0)
  ;;  go to game genie 4 (90)
  ;;if at game genie 4 (90)
  ;;  go to game genie 3 (80)
  ;;if at game genie 3 (80)
  ;;  go to game genie 2 (70)
  ;;if at game genie 2 (70)
  ;; go to game genie 1 (60)
  ;;if at game genie 1 (60)
  ;; go to game genie label(50)
  ;;if at game genie label (50)
  ;; go to load bram (30)
  ;;if at bram (30)
  ;; go to start game (B8)
NorthStart:
  lda cursorY
  cmp #$B8
  bne NorthGG5
  lda #$A0
  sta cursorY
  jmp NorthIndent
NorthGG5:
  lda cursorY
  cmp #$A0
  bne NorthGG4
  lda #$90
  sta cursorY
  jmp NorthIndent
NorthGG4:
  lda cursorY
  cmp #$90
  bne NorthGG3
  lda #$80
  sta cursorY
  jmp NorthIndent
NorthGG3:
  lda cursorY
  cmp #$80
  bne NorthGG2
  lda #$70
  sta cursorY
  jmp NorthIndent
NorthGG2:
  lda cursorY
  cmp #$70
  bne NorthGG1
  lda #$60
  sta cursorY
  jmp NorthIndent
NorthGG1:
  lda cursorY
  cmp #$60
  bne NorthGGLabel
  lda #$50
  sta cursorY
  jmp NorthIndent
NorthGGLabel:
  lda cursorY
  cmp #$50
  bne NorthBram
  lda #$30
  sta cursorY
  jmp NorthIndent
NorthBram:
  lda #$B8
  sta cursorY


NorthIndent:
  lda cursorY
  cmp #$28
  bne CheckIndent
  lda #$A8
  sta cursorY

  jmp CheckDone               ;if at start+2, set to load

  

CheckIndent:
  ;if at gamegenie or arcade, cursorX = start
  jsr AtIndent
  lda temp
  cmp #$01
  bne DoNoIntent
  ;if at 0s or underscores, cursorX = start + 8
  jsr AtIndent
  lda temp
  cmp #$01
  beq DoIntent
  jmp CheckDone
DoNoIntent:
  lda #$38
  sta cursorX
  jmp CheckDone
DoIntent:
  lda #$40
  sta cursorX
  jmp CheckDone
  

SouthCheck:
  lda Joy1New+1
  and #%00000100
  beq WestCheck
  
  ;handle down button

  
  ;;if at start game (B8)
  ;; go to load bram (30)
  ;;if at game genie 5 (A0)
  ;; go to start game (B8)
  ;;if at game genie 4 (90)
  ;; go to game genie 5 (A0)
  ;;if at game genie 3 (80)
  ;; go to game genie 4 (90)
  ;;if at game genie 2 (70)
  ;;  go to game genie 3 (80)
  ;;if at game genie 1 (60)
  ;;  go to game genie 2 (70)
  ;;if at bram (30)
  
  ;; go to game genie label (50)
  
  ;;if at gglabel (50)
  ;; go to game genie 1 (60)
SouthStart:
  lda cursorY
  cmp #$B8
  bne SouthGG5
  lda #$30
  sta cursorY
  jmp SouthIndent
SouthGG5:
  lda cursorY
  cmp #$A0
  bne SouthGG4
  lda #$B8
  sta cursorY
  jmp SouthIndent
SouthGG4:
  lda cursorY
  cmp #$90
  bne SouthGG3
  lda #$A0
  sta cursorY
  jmp SouthIndent
SouthGG3:
  lda cursorY
  cmp #$80
  bne SouthGG2
  lda #$90
  sta cursorY
  jmp SouthIndent
SouthGG2:
  lda cursorY
  cmp #$70
  bne SouthGG1
  lda #$80
  sta cursorY
  jmp SouthIndent
SouthGG1:
  lda cursorY
  cmp #$60
  bne SouthBram
  lda #$70
  sta cursorY
  jmp SouthIndent
SouthBram:
  lda cursorY
  cmp #$30
  bne SouthGGLabel
  lda #$50
  sta cursorY
  jmp SouthIndent
SouthGGLabel:
  lda #$60
  sta cursorY



SouthIndent:
  cmp #$C0
  beq SouthIndentChange
  jmp CheckIndent            ;if not below start, check for indent change
SouthIndentChange:
  lda #$30
  sta cursorY
  
  jmp CheckDone               ;if at start+2, set to load    




WestCheck:
  lda Joy1New+1
  and #%00000010
  beq EastCheck
  
  ;handle left button
  jsr AtIndent
  lda temp
  cmp #$01
  beq MoveCursorWest
  jmp CheckDone  
MoveCursorWest:    
  lda cursorX
  cmp #$40                ; and not at start    
  beq CheckDone
  sec
  lda cursorX
  sbc #$10
  sta cursorX             ;cursorX = cursorX-16
  jmp CheckDone
  
  
EastCheck:
  lda Joy1New+1
  and #%00000001
  beq CheckDone
  
  ;handle right button
  jsr AtIndent
  lda temp
  cmp #$01
  beq MoveCursorEast
  jmp CheckDone  
MoveCursorEast:    
  lda cursorX
  cmp #$B0                ; and not at end    
  beq CheckDone
  clc
  lda cursorX
  adc #$10
  sta cursorX             ;cursorX = cursorX+16
  jmp CheckDone
  
CheckDone:
  rts
  
  
  
AtIndent:
  lda #$00
  sta temp
  lda cursorY
  cmp #$60
  beq AtIndentYes     ;if at underscores or 0s
  cmp #$70
  beq AtIndentYes
  cmp #$80
  beq AtIndentYes
  cmp #$90
  beq AtIndentYes
  cmp #$A0
  beq AtIndentYes
  rts
AtIndentYes:
  lda #$01
  sta temp
  rts
  
  
  
SelectButton:
  SetCursorPos 12,10
  ldy #$0000
  jump jGameGenieDecode
  SetCursorPos 14,10
  ldy #$0008
  jump jGameGenieDecode
  SetCursorPos 16,10
  ldy #$0010
  jump jGameGenieDecode
  SetCursorPos 18,10
  ldy #$0018
  jump jGameGenieDecode
  SetCursorPos 20,10
  ldy #$0020
  jump jGameGenieDecode
  rts
