1/**************************************************
2 *
3 * System initialization.
4 *
5 * Copyright 2019 IAR Systems AB.
6 *
7 **************************************************/
8
9#include "iarMacros.m"
10#include "iarCfi.m"
11
12        MODULE  ?cstartup
13
14        PUBLIC  __iar_program_start
15        PUBLIC  __iar_program_start_metal
16
17        SECTION CSTACK:DATA:NOROOT(2)
18
19        // --------------------------------------------------
20
21        SECTION `.cstartup`:CODE:NOROOT(2)
22        CODE
23
24        CfiCom  ra,1,0
25        CfiCom  ra,1,1
26        CfiCom  ra,1,2
27        CfiCom  ra,1,3
28
29__iar_program_start:
30        REQUIRE ?cstart_init_sp
31
32// ----------
33//Init gp (required in by the linker config file, if applicable)
34        SECTION `.cstartup`:CODE:NOROOT(1)
35        CfiBlk 1,__iar_program_start
36        CALL_GRAPH_ROOT __iar_program_start, "Reset"
37        CODE
38        PUBLIC __iar_cstart_init_gp
39__iar_cstart_init_gp:
40        cfi ?RET Undefined
41        EXTERN  __iar_static_base$$GPREL
42        .option push
43        .option norelax
44        lui  gp, %hi(__iar_static_base$$GPREL)
45        addi gp, gp, %lo(__iar_static_base$$GPREL)
46        .option pop
47
48        CfiEnd  1
49
50// ----------
51// Init sp, note that this MAY be gp relaxed! (since if gp relaxations are
52// allowed, __iar_cstart_init_gp is already done
53        SECTION `.cstartup`:CODE:NOROOT(1)
54        CfiBlk 2,__iar_program_start
55        CODE
56?cstart_init_sp:
57        cfi ?RET Undefined
58        lui     sp, %hi(SFE(CSTACK))
59        addi    sp, sp, %lo(SFE(CSTACK))
60
61        // Setup up a default interrupt handler to handle any exceptions that
62        // might occur during startup
63        EXTERN __iar_default_minterrupt_handler
64        lui     a0, %hi(__iar_default_minterrupt_handler)
65        addi    a0, a0, %lo(__iar_default_minterrupt_handler)
66        csrrci  x0, mtvec, 0x3
67        csrs    mtvec, a0
68
69
70        EXTWEAK __machine_interrupt_vector_setup
71        CfiCall __machine_interrupt_vector_setup
72        call    __machine_interrupt_vector_setup
73
74__iar_program_start_metal:
75#ifdef __riscv_flen
76        // Enable the floating-point unit by setting the "fs" field in
77        // the "mstatus" register.
78
79        lui     a0, %hi(1 << 13)
80        csrs    mstatus, a0
81
82        // Set rounding mode to "round to nearest" and clear
83        // the floating-point exception flags.
84        csrwi   fcsr, 0
85#endif
86
87        EXTERN  __low_level_init
88        EXTERN  __iar_data_init2
89
90        CfiCall __low_level_init
91        call    __low_level_init
92        beq     a0, zero, ?cstart_call_main
93
94        CfiCall __iar_data_init2
95        call    __iar_data_init2
96
97?cstart_call_main:
98        EXTERN  SystemInit
99        //li12    a0, 0                   ; argc
100        CfiCall SystemInit
101        call    SystemInit
102
103        EXTERN  main
104        li12    a0, 0                   ; argc
105        CfiCall main
106        call    main
107        EXTERN  exit
108
109        CfiCall exit
110        call    exit
111?cstart_end:
112        j       ?cstart_end
113        CfiEnd  2
114
115
116        /* This section is required by some devices to handle HW reset */
117        SECTION `.alias.hwreset`:CODE:NOROOT(2)
118        PUBLIC __alias_hw_reset
119__alias_hw_reset:
120        csrci        mstatus, 0x08
121        lui     a0, %hi(__iar_program_start)
122        addi    a0, a0, %lo(__iar_program_start)
123        jr       a0
124
125        END
126
127        End
128
129