;;;;;I.MAP - intro 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

  
START:
  
  lda #$40
  sta $4017  ;disable frame IRQ
  
  ldx #$FF   ;reset stack pointer
  txs


  jsr ClearNameTable
  jsr LoadLogo
  
  lda #messageWEBSITE
  sta temp
  jsr StrCopy 
  lda #$0D
  sta printY
  jsr PrintLine

  jsr ClearLine
  lda #'R'
  sta printString+$A
  lda #'O'
  sta printString+$B
  lda #'M'
  sta printString+$C
  lda #'V'
  sta printString+$E
  lda #'E'
  sta printString+$F
  lda #'R'
  sta printString+$10
  lda #'S'
  sta printString+$11
  lda romVers
  beq RomVersOne
  
  lda romVers
  sta printString+$13
  lda #'.'
  sta printString+$14
  lda romVers+1
  sta printString+$15
  lda romVers+2
  sta printString+$16
  jmp RomVersPrint

RomVersOne:  
  lda #'1'
  sta printString+$13
  lda #'.'
  sta printString+$14
  lda #'0'
  sta printString+$15
  lda #'0'
  sta printString+$16



RomVersPrint:
  lda #$19
  sta printY
  jsr PrintLine


  jsr ClearLine
  lda #'C'
  sta printString+$9
  lda #'A'
  sta printString+$A
  lda #'R'
  sta printString+$B
  lda #'D'
  sta printString+$C
  lda #'V'
  sta printString+$E
  lda #'E'
  sta printString+$F
  lda #'R'
  sta printString+$10
  lda #'S'
  sta printString+$11
  lda #'1'
  sta printString+$13
  lda #'.'
  sta printString+$14
  lda #'3'
  sta printString+$15
  lda #'5'
  sta printString+$16
  lda #'B'
  sta printString+$17
  lda #'2'
  sta printString+$18
  
  lda #$1A
  sta printY
  jsr PrintLine
  
  
  
  
  lda #$F0
  sta cursorY
  sta cursorX

  lda #$10
  ldx #$00
ClearGameGenieLoop:  
  sta gameGenie, x
  inx
  cpx #$28
  bne ClearGameGenieLoop
  
  jmp Forever  
  
  
  
  

CONTROLLER:
  jsr ReadJoystick
  lda joystick
  bne ReadyCheck
  lda #$01
  sta moduleReady
  rts

ReadyCheck:
  lda moduleReady
  bne ReadyCheckDone
  rts
ReadyCheckDone:
  
FPGACheck:
  lda joystick
  and #%00100000    ;;select = test  
  bne FPGACheckDone
  
  lda $2002         ;clear 2006 latch
  lda #$3F
  sta $2006
  lda #$00
  sta $2006
  ldx #$02
WritePaletteBlue:
  lda #$3A    ;;green bg
  sta $2007
  lda #$00
  sta $2007   ;;gray scale
  lda #$10
  sta $2007
  lda #$30
  sta $2007

  lda #$3A
  sta $2007
  lda #$28
  sta $2007   ;;title screen graphics
  lda #$2D
  sta $2007
  lda #$30
  sta $2007

  lda #$3A
  sta $2007
  lda #$22
  sta $2007  ;;was solid red, now blue
  lda #$22
  sta $2007
  lda #$22
  sta $2007
  
  lda #$3A
  sta $2007
  lda #$22
  sta $2007   ;;was reds now blues
  lda #$10
  sta $2007
  lda #$11
  sta $2007      
  dex
  bne WritePaletteBlue


  
FPGACheckDone

ModuleLoaded:
  ;check each button
  ; when pressed, go to next module
  lda joystick
  and #%00010000   ;;start button = last game
  bne LoadLast
  
  lda joystick
  and #%11000000  ;;a or b button = continue
  bne NextModule
  rts
  
NextModule:
  ;load the next module from base directory
  lda #'N'
  sta findEntry  
  lda #' '
  sta findEntry+1
  sta findEntry+2
  sta findEntry+3
  sta findEntry+4
  sta findEntry+5
  sta findEntry+6
  sta findEntry+7
  jmp CardLoadModule      ;load NES list module

  
  
LoadLast:
  lda #$60
  sta $0404      ;module not ready, set controller vector to rts
  lda #$00
  sta moduleReady


  lda #'T'
  sta exMatch1+0     ;.TXT
  sta exMatch1+1    
  sta exMatch1+2     
  sta exMatch1+3    

  lda #'X'
  sta exMatch2+0     ;.TXT
  sta exMatch2+1    
  sta exMatch2+2     
  sta exMatch2+3    

  lda #'T'
  sta exMatch3+0     ;.TXT
  sta exMatch3+1    
  sta exMatch3+2     
  sta exMatch3+3    
;-------------------------------------;

  lda baseDirCluster
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3
  jsr CardLoadDir        ; powerpak dir

  lda #'L'
  sta findEntry  
  lda #'A'
  sta findEntry+1       ;;find LASTNES.TXT in powerpak dir
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #'N'
  sta findEntry+4
  lda #'E'
  sta findEntry+5
  lda #'S'
  sta findEntry+6
  lda #' '
  sta findEntry+7


  jsr DirFindEntry   
      
  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             ;;load first sector into ram buffer
  lda #$02
  sta destHi
  jsr ClusterToLBA
  jsr CardReadSector
  
  ldx #$00
  ldy #$00
LoadLastGameLoop:         ;;game name
  lda $0200, y
  sta gameName, x
  iny
  inx
  cpx #32
  bne LoadLastGameLoop

  ldx #$00
LoadLastGameCLoop:         ;;game cluster
  lda $0200, y
  sta gameCluster, x
  iny
  inx
  cpx #4
  bne LoadLastGameCLoop
  
  ldx #$00
LoadLastSaveLoop:         ;;save name 
  lda $0200, y
  sta saveName, x
  iny
  inx
  cpx #32
  bne LoadLastSaveLoop

  ldx #$00
LoadLastSaveCLoop:         ;;save cluster 
  lda $0200, y
  sta wramCluster, x
  iny
  inx
  cpx #4
  bne LoadLastSaveCLoop

  
  ldx #$00
LoadLastGameGenieLoop:    ;;game genie chars 5x8
  lda $0200, y
  sta gameGenie, x
  iny
  inx
  cpx #40
  bne LoadLastGameGenieLoop








  lda gameCluster
  sta sourceCluster
  lda gameCluster+1
  sta sourceCluster+1
  lda gameCluster+2
  sta sourceCluster+2
  lda gameCluster+3      ;copy to game cluster
  sta sourceCluster+3      ;copy to source cluster
  
  lda #$00
  sta destLo
  lda #$02
  sta destHi
  jsr ClusterToLBA
  jsr CardReadSector
  
  ldy #$00
CopyMapperData:
  lda $0200, y
  sta mapperData, y
  iny
  cpy #$10
  bne CopyMapperData   ;;get header info
  
  lda $0200
  cmp #'F'
  beq LoadFDSModule     ;check if FDS file
  jmp LoadNESHeader
  
LoadFDSModule:
  lda $0204    ;64KB banks
  asl a        ;32KB banks
  asl a        ;16KB banks
  asl a        ;8KB banks
  sta prgSize           ;how many 8K banks

  lda #$00
  sta chrSize

  jsr ClearFindEntry
  lda #'F'
  sta findEntry  
  jmp CardLoadModule      ;load FDS loader module 
  

LoadNESHeader:
  lda $0204
  asl a
  sta prgSize           ;how many 8K banks
  lda $0205
  sta chrSize           ;how many 8K banks
  lda $0206
  and #$F0
  lsr a
  lsr a
  lsr a
  lsr a
  sta mapperNumber
  lda $0207
  and #$F0
  clc
  adc mapperNumber
  sta mapperNumber
  lda $0206
  and #$0F
  sta mirroring      ;0x0=Horiz  0x1=Vert   0x8=4screen
  lda $0206
  lsr a
  and #$01
  sta useBattery
  lda $0208
  sta wramBanks
  
  lda $020F
  bne BadHeader    ;;check for disk dude
  

ConvertMapper:          ;convert mapperNumber to mapperModule
  lda mapperNumber
  and #$F0
  lsr a
  lsr a
  lsr a
  lsr a
  adc #$30
  sta mapperModule
  lda mapperModule
  cmp #$3A
  bcc ConvertMapperLow
  lda mapperModule
  clc
  adc #$07
  sta mapperModule
ConvertMapperLow:
  lda mapperNumber
  and #$0F
  adc #$30
  sta mapperModule+1
  lda mapperModule+1
  cmp #$3A
  bcc ConvertMapperDone
  lda mapperModule+1
  clc
  adc #$07
  sta mapperModule+1
ConvertMapperDone:

  jsr ClearFindEntry
  lda #'O'
  sta findEntry  
  lda #' '
  jmp CardLoadModule      ;load options module
 
 
BadHeader:
  jsr ClearFindEntry
  lda #'E'
  sta findEntry  
  jmp CardLoadModule      ;load header error module
  jmp Forever  

  
  
  
  
  
  
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

