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