.INCLUDE "mapper.inc"
.INCLUDE "variables.asm"
Main:
  SEP #$20
  REP #$10
  lda #$FF
  sta cursorX
  lda #$F0
  sta cursorY
  SetCursorPos 12, 0
  PrintString "        Card ROM  v1.05"
  wai
  ldy #$0000
  jump jGameGenieClear
  ldy #$0008
  jump jGameGenieClear
  ldy #$0010
  jump jGameGenieClear
  ldy #$0018
  jump jGameGenieClear
  ldy #$0020
  jump jGameGenieClear



ClearGameName:
  ldy #$0000
  lda #' '
ClearNameLoop:
  sta gameName, y
  iny
  cpy #34
  bne ClearNameLoop
  lda #$00
  sta gameCluster
  sta gameCluster+1
  sta gameCluster+2
  sta gameCluster+3



ClearSaveName:
  ldy #$0000
  lda #' '
ClearSaveNameLoop:
  sta saveName, y
  iny
  cpy #30
  bne ClearSaveNameLoop
  lda #$00
  sta saveCluster
  sta saveCluster+1
  sta saveCluster+2
  sta saveCluster+3
  

  
  lda #$00
  sta CONFIGWRITEDSP  ;;turn off dsp chip
  stz Joy1Press
  stz Joy1Press+1  ;;reset input buttons  
  stz Joy1New
  stz Joy1New+1
  
  lda CONFIGREADSTATUS   ;open bus = $20

;  sta errorCode
;	SetCursorPos 14, 1
;	PrintString "FPGA status="
;  PrintHexNum errorCode
;  lda errorCode
;Wait:
;  jmp Wait


  and #$F0
  cmp #$A0
  beq fpgaISconfigured
  jmp fpgaNOTconfigured
fpgaISconfigured:
;	SetCursorPos 15, 1
;	PrintString "FPGA was configured"
  lda CONFIGREADSTATUS   ;battery used = D1
  and #$02
  bne BatteryUsed
;	SetCursorPos 16, 1
;	PrintString "battery not used"
  jmp Forever
BatteryUsed:
;	SetCursorPos 16, 1
;	PrintString "battery used"
  jmp NextModuleSQ

fpgaNOTconfigured:
  jump jClearFindEntry
  lda #'T'
  sta findEntry
  lda #'O'
  sta findEntry+1
  lda #'P'
  sta findEntry+2
  lda #'L'
  sta findEntry+3
  lda #'E'
  sta findEntry+4
  lda #'V'
  sta findEntry+5
  lda #'E'
  sta findEntry+6
  lda #'L'
  sta findEntry+7
  
  ;lda #$FF
  ;sta $420d  ;;turn ON FASTROM


  lda #$01
  sta FPGAPROGRAMWRITE   ;;SEND PROGRAM SIGNAL TO FPGA
  lda #$00
  sta FPGAPROGRAMWRITE   ;;SEND PROGRAM SIGNAL TO FPGA
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  lda #$01
  sta FPGAPROGRAMWRITE   ;;SEND PROGRAM SIGNAL TO FPGA


    
  jump jCardLoadFPGA
  ;ClearLine 18
	;SetCursorPos 18, 1
	;PrintString "FPGA just configured\n"
  
  wai
  lda #%00000001
  sta CONFIGWRITESTATUS  ;;unlock sdram

  
;	PrintString "unlocked sdram"

	SetCursorPos 24, 0
  jsr FPGACheck
  jsr DSPCheck
  jsr SDRAMCheck

Forever:  

ForeverLoop:
  wai
  
;  ClearLine 27
;	SetCursorPos 27, 1
;	PrintString "FPGA STATUS = "
;  lda CONFIGREADSTATUS
;  sta errorCode
;  PrintHexNum errorCode
  lda Joy1New+1         ;;check for user input  
  and #%11001111
  bne NextModuleSN      ;;go to sn for .smc displaying
  lda Joy1New+0         ;;check for user input  
  and #%11110000
  bne NextModuleSN      ;;go to sn for .smc displaying

SelectCheck:
  lda Joy1New+1         ;;check for select button  
  and #%00100000
  beq SelectCheckDone
  jsr ShowConsoleVersionICheck  ;;select pressed, show version numbers and run memory check
  jsr FPGACheck
  jsr DSPCheck
  jsr SDRAMCheck
  jump jMemCheck

  jmp ForeverLoop
SelectCheckDone:

StartCheck:
  lda Joy1New+1         ;;check for start button  
  and #%00010000
  beq StartCheckDone
  jsr LoadLast          ;;start pressed, load last game info
  jmp NextModuleSO
StartCheckDone:
  jmp ForeverLoop



NextModuleSN:
  jump jClearFindEntry
  lda #'N'
  sta findEntry+1
  jmp NextModule
  
NextModuleSQ:
  jump jClearFindEntry
  lda #'Q'
  sta findEntry+1
  jmp NextModule
  
NextModuleSO:             ;;preload options
  jump jClearFindEntry
  lda #'O'
  sta findEntry+1
  jmp NextModule

NextModule:
  lda #$FF
  sta SpriteBuf1+17     ;;move title graphics off screen
	sta SpriteBuf1+21
	sta SpriteBuf1+25
	sta SpriteBuf1+29
	sta SpriteBuf2+1  ;;32x32 sprites
  lda #'S'
  sta findEntry
  jump jCardLoadModule      ;load SNES list module
  
  
FPGACheck:
  lda #$00
  sta CONFIGWRITEDSP     ;turn off dsp chip
  lda CONFIGREADSTATUS   ;open bus = $20
  sta errorCode
  and #$F0
  cmp #$A0
  beq FPGACheckPass
FPGACheckFail:
  PrintString "\n    FPGA error "  
  PrintHexNum errorCode
FPGACheckFailLoop:
  jmp FPGACheckFailLoop
FPGACheckPass:
  rts

  
  
SDRAMCheck:
  lda $F00000
  sta errorCode
  
  lda #$55
  sta $F00000
  lda $F00000
  sta errorCode+1
  
  lda errorCode
  sta $F00000
  
  lda errorCode+1
  cmp #$55
  
  lda #$AA
  sta $F00000
  lda $F00000
  cmp #$AA
  
  bne SDRAMError
  jmp SDRAMPass1
SDRAMError:
  PrintString "\n         SDRAM error " 
  lda $F00000
  sta errorCode
  PrintHexNum errorCode
SDRAMFail:
  jmp SDRAMFail
SDRAMPass1:
  rts
  
  
DSPCheck:
  lda #$04
  sta CONFIGWRITEDSP  ;;turn on hirom chip     HiROM      $00:6000 = DR, $00:7000 = SR  
  lda.l $007000
  sta errorCode
  and #%10000000
  beq DSPBad
  jmp DSPGood
DSPBad:
  PrintString "\n       DSP1 not installed "
  rts
DSPGood:
  PrintString "\n         DSP1 installed "
  rts




ShowConsoleVersionICheck:
  lda Joy1Press+1
  and #%00100000
  bne ShowConsoleVersionI
  jmp ShowConsoleVersionIDone   ;;hold select to show console version
ShowConsoleVersionI:  
  lda $4210   ;;CPU version
  and #$0F
  sta temp
  lda $213E   ;;PPU1 version
  and #$0F
  sta temp+1
  lda $213F   ;;PPU2 version
  and #$0F
  sta temp+2

  SetCursorPos 13,0
  PrintString "CPU="
  PrintHexNum temp
  PrintString "  PPU1="
  PrintHexNum temp+1
  PrintString " PPU2="
  PrintHexNum temp+2
  PrintString "  DMA="
  lda dontUseDMA
  bne ShowConsoleVersionIOff
ShowConsoleVersionOnI:
  PrintString "ON "
  jmp ShowConsoleVersionIDone
ShowConsoleVersionIOff:
  PrintString "OFF"
ShowConsoleVersionIDone:
  rts
  
  
LoadLast:
  ;;load LAST.TXT file
  lda #'T'
  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 baseDirCluster   ;;powerpak dir start
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3


  jump jClearFindEntry    ;;find LASTSNES.TXT
  lda #'L'
  sta findEntry
  lda #'A'
  sta findEntry+1
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #'S'
  sta findEntry+4
  lda #'N'
  sta findEntry+5
  lda #'E'
  sta findEntry+6
  lda #'S'
  sta findEntry+7

  jump jCardLoadDir    ; powerpak dir
  
  jump jDirFindEntry        ;;get first cluster
  lda tempEntry+$1C
  sta sourceCluster
  lda tempEntry+$1D
  sta sourceCluster+1
  lda tempEntry+$1E
  sta sourceCluster+2
  lda tempEntry+$1F
  sta sourceCluster+3

  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
  
  ldx #$0000
  ldy #$0000
LoadLastGameLoop:         ;;game name and cluster
  lda sectorBuffer1, y
  sta gameName, x
  iny
  inx
  cpx #34
  bne LoadLastGameLoop
  
  ldx #$0000
LoadLastSaveLoop:         ;;save name and cluster
  lda sectorBuffer1, y
  sta saveName, x
  iny
  inx
  cpx #34
  bne LoadLastSaveLoop
  
  ldx #$0000
LoadLastGameGenieLoop:    ;;game genie chars 5x8
  lda sectorBuffer1, y
  sta gameGenie, x
  iny
  inx
  cpx #40
  bne LoadLastGameGenieLoop
  
  rts


  
  
