;----------------------------------------------------------------------
; A2X.BANKSW  ------------                                             |
;
; Copyright Apple Computer, Inc. 1984, 1985
; All Rights Reserved
;                                                                      |
; * This file is for the assembly language programmer who is using     |
;      both main banks of memory in a 128K Apple //.  It allows the    |
;      program and its data to reside in different banks.              |
;                                                                      |
; * The soft switches RAMRD and RAMWRT ($C00X and $C01X) are used in   |
;      the assembly code below.  See the Apple // Reference Manual,    |
;      page 75, for details.                                           |
;                                                                      |
; * To use this routine:                                               |
;      1)  Store the number of the bank containing data at $FFFD.      |
;      2)  DO NOT make calls to FP6502 at $E000 as usual.  Instead     |
;          change your code to enter at $FF80.  (FP6502  EQU  $FF80)   |
;                                                                      |
;                                                                      |
; * NOTE : Using this routine REQUIRES that the byte DATABANK          |
;          at $FFFD be preset by the user to either:                   |
;                    0 - Data in Main memory bank    or                |
;                    1 - Data in Alternate memory bank.                |
;                                                                      |
; * This is a sample file.  If additional code is required, this       |
;      routine can be expanded to a maximum of 126 bytes - it must     |
;      begin at or after $FF80 and must not interfere with $FFFE/FFFF. |
;                                                                      |
;                                                                      |
; MODULES:                                                             |
;      1.   REMOVE RETURN ADDRESS FROM STACK AND SAVE.                 |
;      2.   SAVE CURRENT STATE OF BANK SWITCHES.                       |
;      3.   SWITCH TO DATA BANK.                                       |
;      4.   JSR TO THE REAL FP6502 AT $E000.                           |
;      5.   SET BANK SWITCHES BACK TO ORIGINAL STATE.                  |
;      6.   PUT RETURN ADDRESS ON THE STACK AND RTS TO CALLER.         |
;                                                                      |
;---------------------------------------------------------------------- 
;
 ORG $FF80
;
; data bank byte (0 or 1) must be previously loaded at $FFFD.
;
DATABANK EQU $FFFD
SANE EQU $E000
;
;------------------------------------------------
; 1.   REMOVE RETURN ADDRESS FROM STACK AND SAVE.
;------------------------------------------------
 PLA  ;save return address in RETURN
 STA RETURN ;   (see end of program).
 PLA
 STA RETURN+1
;
;------------------------------------------
; 2.   SAVE CURRENT STATE OF BANK SWITCHES.
;------------------------------------------
 LDA #0 ;record current bank arrangement as
 STA CALRDBNK ;       0 , for main memory.
 STA CALWRTBNK
 LDA $C013 ;check for actual readbank.
 BPL NEXT ;       if main was correct, go on
 INC CALRDBNK ;      else readbank was bank 1.
NEXT LDA $C014 ;check for actual writebank.
 BPL SWITCH ;       if main was correct, go on
 INC CALWRTBNK ;       else writebank was bank 1.
;
;--------------------------
; 3.   SWITCH TO DATA BANK.
;--------------------------
SWITCH LDX DATABANK 
 STA $C002,X ;X=0 ==> read main,    X=1 ==> read aux. 
 STA $C004,X ;X=0 ==> write main,   X=1 ==> write aux. 
;
;--------------------------------------
; 4.   JSR TO THE REAL FP6502 AT $E000.
;--------------------------------------
 JSR SANE
;
;-----------------------------------------------
; 5.   SET BANK SWITCHES BACK TO ORIGINAL STATE.
;-----------------------------------------------
 LDX CALRDBNK 
 STA $C002,X ;X=0 ==> read main,   X=1 ==> read aux.  
 LDX CALWRTBNK
 STA $C004,X ;X=0 ==> write main,  X=1 ==> write aux.  
;
;
;--------------------------------------------------------
; 6.   PUT RETURN ADDRESS ON THE STACK AND RTS TO CALLER.
;--------------------------------------------------------
 LDA RETURN+1 
 PHA
 LDA RETURN
 PHA
;
 RTS
;
;------------------------------------------------------------
; **   STORAGE FOR RETURN ADDRESS AND STATE OF SOFT SWITCHES.
;------------------------------------------------------------
RETURN DW 0 ;Store return address here.
CALRDBNK DB 0 ;Caller Read Bank.
CALWRTBNK DB 0 ;Caller Write Bank. 

