* src/pic16/devices.inc,
[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 stats_t statistics = { 0, 0, 0, 0 };
44
45 #define DEVICE_FILE_NAME    "pic16devices.txt"
46
47 static PIC16_device default_device = {
48     { "p18f452", "18f452", "pic18f452", "f452" },
49     0x600,
50     0x80,
51     { 0xf80, 0xfff },
52     { /* configuration words */
53       0x300001, 0x30000d,
54       { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ ,
55         { 0x0f, 0, 0xff } /* 3 */ , {  -1 , 0, 0xff } /* 4 */ ,
56         { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
57         {  -1 , 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ ,
58         { 0xc0, 0, 0xff } /* 9 */ , { 0x0f, 0, 0xff } /* a */ ,
59         { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
60         { 0x40, 0, 0xff } /* d */ }
61     },
62     { /* ID locations */
63       0x200000, 0x200007,
64       { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
65         { 0, 0 }, { 0, 0 }, { 0, 0 } }
66     },
67     NULL
68 };
69
70 PIC16_device *pic16 = &default_device;
71 static PIC16_device *devices = NULL;
72
73 extern set *includeDirsSet;
74 extern set *userIncDirsSet;
75
76 extern char *iComments2;
77
78 void pic16_dump_equates(FILE *of, set *equs)
79 {
80   regs *r;
81
82         r = setFirstItem(equs);
83         if(!r)return;
84         
85         fprintf(of, "%s", iComments2);
86         fprintf(of, ";\tEquates to used internal registers\n");
87         fprintf(of, "%s", iComments2);
88         
89         for(; r; r = setNextItem(equs)) {
90                 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
91         }
92 }
93
94
95 void pic16_dump_access(FILE *of, set *section)
96 {
97   regs *r;
98
99         r = setFirstItem(section);
100         if(!r)return;
101         
102         fprintf(of, "%s", iComments2);
103         fprintf(of, ";\tAccess bank symbols\n");
104         fprintf(of, "%s", iComments2);
105         
106         fprintf(of, "\tudata_acs\n");
107         for(; r; r = setNextItem(section)) {
108                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
109                 statistics.adsize += r->size;
110         }
111 }
112
113
114 int regCompare(const void *a, const void *b)
115 {
116   const regs *const *i = a;
117   const regs *const *j = b;
118
119         /* sort primarily by the address */
120         if( (*i)->address > (*j)->address)return 1;
121         if( (*i)->address < (*j)->address)return -1;
122         
123         /* and secondarily by size */
124         /* register size sorting may have strange results use with care */
125         if( (*i)->size > (*j)->size)return 1;
126         if( (*i)->size < (*j)->size)return -1;
127         
128         /* finally if in same address and same size sort by name */
129         return (strcmp( (*i)->name, (*j)->name));
130
131   return 0;
132 }
133
134 int symCompare(const void *a, const void *b)
135 {
136   const symbol *const *i = a;
137   const symbol *const *j = b;
138
139         /* sort primarily by the address */
140         if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
141         if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
142         
143         /* and secondarily by size */
144         /* register size sorting may have strange results use with care */
145         if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
146         if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
147
148         /* finally if in same address and same size sort by name */
149         return (strcmp( (*i)->rname, (*j)->rname));
150
151   return 0;
152 }
153
154 void pic16_dump_usection(FILE *of, set *section, int fix)
155 {
156   static int abs_usection_no=0;
157   static unsigned int usection_no=0;
158   regs *r, *rprev;
159   unsigned int init_addr, i;
160   regs **rlist;
161   regs *r1;
162
163         /* put all symbols in an array */
164         if (!elementsInSet(section)) return;
165         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
166         r = rlist[0]; i = 0;
167         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
168                 rlist[i] = rprev; i++;
169         }
170         
171         if(!i) {
172                 if(rlist)Safe_free(rlist);
173           return;
174         }
175
176         /* sort symbols according to their address */
177         qsort(rlist, i  /*elementsInSet(section)*/, sizeof(regs *), regCompare);
178         
179         if(!fix) {
180
181 #define EMIT_SINGLE_UDATA_SECTION       0
182 #if EMIT_SINGLE_UDATA_SECTION
183                 fprintf(of, "\n\n\tudata\n");
184                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
185                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
186                         statistics.udsize += r->size;
187                 }
188 #else
189                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
190                         //fprintf(of, "\nudata_%s_%s\tudata\n", moduleName, r->name);
191                         fprintf(of, "\nudata_%s_%u\tudata\n", moduleName, usection_no++);
192                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
193                         statistics.udsize += r->size;
194                 }
195 #endif
196         } else {
197           unsigned int j=0;
198           int deb_addr=0;
199
200                 rprev = NULL;
201                 init_addr = rlist[j]->address;
202                 deb_addr = init_addr;
203                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
204         
205                 for(j=0;j<i;j++) {
206                         r = rlist[j];
207                         if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
208                         
209                         init_addr = r->address;
210                         deb_addr = init_addr;
211                         
212                         if((rprev && (init_addr > (rprev->address + rprev->size)))) {
213                                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
214                         }
215
216                         if(r1 && (init_addr == r1->address)) {
217                                 fprintf(of, "\n%s\tres\t0\n", r->name);
218                         } else {
219                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
220                                 deb_addr += r->size;
221                                 statistics.udsize += r->size;
222                         }
223                         
224                         rprev = r;
225                 }
226         }
227         Safe_free(rlist);
228 }
229
230 void pic16_dump_gsection(FILE *of, set *sections)
231 {
232   regs *r;
233   sectName *sname;
234
235         for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
236                 if(!strcmp(sname->name, "access"))continue;
237                 fprintf(of, "\n\n%s\tudata\n", sname->name);
238
239                 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
240 #if 0
241                         fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
242                                 r->name, sname->name, sname);
243 #endif
244                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
245                         statistics.udsize += r->size;
246                 }
247         }
248 }
249
250
251 /* forward declaration */
252 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
253 extern void pic16_pCodeConstString(char *name, char *value);
254
255 void pic16_dump_isection(FILE *of, set *section, int fix)
256 {
257   static int abs_isection_no=0;
258   symbol *s, *sprev;
259   unsigned int init_addr, i;
260   symbol **slist;
261
262         /* put all symbols in an array */
263         if (!elementsInSet(section)) return;
264         slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
265         s = slist[0]; i = 0;
266         for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
267                 slist[i] = sprev; i++;
268         }
269         
270         if(!i) {
271                 if(slist)Safe_free(slist);
272           return;
273         }
274
275         /* sort symbols according to their address */
276         qsort(slist, i, sizeof(symbol *), symCompare);
277         
278         pic16_initDB();
279
280         if(!fix) {
281                 fprintf(of, "\n\n\tidata\n");
282                 for(s = setFirstItem(section); s; s = setNextItem(section)) {
283
284                         if(s->ival) {
285                                 fprintf(of, "%s", s->rname);
286                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
287                                 pic16_flushDB('f', (void *)of);
288                         } else {
289                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
290                                         && SPEC_CVAL (s->etype).v_char) {
291
292 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
293                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
294                                 } else {
295                                         assert(0);
296                                 }
297                         }
298                                 
299                 }
300         } else {
301           unsigned int j=0;
302           symbol *s1;
303           
304                 sprev = NULL;
305                 init_addr = SPEC_ADDR(slist[j]->etype);
306                 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
307         
308                 for(j=0;j<i;j++) {
309                         s = slist[j];
310                         if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
311                         
312                         init_addr = SPEC_ADDR(s->etype);
313
314                         if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
315                                 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
316                         }
317
318                         if(s->ival) {
319                                 fprintf(of, "%s", s->rname);
320                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
321                                 pic16_flushDB('f', (void *)of);
322                         } else {
323                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
324                                         && SPEC_CVAL (s->etype).v_char) {
325
326 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
327                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
328                                 } else {
329                                         assert(0);
330                                 }
331                         }
332
333
334                         sprev = s;
335                 }
336         }
337         Safe_free(slist);
338 }
339
340
341 void pic16_dump_int_registers(FILE *of, set *section)
342 {
343   regs *r, *rprev;
344   int i;
345   regs **rlist;
346
347         /* put all symbols in an array */
348         if (!elementsInSet(section)) return;
349         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
350         r = rlist[0]; i = 0;
351         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
352                 rlist[i] = rprev; i++;
353         }
354
355         /* sort symbols according to their address */
356         qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
357         
358         if(!i) {
359                 if(rlist)Safe_free(rlist);
360           return;
361         }
362         
363         fprintf(of, "\n\n; Internal registers\n");
364         
365         fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
366         for(r = setFirstItem(section); r; r = setNextItem(section)) {
367                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
368                 statistics.intsize += r->size;
369         }
370
371         Safe_free(rlist);
372 }
373
374 /**
375  * Find the device structure for the named device.
376  * Consider usind pic16_find_device() instead!
377  *
378  * @param   name
379  *      a name for the desired device
380  * @param   head
381  *      a pointer to the head of the list of devices
382  * @return
383  *      a pointer to the structure for the desired
384  *      device, or NULL
385  */
386 static PIC16_device *
387 find_in_list(const char *name, PIC16_device *head)
388 {
389     int i;
390
391     while (head) {
392         for (i = 0; i < 4; i++) {
393             if (0 == strcmp(head->name[i], name)) {
394                 return (head);
395             } // if
396         } // for
397
398         head = head->next;
399     } // while
400
401     return (NULL);
402 }
403
404 /**
405  * Print a list of supported devices.
406  * If --verbose was given, also emit key characteristics (memory size,
407  * access bank split point, address range of SFRs and config words).
408  *
409  * @param   head
410  *      a pointer to the head of the list of devices
411  */
412 static void
413 pic16_list_devices(PIC16_device *head)
414 {
415     int i = 0;
416
417     if (options.verbose) {
418         printf("device        RAM  split        SFRs           config words\n");
419     } // if
420     while (head) {
421         printf("%-10s  ", head->name[0]);
422         if (options.verbose) {
423             printf("%5d   0x%02x    0x%03x..0x%03x    0x%06x..0x%06x\n",
424                     head->RAMsize,
425                     head->acsSplitOfs,
426                     head->sfrRange.sfrLoAddr,
427                     head->sfrRange.sfrHiAddr,
428                     head->cwInfo.confAddrStart,
429                     head->cwInfo.confAddrEnd);
430         } else {
431             i++;
432             if (0 == (i % 6)) {
433                 printf("\n");
434             } // if
435         } // if
436         head = head->next;
437     } // while
438     printf("\n");
439 }
440
441 /**
442  * Read a single line from the given file.
443  * The caller should free() the returned value!
444  *
445  * @param   file
446  *      a pointer to the open file to read
447  * @return
448  *      a pointer to a malloc'ed copy of the (next) line, or NULL
449  */
450 static char *
451 get_line (FILE *file)
452 {
453     char *line = NULL;
454     size_t len = 129;
455     size_t done = 0;
456     size_t got = 0;
457
458     while (!feof(file)) {
459         line = (char *) Safe_realloc(line, len);
460         got += fread(&line[done], 1, 128, file);
461         line[got] = 0;
462
463         /* find EOL */
464         while (done < got) {
465             if (('\r' == line[done]) || ('\n' == line[done])) {
466                 line[done] = 0;
467                 fseek(file, done + 1 - got, SEEK_CUR);
468                 return (line);
469             } // if
470             done++;
471         } // while
472
473         len += 128;
474     } // while
475
476     return (line);
477 }
478
479 /**
480  * Truncate the given string in place (!) at the first '#' character (if any).
481  *
482  * @param   line
483  *      a pointer to the string to truncate
484  * @return
485  *      a pointer to the truncated string (i.e., line)
486  */
487 static char *
488 strip_comment (char *line)
489 {
490     char *l = line;
491     char c;
492
493     if (!line) {
494         return (line);
495     } // if
496
497     while (0 != (c = *l)) {
498         if ('#' == c) {
499             *l = 0;
500             return (line);
501         } // if
502         l++;
503     } // while
504
505     return (line);
506 }
507
508 /**
509  * Report errors in the device specification.
510  *
511  * @param   msg
512  *      a pointer to the detailed message
513  */
514 #define SYNTAX(msg) do {                                \
515     fprintf(stderr, "%s:%d: Syntax error: %s\n",        \
516             DEVICE_FILE_NAME, lineno, msg);             \
517 } while (0)
518
519 /**
520  * Locate and read in the device specification (if required) and
521  * return the device structure for the named device.
522  *
523  * @param   name
524  *      a pointer to the name of the desired device
525  * @return
526  *      a pointer to the device structure, or NULL
527  */
528 static PIC16_device *
529 pic16_find_device(const char *name)
530 {
531     const char *path;
532     char buffer[PATH_MAX];
533     char *line, *key;
534     const char *sep = " \t\n\r";
535     FILE *f = NULL;
536     PIC16_device *d = NULL, *template;
537     PIC16_device *head = NULL, *tail = NULL;
538     set *_sets[] = { userIncDirsSet, includeDirsSet };
539     set **sets = &_sets[0];
540     int lineno = 0;
541     int res, i;
542     int val[3];
543
544     if (!devices) {
545         //printf("%s: searching %s\n", __func__, DEVICE_FILE_NAME);
546
547         // locate the specification file in the include search paths
548         for (i = 0; (NULL == f) && (i < 2); i++) {
549             for (path = setFirstItem(sets[i]);
550                     (NULL == f) && path;
551                     path = setNextItem(sets[i]))
552             {
553                 SNPRINTF(&buffer[0], PATH_MAX, "%s%s%s",
554                         path, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
555                 //printf("%s: checking %s\n", __func__, &buffer[0]);
556                 f = fopen(&buffer[0], "r");
557             } // for
558         } // while
559     } // if
560
561     if (devices) {
562         // list already set up, nothing to do
563     } else if (NULL == f) {
564         fprintf(stderr, "ERROR: device list %s not found, specify its path via -I<path>\n", DEVICE_FILE_NAME);
565         d = &default_device;
566     } else {
567         // parse the specification file and construct a linked list of
568         // supported devices
569         d = NULL;
570         while (NULL != (line = get_line(f))) {
571             strip_comment(line);
572             //printf("%s: read %s\n", __func__, line);
573             lineno++;
574             key = strtok(line, sep);
575             if (!key) {
576                 // empty line---ignore
577             } else if (0 == strcmp(key, "name")) {
578                 // name %<name>s
579                 if (d) {
580                     if (tail) {
581                         tail->next = d;
582                     } else {
583                         head = d;
584                     } // if
585                     tail = d;
586                     d = NULL;
587                 } // if
588
589                 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[3]);
590                 if ((1 < res) || (3 > strlen(&buffer[3]))) {
591                     SYNTAX("<name> (e.g., 18f452) expected.");
592                 } else {
593                     d = Safe_calloc(1, sizeof(PIC16_device));
594
595                     // { "p18f452", "18f452", "pic18f452", "f452" }
596                     buffer[0] = 'p';
597                     buffer[1] = 'i';
598                     buffer[2] = 'c';
599                     d->name[3] = Safe_strdup(&buffer[5]);
600                     d->name[2] = Safe_strdup(&buffer[0]);
601                     d->name[1] = Safe_strdup(&buffer[3]);
602                     buffer[2] = 'p';
603                     d->name[0] = Safe_strdup(&buffer[2]);
604                 } // if
605             } else if (0 == strcmp(key, "using")) {
606                 // using %<name>s
607                 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[0]);
608                 if ((1 < res) || (3 > strlen(&buffer[3]))) {
609                     SYNTAX("<name> (e.g., 18f452) expected.");
610                 } else {
611                     template = find_in_list(&buffer[0], head);
612                     if (!template) {
613                         SYNTAX("<name> (e.g., 18f452) expected.");
614                     } else {
615                         memcpy(&d->RAMsize, &template->RAMsize,
616                                 ((char *)&d->next) - ((char *)&d->RAMsize));
617                     } // if
618                 } // if
619             } else if (0 == strcmp(key, "ramsize")) {
620                 // ramsize %<bytes>i
621                 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
622                 if (res < 1) {
623                     SYNTAX("<bytes> (e.g., 256) expected.");
624                 } else {
625                     d->RAMsize = val[0];
626                 } // if
627             } else if (0 == strcmp(key, "split")) {
628                 // split %<offset>i
629                 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
630                 if (res < 1) {
631                     SYNTAX("<offset> (e.g., 0x80) expected.");
632                 } else {
633                     d->acsSplitOfs = val[0];
634                 } // if
635             } else if (0 == strcmp(key, "sfrrange")) {
636                 // sfrrange %<first>i %<last>i
637                 res = sscanf(&line[1+strlen(key)], " %i %i",
638                         &val[0], &val[1]);
639                 if (res < 2) {
640                     SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
641                 } else {
642                     d->sfrRange.sfrLoAddr = val[0];
643                     d->sfrRange.sfrHiAddr = val[1];
644                 } // if
645             } else if (0 == strcmp(key, "configrange")) {
646                 // configrange %<first>i %<last>i
647                 res = sscanf(&line[1+strlen(key)], " %i %i",
648                         &val[0], &val[1]);
649                 if (res < 2) {
650                     SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
651                 } else {
652                     d->cwInfo.confAddrStart = val[0];
653                     d->cwInfo.confAddrEnd = val[1];
654                 } // if
655             } else if (0 == strcmp(key, "configword")) {
656                 // configword %<address>i %<mask>i %<value>i
657                 res = sscanf(&line[1+strlen(key)], " %i %i %i",
658                         &val[0], &val[1], &val[2]);
659                 if (res < 3) {
660                     SYNTAX("<address> <mask> <value> (e.g., 0x200001 0x0f 0x07) expected.");
661                 } else {
662                     val[0] -= d->cwInfo.confAddrStart;
663                     if ((val[0] < 0)
664                             || (val[0] > (d->cwInfo.confAddrEnd - d->cwInfo.confAddrStart))
665                             || (val[0] >= CONFIGURATION_WORDS))
666                     {
667                         SYNTAX("address out of bounds.");
668                     } else {
669                         d->cwInfo.crInfo[val[0]].mask = val[1];
670                         d->cwInfo.crInfo[val[0]].value = val[2];
671                     } // if
672                 } // if
673             } else if (0 == strcmp(key, "idlocrange")) {
674                 // idlocrange %<first>i %<last>i
675                 res = sscanf(&line[1+strlen(key)], " %i %i",
676                         &val[0], &val[1]);
677                 if (res < 2) {
678                     SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
679                 } else {
680                     d->idInfo.idAddrStart = val[0];
681                     d->idInfo.idAddrEnd = val[1];
682                 } // if
683             } else if (0 == strcmp(key, "idword")) {
684                 // idword %<address>i %<value>i
685                 res = sscanf(&line[1+strlen(key)], " %i %i",
686                         &val[0], &val[1]);
687                 if (res < 2) {
688                     SYNTAX("<address> <value> (e.g., 0x3fffff 0x00) expected.");
689                 } else {
690                     val[0] -= d->idInfo.idAddrStart;
691                     if ((val[0] < 0)
692                             || (val[0] > (d->idInfo.idAddrEnd - d->idInfo.idAddrStart))
693                             || (val[0] >= IDLOCATION_BYTES))
694                     {
695                         SYNTAX("address out of bounds.");
696                     } else {
697                         d->idInfo.irInfo[val[0]].value = val[1];
698                     } // if
699                 } // if
700             } else {
701                 printf("%s: Invalid keyword in %s ignored: %s\n",
702                         __func__, DEVICE_FILE_NAME, key);
703             } // if
704             free(line);
705         } // while
706
707         if (d) {
708             if (tail) {
709                 tail->next = d;
710             } else {
711                 head = d;
712             } // if
713             tail = d;
714             d = NULL;
715         } // if
716
717         devices = head;
718
719         fclose(f);
720     } // if
721
722     d = find_in_list(name, devices);
723     if (!d) {
724         d = &default_device;
725     } // if
726
727     return (d);
728 }
729
730 /*-----------------------------------------------------------------*
731  *  
732  *-----------------------------------------------------------------*/
733 void pic16_init_pic(const char *pic_type)
734 {
735     pic16 = pic16_find_device(pic_type);
736
737     if (&default_device == pic16) {
738         if (pic_type) {
739             fprintf(stderr, "'%s' was not found.\n", pic_type);
740         } else {
741             fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
742         } // if
743
744         if (devices) {
745             fprintf(stderr,"Valid devices are (use --verbose for more details):\n");
746             pic16_list_devices(devices);
747         } // if
748         exit(EXIT_FAILURE);
749     } // if
750 }
751
752 /*-----------------------------------------------------------------*
753  *  char *pic16_processor_base_name(void) - Include file is derived from this.
754  *-----------------------------------------------------------------*/
755 char *pic16_processor_base_name(void)
756 {
757
758   if(!pic16)
759     return NULL;
760
761   return pic16->name[0];
762 }
763
764 #define DEBUG_CHECK     0
765
766 /*
767  * return 1 if register wasn't found and added, 0 otherwise
768  */
769 int checkAddReg(set **set, regs *reg)
770 {
771   regs *tmp;
772
773
774         if(!reg)return 0;
775 #if DEBUG_CHECK
776         fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
777 #endif
778
779         for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
780                 if(!strcmp(tmp->name, reg->name))break;
781         }
782         
783         if(!tmp) {
784                 addSet(set, reg);
785 #if DEBUG_CHECK
786                 fprintf(stderr, "added\n");
787 #endif
788                 return 1;
789         }
790
791 #if DEBUG_CHECK
792         fprintf(stderr, "already added\n");
793 #endif
794   return 0;
795 }
796
797 int checkAddSym(set **set, symbol *sym)
798 {
799   symbol *tmp;
800
801         if(!sym)return 0;
802 #if DEBUG_CHECK
803         fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
804 #endif
805
806         for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
807                 if(!strcmp(tmp->name, sym->name))break;
808         }
809         
810         if(!tmp) {
811                 addSet(set, sym);
812 #if DEBUG_CHECK
813                 fprintf(stderr, "added\n");
814 #endif
815                 return 1;
816         }
817
818 #if DEBUG_CHECK
819         fprintf(stderr, "already added\n");
820 #endif
821
822   return 0;
823 }
824
825 int checkSym(set *set, symbol *sym)
826 {
827   symbol *tmp;
828
829         if(!sym)return 0;
830         
831 #if DEUG_CHECK
832         fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
833 #endif
834
835         for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
836                 if(!strcmp(tmp->name, sym->name))break;
837         }
838         
839         if(!tmp) {
840 #if DEBUG_CHECK
841                 fprintf(stderr, "not found\n");
842 #endif
843                 return 0;
844         }
845
846 #if DEBUG_CHECK
847         fprintf(stderr, "found\n");
848 #endif
849
850   return 1;
851 }
852
853 /*-----------------------------------------------------------------*
854  * void pic16_groupRegistersInSection - add each register to its   *
855  *      corresponding section                                      *
856  *-----------------------------------------------------------------*/
857 void pic16_groupRegistersInSection(set *regset)
858 {
859   regs *reg;
860   sectSym *ssym;
861   int docontinue=0;
862
863         for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
864
865 #if 0
866                 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
867                         __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
868 #endif
869                 if((reg->wasUsed
870                         && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
871                   ) {
872                         
873                         /* avoid grouping registers that have an initial value,
874                          * they will be added later in idataSymSet */
875                         if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
876                                 continue;
877
878 #if 0
879                         fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
880                                 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
881                                         (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
882                                         (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
883                                         (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
884 #endif
885                         
886                         docontinue=0;
887                         for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
888                                 if(!strcmp(ssym->name, reg->name)) {
889 //                                      fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
890 //                                                      __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
891                                         if(strcmp(ssym->section->name, "access")) {
892                                                 addSet(&ssym->section->regsSet, reg);
893                                                 docontinue=1;
894                                                 break;
895                                         } else {
896                                                 docontinue=0;
897                                                 reg->accessBank = 1;
898                                                 break;
899                                         }
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                                         if(reg->accessBank) {
918                                                 if(reg->alias != 0x40)
919                                                         checkAddReg(&pic16_acs_udata, reg);
920                                         } else
921                                                 checkAddReg(&pic16_rel_udata, reg);
922                                 }
923                         }
924                 }
925         }
926 }
927
928
929
930
931
932 /*-----------------------------------------------------------------*
933  *  void pic16_assignConfigWordValue(int address, int value)
934  *
935  * All high performance RISC CPU PICs have seven config word starting
936  * at address 0x300000.
937  * This routine will assign a value to that address.
938  *
939  *-----------------------------------------------------------------*/
940 void pic16_assignConfigWordValue(int address, unsigned int value)
941 {
942   int i;
943
944         for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
945                 if((address == pic16->cwInfo.confAddrStart+i)
946                   && (pic16->cwInfo.crInfo[i].mask != -1)
947                   && (pic16->cwInfo.crInfo[i].mask != 0)) {
948
949 #if 0
950                         fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
951                                 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
952                                         pic16->cwInfo.crInfo[i].mask,
953                                         (pic16->cwInfo.crInfo[i].mask) & (~value));
954 #endif
955
956 #if 0
957                         if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
958                                 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
959                                         __FILE__, __LINE__, address);
960                                         return;
961                         }
962 #endif
963
964                         pic16->cwInfo.crInfo[i].value = value;
965                         pic16->cwInfo.crInfo[i].emit = 1;
966                         return;
967                 }
968         }
969 }
970
971 void pic16_assignIdByteValue(int address, char value)
972 {
973   int i;
974
975         for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
976                 if(address == pic16->idInfo.idAddrStart+i) {
977                         pic16->idInfo.irInfo[i].value = value;
978                         pic16->idInfo.irInfo[i].emit = 1;
979                 }
980         }
981 }