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