/**************************************************
 *
 * Part one of the system initialization code, contains low-level
 * initialization, plain thumb variant.
 *
 * Copyright 2010 IAR Systems. All rights reserved.
 *
 * $Revision: 34539 $
 *
 **************************************************/

;
; The modules in this file are included in the libraries, and may be replaced
; by any user-defined modules that define the PUBLIC symbol _program_start or
; a user defined start symbol.
; To override the cstartup defined in the library, simply add your modified
; version to the workbench project.
;
; The vector table is normally located at address 0.
; When debugging in RAM, it can be located in RAM, aligned to at least 2^6.
; The name "__vector_table" has special meaning for C-SPY:
; it is where the SP start value is found, and the NVIC vector
; table register (VTOR) is initialized to this address if != 0.
;
; Cortex-M version
;

        MODULE  ?cstartup

        ;; Forward declaration of sections.
        SECTION CSTACK:DATA:NOROOT(3)

        SECTION .intvec:CODE:NOROOT(2)

        EXTERN  __iar_program_start
;        EXTERN  SystemInit
        PUBLIC  __vector_table

        DATA
__vector_table
                DCD     sfe(CSTACK)               ; Top of Stack
                DCD     Reset_Handler       	  ; Reset Handler
                DCD     NMI_Handler               ; NMI Handler
                DCD     HardFault_Handler         ; Hard Fault Handler
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     SVC_Handler               ; SVCall Handler
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     PendSV_Handler            ; PendSV Handler
                DCD     SysTick_Handler           ; SysTick Handler

                ; External Interrupts
                                                  ; maximum of 32 External Interrupts are possible
                DCD		WDT_IRQHandler	
				DCD		APU_IRQHandler	
				DCD		ADC_IRQHandler
				DCD		EXINT_IRQHandler
				DCD		USBD_IRQHandler 
				DCD		TMR0_IRQHandler
				DCD		TMR1_IRQHandler	
				DCD		TMR2_IRQHandler	
				DCD		GPAB_IRQHandler	
				DCD		SPI0_IRQHandler	
				DCD		PWM0_IRQHandler	
				DCD		SPI1_IRQHandler	
				DCD		TMRF_IRQHandler
				DCD		RTC_IRQHandler
				DCD		PWRWU_IRQHandler


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Default interrupt handlers.
;;
      PUBWEAK Reset_Handler
      PUBWEAK NMI_Handler
      PUBWEAK HardFault_Handler
      PUBWEAK SVC_Handler
      PUBWEAK PendSV_Handler
      PUBWEAK SysTick_Handler
      PUBWEAK WDT_IRQHandler
      PUBWEAK APU_IRQHandler
      PUBWEAK ADC_IRQHandler
      PUBWEAK EXINT_IRQHandler
      PUBWEAK USBD_IRQHandler
      PUBWEAK TMR0_IRQHandler
      PUBWEAK TMR1_IRQHandler
      PUBWEAK TMR2_IRQHandler
      PUBWEAK GPAB_IRQHandler
      PUBWEAK SPI0_IRQHandler
      PUBWEAK PWM0_IRQHandler
      PUBWEAK SPI1_IRQHandler
      PUBWEAK TMRF_IRQHandler
      PUBWEAK RTC_IRQHandler
      PUBWEAK PWRWU_IRQHandler
	
        THUMB
        SECTION .text:CODE:REORDER(2)
Reset_Handler
;                LDR     R0, =SystemInit
;                BLX     R0
;*************Add by Nuvoton***************

;******************************************
               LDR     R0, =__iar_program_start
               BX      R0
              SECTION .text:CODE:REORDER(2)
NMI_Handler
HardFault_Handler
        LDR    R0, [R13, #24]        ; Get previous PC
        LDRH   R1, [R0]              ; Get instruction
        LDR    R2, =0xBEAB           ; The sepcial BKPT instruction
        CMP    R1, R2                ; Test if the instruction at previous PC is BKPT
        BNE    HardFault_Handler_Ret ; Not BKPT

        ADDS   R0, #4                ; Skip BKPT and next line
        STR    R0, [R13, #24]        ; Save previous PC

        BX     LR
HardFault_Handler_Ret
        B      .

SVC_Handler
PendSV_Handler
SysTick_Handler
WDT_IRQHandler	
APU_IRQHandler	
ADC_IRQHandler
EXINT_IRQHandler
USBD_IRQHandler 
TMR0_IRQHandler
TMR1_IRQHandler	
TMR2_IRQHandler	
GPAB_IRQHandler	
SPI0_IRQHandler	
PWM0_IRQHandler	
SPI1_IRQHandler	
TMRF_IRQHandler
RTC_IRQHandler
PWRWU_IRQHandler
Default_Handler
        B Default_Handler
        END
