1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*-------------------------------------------------------------------------
crt0iz.c - SDCC pic16 port runtime start code with initialisation and
clear RAM
Copyright (C) 2004, Vangelis Rokas <vrokas at otenet.gr>
Copyright (C) 2012, Molnár Károly <molnarkaroly@users.sf.net>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
As a special exception, if you link this library with other files,
some of which are compiled with SDCC, to produce an executable,
this library does not by itself cause the resulting executable to
be covered by the GNU General Public License. This exception does
not however invalidate any other reasons why the executable file
might be covered by the GNU General Public License.
-------------------------------------------------------------------------
$Id: crt0iz.c 9317 2015-09-13 08:19:01Z spth $
*/
/*
* based on Microchip MPLAB-C18 startup files
*/
extern int stack_end;
extern int TBLPTRU;
extern int TBLPTRH;
extern int TBLPTRL;
extern int FSR0L;
extern int FSR0H;
extern int TABLAT;
extern int POSTINC0;
extern int POSTDEC0;
#if 1
/* Global variable for forcing gplink to add _cinit section. */
char __uflags = 0;
#endif
/* External reference to the user's main routine. */
extern void main (void);
void _entry (void) __naked __interrupt 0;
void _startup (void) __naked;
/* Access bank selector. */
#define a 0
/*
* Entry function, placed at interrupt vector 0 (RESET).
*/
void
_entry (void) __naked __interrupt 0
{
__asm
goto __startup
__endasm;
}
/* The cinit table will be filled by the linker. */
extern __code struct
{
unsigned short num_init;
struct
{
unsigned long from;
unsigned long to;
unsigned long size;
} entries[1];
} cinit;
#define TBLRDPOSTINC tblrd*+
#define src_ptr 0x00 /* 0x00 0x01 0x02*/
#define byte_count 0x03 /* 0x03 0x04 */
#define entry_count 0x05 /* 0x05 0x06 */
#define entry_ptr 0x07 /* 0x07 0x08 0x09 */
void
_startup (void) __naked
{
__asm
; Initialize the stack pointer
lfsr 1, _stack_end
lfsr 2, _stack_end
; 1st silicon does not do this on POR
clrf _TBLPTRU, a
; Initialize the flash memory access configuration.
; This is harmless for non-flash devices, so we do it on all parts.
bsf 0xa6, 7, a ; EECON1.EEPGD = 1, TBLPTR accesses program memory
bcf 0xa6, 6, a ; EECON1.CFGS = 0, TBLPTR accesses program memory
/* cleanup the RAM */
; Load FSR0 with top of RAM.
setf _FSR0L, a
movlw 0x0e
movwf _FSR0H, a
; Place 0xff at address 0x00 as a sentinel.
setf 0x00, a
clear_loop:
clrf _POSTDEC0, a
movf 0x00, w, a
bnz clear_loop
/* Initialize global and/or static variables. */
/*
* Access registers 0x00 - 0x09 are not saved in this code.
*/
; TBLPTR = &cinit
movlw low(_cinit)
movwf _TBLPTRL, a
movlw high(_cinit)
movwf _TBLPTRH, a
movlw upper(_cinit)
movwf _TBLPTRU, a
; entry_count = cinit.num_init
TBLRDPOSTINC
movff _TABLAT, entry_count
TBLRDPOSTINC
movff _TABLAT, (entry_count + 1)
; while (entry_count)
bra entry_loop_dec
entry_loop:
; Count down so we only have to look up the data in _cinit once.
; At this point we know that TBLPTR points to the top of the current
; entry in _cinit, so we can just start reading the from, to, and
; size values.
; Read the source address low.
; src_ptr = entry_ptr->from;
TBLRDPOSTINC
movff _TABLAT, src_ptr
; source address high
TBLRDPOSTINC
movff _TABLAT, (src_ptr + 1)
; source address upper
TBLRDPOSTINC
movff _TABLAT, (src_ptr + 2)
; Skip a byte since it is stored as a 32bit int.
TBLRDPOSTINC
; Read the destination address directly into FSR0
; destination address low.
; FSR0 = (unsigned short)entry_ptr->to;
TBLRDPOSTINC
movff _TABLAT, _FSR0L
; destination address high
TBLRDPOSTINC
movff _TABLAT, _FSR0H
; Skip two bytes since it is stored as a 32bit int.
TBLRDPOSTINC
TBLRDPOSTINC
; Read the size of data to transfer to destination address.
; byte_count = (unsigned short)entry_ptr->size;
TBLRDPOSTINC
movff _TABLAT, byte_count
TBLRDPOSTINC
movff _TABLAT, (byte_count + 1)
; Skip two bytes since it is stored as a 32bit int.
TBLRDPOSTINC
TBLRDPOSTINC
; src_ptr = entry_ptr->from;
; FSR0 = (unsigned short)entry_ptr->to;
; byte_count = (unsigned short)entry_ptr->size;
; The table pointer now points to the next entry. Save it
; off since we will be using the table pointer to do the copying
; for the entry.
; entry_ptr = TBLPTR
movff _TBLPTRL, entry_ptr
movff _TBLPTRH, (entry_ptr + 1)
movff _TBLPTRU, (entry_ptr + 2)
; Now assign the source address to the table pointer.
; TBLPTR = src_ptr
movff src_ptr, _TBLPTRL
movff (src_ptr + 1), _TBLPTRH
movff (src_ptr + 2), _TBLPTRU
bra copy_loop_dec
copy_loop:
TBLRDPOSTINC
movff _TABLAT, _POSTINC0
copy_loop_dec:
; while (--byte_count);
; Decrement and test the byte counter.
; The cycle ends when the value of counter reaches the -1.
decf byte_count, f, a
bc copy_loop
decf (byte_count + 1), f, a
bc copy_loop
; Restore the table pointer for the next entry.
; TBLPTR = entry_ptr
movff entry_ptr, _TBLPTRL
movff (entry_ptr + 1), _TBLPTRH
movff (entry_ptr + 2), _TBLPTRU
entry_loop_dec:
; while (--entry_count);
; Decrement and test the entry counter.
; The cycle ends when the value of counter reaches the -1.
decf entry_count, f, a
bc entry_loop
decf (entry_count + 1), f, a
bc entry_loop
__endasm;
/* Call the main routine. */
main ();
__asm
lockup:
; Returning from main will lock up.
bra lockup
__endasm;
}
|