.syntax unified
.cpu cortex-m0plus
.thumb

/* ----------------------------------------------------------------------------
   外部符号声明 (必须与 ld 文件中的符号完全一致)
   ---------------------------------------------------------------------------- */
.extern _estack
.extern _flashappdata, _ramappdata, _edata                /* Data段 */
.extern _flashramcode, _ramcode, _eramcode                /* RAM代码 */
.extern _flashappdatanocache, _ramappdatanocache, _edata_nc /* 非缓存Data */
.extern _sbss, _ebss                                      /* BSS */
.extern _sbss_nc, _ebss_nc                                /* 非缓存BSS */
.extern SystemInit
.extern main
.type main, %function

/* ----------------------------------------------------------------------------
   向量表：
   由 autosar_gcc_flash_*.ld 的 AppFlashBoot 固定在 Flash 0x08000000（VTOR=FMC_BASE）。
   ---------------------------------------------------------------------------- */
.section .intc_vector, "a", %progbits
.align 4
.global VTABLE
VTABLE:
    .word _estack
    /* Cortex-M 向量表项须为 Thumb 地址 (LSB=1) */
    .word (Reset_Handler + 1)
    .word (NMI_Handler + 1)
    .word (HardFault_Handler + 1)
    .word 0, 0, 0, 0, 0, 0, 0 /* Reserved */
    .word (SVC_Handler + 1)
    .word 0, 0                /* Reserved */
    .word (PendSV_Handler + 1)
    .word (SysTick_Handler + 1)

    /* 外部中断 (External Interrupts) */
    .word 0                                 /* IRQ0: Reserved */
    .word (PVD_IRQHandler + 1)              /* IRQ1: PVD */
    .word (RTC_IRQHandler + 1)              /* IRQ2: RTC */
    .word (FLASH_IRQHandler + 1)            /* IRQ3: FLASH */
    .word (RCM_IRQHandler + 1)              /* IRQ4: RCM */
    .word (EINT0_1_IRQHandler + 1)          /* IRQ5: EINT 0-1 */
    .word (EINT2_3_IRQHandler + 1)          /* IRQ6: EINT 2-3 */
    .word (EINT4_15_IRQHandler + 1)         /* IRQ7: EINT 4-15 */
    .word (SERM_IRQHandler + 1)             /* IRQ8: ERP */
    .word (DMA_CH1_IRQHandler + 1)          /* IRQ9: DMA Channel 1 */
    .word (DMA_CH2_3_IRQHandler + 1)        /* IRQ10: DMA Channel 2-3 */
    .word (DMA_CH4_5_IRQHandler + 1)       /* IRQ11: DMA Channel 4-5 */
    .word (ADC_IRQHandler + 1)             /* IRQ12: ADC1 */
    .word (TMR1_BRK_UP_TRG_COM_IRQHandler + 1) /* IRQ13 */
    .word (TMR1_CC_IRQHandler + 1)         /* IRQ14 */
    .word (TMR2_IRQHandler + 1)            /* IRQ15 */
    .word (TMR3_IRQHandler + 1)            /* IRQ16 */
    .word (TMR6_IRQHandler + 1)            /* IRQ17 */
    .word (TMR7_IRQHandler + 1)            /* IRQ18 */
    .word (TMR4_IRQHandler + 1)            /* IRQ19 */
    .word 0                                 /* IRQ20: Reserved */
    .word (SHA256_IRQHandler + 1)         /* IRQ21 */
    .word (AES256_IRQHandler + 1)         /* IRQ22 */
    .word (FDCAN_IT0_IRQHandler + 1)       /* IRQ23 */
    .word (FDCAN_IT1_IRQHandler + 1)       /* IRQ24 */
    .word (SPI_IRQHandler + 1)             /* IRQ25 */
    .word 0                                 /* IRQ26: Reserved */
    .word (USART1_IRQHandler + 1)          /* IRQ27 */
    .word (USART2_IRQHandler + 1)        /* IRQ28 */
    .word (RNG_IRQHandler + 1)            /* IRQ29 */
    .word (TMR8_IRQHandler + 1)            /* IRQ30 */
    .word (FDCAN_SERM_IRQHandler + 1)     /* IRQ31 */

/* ----------------------------------------------------------------------------
   复位处理程序 SP → SystemInit → 搬运/清 BSS → main
   ---------------------------------------------------------------------------- */
.section .text.Reset_Handler, "ax", %progbits
.global Reset_Handler
.thumb_func
.type Reset_Handler, %function

Reset_Handler:
    cpsid i

    ldr   r0, =_estack
    mov   sp, r0

    bl    SystemInit
    dsb   sy
    isb   sy

    ldr   r0, =_flashappdata
    ldr   r1, =_ramappdata
    ldr   r2, =_edata
    bl    .L_copy_loop

    ldr   r0, =_flashramcode
    ldr   r1, =_ramcode
    ldr   r2, =_eramcode
    bl    .L_copy_loop

    ldr   r0, =_flashappdatanocache
    ldr   r1, =_ramappdatanocache
    ldr   r2, =_edata_nc
    bl    .L_copy_loop

    ldr   r1, =_sbss
    ldr   r2, =_ebss
    bl    .L_zero_loop

    ldr   r1, =_sbss_nc
    ldr   r2, =_ebss_nc
    bl    .L_zero_loop

    cpsie i
    bl    main

.thumb_func
.L_copy_loop:
    cmp   r1, r2
    bhs   .L_done
    ldr   r3, [r0]
    str   r3, [r1]
    adds  r0, r0, #4
    adds  r1, r1, #4
    b     .L_copy_loop

.thumb_func
.L_zero_loop:
    movs  r3, #0
.L_zero_inner:
    cmp   r1, r2
    bhs   .L_done
    str   r3, [r1]
    adds  r1, r1, #4
    b     .L_zero_inner

.L_done:
    bx    lr

.size Reset_Handler, . - Reset_Handler

/* ----------------------------------------------------------------------------
   默认中断处理程序 (Default_Handler)
   ---------------------------------------------------------------------------- */
.section .text.Default_Handler, "ax", %progbits
.thumb_func
.type Default_Handler, %function
Default_Handler:
    b .
.size Default_Handler, . - Default_Handler

.macro IRQ_DEF name
    .weak \name
    .thumb_set \name, Default_Handler
.endm

IRQ_DEF NMI_Handler
IRQ_DEF HardFault_Handler
IRQ_DEF SVC_Handler
IRQ_DEF PendSV_Handler
IRQ_DEF SysTick_Handler

IRQ_DEF PVD_IRQHandler
IRQ_DEF RTC_IRQHandler
IRQ_DEF FLASH_IRQHandler
IRQ_DEF RCM_IRQHandler
IRQ_DEF EINT0_1_IRQHandler
IRQ_DEF EINT2_3_IRQHandler
IRQ_DEF EINT4_15_IRQHandler
IRQ_DEF SERM_IRQHandler
IRQ_DEF DMA_CH1_IRQHandler
IRQ_DEF DMA_CH2_3_IRQHandler
IRQ_DEF DMA_CH4_5_IRQHandler
IRQ_DEF ADC_IRQHandler
IRQ_DEF TMR1_BRK_UP_TRG_COM_IRQHandler
IRQ_DEF TMR1_CC_IRQHandler
IRQ_DEF TMR2_IRQHandler
IRQ_DEF TMR3_IRQHandler
IRQ_DEF TMR6_IRQHandler
IRQ_DEF TMR7_IRQHandler
IRQ_DEF TMR4_IRQHandler
IRQ_DEF SHA256_IRQHandler
IRQ_DEF AES256_IRQHandler
IRQ_DEF FDCAN_IT0_IRQHandler
IRQ_DEF FDCAN_IT1_IRQHandler
IRQ_DEF SPI_IRQHandler
IRQ_DEF USART1_IRQHandler
IRQ_DEF USART2_IRQHandler
IRQ_DEF RNG_IRQHandler
IRQ_DEF TMR8_IRQHandler
IRQ_DEF FDCAN_SERM_IRQHandler

/* g32a1085.h CMSIS 名与启动符号别名 */
.weak   TRNG_IRQHandler
.thumb_set TRNG_IRQHandler, RNG_IRQHandler
.weak   SMS_IRQHandler
.thumb_set SMS_IRQHandler, SERM_IRQHandler
.weak   CAN_IT0_IRQHandler
.thumb_set CAN_IT0_IRQHandler, FDCAN_IT0_IRQHandler
.weak   CAN_IT1_IRQHandler
.thumb_set CAN_IT1_IRQHandler, FDCAN_IT1_IRQHandler
.weak   CAN_SMS_IT_IRQHandler
.thumb_set CAN_SMS_IT_IRQHandler, FDCAN_SERM_IRQHandler
