;;; O.MAP   options screen


  .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

            ;0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F   10 
GGCHARS .db 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y', 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N', '_'

  
START:

  jsr RenderingOff
  jsr ClearNameTable
  
  lda #$FF
  sta powerOn
  
;  lda #$10
;  ldx #$00
;ClearGameGenieLoop:  
;  sta gameGenie, x
;  inx
;  cpx #$28
;  bne ClearGameGenieLoop
  
  ;put options screen in name table 
  lda #messageLOADRAM
  sta temp
  jsr StrCopy       ;load b ram
  lda #$06
  sta printY
  jsr PrintLine
  
  jsr ClearLine
  ldy #$00
SaveNameLoop:
  lda saveName, y
  sta printString+3, y      ;;;;;print save name here
  iny
  cpy #$1C
  bne SaveNameLoop
;  lda #$07
;  sta printY
  inc printY
  jsr PrintLine

  lda #messageGG
  sta temp
  jsr StrCopy       ;game genie
  lda #$0A
  sta printY
  jsr PrintLine


  lda #messageSTARTGAME
  sta temp
  jsr StrCopy       ;Start
  lda #$17
  sta printY
  jsr PrintLine

  jsr ClearLine
  ldy #$00
GameNameLoop:
  lda gameName, y
  sta printString+3, y      ;;;;;print game name here
  iny
  cpy #$1C
  bne GameNameLoop
;  lda #$18
;  sta printY
  inc printY
  jsr PrintLine
  
  

;;;;;do arcade dips
;  jsr PrintArcadeDip
;  jsr PrintLine

;;;;do underscores
  lda #$0C
;  sta printY
  jsr PrintGameGenie
  jsr PrintLine
;;;;do underscores
  lda #$0E
;  sta printY
  jsr PrintGameGenie
  jsr PrintLine
;;;;do underscores
  lda #$10
;  sta printY
  jsr PrintGameGenie
  jsr PrintLine
;;;;do underscores
  lda #$12
;  sta printY
  jsr PrintGameGenie
  jsr PrintLine
;;;;do underscores
  lda #$14
;  sta printY
  jsr PrintGameGenie
  jsr PrintLine  
  
  
  lda #$B8
  sta cursorY
  lda #$38
  sta cursorX


  jsr RenderingOn
  jmp Forever
 
  

LoadGGLabelModule:
  lda #'G'
  jmp NextModule
;  jsr RenderingOff
;  jsr ClearFindEntry  
;  jmp CardLoadModule

  

LoadGameModule:     ;;first save LAST game info
  lda #'L'
  jmp NextModule
;  jsr RenderingOff
;  jsr ClearFindEntry  
;  lda #'L'
;  sta findEntry
;  jmp CardLoadModule





PrintGameGenie:
  sta printY
  jsr ClearLine
  ldy #$09
  
  lda printY
  sec
  sbc #$0C
  asl a
  asl a
  sta gameGenieOffset
PrintGameGenieLoop:
  ldx gameGenieOffset
  lda gameGenie, x
  tax
  lda GGCHARS, x          ;;convert 00-0F to A-Z
  sta printString, y
  inc gameGenieOffset
  iny
  iny
  cpy #$19
  bne PrintGameGenieLoop
  rts
  

GameGenieGetOffset:
  lda cursorY
  sec
  sbc #$60
  sta temp     ;game genie 0,8,16,24,32
  lsr temp
  lda cursorX
  sec
  sbc #$40
  lsr a
  lsr a
  lsr a
  lsr a
  sta temp+1  ;char 0..7
  lda temp
  clc
  adc temp+1
  sta gameGenieOffset
  rts
    
GameGenieNextChar:
  jsr GameGenieGetOffset
  ldx gameGenieOffset
  
  lda gameGenie, x
  clc
  adc #$01
  sta gameGenie, x
  
  lda gameGenie, x
  cmp #$11
  bne GameGenieCharStore
  lda #$00
  sta gameGenie, x
  jmp GameGenieCharStore

GameGeniePrevChar:
  jsr GameGenieGetOffset
  ldx gameGenieOffset
  
  lda gameGenie, x
  sec
  sbc #$01
  sta gameGenie, x
  
  lda gameGenie, x
  cmp #$FF
  bne GameGenieCharStore
  lda #$10
  sta gameGenie, x
  jmp GameGenieCharStore
  
GameGenieCharStore:
  lda cursorY
  lsr a
  lsr a
  lsr a
;  sta printY
  jsr PrintGameGenie
  rts

  
NextButton:
  lda cursorY
  cmp #$30
  bne NextCheckGGLabel
;  jsr LoadBRAMModule   ;load battery ram
LoadBRAMModule:
;  jsr ClearFindEntry
  lda #'B'
  jmp NextModule
;  sta findEntry
;  jmp CardLoadModule
;  rts

NextCheckGGLabel:
  lda cursorY
  cmp #$50
  bne NextCheckStart
  jmp LoadGGLabelModule   ;game genie text file

NextCheckStart:
  lda cursorY
  cmp #$B8
  bne NextCheckGameGenie
  jmp LoadGameModule   ;start game
;  rts
  
NextCheckGameGenie:
;  lda cursorY
;  cmp #$60
;  bcc NextCheckDone
;  cmp #$A1
;  bcs NextCheckDone

  lda cursorY
;  cmp #$60
;  beq NextCheckGameGenieNext     ;game genie
;  cmp #$70
;  beq NextCheckGameGenieNext
;  cmp #$80
;  beq NextCheckGameGenieNext
;  cmp #$90
;  beq NextCheckGameGenieNext
;  cmp #$A0
;  beq NextCheckGameGenieNext
  jsr Compare60A0
  beq NextCheckGameGenieNext
  jmp NextCheckDone
NextCheckGameGenieNext:
  jsr GameGenieNextChar
NextCheckDone:
  rts
  
  
PrevButton:
  lda cursorY
  cmp #$30
  bne PrevCheckGGLabel
  jmp LoadBRAMModule   ;load battery ram
;  rts


PrevCheckGGLabel:
  lda cursorY
  cmp #$50
  bne PrevCheckStart
  jmp LoadGGLabelModule   ;game genie text file


PrevCheckStart:
  lda cursorY
  cmp #$B8
  bne PrevCheckGameGenie
  jmp LoadGameModule   ;start game
;  rts
  
PrevCheckGameGenie
;  lda cursorY
;  cmp #$60
;  bcc PrevCheckDone
;  cmp #$A1
;  bcs PrevCheckDone
  
  
  lda cursorY
;  cmp #$60
;  beq PrevCheckGameGeniePrev     ;game genie
;  cmp #$70
;  beq PrevCheckGameGeniePrev
;  cmp #$80
;  beq PrevCheckGameGeniePrev
;  cmp #$90
;  beq PrevCheckGameGeniePrev
;  cmp #$A0
;  beq PrevCheckGameGeniePrev
  jsr Compare60A0
  beq PrevCheckGameGeniePrev
  jmp PrevCheckDone
PrevCheckGameGeniePrev:
  jsr GameGeniePrevChar
PrevCheckDone:
  rts
  
  

  
      
CONTROLLER:
  jsr ReadJoystick

  lda joystick
  bne ReadyCheck
  lda #$01
  sta moduleReady
  rts

ReadyCheck:
  lda moduleReady
  bne ACheck
  rts

ACheck:
  lda #$00
  sta moduleReady

  lda joystick
  and #$80
  beq BCheck
  jsr NextButton
;  jmp CheckDone

BCheck:
  lda joystick
  and #$40
  beq SelectCheck
  jsr PrevButton
;  jmp CheckDone
    
SelectCheck:
  lda joystick
  and #$20
  beq StartCheck
  jsr SelectButton
;  jmp CheckDone
  
StartCheck:
  lda joystick
  and #$10
  beq NorthCheck
;  jsr RenderingOff
;  jsr ClearFindEntry
  lda #'I'
  jmp NextModule
;  sta findEntry
;  jmp CardLoadModule      ;load intro module
  




NorthCheck:
  lda joystick
  and #$08
  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 (80)
  ;;  go to game genie 2 (70)
  ;;if at game genie (70)
  ;; go to game genie 1 (60)
  ;;if at game genie (60)
  ;; 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 joystick
  and #$04
  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 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 joystick
  and #$02
  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 joystick
  and #$01
  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
  jsr Compare60A0
  beq AtIndentYes
  rts
AtIndentYes:
  lda #$01
  sta temp
  rts
  
  
  
  
Compare60A0:
  cmp #$60
  beq Compare60A0True  
  cmp #$70
  beq Compare60A0True  
  cmp #$80
  beq Compare60A0True  
  cmp #$90
  beq Compare60A0True  
  cmp #$A0
  beq Compare60A0True  
Compare60A0False:
  lda #$01
  rts
Compare60A0True:
  lda #$00
  rts
  
SelectButton:  ;show mapper info
;  jsr RenderingOff
;  jsr ClearLine  
;  lda #'M'
;  sta printString+$0      ;; display mapper
;  lda #'A'
;  sta printString+$1
;  lda #'P'
;  sta printString+$2
;  lda mapperModule
;  sta printString+$4
;  lda mapperModule+1
;  sta printString+$5
;  lda #$1A
;  sta printY
;  jsr PrintLine
  
  ;jsr ClearLine           ;; display mirroring
  ;lda #'M'
  ;sta printString+$0
  ;lda #'I'
  ;sta printString+$1
  ;lda #'R'
  ;sta printString+$2
  ;clc
  ;lda mirroring
  ;adc #32
  ;sta printString+$4
  ;lda #$1B
  ;sta printY
  ;jsr PrintLine

  ;jsr ClearLine           ;; display prg
  ;lda #'P'
  ;sta printString+$0
  ;lda #'R'
  ;sta printString+$1
  ;lda #'G'
  ;sta printString+$2
  ;clc
  ;lda prgSize
  ;adc #32
  ;sta printString+$4
  ;lda #$1C
  ;sta printY
  ;jsr PrintLine

  ;jsr ClearLine           ;; display chr
  ;lda #'C'
  ;sta printString+$0
  ;lda #'H'
  ;sta printString+$1
  ;lda #'R'
  ;sta printString+$2
  ;clc
  ;lda prgSize
  ;adc #32
  ;sta printString+$4
  ;lda #$1D
  ;sta printY
  ;jsr PrintLine    

  ;jsr ClearLine           ;; display bat 
  ;lda #'B'
  ;sta printString+$0
  ;lda #'A'
  ;sta printString+$1
  ;lda #'T'
  ;sta printString+$2
  ;clc
  ;lda useBattery
  ;adc #32
  ;sta printString+$4
 ; lda #$1D
  ;sta printY
  ;jsr PrintLine      
  
  jsr RenderingOn
  rts
  
NextModule:
  sta tempEntry
  jsr RenderingOff
  jsr ClearFindEntry
  lda tempEntry
  sta findEntry
  jmp CardLoadModule      ;load intro module



  
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
