/**************************************************************************************************
  Filename:       chipcon_cexit.s51
  Revised:        $Date: 2006-05-17 10:49:59 -0700 (Wed, 17 May 2006) $
  Revision:       $Revision: 10824 $

  Description:    System termination code for the 8051 IAR Systems C/EC++ Compiler.


  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.
**************************************************************************************************/

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

Include this file in the project and add $TOOLKIT_DIR$\SRC\LIB\ to
the assembler include path.

Copyright 2004-2005 IAR Systems. All rights reserved.

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

;
; Naming covention of labels in this file:
;
;  ?xxx	  - External labels only accessed from assembler.
;  __xxx   - External labels accessed from or defined in C.
;  xxx	  - Labels local to one module (note: this file contains
;           several modules).
;  main	  - The starting point of the user program.
;

#define XRSEGCSTART RSEG CSTART:CODE:NOROOT
#include "iar_common.h"

	     EXTERNS_FOR_ALL_DPTR_SYMBOLS()

#if (defined(__DLIB_BUILD__) )


//------------------------------------------------------------------------
//
// DLIB exit sequence
//

// ---------------------------------------------------------
// The exit system
//
// The default exit function, written in C, calls _exit provided
// below.  This function performs system cleanup and calls __exit that
// halts the system.
//

// -----------------------------------------------
// _exit -- Perform system cleanup and jump to __exit.
//
// The module below contains a number of segment parts that is
// included in the final application only if they are needed.
//

   XRSEGCSTART
   MODULE    ?cexit
   PUBLIC    _exit
   FUNCTION  _exit,0203H
   EXTERN    __exit
   REQUIRE   __exit
   REQUIRE   ?exit_br_xxexit

_exit:

// -------------------------------------
// Segment part included if the argument to exit must be saved and
// later restored.

   XRSEGCSTART
   REQUIRE ?exit_restore

?exit_save:
   MOV     A,R2
   MOV     R6,A
   MOV     A,R3
   MOV     R7,A


// -------------------------------------
// Segment part included if destructors must be called.

   XRSEGCSTART
   PUBLIC  __cstart_call_dtors
   EXTERN  __call_dtors
   REQUIRE ?exit_save

   // This label is required by "__record_needed_destruction".
__cstart_call_dtors:
#if (__CODE_MODEL__ == __CM_BANKED__)
   EXTERN  ?BCALL

   MOV     DPL,#low(__call_dtors)
   MOV     DPH,#high(__call_dtors)
#ifdef __EXTENDED_DPTR__
   MOV     ?DPX,#0
#endif
   MOV     A,#BYTE3(__call_dtors)
   LCALL   ?BCALL
#else
   LCALL   __call_dtors
#endif


// -------------------------------------
// Segment part included if DLib standard IO is used.

   XRSEGCSTART
   PUBLIC  __cstart_closeall
   REQUIRE ?exit_save

   // This label is required by DLib.
__cstart_closeall:
   EXTERN  _Close_all
#if (__CODE_MODEL__ == __CM_BANKED__)
   EXTERN  ?BCALL

   MOV     DPL,#low(_Close_all)
   MOV     DPH,#high(_Close_all)
#ifdef __EXTENDED_DPTR__
   MOV     ?DPX,#0
#endif
   MOV     A,#BYTE3(_Close_all)
   LCALL   ?BCALL
#else
   LCALL   _Close_all
#endif


// -------------------------------------
// Restore the argument previously stored by the "save" section above.

   XRSEGCSTART

?exit_restore:
   MOV     A,R6
   MOV     R2,A
   MOV     A,R7
   MOV     R3,A

   XRSEGCSTART

   // Jump to __exit.
?exit_br_xxexit:

#if (__CODE_MODEL__ == __CM_BANKED__)
   EXTERN  ?BCALL

   MOV     DPL,#low(__exit)
   MOV     DPH,#high(__exit)
#ifdef __EXTENDED_DPTR__
   MOV     ?DPX,#0
#endif
   MOV     A,#BYTE3(__exit)
   LCALL   ?BCALL
#else
   LJMP     __exit         // __exit will not return
#endif

   ENDMOD

   END


#else // __CLIB_BUILD__


//------------------------------------------------------------------------
//
// CLIB exit(int code)
//
//------------------------------------------------------------------------

   MODULE   ?cexit

   PUBLIC   exit
   // PUBLIC   ?C_EXIT
   XRSEGCSTART
	FUNCTION exit,0203H

exit:
   // ?C_EXIT:
   PUBLIC  ?ROM_MONITOR_NOPS

?ROM_MONITOR_NOPS:
   NOP                     ; Three NOPs for ROM-Monitor breaks.
   NOP
   NOP

   SJMP    exit
	
   ENDMOD

   END
#endif
