/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

// Amount of memory (in bytes) allocated for Stack
// Tailor this value to your application needs
// <h> Stack Configuration
//   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
// </h>

    .file "startup_ac7840x.s"
    .arch armv7e-m
    .text

    .thumb

    .section .isr_vector
    .align 2
    .globl __Vectors
__Vectors:
    .long   __StackTop                                      // Top of Stack
    .long   Reset_Handler                                   // Reset Handler
    .long   NMI_Handler                                     // NMI Handler
    .long   HardFault_Handler                               // Hard Fault Handler
    .long   MemManage_Handler                               // MPU Fault Handler
    .long   BusFault_Handler                                // Bus Fault Handler
    .long   UsageFault_Handler                              // Usage Fault Handler
    .long   0                                               // Reserved
    .long   0                                               // Reserved
    .long   0                                               // Reserved
    .long   0                                               // Reserved
    .long   SVC_Handler                                     // SVCall Handler
    .long   DebugMon_Handler                                // Debug Monitor Handler
    .long   0                                               // Reserved
    .long   PendSV_Handler                                  // PendSV Handler
    .long   SysTick_Handler                                 // SysTick Handler

// External interrupts
    .long     DMA0_Channel0_IRQHandler            //DMA channel 0 transfer complete
    .long     DMA0_Channel1_IRQHandler            //DMA channel 1 transfer complete
    .long     DMA0_Channel2_IRQHandler            //DMA channel 2 transfer complete
    .long     DMA0_Channel3_IRQHandler            //DMA channel 3 transfer complete
    .long     DMA0_Channel4_IRQHandler            //DMA channel 4 transfer complete
    .long     DMA0_Channel5_IRQHandler            //DMA channel 5 transfer complete
    .long     DMA0_Channel6_IRQHandler            //DMA channel 6 transfer complete
    .long     DMA0_Channel7_IRQHandler            //DMA channel 7 transfer complete
    .long     DMA0_Channel8_IRQHandler            //DMA channel 8 transfer complete
    .long     DMA0_Channel9_IRQHandler            //DMA channel 9 transfer complete
    .long     DMA0_Channel10_IRQHandler           //DMA channel 10 transfer complete
    .long     DMA0_Channel11_IRQHandler           //DMA channel 11 transfer complete
    .long     DMA0_Channel12_IRQHandler           //DMA channel 12 transfer complete
    .long     DMA0_Channel13_IRQHandler           //DMA channel 13 transfer complete
    .long     DMA0_Channel14_IRQHandler           //DMA channel 14 transfer complete
    .long     DMA0_Channel15_IRQHandler           //DMA channel 15 transfer complete
    .long     PORTA_IRQHandler                    //Port A pin detect interruptInterrupt
    .long     PORTB_IRQHandler                    //Port B pin detect interrupt
    .long     PORTC_IRQHandler                    //Port C pin detect interrupt
    .long     PORTD_IRQHandler                    //Port D pin detect interrupt
    .long     PORTE_IRQHandler                    //Port E pin detect interrupt
    .long     UART0_IRQHandler                    //UART0 Transmit / Receive Interrupt
    .long     UART1_IRQHandler                    //UART1 Transmit / Receive Interrupt
    .long     UART2_IRQHandler                    //UART2 Transmit / Receive Interrupt
    .long     UART3_IRQHandler                    //UART3 Transmit / Receive Interrupt
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     SPI0_IRQHandler                     //SPI0 Interrupt
    .long     SPI1_IRQHandler                     //SPI1 Interrupt
    .long     SPI2_IRQHandler                     //SPI2 Interrupt
    .long     0                                   //Reserved
    .long     I2C0_IRQHandler                     //I2C0 Interrupt
    .long     0                                   //Reserved
    .long     EIO_IRQHandler                      //EIO Interrupt
    .long     CAN0_IRQHandler                     //CAN0 Interrupt
    .long     CAN0_Wakeup_IRQHandler              //CAN0 Wakeup Interrupt
    .long     CAN1_IRQHandler                     //CAN1 Interrupt
    .long     CAN1_Wakeup_IRQHandler              //CAN1 Wakeup Interrupt
    .long     CAN2_IRQHandler                     //CAN2 Interrupt
    .long     CAN2_Wakeup_IRQHandler              //CAN2 Wakeup Interrupt
    .long     CAN3_IRQHandler                     //CAN3 Interrupt
    .long     CAN3_Wakeup_IRQHandler              //CAN3 Wakeup Interrupt
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     PDT0_IRQHandler                     //PDT0 Interrupt
    .long     PDT1_IRQHandler                     //PDT1 Interrupt
    .long     ADC0_IRQHandler                     //ADC0 Interrupt
    .long     ADC1_IRQHandler                     //ADC1 Interrupt
    .long     ACMP0_IRQHandler                    //ACMP0 Interrupt
    .long     WDG_IRQHandler                      //WDG Interrupt
    .long     EWDG_IRQHandler                     //EWDG Interrupt
    .long     MCM_IRQHandler                      //MCM Interrupt
    .long     LVD_IRQHandler                      //LVD Interrupt
    .long     SPM_IRQHandler                      //SPM Interrupt
    .long     RCM_IRQHandler                      //RCM Interrupt
    .long     PWM0_Overflow_IRQHandler            //PWM0 Overflow Interrupt
    .long     PWM0_Channel_IRQHandler             //PWM0 Channel Interrupt
    .long     PWM0_Fault_IRQHandler               //PWM0 Fault Interrupt
    .long     PWM1_Overflow_IRQHandler            //PWM1 Overflow Interrupt
    .long     PWM1_Channel_IRQHandler             //PWM1 Channel Interrupt
    .long     PWM1_Fault_IRQHandler               //PWM1 Fault Interrupt
    .long     PWM2_Overflow_IRQHandler            //PWM2 Overflow Interrupt
    .long     PWM2_Channel_IRQHandler             //PWM2 Channel Interrupt
    .long     PWM2_Fault_IRQHandler               //PWM2 Fault Interrupt
    .long     PWM3_Overflow_IRQHandler            //PWM3 Overflow Interrupt
    .long     PWM3_Channel_IRQHandler             //PWM3 Channel Interrupt
    .long     PWM3_Fault_IRQHandler               //PWM3 Fault Interrupt
    .long     PWM4_Overflow_IRQHandler            //PWM4 Overflow Interrupt
    .long     PWM4_Channel_IRQHandler             //PWM4 Channel Interrupt
    .long     PWM4_Fault_IRQHandler               //PWM4 Fault Interrupt
    .long     PWM5_Overflow_IRQHandler            //PWM5 Overflow Interrupt
    .long     PWM5_Channel_IRQHandler             //PWM5 Channel Interrupt
    .long     PWM5_Fault_IRQHandler               //PWM5 Fault Interrupt
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     0                                   //Reserved
    .long     RTC_IRQHandler                      //RTC Interrupt
    .long     PCT_IRQHandler                      //PCT Interrupt
    .long     TIMER_Channel0_IRQHandler           //TIMER Channel0 Interrupt
    .long     TIMER_Channel1_IRQHandler           //TIMER Channel1 Interrupt
    .long     TIMER_Channel2_IRQHandler           //TIMER Channel2 Interrupt
    .long     TIMER_Channel3_IRQHandler           //TIMER Channel3 Interrupt
    .long     CSE_IRQHandler                      //CSE Interrupt
    .long     FLASH_ECC_IRQHandler                //Flash ECC Interrupt
    .long     FLASH_IRQHandler                    //Flash Interrupt
    .long     FLASH_Collision_IRQHandler          //Flash Collision Interrupt
    .long     ECC_1BIT_IRQHandler                 //ECC 1BIT Interrupt
    .long     ECC_2BIT_IRQHandler                 //ECC 2BIT Interrupt

    .long     DefaultISR
    .size    __Vectors, . - __Vectors

    .text
    .thumb

// Reset Handler
    .thumb_func
    .align 2
    .globl   Reset_Handler
    .weak    Reset_Handler
    .type    Reset_Handler, %function
Reset_Handler:
    mrs     r10,primask     // Save PRIMASK
    cpsid   i               // Mask interrupts

    // Init the rest of the registers
    ldr     r1,=0
    ldr     r2,=0
    ldr     r3,=0
    ldr     r4,=0
    ldr     r5,=0
    ldr     r6,=0
    ldr     r7,=0
    mov     r8,r7
    mov     r9,r7
    mov     r10,r7
    mov     r11,r7
    mov     r12,r7

    // Initialize the stack pointer
    ldr     r1,=__StackTop
    mov     r13,r1

    // Init Main stack
    ldr     r0,=STACK_SIZE
    sub     r0, r1, r0
    mov     r2, #0
    mov     r3, r2
STACK_INIT_LOOP:
    stmia   r0!, {r2, r3}
    cmp     r0, r1
    blt     STACK_INIT_LOOP

    // Init ECC SRAM
    ldr     r0,=STACK_SIZE
    sub     r0, r1, r0
    ldr     r3,=InitSram
    blx     r3

    // Call the CMSIS system init routine
    ldr     r0,=SystemInit
    blx     r0

    // Init .data and .bss sections
    ldr     r0,=init_data_bss
    blx     r0
    msr     primask,r10     // Restore PRIMASK
    bl      main

    .pool
    .size Reset_Handler, . - Reset_Handler

    .align  1
    .thumb
    .thumb_func
    .weak DefaultISR
    .type DefaultISR, %function
DefaultISR:
    b       .
    .size DefaultISR, . - DefaultISR

/*    Macro to define default handlers. Default handler
 *    will be weak symbol and just dead loops. They can be
 *    overwritten by other handlers */
    .macro def_irq_handler    handler_name
    .weak \handler_name
    .set  \handler_name, DefaultISR
    .endm

// Exception Handlers
    def_irq_handler    NMI_Handler                         //NMI Handler
    def_irq_handler    HardFault_Handler                   //Hard Fault Handler
    def_irq_handler    MemManage_Handler                   //MPU Fault Handler
    def_irq_handler    BusFault_Handler                    //Bus Fault Handler
    def_irq_handler    UsageFault_Handler                  //Usage Fault Handler
    def_irq_handler    SVC_Handler                         //SVCall Handler
    def_irq_handler    DebugMon_Handler                    //Debug Monitor Handle
    def_irq_handler    PendSV_Handler                      //PendSV Handler
    def_irq_handler    SysTick_Handler                     //SysTick Handler
    def_irq_handler    DMA0_Channel0_IRQHandler            //DMA channel 0 transfer complete
    def_irq_handler    DMA0_Channel1_IRQHandler            //DMA channel 1 transfer complete
    def_irq_handler    DMA0_Channel2_IRQHandler            //DMA channel 2 transfer complete
    def_irq_handler    DMA0_Channel3_IRQHandler            //DMA channel 3 transfer complete
    def_irq_handler    DMA0_Channel4_IRQHandler            //DMA channel 4 transfer complete
    def_irq_handler    DMA0_Channel5_IRQHandler            //DMA channel 5 transfer complete
    def_irq_handler    DMA0_Channel6_IRQHandler            //DMA channel 6 transfer complete
    def_irq_handler    DMA0_Channel7_IRQHandler            //DMA channel 7 transfer complete
    def_irq_handler    DMA0_Channel8_IRQHandler            //DMA channel 8 transfer complete
    def_irq_handler    DMA0_Channel9_IRQHandler            //DMA channel 9 transfer complete
    def_irq_handler    DMA0_Channel10_IRQHandler           //DMA channel 10 transfer complete
    def_irq_handler    DMA0_Channel11_IRQHandler           //DMA channel 11 transfer complete
    def_irq_handler    DMA0_Channel12_IRQHandler           //DMA channel 12 transfer complete
    def_irq_handler    DMA0_Channel13_IRQHandler           //DMA channel 13 transfer complete
    def_irq_handler    DMA0_Channel14_IRQHandler           //DMA channel 14 transfer complete
    def_irq_handler    DMA0_Channel15_IRQHandler           //DMA channel 15 transfer complete
    def_irq_handler    PORTA_IRQHandler                    //Port A pin detect interrupt
    def_irq_handler    PORTB_IRQHandler                    //Port B pin detect interrupt
    def_irq_handler    PORTC_IRQHandler                    //Port C pin detect interrupt
    def_irq_handler    PORTD_IRQHandler                    //Port D pin detect interrupt
    def_irq_handler    PORTE_IRQHandler                    //Port E pin detect interrupt
    def_irq_handler    UART0_IRQHandler                    //UART0 Transmit / Receive Interrupt
    def_irq_handler    UART1_IRQHandler                    //UART1 Transmit / Receive Interrupt
    def_irq_handler    UART2_IRQHandler                    //UART2 Transmit / Receive Interrupt
    def_irq_handler    UART3_IRQHandler                    //UART3 Transmit / Receive Interrupt
    def_irq_handler    SPI0_IRQHandler                     //SPI0 Interrupt
    def_irq_handler    SPI1_IRQHandler                     //SPI1 Interrupt
    def_irq_handler    SPI2_IRQHandler                     //SPI2 Interrupt
    def_irq_handler    I2C0_IRQHandler                     //I2C0 Interrupt
    def_irq_handler    EIO_IRQHandler                      //EIO Interrupt
    def_irq_handler    CAN0_IRQHandler                     //CAN0 Interrupt
    def_irq_handler    CAN0_Wakeup_IRQHandler              //CAN0 Wakeup Interrupt
    def_irq_handler    CAN1_IRQHandler                     //CAN1 Interrupt
    def_irq_handler    CAN1_Wakeup_IRQHandler              //CAN1 Wakeup Interrupt
    def_irq_handler    CAN2_IRQHandler                     //CAN2 Interrupt
    def_irq_handler    CAN2_Wakeup_IRQHandler              //CAN2 Wakeup Interrupt
    def_irq_handler    CAN3_IRQHandler                     //CAN3 Interrupt
    def_irq_handler    CAN3_Wakeup_IRQHandler              //CAN3 Wakeup Interrupt
    def_irq_handler    PDT0_IRQHandler                     //PDT0 Interrupt
    def_irq_handler    PDT1_IRQHandler                     //PDT1 Interrupt
    def_irq_handler    ADC0_IRQHandler                     //ADC0 Interrupt
    def_irq_handler    ADC1_IRQHandler                     //ADC1 Interrupt
    def_irq_handler    ACMP0_IRQHandler                    //ACMP0 Interrupt
    def_irq_handler    WDG_IRQHandler                      //WDG Interrupt
    def_irq_handler    EWDG_IRQHandler                     //EWDG Interrupt
    def_irq_handler    MCM_IRQHandler                      //MCM Interrupt
    def_irq_handler    LVD_IRQHandler                      //LVD Interrupt
    def_irq_handler    SPM_IRQHandler                      //SPM Interrupt
    def_irq_handler    RCM_IRQHandler                      //RCM Interrupt
    def_irq_handler    PWM0_Overflow_IRQHandler            //PWM0 Overflow Interrupt
    def_irq_handler    PWM0_Channel_IRQHandler             //PWM0 Channel Interrupt
    def_irq_handler    PWM0_Fault_IRQHandler               //PWM0 Fault Interrupt
    def_irq_handler    PWM1_Overflow_IRQHandler            //PWM1 Overflow Interrupt
    def_irq_handler    PWM1_Channel_IRQHandler             //PWM1 Channel Interrupt
    def_irq_handler    PWM1_Fault_IRQHandler               //PWM1 Fault Interrupt
    def_irq_handler    PWM2_Overflow_IRQHandler            //PWM2 Overflow Interrupt
    def_irq_handler    PWM2_Channel_IRQHandler             //PWM2 Channel Interrupt
    def_irq_handler    PWM2_Fault_IRQHandler               //PWM2 Fault Interrupt
    def_irq_handler    PWM3_Overflow_IRQHandler            //PWM3 Overflow Interrupt
    def_irq_handler    PWM3_Channel_IRQHandler             //PWM3 Channel Interrupt
    def_irq_handler    PWM3_Fault_IRQHandler               //PWM3 Fault Interrupt
    def_irq_handler    PWM4_Overflow_IRQHandler            //PWM4 Overflow Interrupt
    def_irq_handler    PWM4_Channel_IRQHandler             //PWM4 Channel Interrupt
    def_irq_handler    PWM4_Fault_IRQHandler               //PWM4 Fault Interrupt
    def_irq_handler    PWM5_Overflow_IRQHandler            //PWM5 Overflow Interrupt
    def_irq_handler    PWM5_Channel_IRQHandler             //PWM5 Channel Interrupt
    def_irq_handler    PWM5_Fault_IRQHandler               //PWM5 Fault Interrupt
    def_irq_handler    RTC_IRQHandler                      //RTC Interrupt
    def_irq_handler    PCT_IRQHandler                      //PCT Interrupt
    def_irq_handler    TIMER_Channel0_IRQHandler           //TIMER Channel0 Interrupt
    def_irq_handler    TIMER_Channel1_IRQHandler           //TIMER Channel1 Interrupt
    def_irq_handler    TIMER_Channel2_IRQHandler           //TIMER Channel2 Interrupt
    def_irq_handler    TIMER_Channel3_IRQHandler           //TIMER Channel3 Interrupt
    def_irq_handler    CSE_IRQHandler                      //CSE Interrupt
    def_irq_handler    FLASH_ECC_IRQHandler                //Flash ECC Interrupt
    def_irq_handler    FLASH_IRQHandler                    //Flash Interrupt
    def_irq_handler    FLASH_Collision_IRQHandler          //Flash Collision Interrupt
    def_irq_handler    ECC_1BIT_IRQHandler                 //ECC 1BIT Interrupt
    def_irq_handler    ECC_2BIT_IRQHandler                 //ECC 2BIT Interrupt

    .end

