;----------------------------------------------------------------------
; LANGUAGE CARD LOADER                                                 |
;                                                                      |
; Copyright Apple Computer, Inc. 1984, 1985
; All Rights Reserved
;                                                                      |
; * This file is for the assembly language programmer who cannot       |
;     assume that SANE is preloaded by the user.  It gives the         |
;     programmer the capability to load SANE directly, and might be    |
;     included as part of a stand-alone package designed to boot,      |
;     load SANE, and run.                                              |
;                                                                      |
;                                                                      |
; * Soft-switches are used in several places in the assembly code      |
;     below.  For an explanation of what these switches do and how     |
;     to use them refer to the Apple // Reference Manual, pages 68-75. |
;                                                                      |
; * Assumes the file A2X.AFP.CODE has already been loaded at $1E00.    |
; * Assumes the file A2X.BANKSW.0 has already been loaded at $4200.    |
;        (Note: If bankswitching is not required, ignore (6) below.)   |
;                                                                      |
; * The disk version of A2X.AFP.CODE has 512 bytes of 'header'         |
;     information at the beginning of the file.  The actual code       |
;     starts $200 bytes into the file.  Therefore loading at $1E00     |
;     places the beginning of code at $2000.                           |
;                                                                      |
;                                                                      |
; * Moves code portion of A2X.AFP.CODE from $2000 to $E000.            |
; *   and A2X.BANKSW.0 from $4200 to top of language card, $FF80.      |
;          NOTE:  A2X.BANKSW.0 cannot be more than 126 bytes long.     |
;                                                                      |
; * The loader is broken into the modules:                             |
;                                                                      |
;  1.  READ/WRITE ENABLE LANGUAGE CARD RAM.                            |
;  2.  ENTRY TO AUXILIARY LANGUAGE CARD.                               |
;  3.  INITIALIZE STARTING ADDRESSES FOR MOVING CODE.                  |
;  4.  TRANSFER FP6502 TO LANGUAGE CARD.                               |
;  5.  TRANSFER BANKSWITCHER TO LANGUAGE CARD.                         |
;  6.  COPY INTERRUPT VECTOR FROM ROM TO RAM.                          |
;  7.  EXIT TO MAIN STACK/ZERO PAGE/LANGUAGE CARD.                     |
;  8.  RTS TO CALLER.                                                  |
;----------------------------------------------------------------------|
;
 ORG $300 ;put it where you like, relocatable code.
;  
ALTZP EQU $C009 ;soft switch
MAINZP EQU $C008 ;soft switch
MYSTATE EQU $C08X ;soft switch to return to user's state
;                                of RAM/ROM and 1st/2nd $D000 space.
;                                In Pascal, X=B. In ProDOS, usually X=A.
BASESTART EQU $06 ;zero page, for indirect addressing.
BASEEND EQU $08 ;zero page, for indirect addressing.
;
;------------------------------------------
; 1.   READ/WRITE ENABLE LANGUAGE CARD RAM.
;------------------------------------------
 LDA $C08B ;soft switches.  See Apple //
 LDA $C08B ;  Reference Manual for details.
;
;---------------------------------------
; 2.   ENTRY TO AUXILIARY LANGUAGE CARD.
;---------------------------------------
 PHP  ;Current interrupt state on stack.
 SEI  ;Disable interrupts while stack ptr bad.
 STA ALTZP ;switch to auxiliary stack, zero page, lc.
;
;----------------------------------------------------
; 3.   INITIALIZE STARTING ADDRESSES FOR MOVING CODE.
;----------------------------------------------------
 LDA #0 ;Note that $2000 and $E000 are hard-coded.
 STA BASESTART
 LDA #$20
 STA BASESTART+1
 LDA #$00
 STA BASEEND
 LDA #$E0
 STA BASEEND+1
;
;---------------------------------------
; 4.   TRANSFER FP6502 TO LANGUAGE CARD. 
;---------------------------------------
 LDX #$20 ;transfer 8K bytes (or 32 pages). 
ENGINE LDY #0
LOOP LDA (BASESTART),Y ;collect Y-th byte of engine.
 STA (BASEEND),Y ;store at Y-th byte of page now loading.
 INY  ;next Y.
 BNE LOOP ;done with 256 bytes?
 INC BASESTART+1 ;   if so, go on to next page of source.
 INC BASEEND+1 ;   and of destination.
 DEX
 BNE ENGINE ;keep going if 32 pages not yet moved.
;
;---------------------------------------------
; 5.   TRANSFER BANKSWITCHER TO LANGUAGE CARD.
;---------------------------------------------
;  The last 128 bytes of memory are overwritten by the bankswitcher
;    and the interrupt vector.
 LDY #$7D ;prepare to transfer 126 bytes.
AGAIN LDA $4200,Y
 STA $FF80,Y
 DEY
 BPL AGAIN
;
;--------------------------------------------
; 6.   COPY INTERRUPT VECTOR FROM ROM TO RAM.
;--------------------------------------------
 LDA $C089 ;soft switch, read ROM, write RAM
 LDA $FFFE ;get low byte from ROM
 STA $FFFE ;  put it in RAM
 LDA $FFFF ;get high byte from ROM
 STA $FFFF ;  put it in RAM
 LDA MYSTATE ;restore user state of high
 LDA MYSTATE ;  address space ($D000-FFFF).
;
;------------------------------------------------
; 7.  EXIT TO MAIN STACK/ZERO PAGE/LANGUAGE CARD.
;------------------------------------------------
 STA MAINZP
 PLP  ;Restore original interrupt state.
;
;-------------------
; 8.  RTS TO CALLER.
;-------------------
 RTS

