1 /*-------------------------------------------------------------------------
3 device.c - Accomodates subtle variations in PIC16 devices
5 Written By - Scott Dattalo scott@dattalo.com
6 Ported to PIC16 By - Martin Dubuc m.dubuc@rogers.com
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
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.
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 -------------------------------------------------------------------------*/
25 #include "common.h" // Include everything in the SDCC src directory
27 #include "dbuf_string.h"
34 void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
35 extern void pic16_pCodeConstString (char *name, char *value, unsigned length);
37 stats_t statistics = { 0, 0, 0, 0 };
39 #define DEVICE_FILE_NAME "pic16devices.txt"
41 static PIC16_device default_device = {
42 { "p18f452", "18f452", "pic18f452", "f452" },
45 { /* configuration words */
47 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ ,
48 { 0x0f, 0, 0xff } /* 3 */ , { -1 , 0, 0xff } /* 4 */ ,
49 { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
50 { -1 , 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ ,
51 { 0xc0, 0, 0xff } /* 9 */ , { 0x0f, 0, 0xff } /* a */ ,
52 { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
53 { 0x40, 0, 0xff } /* d */ }
57 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
58 { 0, 0 }, { 0, 0 }, { 0, 0 } }
63 PIC16_device *pic16 = &default_device;
64 static PIC16_device *devices = NULL;
66 extern set *includeDirsSet;
67 extern set *userIncDirsSet;
69 extern char *iComments2;
72 pic16_dump_equates (FILE *of, set *equs)
76 r = setFirstItem (equs);
80 fprintf (of, "%s", iComments2);
81 fprintf (of, ";\tEquates to used internal registers\n");
82 fprintf (of, "%s", iComments2);
84 for (; r; r = setNextItem (equs))
86 fprintf (of, "%s\tequ\t0x%02x\n", r->name, r->address);
92 pic16_dump_access (FILE *of, set *section)
96 r = setFirstItem (section);
100 fprintf (of, "%s", iComments2);
101 fprintf (of, ";\tAccess bank symbols\n");
102 fprintf (of, "%s", iComments2);
104 fprintf (of, "\tudata_acs\n");
105 for (; r; r = setNextItem (section))
107 fprintf (of, "%s\tres\t%d\n", r->name, r->size);
108 statistics.adsize += r->size;
113 regCompare (const void *a, const void *b)
115 const regs *const *i = a;
116 const regs *const *j = b;
118 /* Sort primarily by the address ... */
119 if ((*i)->address > (*j)->address)
122 if ((*i)->address < (*j)->address)
125 /* ... and secondarily by size. */
126 /* Register size sorting may have strange results, use with care! */
127 if ((*i)->size > (*j)->size)
130 if ((*i)->size < (*j)->size)
133 /* Finally, if in same address and same size, sort by name. */
134 return (strcmp ((*i)->name, (*j)->name));
138 symCompare (const void *a, const void *b)
140 const symbol *const *i = a;
141 const symbol *const *j = b;
143 /* Sort primarily by the address ... */
144 if (SPEC_ADDR ((*i)->etype) > SPEC_ADDR ((*j)->etype))
147 if (SPEC_ADDR ((*i)->etype) < SPEC_ADDR ((*j)->etype))
150 /* ... and secondarily by size. */
151 /* Register size sorting may have strange results, use with care! */
152 if (getSize ((*i)->etype) > getSize ((*j)->etype))
155 if (getSize ((*i)->etype) < getSize ((*j)->etype))
158 /* Finally, if in same address and same size, sort by name. */
159 return (strcmp ((*i)->rname, (*j)->rname));
163 pic16_dump_usection (FILE *of, set *section, int fix)
165 static int abs_usection_no = 0;
166 static unsigned int usection_no = 0;
168 unsigned int init_addr, i;
172 /* put all symbols in an array */
173 if (!elementsInSet (section))
176 rlist = Safe_calloc (elementsInSet (section), sizeof (regs *));
179 for (rprev = setFirstItem (section); rprev; rprev = setNextItem (section))
193 /* sort symbols according to their address */
194 qsort (rlist, i, sizeof (regs *), regCompare);
198 #define EMIT_SINGLE_UDATA_SECTION 0
199 #if EMIT_SINGLE_UDATA_SECTION
200 fprintf (of, "\n\n\tudata\n");
201 for (r = setFirstItem (section); r; r = setNextItem (section))
203 fprintf (of, "%s\tres\t%d\n", r->name, r->size);
204 statistics.udsize += r->size;
207 for (r = setFirstItem (section); r; r = setNextItem (section))
209 //fprintf (of, "\nudata_%s_%s\tudata\n", moduleName, r->name);
210 fprintf (of, "\nudata_%s_%u\tudata\n", moduleName, usection_no++);
211 fprintf (of, "%s\tres\t%d\n", r->name, r->size);
212 statistics.udsize += r->size;
219 unsigned int prev_size = 0;
222 init_addr = (rlist[j]->address & 0x0FFF); // warning(s) emitted below
223 fprintf (of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, (init_addr & 0x0FFF));
225 for (j = 0; j < i; j++)
232 init_addr = (r->address & 0x0FFF);
233 if (init_addr != r->address)
235 fprintf (stderr, "%s: WARNING: Changed address of pinned variable %s from 0x%x to 0x%x\n",
236 moduleName, r->name, r->address, init_addr);
239 if ((rprev && (init_addr != ((rprev->address & 0x0FFF) + prev_size))))
240 fprintf (of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
242 /* XXX: Does not handle partial overlap correctly. */
243 if (r1 && (init_addr == (r1->address & 0x0FFF)))
246 fprintf (of, "%-15s\n", r->name);
251 fprintf (of, "%-15s\tres\t%d\n", r->name, prev_size);
252 statistics.udsize += prev_size;
263 pic16_dump_gsection (FILE *of, set *sections)
268 for (sname = setFirstItem (sections); sname; sname = setNextItem (sections))
270 if (!strcmp (sname->name, "access"))
273 fprintf (of, "\n\n%s\tudata\n", sname->name);
275 for (r = setFirstItem (sname->regsSet); r; r = setNextItem (sname->regsSet))
278 fprintf (stderr, "%s:%d emitting variable %s for section %s (%p)\n",
279 __FILE__, __LINE__, r->name, sname->name, sname);
281 fprintf (of, "%s\tres\t%d\n", r->name, r->size);
282 statistics.udsize += r->size;
288 pic16_dump_isection (FILE *of, set *section, int fix)
290 static int abs_isection_no = 0;
292 unsigned int init_addr, i;
295 /* put all symbols in an array */
296 if (!elementsInSet (section))
299 slist = Safe_calloc (elementsInSet (section), sizeof (symbol *));
302 for (sprev = setFirstItem (section); sprev; sprev = setNextItem (section))
316 /* sort symbols according to their address */
317 qsort (slist, i, sizeof (symbol *), symCompare);
323 fprintf (of, "\n\n\tidata\n");
324 for (s = setFirstItem (section); s; s = setNextItem (section))
328 fprintf (of, "%s", s->rname);
329 pic16_printIval (s, s->type, s->ival, 'f', (void *)of);
330 pic16_flushDB ('f', (void *)of);
334 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
335 && SPEC_CVAL (s->etype).v_char)
337 //fprintf (stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
338 pic16_pCodeConstString (s->rname , SPEC_CVAL (s->etype).v_char, getSize (s->type));
353 init_addr = SPEC_ADDR (slist[j]->etype);
354 fprintf (of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
356 for (j = 0; j < i; j++)
363 init_addr = SPEC_ADDR (s->etype);
365 if (sprev && (init_addr > (SPEC_ADDR (sprev->etype) + getSize (sprev->etype))))
366 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
370 fprintf (of, "%s", s->rname);
371 pic16_printIval (s, s->type, s->ival, 'f', (void *)of);
372 pic16_flushDB ('f', (void *)of);
376 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
377 && SPEC_CVAL (s->etype).v_char)
379 //fprintf (stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
380 pic16_pCodeConstString (s->rname , SPEC_CVAL (s->etype).v_char, getSize (s->type));
396 pic16_dump_int_registers (FILE *of, set *section)
402 /* put all symbols in an array */
403 if (!elementsInSet (section))
406 rlist = Safe_calloc (elementsInSet (section), sizeof (regs *));
409 for (rprev = setFirstItem (section); rprev; rprev = setNextItem (section))
423 /* sort symbols according to their address */
424 qsort (rlist, i, sizeof (regs *), regCompare);
426 fprintf (of, "\n\n; Internal registers\n");
428 fprintf (of, "%s\tudata_ovr\t0x0000\n", ".registers");
429 for (r = setFirstItem (section); r; r = setNextItem (section))
431 fprintf (of, "%s\tres\t%d\n", r->name, r->size);
432 statistics.intsize += r->size;
439 * Find the device structure for the named device.
440 * Consider usind pic16_find_device() instead!
443 * a name for the desired device
445 * a pointer to the head of the list of devices
447 * a pointer to the structure for the desired
450 static PIC16_device *
451 find_in_list(const char *name, PIC16_device *head)
456 for (i = 0; i < 4; i++) {
457 if (0 == strcmp(head->name[i], name)) {
469 * Print a list of supported devices.
470 * If --verbose was given, also emit key characteristics (memory size,
471 * access bank split point, address range of SFRs and config words).
474 * a pointer to the head of the list of devices
477 pic16_list_devices(PIC16_device *head)
481 if (options.verbose) {
482 printf("device RAM split config words\n");
485 printf("%-10s ", head->name[0]);
486 if (options.verbose) {
487 printf("%5d 0x%02x 0x%06x..0x%06x\n",
490 head->cwInfo.confAddrStart,
491 head->cwInfo.confAddrEnd);
504 * Read a single line from the given file.
507 * a pointer to the open file to read
509 * a pointer to a malloc'ed copy of the (next) line, or NULL
512 get_line (FILE *file)
514 static struct dbuf_s dbuf;
515 static int initialized = 0;
519 dbuf_init (&dbuf, 129);
523 dbuf_set_length (&dbuf, 0);
526 if (dbuf_getline (&dbuf, file) != 0)
529 /* (char *) type cast is an ugly hack since pic16_find_device() modifies the buffer */
530 return (char *)dbuf_get_buf (&dbuf);
541 * Truncate the given string in place (!) at the first '#' character (if any).
544 * a pointer to the string to truncate
546 * a pointer to the truncated string (i.e., line)
549 strip_comment (char *line)
558 while (0 != (c = *l)) {
570 * Report errors in the device specification.
573 * a pointer to the detailed message
575 #define SYNTAX(msg) do { \
576 fprintf(stderr, "%s:%d: Syntax error: %s\n", \
577 DEVICE_FILE_NAME, lineno, msg); \
581 * Locate and read in the device specification (if required) and
582 * return the device structure for the named device.
585 * a pointer to the name of the desired device
587 * a pointer to the device structure, or NULL
589 static PIC16_device *
590 pic16_find_device(const char *name)
593 char buffer[PATH_MAX];
595 const char *sep = " \t\n\r";
597 PIC16_device *d = NULL, *template;
598 PIC16_device *head = NULL, *tail = NULL;
599 set *_sets[] = { userIncDirsSet, includeDirsSet };
600 set **sets = &_sets[0];
606 //printf("%s: searching %s\n", __func__, DEVICE_FILE_NAME);
608 // locate the specification file in the include search paths
609 for (i = 0; (NULL == f) && (i < 2); i++) {
610 for (path = setFirstItem(sets[i]);
612 path = setNextItem(sets[i]))
614 SNPRINTF(&buffer[0], PATH_MAX, "%s%s%s",
615 path, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
616 //printf("%s: checking %s\n", __func__, &buffer[0]);
617 f = fopen(&buffer[0], "r");
623 // list already set up, nothing to do
624 } else if (NULL == f) {
625 fprintf(stderr, "ERROR: device list %s not found, specify its path via -I<path>\n", DEVICE_FILE_NAME);
628 // parse the specification file and construct a linked list of
631 while (NULL != (line = get_line(f))) {
633 //printf("%s: read %s\n", __func__, line);
635 key = strtok(line, sep);
637 // empty line---ignore
638 } else if (0 == strcmp(key, "name")) {
650 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[3]);
651 if ((1 < res) || (3 > strlen(&buffer[3]))) {
652 SYNTAX("<name> (e.g., 18f452) expected.");
654 d = Safe_calloc(1, sizeof(PIC16_device));
656 // { "p18f452", "18f452", "pic18f452", "f452" }
660 d->name[3] = Safe_strdup(&buffer[5]);
661 d->name[2] = Safe_strdup(&buffer[0]);
662 d->name[1] = Safe_strdup(&buffer[3]);
664 d->name[0] = Safe_strdup(&buffer[2]);
666 } else if (0 == strcmp(key, "using")) {
668 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[0]);
669 if ((1 < res) || (3 > strlen(&buffer[3]))) {
670 SYNTAX("<name> (e.g., 18f452) expected.");
672 template = find_in_list(&buffer[0], head);
674 SYNTAX("<name> (e.g., 18f452) expected.");
676 memcpy(&d->RAMsize, &template->RAMsize,
677 ((char *)&d->next) - ((char *)&d->RAMsize));
680 } else if (0 == strcmp(key, "ramsize")) {
682 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
684 SYNTAX("<bytes> (e.g., 256) expected.");
688 } else if (0 == strcmp(key, "split")) {
690 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
692 SYNTAX("<offset> (e.g., 0x80) expected.");
694 d->acsSplitOfs = val[0];
696 } else if (0 == strcmp(key, "configrange")) {
697 // configrange %<first>i %<last>i
698 res = sscanf(&line[1+strlen(key)], " %i %i",
701 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
703 d->cwInfo.confAddrStart = val[0];
704 d->cwInfo.confAddrEnd = val[1];
706 } else if (0 == strcmp(key, "configword")) {
707 // configword %<address>i %<mask>i %<value>i
708 res = sscanf(&line[1+strlen(key)], " %i %i %i",
709 &val[0], &val[1], &val[2]);
711 SYNTAX("<address> <mask> <value> (e.g., 0x200001 0x0f 0x07) expected.");
713 val[0] -= d->cwInfo.confAddrStart;
715 || (val[0] > (d->cwInfo.confAddrEnd - d->cwInfo.confAddrStart))
716 || (val[0] >= CONFIGURATION_WORDS))
718 SYNTAX("address out of bounds.");
720 d->cwInfo.crInfo[val[0]].mask = val[1];
721 d->cwInfo.crInfo[val[0]].value = val[2];
724 } else if (0 == strcmp(key, "idlocrange")) {
725 // idlocrange %<first>i %<last>i
726 res = sscanf(&line[1+strlen(key)], " %i %i",
729 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
731 d->idInfo.idAddrStart = val[0];
732 d->idInfo.idAddrEnd = val[1];
734 } else if (0 == strcmp(key, "idword")) {
735 // idword %<address>i %<value>i
736 res = sscanf(&line[1+strlen(key)], " %i %i",
739 SYNTAX("<address> <value> (e.g., 0x3fffff 0x00) expected.");
741 val[0] -= d->idInfo.idAddrStart;
743 || (val[0] > (d->idInfo.idAddrEnd - d->idInfo.idAddrStart))
744 || (val[0] >= IDLOCATION_BYTES))
746 SYNTAX("address out of bounds.");
748 d->idInfo.irInfo[val[0]].value = val[1];
752 printf("%s: Invalid keyword in %s ignored: %s\n",
753 __func__, DEVICE_FILE_NAME, key);
772 d = find_in_list(name, devices);
780 /*-----------------------------------------------------------------*
782 *-----------------------------------------------------------------*/
783 void pic16_init_pic(const char *pic_type)
785 pic16 = pic16_find_device(pic_type);
787 if (&default_device == pic16) {
789 fprintf(stderr, "'%s' was not found.\n", pic_type);
791 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
795 fprintf(stderr,"Valid devices are (use --verbose for more details):\n");
796 pic16_list_devices(devices);
802 /*-----------------------------------------------------------------*
803 * char *pic16_processor_base_name(void) - Include file is derived from this.
804 *-----------------------------------------------------------------*/
805 char *pic16_processor_base_name(void)
811 return pic16->name[0];
814 #define DEBUG_CHECK 0
817 * return 1 if register wasn't found and added, 0 otherwise
819 int checkAddReg(set **set, regs *reg)
826 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
829 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
830 if(!strcmp(tmp->name, reg->name))break;
836 fprintf(stderr, "added\n");
842 fprintf(stderr, "already added\n");
847 int checkAddSym(set **set, symbol *sym)
853 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
856 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
857 if(!strcmp(tmp->name, sym->name))break;
863 fprintf(stderr, "added\n");
869 fprintf(stderr, "already added\n");
875 int checkSym(set *set, symbol *sym)
882 fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
885 for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
886 if(!strcmp(tmp->name, sym->name))break;
891 fprintf(stderr, "not found\n");
897 fprintf(stderr, "found\n");
903 /*-----------------------------------------------------------------*
904 * void pic16_groupRegistersInSection - add each register to its *
905 * corresponding section *
906 *-----------------------------------------------------------------*/
907 void pic16_groupRegistersInSection(set *regset)
913 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
916 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
917 __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
920 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
923 /* avoid grouping registers that have an initial value,
924 * they will be added later in idataSymSet */
925 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
929 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
930 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
931 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
932 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
933 (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
937 for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
938 if(!strcmp(ssym->name, reg->name)) {
939 // fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
940 // __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
941 if(strcmp(ssym->section->name, "access")) {
942 addSet(&ssym->section->regsSet, reg);
953 if(docontinue)continue;
955 // fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
957 if(reg->alias == 0x80) {
958 checkAddReg(&pic16_equ_data, reg);
961 checkAddReg(&pic16_fix_udata, reg);
964 if(reg->pc_type == PO_GPR_TEMP)
965 checkAddReg(&pic16_int_regs, reg);
967 if(reg->accessBank) {
968 if(reg->alias != 0x40)
969 checkAddReg(&pic16_acs_udata, reg);
971 checkAddReg(&pic16_rel_udata, reg);
982 /*-----------------------------------------------------------------*
983 * void pic16_assignConfigWordValue(int address, int value)
985 * All high performance RISC CPU PICs have seven config word starting
986 * at address 0x300000.
987 * This routine will assign a value to that address.
989 *-----------------------------------------------------------------*/
990 void pic16_assignConfigWordValue(int address, unsigned int value)
994 for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
995 if((address == pic16->cwInfo.confAddrStart+i)
996 && (pic16->cwInfo.crInfo[i].mask != -1)
997 && (pic16->cwInfo.crInfo[i].mask != 0)) {
1000 fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
1001 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
1002 pic16->cwInfo.crInfo[i].mask,
1003 (pic16->cwInfo.crInfo[i].mask) & (~value));
1007 if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
1008 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
1009 __FILE__, __LINE__, address);
1014 pic16->cwInfo.crInfo[i].value = value;
1015 pic16->cwInfo.crInfo[i].emit = 1;
1021 void pic16_assignIdByteValue(int address, char value)
1025 for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
1026 if(address == pic16->idInfo.idAddrStart+i) {
1027 pic16->idInfo.irInfo[i].value = value;
1028 pic16->idInfo.irInfo[i].emit = 1;