e0cd7153
[litex.git] /
1 /*
2 * LatticeMico32 C startup code.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 /* Exception handlers - Must be 32 bytes long. */
27 .section .text, "ax", @progbits
28 .global _start
29 _start:
30 _reset_handler:
31 xor r0, r0, r0
32 wcsr IE, r0
33 mvhi r1, hi(_reset_handler)
34 ori r1, r1, lo(_reset_handler)
35 wcsr EBA, r1
36 bi _crt0
37 nop
38 nop
39
40 _breakpoint_handler:
41 bi _breakpoint_handler
42 nop
43 nop
44 nop
45 nop
46 nop
47 nop
48 nop
49
50 _instruction_bus_error_handler:
51 bi _instruction_bus_error_handler
52 nop
53 nop
54 nop
55 nop
56 nop
57 nop
58 nop
59
60 _watchpoint_hander:
61 bi _watchpoint_hander
62 nop
63 nop
64 nop
65 nop
66 nop
67 nop
68 nop
69
70 _data_bus_error_handler:
71 bi _data_bus_error_handler
72 nop
73 nop
74 nop
75 nop
76 nop
77 nop
78 nop
79
80 _divide_by_zero_handler:
81 bi _divide_by_zero_handler
82 nop
83 nop
84 nop
85 nop
86 nop
87 nop
88 nop
89
90 _interrupt_handler:
91 sw (sp+0), ra
92 calli .save_all
93 calli isr
94 bi .restore_all_and_eret
95 nop
96 nop
97 nop
98 nop
99
100 _syscall_handler:
101 bi _syscall_handler
102 nop
103 nop
104 nop
105 nop
106 nop
107 nop
108 nop
109
110 _crt0:
111 /* Setup stack and global pointer */
112 mvhi sp, hi(_fstack)
113 ori sp, sp, lo(_fstack)
114
115 /* Clear BSS */
116 mvhi r1, hi(_fbss)
117 ori r1, r1, lo(_fbss)
118 mvhi r3, hi(_ebss)
119 ori r3, r3, lo(_ebss)
120 .clearBSS:
121 be r1, r3, .callMain
122 sw (r1+0), r0
123 addi r1, r1, 4
124 bi .clearBSS
125
126 .callMain:
127 bi main
128
129 .save_all:
130 addi sp, sp, -56
131 sw (sp+4), r1
132 sw (sp+8), r2
133 sw (sp+12), r3
134 sw (sp+16), r4
135 sw (sp+20), r5
136 sw (sp+24), r6
137 sw (sp+28), r7
138 sw (sp+32), r8
139 sw (sp+36), r9
140 sw (sp+40), r10
141 sw (sp+48), ea
142 sw (sp+52), ba
143 /* ra needs to be moved from initial stack location */
144 lw r1, (sp+56)
145 sw (sp+44), r1
146 ret
147
148 .restore_all_and_eret:
149 lw r1, (sp+4)
150 lw r2, (sp+8)
151 lw r3, (sp+12)
152 lw r4, (sp+16)
153 lw r5, (sp+20)
154 lw r6, (sp+24)
155 lw r7, (sp+28)
156 lw r8, (sp+32)
157 lw r9, (sp+36)
158 lw r10, (sp+40)
159 lw ra, (sp+44)
160 lw ea, (sp+48)
161 lw ba, (sp+52)
162 addi sp, sp, 56
163 eret