
;Willams cart Bank Copy / Run routine
;written by Nir Dary
;
;i.e SpeedKing game from Mastertronics
;Loads to memory $5480 -> $9840, Run address $9800

DOSVEC    = $0A
TMPFROM   = $CA ;Tmp Source addrss
TMPTO     = $CC ;Tmp Destination address
TMPEND    = $CE ;Tmp Last source address to copy
TMPTABLE  = $D0 ;Table address
TMPRUN    = $D2 ;Game Run address

         *=$0600 ; (This Data will be located at $A000)

TABLE  .BYTE $00,$32 ; Game Start Addrss $3200
       .BYTE $00,$00,$A2,$E7,$2F,$00,$B4 ; Bank 00, A100 -> 2FE7 (move bytes)
       .BYTE $FF,$00,$00,$00,$00,$00,$00 ; $FF to end copy routine.
       .BYTE $00,$00,$00,$00,$00,$00,$00 ; $00 are extra space for future
       .BYTE $00,$00,$00,$00,$00,$00,$00 ; Hex edit purpose
       .BYTE $00,$00,$00,$00,$00,$00,$00
       .BYTE $00,$00,$00,$00,$00,$00,$00
       .BYTE $00,$00,$00,$00,$00,$00,$00
       .BYTE $00,$00,$00,$00,$00,$00,$00
       .BYTE $00

START   STX $03E9                        ;address $0700 ($A100)
        STX $03EA
        DEX
        STX $D301
        LDA #$01
        STA $09
        STA $42
        STA $03F8

        LDA #<TABLE      ; Points to the begining of the table
        STA TMPTABLE
        LDA #>TABLE
        STA TMPTABLE+1

        LDA (TMPTABLE),Y   ; Load GameRun Address from table
        STA TMPRUN
        INC TMPTABLE
        LDA (TMPTABLE),Y
        STA TMPRUN+1
        INC TMPTABLE

        LDA #$00    ; Move 0100 bytes to 0600 region, jump there to finish load
        STA TMPTO
        STA TMPFROM
        TAY
        LDA #$06
        STA TMPTO+1
        LDA #$A0
        STA TMPFROM+1
L11     LDA (TMPFROM),Y
        STA (TMPTO),Y
        INY
        BNE L11
        JMP COPY    ;ie. jmp 0600 region to move code out of cartridge


COPY      LDA #$00
          STA $D40E
          SEI
          LDA #$FE
          STA $D301
L22       LDY #$00           ;Load Bank# from table into X register
          LDA (TMPTABLE),Y   ;and deted if End of Table then exit to run game
          CMP #$FF
          BEQ EXITCOPY
          TAX
          INC TMPTABLE

          LDA (TMPTABLE),Y   ; Load Source Address from table
          STA TMPFROM
          INC TMPTABLE
          LDA (TMPTABLE),Y
          STA TMPFROM+1
          INC TMPTABLE

          LDA (TMPTABLE),Y   ;Load Destination Address from table
          STA TMPTO
          INC TMPTABLE
          LDA (TMPTABLE),Y
          STA TMPTO+1
          INC TMPTABLE

          LDA (TMPTABLE),Y   ;Load End address of copy sequace
          STA TMPEND
          INC TMPTABLE
          LDA (TMPTABLE),Y
          STA TMPEND+1
          INC TMPTABLE
                             ;Start of Copy Routine
MAINLOOP  STA $D500,X        ;enter Williams cart desired bank
          LDA (TMPFROM),Y
          STA $D508          ;Turn cartrige off
          STA (TMPTO),Y
          INC TMPFROM
          BNE L1
          INC TMPFROM+1
L1        INC TMPTO
          BNE L2
          INC TMPTO+1
L2        LDA TMPFROM+1      ; Copy unitl address end addres in table
          CMP TMPEND+1
          BNE MAINLOOP
          LDA TMPFROM
          CMP TMPEND
          BNE MAINLOOP
          BEQ L22       ; Do next Bank?

EXITCOPY  LDA #$22
          STA $D400     ;DMA control
          LDA #$FF
          STA $D301     ;RAM under ROM off, ROM on
          LDA #$40      ;Some games need C0???
          STA $D40E     ;NMI interrupts allowed, see Mapping Atari
          CLI
          LDA TMPRUN
          STA DOSVEC
          LDA TMPRUN+1
          STA DOSVEC+1
          LDA #$C0      ;C000 high byte
          STA $2E4      ;system size, sometimes not important, sometimes is!
          STA $6A  ;dl list ends here and goes down in memory apprx0300h bytes.
          LDA #$00      ;cart interlock, could be LDA d013
          STA $03FA     ;off
          STA $42       ;critic OK for long interrupts
          RTS

          .BYTE $00,$00,$00,$00,$00 ; extra bytes to fill till $*+0100

          *=$BFFA
         .WORD START
         .BYTE $00   ;MUST BE 0
         .BYTE $04   ;DOS=5,NODOS=4
         .WORD START