/**************************************************************************************************
  Filename:       chipcon_banked_code_support.s51
  Revised:        $Date: 2007-08-17 10:12:09 -0700 (Fri, 17 Aug 2007) $
  Revision:       $Revision: 15106 $

  Description:    Functions to make banked CALL and RET.


  Copyright 2005-2007 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com. 
**************************************************************************************************/

/******************************************************************************

The register holding current code bank:
?CBANK

Should be defined to IAR XLINK Linker with:
-D?CBANK=xx

Copyright 2004-2005 IAR Systems. All rights reserved.

******************************************************************************/



        MODULE  ?BANKED_CODE_SUPPORT

#include "iar_common.h"

        EXTERN  ?CBANK
        EXTERN  ?RESET_CODE_BANK
        REQUIRE ?RESET_CODE_BANK
        RSEG    CSTART:CODE:NOROOT



;-----------------------------------------------------------------------------
;
;       Function: ?BCALL
;
;       Description: Saves the caller bank on stack, Switches to bank defined
;                    by A register and jumps to the banked function A:DPTR.
;
;       Register input:
;               DPTR:  16-bit function address
;               A:     8-bit page address
;
;       Stack:
;
;           +--------+
;           | ?CBANK | <-- SP end
;           + - - - -+
;           |  retH  | <-- SP begin
;           + - - - -+
;           |  retL  |
;           + - - - -+
;   CFA --> | xxxxxx |
;           +--------+
;
;               16-bit return address
;
;       Register output:
;               DPTR:  16-bit function-address
;               A  = 0
;
;       Stack usage: 1
;-----------------------------------------------------------------------------
   PUBLIC  ?BCALL

   cfi BLOCK ?BCALL USING cfi_common
   cfi NOFUNCTION
   cfi A undefined
   cfi CFA_MACHINE MACHINE_SP - 2

?BCALL:
   SWAP  A
   MOV   B, A
   MOV   A, ?CBANK   ; read current MEMCTR
   ANL   A, #0x30    ; mask bank bits
   SWAP  A
   PUSH  A           ; store return bank (bank number only)

   cfi CFA_MACHINE MACHINE_SP - 3

   MOV   A, ?CBANK   ; read current MEMCTR
   ANL   A, #0xCF    ; mask bank bits
   ORL   A, B        ; add new bank number to MEMCTR
   MOV   ?CBANK, A

   CLR   A
   JMP   @A+DPTR


;-----------------------------------------------------------------------------
;
;       Function: ?BRET
;
;       Description: Return from banked function.
;                    Leaves current function, Switched bank and returns to
;                    function that called ?BCALL.
;
;                    NOTE: This function will be jumped to.
;
;       Register input:
;               None
;
;       Stack input:
;
;               +------------+ <-- SP
;               |  RET bank  |
;               + - - - - - -+
;               |  RET High  |
;               + - - - - - -+
;               |  RET Low   |
;               +------------+
;
;               24-bit return address
;
;       Register output:
;               DPTR:  16-bit function-address
;               A  = 0
;
;       Stack usage: 3
;               Stack: 16-bit return address
;-----------------------------------------------------------------------------
   PUBLIC  ?BRET

?BRET:
   cfi CFA_MACHINE MACHINE_SP - 3

   MOV   B, ?CBANK   ; get current bank
   ANL   B, #0xCF    ; mask bank bits

   POP   A           ; pop bank number
   SWAP  A
   ORL   A, B        ; add the MEMCTR bits
   MOV   ?CBANK, A   ; set new bank
   cfi   CFA_MACHINE MACHINE_SP - 2

   RET

;-----------------------------------------------------------------------------
;
;	Function: ?BDISPATCH
;
;	Description: Pushes current bank number, and reads function address
; 	from relay function into ?CBANK and onto stack and RET's
;
;	Register input:
;
;	Stack: +1
;
;	Register output: A = ?
;			 DPTR = 16-bit function adress
;
;	Stack usage:	PUSH	A
;-----------------------------------------------------------------------------
	PUBLIC	?BDISPATCH

         CFI CFA_MACHINE MACHINE_SP - 4

?BDISPATCH:
	POP	DPH
        CFI CFA_MACHINE MACHINE_SP - 3
	POP	DPL
        CFI CFA_MACHINE MACHINE_SP - 2

        MOV     A,?CBANK
        SWAP    A
        ANL     A,#0x03
	PUSH	A
        CFI CFA_MACHINE MACHINE_SP - 3

	CLR	A
	MOVC	A,@A+DPTR
	PUSH	A
        CFI CFA_MACHINE MACHINE_SP - 4
	MOV	A,#1;
	MOVC	A,@A+DPTR
	PUSH	A
        CFI CFA_MACHINE MACHINE_SP - 5
	MOV	A,#2
	MOVC	A,@A+DPTR

        SWAP    A
        ANL     A,#0x30
        MOV     B,A
	MOV	A,?CBANK
        ANL     A,#0xCF
        ORL     A,B
	MOV	?CBANK,A

	RET	

  cfi ENDBLOCK ?BCALL

   END
