

;for(i=0; i<0x80; i++)
;        PRG[i]=FILE[i];                         load header into 0000 of prg ram
;
;offset=(FILE[0x08] + (FILE[0x09]<<8)) & 0xfff;   get load address from header
;src=0x80;                                        skip header
;dst=0x2000+offset;                               load address;
;
;do {
;        do {
;                PRG[dst]=FILE[src];              low 4KB
;                PRG[dst+0x1000]=FILE[src];       high 4KB
;                src++;
;                dst++;
;        } while(dst & 0xfff);
;        dst+=0x1000;                             goto next 8kb bank
;} while(!EOF)



;;;NSF PLAYER
;;;  nsf header      0000-007F
;;;  player code 8kb 007F-1FFF
;;;
;;;  nsf file        2000+



  .include "variables.h"
  .include "loader.fns"

  .rsset $F0
destnsf1lo         .rs 1
destnsf1hi         .rs 1
destnsf2lo         .rs 1
destnsf2hi         .rs 1
sectorbyteslo      .rs 1
sectorbyteshi      .rs 1
bankbyteslo        .rs 1
bankbyteshi        .rs 1

nsfloadlo          .rs 1
nsfloadhi          .rs 1



  .bank 0
  .org $0400
  jmp START
  .org $0404    
  jmp CONTROLLER
  
  



  ;;sector bytes counter, get new sector at 512, check if sector is last  0x0200
  ;;bank bytes counter, get new bank at 4096     0x1000
  
  
  
START:
  jsr RenderingOff
  jsr ClearNameTable
  jsr LoadLogo
  
  lda gameCluster
  sta wramCluster
  lda gameCluster+1
  sta wramCluster+1
  lda gameCluster+2
  sta wramCluster+2
  lda gameCluster+3
  sta wramCluster+3
  
  
  

  ldy #$00
LoadingNSFPlayerStrLoop:
  lda LoadingNSFPlayerStr, y
  sta printString, y      ;copy 32 text chars loading NSF
  iny
  cpy #$20
  bne LoadingNSFPlayerStrLoop

  lda #$1B
  sta printY
  jsr PrintLine
  
  lda #$F8
  sta cursorY
  lda #$F8
  sta cursorX
  lda #$00
  sta scrollY           
  sta scrollYCounter    
  sta scrollYUp         
  sta scrollYDown       
  sta cursorYCounter
  sta cursorYUp         
  sta cursorYDown

  jsr RenderingOn


  lda baseDirCluster
  sta sourceCluster
  lda baseDirCluster+1
  sta sourceCluster+1
  lda baseDirCluster+2
  sta sourceCluster+2
  lda baseDirCluster+3
  sta sourceCluster+3

  lda #'N'
  sta exMatch1
  lda #'E'
  sta exMatch2
  lda #'S'
  sta exMatch3

  jsr CardLoadDir    ; powerpak dir

  lda #'N'
  sta findEntry+0
  lda #'S'
  sta findEntry+1
  lda #'F'
  sta findEntry+2
  lda #' '
  sta findEntry+3
  sta findEntry+4
  sta findEntry+5
  sta findEntry+6
  sta findEntry+7

  jsr DirFindEntry       
  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    ;;starting cluster of nsf player
  sta sourceCluster+3
  
  lda #$00
  sta destLo
  lda #$02
  sta destHi
  jsr ClusterToLBA
  jsr CardReadSector

  lda $0204
  asl a
  sta prgSize
  lda $0205
  sta chrSize  
  
  

  lda #$00       ;;#$80 = wram     #$00 = prg ram
  sta destBank   ;;$0000 in prg ram
  sta PRGBANK
  
  jsr LoadPRGRam     ;;16KB prg
  jsr RenderingOff
  jsr LoadCHRRam     ;;8KB chr 


  jsr ClearNameTable
  ldy #$00
LoadingNSFStrLoop:
  lda LoadingNSFStr, y
  sta printString, y      ;copy 32 text chars loading NSF
  iny
  cpy #$20
  bne LoadingNSFStrLoop

  lda #$1B
  sta printY
  jsr PrintLine

  jsr RenderingOn



  lda #$00
  sta destBank          ;bank = 0
  sta PRGBANK           ;;NSF header into bank 0 of prg ram
  sta bankCounter 
  sta sectorCounter
  
  lda wramCluster
  sta sourceCluster
  lda wramCluster+1
  sta sourceCluster+1
  lda wramCluster+2
  sta sourceCluster+2
  lda wramCluster+3
  sta sourceCluster+3
  jsr ClusterToLBA
  
  


  lda #$60
  sta destHi            ;dest = 6080
  lda #$80
  sta destLo

  jsr CardReadSector128  ;card read 16 bytes  8x  NSF header = 128 bytes

  lda #$80
  sta sectorbyteslo     ;sector bytes counter = 128
  lda #$00
  sta sectorbyteshi





  lda $6088
  sta nsfloadlo
  lda $6089
  sta nsfloadhi         ;load address from NSF header


  lda #$01
  sta destBank
  sta PRGBANK           ;bank = 1
  
  lda nsfloadlo
  sta bankbyteslo
  lda nsfloadhi
  and #$0F
  sta bankbyteshi       ;bank bytes counter = load address & $0FFF
  
  lda bankbyteslo
  sta destnsf1lo
  sta destnsf2lo
  lda bankbyteshi
  clc
  adc #$60
  sta destnsf1hi        ;destnsf1 = bank bytes + $6000
  adc #$10
  sta destnsf2hi        ;destnsf2 = bank bytes + $7000
  
  
  

    
LoadNSFLoop:
  ldy #$00
  lda CARDDATAREAD      ;read byte
  sta [destnsf1lo], y   ;lo 4KB
  sta [destnsf2lo], y   ;hi 4KB

  lda destnsf1lo
  clc
  adc #$01
  sta destnsf1lo        ;inc destnsf1
  lda destnsf1hi
  adc #$00
  sta destnsf1hi

  lda destnsf2lo
  clc
  adc #$01
  sta destnsf2lo        ;inc destnsf2
  lda destnsf2hi
  adc #$00
  sta destnsf2hi

  lda bankbyteslo
  clc
  adc #$01
  sta bankbyteslo       ;inc bank byte counter
  lda bankbyteshi
  adc #$00
  sta bankbyteshi

  lda sectorbyteslo
  clc
  adc #$01
  sta sectorbyteslo       ;inc sector byte counter
  lda sectorbyteshi
  adc #$00
  sta sectorbyteshi


LoadNSFLoopBankCheck:
  lda bankbyteslo
  bne LoadNSFLoopBankCheckDone
  lda bankbyteshi
  cmp #$10
  bne LoadNSFLoopBankCheckDone   ;check for bank end at $1000 bank bytes - 4KB
  
LoadNSFLoopBankCheckNext:
  lda #$00
  sta bankbyteslo
  sta bankbyteshi         ;bank bytes counter = 0
  
  lda destBank
  clc
  adc #$01                ;inc bank
  sta destBank
  sta PRGBANK
  
  lda #$00
  sta destnsf1lo
  sta destnsf2lo
  lda #$60
  sta destnsf1hi          ;destnsf1 = $6000
  lda #$70
  sta destnsf2hi          ;destnsf2 = $7000
LoadNSFLoopBankCheckDone:







LoadNSFLoopSectorCheck:
  lda sectorbyteslo
  bne LoadNSFLoopSectorDone
  lda sectorbyteshi
  cmp #$02
  bne LoadNSFLoopSectorDone   ;check for sector end at 0200 sector bytes - 512 bytes
  

NSFStartNextSector:

  lda #$00
  sta sectorbyteslo
  sta sectorbyteshi


  jsr LoadNextSectorNum      ;;get next sector/cluster num

NSFStartNextSectorCheckCluster16:
  lda sourceCluster
  cmp #$FF
  bne NSFStartNextSectorRead  ;cluster != last cluster
  
  lda sourceCluster+1
  cmp #$FF
  bne NSFStartNextSectorRead  ;cluster != last cluster
  
  lda fat32Enabled
  beq LoadNSFDone   ;if fat 16, done with file
  
NSFStartNextSectorCheckCluster32:
  lda sourceCluster+2
  cmp #$FF
  bne NSFStartNextSectorRead  ;cluster != last cluster
  
  lda sourceCluster+3
  and #$0F
  cmp #$0F
  bne NSFStartNextSectorRead  ;cluster != last cluster
  
  jmp LoadNSFDone  ;fat 32, done with file





NSFStartNextSectorRead:    ;;wasnt end of file
  jsr CardCheckError       ;;send read command for next sector
  jsr CardWaitNotBusy
  jsr CardLoadLBA
  jsr CardCheckError

  lda #$20
  sta CARDCOMMAND           ;start card read sector command
  nop
  nop

  jsr CardCheckError
  jsr CardWaitDataReq

LoadNSFLoopSectorDone:
  jmp LoadNSFLoop
  
  
  
  
LoadNSFDone:  
  
  jsr RenderingOff

  jsr LoadFPGA 
  lda #$00
  sta BOOTENWR
  jmp [$FFFC]







CardReadSector128:

  jsr CardWaitNotBusy
  jsr CardLoadLBA

  lda #$20
  sta CARDCOMMAND           ;send card read sector command
  nop
  nop
  
  jsr CardCheckError
  jsr CardWaitDataReq
  
  ldy #$00
CardReadSector128Loop:         ;read 128 bytes into ram
  lda CARDDATAREAD
  sta [destLo], y
  iny
  cpy #$80
  bne CardReadSector128Loop
  
  rts



ForeverNSF:
  jmp ForeverNSF


LoadingNSFStr:
  ;    12345678901234567890123456789012
  .db "        Loading NSF...          "

LoadingNSFPlayerStr:
  ;    12345678901234567890123456789012
  .db "    Loading NSF Player...       "


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

CONTROLLER:
  rts


  .org    $0800
  .incbin "nsf2.bit"




