* device/lib/Makefile.in: fixed bug in target objects-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 "main.h"
39 #include "pcode.h"
40 #include "ralloc.h"
41 #include "device.h"
42
43
44 static PIC16_device Pics16[] = {
45 //  {
46 //    {"p18f242", "18f242", "pic18f242", "f242"},               // aliases
47 //    0,
48 //    0x300,                                            // RAMsize
49 //    0,
50 //    { 0xf80, 0xfff },       /* PIC18F242 range of SFR's */
51 //      {
52 //              /* PIC18F242 configuration words */
53 //              0x300001,
54 //              0x30000d,
55 //              { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
56 //              { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
57 //              { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
58 //              { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
59 //              { 0x40, 0, 0xff } /* d */ } 
60 //      }
61 //  },
62
63   {
64     {"p18f242", "18f242", "pic18f242", "f242"},
65     0,
66     0x300,      /* 768 */
67     0x80,
68     0,
69     { 0xf80, 0xfff },   /* PIC18F242 range of SFR's */
70     {
71     /* PIC18F242 configuration words */
72       0x300001,
73       0x30000d,
74       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
75       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
76       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
77       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
78       { 0x40, 0, 0xff } /* d */  }
79     }
80   },
81   {
82     {"p18f248", "18f248", "pic18f248", "f248"},
83     0,
84     0x300,      /* 768 */
85     0x60,
86     0,
87     { 0xf00, 0xfff },   /* PIC18F248 range of SFR's */
88     {
89     /* PIC18F248 configuration words */
90       0x300001,
91       0x30000d,
92       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
93       { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
94       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
95       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
96       { 0x40, 0, 0xff } /* d */  }
97     }
98   },
99   {
100     {"p18f252", "18f252", "pic18f252", "f252"},
101     0,
102     0x600,      /* 1536 */
103     0x80,
104     0,
105     { 0xf80, 0xfff },   /* PIC18F252 range of SFR's */
106     {
107     /* PIC18F252 configuration words */
108       0x300001,
109       0x30000d,
110       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
111       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
112       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
113       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
114       { 0x40, 0, 0xff } /* d */  }
115     }
116   },
117   {
118     {"p18f258", "18f258", "pic18f258", "f258"},
119     0,
120     0x600,      /* 1536 */
121     0x60,
122     0,
123     { 0xf00, 0xfff },   /* PIC18F258 range of SFR's */
124     {
125     /* PIC18F258 configuration words */
126       0x300001,
127       0x30000d,
128       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
129       { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
130       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
131       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
132       { 0x40, 0, 0xff } /* d */  }
133     }
134   },
135   {
136     {"p18f442", "18f442", "pic18f442", "f442"},
137     0,
138     0x300,      /* 768 */
139     0x80,
140     0,
141     { 0xf80, 0xfff },   /* PIC18F442 range of SFR's */
142     {
143     /* PIC18F442 configuration words */
144       0x300001,
145       0x30000d,
146       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
147       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
148       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
149       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
150       { 0x40, 0, 0xff } /* d */  }
151     }
152   },
153   {
154     {"p18f448", "18f448", "pic18f448", "f448"},
155     0,
156     0x300,      /* 768 */
157     0x60,
158     0,
159     { 0xf00, 0xfff },   /* PIC18F448 range of SFR's */
160     {
161     /* PIC18F448 configuration words */
162       0x300001,
163       0x30000d,
164       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
165       { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
166       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
167       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
168       { 0x40, 0, 0xff } /* d */  }
169     }
170   },
171   {
172     {"p18f452", "18f452", "pic18f452", "f452"},
173     0,
174     0x600,      /* 1536 */
175     0x80,
176     0,
177     { 0xf80, 0xfff },   /* PIC18F452 range of SFR's */
178     {
179     /* PIC18F452 configuration words */
180       0x300001,
181       0x30000d,
182       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
183       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
184       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
185       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
186       { 0x40, 0, 0xff } /* d */  }
187     }
188   },
189   {
190     {"p18f458", "18f458", "pic18f458", "f458"},
191     0,
192     0x600,      /* 1536 */
193     0x60,
194     0,
195     { 0xf00, 0xfff },   /* PIC18F458 range of SFR's */
196     {
197     /* PIC18F458 configuration words */
198       0x300001,
199       0x30000d,
200       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
201       { -1, 0, 0xff } /* 4 */  , { -1, 0, 0xff } /* 5 */  , { 0x85, 0, 0xff } /* 6 */ , 
202       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
203       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
204       { 0x40, 0, 0xff } /* d */  }
205     }
206   },
207   {
208     {"p18f1220", "18f1220", "pic18f1220", "f1220"},
209     0,
210     0x100,      /* 256 */
211     0x80,
212     0,
213     { 0xf80, 0xfff },   /* PIC18F1220 range of SFR's */
214     {
215     /* PIC18F1220 configuration words */
216       0x300001,
217       0x30000d,
218       { { 0xcf, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
219       { -1, 0, 0xff } /* 4 */  , { 0x80, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
220       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
221       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
222       { 0x40, 0, 0xff } /* d */  }
223     }
224   },
225   {
226     {"p18f2220", "18f2220", "pic18f2220", "f2220"},
227     0,
228     0x200,      /* 512 */
229     0x80,
230     0,
231     { 0xf80, 0xfff },   /* PIC18F2220 range of SFR's */
232     {
233     /* PIC18F2220 configuration words */
234       0x300001,
235       0x30000d,
236       { { 0xcf, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
237       { -1, 0, 0xff } /* 4 */  , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
238       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
239       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
240       { 0x40, 0, 0xff } /* d */  }
241     }
242   },
243   {
244     {"p18f6520", "18f6520", "pic18f6520", "f6520"},
245     0,
246     0x800,      /* 2048 */
247     0x60,
248     0,
249     { 0xf6b, 0xfff },   /* PIC18F6520 range of SFR's */
250     {
251     /* PIC18F6520 configuration words */
252       0x300001,
253       0x30000d,
254       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
255       { 0x80, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */  , 
256       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
257       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
258       { 0x40, 0, 0xff } /* d */  }
259     }
260   },
261   {
262     {"p18f6620", "18f6620", "pic18f6620", "f6620"},
263     0,
264     0xf00,      /* 3840 */
265     0x60,
266     0,
267     { 0xf6b, 0xfff },   /* PIC18F6620 range of SFR's */
268     {
269     /* PIC18F6620 configuration words */
270       0x300001,
271       0x30000d,
272       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
273       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
274       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
275       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
276       { 0x40, 0, 0xff } /* d */  }
277     }
278   },
279   {
280     {"p18f6680", "18f6680", "pic18f6680", "f6680"},
281     0,
282     0xf00,      /* 3840 */
283     0x60,
284     0,
285     { 0xf00, 0xfff },   /* PIC18F6680 range of SFR's */
286     {
287     /* PIC18F6680 configuration words */
288       0x300001,
289       0x30000d,
290       { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
291       { -1, 0, 0xff } /* 4 */  , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
292       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
293       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
294       { 0x40, 0, 0xff } /* d */  }
295     }
296   },
297   {
298     {"p18f6720", "18f6720", "pic18f6720", "f6720"},
299     0,
300     0xf00,      /* 3840 */
301     0x60,
302     0,
303     { 0xf6b, 0xfff },   /* PIC18F6720 range of SFR's */
304     {
305     /* PIC18F6720 configuration words */
306       0x300001,
307       0x30000d,
308       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
309       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
310       { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
311       { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
312       { 0x40, 0, 0xff } /* d */  }
313     }
314   },
315   {
316     {"p18f8520", "18f8520", "pic18f8520", "f8520"},
317     0,
318     0x800,      /* 2048 */
319     0x60,
320     1,
321     { 0xf6b, 0xfff },   /* PIC18F8520 range of SFR's */
322     {
323     /* PIC18F8520 configuration words */
324       0x300001,
325       0x30000d,
326       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
327       { 0x83, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */  , 
328       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
329       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
330       { 0x40, 0, 0xff } /* d */  }
331     }
332   },
333   {
334     {"p18f8620", "18f8620", "pic18f8620", "f8620"},
335     0,
336     0xf00,      /* 3840 */
337     0x60,
338     1,
339     { 0xf6b, 0xfff },   /* PIC18F8620 range of SFR's */
340     {
341     /* PIC18F8620 configuration words */
342       0x300001,
343       0x30000d,
344       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
345       { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
346       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
347       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
348       { 0x40, 0, 0xff } /* d */  }
349     }
350   },
351   {
352     {"p18f8680", "18f8680", "pic18f8680", "f8680"},
353     0,
354     0xf00,      /* 3840 */
355     0x60,
356     0,
357     { 0xf00, 0xfff },   /* PIC18F8680 range of SFR's */
358     {
359     /* PIC18F8680 configuration words */
360       0x300001,
361       0x30000d,
362       { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
363       { 0x83, 0, 0xff } /* 4 */ , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
364       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
365       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
366       { 0x40, 0, 0xff } /* d */  }
367     }
368   },
369   {
370     {"p18f8720", "18f8720", "pic18f8720", "f8720"},
371     0,
372     0xf00,      /* 3840 */
373     0x60,
374     1,
375     { 0xf6b, 0xfff },   /* PIC18F8720 range of SFR's */
376     {
377     /* PIC18F8720 configuration words */
378       0x300001,
379       0x30000d,
380       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
381       { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
382       { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
383       { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
384       { 0x40, 0, 0xff } /* d */  }
385     }
386   }
387
388 };
389
390 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
391
392 #define DEFAULT_PIC "452"
393
394 PIC16_device *pic16=NULL;
395 unsigned int stackPos = 0;
396
397 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
398
399 void pic16_setMaxRAM(int size)
400 {
401         pic16->maxRAMaddress = size;
402         stackPos = pic16->RAMsize-1;
403
404         if (pic16->maxRAMaddress < 0) {
405                 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
406                         pic16->maxRAMaddress);
407           return;
408         }
409 }
410
411 extern char *iComments2;
412
413 void pic16_dump_equates(FILE *of, set *equs)
414 {
415   regs *r;
416
417         r = setFirstItem(equs);
418         if(!r)return;
419         
420         fprintf(of, "%s", iComments2);
421         fprintf(of, ";\tEquates to used internal registers\n");
422         fprintf(of, "%s", iComments2);
423         
424         for(; r; r = setNextItem(equs)) {
425                 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
426         }
427 }
428
429
430 int regCompare(const void *a, const void *b)
431 {
432   const regs *const *i = a;
433   const regs *const *j = b;
434
435         /* sort primarily by the address */
436         if( (*i)->address > (*j)->address)return 1;
437         if( (*i)->address < (*j)->address)return -1;
438         
439         /* and secondarily by size */
440         /* register size sorting may have strange results use with care */
441         if( (*i)->size > (*j)->size)return 1;
442         if( (*i)->size < (*j)->size)return -1;
443         
444         /* finally if in same address and same size sort by name */
445         return (strcmp( (*i)->name, (*j)->name));
446
447   return 0;
448 }
449
450 int symCompare(const void *a, const void *b)
451 {
452   const symbol *const *i = a;
453   const symbol *const *j = b;
454
455         /* sort primarily by the address */
456         if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
457         if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
458         
459         /* and secondarily by size */
460         /* register size sorting may have strange results use with care */
461         if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
462         if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
463
464         /* finally if in same address and same size sort by name */
465         return (strcmp( (*i)->rname, (*j)->rname));
466
467   return 0;
468 }
469
470 void pic16_dump_usection(FILE *of, set *section, int fix)
471 {
472   static int abs_usection_no=0;
473   regs *r, *rprev;
474   int init_addr, i;
475   regs **rlist;
476   regs *r1;
477
478         /* put all symbols in an array */
479         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
480         r = rlist[0]; i = 0;
481         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
482                 rlist[i] = rprev; i++;
483         }
484         
485         if(!i) {
486                 if(rlist)free(rlist);
487           return;
488         }
489
490         /* sort symbols according to their address */
491         qsort(rlist, i  /*elementsInSet(section)*/, sizeof(regs *), regCompare);
492         
493         if(!fix) {
494                 fprintf(of, "\n\n\tudata\n");
495                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
496                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
497                 }
498         } else {
499           int j=0;
500           int deb_addr=0;
501
502                 rprev = NULL;
503                 init_addr = rlist[j]->address;
504                 deb_addr = init_addr;
505                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
506         
507                 for(j=0;j<i;j++) {
508                         r = rlist[j];
509                         if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
510                         
511                         init_addr = r->address;
512                         deb_addr = init_addr;
513                         
514                         if((rprev && (init_addr > (rprev->address + rprev->size)))) {
515                                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
516                         }
517
518                         if(r1 && (init_addr == r1->address)) {
519                                 fprintf(of, "\n%s\tres\t0\n", r->name);
520                         } else {
521                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
522                                 deb_addr += r->size;
523                         }
524                         
525                         rprev = r;
526                 }
527         }
528         free(rlist);
529 }
530
531 void pic16_dump_gsection(FILE *of, set *sections)
532 {
533   regs *r;
534   sectName *sname;
535
536         for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
537                 fprintf(of, "\n\n%s\tudata\n", sname->name);
538
539                 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
540 #if 0
541                         fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
542                                 r->name, sname->name, sname);
543 #endif
544                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
545                 }
546         }
547 }
548
549
550 /* forward declaration */
551 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
552 extern void pic16_pCodeConstString(char *name, char *value);
553
554 void pic16_dump_isection(FILE *of, set *section, int fix)
555 {
556   static int abs_isection_no=0;
557   symbol *s, *sprev;
558   int init_addr, i;
559   symbol **slist;
560
561         /* put all symbols in an array */
562         slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
563         s = slist[0]; i = 0;
564         for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
565                 slist[i] = sprev; i++;
566         }
567         
568         if(!i) {
569                 if(slist)free(slist);
570           return;
571         }
572
573         /* sort symbols according to their address */
574         qsort(slist, i, sizeof(symbol *), symCompare);
575         
576         pic16_initDB();
577
578         if(!fix) {
579                 fprintf(of, "\n\n\tidata\n");
580                 for(s = setFirstItem(section); s; s = setNextItem(section)) {
581
582                         if(s->ival) {
583                                 fprintf(of, "%s", s->rname);
584                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
585                                 pic16_flushDB('f', (void *)of);
586                         } else {
587                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
588                                         && SPEC_CVAL (s->etype).v_char) {
589
590 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
591                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
592                                 } else {
593                                         assert(0);
594                                 }
595                         }
596                                 
597                 }
598         } else {
599           int j=0;
600           symbol *s1;
601           
602                 sprev = NULL;
603                 init_addr = SPEC_ADDR(slist[j]->etype);
604                 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
605         
606                 for(j=0;j<i;j++) {
607                         s = slist[j];
608                         if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
609                         
610                         init_addr = SPEC_ADDR(s->etype);
611
612                         if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
613                                 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
614                         }
615
616                         if(s->ival) {
617                                 fprintf(of, "%s", s->rname);
618                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
619                                 pic16_flushDB('f', (void *)of);
620                         } else {
621                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
622                                         && SPEC_CVAL (s->etype).v_char) {
623
624 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
625                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
626                                 } else {
627                                         assert(0);
628                                 }
629                         }
630
631
632                         sprev = s;
633                 }
634         }
635         free(slist);
636 }
637
638
639 void pic16_dump_int_registers(FILE *of, set *section)
640 {
641   regs *r, *rprev;
642   int i;
643   regs **rlist;
644
645         /* put all symbols in an array */
646         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
647         r = rlist[0]; i = 0;
648         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
649                 rlist[i] = rprev; i++;
650         }
651
652         /* sort symbols according to their address */
653         qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
654         
655         if(!i) {
656                 if(rlist)free(rlist);
657           return;
658         }
659         
660         fprintf(of, "\n\n; Internal registers\n");
661         
662         fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
663         for(r = setFirstItem(section); r; r = setNextItem(section))
664                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
665
666         free(rlist);
667 }
668
669
670 #ifdef WORDS_BIGENDIAN
671   #define _ENDIAN(x)  (3-x)
672 #else
673   #define _ENDIAN(x)  (x)
674 #endif
675
676 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
677
678
679 /*-----------------------------------------------------------------*
680  *  void pic16_list_valid_pics(int ncols, int list_alias)
681  *
682  * Print out a formatted list of valid PIC devices
683  *
684  * ncols - number of columns in the list.
685  *
686  * list_alias - if non-zero, print all of the supported aliases
687  *              for a device (e.g. F84, 16F84, etc...)
688  *-----------------------------------------------------------------*/
689 void pic16_list_valid_pics(int ncols, int list_alias)
690 {
691   int col,longest;
692   int i,j,k,l;
693
694   if(list_alias)
695     list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
696
697   /* decrement the column number if it's greater than zero */
698   ncols = (ncols > 1) ? ncols-1 : 4;
699
700   /* Find the device with the longest name */
701   for(i=0,longest=0; i<num_of_supported_PICS; i++) {
702     for(j=0; j<=list_alias; j++) {
703       k = strlen(Pics16[i].name[j]);
704       if(k>longest)
705         longest = k;
706     }
707   }
708
709   col = 0;
710
711   for(i=0;  i < num_of_supported_PICS; i++) {
712     j = 0;
713     do {
714
715       fprintf(stderr,"%s", Pics16[i].name[j]);
716       if(col<ncols) {
717         l = longest + 2 - strlen(Pics16[i].name[j]);
718         for(k=0; k<l; k++)
719           fputc(' ',stderr);
720
721         col++;
722
723       } else {
724         fputc('\n',stderr);
725         col = 0;
726       }
727
728     } while(++j<list_alias);
729
730   }
731   if(col != ncols)
732     fputc('\n',stderr);
733
734 }
735
736 /*-----------------------------------------------------------------*
737  *  
738  *-----------------------------------------------------------------*/
739 PIC16_device *pic16_find_device(char *name)
740 {
741
742   int i,j;
743
744   if(!name)
745     return NULL;
746
747   for(i = 0; i<num_of_supported_PICS; i++) {
748
749     for(j=0; j<PROCESSOR_NAMES; j++)
750       if(!STRCASECMP(Pics16[i].name[j], name) )
751         return &Pics16[i];
752   }
753
754   /* not found */
755   return NULL; 
756 }
757
758 /*-----------------------------------------------------------------*
759  *  
760  *-----------------------------------------------------------------*/
761 void pic16_init_pic(char *pic_type)
762 {
763         pic16 = pic16_find_device(pic_type);
764
765         if(!pic16) {
766                 if(pic_type)
767                         fprintf(stderr, "'%s' was not found.\n", pic_type);
768                 else
769                         fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
770
771                 fprintf(stderr,"Valid devices are:\n");
772
773                 pic16_list_valid_pics(4,0);
774                 exit(1);
775         }
776
777 //      printf("PIC processor found and initialized: %s\n", pic_type);
778         pic16_setMaxRAM( 0xfff  );
779 }
780
781 /*-----------------------------------------------------------------*
782  *  
783  *-----------------------------------------------------------------*/
784 int pic16_picIsInitialized(void)
785 {
786   if(pic16 && pic16->maxRAMaddress > 0)
787     return 1;
788
789   return 0;
790
791 }
792
793 /*-----------------------------------------------------------------*
794  *  char *pic16_processor_base_name(void) - Include file is derived from this.
795  *-----------------------------------------------------------------*/
796 char *pic16_processor_base_name(void)
797 {
798
799   if(!pic16)
800     return NULL;
801
802   return pic16->name[0];
803 }
804
805 #define DEBUG_CHECK     0
806
807 /*
808  * return 1 if register wasn't found and added, 0 otherwise
809  */
810 int checkAddReg(set **set, regs *reg)
811 {
812   regs *tmp;
813
814 #if DEBUG_CHECK
815         fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
816 #endif
817
818         for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
819                 if(!strcmp(tmp->name, reg->name))break;
820         }
821         
822         if(!tmp) {
823                 addSet(set, reg);
824 #if DEBUG_CHECK
825                 fprintf(stderr, "added\n");
826 #endif
827                 return 1;
828         }
829
830 #if DEBUG_CHECK
831         fprintf(stderr, "already added\n");
832 #endif
833   return 0;
834 }
835
836 int checkAddSym(set **set, symbol *sym)
837 {
838   symbol *tmp;
839
840 #if DEBUG_CHECK
841         fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
842 #endif
843
844         for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
845                 if(!strcmp(tmp->name, sym->name))break;
846         }
847         
848         if(!tmp) {
849                 addSet(set, sym);
850 #if DEBUG_CHECK
851                 fprintf(stderr, "added\n");
852 #endif
853                 return 1;
854         }
855
856 #if DEBUG_CHECK
857         fprintf(stderr, "already added\n");
858 #endif
859
860   return 0;
861 }
862
863
864 /*-----------------------------------------------------------------*
865  * void pic16_groupRegistersInSection - add each register to its   *
866  *      corresponding section                                      *
867  *-----------------------------------------------------------------*/
868 void pic16_groupRegistersInSection(set *regset)
869 {
870   regs *reg;
871   sectSym *ssym;
872   int docontinue=0;
873
874         for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
875
876 //              fprintf(stderr, "%s:%d group registers in section, reg: %s\n", __FILE__, __LINE__, reg->name);
877
878                 if(reg->wasUsed
879                         && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
880                         
881                         /* avoid grouping registers that have an initial value,
882                          * they will be added later in idataSymSet */
883                         if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
884                                 continue;
885
886 #if 0
887                         fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i\n",
888                                 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
889                                         (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
890                                         (reg->regop?(OP_SYMBOL(reg->regop)->level):-1) );
891 #endif
892                         
893                         docontinue=0;
894                         for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
895                                 if(!strcmp(ssym->name, reg->name)) {
896 //                                      fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
897 //                                                      __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
898                                         addSet(&ssym->section->regsSet, reg);
899                                         docontinue=1;
900                                 }
901                         }
902
903                         if(docontinue)continue;
904
905 //                      fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
906
907                         if(reg->alias == 0x80) {
908                                 checkAddReg(&pic16_equ_data, reg);
909                         } else
910                         if(reg->isFixed) {
911                                 checkAddReg(&pic16_fix_udata, reg);
912                         } else
913                         if(!reg->isFixed) {
914                                 if(reg->pc_type == PO_GPR_TEMP)
915                                         checkAddReg(&pic16_int_regs, reg);
916                                 else
917                                         checkAddReg(&pic16_rel_udata, reg);
918                         }
919                 }
920         }
921 }
922
923
924
925
926
927 /*-----------------------------------------------------------------*
928  *  void pic16_assignConfigWordValue(int address, int value)
929  *
930  * All high performance RISC CPU PICs have seven config word starting
931  * at address 0x300000.
932  * This routine will assign a value to that address.
933  *
934  *-----------------------------------------------------------------*/
935 void pic16_assignConfigWordValue(int address, int value)
936 {
937   int i;
938
939         for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
940                 if((address == pic16->cwInfo.confAddrStart+i)
941                   && (pic16->cwInfo.crInfo[i].mask != -1)) {
942 //                        fprintf(stderr, "setting location 0x%X to value 0x%x\n", /*address*/ pic16->cwInfo.confAddrStart+i, val
943                         pic16->cwInfo.crInfo[i].value = value;
944                         pic16->cwInfo.crInfo[i].emit = 1;
945                         return;
946                 }
947         }
948 }
949