struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / pic16 / startup / crt0i.c
blob09dacd7f1b0287dbc2e66b886d77355b78205acd
1 /*-------------------------------------------------------------------------
2 crt0i.c - SDCC pic16 port runtime start code with initialisation
4 Copyright (C) 2004, Vangelis Rokas <vrokas at otenet.gr>
5 Copyright (C) 2012, Molnár Károly <molnarkaroly@users.sf.net>
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this library; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA.
22 As a special exception, if you link this library with other files,
23 some of which are compiled with SDCC, to produce an executable,
24 this library does not by itself cause the resulting executable to
25 be covered by the GNU General Public License. This exception does
26 not however invalidate any other reasons why the executable file
27 might be covered by the GNU General Public License.
28 -------------------------------------------------------------------------
30 $Id$
34 * based on Microchip MPLAB-C18 startup files
37 extern int stack_end;
38 extern int TBLPTRU;
39 extern int TBLPTRH;
40 extern int TBLPTRL;
41 extern int FSR0L;
42 extern int FSR0H;
43 extern int TABLAT;
44 extern int POSTINC0;
47 #if 1
48 /* Global variable for forcing gplink to add _cinit section. */
49 char __uflags = 0;
50 #endif
52 /* External reference to the user's main routine. */
53 extern void main (void);
55 void _entry (void) __naked __interrupt (0);
56 void _startup (void) __naked;
58 /* Access bank selector. */
59 #define a 0
63 * Entry function, placed at interrupt vector 0 (RESET).
65 void
66 _entry (void) __naked __interrupt (0)
68 __asm
69 goto __startup
70 __endasm;
73 /* The cinit table will be filled by the linker. */
74 extern __code struct
76 unsigned short num_init;
77 struct
79 unsigned long from;
80 unsigned long to;
81 unsigned long size;
82 } entries[1];
83 } cinit;
85 #define TBLRDPOSTINC tblrd*+
87 #define src_ptr 0x00 /* 0x00 0x01 0x02*/
88 #define byte_count 0x03 /* 0x03 0x04 */
89 #define entry_count 0x05 /* 0x05 0x06 */
90 #define entry_ptr 0x07 /* 0x07 0x08 0x09 */
92 void
93 _startup (void) __naked
95 __asm
96 ; Initialize the stack pointer
97 lfsr 1, _stack_end
98 lfsr 2, _stack_end
100 ; 1st silicon does not do this on POR
101 clrf _TBLPTRU, a
103 ; Initialize the flash memory access configuration.
104 ; This is harmless for non-flash devices, so we do it on all parts.
105 bsf 0xa6, 7, a ; EECON1.EEPGD = 1, TBLPTR accesses program memory
106 bcf 0xa6, 6, a ; EECON1.CFGS = 0, TBLPTR accesses program memory
108 /* Initialize global and/or static variables. */
111 * Access registers 0x00 - 0x09 are not saved in this code.
113 ; TBLPTR = &cinit
114 movlw low(_cinit)
115 movwf _TBLPTRL, a
116 movlw high(_cinit)
117 movwf _TBLPTRH, a
118 movlw upper(_cinit)
119 movwf _TBLPTRU, a
121 ; entry_count = cinit.num_init
122 TBLRDPOSTINC
123 movff _TABLAT, entry_count
125 TBLRDPOSTINC
126 movff _TABLAT, (entry_count + 1)
128 ; while (entry_count)
130 bra entry_loop_dec
132 entry_loop:
134 ; Count down so we only have to look up the data in _cinit once.
136 ; At this point we know that TBLPTR points to the top of the current
137 ; entry in _cinit, so we can just start reading the from, to, and
138 ; size values.
140 ; Read the source address low.
142 ; src_ptr = entry_ptr->from;
144 TBLRDPOSTINC
145 movff _TABLAT, src_ptr
147 ; source address high
148 TBLRDPOSTINC
149 movff _TABLAT, (src_ptr + 1)
151 ; source address upper
152 TBLRDPOSTINC
153 movff _TABLAT, (src_ptr + 2)
155 ; Skip a byte since it is stored as a 32bit int.
156 TBLRDPOSTINC
158 ; Read the destination address directly into FSR0
159 ; destination address low.
161 ; FSR0 = (unsigned short)entry_ptr->to;
163 TBLRDPOSTINC
164 movff _TABLAT, _FSR0L
166 ; destination address high
167 TBLRDPOSTINC
168 movff _TABLAT, _FSR0H
170 ; Skip two bytes since it is stored as a 32bit int.
171 TBLRDPOSTINC
172 TBLRDPOSTINC
174 ; Read the size of data to transfer to destination address.
176 ; byte_count = (unsigned short)entry_ptr->size;
178 TBLRDPOSTINC
179 movff _TABLAT, byte_count
181 TBLRDPOSTINC
182 movff _TABLAT, (byte_count + 1)
184 ; Skip two bytes since it is stored as a 32bit int.
185 TBLRDPOSTINC
186 TBLRDPOSTINC
188 ; src_ptr = entry_ptr->from;
189 ; FSR0 = (unsigned short)entry_ptr->to;
190 ; byte_count = (unsigned short)entry_ptr->size;
192 ; The table pointer now points to the next entry. Save it
193 ; off since we will be using the table pointer to do the copying
194 ; for the entry.
196 ; entry_ptr = TBLPTR
197 movff _TBLPTRL, entry_ptr
198 movff _TBLPTRH, (entry_ptr + 1)
199 movff _TBLPTRU, (entry_ptr + 2)
201 ; Now assign the source address to the table pointer.
202 ; TBLPTR = src_ptr
203 movff src_ptr, _TBLPTRL
204 movff (src_ptr + 1), _TBLPTRH
205 movff (src_ptr + 2), _TBLPTRU
206 bra copy_loop_dec
208 copy_loop:
209 TBLRDPOSTINC
210 movff _TABLAT, _POSTINC0
212 copy_loop_dec:
213 ; while (--byte_count);
215 ; Decrement and test the byte counter.
216 ; The cycle ends when the value of counter reaches the -1.
217 decf byte_count, f, a
218 bc copy_loop
219 decf (byte_count + 1), f, a
220 bc copy_loop
222 ; Restore the table pointer for the next entry.
223 ; TBLPTR = entry_ptr
224 movff entry_ptr, _TBLPTRL
225 movff (entry_ptr + 1), _TBLPTRH
226 movff (entry_ptr + 2), _TBLPTRU
228 entry_loop_dec:
229 ; while (--entry_count);
231 ; Decrement and test the entry counter.
232 ; The cycle ends when the value of counter reaches the -1.
233 decf entry_count, f, a
234 bc entry_loop
235 decf (entry_count + 1), f, a
236 bc entry_loop
237 __endasm;
239 /* Call the main routine. */
240 main ();
242 __asm
243 lockup:
244 ; Returning from main will lock up.
245 bra lockup
246 __endasm;