2 * crt0i.c - SDCC pic16 port runtime start code with
5 * Converted for SDCC and pic16 port
6 * by Vangelis Rokas (vrokas@otenet.gr)
8 * based on Microchip MPLAB-C18 startup files
23 /* external reference to the user's main routine */
24 extern void main (void);
26 /* prototype for the startup function */
27 void _entry (void) _naked interrupt 0;
30 /* prototype for the initialized data setup */
31 void _do_cinit (void);
35 * entry function, placed at interrupt vector 0 (RESET)
38 void _entry (void) _naked interrupt 0
40 _asm goto __startup _endasm;
47 // Initialize the stack pointer
50 clrf _TBLPTRU, 0 // 1st silicon doesn't do this on POR
52 // initialize the flash memory access configuration. this is harmless
53 // for non-flash devices, so we do it on all parts.
61 /* Call the user's main routine */
65 /* return from main will lock up */
70 /* the cinit table will be filled by the linker */
73 unsigned short num_init;
82 #define tblrdpostinc tblrd*+
84 /* the variable initialisation routine */
88 * we'll make the assumption in the following code that these statics
89 * will be allocated into the same bank.
91 static short long prom;
92 static unsigned short curr_byte;
93 static unsigned short curr_entry;
94 static short long data_ptr;
108 /* curr_entry = cinit.num_init */
110 movlb __do_cinit_data_ptr_1_1
113 movwf __do_cinit_curr_entry_1_1, 1
116 movwf __do_cinit_curr_entry_1_1+1, 1
120 //while (curr_entry) {
124 tstfsz __do_cinit_curr_entry_1_1, 1
134 /* Count down so we only have to look up the data in _cinit
137 * At this point we know that TBLPTR points to the top of the current
138 * entry in _cinit, so we can just start reading the from, to, and
143 /* read the source address low */
146 movwf __do_cinit_prom_1_1, 1
148 /* source address high */
151 movwf __do_cinit_prom_1_1 + 1, 1
153 /* source address upper */
156 movwf __do_cinit_prom_1_1 + 2, 1
158 /* skip a byte since it's stored as a 32bit int */
161 /* read the destination address directly into FSR0 */
162 /* destination address low */
167 /* destination address high */
172 /* skip two bytes since it's stored as a 32bit int */
176 /* read the destination address directly into FSR0 */
179 movwf __do_cinit_curr_byte_1_1, 1
182 movwf __do_cinit_curr_byte_1_1+1, 1
184 /* skip two bytes since it's stored as a 32bit int */
189 //prom = data_ptr->from;
190 //FSR0 = data_ptr->to;
191 //curr_byte = (unsigned short) data_ptr->size;
192 /* the table pointer now points to the next entry. Save it
193 * off since we'll be using the table pointer to do the copying
196 /* data_ptr = TBLPTR */
198 movff _TBLPTRL, __do_cinit_data_ptr_1_1
199 movff _TBLPTRH, __do_cinit_data_ptr_1_1 + 1
200 movff _TBLPTRU, __do_cinit_data_ptr_1_1 + 2
204 /* now assign the source address to the table pointer */
207 movff __do_cinit_prom_1_1, _TBLPTRL
208 movff __do_cinit_prom_1_1 + 1, _TBLPTRH
209 movff __do_cinit_prom_1_1 + 2, _TBLPTRU
212 /* do the copy loop */
215 /* determine if we have any more bytes to copy */
216 movlb __do_cinit_curr_byte_1_1
217 movf __do_cinit_curr_byte_1_1, 1, 1
219 bnz copy_one_byte // copy_one_byte
220 movf __do_cinit_curr_byte_1_1 + 1, 1, 1
228 /* decrement byte counter */
229 decf __do_cinit_curr_byte_1_1, 1, 1
230 bnc copy_loop // copy_loop
231 decf __do_cinit_curr_byte_1_1 + 1, 1, 1
237 /* restore the table pointer for the next entry */
238 /* TBLPTR = data_ptr */
240 movff __do_cinit_data_ptr_1_1, _TBLPTRL
241 movff __do_cinit_data_ptr_1_1 + 1, _TBLPTRH
242 movff __do_cinit_data_ptr_1_1 + 2, _TBLPTRU
252 /* emit done label */