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