Advertisement
NovaYoshi

PowerPak boot ROM

Mar 25th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.   .inesprg 4 ;04 x 16k prg bank   64KB = 4x16KB  8x8KB
  3.   .ineschr 0    ;no chr bank
  4.   .inesmir 1    ; VERTICAL mirroring
  5.   .inesmap 2    ;uxrom
  6.  
  7.   .include "variables.h"
  8.  
  9.   .bank 0
  10.   .org    $8000
  11.   .incbin "loadertop.bit"   ;;BOOT FPGA CONFIG FILE  xc2s30 = 44KB of 48KB     fpga = 42096 bytes
  12.  
  13.    
  14.    
  15.   .bank 6
  16.   .org $8000
  17.   .incbin "powerpak.nes"   ;;GRAPHICS FILE 4KB
  18.   .incbin "powerpak.nes"   ;;GRAPHICS FILE 4KB
  19.    
  20.   .bank 7   ;;LAST BANK OF FLASH ROM 8KB
  21.   .org $E000
  22.  
  23.  
  24. ;16KB  8KB  banks
  25. ;0     0/1 - fpga config 16KB
  26. ;1     2/3 - fpga config 16KB
  27. ;2     4/5 - fpga config 12KB
  28. ;3     6/7 - chr in first 8KB, boot code in last 8KB
  29.  
  30.  
  31.  
  32. ;--------------------Set Up Program--------------------:
  33. ;------------------------------------------------------:
  34.  
  35.  
  36. MESSAGETABLE .dw PP,NOCARD,NOTREADY,FORMAT,RERR,WERR,NOTFOUND,LOADDIR,LOADRAM,GG,WEBSITE,STARTG,LOADGAME,HEADER,DUDE,SAVERAM,CFBUSY
  37. PP:           .db "            PowerPak            "
  38. NOCARD:       .db "   Card Error - No Card Found   "
  39. NOTREADY:     .db "      Card Error - Not Ready    "
  40. FORMAT:       .db "    Bad Format - Use FAT16/FAT32"
  41. RERR:         .db "      Fatal Card Read Error     "
  42. WERR:         .db "     Fatal Card Write Error     "
  43. NOTFOUND:     .db "         File Not Found         "
  44. LOADDIR:      .db "      Loading Directory...      "
  45. LOADRAM:      .db "        Load Battery RAM        "
  46. GG:           .db "        Game Genie Codes        "
  47. WEBSITE:      .db "        www.retrousb.com        "
  48. STARTG:       .db "        Start Game              "
  49. LOADGAME:     .db "         Loading Game...        "
  50. HEADER:       .db "         Bad iNES Header        "
  51. DUDE:         .db "       Say NO to DiskDude       "
  52. SAVERAM:      .db "      Saving Battery RAM...     "
  53. CFBUSY:       .db "           Card Busy            "
  54. LOGO1:        .db $80, $81, $82, $83, $84, $85, $86, $87
  55. LOGO2:        .db $90, $91, $92, $93, $94, $95, $96, $97
  56. LOGO3:        .db $A0, $A1, $A2, $A3, $A4, $A5, $A6, $A7
  57. LOGO4:        .db $B0, $B1, $B2, $B3, $B4, $B5, $B6, $B7
  58. LOGO5:        .db $C0, $C1, $C2, $C3, $C4, $C5, $C6, $C7
  59. LOGO6:        .db $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7
  60. LOGO7:        .db $E0, $E1, $E2, $E3, $E4, $E5, $E6, $E7
  61. LOGO8:        .db $F0, $F1, $F2, $F3, $F4, $F5, $F6, $F7
  62.  
  63. IRQ:
  64.   rti
  65.  
  66.  
  67.  
  68. NMI:
  69.   ;backup registers
  70.   php     ;push status
  71.   pha     ;push a
  72.   tya
  73.   pha     ;push y
  74.   txa
  75.   pha     ;push x  
  76.  
  77.     lda #%00000110           ;graphics off
  78.   sta $2001
  79.    
  80.   ;do graphics updates
  81.   jsr PrintLine
  82.  
  83.  
  84. MoveSprite:
  85.   ;move sprite 0
  86.   lda $2002
  87.   lda #$3F
  88.   sta $2003
  89.   lda #$10
  90.   sta $2003
  91.   lda cursorY      ; initialize Sprite 0 = cursor
  92.   sec
  93.   sbc #$01  
  94.   sta $2004        ; Y coordinate
  95.   lda #$10
  96.   sta $2004        ; Pattern number
  97.   lda #$03
  98.   sta $2004        ; color/attributes  CHANGE ME cursor color
  99.   lda cursorX
  100.   sta $2004        ; X coordinate  
  101.  
  102.    
  103. SetScroll:    
  104.     lda #%10001000           ;nmi on  
  105.   sta $2000
  106.     lda #%00011110           ;4 sprites on, 3 background on
  107.   sta $2001  
  108.   lda $2002
  109.   lda #$00
  110.   sta $2006
  111.   sta $2006
  112.   sta $2005  ;scroll x
  113.   lda scrollY
  114.   sta $2005
  115.   ;all nmi graphics done by here;
  116.   inc frameCounter
  117.   ;restore registers
  118.   pla
  119.   tax   ;pop x
  120.   pla
  121.   tay   ;pop y
  122.   pla   ;pop a
  123.   plp
  124.   rti
  125.  
  126.  
  127.  
  128.  
  129. DoFrame:  
  130.   jsr JoystickRead
  131.   jsr $0404             ;go to module controller handler vector
  132.  
  133. CheckCursor:
  134.   ;check cursor counter, no scroll if counter=0
  135.   lda cursorYCounter
  136.   cmp #$00
  137.   beq CursorDone
  138.   dec cursorYCounter  
  139.   ;scroll = scroll + scrollUp - scrollDown
  140. DoCursorScroll:
  141.   lda cursorY
  142.   sec
  143.   sbc cursorYUp
  144.   clc
  145.   adc cursorYDown
  146.   sta cursorY
  147. CursorDone:
  148.  
  149.    
  150.    
  151. CheckYScroll:
  152.   ;check scroll counter, no scroll if counter=0
  153.   lda scrollYCounter
  154.   beq DoYScrollDone
  155.   dec scrollYCounter  
  156.   ;scroll = scroll - scrollUp + scrollDown
  157. DoYScroll:
  158.   lda scrollY
  159.   sec
  160.   sbc scrollYUp
  161.   clc
  162.   adc scrollYDown
  163.   sta scrollY
  164.     cmp #$F0 ; 240
  165.     bcc DoYScrollDone  ; scrollY < 240, no underflow/overflow
  166. DoYScrollOverflow:
  167.     cmp #$F8    ; 248
  168.     bcs DoYScrollUnderflow  ; scrollY > 248, no overflow
  169.     sec
  170.     sbc #$F0    ; scroll = scroll - 240
  171.     sta scrollY
  172.     jmp DoYScrollDone
  173. DoYScrollUnderflow:
  174.     lda scrollY
  175.     sec
  176.     sbc #$10
  177.     sta scrollY
  178. DoYScrollDone:
  179.  
  180.   rts
  181.  
  182.  
  183.  
  184. ;---------------------------------;
  185.  
  186.  
  187. ScrollUp:
  188.   sec
  189.   lda selectedEntry
  190.   sbc #$01
  191.   sta selectedEntry
  192.   lda selectedEntry+1              ;go to prev entry
  193.   sbc #$00
  194.   sta selectedEntry+1
  195.  
  196.   lda selectedEntry
  197.   cmp #$FF
  198.   bne ScrollUpCheckTop
  199.   lda selectedEntry+1
  200.   cmp #$FF
  201.   bne ScrollUpCheckTop             ;check if selectedEntry = FFFF (underflow)
  202.  
  203.   lda filesInDir
  204.   sta selectedEntry
  205.   lda filesInDir+1
  206.   sta selectedEntry+1              ;underflow, reset selectedEntry = filesInDir-1
  207.   sec
  208.   lda selectedEntry
  209.   sbc #$01
  210.   sta selectedEntry
  211.   lda selectedEntry+1              
  212.   sbc #$00
  213.   sta selectedEntry+1  
  214.  
  215.   lda filesInDir+1
  216.   cmp #$00
  217.   bne ScrollUpCheckTop
  218.   lda filesInDir
  219.   cmp #$1d
  220.   bcs ScrollUpCheckTop              ; check if filesInDir > 28
  221.  
  222.   lda filesInDir
  223.   jsr ASLA3                         ;multiply by 8 for sprite height
  224.   clc
  225.   adc #$08
  226.   sta cursorY                       ; put cursor at bottom of list
  227.   jmp ScrollUpCheckMiddle
  228.  
  229. ScrollUpCheckTop:
  230.   lda cursorY
  231.   cmp #$08
  232.   bne ScrollUpCheckMiddle           ;check if cursor at top
  233.  
  234.   lda speedCounter
  235.   sta scrollYCounter                ;cursor at top, scroll background, leave cursor
  236.   lda speedScroll
  237.   sta scrollYUp
  238.   lda #$00
  239.   sta scrollYDown
  240.  
  241.   lda scrollY
  242.   jsr LSRA3
  243.   sta printY                      
  244.   jsr DirPrintEntry
  245.  
  246.   jmp ScrollUpDone
  247.    
  248. ScrollUpCheckMiddle:    
  249.   lda speedCounter
  250.   sta cursorYCounter
  251.   lda speedScroll
  252.   sta cursorYUp
  253.   lda #$00
  254.   sta cursorYDown
  255.  
  256. ScrollUpDone:
  257.   rts
  258.  
  259.  
  260.  
  261.  
  262.  
  263. ScrollDown:
  264.   clc
  265.   lda selectedEntry
  266.   adc #$01
  267.   sta selectedEntry
  268.   lda selectedEntry+1              ;go to next entry
  269.   adc #$00
  270.   sta selectedEntry+1
  271.  
  272.   lda selectedEntry
  273.   cmp filesInDir
  274.   bne ScrollDownCheckBottom
  275.   lda selectedEntry+1
  276.   cmp filesInDir+1
  277.   bne ScrollDownCheckBottom        ;check if selectedEntry = filesInDir
  278.  
  279.   lda #$00
  280.   sta selectedEntry
  281.   sta selectedEntry+1              ;overflow, reset to 0
  282.  
  283.   lda filesInDir+1
  284.   cmp #$00
  285.   bne ScrollDownCheckBottom
  286.   lda filesInDir
  287.   cmp #$1d
  288.   bcs ScrollDownCheckBottom         ; check if filesInDir > 28
  289.  
  290.   lda #$00
  291.   sta cursorY                       ; put cursor at top of screen
  292.   jmp ScrollDownCheckMiddle
  293.  
  294. ScrollDownCheckBottom:
  295.   lda cursorY
  296.   cmp #$E0   ;= pixel 224
  297.   bne ScrollDownCheckMiddle         ; check if cursor at bottom
  298.  
  299.   lda speedCounter                         ;cursor at bottom, move background, leave cursor
  300.   sta scrollYCounter                ;set scrollYCounter (8 or 4)
  301.   lda speedScroll
  302.   sta scrollYDown                   ;set scrollYDown to speed (1 or 2)
  303.   lda #$00
  304.   sta scrollYUp
  305.  
  306.   lda scrollY
  307.   jsr LSRA3                       ; a = scroll div 8
  308.   sec  
  309.   sbc #$01                         ; a = a - 1
  310.   cmp #$FF
  311.   bne ScrollDownCheckBottomDone
  312.   lda #$1D                         ; if a = ff, a = 29
  313.  
  314. ScrollDownCheckBottomDone  
  315.   sta printY                    
  316.   jsr DirPrintEntry
  317.  
  318.   jmp ScrollDownDone
  319.    
  320. ScrollDownCheckMiddle:
  321.   lda speedCounter
  322.   sta cursorYCounter
  323.   lda speedScroll
  324.   sta cursorYDown
  325.   lda #$00
  326.   sta cursorYUp
  327.    
  328. ScrollDownDone:
  329.   rts
  330.  
  331.  
  332.  
  333.  
  334.  
  335. ;--------------------Main Code-------------------------:
  336. ;------------------------------------------------------:
  337.  
  338. RESET:
  339.   cld
  340.   sei
  341.  
  342.   ;turn rendering OFF
  343.   lda #$00  
  344.   sta $2000     ;7 vblank int, 4 screen addr, 3 sprite addr, 2 vertical wr, 10 name table addr  
  345.   sta $2001     ;765 bg color, 4 sprites on, 3 screen on, 2 sprite mask, 1 image mask
  346.  
  347.  
  348.   lda #$40
  349.   sta $4017  ;disable frame IRQ
  350.  
  351.   ldx #$FF   ;reset stack pointer
  352.   txs
  353.  
  354.   jmp ClearRam
  355. ClearRamReturn:    
  356.   lda #$60
  357.   sta $0400    ;add vectors for dummy module, $60 = rts
  358.   sta $0404
  359.  
  360.  
  361.   ;load from game genie address
  362.   lda $4208
  363.   cmp #$01
  364.   beq ResetFPGAConfigured     ;;fpga was configured and battery must be saved
  365.   jmp ResetFPGANotConfigured  ;;fpga wasnt configured, or battery not saved
  366.  
  367. ResetFPGAConfigured:
  368.   ;  fpga is configured, poweron = 0
  369.   lda #$00
  370.   sta powerOn
  371.   jmp ResetCopyFPGA
  372.  
  373. ResetFPGANotConfigured:
  374.   ;  fpga isnt configured, poweron = 1
  375.   lda #$01
  376.   sta powerOn
  377.   jmp ResetCopyFPGA
  378.  
  379. ResetCopyFPGA:
  380.   ;copy fpga data from rom $8000... to fpga
  381.   jsr FPGACopyRom
  382.  
  383.   jsr WaitVBlank
  384.   jsr WaitVBlank
  385.   jsr WaitVBlank
  386.  
  387.   lda #$40
  388.   sta PRGBANK  ;enable chr ram writing
  389.  
  390.   ;copy chr tiles from rom to chrram  8KB to ppu $0000
  391.   lda #$00
  392.   sta sourceLo
  393.   sta destLo
  394.   sta destHi
  395.   lda #$80
  396.   sta sourceHi
  397.   lda #$20      ;32*256 = 8KB
  398.   sta source256
  399.   jsr CopyChrRam
  400.  
  401.   lda #$00
  402.   sta PRGBANK  ;disable chr ram writing
  403.  
  404.   jsr ClearNameTable
  405.  
  406.   ;               00 TEXT CART        01  CF         10  RED         11  CART/RED
  407.   ;               blk dgr gry wht   blk ye gry wht  blk lrd rd drd   blk lrd gry rd
  408.   ;IMAGE PALETTE    3E 00 10 30      3E 28 10 30     3E 26 16 17      3E 17 10 16
  409.  
  410.     ;Write image then sprite palettes 32 bytes
  411.   lda $2002         ;clear 2006 latch
  412.   lda #$3F
  413.   sta $2006
  414.   lda #$00
  415.   sta $2006
  416.   ldx #$02
  417. WritePalette:
  418.   lda #$3E
  419.   sta $2007
  420.   lda #$00
  421.   sta $2007
  422.   lda #$10
  423.   sta $2007
  424.   lda #$30
  425.   sta $2007
  426.  
  427.   lda #$3E
  428.   sta $2007
  429.   lda #$28
  430.   sta $2007
  431.   lda #$2D
  432.   sta $2007
  433.   lda #$30
  434.   sta $2007
  435.  
  436.   lda #$3E
  437.   sta $2007
  438.   lda #$26
  439.   sta $2007
  440.   lda #$26
  441.   sta $2007
  442.   lda #$26
  443.   sta $2007
  444.  
  445.   lda #$3E
  446.   sta $2007
  447.   lda #$26
  448.   sta $2007
  449.   lda #$10
  450.   sta $2007
  451.   lda #$16
  452.   sta $2007      
  453.   dex
  454.   bne WritePalette
  455.  
  456.    
  457.   ;put intro screen in name table
  458.   lda #messagePP           ;load message = POWERPAK
  459.   sta temp
  460.   jsr StrCopy
  461.   lda #$05
  462.   sta printY
  463.   jsr PrintLine
  464.      
  465.   lda #$F8
  466.   sta cursorX
  467.   sta cursorY
  468.   jsr RenderingOn
  469.  
  470.                        
  471.  
  472.   nop
  473.   nop
  474.   nop
  475.   nop
  476.   nop
  477.   nop
  478.   ;nop
  479.   ;nop
  480.   ;nop
  481.   jmp NewCFBoot    ;;new card booting, skips to CardReady when done
  482.  
  483. CardBootLoop:
  484.   ;read status byte   all good = 01010000 = $50
  485.   ;                   no card = 11111111 = $FF
  486.   lda CARDSTATUS           ;card status read
  487.   cmp #$FF                 ;compare to 11111111
  488.   bne CheckCardReset
  489.   lda #messageNOCARD       ;load message = no card
  490.   sta temp
  491.   jsr StrCopy
  492.   lda #$07                 ; message position
  493.   sta printY
  494.   jmp ForeverLoop          ;stop everything
  495.  
  496.  
  497.  
  498. CheckCardReset:
  499.   lda #$01  ; directly loads the Accumulator with 1
  500.   sta $4016 ; stores the Accumulator (1) into $4016
  501.   lda #$00  ; directly loads the Accumulator with 0
  502.   sta $4016 ; stores the Accumulator (0) into $4016
  503.  
  504.   lda $4016 ; loads the info from the joystick
  505.   and #$01  ; ||
  506.   cmp #$00
  507.   beq CardInserted         ;if no button is pressed, no card reset
  508. CardReset:                 ;if A is pressed, do card reset
  509.   lda #%00000110           ;do card sw reset
  510.   sta CARDDEVICE           ;in device ctl reg
  511.   nop
  512.   nop
  513.   nop
  514.   nop
  515.   nop
  516.   lda #%00000010
  517.   sta CARDDEVICE           ;clear reset
  518.   nop
  519.   nop
  520.   jmp CardBootLoop
  521.  
  522.  
  523. CardInserted:
  524.   lda CARDSTATUS           ;card status read
  525.   sta sourceSector         ;store somewhere unused
  526.   and #%10000000
  527.   cmp #%10000000           ;check busy bit
  528.   bne CardNotBusy
  529.  
  530. CardBusy:
  531.   lda #messageCARDBUSY     ;load message = card busy
  532.   sta temp
  533.   jsr StrCopy
  534.   lda #$1A                
  535.   sta printY
  536.  
  537.   lda sourceSector         ;card status read
  538.   sta temp
  539.   jsr Byte2Str  
  540.  
  541.   lda sourceSector+1
  542.   clc
  543.   adc #$01
  544.   sta sourceSector+1
  545.   beq CardReset
  546.  
  547.   jmp CardBootLoop
  548.  
  549.  
  550.  
  551.  
  552. CardNotBusy:
  553.   lda CARDSTATUS           ;card status read
  554.   sta sourceSector
  555.   and #%01010000
  556.   cmp #%01010000           ;check ready bit
  557.   beq CardReady
  558.  
  559. CardNotReady:
  560.   lda #messageNOTREADY     ;load message = card not ready
  561.   sta temp
  562.   jsr StrCopy
  563.   lda #$1A                
  564.   sta printY
  565.  
  566.   lda sourceSector           ;card status read
  567.   sta temp
  568.   jsr Byte2Str  
  569.   jmp CardInserted
  570.    
  571.    
  572.    
  573.    
  574. CardReady:
  575.   jmp CheckCardFormat
  576.  
  577.  
  578.  
  579.  
  580.  
  581.   .org $E642
  582. GetVolumeID:  
  583.   lda #$00
  584.   sta destLo
  585.   lda #$02
  586.   sta destHi  
  587.   jsr CardReadSector       ;read FAT16/FAT32 Volume ID sector  (partition boot record)
  588.  
  589.  
  590.   lda $020D                ;copy FAT16/FAT32 sectorsPerCluster from offset 13
  591.   sta sectorsPerCluster
  592.  
  593.   lda $020E                ;copy FAT16/FAT32 reservedSectors from offset 14
  594.   sta reservedSectors
  595.   lda $020F
  596.   sta reservedSectors+1
  597.        
  598.   ldx #$00
  599. CardCopySectorsPerFat:     ;copy FAT32 sectorsPerFat from offset 36
  600.   lda $0224, x
  601.   sta sectorsPerFat, x
  602.   inx
  603.   cpx #$04
  604.   bne CardCopySectorsPerFat
  605.    
  606.   ldx #$00
  607. CardCopyRootDirCluster32:    ;copy FAT32 rootDirCluster from offset 44
  608.   lda $022C, x
  609.   sta rootDirCluster, x
  610.   inx
  611.   cpx #$04
  612.   bne CardCopyRootDirCluster32
  613.  
  614.   lda #$00
  615.   sta fat16RootSectors         ; FAT32 no entry limit in root directory
  616.  
  617.   lda fat32Enabled
  618.   cmp #$01
  619.   beq CardCopyFatBeginLBA      ;when FAT32, leave alone
  620.  
  621.  
  622. ;;;SPECIAL FAT16 handling
  623.  
  624.  
  625. CardCopyRootDirCluster16:
  626.   lda #$00                  ;when FAT16, rootDirCluster = 0000    root dir is not in cluster nums
  627.   sta rootDirCluster
  628.   sta rootDirCluster+1
  629.   sta rootDirCluster+2
  630.   sta rootDirCluster+3
  631.          
  632.                                
  633.   lda $0216
  634.   sta sectorsPerFat           ;FAT16 copy sectorsPerFat from offset 22
  635.   lda $0217
  636.   sta sectorsPerFat+1
  637.   lda #$00
  638.   sta sectorsPerFat+2
  639.   sta sectorsPerFat+3         ;;FAT16 sectors per fat = 16 bits, in different place than FAT32
  640.                                                                                                                                                
  641.  
  642.  
  643. CardCopyRootDirEntries16:     ;copy max root directory entries from offset 17
  644.   lda $0211
  645.   sta fat16RootSectors
  646.   lda $0212
  647.   sta fat16RootSectors+1
  648.  
  649. ;  clc
  650. ;  lsr fat16RootSectors
  651. ;  lsr fat16RootSectors           ; FAT16 rootSectors =  (max root entries * 32) / 512   =   fat16RootSectors/16  ASR x4
  652. ;  lsr fat16RootSectors
  653. ;  lsr fat16RootSectors
  654.  
  655.   lda #$20
  656.   sta fat16RootSectors   ;FAT16 root dir fixed at 512 entries = 32 sectors
  657.  
  658.  
  659. CardCopyFatBeginLBA:
  660.   clc                       ;fatBeginLBA(4) = partitionLBABegin(4) + reservedSectors(2)
  661.   lda reservedSectors
  662.   adc partitionLBABegin
  663.   sta fatBeginLBA
  664.   lda reservedSectors+1
  665.   adc partitionLBABegin+1
  666.   sta fatBeginLBA+1
  667.   lda partitionLBABegin+2
  668.   adc #$00
  669.   sta fatBeginLBA+2
  670.   lda partitionLBABegin+3
  671.   adc #$00
  672.   sta fatBeginLBA+3
  673.  
  674.  
  675.  
  676. CardCopyClusterBeginLBA:
  677.   lda sectorsPerFat
  678.   sta clusterBeginLBA
  679.   lda sectorsPerFat+1
  680.   sta clusterBeginLBA+1
  681.   lda sectorsPerFat+2
  682.   sta clusterBeginLBA+2
  683.   lda sectorsPerFat+3
  684.   sta clusterBeginLBA+3
  685.  
  686.   clc
  687.   asl clusterBeginLBA          ;clusterBeginLBA(4) = fatBeginLBA(4) + (2 * sectorPerFat(4))
  688.   rol clusterBeginLBA+1      
  689.   rol clusterBeginLBA+2
  690.   rol clusterBeginLBA+3
  691.  
  692.    
  693.   clc                          ;FAT16 = 32 sectors       FAT32 = 0 sectors
  694.   lda clusterBeginLBA          ;clusterBeginLBA(4) = clusterBeginLBA(4) + fat16RootSectors(1)
  695.   adc fat16RootSectors
  696.   sta clusterBeginLBA
  697.   lda clusterBeginLBA+1
  698.   adc #$00
  699.   sta clusterBeginLBA+1
  700.   lda clusterBeginLBA+2
  701.   adc #$00
  702.   sta clusterBeginLBA+2
  703.   lda clusterBeginLBA+3
  704.   adc #$00
  705.   sta clusterBeginLBA+3
  706.  
  707.  
  708.   clc                        
  709.   lda clusterBeginLBA
  710.   adc fatBeginLBA
  711.   sta clusterBeginLBA
  712.   lda clusterBeginLBA+1
  713.   adc fatBeginLBA+1
  714.   sta clusterBeginLBA+1  
  715.   lda clusterBeginLBA+2
  716.   adc fatBeginLBA+2
  717.   sta clusterBeginLBA+2  
  718.   lda clusterBeginLBA+3
  719.   adc fatBeginLBA+3
  720.   sta clusterBeginLBA+3    
  721.  
  722.  
  723.   lda rootDirCluster
  724.   sta sourceCluster
  725.   lda rootDirCluster+1
  726.   sta sourceCluster+1
  727.   lda rootDirCluster+2
  728.   sta sourceCluster+2
  729.   lda rootDirCluster+3
  730.   sta sourceCluster+3
  731.    
  732.   jsr CardLoadDir        ; root dir
  733.  
  734.  
  735.  
  736.   lda #'P'
  737.   sta findEntry
  738.   lda #'O'
  739.   sta findEntry+1
  740.   lda #'W'
  741.   sta findEntry+2
  742.   lda #'E'
  743.   sta findEntry+3
  744.   lda #'R'
  745.   sta findEntry+4
  746.   lda #'P'
  747.   sta findEntry+5
  748.   lda #'A'
  749.   sta findEntry+6
  750.   lda #'K'
  751.   sta findEntry+7
  752.  
  753.   jsr DirFindEntryNew         ;"POWERPAK" dir into tempEntry
  754.  
  755.  
  756.  
  757.   lda tempEntry+$1C
  758.   sta baseDirCluster
  759.   lda tempEntry+$1D
  760.   sta baseDirCluster+1
  761.   lda tempEntry+$1E
  762.   sta baseDirCluster+2
  763.   lda tempEntry+$1F
  764.   sta baseDirCluster+3
  765.   ;;;; powerpak dir found   write base cluster
  766.  
  767.    
  768.   ;if poweron = 1  
  769.   ;  fpga isnt configured, go to i.map
  770.   ;if poweron = 0
  771.   ;  fpga is configured, go to q.map
  772.    
  773.   lda powerOn
  774.   beq LoadModuleQ
  775. LoadModuleI:
  776.   lda #'I'
  777.   sta findEntry  
  778.   jmp LoaderLoadModuleDone
  779. LoadModuleQ:
  780.   lda #'Q'
  781.   sta findEntry  
  782.   jmp LoaderLoadModuleDone
  783.  
  784.  
  785.  
  786. LoaderLoadModuleDone:  
  787.  
  788.   lda #' '
  789.   sta findEntry+1
  790.   sta findEntry+2
  791.   sta findEntry+3
  792.   sta findEntry+4
  793.   sta findEntry+5
  794.   sta findEntry+6
  795.   sta findEntry+7
  796.   jmp CardLoadModule      ;load intro module
  797.  
  798.  
  799. Forever:
  800.   jsr RenderingOn
  801. ForeverLoop:  
  802.   ;wait for nmi
  803.   lda frameCounter
  804.   cmp prevFrameCounter
  805.   beq ForeverLoop
  806.   jsr DoFrame
  807.   lda frameCounter
  808.   sta prevFrameCounter
  809.   jmp ForeverLoop
  810.  
  811.  
  812.  
  813. ;----------------------------;
  814.  
  815.  
  816. WaitVBlank:
  817.   lda $2002
  818.   bpl WaitVBlank
  819.   rts
  820.  
  821.  
  822. ;---------------------------:
  823.  
  824.  
  825. JoystickRead:       ;**-Get Joystick Status**
  826.  
  827.   lda #$01  ; directly loads the Accumulator with 1
  828.   sta $4016 ; stores the Accumulator (1) into $4016
  829.   lda #$00  ; directly loads the Accumulator with 0
  830.   sta $4016 ; stores the Accumulator (0) into $4016
  831.  
  832.   ldx #$08  ; loads 8 so it can loop for each byte
  833.  
  834. Joyloop:
  835.   lda $4016 ; loads the info from the joystick
  836.   and #$03  ; ||
  837.   cmp #$01  ; narrows down the data
  838.   rol $02   ; rotates info into memory...
  839.   dex       ; decrements X
  840.   bne Joyloop   ; loops again if not 0
  841.  
  842.   lda $02
  843.   sta joystick
  844.  
  845.   rts       ; returns to main routine
  846.  
  847.  
  848.  
  849.  
  850. ;-------------------------;  
  851. ClearNameTable:
  852.   lda $2002
  853.   lda #$20
  854.   sta $2006
  855.   lda #$00
  856.   sta $2006
  857.   ldx #$00
  858.   ldy #$08
  859. ClearNameTableLoop:
  860.   sta $2007
  861.   dex
  862.   bne ClearNameTableLoop
  863.   dey   ;LAST CHANGE
  864.   bne ClearNameTableLoop
  865.   rts
  866.  
  867.  
  868. ClearSpriteRam:
  869.   ;setup sprite ram    
  870.   lda $2002
  871.   lda #$00
  872.   sta $2003
  873.   sta $2003
  874.   lda #$ff
  875.   ldx #$00
  876. ClearSpriteRamLoop:
  877.   sta $2004
  878.   inx
  879.   bne ClearSpriteRamLoop
  880.   rts
  881.  
  882.  
  883. ClearLine:
  884.   ldy #$00
  885.   lda #' '
  886. ClearLineLoop:
  887.   sta printString, y
  888.   iny
  889.   cpy #$20
  890.   bne ClearLineLoop
  891.   rts
  892.  
  893. ClearFindEntry:
  894.   ldx #$08
  895.   lda #' '
  896. ClearFindEntryLoop
  897.   sta findEntry, x
  898.   dex
  899.   cpx #$00
  900.   bne ClearFindEntryLoop
  901.   rts
  902.  
  903.  
  904. PrintLine:
  905.   lda #$00
  906.   sta printAddrLo
  907.   sta printAddrHi
  908.     ldx #$03
  909.     lda printY
  910.     sta printAddrHi       ; x256
  911. ShiftLine:                          ;shift right 3x to mult x32
  912.   clc
  913.     lsr printAddrHi
  914.     ror printAddrLo
  915.     dex
  916.     bne ShiftLine
  917.  
  918.     lda printAddrHi
  919.     clc                                     ;add printAddr + $2000 (name table address)
  920.     adc #$20
  921.     sta printAddrHi
  922.  
  923.   lda $2002             ;clear latch
  924.     lda printAddrHi       ;starting address to ppu
  925.     sta $2006
  926.     lda printAddrLo
  927.     sta $2006
  928.    
  929.     ldx #$00
  930. PrintChar:
  931.     lda printString, x
  932.     sta $2007
  933.     inx
  934.     cpx #$20
  935.   bne   PrintChar
  936.            
  937.     rts
  938.  
  939.  
  940.  
  941.  
  942.  
  943. LoadLogo:
  944.   lda #$03
  945.   sta printY
  946.   jsr ClearLine
  947.   ldx #$00
  948.  
  949. LoadLogoLineLoop:
  950.   ldy #$00
  951.  
  952. LoadLogoCharLoop:  
  953.   lda LOGO1, x
  954.   sta printString+12, y
  955.   inx
  956.   iny
  957.   cpy #$08
  958.   bne LoadLogoCharLoop
  959.  
  960.   stx temp
  961.   inc printY
  962.   jsr PrintLine
  963.   ldx temp
  964.   lda printY
  965.   cmp #$0B
  966.   bne LoadLogoLineLoop
  967.  
  968.   ;;copy attributes
  969.   lda $2002             ;clear latch
  970.     lda #$23       ;starting address to ppu
  971.     sta $2006
  972.     lda #$C0       ;$23C0 = attribute table
  973.     sta $2006  
  974.  
  975.   lda #$55
  976.   ldx #$00
  977. LoadLogoPaletteLoop1:   ;;load yellow area  first 8 rows
  978.   sta $2007
  979.   inx
  980.   cpx #$10
  981.   bne LoadLogoPaletteLoop1
  982.  
  983.   lda #$F0              ;;load left side cart area  
  984.   sta $2007
  985.   sta $2007
  986.   sta $2007
  987.   sta $2007
  988.  
  989.   lda #$FD             ;;load right side cart area
  990.   sta $2007
  991.   sta $2007
  992.   sta $2007
  993.   sta $2007
  994.  
  995.  
  996.  
  997.  
  998.   rts
  999.  
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005. ;------------------------;
  1006.  
  1007. CopyChrRam:
  1008.   lda #$03  ;16KB bank 3, 8KB bank 6
  1009.   sta MAPPERWR
  1010.  
  1011.   lda $2002
  1012.   lda destHi
  1013.   sta $2006
  1014.   lda destLo
  1015.   sta $2006
  1016.   ldy #$00          ;256 byte counter
  1017.   ldx source256    ;block counter
  1018. CopyChrRamLoop:
  1019.   lda [sourceLo], y
  1020.   sta $2007
  1021.   iny
  1022.   bne CopyChrRamLoop
  1023.   inc sourceHi
  1024.   dex
  1025.   bne CopyChrRamLoop
  1026.   rts
  1027.  
  1028. ;-------------------------;
  1029.  
  1030. StrCopy:
  1031.   asl temp
  1032.   ldx temp
  1033.   lda MESSAGETABLE, x
  1034.   sta sourceStrLo
  1035.   lda MESSAGETABLE+1, x
  1036.   sta sourceStrHi
  1037.   jsr ClearLine
  1038.   ldy #$00
  1039. StrCopyLoop:
  1040.   lda [sourceStrLo], y
  1041.   sta printString, y      ;copy 32 text chars
  1042.   iny
  1043.   cpy #$20
  1044.   bne StrCopyLoop
  1045.  
  1046. ;  ldy #$00  
  1047. ;  lda #' '
  1048. ;  jsr StrCopyBlankLoop
  1049. ;  ldy #$18
  1050. ;  jsr StrCopyBlankLoop
  1051.   rts
  1052.  
  1053.  
  1054.  
  1055. ;StrCopyBlankLoop:
  1056. ;  ldx #$08
  1057. ;StrCopyBlankLoop1:  
  1058. ;  sta printString, y        ;blank 8 chars
  1059. ;  iny
  1060. ;  dex
  1061. ;  bne StrCopyBlankLoop1
  1062. ;  rts
  1063.  
  1064.  
  1065. ;----------------------------;
  1066.  
  1067. MemCopy:
  1068.   ldy #$00          ;256 byte counter
  1069.   ldx source256    ;block counter
  1070. MemCopyLoop:
  1071.   lda [sourceLo], y
  1072.   sta [destLo], y
  1073.   iny
  1074.   bne MemCopyLoop
  1075.   inc sourceHi
  1076.   inc destHi
  1077.   dex
  1078.   bne MemCopyLoop
  1079.   rts
  1080.  
  1081. ;------------------------------;  
  1082. Long2Str:
  1083.   lda temp+3
  1084.   and #$F0
  1085.   lsr a
  1086.   lsr a
  1087.   lsr a
  1088.   lsr a
  1089.   clc
  1090.   adc #$30
  1091.   sta printString+6
  1092.   cmp #$3A
  1093.   bcc Long2Str2
  1094.   clc
  1095.   lda printString+6
  1096.   adc #$07
  1097.   sta printString+6
  1098. Long2Str2:  
  1099.   lda temp+3
  1100.   and #$0F
  1101.   clc
  1102.   adc #$30
  1103.   sta printString+7
  1104.   cmp #$3A
  1105.   bcc Long2Str3
  1106.   clc
  1107.   lda printString+7
  1108.   adc #$07
  1109.   sta printString+7
  1110. Long2Str3:
  1111.   lda temp+2
  1112.   and #$F0
  1113.   lsr a
  1114.   lsr a
  1115.   lsr a
  1116.   lsr a
  1117.   clc
  1118.   adc #$30
  1119.   sta printString+4
  1120.   cmp #$3A
  1121.   bcc Long2Str4
  1122.   clc
  1123.   lda printString+4
  1124.   adc #$07
  1125.   sta printString+4
  1126. Long2Str4:  
  1127.   lda temp+2
  1128.   and #$0F
  1129.   clc
  1130.   adc #$30
  1131.   sta printString+5
  1132.   cmp #$3A
  1133.   bcc Short2Str
  1134.   clc
  1135.   lda printString+5
  1136.   adc #$07
  1137.   sta printString+5
  1138. Short2Str:
  1139.   lda temp+1
  1140.   and #$F0
  1141.   lsr a
  1142.   lsr a
  1143.   lsr a
  1144.   lsr a
  1145.   clc
  1146.   adc #$30
  1147.   sta printString+2
  1148.   cmp #$3A
  1149.   bcc Short2Str2
  1150.   clc
  1151.   lda printString+2
  1152.   adc #$07
  1153.   sta printString+2
  1154. Short2Str2:
  1155.   lda temp+1
  1156.   and #$0F
  1157.   clc
  1158.   adc #$30
  1159.   sta printString+3
  1160.   cmp #$3A
  1161.   bcc Byte2Str
  1162.   clc
  1163.   lda printString+3
  1164.   adc #$07
  1165.   sta printString+3
  1166. Byte2Str:  
  1167.   lda temp
  1168.   and #$F0
  1169.   lsr a
  1170.   lsr a
  1171.   lsr a
  1172.   lsr a
  1173.   clc
  1174.   adc #$30
  1175.   sta printString+0
  1176.   cmp #$3A
  1177.   bcc Byte2Str2
  1178.   clc
  1179.   lda printString+0
  1180.   adc #$07
  1181.   sta printString+0
  1182. Byte2Str2:
  1183.   lda temp
  1184.   and #$0F
  1185.   clc
  1186.   adc #$30
  1187.   sta printString+1  
  1188.   cmp #$3A
  1189.   bcc Byte2Str3
  1190.   clc
  1191.   lda printString+1
  1192.   adc #$07
  1193.   sta printString+1
  1194. Byte2Str3:
  1195.   rts
  1196.  
  1197.    
  1198. ;----------------------------;
  1199. GameGenieEncodeBad:
  1200.   lda #$00
  1201.   sta gameGenieCodes
  1202.   sta gameGenieCodes+1
  1203.   sta gameGenieCodes+2
  1204.   sta gameGenieCodes+3
  1205.   sta gameGenieCodes+4
  1206.   sta gameGenieCodes+5
  1207.   sta gameGenieCodes+6
  1208.   sta gameGenieCodes+7
  1209.   rts
  1210.  
  1211. GameGenieAddressEncode:
  1212.   ;ADDRESS  LO/HI
  1213.   lda gameGenie+3, x       ;n3 & 8
  1214.   and #$08
  1215.   sta gameGenieCodes
  1216.   lda gameGenie+4, x       ;n4 & 7
  1217.   and #$07
  1218.   ora gameGenieCodes
  1219.   sta gameGenieCodes
  1220.   lda gameGenie+1, x       ;n1 & 8 << 4
  1221.   and #$08
  1222.   jsr ASLA4
  1223.   ora gameGenieCodes
  1224.   sta gameGenieCodes
  1225.   lda gameGenie+2, x       ;n2 & 7 << 4
  1226.   and #$07
  1227.   jsr ASLA4
  1228.   ora gameGenieCodes
  1229.   sta gameGenieCodes
  1230.  
  1231.  
  1232.   lda gameGenie+3, x       ;n3 & 7 << 4
  1233.   and #$07
  1234.   jsr ASLA4
  1235.   sta gameGenieCodes+1
  1236.   lda gameGenie+5, x       ;n5 & 7
  1237.   and #$07
  1238.   ora gameGenieCodes+1
  1239.   sta gameGenieCodes+1
  1240.   lda gameGenie+4, x       ;n4 & 8
  1241.   and #$08
  1242.   ora gameGenieCodes+1
  1243.   sta gameGenieCodes+1
  1244.  
  1245.   lda gameGenieCodes+1  ;;enable a15=1
  1246.   ora #$80
  1247.   sta gameGenieCodes+1
  1248.   rts  
  1249.  
  1250. GameGenieEncodeBadJump:
  1251.   jmp GameGenieEncodeBad
  1252.  
  1253. GameGenieEncode:
  1254.   lda temp
  1255.   asl a
  1256.   asl a
  1257.   asl a
  1258.   sta temp
  1259.   ldx temp          ; x = 0/8/16/24/32, beginning of code
  1260.  
  1261.   lda gameGenie+0, x
  1262.   cmp #$10   ;'_'
  1263.   beq GameGenieEncodeBadJump
  1264.   lda gameGenie+1, x
  1265.   cmp #$10   ;'_'
  1266.   beq GameGenieEncodeBadJump
  1267.   lda gameGenie+2, x
  1268.   cmp #$10   ;'_'
  1269.   beq GameGenieEncodeBadJump
  1270.   lda gameGenie+3, x
  1271.   cmp #$10   ;'_'
  1272.   beq GameGenieEncodeBadJump
  1273.   lda gameGenie+4, x
  1274.   cmp #$10   ;'_'
  1275.   beq GameGenieEncodeBadJump
  1276.   lda gameGenie+5, x
  1277.   cmp #$10   ;'_'
  1278.   beq GameGenieEncodeBadJump  
  1279.  
  1280.   lda gameGenie+7, x
  1281.   cmp #$10   ;'_'
  1282.   beq GameGenie6Encode
  1283.   jmp GameGenie8Encode
  1284.                         ;                               0      1     2     3       4
  1285. GameGenie6Encode:       ;x=source  y=dest    codes = addrlo/addrhi/data/compare/enable8
  1286.   jsr GameGenieAddressEncode
  1287.   ;DATA
  1288.   lda gameGenie+1, x       ;n1 & 7 << 4
  1289.   and #$07
  1290.   jsr ASLA4
  1291.   sta gameGenieCodes+2
  1292.   lda gameGenie+0, x       ;n0 & 8 << 4
  1293.   and #$08
  1294.   jsr ASLA4
  1295.   ora gameGenieCodes+2
  1296.   sta gameGenieCodes+2
  1297.   lda gameGenie+0, x       ;n0 & 7
  1298.   and #$07
  1299.   ora gameGenieCodes+2
  1300.   sta gameGenieCodes+2
  1301.   lda gameGenie+5, x       ;n5 & 8
  1302.   and #$08
  1303.   ora gameGenieCodes+2
  1304.   sta gameGenieCodes+2
  1305.  
  1306.   lda #$00
  1307.   sta gameGenieCodes+3
  1308.   sta gameGenieCodes+4
  1309.   rts
  1310.  
  1311. GameGenie8Encode:       ;x=source  y=dest    codes = addrlo/addrhi/data/compare/enable8
  1312.   jsr GameGenieAddressEncode
  1313.   lda #$FF
  1314.   sta gameGenieCodes+4
  1315.   ;DATA
  1316.   lda gameGenie+1, x       ;n1 & 7 << 4
  1317.   and #$07
  1318.   jsr ASLA4
  1319.   sta gameGenieCodes+2
  1320.   lda gameGenie+0, x       ;n0 & 8 << 4
  1321.   and #$08
  1322.   jsr ASLA4
  1323.   ora gameGenieCodes+2
  1324.   sta gameGenieCodes+2
  1325.   lda gameGenie+0, x       ;n0 & 7
  1326.   and #$07
  1327.   ora gameGenieCodes+2
  1328.   sta gameGenieCodes+2
  1329.   lda gameGenie+7, x       ;n7 & 8
  1330.   and #$08
  1331.   ora gameGenieCodes+2
  1332.   sta gameGenieCodes+2
  1333.  
  1334.   ;COMPARE
  1335.   lda gameGenie+7, x       ;n7 & 7 << 4
  1336.   and #$07
  1337.   jsr ASLA4
  1338.   sta gameGenieCodes+3
  1339.   lda gameGenie+6, x       ;n6 & 8 << 4
  1340.   and #$08
  1341.   jsr ASLA4
  1342.   ora gameGenieCodes+3
  1343.   sta gameGenieCodes+3
  1344.   lda gameGenie+6, x       ;n6 & 7
  1345.   and #$07
  1346.   ora gameGenieCodes+3
  1347.   sta gameGenieCodes+3
  1348.   lda gameGenie+5, x       ;n5 & 8
  1349.   and #$08
  1350.   ora gameGenieCodes+3
  1351.   sta gameGenieCodes+3
  1352.  
  1353.   rts
  1354.  
  1355.  
  1356.  
  1357.  
  1358. ASLA4:
  1359.   asl a
  1360.   asl a
  1361.   asl a
  1362.   asl a
  1363.   rts
  1364.  
  1365. ASLA3:
  1366.   asl a
  1367.   asl a
  1368.   asl a
  1369.   rts
  1370.  
  1371. LSRA3:
  1372.   lsr a
  1373.   lsr a
  1374.   lsr a
  1375.   rts
  1376.    
  1377.      
  1378.  
  1379. ;--------------------------;
  1380.  
  1381. ClusterToLBA:
  1382.  
  1383.  
  1384.   ;  cluster->lba     lba_addr(4) = clusterBeginLBA(4) + (cluster_number(2)-2 * sectorsPerCluster(1))
  1385.   sec
  1386.   lda sourceCluster
  1387.   sbc #$02
  1388.   sta sourceSector
  1389.   lda sourceCluster+1
  1390.   sbc #$00
  1391.   sta sourceSector+1
  1392.   lda sourceCluster+2
  1393.   sbc #$00
  1394.   sta sourceSector+2        ;sourceSector = sourceCluster - 2
  1395.   lda sourceCluster+3
  1396.   sbc #$00
  1397.   sta sourceSector+3
  1398.  
  1399.   lda sectorsPerCluster
  1400.   sta source256
  1401.  
  1402.   clc
  1403.   lsr source256
  1404.   lda source256
  1405.   cmp #$00
  1406.   beq ClusterToLBAAddClusterBeginLBA   ;handle 1 sector per cluster
  1407.  
  1408. ClusterToLBALoop:
  1409.   clc
  1410.   asl sourceSector
  1411.   rol sourceSector+1
  1412.   rol sourceSector+2         ;sourceSector = sourceSector * sectorsPerCluster
  1413.   rol sourceSector+3
  1414.   clc
  1415.   lsr source256
  1416.   lda source256
  1417.   cmp #$00
  1418.   bne ClusterToLBALoop
  1419.  
  1420. ClusterToLBAAddClusterBeginLBA:  
  1421.   clc
  1422.   lda sourceSector
  1423.   adc clusterBeginLBA
  1424.   sta sourceSector
  1425.   lda sourceSector+1
  1426.   adc clusterBeginLBA+1       ;sourceSector = sourceSector(4) + clusterBeginLBA(4)
  1427.   sta sourceSector+1
  1428.   lda sourceSector+2
  1429.   adc clusterBeginLBA+2
  1430.   sta sourceSector+2  
  1431.   lda sourceSector+3
  1432.   adc clusterBeginLBA+3
  1433.   sta sourceSector+3
  1434.  
  1435.  
  1436.   ;;FAT32 all done
  1437.   ;lda fat32Enabled
  1438.   ;cmp #$01
  1439.   ;beq ClusterToLBADone
  1440.    
  1441.   ;;;FIXME FAT16 add fat16RootSectors    already included in clusterBeginLBA?
  1442.      
  1443. ClusterToLBADone:
  1444.   rts
  1445.  
  1446.  
  1447. ;-------------------------;
  1448.  
  1449.  
  1450. NextCluster:    ;load fat entry for sourceCluster, store next cluster number into sourceCluster
  1451.   ;fat16:
  1452.     ; offset = clusternum << 1
  1453.     ;mask = 00 00 ff ff
  1454.   asl sourceCluster
  1455.   rol sourceCluster+1
  1456.   rol sourceCluster+2
  1457.   rol sourceCluster+3
  1458.   ;fat32:
  1459.     ;offset = clutsernum << 2       cluster 2 << 2 = 8
  1460.     ;mask = 0f ff ff ff
  1461.   lda fat32Enabled
  1462.   cmp #$00
  1463.   beq NextClusterSectorNum
  1464.  
  1465.   asl sourceCluster
  1466.   rol sourceCluster+1
  1467.   rol sourceCluster+2
  1468.   rol sourceCluster+3    
  1469.  
  1470. NextClusterSectorNum:
  1471.   ;fat sector num = fatBeginLBA + (offset / 512)     cluster = $2f8
  1472.   lda sourceCluster+1
  1473.   sta sourceSector+0
  1474.   lda sourceCluster+2
  1475.   sta sourceSector+1
  1476.   lda sourceCluster+3                ;divide by 256   sector = 02
  1477.   sta sourceSector+2
  1478.   lda #$00
  1479.   sta sourceSector+3
  1480.  
  1481.   lsr sourceSector+3                   ;divide by 512   sector = 01
  1482.   ror sourceSector+2
  1483.   ror sourceSector+1        
  1484.   ror sourceSector+0    
  1485.   clc
  1486.   lda sourceSector
  1487.   adc fatBeginLBA
  1488.   sta sourceSector
  1489.   lda sourceSector+1                 ;add fatBeginLBA   sector = 60 = $c000
  1490.   adc fatBeginLBA+1
  1491.   sta sourceSector+1
  1492.   lda sourceSector+2
  1493.   adc fatBeginLBA+2
  1494.   sta sourceSector+2
  1495.   lda sourceSector+3
  1496.   adc fatBeginLBA+3
  1497.   sta sourceSector+3
  1498.  
  1499.   ;load fat sector
  1500.   lda #$02
  1501.   sta destHi
  1502.   lda #$00
  1503.   sta destLo
  1504.   jsr CardReadSector
  1505.  
  1506.   ;offset = offset % 512    ;offset of fat entry within loaded sector 0-511
  1507.   lda sourceCluster+1
  1508.   and #%00000001
  1509.   sta sourceCluster+1    ; cluster+1=0
  1510.  
  1511.   ;next cluster = [sector], offset
  1512.   lda #$00
  1513.   sta sourceLo
  1514.  
  1515.   lda #$02
  1516.   adc sourceCluster+1       ;sourceHi = 2 or 3 for which 256 byte block too look in
  1517.   sta sourceHi
  1518.  
  1519.   ldy sourceCluster
  1520.   ldx #$00
  1521. NextClusterLoop:  
  1522.   lda [sourceLo], y
  1523.   sta sourceCluster, x
  1524.   iny
  1525.   inx
  1526.   cpx #$04
  1527.   bne NextClusterLoop
  1528.  
  1529.   lda sourceCluster+3        ;FAT32 mask off top 4 bits
  1530.   and #$0F
  1531.   sta sourceCluster+3
  1532.  
  1533.   lda fat32Enabled
  1534.   cmp #$01
  1535.   beq NextClusterDone        ;no more mask for FAT32
  1536.  
  1537.   lda #$00
  1538.   sta sourceCluster+3     ;fat16 mask
  1539.   sta sourceCluster+2
  1540.  
  1541. NextClusterDone:
  1542.   rts
  1543.  
  1544.  
  1545.  
  1546. LoadNextSectorNum:
  1547.   ;get next sector, use sectorCounter to check if next cluster needed
  1548.  
  1549.   clc
  1550.   lda sourceSector  
  1551.   adc #$01
  1552.   sta sourceSector                   ;go to next sector num
  1553.   lda sourceSector+1
  1554.   adc #$00
  1555.   sta sourceSector+1
  1556.   lda sourceSector+2
  1557.   adc #$00
  1558.   sta sourceSector+2
  1559.   lda sourceSector+3
  1560.   adc #$00
  1561.   sta sourceSector+3
  1562.  
  1563.   clc
  1564.   lda sectorCounter                   ;one more sector
  1565.   adc #$01
  1566.   sta sectorCounter
  1567.   cmp sectorsPerCluster
  1568.   beq LoadNextClusterNum
  1569.   rts
  1570.  
  1571.  
  1572. LoadNextClusterNum:
  1573.   lda #$00
  1574.   sta sectorCounter
  1575.   jsr NextCluster                      ;get cluster num into sourceCluster
  1576.   jsr ClusterToLBA                     ;get sector num into sourceSector
  1577.   rts
  1578.  
  1579.  
  1580. ;----------------------------;  
  1581.  
  1582. CardWaitNotBusy:           ;wait for not busy
  1583.   lda CARDSTATUS           ;card status read
  1584.   and #%10000000
  1585.   cmp #%10000000           ;check busy bit
  1586.   beq CardWaitNotBusy
  1587.   rts
  1588.  
  1589. CardWaitDataReq:           ;wait for not busy, ready, datareq, no error
  1590.   lda CARDSTATUS
  1591.   and #%01011000
  1592.   cmp #%01011000
  1593.   bne CardWaitDataReq
  1594.   rts
  1595.  
  1596. CardCheckError:
  1597.   lda CARDSTATUS           ; get card status, check for general error
  1598.   and #%00000001
  1599.   cmp #%00000001
  1600.   beq CardSectorError
  1601.   rts  
  1602.  
  1603.  
  1604. CardSectorError:
  1605.   lda #messageREADERR
  1606.   sta temp
  1607.   jsr StrCopy
  1608.   lda #$07                 ;message position
  1609.   sta printY  
  1610.   lda CARDERROR            ;card error read
  1611.   sta temp
  1612.   jsr Byte2Str
  1613.   jmp Forever              ;display message, stop everything
  1614.  
  1615.  
  1616. CardLoadLBA:
  1617.   lda #$01
  1618.   sta CARDSECTORCOUNT
  1619.   lda sourceSector
  1620.   sta CARDLBA0
  1621.   lda sourceSector+1
  1622.   sta CARDLBA1
  1623.   lda sourceSector+2
  1624.   sta CARDLBA2
  1625.   lda sourceSector+3        ;load lba number
  1626.   and #%00001111
  1627.   ora #%11100000
  1628.   sta CARDLBA3
  1629.   rts
  1630.  
  1631.  
  1632. CardReadSector:
  1633.   jsr CardCheckError
  1634.   jsr CardWaitNotBusy
  1635.   jsr CardLoadLBA
  1636.   jsr CardCheckError
  1637.  
  1638.  
  1639.   lda #$20
  1640.   sta CARDCOMMAND           ;send card read sector command
  1641.   nop
  1642.   nop
  1643.  
  1644.   jsr CardCheckError
  1645.   jsr CardWaitDataReq
  1646.  
  1647.   lda #$00
  1648.   sta sourceBytes
  1649.   lda #$02
  1650.   sta source256
  1651.   jsr CardReadBytes
  1652.  
  1653.   jsr CardCheckError
  1654.  
  1655. CardReadSectorDone:
  1656.   rts  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662. CardReadBytes:         ;read source256*sourceBytes bytes into ram
  1663.   ldx source256
  1664.   ldy sourceBytes
  1665. CardReadBytesLoop:
  1666.   lda CARDDATAREAD
  1667.   sta [destLo], y
  1668.   iny
  1669.   bne CardReadBytesLoop
  1670.   inc destHi
  1671.   dex
  1672.   bne CardReadBytesLoop
  1673.   rts
  1674.  
  1675.  
  1676.  
  1677.  
  1678. CardReadSector16:
  1679.  
  1680.   jsr CardWaitNotBusy
  1681.   jsr CardLoadLBA
  1682.  
  1683.   lda #$20
  1684.   sta CARDCOMMAND           ;send card read sector command
  1685.   nop
  1686.   nop
  1687.  
  1688.   jsr CardCheckError
  1689.   jsr CardWaitDataReq
  1690.  
  1691.   ldy #$00
  1692. CardReadSector16Loop:         ;read 16 bytes into ram
  1693.   lda CARDDATAREAD
  1694.   sta [destLo], y
  1695.   iny
  1696.   cpy #$10
  1697.   bne CardReadSector16Loop
  1698.  
  1699.   clc
  1700.   lda destLo
  1701.   adc #$10
  1702.   sta destLo
  1703.   lda destHi
  1704.   adc #$00
  1705.   sta destHi
  1706.  
  1707.   rts
  1708.  
  1709.  
  1710. CardReadSector496:
  1711.   ldx #$00
  1712.   ldy #$00
  1713. CardReadSector496Loop:         ;read 496 bytes into ram
  1714.   lda CARDDATAREAD
  1715.   sta [destLo], y
  1716.  
  1717.   cpy #$EF
  1718.   bne CardReadSector496LoopInc
  1719.   cpx #$01
  1720.   bne CardReadSector496LoopInc
  1721.   jmp CardReadSector496LoopDone
  1722.  
  1723. CardReadSector496LoopInc:
  1724.   iny
  1725.   bne CardReadSector496Loop
  1726.   inc destHi
  1727.   inx
  1728.   jmp CardReadSector496Loop
  1729.  
  1730. CardReadSector496LoopDone:  
  1731.   jsr CardCheckError
  1732.  
  1733. CardReadSector496Done:
  1734.   rts  
  1735.  
  1736.  
  1737.  
  1738. ;------------------------------;  
  1739.  
  1740. CardWriteSector:
  1741.   jsr CardWaitNotBusy
  1742.   jsr CardLoadLBA  
  1743.  
  1744.   lda #$30
  1745.   sta CARDCOMMAND           ;send card write sector command
  1746.   nop
  1747.   nop
  1748.  
  1749.   jsr CardCheckError
  1750.   jsr CardWaitDataReq
  1751.    
  1752.   ldy #$00
  1753.   ldx #$02
  1754. CardWriteSectorLoop:         ;read 512 bytes from ram to card
  1755.   lda [sourceLo], y
  1756.   sta CARDDATAWRITE
  1757.   iny
  1758.   bne CardWriteSectorLoop
  1759.   inc sourceHi
  1760.   dex
  1761.   bne CardWriteSectorLoop
  1762.  
  1763.   jsr CardCheckError
  1764.  
  1765. CardWriteSectorDone:
  1766.   rts
  1767.  
  1768.  
  1769. ;------------------------------;
  1770. CardLoadDirClearEntryName:
  1771.   lda #$00
  1772.   sta lfnFound
  1773.   ldy #$00
  1774.   lda #$00  
  1775. CardLoadDirClearEntryNameLoop:            ;clear entry name    //CHANGE ME 128 BYTES
  1776.   sta tempEntry, y
  1777.   iny
  1778.   cpy #$20
  1779.   bne CardLoadDirClearEntryNameLoop
  1780.   rts
  1781.  
  1782. ;dest = sector dest  0200
  1783. ;sourceEntry = where to get entry in 0200-0400, when overflow, load next sector
  1784. ;destBank =  which bank to select in prg ram
  1785. ;destEntry = where to put entry in prg ram 6000-7FFF, when overflow, increment bank
  1786.  
  1787.  
  1788. CardLoadDir:
  1789.  
  1790.   lda #$02
  1791.   sta destHi                   ; $0200 = where to put sector
  1792.   sta sourceEntryHi
  1793.  
  1794.   lda #$60                     ; $6000 = where to put entry
  1795.   sta destEntryHi
  1796.  
  1797.   lda #$F8
  1798.   sta cursorX
  1799.   sta cursorY                  ; cursor off screen
  1800.  
  1801.   lda #$00
  1802.   sta destLo
  1803.   sta sourceEntryLo
  1804.   sta destEntryLo
  1805.   sta filesInDir
  1806.   sta filesInDir+1
  1807.   sta destBank
  1808.   sta sectorCounter
  1809.   sta temp
  1810.   sta temp+1
  1811.   sta selectedEntry
  1812.   sta selectedEntry+1
  1813.   sta cursorYCounter
  1814.   sta speedCounter
  1815.   sta speedScroll
  1816.   sta scrollYCounter
  1817.   sta PRGBANK
  1818.   sta lfnFound
  1819.    
  1820.   jsr ClusterToLBA            ; sourceCluster -> first sourceSector
  1821.  
  1822.   lda fat32Enabled
  1823.   cmp #$01
  1824.   beq CardLoadDirReadSector   ; FAT32 go to read sector
  1825.  
  1826.   lda sourceCluster            ;FAT16  check if trying to load root dir
  1827.   cmp rootDirCluster
  1828.   bne CardLoadDirReadSector
  1829.   lda sourceCluster+1
  1830.   cmp rootDirCluster+1
  1831.   bne CardLoadDirReadSector
  1832.   lda sourceCluster+2
  1833.   cmp rootDirCluster+2
  1834.   bne CardLoadDirReadSector
  1835.   lda sourceCluster+3
  1836.   cmp rootDirCluster+3
  1837.   bne CardLoadDirReadSector  
  1838.  
  1839.  
  1840.   sec
  1841.   lda clusterBeginLBA
  1842.   sbc fat16RootSectors          ; FAT16 sourceSector = root dir first sector =>  clusterLBABegin(4) - fat16RootSectors(1)
  1843.   sta sourceSector
  1844.   lda clusterBeginLBA+1
  1845.   sbc #$00
  1846.   sta sourceSector+1
  1847.   lda clusterBeginLBA+2
  1848.   sbc #$00
  1849.   sta sourceSector+2  
  1850.   lda clusterBeginLBA+3
  1851.   sbc #$00
  1852.   sta sourceSector+3
  1853.  
  1854.  
  1855.      
  1856. CardLoadDirReadSector:  
  1857.   jsr CardReadSector          ; put into dest
  1858.   jsr CardLoadDirClearEntryName   ;clear entry name
  1859.  
  1860. CardLoadDirLoop:
  1861.   ldy #$00
  1862.   lda [sourceEntryLo], y            ; if name[0] = 0x00, no more entries
  1863.   cmp #$00
  1864.   beq CardLoadDirLastEntryFound
  1865.   jmp CardLoadDirCheckUnused
  1866.    
  1867. CardLoadDirLastEntryFound:
  1868.   rts
  1869.  
  1870. CardLoadDirCheckUnused:
  1871.   ldy #$00
  1872.   lda [sourceEntryLo], y
  1873.   cmp #$E5                     ; if name[0] = 0xE5, entry unused, skip
  1874.   bne CardLoadDirCheckLongName
  1875.   lda #$00
  1876.   sta lfnFound
  1877.   jmp CardLoadDirNextEntry
  1878.  
  1879.  
  1880.  
  1881. CardLoadDirCheckLongName:
  1882.   ldy #$0B
  1883.   lda [sourceEntryLo], y            ; if flag = %00001111, long file name entry found
  1884.   and #$0F
  1885.   cmp #$0F
  1886.   bne CardLoadDirCheckShortName
  1887.  
  1888.   ldy #$00
  1889.   lda [sourceEntryLo], y
  1890.   and #%10111111                 ;mask off "last entry" bit
  1891.   cmp #$01                     ; if index = 1 or 2, load name    //CHANGE ME  INDEX < 10
  1892.   beq CardLoadDirLongName1      ; else skip entry
  1893.   cmp #$02
  1894.   beq CardLoadDirLongName2
  1895.  
  1896.   jsr CardLoadDirClearEntryName
  1897.  
  1898.   jmp CardLoadDirNextEntry
  1899.  
  1900.  
  1901.       ;//CHANGE ME  USE MULTIPLY13 FUNCTION
  1902. CardLoadDirLongName1:
  1903.   ldx #$00                     ; index 1 = chars 0-12
  1904.   jmp CardLoadDirLongName
  1905. CardLoadDirLongName2:
  1906.   ldx #$0D                     ; index 2 = chars 13-25
  1907.   jmp CardLoadDirLongName
  1908.  
  1909.  
  1910.  
  1911. CardLoadDirLongName:
  1912.   lda #$00
  1913.   sta tempEntry+$20                 ;use as char counter    //CHANGE ME
  1914.   ldy #$01
  1915. CardLoadDirLongNameLoop1:
  1916.   lda [sourceEntryLo], y             ; loop thro 5 chars   1x3x5x7x9x
  1917.   cmp #$FF
  1918.   beq CardLoadDirLongNameLoop1FF
  1919.   sta tempEntry, x
  1920. CardLoadDirLongNameLoop1FF:
  1921.   inx
  1922.   iny
  1923.   iny
  1924.   inc tempEntry+$20                 ;use as char counter      //CHANGE ME
  1925.   lda tempEntry+$20                 ;use as char counter      //CHANGE ME
  1926.   cmp #$05
  1927.   bne CardLoadDirLongNameLoop1
  1928.  
  1929.   ldy #$0E
  1930. CardLoadDirLongNameLoop2:
  1931.   lda [sourceEntryLo], y             ; loop thro 6 chars   Ex10x12x14x16x18x
  1932.   cmp #$FF
  1933.   beq CardLoadDirLongNameLoop2FF  
  1934.   sta tempEntry, x
  1935. CardLoadDirLongNameLoop2FF:
  1936.   inx
  1937.   iny
  1938.   iny
  1939.   inc tempEntry+$20                 ;use as char counter      //CHANGE ME  
  1940.   lda tempEntry+$20                 ;use as char counter    //CHANGE ME
  1941.   cmp #$0B
  1942.   bne CardLoadDirLongNameLoop2
  1943.    
  1944.   ldy #$1C
  1945. CardLoadDirLongNameLoop3:
  1946.   lda [sourceEntryLo], y             ;  thro 2 chars   1Cx1Ex
  1947.   cmp #$FF
  1948.   beq CardLoadDirLongNameLoop3FF  
  1949.   sta tempEntry, x
  1950. CardLoadDirLongNameLoop3FF:
  1951.   inx
  1952.   iny
  1953.   iny
  1954.   lda [sourceEntryLo], y
  1955.   cmp #$FF
  1956.   beq CardLoadDirLongNameLoop4FF  
  1957.   sta tempEntry, x
  1958. CardLoadDirLongNameLoop4FF:
  1959.  
  1960.   lda #$01
  1961.   sta lfnFound
  1962.   jmp CardLoadDirNextEntry
  1963.  
  1964. CardLoadDirCheckShortName:
  1965.   ldy #$0B
  1966.   lda [sourceEntryLo], y                 ;if flag = volume id, skip
  1967.   and #$08
  1968.   cmp #$08
  1969.   bne CardLoadDirCheckHidden
  1970.   jsr CardLoadDirClearEntryName
  1971.   lda #$00
  1972.   sta lfnFound
  1973.   jmp CardLoadDirNextEntry
  1974.  
  1975.  
  1976. CardLoadDirCheckHidden:
  1977.   ldy #$0B
  1978.   lda [sourceEntryLo], y
  1979.   and #$02
  1980.   cmp #$02                                ; if flag = 0x02, hidden, skip
  1981.   bne CardLoadDirCheckDir
  1982.   lda #$00
  1983.   sta lfnFound
  1984.   jsr CardLoadDirClearEntryName           ;clear entry name  
  1985.   jmp CardLoadDirNextEntry
  1986.  
  1987.      
  1988. CardLoadDirCheckDir:
  1989.   ldy #$0B
  1990.   lda [sourceEntryLo], y                 ;if flag = directory, load entry
  1991.   and #$10
  1992.   cmp #$10
  1993.   bne CardLoadDirCheckEx1
  1994.   lda #$01
  1995.   sta tempEntry+$1B                      ;$1B = dir flag  //CHANGE ME
  1996.   jmp CardLoadDirShortName
  1997.  
  1998. CardLoadDirCheckEx1:
  1999.   ldx #$00
  2000.   ldy #$08
  2001. CardLoadDirCheckEx1Loop:
  2002.   lda [sourceEntryLo], y
  2003.   cmp exMatch1, x
  2004.   beq CardLoadDirCheckEx2
  2005.   inx
  2006.   cpx #$04
  2007.   bne CardLoadDirCheckEx1Loop
  2008.   lda #$00
  2009.   sta lfnFound
  2010.   jsr CardLoadDirClearEntryName    
  2011.   jmp CardLoadDirNextEntry          ;if extension doesnt match, skip
  2012.  
  2013. CardLoadDirCheckEx2:
  2014.   ldx #$00
  2015.   ldy #$09
  2016. CardLoadDirCheckEx2Loop:
  2017.   lda [sourceEntryLo], y
  2018.   cmp exMatch2, x
  2019.   beq CardLoadDirCheckEx3
  2020.   inx
  2021.   cpx #$04
  2022.   bne CardLoadDirCheckEx2Loop
  2023.   lda #$00
  2024.   sta lfnFound
  2025.   jsr CardLoadDirClearEntryName
  2026.   jmp CardLoadDirNextEntry          ;if extension doesnt match, skip
  2027.  
  2028. CardLoadDirCheckEx3:
  2029.   ldx #$00
  2030.   ldy #$0A
  2031. CardLoadDirCheckEx3Loop:
  2032.   lda [sourceEntryLo], y
  2033.   cmp exMatch3, x
  2034.   beq CardLoadDirShortName
  2035.   inx
  2036.   cpx #$04
  2037.   bne CardLoadDirCheckEx3Loop
  2038.   lda #$00
  2039.   sta lfnFound
  2040.   jsr CardLoadDirClearEntryName
  2041.   jmp CardLoadDirNextEntry          ;if extension doesnt match, skip
  2042.  
  2043. CardLoadDirShortName:
  2044.  
  2045.   clc  
  2046.   lda filesInDir                      ;filesInDir++
  2047.   adc #$01
  2048.   sta filesInDir
  2049.   lda filesInDir+1
  2050.   adc #$00
  2051.   sta filesInDir+1
  2052.  
  2053.   lda lfnFound
  2054.   cmp #$01
  2055.   beq CardLoadDirLongShortNameFound
  2056.  
  2057. CardLoadDirShortNameFound:  
  2058.   ldy #$00
  2059. CardLoadDirShortNameLoop:            ;if lfnFound = 0, copy short name
  2060.   lda [sourceEntryLo], y
  2061.   sta tempEntry, y
  2062.   iny
  2063.   cpy #$08
  2064.   bne CardLoadDirShortNameLoop
  2065.  
  2066. CardLoadDirShortFileName:  
  2067.   lda tempEntry+$1B                      ;$1B = dir flag  //CHANGE ME
  2068.   cmp #$01
  2069.   beq CardLoadDirSaveEntry
  2070.  
  2071.   lda #'.'
  2072.   sta tempEntry+$8
  2073.   ldy #$08
  2074.   lda [sourceEntryLo], y    ;//copy short file name
  2075.   sta tempEntry+$9
  2076.   iny
  2077.   lda [sourceEntryLo], y
  2078.   sta tempEntry+$A
  2079.   iny
  2080.   lda [sourceEntryLo], y
  2081.   sta tempEntry+$B  
  2082.  
  2083.   jmp CardLoadDirSaveEntry
  2084.  
  2085.  
  2086.      
  2087. CardLoadDirLongShortNameFound:    
  2088.      
  2089. CardLoadDirSaveEntry:
  2090.   ldy #$1A
  2091.   lda [sourceEntryLo], y                  ;copy clusterhilo to last 4 bytes of entry   ///CHANGE ME
  2092.   sta tempEntry+$1C
  2093.   iny
  2094.   lda [sourceEntryLo], y
  2095.   sta tempEntry+$1D
  2096.   ldy #$14
  2097.   lda [sourceEntryLo], y
  2098.   sta tempEntry+$1E
  2099.   iny
  2100.   lda [sourceEntryLo], y
  2101.   sta tempEntry+$1F
  2102.  
  2103.   lda destBank                            ;change to entry storage bank
  2104.   sta PRGBANK
  2105.  
  2106.   ldy #$00
  2107. CardLoadDirSaveEntryLoop:                 ;copy entry to prg ram   //CHANGE ME  128 bytes
  2108.   lda tempEntry, y
  2109.   sta [destEntryLo], y                
  2110.   iny
  2111.   cpy #$20
  2112.   bne CardLoadDirSaveEntryLoop
  2113.    
  2114.   clc
  2115.   lda destEntryLo                         ;destEntry += 32   //CHANGE ME  to 128 bytes
  2116.   adc #$20
  2117.   sta destEntryLo
  2118.   lda destEntryHi
  2119.   adc #$00
  2120.   sta destEntryHi
  2121.  
  2122.   lda destEntryHi                        ;check if destEntry is overflowing, inc bank, destEntry=6000
  2123.   cmp #$80
  2124.   bne CardLoadDirClearTempEntryName
  2125.   lda #$60
  2126.   sta destEntryHi                        ;reset to $6000
  2127.   lda #$00
  2128.   sta destEntryLo
  2129.   inc destBank                           ;increment bank num
  2130.  
  2131. CardLoadDirClearTempEntryName:
  2132.   ldy #$00
  2133.   lda #$00  
  2134.   sta lfnFound  
  2135. CardLoadDirClearTempEntryLoop:            ;clear entry name
  2136.   sta tempEntry, y
  2137.   iny
  2138.   cpy #$20
  2139.   bne CardLoadDirClearTempEntryLoop
  2140.    
  2141. CardLoadDirNextEntry:
  2142.   clc                                 ;increment entry source address
  2143.   lda sourceEntryLo                        ;sourceEntry += 32 in 0200-0400
  2144.   adc #$20
  2145.   sta sourceEntryLo
  2146.   lda sourceEntryHi
  2147.   adc #$00
  2148.   sta sourceEntryHi  
  2149.  
  2150.   lda sourceEntryHi                    ;if source overflows, get next sector
  2151.   cmp #$04
  2152.   beq CardLoadDirNextSector
  2153.  
  2154.   jmp CardLoadDirLoop
  2155.    
  2156. CardLoadDirNextSector:    
  2157.   clc
  2158.   lda sourceSector  
  2159.   adc #$01
  2160.   sta sourceSector                   ;go to next sector num
  2161.   lda sourceSector+1
  2162.   adc #$00
  2163.   sta sourceSector+1
  2164.   lda sourceSector+2
  2165.   adc #$00
  2166.   sta sourceSector+2
  2167.   lda sourceSector+3
  2168.   adc #$00
  2169.   sta sourceSector+3
  2170.  
  2171.  
  2172.   ;  if fat32, goto CardLoadDirSectorInc
  2173.   lda fat32Enabled
  2174.   cmp #$01
  2175.   beq CardLoadDirSectorInc
  2176.  
  2177.  
  2178.   lda sourceCluster            ;FAT16  check if trying to load root dir
  2179.   cmp rootDirCluster
  2180.   bne CardLoadDirSectorInc
  2181.   lda sourceCluster+1
  2182.   cmp rootDirCluster+1
  2183.   bne CardLoadDirSectorInc
  2184.   lda sourceCluster+2
  2185.   cmp rootDirCluster+2
  2186.   bne CardLoadDirSectorInc
  2187.   lda sourceCluster+3
  2188.   cmp rootDirCluster+3
  2189.   bne CardLoadDirSectorInc  
  2190.  
  2191.   clc
  2192.   lda sectorCounter              ;FAT16 root dir all sequential sectors
  2193.   adc #$01
  2194.   sta sectorCounter
  2195.  
  2196.   ; if sectorCounter = fat16RootSectors
  2197.   ;        goto CardLoadDirLastEntryFound
  2198.   clc
  2199.   lda sectorCounter
  2200.   cmp fat16RootSectors
  2201.   beq CardLoadDirLastEntryFoundJmp
  2202.   jmp CardLoadDirLoadNextSector      ;FAT16 skip cluster lookup when max root sectors not reached
  2203.  
  2204.  
  2205. CardLoadDirLastEntryFoundJmp:
  2206.   jmp CardLoadDirLastEntryFound       ;FAT16 max root sectors reached, all root entries found
  2207.      
  2208.  
  2209. CardLoadDirSectorInc:
  2210.   clc
  2211.   lda sectorCounter                   ;one more sector
  2212.   adc #$01
  2213.   sta sectorCounter
  2214.   cmp sectorsPerCluster                ;make sure cluster isnt overflowing      
  2215.   bne CardLoadDirLoadNextSector
  2216.  
  2217.   ;;;move to next cluster
  2218.   jsr NextCluster                      ;get cluster num into sourceCluster
  2219.  
  2220.  
  2221. CardLoadDirLastEntryCheck:  
  2222.   lda #$0F
  2223.   sta temp
  2224.   lda #$FF
  2225.   sta temp+1                            ;;FAT32 last cluster = 0x0FFFFFFF
  2226.   sta temp+2
  2227.   sta temp+3
  2228.  
  2229.   lda fat32Enabled
  2230.   cmp #$01
  2231.   beq CardLoadDirLastEntryCheck2        ;;if FAT32, last cluster mask done
  2232.  
  2233.   lda #$00                              ;;FAT16 last cluster = 0x0000FFFF
  2234.   sta temp
  2235.   sta temp+1
  2236.  
  2237. CardLoadDirLastEntryCheck2:
  2238.   ;check if cluster = last cluster
  2239.   ; if match, jmp to last entry found
  2240.   lda temp
  2241.   cmp sourceCluster+3
  2242.   bne CardLoadDirNextSectorNum
  2243.   lda temp+1
  2244.   cmp sourceCluster+2
  2245.   bne CardLoadDirNextSectorNum
  2246.   lda temp+2
  2247.   cmp sourceCluster+1
  2248.   bne CardLoadDirNextSectorNum
  2249.   lda temp+3
  2250.   cmp sourceCluster+0
  2251.   bne CardLoadDirNextSectorNum
  2252.  
  2253.   jmp CardLoadDirLastEntryFound
  2254.  
  2255.  
  2256. CardLoadDirNextSectorNum:
  2257.   jsr ClusterToLBA                     ;sourceCluster -> first sourceSector
  2258.   lda #$00
  2259.   sta sectorCounter                    ;reset sector counter
  2260.  
  2261. CardLoadDirLoadNextSector:
  2262.   lda #$00
  2263.   sta destLo                          ;reset sector dest
  2264.   sta sourceEntryLo                   ;reset entry source
  2265.   lda #$02                              
  2266.   sta destHi
  2267.   sta sourceEntryHi
  2268.   jsr CardReadSector
  2269.   jmp CardLoadDirLoop
  2270.  
  2271.  
  2272.  
  2273.  
  2274.  
  2275.    
  2276. ;-------------------------------------;
  2277.  
  2278.  
  2279. CardLoadModule:
  2280.   lda #$60
  2281.   sta $0404      ;module not ready, set controller vector to rts
  2282.   lda #$00
  2283.   sta moduleReady
  2284.  
  2285.   lda #'M'
  2286.   sta exMatch1
  2287.   sta exMatch1+1
  2288.   sta exMatch1+2
  2289.   sta exMatch1+3
  2290.   lda #'A'
  2291.   sta exMatch2
  2292.   sta exMatch2+1
  2293.   sta exMatch2+2
  2294.   sta exMatch2+3
  2295.   lda #'P'
  2296.   sta exMatch3
  2297.   sta exMatch3+1
  2298.   sta exMatch3+2
  2299.   sta exMatch3+3
  2300.  
  2301.   lda baseDirCluster
  2302.   sta sourceCluster
  2303.   lda baseDirCluster+1
  2304.   sta sourceCluster+1
  2305.   lda baseDirCluster+2
  2306.   sta sourceCluster+2
  2307.   lda baseDirCluster+3
  2308.   sta sourceCluster+3
  2309.  
  2310.  
  2311.  
  2312.   jsr CardLoadDir    ; powerpak dir
  2313. ;-------------------;
  2314. ;  jsr DirPrintDir
  2315. ;--------------------;  
  2316.  
  2317.   ;findEntry info already loaded
  2318.  
  2319.   jsr DirFindEntryNew      
  2320.   lda tempEntry+$1C
  2321.   sta sourceCluster
  2322.   sta mapperCluster
  2323.   lda tempEntry+$1D
  2324.   sta sourceCluster+1
  2325.   sta mapperCluster+1
  2326.   lda tempEntry+$1E
  2327.   sta sourceCluster+2
  2328.   sta mapperCluster+2
  2329.   lda tempEntry+$1F
  2330.   sta sourceCluster+3
  2331.   sta mapperCluster+3
  2332.  
  2333.  
  2334.  
  2335.   lda #$00
  2336.   sta destLo
  2337.   lda #$04
  2338.   sta destHi      ;put into module ram
  2339.  
  2340.   ;find first sector
  2341.   jsr ClusterToLBA            ;sourceCluster -> first sourceSector
  2342.  
  2343.  
  2344.  
  2345.   jsr RenderingOff    ;;DEBUG REMOVE
  2346.   jsr CardReadSector
  2347.  
  2348.   clc
  2349.   lda sourceSector
  2350.   adc #$01  
  2351.   sta sourceSector
  2352.   lda sourceSector+1
  2353.   adc #$00
  2354.   sta sourceSector+1
  2355.   lda sourceSector+2
  2356.   adc #$00  
  2357.   sta sourceSector+2
  2358.   lda sourceSector+3
  2359.   adc #$00  
  2360.   sta sourceSector+3
  2361.   lda sectorsPerCluster
  2362.   cmp #$01
  2363.   bne CardLoadModuleNextSector
  2364.   ;only one sector per cluster, go to next cluster
  2365.   jsr NextCluster
  2366.   jsr ClusterToLBA
  2367.  
  2368. CardLoadModuleNextSector:
  2369.   lda #$00
  2370.   sta destLo
  2371.   lda #$06
  2372.   sta destHi
  2373.   jsr CardReadSector
  2374.  
  2375.  ;;;DEBUG
  2376. ;  lda $0400
  2377. ;  sta temp
  2378. ;  lda $0400+1
  2379. ;  sta temp+1
  2380. ;  lda $0400+2
  2381. ;  sta temp+2
  2382. ;  lda $0400+3
  2383. ;  sta temp+3
  2384. ;  jsr Long2Str
  2385. ;  jsr PrintLine
  2386. ;  jsr ForeverLoop
  2387. ;;;DEBUG
  2388.  
  2389.  
  2390.  
  2391.   lda #$00
  2392.   sta scrollY          
  2393.   sta scrollYCounter    
  2394.   sta scrollYUp        
  2395.   sta scrollYDown      
  2396.   sta cursorYCounter
  2397.   sta cursorYUp        
  2398.   sta cursorYDown
  2399.   lda #$F8
  2400.   sta cursorX
  2401.   sta cursorY
  2402.  
  2403.  
  2404.   lda #$4C
  2405.   sta $0404      ;module ready, set controller vector to jmp
  2406.  
  2407.   jmp $0400      ;go to module begin
  2408.  
  2409.  
  2410.  
  2411. ;-------------------------------------;
  2412.  
  2413.  
  2414. DirPrintEntry:
  2415.   ;offset = temp * 32   temp << 5
  2416.   ;bank num = temp+1 -> temp+2
  2417.  
  2418.   lda selectedEntry
  2419.   sta sourceEntryLo
  2420.   lda #$00
  2421.   sta sourceEntryHi
  2422.   sta printString
  2423.   sta printString+$1     ;replace with dir symbol
  2424.   sta printString+$1D    ;empty text at end of string
  2425.   sta printString+$1E
  2426.   sta printString+$1F
  2427.   lda selectedEntry+$1
  2428.   sta PRGBANK
  2429.  
  2430.   ldy #$05
  2431. DirPrintEntryShiftLoop:         ;//CHANGE ME temp << 7 = 128 bytes
  2432.   clc
  2433.   asl sourceEntryLo
  2434.   rol sourceEntryHi
  2435.   dey
  2436.   bne DirPrintEntryShiftLoop
  2437.  
  2438.   clc
  2439.   lda #$60
  2440.   adc sourceEntryHi
  2441.   sta sourceEntryHi
  2442.    
  2443.   ldy #$00                          ;//CHANGE ME  set starting char number
  2444. DirPrintEntryCopyLoop:
  2445.   lda [sourceEntryLo], y
  2446.   sta printString+2, y
  2447.   iny
  2448.   cpy #$1B
  2449.   bne DirPrintEntryCopyLoop
  2450.  
  2451.   ;;; add dir symbol or empty symbol     //CHANGE ME dir flag in new place
  2452.   ldy #$1B
  2453.   lda [sourceEntryLo], y
  2454.   beq DirPrintEntryDone
  2455.  
  2456.   lda #'/'
  2457.   sta printString+1
  2458.  
  2459. DirPrintEntryDone:
  2460.   rts
  2461.  
  2462.  
  2463.  
  2464.  
  2465. DirGetEntry:
  2466.   ;offset = selectedEntry * 32   temp << 5
  2467.   ;bank num = selectedEntry+1
  2468.  
  2469.   lda selectedEntry
  2470.   sta sourceEntryLo
  2471.   lda #$00
  2472.   sta sourceEntryHi
  2473.   lda selectedEntry+1
  2474.   sta PRGBANK
  2475.  
  2476.   ldy #$05
  2477. DirGetEntryShiftLoop:
  2478.   asl sourceEntryLo
  2479.   rol sourceEntryHi
  2480.   dey
  2481.   bne DirGetEntryShiftLoop
  2482.  
  2483.   clc
  2484.   lda #$60
  2485.   adc sourceEntryHi
  2486.   sta sourceEntryHi
  2487.    
  2488.   ldy #$00
  2489. DirGetEntryCopyLoop:
  2490.   lda [sourceEntryLo], y
  2491.   sta tempEntry, y
  2492.   iny
  2493.   cpy #$20
  2494.   bne DirGetEntryCopyLoop
  2495.  
  2496.   rts
  2497.  
  2498.  
  2499.  
  2500.  
  2501. DirFindEntry:
  2502.   lda #$00
  2503.   sta selectedEntry
  2504.   sta selectedEntry+1
  2505.  
  2506. DirFindEntryLoop:
  2507.   jsr DirGetEntry
  2508.  
  2509.   ;;check if entry matches, only look at first 8 chars
  2510.   ldy #$00
  2511. DirFindEntryCharLoop:
  2512.   lda tempEntry, y
  2513.   cmp findEntry, y
  2514.   bne DirFindEntryNext
  2515.   iny
  2516.   cpy #$08
  2517.   bne DirFindEntryCharLoop
  2518.  
  2519.   rts  ;all 8 chars match
  2520.  
  2521. DirFindEntryNext:
  2522.   clc
  2523.   lda selectedEntry
  2524.   adc #$01
  2525.   sta selectedEntry
  2526.   lda selectedEntry+1            ;increment to next entry index
  2527.   adc #$00
  2528.   sta selectedEntry+1
  2529.  
  2530.   lda selectedEntry
  2531.   cmp filesInDir
  2532.   bne DirFindEntryLoop
  2533.   lda selectedEntry+1
  2534.   cmp filesInDir+1
  2535.   bne DirFindEntryLoop
  2536.  
  2537. DirFindEntryFailed:
  2538.   lda #messageNOTFOUND
  2539.   sta temp
  2540.   jsr StrCopy
  2541.   lda #$1A                       ;FIXME message position
  2542.   sta printY
  2543. ;  jsr PrintLine                  ;FIXME change to 3x nops
  2544.   jsr FileNotFound
  2545. ;  nop
  2546. ;  nop
  2547. ;  nop
  2548.   jmp Forever
  2549.  
  2550. ;---------------------------------;  
  2551.  
  2552.  
  2553. DirPrintDir:
  2554.  
  2555.   ;turn off rendering
  2556.   jsr RenderingOff
  2557.   jsr ClearNameTable
  2558.  
  2559.   ;loop thro entries 0 to 31 or 0 to filesInDir
  2560.   lda #$00
  2561.   sta selectedEntry
  2562.   sta selectedEntry+1
  2563.   sta printY
  2564.   sta destBank
  2565.   sta PRGBANK
  2566.  
  2567. DirPrintDirLoop:
  2568.   inc printY                                   ; go to next print line
  2569.  
  2570.   jsr DirPrintEntry                            ;copy dir entry to printString
  2571.   jsr PrintLine                                ;print to name table
  2572.    
  2573.   clc
  2574.   lda selectedEntry
  2575.   adc #$01
  2576.   sta selectedEntry
  2577.   lda selectedEntry+1                                   ;increment to next entry index
  2578.   adc #$00
  2579.   sta selectedEntry+1
  2580.  
  2581. DirPrintDir32Check:
  2582.   ;check if printY equal to 29, jump out
  2583.   lda printY
  2584.   cmp #$1D
  2585.   beq DirPrintDirLoopDone
  2586.  
  2587. DirPrintDirFileCheck:
  2588.   ;check if temp/temp+1 equal to filesInDir, jump out
  2589.   lda selectedEntry
  2590.   cmp filesInDir
  2591.   bne DirPrintDirLoop
  2592.   lda selectedEntry+1
  2593.   cmp filesInDir+1
  2594.   bne DirPrintDirLoop
  2595.    
  2596. DirPrintDirLoopDone:
  2597.  
  2598.   lda #$00
  2599.   sta cursorX
  2600.   sta scrollY
  2601.   sta selectedEntry
  2602.   sta selectedEntry+1
  2603.   sta scrollYCounter
  2604.   sta scrollYUp
  2605.   sta scrollYDown
  2606.   sta cursorYCounter
  2607.   sta cursorYUp
  2608.   sta cursorYDown
  2609.   sta speedCounter
  2610.   sta speedScroll
  2611.   sta destBank
  2612.  
  2613.   lda #$08
  2614.   sta cursorY
  2615.  
  2616.   jsr RenderingOn
  2617.  
  2618.   rts
  2619.  
  2620.  
  2621.    
  2622. ;------------------------------;
  2623.  
  2624. FPGACopyRom:
  2625.  
  2626.   lda #$FF
  2627.   sta FPGAPROGRAM         ;tell fpga its being reprogrammed
  2628.  
  2629.    
  2630.  
  2631.   lda #$00
  2632.   sta MAPPERWR      ;;;;SWITCH BANK TO 16KB bank 0
  2633.   lda #$00
  2634.   sta sourceLo      ;copy FPGA from $8000 to $BFFF = 16KB
  2635.   lda #$80
  2636.   sta sourceHi
  2637.   ldy #$00          ;256 byte counter
  2638.   ldx #$40          ;block counter $40 = 64 * 256 = 16KB
  2639. FPGACopyRomLoop0:
  2640.   lda [sourceLo], y
  2641.   sta FPGADATA         ;fpga data address
  2642.   iny
  2643.   bne FPGACopyRomLoop0
  2644.   inc sourceHi
  2645.   dex
  2646.   bne FPGACopyRomLoop0
  2647.  
  2648.  
  2649.  
  2650.   lda #$01
  2651.   sta MAPPERWR      ;;;;SWITCH BANK TO 16KB bank 1
  2652.   lda #$00
  2653.   sta sourceLo      ;copy FPGA from $8000 to $BFFF = 16KB
  2654.   lda #$80
  2655.   sta sourceHi
  2656.   ldy #$00          ;256 byte counter
  2657.   ldx #$40          ;block counter $40 = 64 * 256 = 16KB
  2658. FPGACopyRomLoop1:
  2659.   lda [sourceLo], y
  2660.   sta FPGADATA         ;fpga data address
  2661.   iny
  2662.   bne FPGACopyRomLoop1
  2663.   inc sourceHi
  2664.   dex
  2665.   bne FPGACopyRomLoop1
  2666.  
  2667.  
  2668.  
  2669.   lda #$02
  2670.   sta MAPPERWR      ;;;;SWITCH BANK TO 16KB bank 2
  2671.   lda #$00
  2672.   sta sourceLo      ;copy FPGA from $8000 to $8125
  2673.   lda #$80
  2674.   sta sourceHi
  2675.   ldy #$00          ;256 byte counter
  2676.   ldx #$25          ;block counter 9328 more bytes= 37 * 256 = $25
  2677. FPGACopyRomLoop2:
  2678.   lda [sourceLo], y
  2679.   sta FPGADATA         ;fpga data address
  2680.   iny
  2681.   bne FPGACopyRomLoop2
  2682.   inc sourceHi
  2683.   dex
  2684.   bne FPGACopyRomLoop2
  2685.  
  2686.   rts
  2687.  
  2688.  
  2689. LoadPRGRam:
  2690.   lda #$00
  2691.   sta sectorCounter
  2692.   sta destLo
  2693.   lda #$60
  2694.   sta destHi  
  2695.  
  2696.   lda gameCluster
  2697.   sta sourceCluster
  2698.   lda gameCluster+1
  2699.   sta sourceCluster+1
  2700.   lda gameCluster+2
  2701.   sta sourceCluster+2
  2702.   lda gameCluster+3
  2703.   sta sourceCluster+3
  2704.  
  2705.   jsr ClusterToLBA
  2706.   jsr CardReadSector16     ;get ines header  16bytes
  2707.  
  2708.   lda #$00
  2709.   sta bankCounter
  2710.   sta destLo               ;reset to beginning of prgram
  2711.   lda #$60
  2712.   sta destHi  
  2713. LoadPRGRamLoop:
  2714.   lda #$00
  2715.   sta destLo
  2716.  
  2717.   jsr CardReadSector496    ;get 496 bytes
  2718.  
  2719.   lda destHi
  2720.   sta temp
  2721.   jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  2722.   lda temp
  2723.   sta destHi
  2724.   lda #$F0
  2725.   sta destLo
  2726.   jsr CardReadSector16     ;get 16 bytes  
  2727. LoadPRGRamLoopCheckBank:
  2728.   lda destHi
  2729.   cmp #$80
  2730.   bne LoadPRGRamLoop       ;check if next bank needed, destHi=80
  2731.   lda #$60
  2732.   sta destHi
  2733.   lda #$00
  2734.   sta destLo
  2735.   inc destBank
  2736.   inc bankCounter
  2737. LoadPRGRamLoopCheckDone:
  2738.   ;check if all prg loaded
  2739.   lda bankCounter
  2740.   cmp prgSize
  2741.   beq LoadPRGRamDone
  2742.   lda destBank
  2743.   sta PRGBANK
  2744.   jmp LoadPRGRamLoop
  2745. LoadPRGRamDone:
  2746.   rts
  2747.  
  2748.  
  2749.  
  2750. LoadWRam:
  2751.   rts
  2752.  
  2753.  
  2754.  
  2755.  
  2756.  
  2757.  .org $F51F
  2758. LoadCHRRam:
  2759.  
  2760.   lda chrSize           ;if no chr, set to chrram
  2761.   cmp #$00
  2762.   beq LoadCHRRamDone  
  2763.  
  2764. LoadCHRRam1:
  2765.   lda #$00
  2766.   sta destBank
  2767.   sta CHRBANK
  2768.   sta destLo
  2769.   sta destHi  
  2770.   sta bankCounter
  2771.  
  2772.  
  2773.   ;16 bytes already read for prg  
  2774. LoadCHRRamLoop:
  2775.  
  2776.   lda #$00
  2777.   sta destLo
  2778.  
  2779.   lda $2002
  2780.   lda destHi
  2781.   sta $2006
  2782.   lda destLo
  2783.   sta $2006
  2784.  
  2785.   jsr CardReadSector496Chr   ;get 496 bytes
  2786.   lda destHi
  2787.   sta temp
  2788.   jsr LoadNextSectorNum
  2789.   lda temp
  2790.   sta destHi
  2791.   lda #$F0
  2792.   sta destLo
  2793.   jsr CardReadSector16Chr    ;get 16 bytes  
  2794. LoadCHRRamLoopCheckBank:
  2795.   lda destHi
  2796.   cmp #$20
  2797.   bne LoadCHRRamLoop          ;check if next bank needed, destHi=20
  2798.   lda #$00
  2799.   sta destHi
  2800.   sta destLo
  2801.   inc destBank
  2802.   inc bankCounter
  2803.   lda $2002
  2804.   lda destHi
  2805.   sta $2006
  2806.   lda destLo
  2807.   sta $2006
  2808. LoadCHRRamLoopCheckDone:
  2809.   ;check if all prg loaded
  2810.   lda bankCounter
  2811.   cmp chrSize
  2812.   beq LoadCHRRamDone
  2813.   lda destBank
  2814.   sta CHRBANK
  2815.   jmp LoadCHRRamLoop
  2816. LoadCHRRamDone:
  2817.   lda #$02
  2818.   sta destHi
  2819.   lda #$00
  2820.   sta destLo
  2821.   jsr CardReadSector496         ;get 496 bytes to finish sector
  2822.   lda #$00
  2823.   sta CHRBANK
  2824.   rts
  2825.  
  2826.  
  2827.  
  2828. CardReadSector16Chr:
  2829.  
  2830.   jsr CardWaitNotBusy
  2831.   jsr CardLoadLBA
  2832.  
  2833.   lda #$20
  2834.   sta CARDCOMMAND           ;send card read sector command
  2835.   nop
  2836.   nop
  2837.  
  2838.   jsr CardCheckError
  2839.   jsr CardWaitDataReq
  2840.  
  2841.   ldy #$00
  2842. CardReadSector16ChrLoop:         ;read 16 bytes into ram
  2843.   lda CARDDATAREAD
  2844.   sta $2007
  2845.   iny
  2846.   cpy #$10
  2847.   bne CardReadSector16ChrLoop
  2848.  
  2849.   clc
  2850.   lda destLo
  2851.   adc #$10
  2852.   sta destLo
  2853.   lda destHi
  2854.   adc #$00
  2855.   sta destHi
  2856.  
  2857.   rts
  2858.  
  2859.  
  2860. CardReadSector496Chr:
  2861.   ldx #$00
  2862.   ldy #$00
  2863. CardReadSector496ChrLoop:         ;read 496 bytes into ram
  2864.   lda CARDDATAREAD
  2865.   sta $2007
  2866.  
  2867.   cpy #$EF
  2868.   bne CardReadSector496ChrLoopInc
  2869.   cpx #$01
  2870.   bne CardReadSector496ChrLoopInc
  2871.   jmp CardReadSector496ChrLoopDone
  2872.  
  2873. CardReadSector496ChrLoopInc:
  2874.   iny
  2875.   bne CardReadSector496ChrLoop
  2876.   inc destHi
  2877.   inx
  2878.   jmp CardReadSector496ChrLoop
  2879.  
  2880. CardReadSector496ChrLoopDone:  
  2881.   jsr CardCheckError
  2882.  
  2883. CardReadSector496ChrDone:
  2884.   rts  
  2885.  
  2886.  
  2887.  
  2888.  
  2889.  
  2890.  
  2891. LoadFPGA:
  2892.   ;get fpga data starting after 2 sectors
  2893.   lda mapperCluster
  2894.   sta sourceCluster
  2895.   lda mapperCluster+1
  2896.   sta sourceCluster+1
  2897.   lda mapperCluster+2
  2898.   sta sourceCluster+2
  2899.   lda mapperCluster+3
  2900.   sta sourceCluster+3
  2901.  
  2902.   lda #$00
  2903.   sta sectorCounter
  2904.   sta tempEntry
  2905.    
  2906.   jsr ClusterToLBA     ;cluster->sector#1 into sourceSector
  2907.   jsr LoadNextSectorNum    ;sector#2  
  2908.  
  2909.   lda #$FF
  2910.   sta FPGAPROGRAM         ;fpga program address
  2911. LoadFPGALoop:
  2912.   jsr LoadNextSectorNum    ;uses x,y,destHi,destLo
  2913.   jsr LoadFPGASector
  2914.   inc tempEntry
  2915.   lda tempEntry
  2916.   cmp #$53         ;$53 sectors = 83 sectors = 42496 bytes, fpga = 42096 bytes
  2917.   bne LoadFPGALoop
  2918.   rts  
  2919.  
  2920.  
  2921.  
  2922. LoadFPGASector:
  2923.  
  2924.   jsr CardWaitNotBusy
  2925.   jsr CardLoadLBA
  2926.  
  2927.   lda #$20
  2928.   sta CARDCOMMAND           ;send card read sector command
  2929.   nop
  2930.   nop
  2931.  
  2932.   jsr CardCheckError
  2933.   jsr CardWaitDataReq
  2934.  
  2935.   ldy #$00
  2936.   ldx #$02
  2937. LoadFPGASectorLoop:         ;read 512 bytes into fpga data
  2938.   lda CARDDATAREAD
  2939.   sta FPGADATA
  2940.   iny
  2941.   bne LoadFPGASectorLoop
  2942.   dex
  2943.   bne LoadFPGASectorLoop
  2944.  
  2945.   jsr CardCheckError
  2946.  
  2947. LoadFPGASectorDone:
  2948.   rts  
  2949.  
  2950.  
  2951.  
  2952.  
  2953. LoadGameGenie:
  2954.   ;send 5 game genie codes, newdata/enable8mode/comparedata/addresslo/addresshi
  2955.  
  2956.   lda #$00
  2957.   sta temp
  2958.   sta temp+1
  2959.  
  2960. LoadGameGenieLoop:
  2961.  
  2962.   lda temp+1
  2963.   sta temp
  2964.   inc temp+1
  2965.   jsr GameGenieEncode
  2966.  
  2967.   lda gameGenieCodes+2
  2968.   sta GAMEGENIEWR       ;new data
  2969.   lda gameGenieCodes+4
  2970.   sta GAMEGENIEWR       ;enable8 mode
  2971.   lda gameGenieCodes+3
  2972.   sta GAMEGENIEWR       ;compare data
  2973.   lda gameGenieCodes+0
  2974.   sta GAMEGENIEWR       ;address lo
  2975.   lda gameGenieCodes+1
  2976.   sta GAMEGENIEWR       ;address hi
  2977.  
  2978.   lda temp+1
  2979.   cmp #$05
  2980.   bne LoadGameGenieLoop
  2981.  
  2982.   ;;load fpga configured info
  2983.   lda useBattery
  2984.   sta GAMEGENIEWR       ;new data
  2985.   lda #$00
  2986.   sta GAMEGENIEWR       ;enable8mode
  2987.   sta GAMEGENIEWR       ;compare data
  2988.   lda #$08
  2989.   sta GAMEGENIEWR       ;address lo
  2990.   lda #$42
  2991.   sta GAMEGENIEWR       ;address hi
  2992.  
  2993.   rts
  2994.  
  2995.  
  2996.  
  2997.  
  2998. LoadPrgChrSize:
  2999.   lda prgSize     ;4x8KB 0100
  3000.   sec
  3001.   sbc #$01        ;0011
  3002.   and #$7F        ;wram disabled
  3003.   sta PRGSIZEWR
  3004.  
  3005.   lda chrSize
  3006.   beq LoadPrgChrSizeChrRam
  3007.  
  3008.   lda chrSize     ;4x8KB 0100
  3009.   asl a           ;1000
  3010.   sec
  3011.   sbc #$01        ;0111
  3012.   and #$7F        ;chrram wr disabled
  3013.   sta CHRSIZEWR  
  3014.   rts
  3015.  
  3016. LoadPrgChrSizeChrRam:
  3017.   lda #$81
  3018.   sta CHRSIZEWR  ;chr size = 0, enable chrram wr
  3019.   rts
  3020.  
  3021.  
  3022.  
  3023. LoadMirroring:
  3024.   ;send mirroring h/v/four
  3025.   lda mirroring
  3026.   sta MIRRORINGWR
  3027.   rts  
  3028.  
  3029. ;---------------------------:
  3030.  
  3031.  
  3032. RenderingOn:
  3033.   ;turn screen on, display screen/sprites
  3034.   jsr ClearSpriteRam
  3035.   jsr WaitVBlank
  3036.  
  3037.     lda #%10001000           ;7 vblank nmi, 4 screen addr, 3 sprite addr, 2 vertical wr, 10 name table addr  
  3038.   sta $2000
  3039.     lda #%00011110           ;567 emphasis color BGR, 4 sprites on, 3 background on, 2 sprite clipping, 1 image clipping
  3040.   sta $2001
  3041.   ;set scroll registers
  3042.   lda $2002                ;clear 2006 latch
  3043.   lda #$00                 ;;fixme????  was #$20
  3044.   sta $2006
  3045.   sta $2006  
  3046.   sta $2005
  3047.   sta $2005
  3048.  
  3049.   rts
  3050.  
  3051.  
  3052. RenderingOff:
  3053.   ;turn screen off
  3054.   jsr WaitVBlank
  3055.   lda $2002                ;clear 2006 latch
  3056.     lda #%00001000           ;7 vblank nmi, 4 screen addr, 3 sprite addr, 2 vertical wr, 10 name table addr  
  3057.   sta $2000
  3058.     lda #%00000110           ;567 emphasis color BGR, 4 sprites on, 3 background on, 2 sprite clipping, 1 image clipping
  3059.   sta $2001
  3060.   rts
  3061.  
  3062. ClearRam:
  3063.   ;clear ram variable 2KB bytes
  3064.   lda #$00
  3065.   ldx #$00
  3066. ClearRamLoop:
  3067.   sta $0000, x
  3068.   sta $0100, x
  3069.   sta $0200, x
  3070.   sta $0300, x
  3071.   sta $0400, x
  3072.   sta $0500, x
  3073.   sta $0600, x
  3074.   sta $0700, x
  3075.   inx
  3076.   bne ClearRamLoop
  3077.  
  3078.   lda #'1'
  3079.   sta romVers
  3080.   lda #'1'
  3081.   sta romVers+1
  3082.   lda #'2'
  3083.   sta romVers+2
  3084.  
  3085.   jmp ClearRamReturn
  3086.  
  3087.  
  3088.  
  3089. CheckCardFormat:
  3090.   lda #$00
  3091.   sta sourceSector
  3092.   sta sourceSector+1
  3093.   sta sourceSector+2
  3094.   sta sourceSector+3
  3095.   sta destLo
  3096.   lda #$02
  3097.   sta destHi
  3098.  
  3099.   jsr CardReadSector        ;read sector 0 to internal ram
  3100.  
  3101.   lda $03FE
  3102.   cmp #$55
  3103.   bne CardFormatError       ;last word check  should be $55AA
  3104.   lda $03FF
  3105.   cmp #$AA
  3106.   bne CardFormatError
  3107.   lda #$01
  3108.   sta fat32Enabled
  3109.   lda $03C2                 ;format  fat32 = 0x0B   FAT32
  3110.   cmp #$0B
  3111.   beq CardGoodFormat
  3112.   lda $03C2                 ;format  fat32 = 0x0C   FAT32 LBA
  3113.   cmp #$0C
  3114.   beq CardGoodFormat
  3115.  
  3116.  
  3117.   lda #$00
  3118.   sta fat32Enabled
  3119.   lda $03C2                ;format  fat16 = 0x06  larger than 32MB
  3120.   cmp #$06
  3121.   beq CardGoodFormat
  3122.   lda $03C2                ;format  fat16 = 0x04  smaller than 32MB
  3123.   cmp #$04
  3124.   beq CardGoodFormat
  3125.   lda $03C2                ;format  fat16 = 0x0E  with LBA
  3126.   cmp #$0E
  3127.   beq CardGoodFormat
  3128.  
  3129.   nop
  3130.   nop
  3131.   nop
  3132.  
  3133. CardFormatError:
  3134.   lda #messageBADFORMAT
  3135.   sta temp
  3136.   jsr StrCopy              ;print format error message, stop everything
  3137.   lda #$1A                
  3138.   sta printY
  3139.  
  3140.   lda $03C2                ;card last word read
  3141.   sta temp
  3142.   jsr Byte2Str
  3143.   jmp ForeverLoop
  3144.  
  3145.  
  3146. CardGoodFormat:            ;fat32/fat16 partition found  
  3147.  
  3148.   ldx #$00
  3149. CardCopyPartitionLBABegin: ;copy partitionLBABegin from offset 455
  3150.   lda $03C6, x
  3151.   sta sourceSector, x
  3152.   sta partitionLBABegin, x
  3153.   inx
  3154.   cpx #$04
  3155.   bne CardCopyPartitionLBABegin
  3156.  
  3157.   jmp GetVolumeID  ;;rts
  3158.  
  3159.  
  3160.  
  3161.  
  3162.  
  3163.  
  3164.  
  3165.  
  3166.  
  3167.  
  3168.  
  3169.  
  3170.  
  3171.  
  3172.  
  3173.  
  3174.  
  3175. CheckCardFormatFail:
  3176.   lda #$00
  3177.   sta sourceSector
  3178.   sta sourceSector+1
  3179.   sta sourceSector+2
  3180.   sta sourceSector+3
  3181.   sta destLo
  3182.   lda #$02
  3183.   sta destHi
  3184.  
  3185.   jsr CardReadSector        ;read sector 0 to internal ram
  3186.  
  3187. FindCardPartition:  
  3188.   lda $03FE
  3189.   cmp #$55
  3190.   bne CardFormatErrorX       ;last word check  should be $55AA
  3191.   lda $03FF
  3192.   cmp #$AA
  3193.   bne CardFormatErrorX
  3194.  
  3195.  
  3196.  
  3197.   ldx #$00      ;;x = partition index  0, 16, 32, 48
  3198.   ldy #$00      ;;y = partition number 0, 1, 2, 3
  3199. CardFormatCheckLoop:
  3200.   lda #$01
  3201.   sta fat32Enabled
  3202.   lda $03C2, x                 ;format  fat32 = 0x0B   FAT32
  3203.   cmp #$0B
  3204.   beq CardGoodFormatX
  3205.   lda $03C2, x                 ;format  fat32 = 0x0C   FAT32 LBA
  3206.   cmp #$0C
  3207.   beq CardGoodFormatX
  3208.  
  3209.  
  3210.   lda #$00
  3211.   sta fat32Enabled
  3212.   lda $03C2, x                ;format  fat16 = 0x06  larger than 32MB
  3213.   cmp #$06
  3214.   beq CardGoodFormatX
  3215.   lda $03C2, x                ;format  fat16 = 0x04  smaller than 32MB
  3216.   cmp #$04
  3217.   beq CardGoodFormatX
  3218.   lda $03C2, x                ;format  fat16 = 0x0E  with LBA
  3219.   cmp #$0E
  3220.   beq CardGoodFormatX
  3221.  
  3222.   inx
  3223.   inx
  3224.   inx
  3225.   inx
  3226.  
  3227.   inx
  3228.   inx
  3229.   inx
  3230.   inx
  3231.  
  3232.   inx
  3233.   inx
  3234.   inx
  3235.   inx
  3236.  
  3237.   inx
  3238.   inx
  3239.   inx
  3240.   inx
  3241.  
  3242.   iny
  3243.   cpy #$04
  3244.   bne CardFormatCheckLoop
  3245.  
  3246. CardFormatErrorX:
  3247.   lda #messageBADFORMAT
  3248.   sta temp
  3249.   jsr StrCopy              ;print format error message, stop everything
  3250.   lda #$1A                
  3251.   sta printY
  3252.  
  3253.   lda $03C2                ;card last word read
  3254.   sta temp
  3255.   jsr Byte2Str
  3256.   jmp ForeverLoop
  3257.  
  3258.  
  3259. CardGoodFormatX:                ;fat32/fat16 partition found  
  3260.  
  3261.   lda #messageBADFORMAT
  3262.   sta temp
  3263.   jsr StrCopy              ;print format error message, stop everything
  3264.   lda #$1A                
  3265.   sta printY
  3266.  
  3267.   lda $03C2,x                ;card last word read
  3268.   sta temp
  3269.   jsr Byte2Str
  3270.  
  3271.   ldy #$00
  3272. CardCopyPartitionLBABeginX:    ;copy partitionLBABegin from offset 455
  3273.   lda $03C6, x                ;;x = partition index
  3274.   sta sourceSector, y
  3275.   sta partitionLBABegin, y
  3276.   inx
  3277.   iny
  3278.   cpy #$04
  3279.   bne CardCopyPartitionLBABeginX
  3280.  
  3281.  
  3282.   jmp GetVolumeID  ;;rts
  3283.  
  3284.  
  3285.  
  3286.  
  3287.  
  3288.  
  3289.  
  3290. DirFindEntryNew:
  3291.   lda #$00
  3292.   sta selectedEntry
  3293.   sta selectedEntry+1
  3294.  
  3295.   lda selectedEntry
  3296.   cmp filesInDir
  3297.   bne DirFindEntryNewLoop
  3298.   lda selectedEntry+1
  3299.   cmp filesInDir+1
  3300.   bne DirFindEntryNewLoop
  3301.   jmp DirFindEntryNewFailed
  3302.  
  3303.  
  3304. DirFindEntryNewLoop:
  3305.   jsr DirGetEntry
  3306.  
  3307.   ;;check if entry matches, only look at first 8 chars
  3308.   ldy #$00
  3309. DirFindEntryNewCharLoop:
  3310.   lda tempEntry, y
  3311.   cmp findEntry, y
  3312.   bne DirFindEntryNewNext
  3313.   iny
  3314.   cpy #$08
  3315.   bne DirFindEntryNewCharLoop
  3316.  
  3317.   rts  ;all 8 chars match
  3318.  
  3319. DirFindEntryNewNext:
  3320.   clc
  3321.   lda selectedEntry
  3322.   adc #$01
  3323.   sta selectedEntry
  3324.   lda selectedEntry+1            ;increment to next entry index
  3325.   adc #$00
  3326.   sta selectedEntry+1
  3327.  
  3328.   lda selectedEntry
  3329.   cmp filesInDir
  3330.   bne DirFindEntryNewLoop
  3331.   lda selectedEntry+1
  3332.   cmp filesInDir+1
  3333.   bne DirFindEntryNewLoop
  3334.  
  3335. DirFindEntryNewFailed:
  3336.   lda #messageNOTFOUND
  3337.   sta temp
  3338.   jsr StrCopy
  3339.   lda #$1A
  3340.   sta printY
  3341.  
  3342. FileNotFound:
  3343.   ldy #$00
  3344. FileNotFoundLoop:
  3345.   lda findEntry, y
  3346.   sta printString, y
  3347.   iny
  3348.   cpy #$08
  3349.   bne FileNotFoundLoop
  3350.   jmp Forever
  3351.  
  3352.  
  3353. WaitFrame:
  3354.   lda frameCounter
  3355. WaitFrameLoop:
  3356.   cmp frameCounter
  3357.   beq WaitFrameLoop
  3358.   rts
  3359.  
  3360.  
  3361. NewCFBoot:
  3362. NewCFInserted:
  3363.   lda CARDSTATUS           ;card status read
  3364.   cmp #$FF                 ;compare to 11111111
  3365.   bne NewCFResetDone
  3366.   jsr WaitFrame
  3367.   lda #messageNOCARD       ;load message = no card
  3368.   sta temp
  3369.   jsr StrCopy
  3370.   lda #$07                 ; message position
  3371.   sta printY
  3372.   jmp ForeverLoop          ;stop everything
  3373.  
  3374.  
  3375. NewCFReset:
  3376. ;  lda #%00001110
  3377. ;  sta CARDDEVICE
  3378. ;  jsr WaitFrame
  3379. ;  jsr WaitFrame
  3380. ;  lda #%00001010
  3381. ;  sta CARDDEVICE    ;;do software reset
  3382.   lda #$FF
  3383.   sta $8000
  3384.   jsr WaitFrame
  3385.   jsr WaitFrame
  3386.   jsr WaitFrame
  3387.   jsr WaitFrame
  3388.   lda #$00
  3389.   sta $8000
  3390.   jsr WaitFrame
  3391.   jsr WaitFrame
  3392.   jsr WaitFrame
  3393.   jsr WaitFrame
  3394.  
  3395. NewCFResetDone
  3396.   jsr WaitFrame
  3397.   jsr WaitFrame
  3398.   jsr WaitFrame
  3399.   jsr WaitFrame
  3400.   jsr WaitFrame
  3401.   jsr WaitFrame
  3402.   jsr WaitFrame
  3403.   jsr WaitFrame
  3404.   jsr WaitFrame
  3405.  
  3406.   lda #$00
  3407.   sta sourceSector+1       ;use as loop counter
  3408. NewCFBusy:
  3409.   lda CARDSTATUS           ;card status read
  3410.   sta sourceSector         ;store somewhere unused
  3411.   and #%10000000
  3412.   cmp #%10000000           ;check busy bit
  3413.   bne NewCFNotBusy
  3414.  
  3415.   lda #messageCARDBUSY     ;load message = card busy
  3416.   sta temp
  3417.   jsr StrCopy
  3418.   lda #$1A                
  3419.   sta printY
  3420.  
  3421.   lda sourceSector         ;card status read
  3422.   sta temp
  3423.   jsr Byte2Str
  3424.  
  3425.   jsr WaitFrame
  3426.  
  3427.   lda sourceSector+1
  3428.   clc
  3429.   adc #$01
  3430.   sta sourceSector+1
  3431.   cmp #30              ;;only check busy bit for 30 frames
  3432.   beq NewCFReset
  3433.   jmp NewCFBusy
  3434.  
  3435.  
  3436. NewCFNotBusy:
  3437.   lda #$00
  3438.   sta sourceSector+1       ;use as loop counter
  3439.  
  3440. NewCFReady:
  3441.   lda CARDSTATUS           ;card status read
  3442.   sta sourceSector
  3443.   and #%11010000
  3444.   cmp #%01010000           ;check busy+ready bit
  3445.   beq NewCFDone
  3446.  
  3447. NewCFNotReady:
  3448.   lda #messageNOTREADY     ;load message = card not ready
  3449.   sta temp
  3450.   jsr StrCopy
  3451.   lda #$1A                
  3452.   sta printY
  3453.  
  3454.   lda sourceSector           ;card status read
  3455.   sta temp
  3456.   jsr Byte2Str
  3457.  
  3458.   jsr WaitFrame
  3459.  
  3460.   lda sourceSector+1
  3461.   clc
  3462.   adc #$01
  3463.   sta sourceSector+1
  3464.   cmp #30              ;;only check ready bit for 30 frames
  3465.   bne NewCFReady
  3466.   jmp NewCFReset
  3467.  
  3468.  
  3469. NewCFDone:
  3470.   jmp CardReady
  3471. ;--------------------End Code--------------------------:
  3472. ;------------------------------------------------------:
  3473.  
  3474.   .org  $FFFA
  3475.   .dw   NMI    ;FFFa FFFb
  3476.   .dw   RESET  ;FFFc FFFd
  3477.   .dw   IRQ    ;FFFe FFFf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement