;;;;  N.MAP    GAME DISPLAY, show .nes .NES games, do scrolling

  .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 #'N'
  sta exMatch1+0     ;.Nes
  sta exMatch1+1     ;.Nsf
  lda #'F'
  sta exMatch1+2     ;.Fds
  sta exMatch1+3     ;.Fds

  lda #'D'
  sta exMatch2+2     ;.fDs
  sta exMatch2+3     ;.fDs
  lda #'E'
  sta exMatch2+0     ;.nEs
  lda #'S'
  sta exMatch2+1     ;.nSf

  sta exMatch3+0     ;.neS
  sta exMatch3+2     ;.fdS
  sta exMatch3+3     ;.fdS
  lda #'F'
  sta exMatch3+1     ;.nsF
;-------------------------------------;

  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 DirSort  ;;ADDED
  
  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
  
  ldy #$00
CopyGameNameLoop:
  lda tempEntry, y
  sta gameName, y
  iny
  cpy #$1C               ;copy 28 chars
  bne CopyGameNameLoop
  
  lda tempEntry+$1C
  sta gameCluster
  sta sourceCluster
  lda tempEntry+$1D
  sta gameCluster+1
  sta sourceCluster+1
  lda tempEntry+$1E
  sta gameCluster+2
  sta sourceCluster+2
  lda tempEntry+$1F
  sta 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
  
  
  lda $0203             ;check if NESM (nsf) file
  cmp #'M'
  beq LoadNSFModule
  
  lda $0200
  cmp #'F'
  beq LoadFDSModule     ;check if FDS file
  
  jmp LoadNESHeader     ;may be NES file
  
  
LoadNSFModule:
  jsr ClearFindEntry
  lda #'M'
  sta findEntry  
  jmp CardLoadModule      ;load NSF loader module 
  

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 SaveLastFDS

  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 #'A'
  sta findEntry  
  jmp CardLoadModule      ;load AUTO SAVE module, match save name
 
 
BadHeader:
  jsr ClearFindEntry
  lda #'E'
  sta findEntry  
  jmp CardLoadModule      ;load header error module



IntroModule:
  jsr ClearFindEntry
  lda #'I'
  sta findEntry  
  jmp CardLoadModule      ;load NSF loader module 


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

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

  lda joystick
  and oldButtons
  sta heldButtons
  lda joystick
  sta oldButtons


  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 IntroModule

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


EastCheck:
  lda heldButtons
  and #$01
  beq NoButtons
  
  ;handle right button
  lda scrollYCounter    ;no scrolling while scrolling;
  bne CheckDone
  lda cursorYCounter
  bne CheckDone
  jsr CalcScrollSpeed
  jsr ScrollDown
  jmp CheckDone
  
NoButtons:
  lda #$00
  sta scrollHoldTimer
  
CheckDone:
  rts
  
  
  
  


CalcScrollSpeed:
  inc scrollHoldTimer
  lda scrollHoldTimer
  cmp #40
  bcc ScrollSlow
;  cmp #120
;  bcc ScrollMedium
;ScrollFast:
  dec scrollHoldTimer
;  lda #$08
;  sta speedScroll
;  lda #$01
;  sta speedCounter
;  rts
ScrollMedium:
  lda #$04
  sta speedScroll
  lda #$02
  sta speedCounter
  rts
ScrollSlow:
  lda #$02
  sta speedScroll
  lda #$04
  sta speedCounter
  rts

  
  
  
  
  
  
  
  
  
  
SaveLastFDS:
  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 LAST.TXT in powerpak dir
  lda #'S'
  sta findEntry+2
  lda #'T'
  sta findEntry+3
  lda #' '
  sta findEntry+4
  sta findEntry+5
  sta findEntry+6
  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
  
  jsr ClusterToLBA
  
  
  ldx #$00
  ldy #$00
SaveLastGameLoop:         ;;game name
  lda gameName, x
  sta $0200, y
  iny
  inx
  cpx #32
  bne SaveLastGameLoop

  ldx #$00
SaveLastGameCLoop:         ;;game cluster
  lda gameCluster, x
  sta $0200, y
  iny
  inx
  cpx #4
  bne SaveLastGameCLoop
  
  ldx #$00
SaveLastSaveLoop:         ;;save name SAME AS GAME
  lda gameName, x
  sta $0200, y
  iny
  inx
  cpx #32
  bne SaveLastSaveLoop

  ldx #$00
SaveLastSaveCLoop:         ;;save cluster SAME AS GAME
  lda gameCluster, x
  sta $0200, y
  iny
  inx
  cpx #4
  bne SaveLastSaveCLoop

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

  lda #$00
  sta bankCounter
  sta sectorCounter
  sta sourceLo               ;reset to beginning of sector buffer
  lda #$02
  sta sourceHi    
  jsr CardWriteSector    ;write 512 bytes to file

  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


