-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifi_startup.c
executable file
·184 lines (164 loc) · 4.37 KB
/
ifi_startup.c
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
/*******************************************************************************
* FILE NAME: ifi_startup.c
*
* DESCRIPTION:
* This file contains important startup code.
*
* USAGE:
* This file should not be modified at all by the user.
*
* DO NOT MODIFY THIS FILE!
*******************************************************************************/
#include "ifi_default.h"
extern void Clear_Memory (void);
extern void main (void);
void _entry (void); /* prototype for the startup function */
void _startup (void);
void _do_cinit (void); /* prototype for the initialized data setup */
extern volatile near unsigned long short TBLPTR;
extern near unsigned FSR0;
extern near char FPFLAGS;
#define RND 6
#pragma code _entry_scn=RESET_VECTOR
void _entry (void)
{
_asm goto _startup _endasm
}
#pragma code _startup_scn
void _startup (void)
{
_asm
/* Initialize the stack pointer */
lfsr 1, _stack lfsr 2, _stack clrf TBLPTRU, 0 /* 1st silicon doesn't do this on POR */
bcf FPFLAGS,RND,0 /* Initialize rounding flag for floating point libs */
/* initialize the flash memory access configuration. this is harmless */
/* for non-flash devices, so we do it on all parts. */
bsf 0xa6, 7, 0
bcf 0xa6, 6, 0
_endasm
loop:
Clear_Memory();
_do_cinit ();
/* Call the user's main routine */
main ();
goto loop;
} /* end _startup() */
/* MPLAB-C18 initialized data memory support */
/* The linker will populate the _cinit table */
extern far rom struct
{
unsigned short num_init;
struct _init_entry
{
unsigned long from;
unsigned long to;
unsigned long size;
}
entries[];
}
_cinit;
#pragma code _cinit_scn
void
_do_cinit (void)
{
/* we'll make the assumption in the following code that these statics
* will be allocated into the same bank.
*/
static short long prom;
static unsigned short curr_byte;
static unsigned short curr_entry;
static short long data_ptr;
/* Initialized data... */
TBLPTR = (short long)&_cinit;
_asm
movlb data_ptr
tblrdpostinc
movf TABLAT, 0, 0
movwf curr_entry, 1
tblrdpostinc
movf TABLAT, 0, 0
movwf curr_entry+1, 1
_endasm
test:
_asm
bnz 3
tstfsz curr_entry, 1
bra 1
_endasm
goto done;
/* 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.
*/
_asm
/* read the source address */
tblrdpostinc
movf TABLAT, 0, 0
movwf prom, 1
tblrdpostinc
movf TABLAT, 0, 0
movwf prom+1, 1
tblrdpostinc
movf TABLAT, 0, 0
movwf prom+2, 1
/* skip a byte since it's stored as a 32bit int */
tblrdpostinc
/* read the destination address directly into FSR0 */
tblrdpostinc
movf TABLAT, 0, 0
movwf FSR0L, 0
tblrdpostinc
movf TABLAT, 0, 0
movwf FSR0H, 0
/* skip two bytes since it's stored as a 32bit int */
tblrdpostinc
tblrdpostinc
/* read the destination address directly into FSR0 */
tblrdpostinc
movf TABLAT, 0, 0
movwf curr_byte, 1
tblrdpostinc
movf TABLAT, 0, 0
movwf curr_byte+1, 1
/* skip two bytes since it's stored as a 32bit int */
tblrdpostinc
tblrdpostinc
_endasm
/* the table pointer now points to the next entry. Save it
* off since we'll be using the table pointer to do the copying
* for the entry.
*/
data_ptr = TBLPTR;
/* now assign the source address to the table pointer */
TBLPTR = prom;
/* do the copy loop */
_asm
/* determine if we have any more bytes to copy */
movlb curr_byte
movf curr_byte, 1, 1
copy_loop:
bnz 2 /* copy_one_byte */
movf curr_byte + 1, 1, 1
bz 7 /* done_copying */
copy_one_byte:
tblrdpostinc
movf TABLAT, 0, 0
movwf POSTINC0, 0
/* decrement byte counter */
decf curr_byte, 1, 1
bc -8 /* copy_loop */
decf curr_byte + 1, 1, 1
bra -7 /* copy_one_byte */
done_copying:
_endasm
/* restore the table pointer for the next entry */
TBLPTR = data_ptr;
/* next entry... */
curr_entry--;
goto test;
done:
;
}