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
24 /* external reference to the user's main routine */
25 extern void main (void);
27 /* prototype for the startup function */
28 void _entry (void) _naked interrupt 0;
31 /* prototype for the initialized data setup */
32 void _do_cinit (void);
36 * entry function, placed at interrupt vector 0 (RESET)
39 void _entry (void) _naked interrupt 0
41 _asm goto __startup _endasm;
48 // Initialize the stack pointer
51 clrf _TBLPTRU, 0 // 1st silicon doesn't do this on POR
53 // initialize the flash memory access configuration. this is harmless
54 // for non-flash devices, so we do it on all parts.
62 /* Call the user's main routine */
66 /* return from main will lock up */
71 /* the cinit table will be filled by the linker */
74 unsigned short num_init;
83 #define tblrdpostinc tblrd*+
85 /* the variable initialisation routine */
89 * we'll make the assumption in the following code that these statics
90 * will be allocated into the same bank.
92 static short long prom;
93 static unsigned short curr_byte;
94 static unsigned short curr_entry;
95 static short long data_ptr;
109 /* curr_entry = cinit.num_init */
111 movlb __do_cinit_data_ptr_1_1
114 movwf __do_cinit_curr_entry_1_1, 1
117 movwf __do_cinit_curr_entry_1_1+1, 1
121 //while (curr_entry) {
125 tstfsz __do_cinit_curr_entry_1_1, 1
135 /* Count down so we only have to look up the data in _cinit
138 * At this point we know that TBLPTR points to the top of the current
139 * entry in _cinit, so we can just start reading the from, to, and
144 /* read the source address low */
147 movwf __do_cinit_prom_1_1, 1
149 /* source address high */
152 movwf __do_cinit_prom_1_1 + 1, 1
154 /* source address upper */
157 movwf __do_cinit_prom_1_1 + 2, 1
159 /* skip a byte since it's stored as a 32bit int */
162 /* read the destination address directly into FSR0 */
163 /* destination address low */
168 /* destination address high */
173 /* skip two bytes since it's stored as a 32bit int */
177 /* read the destination address directly into FSR0 */
180 movwf __do_cinit_curr_byte_1_1, 1
183 movwf __do_cinit_curr_byte_1_1+1, 1
185 /* skip two bytes since it's stored as a 32bit int */
190 //prom = data_ptr->from;
191 //FSR0 = data_ptr->to;
192 //curr_byte = (unsigned short) data_ptr->size;
193 /* the table pointer now points to the next entry. Save it
194 * off since we'll be using the table pointer to do the copying
197 /* data_ptr = TBLPTR */
199 movff _TBLPTRL, __do_cinit_data_ptr_1_1
200 movff _TBLPTRH, __do_cinit_data_ptr_1_1 + 1
201 movff _TBLPTRU, __do_cinit_data_ptr_1_1 + 2
205 /* now assign the source address to the table pointer */
208 movff __do_cinit_prom_1_1, _TBLPTRL
209 movff __do_cinit_prom_1_1 + 1, _TBLPTRH
210 movff __do_cinit_prom_1_1 + 2, _TBLPTRU
213 /* do the copy loop */
216 /* determine if we have any more bytes to copy */
217 movlb __do_cinit_curr_byte_1_1
218 movf __do_cinit_curr_byte_1_1, 1, 1
220 bnz copy_one_byte // copy_one_byte
221 movf __do_cinit_curr_byte_1_1 + 1, 1, 1
229 /* decrement byte counter */
230 decf __do_cinit_curr_byte_1_1, 1, 1
231 bnc copy_loop // copy_loop
232 decf __do_cinit_curr_byte_1_1 + 1, 1, 1
238 /* restore the table pointer for the next entry */
239 /* TBLPTR = data_ptr */
241 movff __do_cinit_data_ptr_1_1, _TBLPTRL
242 movff __do_cinit_data_ptr_1_1 + 1, _TBLPTRH
243 movff __do_cinit_data_ptr_1_1 + 2, _TBLPTRU
253 /* emit done label */