* device/lib/Makefile.in: removed comment line with model-pic16,
[fw/sdcc] / src / pic16 / device.c
1 /*-------------------------------------------------------------------------
2
3   device.c - Accomodates subtle variations in PIC16 devices
4
5    Written By -  Scott Dattalo scott@dattalo.com
6    Ported to PIC16 By -  Martin Dubuc m.dubuc@rogers.com
7
8    This program 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
11    later version.
12    
13    This program 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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 -------------------------------------------------------------------------*/
22
23
24 /*
25         VR - Began writing code to make PIC16 C source files independent from
26         the header file (created by the inc2h.pl)
27
28         - adding maximum RAM memory into PIC_Device structure
29
30 */
31
32 #include <stdio.h>
33
34 #include "common.h"   // Include everything in the SDCC src directory
35 #include "newalloc.h"
36
37
38 #include "pcode.h"
39 #include "ralloc.h"
40 #include "device.h"
41
42
43 static PIC16_device Pics16[] = {
44   {
45     {"p18f242", "18f242", "pic18f242", "f242"},         // aliases
46     0,
47     0x300,                                              // bank mask
48     0x300,                                              // RAMsize
49     0,
50         {
51                 /* PIC18F242 configuration words */
52                 0x300001,
53                 0x30000d,
54                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
55                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
56                 { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
57                 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
58                 { 0x40, 0, 0xff } /* d */ } 
59         }
60   },
61
62   {
63     {"p18f248", "18f248", "pic18f248", "f248"},         // aliases
64     0,
65     0x300,                                              // bank mask
66     0x300,                                              // RAMsize
67     0,
68         {
69                 /* PIC18F248 configuration words */
70                 0x300001,
71                 0x30000d,
72                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
73                 { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
74                 { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
75                 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
76                 { 0x40, 0, 0xff } /* d */ } 
77         }
78   },
79
80   {
81     {"p18f252", "18f252", "pic18f252", "f252"},         // aliases
82     0,
83     0x600,                                              // bank mask
84     0x600,                                              // RAMsize
85     0,
86         {
87                 /* PIC18F252 configuration words */
88                 0x300001,
89                 0x30000d,
90                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
91                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
92                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
93                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
94                 { 0x40, 0, 0xff } /* d */ } 
95         }
96   },
97
98   {
99     {"p18f258", "18f258", "pic18f258", "f258"},         // aliases
100     0,
101     0x600,                                              // bank mask
102     0x600,                                              // RAMsize
103     0,
104         {
105                 /* PIC18F258 configuration words */
106                 0x300001,
107                 0x30000d,
108                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
109                 { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
110                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
111                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
112                 { 0x40, 0, 0xff } /* d */ } 
113         }
114   },
115
116   {
117     {"p18f442", "18f442", "pic18f442", "f442"},         // aliases
118     0,
119     0x300,                                              // bank mask
120     0x300,                                              // RAMsize
121     0,
122         {
123                 /* PIC18F442 configuration words */
124                 0x300001,
125                 0x30000d,
126                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
127                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
128                 { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
129                 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
130                 { 0x40, 0, 0xff } /* d */ } 
131         }
132   },
133
134   {
135     {"p18f448", "18f448", "pic18f448", "f448"},         // aliases
136     0,
137     0x300,                                              // bank mask
138     0x300,                                              // RAMsize
139     0,
140         {
141                 /* PIC18F448 configuration words */
142                 0x300001,
143                 0x30000d,
144                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
145                 { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
146                 { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
147                 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
148                 { 0x40, 0, 0xff } /* d */ } 
149         }
150   },
151   
152   {
153     {"p18f452", "18f452", "pic18f452", "f452"},         // aliases
154     0,
155     0x600,                                              // bank mask
156     0x600,                                              // RAMsize
157     0,
158         {
159                 /* PIC18F452 configuration words */
160                 0x300001,
161                 0x30000d,
162                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
163                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
164                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
165                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
166                 { 0x40, 0, 0xff } /* d */ } 
167         }
168   },
169
170   {
171     {"p18f458", "18f458", "pic18f458", "f458"},         // aliases
172     0,
173     0x600,                                              // bank mask
174     0x600,                                              // RAMsize
175     0,
176         {
177                 /* PIC18F458 configuration words */
178                 0x300001,
179                 0x30000d,
180                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
181                 { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
182                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
183                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
184                 { 0x40, 0, 0xff } /* d */ } 
185         }
186   },
187
188   {
189     {"p18f1220", "18f1220", "pic18f1220", "f1220"},     // aliases
190     0,
191     0x200,
192     0x200,
193     0,
194         {
195                 /* PIC18F1220 configuration words */
196                 0x300001,
197                 0x30000d,
198                 { { 0xcf, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
199                 { -1, 0, 0xff } /* 4 */  , { 0x80, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
200                 { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
201                 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
202                 { 0x40, 0, 0xff } /* d */ } 
203         }
204   },
205
206
207   {
208     {"p18f6520", "18f6520", "pic18f6520", "f6520"},     // aliases
209     0,
210     0x800,                                              // bank mask
211     0x800,                                              // RAMsize
212     1,
213         {
214                 /* PIC18F6520 configuration words */
215                 0x300001,
216                 0x30000d,
217                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
218                 { 0x80, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */  , 
219                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
220                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
221                 { 0x40, 0, 0xff } /* d */ } 
222         }
223   },
224
225   {
226     {"p18f6620", "18f6620", "pic18f6620", "f6620"},     // aliases
227     0,
228     0xf00,                                              // bank mask
229     0xf00,                                              // RAMsize
230     1,
231         {
232                 /* PIC18F6620 configuration words */
233                 0x300001,
234                 0x30000d,
235                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
236                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
237                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
238                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
239                 { 0x40, 0, 0xff } /* d */ } 
240         }
241   },
242
243   {
244     {"p18f6680", "18f6680", "pic18f6680", "f6680"},     // aliases
245     0,
246     0xc00,                                              // bank mask
247     0xc00,                                              // RAMsize
248     1,
249         {
250                 /* PIC18F6680 configuration words */
251                 0x300001,
252                 0x30000d,
253                 { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
254                 { -1, 0, 0xff } /* 4 */  , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
255                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
256                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
257                 { 0x40, 0, 0xff } /* d */ } 
258         }
259   },
260
261   {
262     {"p18f6720", "18f6720", "pic18f6720", "f6720"},     // aliases
263     0,
264     0xf00,                                              // bank mask
265     0xf00,                                              // RAMsize
266     1,
267         {
268                 /* PIC18F6720 configuration words */
269                 0x300001,
270                 0x30000d,
271                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
272                 { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
273                 { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
274                 { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
275                 { 0x40, 0, 0xff } /* d */ } 
276         }
277   },
278
279   {
280     {"p18f8520", "18f8520", "pic18f8520", "f8520"},     // aliases
281     0,
282     0x800,                                              // bank mask
283     0x800,                                              // RAMsize
284     1,
285         {
286                 /* PIC18F8520 configuration words */
287                 0x300001,
288                 0x30000d,
289                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
290                 { 0x83, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */  , 
291                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
292                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
293                 { 0x40, 0, 0xff } /* d */ } 
294         }
295   },
296
297   {
298     {"p18f8620", "18f8620", "pic18f8620", "f8620"},     // aliases
299     0,
300     0xf00,                                              // bank mask
301     0xf00,                                              // RAMsize
302     1,
303         {
304                 /* PIC18F8620 configuration words */
305                 0x300001,
306                 0x30000d,
307                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
308                 { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
309                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
310                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
311                 { 0x40, 0, 0xff } /* d */ }
312         }
313   },
314   {
315     {"p18f8680", "18f8680", "pic18f8680", "f8680"},     // aliases
316     0,
317     0xc00,                                              // bank mask
318     0x800,                                              // RAMsize
319     1,
320         {
321                 /* PIC18F8680 configuration words */
322                 0x300001,
323                 0x30000d,
324                 { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
325                 { 0x83, 0, 0xff } /* 4 */ , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
326                 { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
327                 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
328                 { 0x40, 0, 0xff } /* d */ }
329         }
330   },
331
332   {
333     {"p18f8720", "18f8720", "pic18f8720", "f8720"},     // aliases
334     0,
335     0xf00,                                              // bank mask
336     0xf00,                                              // RAMsize
337     1,
338         {
339                 /* PIC18F8720 configuration words */
340                 0x300001,
341                 0x30000d,
342                 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
343                 { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
344                 { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
345                 { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
346                 { 0x40, 0, 0xff } /* d */ }
347         }
348   }
349
350 };
351
352 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
353
354 #define DEFAULT_PIC "452"
355
356 PIC16_device *pic16=NULL;
357 unsigned int stackPos = 0;
358
359 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
360
361 void pic16_setMaxRAM(int size)
362 {
363         pic16->maxRAMaddress = size;
364         stackPos = pic16->RAMsize-1;
365
366         if (pic16->maxRAMaddress < 0) {
367                 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
368                         pic16->maxRAMaddress);
369           return;
370         }
371 }
372
373 extern char *iComments2;
374
375 void pic16_dump_equates(FILE *of, set *equs)
376 {
377   regs *r;
378
379         r = setFirstItem(equs);
380         if(!r)return;
381         
382         fprintf(of, "%s", iComments2);
383         fprintf(of, ";\tEquates to used internal registers\n");
384         fprintf(of, "%s", iComments2);
385         
386         for(; r; r = setNextItem(equs)) {
387                 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
388         }
389 }
390
391
392 int regCompare(const void *a, const void *b)
393 {
394   const regs *const *i = a;
395   const regs *const *j = b;
396
397         /* sort primarily by the address */
398         if( (*i)->address > (*j)->address)return 1;
399         if( (*i)->address < (*j)->address)return -1;
400         
401         /* and secondarily by size */
402         /* register size sorting may have strange results use with care */
403         if( (*i)->size > (*j)->size)return 1;
404         if( (*i)->size < (*j)->size)return -1;
405         
406         /* finally if in same address and same size sort by name */
407         return (strcmp( (*i)->name, (*j)->name));
408
409   return 0;
410 }
411
412 int symCompare(const void *a, const void *b)
413 {
414   const symbol *const *i = a;
415   const symbol *const *j = b;
416
417         /* sort primarily by the address */
418         if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
419         if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
420         
421         /* and secondarily by size */
422         /* register size sorting may have strange results use with care */
423         if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
424         if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
425
426         /* finally if in same address and same size sort by name */
427         return (strcmp( (*i)->rname, (*j)->rname));
428
429   return 0;
430 }
431
432 void pic16_dump_usection(FILE *of, set *section, int fix)
433 {
434   static int abs_usection_no=0;
435   regs *r, *rprev;
436   int init_addr, i;
437   regs **rlist;
438   regs *r1;
439
440         /* put all symbols in an array */
441         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
442         r = rlist[0]; i = 0;
443         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
444                 rlist[i] = rprev; i++;
445         }
446         
447         if(!i) {
448                 if(rlist)free(rlist);
449           return;
450         }
451
452         /* sort symbols according to their address */
453         qsort(rlist, i  /*elementsInSet(section)*/, sizeof(regs *), regCompare);
454         
455         if(!fix) {
456                 fprintf(of, "\n\n\tudata\n");
457                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
458                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
459                 }
460         } else {
461           int j=0;
462           int deb_addr=0;
463
464                 rprev = NULL;
465                 init_addr = rlist[j]->address;
466                 deb_addr = init_addr;
467                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
468         
469                 for(j=0;j<i;j++) {
470                         r = rlist[j];
471                         if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
472                         
473                         init_addr = r->address;
474                         deb_addr = init_addr;
475                         
476                         if((rprev && (init_addr > (rprev->address + rprev->size)))) {
477                                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
478                         }
479
480                         if(r1 && (init_addr == r1->address)) {
481                                 fprintf(of, "\n%s\tres\t0\n", r->name);
482                         } else {
483                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
484                                 deb_addr += r->size;
485                         }
486                         
487                         rprev = r;
488                 }
489         }
490         free(rlist);
491 }
492
493
494 /* forward declaration */
495 void print_idata(FILE *of, symbol * sym, sym_link * type, initList * ilist);
496
497 void pic16_dump_isection(FILE *of, set *section, int fix)
498 {
499   static int abs_isection_no=0;
500   symbol *s, *sprev;
501   int init_addr, i;
502   symbol **slist;
503
504         /* put all symbols in an array */
505         slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
506         s = slist[0]; i = 0;
507         for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
508                 slist[i] = sprev; i++;
509         }
510         
511         if(!i) {
512                 if(slist)free(slist);
513           return;
514         }
515
516         /* sort symbols according to their address */
517         qsort(slist, i, sizeof(symbol *), symCompare);
518         
519         if(!fix) {
520                 fprintf(of, "\n\n\tidata\n");
521                 for(s = setFirstItem(section); s; s = setNextItem(section)) {
522                         print_idata(of, s, s->type, s->ival);
523                 }
524         } else {
525           int j=0;
526           symbol *s1;
527           
528                 sprev = NULL;
529                 init_addr = SPEC_ADDR(slist[j]->etype);
530                 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
531         
532                 for(j=0;j<i;j++) {
533                         s = slist[j];
534                         if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
535                         
536                         init_addr = SPEC_ADDR(s->etype);
537
538                         if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
539                                 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
540                         }
541
542                         print_idata(of, s, s->type, s->ival);
543                         
544 #if 0
545                         if(r1 && (init_addr == r1->address)) {
546                                 fprintf(of, "%s\tres\t0\n\n", r->name);
547                         } else {
548                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
549                         }
550 #endif
551                         
552                         sprev = s;
553                 }
554         }
555         free(slist);
556 }
557
558
559 void pic16_dump_int_registers(FILE *of, set *section)
560 {
561   regs *r, *rprev;
562   int i;
563   regs **rlist;
564
565         /* put all symbols in an array */
566         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
567         r = rlist[0]; i = 0;
568         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
569                 rlist[i] = rprev; i++;
570         }
571
572         /* sort symbols according to their address */
573         qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
574         
575         if(!i) {
576                 if(rlist)free(rlist);
577           return;
578         }
579         
580         fprintf(of, "\n\n; Internal registers\n");
581         
582         fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
583         for(r = setFirstItem(section); r; r = setNextItem(section))
584                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
585
586         free(rlist);
587 }
588
589
590 #ifdef WORDS_BIGENDIAN
591   #define _ENDIAN(x)  (3-x)
592 #else
593   #define _ENDIAN(x)  (x)
594 #endif
595
596 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
597
598
599
600 /*-----------------------------------------------------------------*/
601 /* printIvalType - generates ival for int/char                     */
602 /*-----------------------------------------------------------------*/
603 void print_idataType (FILE *of, symbol *sym, sym_link * type, initList * ilist)
604 {
605   value *val;
606   unsigned long ulval;
607
608   //fprintf(stderr, "%s\n",__FUNCTION__);
609
610   /* if initList is deep */
611   if (ilist->type == INIT_DEEP)
612     ilist = ilist->init.deep;
613
614   if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
615     werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
616   }
617
618   if (!(val = list2val (ilist))) {
619     // assuming a warning has been thrown
620     val=constVal("0");
621   }
622
623   if (val->type != type) {
624     val = valCastLiteral(type, floatFromVal(val));
625   }
626
627   if(val) 
628     ulval = (unsigned long) floatFromVal (val);
629   else
630     ulval =0;
631
632   switch (getSize (type)) {
633     case 1:
634         if(isprint(BYTE_IN_LONG(ulval, 0)))
635                 fprintf(of, "%s\tdata\t\"%c\"\n", sym->rname, (unsigned char)BYTE_IN_LONG(ulval, 0));
636         else
637                 fprintf(of, "%s\tdata\t0x%02x\n", sym->rname, (unsigned char)BYTE_IN_LONG(ulval, 0));
638 //      pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
639         break;
640
641     case 2:
642         fprintf(of, "%s\tdw\t0x%02x, 0x%02x\n", sym->rname, (unsigned int)(BYTE_IN_LONG(ulval, 0)),
643                                         (unsigned int)(BYTE_IN_LONG(ulval, 1)));
644 //      fprintf(of, "%s\tdata\t0x%04x\n", sym->rname, (unsigned int)BYTE_IN_LONG(ulval, 0) +
645 //                                      (unsigned int)(BYTE_IN_LONG(ulval, 1) << 8));
646         break;
647
648     case 4:
649         fprintf(of, "%s\tdw\t0x%04x,0x%04x\n", sym->rname, (unsigned int)(BYTE_IN_LONG(ulval, 0)
650                                         + (BYTE_IN_LONG(ulval, 1) << 8)),
651                                         (unsigned)(BYTE_IN_LONG(ulval, 2)
652                                         + (BYTE_IN_LONG(ulval, 3) << 8)));
653 //      pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
654 //      pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
655 //      pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,2))));
656 //      pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,3))));
657         break;
658   }
659 }
660
661
662 /*-----------------------------------------------------------------*/
663 /* print_idataChar - generates initital value for character array    */
664 /*-----------------------------------------------------------------*/
665 static int 
666 print_idataChar (FILE *of, symbol *sym, sym_link * type, initList * ilist, char *s)
667 {
668   value *val;
669   int remain;
670   char old_ch=0, *vchar;
671
672   // fprintf(stderr, "%s\n",__FUNCTION__);
673   if (!s)
674     {
675
676       val = list2val (ilist);
677       /* if the value is a character string  */
678       if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
679         {
680           if (!DCL_ELEM (type))
681             DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
682
683
684           fprintf(of, "%s\tdata\t", sym->rname);
685           vchar = SPEC_CVAL(val->etype).v_char;
686           for(remain=0; remain<DCL_ELEM(type); remain++) {
687                 if(!isprint(old_ch)) {
688                         if(isprint(vchar[remain]))fprintf(of, "%s\"%c", (remain?", ":""), vchar[remain]);
689                         else fprintf(of, "0x%02x", vchar[remain]);
690                 } else
691                         if(!isprint(vchar[remain]))fprintf(of, "\", 0x%02x", vchar[remain]);
692                         else fprintf(of, "%c", vchar[remain]);
693                 old_ch = vchar[ remain ];
694           }
695 //           fprintf(of, "0x%02x%s", SPEC_CVAL(val->etype).v_char[ remain ], (remain==DCL_ELEM(type)-1?"":","));
696                         
697
698           if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0)
699             {
700               while (remain--)
701                 {
702                   fprintf(of, "0x%02x%s", 0x00 /*SPEC_CVAL(val->etype).v_char[ remain ]*/ , (remain==DCL_ELEM(type)-1?"":","));
703                 }
704             }
705                 fprintf(of, "\n");
706           return 1;
707         }
708       else
709         return 0;
710     }
711   else {
712     fprintf(of, "%s\tdata\t", sym->rname);
713     vchar = s; old_ch = 0;
714     for(remain=0; remain<strlen(s); remain++) 
715       {
716         if(!isprint(old_ch)) {
717                 if(isprint(vchar[remain]))fprintf(of, "%s\"%c", (remain?", ":""), vchar[remain]);
718                 else fprintf(of, "0x%02x", vchar[remain]);
719         } else
720                 if(!isprint(vchar[remain]))fprintf(of, "\", 0x%02x", vchar[remain]);
721                 else fprintf(of, "%c", vchar[remain]);
722         old_ch = vchar[ remain ];
723 //      fprintf(of, "0x%02x%s", s[ remain ], (remain==strlen(s)-1?"":","));
724       }
725   }
726   return 1;
727 }
728
729 /*-----------------------------------------------------------------*/
730 /* print_idataArray - generates code for array initialization        */
731 /*-----------------------------------------------------------------*/
732 static void 
733 print_idataArray (FILE *of, symbol * sym, sym_link * type, initList * ilist)
734 {
735   initList *iloop;
736   int lcnt = 0, size = 0;
737
738   /* take care of the special   case  */
739   /* array of characters can be init  */
740   /* by a string                      */
741   if (IS_CHAR (type->next)) {
742 //    fprintf(stderr,"%s:%d - is_char\n",__FUNCTION__,__LINE__);
743     if (!IS_LITERAL(list2val(ilist)->etype)) {
744       werror (W_INIT_WRONG);
745       return;
746     }
747     if (print_idataChar (of, sym, type,
748                        (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
749                        SPEC_CVAL (sym->etype).v_char))
750       return;
751   }
752   /* not the special case             */
753   if (ilist->type != INIT_DEEP)
754     {
755       werror (E_INIT_STRUCT, sym->name);
756       return;
757     }
758
759   iloop = ilist->init.deep;
760   lcnt = DCL_ELEM (type);
761
762   for (;;)
763     {
764       //fprintf(stderr,"%s:%d - is_char\n",__FUNCTION__,__LINE__);
765       size++;
766       print_idata (of, sym, type->next, iloop);
767       iloop = (iloop ? iloop->next : NULL);
768
769
770       /* if not array limits given & we */
771       /* are out of initialisers then   */
772       if (!DCL_ELEM (type) && !iloop)
773         break;
774
775       /* no of elements given and we    */
776       /* have generated for all of them */
777       if (!--lcnt) {
778         /* if initializers left */
779         if (iloop) {
780           werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
781         }
782         break;
783       }
784     }
785
786   /* if we have not been given a size  */
787   if (!DCL_ELEM (type))
788     DCL_ELEM (type) = size;
789
790   return;
791 }
792
793 /*-----------------------------------------------------------------*/
794 /* print_idata - generates code for initial value                    */
795 /*-----------------------------------------------------------------*/
796 void print_idata(FILE *of, symbol * sym, sym_link * type, initList * ilist)
797 {
798   if (!ilist)
799     return;
800
801   /* if structure then    */
802   if (IS_STRUCT (type))
803     {
804       //fprintf(stderr,"%s struct\n",__FUNCTION__);
805       //printIvalStruct (sym, type, ilist, oFile);
806       fprintf(stderr, "%s:%d: PIC16 port error: structure initialisation is not implemented yet.\n",
807         __FILE__, __LINE__);
808       abort();
809       return;
810     }
811
812   /* if this is a pointer */
813   if (IS_PTR (type))
814     {
815       //fprintf(stderr,"%s pointer\n",__FUNCTION__);
816       //printIvalPtr (sym, type, ilist, oFile);
817       fprintf(stderr, "%s:%d: PIC16 port error: pointer initialisation is not immplemented yet.\n",
818         __FILE__, __LINE__);
819       abort();
820       return;
821     }
822
823   /* if this is an array   */
824   if (IS_ARRAY (type))
825     {
826       //fprintf(stderr,"%s array\n",__FUNCTION__);
827       print_idataArray (of, sym, type, ilist);
828       return;
829     }
830
831   /* if type is SPECIFIER */
832   if (IS_SPEC (type))
833     {
834 //      fprintf(stderr,"%s spec\n",__FUNCTION__);
835       print_idataType(of, sym, type, ilist);
836       return;
837     }
838 }
839
840
841
842
843 /*-----------------------------------------------------------------*
844  *  void pic16_list_valid_pics(int ncols, int list_alias)
845  *
846  * Print out a formatted list of valid PIC devices
847  *
848  * ncols - number of columns in the list.
849  *
850  * list_alias - if non-zero, print all of the supported aliases
851  *              for a device (e.g. F84, 16F84, etc...)
852  *-----------------------------------------------------------------*/
853 void pic16_list_valid_pics(int ncols, int list_alias)
854 {
855   int col,longest;
856   int i,j,k,l;
857
858   if(list_alias)
859     list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
860
861   /* decrement the column number if it's greater than zero */
862   ncols = (ncols > 1) ? ncols-1 : 4;
863
864   /* Find the device with the longest name */
865   for(i=0,longest=0; i<num_of_supported_PICS; i++) {
866     for(j=0; j<=list_alias; j++) {
867       k = strlen(Pics16[i].name[j]);
868       if(k>longest)
869         longest = k;
870     }
871   }
872
873   col = 0;
874
875   for(i=0;  i < num_of_supported_PICS; i++) {
876     j = 0;
877     do {
878
879       fprintf(stderr,"%s", Pics16[i].name[j]);
880       if(col<ncols) {
881         l = longest + 2 - strlen(Pics16[i].name[j]);
882         for(k=0; k<l; k++)
883           fputc(' ',stderr);
884
885         col++;
886
887       } else {
888         fputc('\n',stderr);
889         col = 0;
890       }
891
892     } while(++j<list_alias);
893
894   }
895   if(col != ncols)
896     fputc('\n',stderr);
897
898 }
899
900 /*-----------------------------------------------------------------*
901  *  
902  *-----------------------------------------------------------------*/
903 PIC16_device *pic16_find_device(char *name)
904 {
905
906   int i,j;
907
908   if(!name)
909     return NULL;
910
911   for(i = 0; i<num_of_supported_PICS; i++) {
912
913     for(j=0; j<PROCESSOR_NAMES; j++)
914       if(!STRCASECMP(Pics16[i].name[j], name) )
915         return &Pics16[i];
916   }
917
918   /* not found */
919   return NULL; 
920 }
921
922 /*-----------------------------------------------------------------*
923  *  
924  *-----------------------------------------------------------------*/
925 void pic16_init_pic(char *pic_type)
926 {
927         pic16 = pic16_find_device(pic_type);
928
929         if(!pic16) {
930                 if(pic_type)
931                         fprintf(stderr, "'%s' was not found.\n", pic_type);
932                 else
933                         fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
934
935                 fprintf(stderr,"Valid devices are:\n");
936
937                 pic16_list_valid_pics(4,0);
938                 exit(1);
939         }
940
941 //      printf("PIC processor found and initialized: %s\n", pic_type);
942         pic16_setMaxRAM( 0xfff  );
943 }
944
945 /*-----------------------------------------------------------------*
946  *  
947  *-----------------------------------------------------------------*/
948 int pic16_picIsInitialized(void)
949 {
950   if(pic16 && pic16->maxRAMaddress > 0)
951     return 1;
952
953   return 0;
954
955 }
956
957 /*-----------------------------------------------------------------*
958  *  char *pic16_processor_base_name(void) - Include file is derived from this.
959  *-----------------------------------------------------------------*/
960 char *pic16_processor_base_name(void)
961 {
962
963   if(!pic16)
964     return NULL;
965
966   return pic16->name[0];
967 }
968
969 #define DEBUG_CHECK     0
970
971 /*
972  * return 1 if register wasn't found and added, 0 otherwise
973  */
974 int checkAddReg(set **set, regs *reg)
975 {
976   regs *tmp;
977
978 #if DEBUG_CHECK
979         fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
980 #endif
981
982         for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
983                 if(!strcmp(tmp->name, reg->name))break;
984         }
985         
986         if(!tmp) {
987                 addSet(set, reg);
988 #if DEBUG_CHECK
989                 fprintf(stderr, "added\n");
990 #endif
991                 return 1;
992         }
993
994 #if DEBUG_CHECK
995         fprintf(stderr, "already added\n");
996 #endif
997   return 0;
998 }
999
1000 int checkAddSym(set **set, symbol *sym)
1001 {
1002   symbol *tmp;
1003
1004 #if DEBUG_CHECK
1005         fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
1006 #endif
1007
1008         for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
1009                 if(!strcmp(tmp->name, sym->name))break;
1010         }
1011         
1012         if(!tmp) {
1013                 addSet(set, sym);
1014 #if DEBUG_CHECK
1015                 fprintf(stderr, "added\n");
1016 #endif
1017                 return 1;
1018         }
1019
1020 #if DEBUG_CHECK
1021         fprintf(stderr, "already added\n");
1022 #endif
1023
1024   return 0;
1025 }
1026
1027
1028 /*-----------------------------------------------------------------*
1029  * void pic16_groupRegistersInSection - add each register to its   *
1030  *      corresponding section                                      *
1031  *-----------------------------------------------------------------*/
1032 void pic16_groupRegistersInSection(set *regset)
1033 {
1034   regs *reg;
1035
1036         for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
1037
1038 //              fprintf(stderr, "%s:%d group registers in section, reg: %s\n", __FILE__, __LINE__, reg->name);
1039
1040                 if(reg->wasUsed
1041                         && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
1042                         
1043                         /* avoid grouping registers that have an initial value,
1044                          * they will be added later in idataSymSet */
1045                         if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
1046                                 continue;
1047
1048 #if 0
1049                         fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i\n",
1050                                 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
1051                                         (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
1052                                         (reg->regop?(OP_SYMBOL(reg->regop)->level):-1) );
1053 #endif
1054
1055                         if(reg->alias) {
1056                                 checkAddReg(&pic16_equ_data, reg);
1057                         } else
1058                         if(reg->isFixed) {
1059                                 checkAddReg(&pic16_fix_udata, reg);
1060                         } else
1061                         if(!reg->isFixed) {
1062                                 if(reg->pc_type == PO_GPR_TEMP)
1063                                         checkAddReg(&pic16_int_regs, reg);
1064                                 else
1065                                         checkAddReg(&pic16_rel_udata, reg);
1066                         }
1067                 }
1068         }
1069 }
1070
1071
1072
1073
1074
1075 /*-----------------------------------------------------------------*
1076  *  void pic16_assignConfigWordValue(int address, int value)
1077  *
1078  * All high performance RISC CPU PICs have seven config word starting
1079  * at address 0x300000.
1080  * This routine will assign a value to that address.
1081  *
1082  *-----------------------------------------------------------------*/
1083 void pic16_assignConfigWordValue(int address, int value)
1084 {
1085   int i;
1086
1087         for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
1088                 if(pic16->cwInfo.crInfo[i].mask != -1) {
1089 //                      fprintf(stderr, "setting location 0x%X\n", pic16->cwInfo.confAddrStart+i);
1090                         pic16->cwInfo.crInfo[i].value = value;
1091                         pic16->cwInfo.crInfo[i].emit = 1;
1092                         return;
1093                 }
1094         }
1095 }
1096