2 * crt0iz.c - SDCC pic16 port runtime start code with
3 * initialisation and RAM memory zero
5 * Converted for SDCC and pic16 port
6 * by Vangelis Rokas (vrokas@otenet.gr)
8 * based on Microchip MPLAB-C18 startup files
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * In other words, you are welcome to use, share and improve this program.
25 * You are forbidden to forbid anyone else to use, share and improve
26 * what you give them. Help stamp out software-hoarding!
44 /* global variable for forcing gplink to add _cinit section */
48 /* external reference to the user's main routine */
49 extern void main (void);
51 /* prototype for the startup function */
52 void _entry (void) _naked interrupt 0;
53 void _startup (void) _naked;
55 /* prototype for the initialized data setup */
56 void _do_cinit (void) _naked;
60 * entry function, placed at interrupt vector 0 (RESET)
63 void _entry (void) _naked interrupt 0
65 _asm goto __startup _endasm;
69 void _startup (void) _naked
72 // Initialize the stack pointer
75 clrf _TBLPTRU, 0 // 1st silicon doesn't do this on POR
77 // initialize the flash memory access configuration. this is harmless
78 // for non-flash devices, so we do it on all parts.
85 /* load FSR0 with top of RAM memory */
92 /* place a 1 at address 0x00, as a marker
93 * we haven't reached yet to it */
98 /* load WREG with zero */
109 /* Call the user's main routine */
113 /* return from main will lock up */
118 /* the cinit table will be filled by the linker */
121 unsigned short num_init;
130 #define TBLRDPOSTINC tblrd*+
132 #define prom 0x00 /* 0x00 0x01 0x02*/
133 #define curr_byte 0x03 /* 0x03 0x04 */
134 #define curr_entry 0x05 /* 0x05 0x06 */
135 #define data_ptr 0x07 /* 0x07 0x08 0x09 */
138 * static short long _do_cinit_prom;
139 * static unsigned short _do_cinit_curr_byte;
140 * static unsigned short _do_cinit_curr_entry;
141 * static short long _do_cinit_data_ptr;
144 /* the variable initialisation routine */
145 void _do_cinit (void) _naked
148 * access registers 0x00 - 0x09 are not saved in this function
160 ; curr_entry = cinit.num_init
170 ; while (curr_entry) {
181 ; Count down so we only have to look up the data in _cinit once.
183 ; At this point we know that TBLPTR points to the top of the current
184 ; entry in _cinit, so we can just start reading the from, to, and
187 ; read the source address low
192 ; source address high
197 ; source address upper
202 ; skip a byte since it is stored as a 32bit int
205 ; read the destination address directly into FSR0
206 ; destination address low
211 ; destination address high
216 ; skip two bytes since it is stored as a 32bit int
220 ; read the destination address directly into FSR0
230 ; skip two bytes since it is stored as a 32bit int
234 ; prom = data_ptr->from;
235 ; FSR0 = data_ptr->to;
236 ; curr_byte = (unsigned short) data_ptr->size;
238 ; the table pointer now points to the next entry. Save it
239 ; off since we will be using the table pointer to do the copying
244 movff _TBLPTRL, data_ptr
245 movff _TBLPTRH, data_ptr + 1
246 movff _TBLPTRU, data_ptr + 2
248 ; now assign the source address to the table pointer
252 movff prom + 1, _TBLPTRH
253 movff prom + 2, _TBLPTRU
257 ; determine if we have any more bytes to copy
262 bnz copy_one_byte ; copy_one_byte
263 movf curr_byte + 1, w
271 ; decrement byte counter
273 bnc copy_loop ; copy_loop
274 decf curr_byte + 1, f
280 ; restore the table pointer for the next entry
282 movff data_ptr, _TBLPTRL
283 movff data_ptr + 1, _TBLPTRH
284 movff data_ptr + 2, _TBLPTRU
288 decf curr_entry + 1, f
291 ; _do_cinit_curr_entry--;