1 /*-------------------------------------------------------------------------
2 crt0iz.c - SDCC pic16 port runtime start code with initialisation and
5 Copyright (C) 2004, Vangelis Rokas <vrokas at otenet.gr>
6 Copyright (C) 2012, Molnár Károly <molnarkaroly@users.sf.net>
8 This library is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this library; see the file COPYING. If not, write to the
20 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
23 As a special exception, if you link this library with other files,
24 some of which are compiled with SDCC, to produce an executable,
25 this library does not by itself cause the resulting executable to
26 be covered by the GNU General Public License. This exception does
27 not however invalidate any other reasons why the executable file
28 might be covered by the GNU General Public License.
29 -------------------------------------------------------------------------
35 * based on Microchip MPLAB-C18 startup files
50 /* Global variable for forcing gplink to add _cinit section. */
54 /* External reference to the user's main routine. */
55 extern void main (void);
57 void _entry (void) __naked
__interrupt (0);
58 void _startup (void) __naked
;
60 /* Access bank selector. */
65 * Entry function, placed at interrupt vector 0 (RESET).
68 _entry (void) __naked
__interrupt (0)
75 /* The cinit table will be filled by the linker. */
78 unsigned short num_init
;
87 #define TBLRDPOSTINC tblrd*+
89 #define src_ptr 0x00 /* 0x00 0x01 0x02*/
90 #define byte_count 0x03 /* 0x03 0x04 */
91 #define entry_count 0x05 /* 0x05 0x06 */
92 #define entry_ptr 0x07 /* 0x07 0x08 0x09 */
95 _startup (void) __naked
98 ; Initialize the stack pointer
102 ; 1st silicon does
not do this on POR
105 ; Initialize the flash memory access configuration
.
106 ; This is harmless
for non
-flash devices
, so we
do it on all parts
.
107 bsf
0xa6, 7, a
; EECON1
.EEPGD
= 1, TBLPTR accesses program memory
108 bcf
0xa6, 6, a
; EECON1
.CFGS
= 0, TBLPTR accesses program memory
110 /* cleanup the RAM */
111 ; Load FSR0 with top of SRAM
.
114 ; Place
0xff at address
0x00 as a sentinel
.
122 /* Initialize global and/or static variables. */
125 * Access registers 0x00 - 0x09 are not saved in this code.
135 ; entry_count
= cinit
.num_init
137 movff _TABLAT
, entry_count
140 movff _TABLAT
, (entry_count
+ 1)
142 ; while (entry_count
)
148 ; Count down so we only have to look up the data in _cinit once
.
150 ; At
this point we know that TBLPTR points to the top of the current
151 ; entry in _cinit
, so we can just start reading the from
, to
, and
154 ; Read the source address low
.
156 ; src_ptr
= entry_ptr
->from
;
159 movff _TABLAT
, src_ptr
161 ; source address high
163 movff _TABLAT
, (src_ptr
+ 1)
165 ; source address upper
167 movff _TABLAT
, (src_ptr
+ 2)
169 ; Skip a byte since it is stored as a
32bit
int.
172 ; Read the destination address directly into FSR0
173 ; destination address low
.
175 ; FSR0
= (unsigned short)entry_ptr
->to
;
178 movff _TABLAT
, _FSR0L
180 ; destination address high
182 movff _TABLAT
, _FSR0H
184 ; Skip two bytes since it is stored as a
32bit
int.
188 ; Read the size of data to transfer to destination address
.
190 ; byte_count
= (unsigned short)entry_ptr
->size
;
193 movff _TABLAT
, byte_count
196 movff _TABLAT
, (byte_count
+ 1)
198 ; Skip two bytes since it is stored as a
32bit
int.
202 ; src_ptr
= entry_ptr
->from
;
203 ; FSR0
= (unsigned short)entry_ptr
->to
;
204 ; byte_count
= (unsigned short)entry_ptr
->size
;
206 ; The table pointer now points to the next entry
. Save it
207 ; off since we will be
using the table pointer to
do the copying
211 movff _TBLPTRL
, entry_ptr
212 movff _TBLPTRH
, (entry_ptr
+ 1)
213 movff _TBLPTRU
, (entry_ptr
+ 2)
215 ; Now assign the source address to the table pointer
.
217 movff src_ptr
, _TBLPTRL
218 movff (src_ptr
+ 1), _TBLPTRH
219 movff (src_ptr
+ 2), _TBLPTRU
224 movff _TABLAT
, _POSTINC0
227 ; while (--byte_count
);
229 ; Decrement
and test the byte counter
.
230 ; The cycle ends when the value of counter reaches the
-1.
231 decf byte_count
, f
, a
233 decf (byte_count
+ 1), f
, a
236 ; Restore the table pointer
for the next entry
.
238 movff entry_ptr
, _TBLPTRL
239 movff (entry_ptr
+ 1), _TBLPTRH
240 movff (entry_ptr
+ 2), _TBLPTRU
243 ; while (--entry_count
);
245 ; Decrement
and test the entry counter
.
246 ; The cycle ends when the value of counter reaches the
-1.
247 decf entry_count
, f
, a
249 decf (entry_count
+ 1), f
, a
253 /* Call the main routine. */
258 ; Returning from main will lock up
.