Root/lm32/logic/sakc/firmware/gdb-test/crt0gdb.S

1/*
2 * LatticeMico32 GDB 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
28        .section .boot, "ax", @progbits
29        
30        .global _start
31_start:
32_reset_handler:
33        bi _crt0
34        nop
35        nop
36        nop
37        nop
38        nop
39        nop
40        nop
41
42_breakpoint_handler:
43        sw (sp+0), ra
44        calli _save_all
45        sw (sp+128), ba
46        mvi r1, 1
47        sw (sp+132), r1
48        mv r1, sp
49        calli _handle_exception
50        bi _b_restore_all_and_return
51
52_instruction_bus_error_handler:
53        sw (sp+0), ra
54        calli _save_all
55        sw (sp+128), ea
56        mvi r1, 2
57        sw (sp+132), r1
58        mv r1, sp
59        calli _handle_exception
60        bi _e_restore_all_and_return
61        
62_watchpoint_handler:
63        sw (sp+0), ra
64        calli _save_all
65        sw (sp+128), ba
66        mvi r1, 3
67        sw (sp+132), r1
68        mv r1, sp
69        calli _handle_exception
70        bi _b_restore_all_and_return
71
72_data_bus_error_handler:
73        sw (sp+0), ra
74        calli _save_all
75        sw (sp+128), ea
76        mvi r1, 4
77        sw (sp+132), r1
78        mv r1, sp
79        calli _handle_exception
80        bi _e_restore_all_and_return
81        
82_divide_by_zero_handler:
83        sw (sp+0), ra
84        calli _save_all
85        sw (sp+128), ea
86        mvi r1, 5
87        sw (sp+132), r1
88        mv r1, sp
89        calli _handle_exception
90        bi _e_restore_all_and_return
91
92_interrupt_handler:
93        sw (sp+0), ra
94        calli _save_all
95        sw (sp+128), ea
96        mvi r1, 6
97        sw (sp+132), r1
98        mv r1, sp
99        calli _handle_exception
100        bi _e_restore_all_and_return
101        
102_system_call_handler:
103        sw (sp+0), ra
104        calli _save_all
105        sw (sp+128), ea
106        mvi r1, 7
107        sw (sp+132), r1
108        mv r1, sp
109        calli _handle_exception
110        bi _e_restore_all_and_return
111        
112        /* Program starts here */
113        
114_crt0:
115        /* Clear r0 */
116        xor r0, r0, r0
117        
118        /* Clear all other regs (needed so that simulations match) */
119        mvi r1, 0
120        mvi r2, 0
121        mvi r3, 0
122        mvi r4, 0
123        mvi r5, 0
124        mvi r6, 0
125        mvi r7, 0
126        mvi r8, 0
127        mvi r9, 0
128        mvi r10, 0
129        mvi r11, 0
130        mvi r12, 0
131        mvi r13, 0
132        mvi r14, 0
133        mvi r15, 0
134        mvi r16, 0
135        mvi r17, 0
136        mvi r18, 0
137        mvi r19, 0
138        mvi r20, 0
139        mvi r21, 0
140        mvi r22, 0
141        mvi r23, 0
142        mvi r24, 0
143        mvi r25, 0
144        mvi r26, 0
145        mvi r27, 0
146        mvi r28, 0
147        mvi r29, 0
148        mvi r30, 0
149        mvi r31, 0
150
151        /* Setup stack and global pointer */
152        mvhi sp, hi(_fstack)
153        ori sp, sp, lo(_fstack)
154        mvhi gp, hi(_gp)
155        ori gp, gp, lo(_gp)
156                        
157        /* Clear BSS */
158        mvhi r1, hi(_fbss)
159        ori r1, r1, lo(_fbss)
160        mvhi r3, hi(_ebss)
161        ori r3, r3, lo(_ebss)
162.clearBSS:
163        be r1, r3, .callMain
164        sw (r1+0), r0
165        addi r1, r1, 4
166        bi .clearBSS
167
168.callMain:
169        mvi r1, 0
170        mvi r2, 0
171        mvi r3, 0
172        /* Enter debug exception handler */
173        mvhi ba, hi(main)
174        ori ba, ba, lo(main)
175        bi _breakpoint_handler
176                          
177        /* Save all registers onto the stack */
178_save_all:
179        addi sp, sp, -136
180        sw (sp+0), r0
181        sw (sp+4), r1
182        sw (sp+8), r2
183        sw (sp+12), r3
184        sw (sp+16), r4
185        sw (sp+20), r5
186        sw (sp+24), r6
187        sw (sp+28), r7
188        sw (sp+32), r8
189        sw (sp+36), r9
190        sw (sp+40), r10
191        sw (sp+44), r11
192        sw (sp+48), r12
193        sw (sp+52), r13
194        sw (sp+56), r14
195        sw (sp+60), r15
196        sw (sp+64), r16
197        sw (sp+68), r17
198        sw (sp+72), r18
199        sw (sp+76), r19
200        sw (sp+80), r20
201        sw (sp+84), r21
202        sw (sp+88), r22
203        sw (sp+92), r23
204        sw (sp+96), r24
205        sw (sp+100), r25
206        sw (sp+104), r26
207        sw (sp+108), r27
208        sw (sp+120), ea
209        sw (sp+124), ba
210        /* ra and sp need special handling, as they have been modified */
211        lw r1, (sp+136)
212        sw (sp+116), r1
213        mv r1, sp
214        addi r1, r1, 136
215        sw (sp+112), r1
216        ret
217        
218        /* Restore all registers and return from exception */
219_e_restore_all_and_return:
220        lw r1, (sp+4)
221        lw r2, (sp+8)
222        lw r3, (sp+12)
223        lw r4, (sp+16)
224        lw r5, (sp+20)
225        lw r6, (sp+24)
226        lw r7, (sp+28)
227        lw r8, (sp+32)
228        lw r9, (sp+36)
229        lw r10, (sp+40)
230        lw r11, (sp+44)
231        lw r12, (sp+48)
232        lw r13, (sp+52)
233        lw r14, (sp+56)
234        lw r15, (sp+60)
235        lw r16, (sp+64)
236        lw r17, (sp+68)
237        lw r18, (sp+72)
238        lw r19, (sp+76)
239        lw r20, (sp+80)
240        lw r21, (sp+84)
241        lw r22, (sp+88)
242        lw r23, (sp+92)
243        lw r24, (sp+96)
244        lw r25, (sp+100)
245        lw r26, (sp+104)
246        lw r27, (sp+108)
247        lw ra, (sp+116)
248        lw ba, (sp+124)
249        /* Restore EA from PC */
250        lw ea, (sp+128)
251        /* Stack pointer must be restored last, in case it has been updated */
252        lw sp, (sp+112)
253        eret
254        
255        /* Restore all registers and return from breakpoint */
256_b_restore_all_and_return:
257        lw r1, (sp+4)
258        lw r2, (sp+8)
259        lw r3, (sp+12)
260        lw r4, (sp+16)
261        lw r5, (sp+20)
262        lw r6, (sp+24)
263        lw r7, (sp+28)
264        lw r8, (sp+32)
265        lw r9, (sp+36)
266        lw r10, (sp+40)
267        lw r11, (sp+44)
268        lw r12, (sp+48)
269        lw r13, (sp+52)
270        lw r14, (sp+56)
271        lw r15, (sp+60)
272        lw r16, (sp+64)
273        lw r17, (sp+68)
274        lw r18, (sp+72)
275        lw r19, (sp+76)
276        lw r20, (sp+80)
277        lw r21, (sp+84)
278        lw r22, (sp+88)
279        lw r23, (sp+92)
280        lw r24, (sp+96)
281        lw r25, (sp+100)
282        lw r26, (sp+104)
283        lw r27, (sp+108)
284        lw ra, (sp+116)
285        lw ea, (sp+120)
286        /* Restore BA from PC */
287        lw ba, (sp+128)
288        /* Stack pointer must be restored last, in case it has been updated */
289        lw sp, (sp+112)
290        bret
291

Archive Download this file

Branches:
master



interactive