* device/include/pic16/pic18fregs.h: added case for pic18f2220.h
[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     {"p18f2220", "18f2220", "pic18f2220", "f2220"},
226     0,
227     0x200,      /* 512 */
228     0x80,
229     0,
230     { 0xf80, 0xfff },   /* PIC18F2220 range of SFR's */
231     {
232     /* PIC18F2220 configuration words */
233       0x300001,
234       0x30000d,
235       { { 0xcf, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
236       { -1, 0, 0xff } /* 4 */  , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
237       { -1, 0, 0xff } /* 7 */  , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
238       { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ , 
239       { 0x40, 0, 0xff } /* d */  }
240     }
241   },
242   {
243     {"p18f6520", "18f6520", "pic18f6520", "f6520"},
244     0,
245     0x800,      /* 2048 */
246     0x60,
247     0,
248     { 0xf6b, 0xfff },   /* PIC18F6520 range of SFR's */
249     {
250     /* PIC18F6520 configuration words */
251       0x300001,
252       0x30000d,
253       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
254       { 0x80, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 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     {"p18f6620", "18f6620", "pic18f6620", "f6620"},
262     0,
263     0xf00,      /* 3840 */
264     0x60,
265     0,
266     { 0xf6b, 0xfff },   /* PIC18F6620 range of SFR's */
267     {
268     /* PIC18F6620 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 */  , { 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     {"p18f6680", "18f6680", "pic18f6680", "f6680"},
280     0,
281     0xf00,      /* 3840 */
282     0x60,
283     0,
284     { 0xf00, 0xfff },   /* PIC18F6680 range of SFR's */
285     {
286     /* PIC18F6680 configuration words */
287       0x300001,
288       0x30000d,
289       { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
290       { -1, 0, 0xff } /* 4 */  , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 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     {"p18f6720", "18f6720", "pic18f6720", "f6720"},
298     0,
299     0xf00,      /* 3840 */
300     0x60,
301     0,
302     { 0xf6b, 0xfff },   /* PIC18F6720 range of SFR's */
303     {
304     /* PIC18F6720 configuration words */
305       0x300001,
306       0x30000d,
307       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
308       { -1, 0, 0xff } /* 4 */  , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
309       { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
310       { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
311       { 0x40, 0, 0xff } /* d */  }
312     }
313   },
314   {
315     {"p18f8520", "18f8520", "pic18f8520", "f8520"},
316     0,
317     0x800,      /* 2048 */
318     0x60,
319     1,
320     { 0xf6b, 0xfff },   /* PIC18F8520 range of SFR's */
321     {
322     /* PIC18F8520 configuration words */
323       0x300001,
324       0x30000d,
325       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
326       { 0x83, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 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     {"p18f8620", "18f8620", "pic18f8620", "f8620"},
334     0,
335     0xf00,      /* 3840 */
336     0x60,
337     1,
338     { 0xf6b, 0xfff },   /* PIC18F8620 range of SFR's */
339     {
340     /* PIC18F8620 configuration words */
341       0x300001,
342       0x30000d,
343       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
344       { 0x83, 0, 0xff } /* 4 */ , { 0x01, 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     {"p18f8680", "18f8680", "pic18f8680", "f8680"},
352     0,
353     0xf00,      /* 3840 */
354     0x60,
355     0,
356     { 0xf00, 0xfff },   /* PIC18F8680 range of SFR's */
357     {
358     /* PIC18F8680 configuration words */
359       0x300001,
360       0x30000d,
361       { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ , 
362       { 0x83, 0, 0xff } /* 4 */ , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
363       { -1, 0, 0xff } /* 7 */  , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
364       { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ , 
365       { 0x40, 0, 0xff } /* d */  }
366     }
367   },
368   {
369     {"p18f8720", "18f8720", "pic18f8720", "f8720"},
370     0,
371     0xf00,      /* 3840 */
372     0x60,
373     1,
374     { 0xf6b, 0xfff },   /* PIC18F8720 range of SFR's */
375     {
376     /* PIC18F8720 configuration words */
377       0x300001,
378       0x30000d,
379       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ , 
380       { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ , 
381       { -1, 0, 0xff } /* 7 */  , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ , 
382       { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ , 
383       { 0x40, 0, 0xff } /* d */  }
384     }
385   },
386
387 };
388
389 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
390
391 #define DEFAULT_PIC "452"
392
393 PIC16_device *pic16=NULL;
394 unsigned int stackPos = 0;
395
396 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
397
398 void pic16_setMaxRAM(int size)
399 {
400         pic16->maxRAMaddress = size;
401         stackPos = pic16->RAMsize-1;
402
403         if (pic16->maxRAMaddress < 0) {
404                 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
405                         pic16->maxRAMaddress);
406           return;
407         }
408 }
409
410 extern char *iComments2;
411
412 void pic16_dump_equates(FILE *of, set *equs)
413 {
414   regs *r;
415
416         r = setFirstItem(equs);
417         if(!r)return;
418         
419         fprintf(of, "%s", iComments2);
420         fprintf(of, ";\tEquates to used internal registers\n");
421         fprintf(of, "%s", iComments2);
422         
423         for(; r; r = setNextItem(equs)) {
424                 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
425         }
426 }
427
428
429 int regCompare(const void *a, const void *b)
430 {
431   const regs *const *i = a;
432   const regs *const *j = b;
433
434         /* sort primarily by the address */
435         if( (*i)->address > (*j)->address)return 1;
436         if( (*i)->address < (*j)->address)return -1;
437         
438         /* and secondarily by size */
439         /* register size sorting may have strange results use with care */
440         if( (*i)->size > (*j)->size)return 1;
441         if( (*i)->size < (*j)->size)return -1;
442         
443         /* finally if in same address and same size sort by name */
444         return (strcmp( (*i)->name, (*j)->name));
445
446   return 0;
447 }
448
449 int symCompare(const void *a, const void *b)
450 {
451   const symbol *const *i = a;
452   const symbol *const *j = b;
453
454         /* sort primarily by the address */
455         if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
456         if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
457         
458         /* and secondarily by size */
459         /* register size sorting may have strange results use with care */
460         if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
461         if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
462
463         /* finally if in same address and same size sort by name */
464         return (strcmp( (*i)->rname, (*j)->rname));
465
466   return 0;
467 }
468
469 void pic16_dump_usection(FILE *of, set *section, int fix)
470 {
471   static int abs_usection_no=0;
472   regs *r, *rprev;
473   int init_addr, i;
474   regs **rlist;
475   regs *r1;
476
477         /* put all symbols in an array */
478         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
479         r = rlist[0]; i = 0;
480         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
481                 rlist[i] = rprev; i++;
482         }
483         
484         if(!i) {
485                 if(rlist)free(rlist);
486           return;
487         }
488
489         /* sort symbols according to their address */
490         qsort(rlist, i  /*elementsInSet(section)*/, sizeof(regs *), regCompare);
491         
492         if(!fix) {
493                 fprintf(of, "\n\n\tudata\n");
494                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
495                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
496                 }
497         } else {
498           int j=0;
499           int deb_addr=0;
500
501                 rprev = NULL;
502                 init_addr = rlist[j]->address;
503                 deb_addr = init_addr;
504                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
505         
506                 for(j=0;j<i;j++) {
507                         r = rlist[j];
508                         if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
509                         
510                         init_addr = r->address;
511                         deb_addr = init_addr;
512                         
513                         if((rprev && (init_addr > (rprev->address + rprev->size)))) {
514                                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
515                         }
516
517                         if(r1 && (init_addr == r1->address)) {
518                                 fprintf(of, "\n%s\tres\t0\n", r->name);
519                         } else {
520                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
521                                 deb_addr += r->size;
522                         }
523                         
524                         rprev = r;
525                 }
526         }
527         free(rlist);
528 }
529
530
531 /* forward declaration */
532 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
533
534 void pic16_dump_isection(FILE *of, set *section, int fix)
535 {
536   static int abs_isection_no=0;
537   symbol *s, *sprev;
538   int init_addr, i;
539   symbol **slist;
540
541         /* put all symbols in an array */
542         slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
543         s = slist[0]; i = 0;
544         for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
545                 slist[i] = sprev; i++;
546         }
547         
548         if(!i) {
549                 if(slist)free(slist);
550           return;
551         }
552
553         /* sort symbols according to their address */
554         qsort(slist, i, sizeof(symbol *), symCompare);
555         
556         pic16_initDB();
557
558         if(!fix) {
559                 fprintf(of, "\n\n\tidata\n");
560                 for(s = setFirstItem(section); s; s = setNextItem(section)) {
561                         fprintf(of, "%s", s->rname);
562                         pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
563                         pic16_flushDB('f', (void *)of);
564                 }
565         } else {
566           int j=0;
567           symbol *s1;
568           
569                 sprev = NULL;
570                 init_addr = SPEC_ADDR(slist[j]->etype);
571                 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
572         
573                 for(j=0;j<i;j++) {
574                         s = slist[j];
575                         if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
576                         
577                         init_addr = SPEC_ADDR(s->etype);
578
579                         if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
580                                 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
581                         }
582
583                         fprintf(of, "%s", s->rname);
584                         pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
585                         pic16_flushDB('f', (void *)of);
586
587                         sprev = s;
588                 }
589         }
590         free(slist);
591 }
592
593
594 void pic16_dump_int_registers(FILE *of, set *section)
595 {
596   regs *r, *rprev;
597   int i;
598   regs **rlist;
599
600         /* put all symbols in an array */
601         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
602         r = rlist[0]; i = 0;
603         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
604                 rlist[i] = rprev; i++;
605         }
606
607         /* sort symbols according to their address */
608         qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
609         
610         if(!i) {
611                 if(rlist)free(rlist);
612           return;
613         }
614         
615         fprintf(of, "\n\n; Internal registers\n");
616         
617         fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
618         for(r = setFirstItem(section); r; r = setNextItem(section))
619                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
620
621         free(rlist);
622 }
623
624
625 #ifdef WORDS_BIGENDIAN
626   #define _ENDIAN(x)  (3-x)
627 #else
628   #define _ENDIAN(x)  (x)
629 #endif
630
631 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
632
633
634 /*-----------------------------------------------------------------*
635  *  void pic16_list_valid_pics(int ncols, int list_alias)
636  *
637  * Print out a formatted list of valid PIC devices
638  *
639  * ncols - number of columns in the list.
640  *
641  * list_alias - if non-zero, print all of the supported aliases
642  *              for a device (e.g. F84, 16F84, etc...)
643  *-----------------------------------------------------------------*/
644 void pic16_list_valid_pics(int ncols, int list_alias)
645 {
646   int col,longest;
647   int i,j,k,l;
648
649   if(list_alias)
650     list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
651
652   /* decrement the column number if it's greater than zero */
653   ncols = (ncols > 1) ? ncols-1 : 4;
654
655   /* Find the device with the longest name */
656   for(i=0,longest=0; i<num_of_supported_PICS; i++) {
657     for(j=0; j<=list_alias; j++) {
658       k = strlen(Pics16[i].name[j]);
659       if(k>longest)
660         longest = k;
661     }
662   }
663
664   col = 0;
665
666   for(i=0;  i < num_of_supported_PICS; i++) {
667     j = 0;
668     do {
669
670       fprintf(stderr,"%s", Pics16[i].name[j]);
671       if(col<ncols) {
672         l = longest + 2 - strlen(Pics16[i].name[j]);
673         for(k=0; k<l; k++)
674           fputc(' ',stderr);
675
676         col++;
677
678       } else {
679         fputc('\n',stderr);
680         col = 0;
681       }
682
683     } while(++j<list_alias);
684
685   }
686   if(col != ncols)
687     fputc('\n',stderr);
688
689 }
690
691 /*-----------------------------------------------------------------*
692  *  
693  *-----------------------------------------------------------------*/
694 PIC16_device *pic16_find_device(char *name)
695 {
696
697   int i,j;
698
699   if(!name)
700     return NULL;
701
702   for(i = 0; i<num_of_supported_PICS; i++) {
703
704     for(j=0; j<PROCESSOR_NAMES; j++)
705       if(!STRCASECMP(Pics16[i].name[j], name) )
706         return &Pics16[i];
707   }
708
709   /* not found */
710   return NULL; 
711 }
712
713 /*-----------------------------------------------------------------*
714  *  
715  *-----------------------------------------------------------------*/
716 void pic16_init_pic(char *pic_type)
717 {
718         pic16 = pic16_find_device(pic_type);
719
720         if(!pic16) {
721                 if(pic_type)
722                         fprintf(stderr, "'%s' was not found.\n", pic_type);
723                 else
724                         fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
725
726                 fprintf(stderr,"Valid devices are:\n");
727
728                 pic16_list_valid_pics(4,0);
729                 exit(1);
730         }
731
732 //      printf("PIC processor found and initialized: %s\n", pic_type);
733         pic16_setMaxRAM( 0xfff  );
734 }
735
736 /*-----------------------------------------------------------------*
737  *  
738  *-----------------------------------------------------------------*/
739 int pic16_picIsInitialized(void)
740 {
741   if(pic16 && pic16->maxRAMaddress > 0)
742     return 1;
743
744   return 0;
745
746 }
747
748 /*-----------------------------------------------------------------*
749  *  char *pic16_processor_base_name(void) - Include file is derived from this.
750  *-----------------------------------------------------------------*/
751 char *pic16_processor_base_name(void)
752 {
753
754   if(!pic16)
755     return NULL;
756
757   return pic16->name[0];
758 }
759
760 #define DEBUG_CHECK     0
761
762 /*
763  * return 1 if register wasn't found and added, 0 otherwise
764  */
765 int checkAddReg(set **set, regs *reg)
766 {
767   regs *tmp;
768
769 #if DEBUG_CHECK
770         fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
771 #endif
772
773         for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
774                 if(!strcmp(tmp->name, reg->name))break;
775         }
776         
777         if(!tmp) {
778                 addSet(set, reg);
779 #if DEBUG_CHECK
780                 fprintf(stderr, "added\n");
781 #endif
782                 return 1;
783         }
784
785 #if DEBUG_CHECK
786         fprintf(stderr, "already added\n");
787 #endif
788   return 0;
789 }
790
791 int checkAddSym(set **set, symbol *sym)
792 {
793   symbol *tmp;
794
795 #if DEBUG_CHECK
796         fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
797 #endif
798
799         for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
800                 if(!strcmp(tmp->name, sym->name))break;
801         }
802         
803         if(!tmp) {
804                 addSet(set, sym);
805 #if DEBUG_CHECK
806                 fprintf(stderr, "added\n");
807 #endif
808                 return 1;
809         }
810
811 #if DEBUG_CHECK
812         fprintf(stderr, "already added\n");
813 #endif
814
815   return 0;
816 }
817
818
819 /*-----------------------------------------------------------------*
820  * void pic16_groupRegistersInSection - add each register to its   *
821  *      corresponding section                                      *
822  *-----------------------------------------------------------------*/
823 void pic16_groupRegistersInSection(set *regset)
824 {
825   regs *reg;
826
827         for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
828
829 //              fprintf(stderr, "%s:%d group registers in section, reg: %s\n", __FILE__, __LINE__, reg->name);
830
831                 if(reg->wasUsed
832                         && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
833                         
834                         /* avoid grouping registers that have an initial value,
835                          * they will be added later in idataSymSet */
836                         if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
837                                 continue;
838
839 #if 0
840                         fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i\n",
841                                 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
842                                         (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
843                                         (reg->regop?(OP_SYMBOL(reg->regop)->level):-1) );
844 #endif
845
846                         if(reg->alias == 0x80) {
847                                 checkAddReg(&pic16_equ_data, reg);
848                         } else
849                         if(reg->isFixed) {
850                                 checkAddReg(&pic16_fix_udata, reg);
851                         } else
852                         if(!reg->isFixed) {
853                                 if(reg->pc_type == PO_GPR_TEMP)
854                                         checkAddReg(&pic16_int_regs, reg);
855                                 else
856                                         checkAddReg(&pic16_rel_udata, reg);
857                         }
858                 }
859         }
860 }
861
862
863
864
865
866 /*-----------------------------------------------------------------*
867  *  void pic16_assignConfigWordValue(int address, int value)
868  *
869  * All high performance RISC CPU PICs have seven config word starting
870  * at address 0x300000.
871  * This routine will assign a value to that address.
872  *
873  *-----------------------------------------------------------------*/
874 void pic16_assignConfigWordValue(int address, int value)
875 {
876   int i;
877
878         for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
879                 if((address == pic16->cwInfo.confAddrStart+i)
880                   && (pic16->cwInfo.crInfo[i].mask != -1)) {
881 //                        fprintf(stderr, "setting location 0x%X to value 0x%x\n", /*address*/ pic16->cwInfo.confAddrStart+i, val
882                         pic16->cwInfo.crInfo[i].value = value;
883                         pic16->cwInfo.crInfo[i].emit = 1;
884                         return;
885                 }
886         }
887 }
888