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