#define VECTOR_C
/******************************************************************************
*
*   Freescale(TM) and the Freescale logo are trademarks of Freescale Semiconductor, Inc. 
*   All other product or service names are the property of their respective owners. 
*   (c) Copyright 2013 Freescale Semiconductor Inc.
*   All Rights Reserved.
*
*   You can use this example for any purpose on any computer system with the
*   following restrictions:
*
*   1. This example is provided  as is , without warranty.
*
*   2. You don't remove this copyright notice from this example or any direct
*      derivation thereof.
*
* Description:    Vector table for OS/MPC574xGv4
*
* Notes:
*
******************************************************************************/

#include    <Os.h>
#include    <Os_platform_config.h>
#include    <Os_compiler_internal_api.h>

#define OSVECTORS_START_SEC_CODE
#include <MemMap.h>

#if defined(OSDIABPPC)
#define OSALIGN   asm volatile (" .skip 0x0C ")
#endif
#if defined(OSGHSPPC)
#define OSALIGN   asm (" .skip 0x0C ")
#endif

#if defined(OSDIABPPC) || defined(OSGHSPPC)
    OSASMF(   .globl VTABLE                   );
    OSASMF(VTABLE:                            ); /* Vector table start address */

#elif defined(OSCWPPC)  /* defined(OSDIABPPC) || defined(OSGHSPPC) */

extern void OSCriticalException( void );
extern void OSMachineCheckException( void );
extern void OSNonCriticalException( void );
extern void OSInterruptDispatcher( void );
extern void OSSystemCallHandler( void );
extern void OSDebugException( void );
extern void VTABLE( void );
#pragma function_align 16
#define SIXTEEN_BYTES 16
#pragma force_active on

#define OSALIGN  .align SIXTEEN_BYTES

/* The section .vector (which contains VTABLE) should have proper alignment in linker file */
asm void VTABLE( void )
{
    nofralloc
#endif

    OSASMF(    e_b   OSCriticalException        ); /* 0x00, Critical input */

    OSALIGN;
    OSASMF(    e_b   OSMachineCheckException    ); /* 0x10, Machine check */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0x20, Data storage */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0x30, Instruction storage */

    OSALIGN;
    OSASMF(    e_b   OSInterruptDispatcher      ); /* 0x40, External input */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0x50, Alignment */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0x60, Program */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0x70, Performance monitor */

    OSALIGN;
#if defined(OSSC3) || defined(OSSC4)               /* 0x80, System call */
    OSASMF(    e_b   OSSystemCallHandler        );
#else
    OSASMF(    e_b   OSNonCriticalException     );
#endif

    OSALIGN;
    OSASMF(    e_b   OSDebugException           ); /* 0x90, Debug */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xA0, EFP data */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xB0, EFP round */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xC0, reserved */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xD0, reserved */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xE0, reserved */

    OSALIGN;
    OSASMF(    e_b   OSNonCriticalException     ); /* 0xF0, reserved */

#if defined(OSCWPPC)
}
#endif

#define OSVECTORS_STOP_SEC_CODE
#include <MemMap.h>

#if defined(FLASH_START)

#define SRAM_BASE_ADDR 0x40000000

#if defined(OSMPC5748G)
#define OSLOOP_SIZE (768*1024)>>7

#elif defined(OSMPC5746C)
#define OSLOOP_SIZE (512*1024)>>7

#else
#error  MCU type is not specified 
#endif

/* Code to intialize all internal RAM.
   This must be executed before using RAM to avoid ECC errors. */
#if defined(OSDIABPPC) || defined(OSGHSPPC)

    OSASMF2( .set BASE_SRAM_ADDRESS, SRAM_BASE_ADDR );
    OSASMF2( .set OS_LOOP_SIZE, OSLOOP_SIZE         );
   
    OSASMF(   .align 4                              );
#if defined(OSGHSPPC)
    OSASMF(     .globl __ghs_board_memory_init      );

    OSASMF( __ghsautoimport_ghs_board_memory_init:  );
    OSASMF( __ghs_board_memory_init:                );
#else
    OSASMF(    .globl          usr_init             );

    OSASMF( usr_init:                               );
#endif /* defined(OSGHSPPC) */

    

#elif defined(OSCWPPC)
void usr_init( void );    
asm void usr_init( void )
{
    nofralloc
#endif

#if (OS_MASTER_CORE == OS_HW_MASTER_CORE)

    /* Do this only on Z4 */

    OSASMF2(    e_lis r11,%hi(BASE_SRAM_ADDRESS)    );
#if 0    
    /* Disable SWT0 */
    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_or2i    r6,0x0010);
    OSASMF2(e_li      r7,0xC520);
    OSASMF2(se_stw    r7,0x0(r6));
    OSASMF2(e_li      r7,0xD928);
    OSASMF2(se_stw    r7,0x0(r6)); /* Disengage soft-lock for SWT0 */

    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_lis     r7,0xFF00);
    OSASMF2(e_or2i    r7,0x000A);
    OSASMF2(se_stw    r7,0x0(r6)); /* SWT0_CR_WEN = 0  */

    /* Disable SWT1 */
    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_or2i    r6,0x4010);
    OSASMF2(e_li      r7,0xC520);
    OSASMF2(se_stw    r7,0x0(r6));
    OSASMF2(e_li      r7,0xD928);
    OSASMF2(se_stw    r7,0x0(r6)); /* Disengage soft-lock for SWT1 */

    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_or2i    r6,0x4000);
    OSASMF2(e_lis     r7,0xFF00);
    OSASMF2(e_or2i    r7,0x000A);
    OSASMF2(se_stw    r7,0x0(r6)); /* SWT1_CR_WEN = 0  */

#if defined(OSMPC5748G)
    /* Disable SWT2 */
    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_or2i    r6,0x8010);
    OSASMF2(e_li      r7,0xC520);
    OSASMF2(se_stw    r7,0x0(r6));
    OSASMF2(e_li      r7,0xD928);
    OSASMF2(se_stw    r7,0x0(r6)); /* Disengage soft-lock for SWT2 */

    OSASMF2(e_lis     r6,0xFC05);
    OSASMF2(e_or2i    r6,0x8000);
    OSASMF2(e_lis     r7,0xFF00);
    OSASMF2(e_or2i    r7,0x000A);
    OSASMF2(se_stw    r7,0x0(r6)); /* SWT2_CR_WEN = 0  */
#endif /* defined(OSMPC5748G) */

/* Code to initialize all internal RAM.
   This must be executed before using RAM to avoid ECC errors. */
#if defined(OSCWPPC)   
    OSASMF2(    e_lis r11,SRAM_BASE_ADDR@h          );    /* Base address of the L2SRAM, 64-bit word aligned */
#endif   
    OSASMF2(    e_li r12,OSLOOP_SIZE                );/* Loop counter to get all of L2SRAM; SIZE/4 bytes/32 GPRs */
    OSASMF(     mtctr r12                           );

    OSASMF( init_l2sram_loop:                       );
    OSASMF2(    e_stmw r0,0(r11)                    );/* Write all 32 GPRs to L2SRAM */
    OSASMF3(    e_addi r11,r11,128                  );/* Inc the ram ptr; 32 GPRs * 4 bytes = 128 */
    OSASMF(     e_bdnz init_l2sram_loop             );/* Branch OSLOOP_SIZE-1 times */
#endif    
#endif /* OS_MASTER_CORE == OS_HW_MASTER_CORE */    

    OSASMF(     se_blr                              );
    
    
#if defined(OSCWPPC)
}
#endif

#endif /* if defined(FLASH_START) */

/* Clear the noncacheable bss at start-up */
#if defined(OSDIABPPC)
#include <string.h>

extern OSBYTE OS_NONCACHE_BSS_START[];
extern OSDWORD OS_NONCACHE_BSS_SIZE[];

void noncache_bss_init( void )
{
    memset(OS_NONCACHE_BSS_START, 0, (OSDWORD) OS_NONCACHE_BSS_SIZE);
}
#endif

#if !defined(OS_MULTI_INSTANCE)

/* StartUp-s for the non-master cores. */

#define OSSTARTUP2_START_SEC_CODE  /* open the section .startup2 */
#include <MemMap.h>

extern void Core_ID_1_Startup (void);
extern void Core_ID_2_Startup (void);


#if defined(OSCWPPC)
extern void main();
extern OSDWORD OS2_MAIN_STACK_MAX[];     /* Main stack for second (non-master) autosar core - it is
                                            assigned in the linker script. */
extern char _SDA_BASE_[], _SDA2_BASE_[];

#pragma function_align 4

OSASM void Core_ID_1_Startup( void )
{
    nofralloc
#else
    OSASMF(   .align 4                              );
    OSASMF(   .globl Core_ID_1_Startup              );
#endif
    OSASMF(Core_ID_1_Startup:                       );

    /* !!! Note OS2_MAIN_STACK_MAX cannot be used if it is startup for non-autosar core */
    OSASMF2(    e_lis     r1, OS2_MAIN_STACK_MAX@ha    );
    OSASMF3(    e_add16i  r1, r1, OS2_MAIN_STACK_MAX@l );
    OSASMF3(    e_add16i  r1, r1, -0x8              );
    
    OSASMF2(    e_lis     r2, _SDA2_BASE_@ha        );
    OSASMF3(    e_add16i  r2, r2, _SDA2_BASE_@l     );
    OSASMF2(    e_lis     r13, _SDA_BASE_@ha        );
    OSASMF3(    e_add16i  r13, r13, _SDA_BASE_@l    );

    OSASMF(     e_b       main                      );
#if defined(OSCWPPC)
}
#endif

#if defined(OSCWPPC)

#pragma function_align 4

OSASM void Core_ID_2_Startup( void )
{
    nofralloc
        
#else
    OSASMF(   .align 4                              );
    OSASMF(   .globl Core_ID_2_Startup              );
#endif
    OSASMF(Core_ID_2_Startup:                       );

    /* !!! Note OS2_MAIN_STACK_MAX cannot be used if it is startup for non-autosar core */
    OSASMF2(    e_lis     r1, OS2_MAIN_STACK_MAX@ha    );
    OSASMF3(    e_add16i  r1, r1, OS2_MAIN_STACK_MAX@l );
    OSASMF3(    e_add16i  r1, r1, -0x8              );
    
    OSASMF2(    e_lis     r2, _SDA2_BASE_@ha        );
    OSASMF3(    e_add16i  r2, r2, _SDA2_BASE_@l     );
    OSASMF2(    e_lis     r13, _SDA_BASE_@ha        );
    OSASMF3(    e_add16i  r13, r13, _SDA_BASE_@l    );

    OSASMF(     e_b       main                      );
#if defined(OSCWPPC)
}
#endif


#define OSSTARTUP2_STOP_SEC_CODE
#include <MemMap.h>

#endif /* !defined(OS_MULTI_INSTANCE) */

