Imported Upstream version 2.9.0
[debian/cc1111] / device / lib / pic / libsdcc / idata.c
1 /*
2  * idata.c - startup code evaluating gputils' cinit structure
3  *
4  * This code fragment copies initialized data from ROM to their
5  * assigned RAM locations. The requierd cinit structure is created
6  * by gputils' linker and comprises initial values of all linked in
7  * modules.
8  *
9  * (c) 2007 by Raphael Neider <rneider @ web.de>
10  * 
11  * This file is part of SDCC's pic14 library and distributed under
12  * the terms of the GPLv2 with linking exception; see COPYING in some
13  * parent directory for details.
14  */
15
16 /*
17  * We call the user's main() after initialization is done.
18  */
19 extern void main(void);
20
21 /*
22  * Force generation of _cinit symbol.
23  */
24 static char force_cinit = 0;
25
26 /*
27  * This struct describes one initialized variable.
28  */
29 typedef struct {
30     unsigned src;   // source address of data in CODE space
31     unsigned dst;   // destination address of values in DATA space
32     unsigned size;  // number of bytes to copy from `src' to `dst'
33 } cinit_t;
34
35 /*
36  * This structure provides the number and position of the above
37  * structs. to initialize all variables in the .hex file.
38  */
39 extern __code struct {
40     unsigned    records;    // number of entries in this file
41     cinit_t     entry[];    // intialization descriptor
42 } cinit;
43
44 /*
45  * Iterate over all records and copy values from ROM to RAM.
46  */
47 void
48 _sdcc_gsinit_startup(void)
49 {
50     unsigned num, size;
51     __code cinit_t *cptr;
52     __code char *src;
53     __data char *dst;
54     
55     num = cinit.records;
56     cptr = &cinit.entry[0];
57     
58     // iterate over all cinit entries
59     while (num--) {
60         size = cptr->size;
61         src = (__code char *) cptr->src;
62         dst = (__data char *) cptr->dst;
63         
64         // copy data byte-wise from ROM to RAM
65         while (size--) {
66             *dst = *src;
67             src++;
68             dst++;
69         } // while
70         
71         // XXX: might need to clear the watchdog timer here...
72         cptr++;
73     } // while
74
75     // call main after initialization
76     __asm
77         PAGESEL _main
78         GOTO _main
79     __endasm;
80 }
81