www.pudn.com > S12G_Bootloader_Host_App.zip > startup.c, change:2011-08-03,size:3969b
/*************************************************** FILE : startup.c PURPOSE : standard startup code LANGUAGE : ANSI-C / HLI ***************************************************/ #include <hidef.h> #include <startup.h> /***************************************************/ struct _tagStartup _startupData; /* startup info */ /*-------------------------------------------------*/ static void ZeroOut(struct _tagStartup *_startupData) { /* purpose: zero out RAM-areas where data is allocated.*/ int i, j; unsigned char *dst; _Range *r; r = _startupData->pZeroOut; for (i=0; i<_startupData->nofZeroOuts; i++) { dst = r->beg; j = r->size; do { *dst = '\0'; /* zero out */ dst++; j--; } while(j>0); r++; } } /*----------------------------------------------------*/ static void CopyDown(struct _tagStartup *_startupData) { /* purpose: zero out RAM-areas where data is allocated. this initializes global variables with their values, e.g. 'int i = 5;' then 'i' is here initialized with '5' */ int i; unsigned char *dst; int *p; /* _startupData.toCopyDownBeg ---> */ /* {nof(16) dstAddr(16) {bytes(8)}^nof} Zero(16) */ p = (int*)_startupData->toCopyDownBeg; while (*p != 0) { i = *p; /* nof */ p++; dst = (unsigned char*)*p; /* dstAddr */ p++; do { /* p points now into 'bytes' */ *dst = *((unsigned char*)p); /* copy byte-wise */ dst++; ((char*)p)++; i--; } while (i>0); } } /*---------------------------------------------------*/ static void CallConstructors(struct _tagStartup *_startupData) { /* purpose: C++ requires that the global constructors have to be called before main. This function is only called for C++ */ #ifdef __ELF_OBJECT_FILE_FORMAT__ short i; _Cpp *fktPtr; fktPtr = _startupData->initBodies; for (i=_startupData->nofInitBodies; i>0; i--) { fktPtr->initFunc(); /* call constructors */ fktPtr++; } #else _PFunc *fktPtr; fktPtr = _startupData->mInits; if (fktPtr != NULL) { while(*fktPtr != NULL) { (**fktPtr)(); /* call constructors */ fktPtr++; } } #endif } /*-------------------------------------------------------*/ static void ProcessStartupDesc(struct _tagStartup *); /*----------------------------------------------------*/ static void InitRomLibraries(struct _tagStartup *_sData) { /* purpose: ROM libraries have their own startup functions which have to be called. This is only necessary if ROM Libraries are used! */ #ifdef __ELF_OBJECT_FILE_FORMAT__ short i; _LibInit *p; p = _sData->libInits; for (i=_sData->nofLibInits; i>0; i--) { ProcessStartupDesc(p->startup); p++; } #else _LibInit *p; p = _sData->libInits; if (p != NULL) { do { ProcessStartupDesc(p->startup); } while ((long)p->startup != 0x0000FFFF); } #endif } /*----------------------------------------------------*/ static void ProcessStartupDesc(struct _tagStartup *_sData) { ZeroOut(_sData); CopyDown(_sData); #ifdef __cplusplus CallConstructors(_sData); #endif if (_sData->flags&STARTUP_FLAGS_ROM_LIB) { InitRomLibraries(_sData); } } /*-------------------------------------------------------*/ #pragma NO_EXIT #ifdef __cplusplus extern "C" #endif void _Startup (void) { for (;;) { asm { /* put your target specific initialization */ /* (e.g. CHIP SELECTS) here */ } if (!(_startupData.flags&STARTUP_FLAGS_NOT_INIT_SP)) { /* initialize the stack pointer */ INIT_SP_FROM_STARTUP_DESC(); /* defined in hidef.h */ } ProcessStartupDesc(&_startupData); (*_startupData.main)(); /* call main function */ app_entry(); } /* end loop forever */ } /*-------------------------------------------------------*/