;Copy / Run routine
;written by Nir Dary
;
;
GSA       = $0606 ;Game Start Adress
DOSINI    = $0C ;**
DOSVEC    = $0A
TMPFROM   = $CA ;Tmp Source addrss
TMPTO     = $CC ;Tmp Destination address
TMPEND    = $CE ;Tmp Last source address to copy
TMPTABLE  = $D0 ;Table address
;
        Org $0500 ; (This Data will be located at $A000)
;
TABLE  .BYTE $00,$00,$A2,$00,$06,$7F,$A3
       .BYTE $01,$00,$A0,$00,$08,$00,$C0
       .BYTE $02,$00,$A0,$00,$28,$00,$C0
       .BYTE $03,$00,$A0,$00,$48,$00,$C0
       .BYTE $04,$00,$A0,$00,$68,$00,$C0
       .BYTE $05,$00,$A0,$00,$88,$00,$B0
       .BYTE $FF  ; $FF ends the copy routine
;
START   STX $03E9       ;Cassette vector disabled - X=0
        STX $03EA       ;Cassette vector disabled
        DEX
        STX $D301       ;Basic off - hardware, no buttons to hold down
        LDA #$01
        STA $09         ;Boot? - say booted
        STA $42         ;Critic set = short VB interrupts only
        STA $03F8       ; Basic off flag - software, ditto on buttons
;
;
;
        LDA #$00        ; Move 0100 bytes to 0500 region, jump there to finish
        STA TMPTO
        STA TMPFROM
        STA TMPTABLE
        TAY
        LDA #$05        ; PAGE 5
        STA TMPTO+1
        STA TMPTABLE+1
        LDA #$A0
        STA TMPFROM+1
L11     LDA (TMPFROM),Y
        STA (TMPTO),Y
        INY
        BNE L11
        JMP COPY    ;ie. jmp 0600 region (COPY) 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 on
          LDA #$40      ;Some games need C0???**
          STA $D40E     ;NMI interrupts allowed, see Mapping Atari
          CLI

        LDA #<GSA
        STA DOSINI
        STA DOSVEC
        LDA #>GSA
        STA DOSINI+1
        STA DOSVEC+1
;
          LDA #$C0      ;C000 high byte
          STA $2E4      ;system size, sometimes not important, sometimes is!
          STA $2E6      ;**
          STA $6A       ;dlist ends and goes down in memory apprx0300h bytes.
;
          LDA #$00      ;cart interlock, could be LDA d013
          STA $03FA     ;no cart status = off
          STA $42       ;critic OK for long interrupts
          STA $244      ; COLDST
          LDA #$01
          STA $08       ; WARMST
          TAY
          CLC
;          JMP $E474
          RTS

;
          Org $1FFA

          .WORD $0000
          .BYTE $00
          .BYTE $00
          .WORD START
