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
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,
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 -------------------------------------------------------------------------
34 * based on Microchip MPLAB-C18 startup files
48 /* Global variable for forcing gplink to add _cinit section. */
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. */
63 * Entry function, placed at interrupt vector 0 (RESET).
66 _entry (void) __naked
__interrupt (0)
73 /* The cinit table will be filled by the linker. */
76 unsigned short num_init
;
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 */
93 _startup (void) __naked
96 ; Initialize the stack pointer
100 ; 1st silicon does
not do this on POR
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.
121 ; entry_count
= cinit
.num_init
123 movff _TABLAT
, entry_count
126 movff _TABLAT
, (entry_count
+ 1)
128 ; while (entry_count
)
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
140 ; Read the source address low
.
142 ; src_ptr
= entry_ptr
->from
;
145 movff _TABLAT
, src_ptr
147 ; source address high
149 movff _TABLAT
, (src_ptr
+ 1)
151 ; source address upper
153 movff _TABLAT
, (src_ptr
+ 2)
155 ; Skip a byte since it is stored as a
32bit
int.
158 ; Read the destination address directly into FSR0
159 ; destination address low
.
161 ; FSR0
= (unsigned short)entry_ptr
->to
;
164 movff _TABLAT
, _FSR0L
166 ; destination address high
168 movff _TABLAT
, _FSR0H
170 ; Skip two bytes since it is stored as a
32bit
int.
174 ; Read the size of data to transfer to destination address
.
176 ; byte_count
= (unsigned short)entry_ptr
->size
;
179 movff _TABLAT
, byte_count
182 movff _TABLAT
, (byte_count
+ 1)
184 ; Skip two bytes since it is stored as a
32bit
int.
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
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
.
203 movff src_ptr
, _TBLPTRL
204 movff (src_ptr
+ 1), _TBLPTRH
205 movff (src_ptr
+ 2), _TBLPTRU
210 movff _TABLAT
, _POSTINC0
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
219 decf (byte_count
+ 1), f
, a
222 ; Restore the table pointer
for the next entry
.
224 movff entry_ptr
, _TBLPTRL
225 movff (entry_ptr
+ 1), _TBLPTRH
226 movff (entry_ptr
+ 2), _TBLPTRU
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
235 decf (entry_count
+ 1), f
, a
239 /* Call the main routine. */
244 ; Returning from main will lock up
.