regnual: relocatable
[gnuk/gnuk.git] / regnual / sys.c
1 #include "types.h"
2
3 static void fatal (void)
4 {
5   for (;;);
6 }
7
8 static void none (void)
9 {
10 }
11
12 /*
13  * Note: the address of this routine 'entry' will be in the vectors as
14  * RESET, but this will be called from application.  It's not RESET
15  * state, then.
16  */
17 static __attribute__ ((naked,section(".text.entry")))
18 void entry (void)
19 {
20   asm volatile ("mov    r0, pc\n\t"
21                 "bic    r0, r0, #255\n\t" /* R0 := vector_table address */
22                 "mov    r1, #0x90\n"      /* R1 := numbers of entries * 4 */
23         "0:\n\t"
24                 "ldr    r2, [r0, r1]\n\t"
25                 "add    r2, r2, #-0x20000000\n\t"
26                 "sub    r2, r2, #0x1400\n\t"
27                 "add    r2, r2, r0\n\t" /* Relocate: -0x20001400 + R0 */
28                 "str    r2, [r0, r1]\n\t"
29                 "subs   r1, r1, #4\n\t"
30                 "bne    0b\n\t"
31                 /* Relocation done.  We don't care the first entry.  */
32                 "ldr    r3, .L00\n"
33         ".LPIC00:\n\t"
34                 "add    r3, pc\n\t" /* R3 := @_GLOBAL_OFFSET_TABLE_ */
35                 "ldr    r4, .L00+4\n\t"
36                 "ldr    r0, [r3, r4]\n\t"
37                 "ldr    r4, .L00+8\n\t"
38                 "ldr    r1, [r3, r4]\n\t"
39                 "sub    r0, r0, r1\n\t"
40                 "mov    sp, r0\n\t"
41                 /* Clear BSS.  */
42                 "mov    r0, #0\n\t"
43                 "ldr    r4, .L00+12\n\t"
44                 "ldr    r1, [r3, r4]\n\t"
45                 "ldr    r4, .L00+16\n\t"
46                 "ldr    r2, [r3, r4]\n"
47         "0:\n\t"
48                 "str    r0, [r1], #4\n\t"
49                 "cmp    r2, r1\n\t"
50                 "bhi    0b\n\t"
51                 "cpsie  i\n\t"  /* Enable interrupts */
52                 "mov    r0, #0\n\t"
53                 "mov    r1, r0\n\t"
54                 "bl     main\n"
55         "1:\n\t"
56                 "b      1b\n"
57         ".L00:\n\t"
58                 ".word  _GLOBAL_OFFSET_TABLE_-(.LPIC00+4)\n\t"
59                 ".word  __ram_end__(GOT)\n\t"
60                 ".word  __main_stack_size__(GOT)\n\t"
61                 ".word  _bss_start(GOT)\n\t"
62                 ".word  _bss_end(GOT)"
63                 : /* no output */ : /* no input */ : "memory");
64 }
65
66 typedef void (*handler)(void);
67 extern uint8_t __ram_end__;
68 extern void usb_interrupt_handler (void);
69
70 handler vector_table[] __attribute__ ((section(".vectors"))) = {
71   (handler)&__ram_end__,
72   entry,
73   fatal,                /* nmi */
74   fatal,                /* hard fault */
75   /* 10 */
76   fatal,                /* mem manage */
77   fatal,                /* bus fault */
78   fatal,                /* usage fault */
79   none,
80   /* 20 */
81   none, none, none, none,  none, none, none, none,
82   /* 40 */
83   none, none, none, none,  none, none, none, none,
84   /* 60 */
85   none, none, none, none,  none, none, none, none,
86   /* 80 */
87   none, none, none, none,
88   /* 90 */
89   usb_interrupt_handler,
90 };