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)
559 while (0 != (c = *l))
573 * Report errors in the device specification.
576 * a pointer to the detailed message
578 #define SYNTAX(msg) do { \
579 fprintf(stderr, "%s:%d: Syntax error: %s\n", \
580 DEVICE_FILE_NAME, lineno, msg); \
584 * Locate and read in the device specification (if required) and
585 * return the device structure for the named device.
588 * a pointer to the name of the desired device
590 * a pointer to the device structure, or NULL
592 static PIC16_device *
593 pic16_find_device(const char *name)
596 char buffer[PATH_MAX];
598 const char *sep = " \t\n\r";
600 PIC16_device *d = NULL, *template;
601 PIC16_device *head = NULL, *tail = NULL;
602 set *_sets[] = { userIncDirsSet, includeDirsSet };
603 set **sets = &_sets[0];
610 //printf("%s: searching %s\n", __func__, DEVICE_FILE_NAME);
612 // locate the specification file in the include search paths
613 for (i = 0; (NULL == f) && (i < 2); i++)
615 for (path = setFirstItem(sets[i]);
617 path = setNextItem(sets[i]))
619 SNPRINTF(&buffer[0], PATH_MAX, "%s%s%s",
620 path, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
621 //printf("%s: checking %s\n", __func__, &buffer[0]);
622 f = fopen(&buffer[0], "r");
629 // list already set up, nothing to do
633 fprintf(stderr, "ERROR: device list %s not found, specify its path via -I<path>\n",
639 // parse the specification file and construct a linked list of
642 while (NULL != (line = get_line(f)))
645 //printf("%s: read %s\n", __func__, line);
647 key = strtok(line, sep);
650 // empty line---ignore
652 else if (0 == strcmp(key, "name"))
669 res = sscanf(&line[1 + strlen(key)], " %16s", &buffer[3]);
670 if ((1 < res) || (3 > strlen(&buffer[3])))
672 SYNTAX("<name> (e.g., 18f452) expected.");
676 d = Safe_calloc(1, sizeof(PIC16_device));
678 // { "p18f452", "18f452", "pic18f452", "f452" }
682 d->name[3] = Safe_strdup(&buffer[5]);
683 d->name[2] = Safe_strdup(&buffer[0]);
684 d->name[1] = Safe_strdup(&buffer[3]);
686 d->name[0] = Safe_strdup(&buffer[2]);
689 else if (0 == strcmp(key, "using"))
692 res = sscanf(&line[1 + strlen(key)], " %16s", &buffer[0]);
693 if ((1 < res) || (3 > strlen(&buffer[3])))
695 SYNTAX("<name> (e.g., 18f452) expected.");
699 template = find_in_list(&buffer[0], head);
702 SYNTAX("<name> (e.g., 18f452) expected.");
706 memcpy(&d->RAMsize, &template->RAMsize,
707 ((char *)&d->next) - ((char *)&d->RAMsize));
711 else if (0 == strcmp(key, "ramsize"))
714 res = sscanf(&line[1 + strlen(key)], " %i", &val[0]);
717 SYNTAX("<bytes> (e.g., 256) expected.");
724 else if (0 == strcmp(key, "split"))
727 res = sscanf(&line[1 + strlen(key)], " %i", &val[0]);
730 SYNTAX("<offset> (e.g., 0x80) expected.");
734 d->acsSplitOfs = val[0];
737 else if (0 == strcmp(key, "configrange"))
739 // configrange %<first>i %<last>i
740 res = sscanf(&line[1 + strlen(key)], " %i %i",
744 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
748 d->cwInfo.confAddrStart = val[0];
749 d->cwInfo.confAddrEnd = val[1];
752 else if (0 == strcmp(key, "configword"))
754 // configword %<address>i %<mask>i %<value>i [%<and-mask>i]
755 res = sscanf(&line[1 + strlen(key)], " %i %i %i %i",
756 &val[0], &val[1], &val[2], &val[3]);
759 SYNTAX("<address> <mask> <value> [<and-mask>] (e.g., 0x200001 0x0f 0x07) expected.");
763 val[0] -= d->cwInfo.confAddrStart;
765 || (val[0] > (d->cwInfo.confAddrEnd - d->cwInfo.confAddrStart))
766 || (val[0] >= CONFIGURATION_WORDS))
768 SYNTAX("address out of bounds.");
772 d->cwInfo.crInfo[val[0]].mask = val[1];
773 d->cwInfo.crInfo[val[0]].value = val[2];
774 d->cwInfo.crInfo[val[0]].andmask = 0;
777 // apply extra mask (e.g., to disable XINST)
778 d->cwInfo.crInfo[val[0]].andmask = val[3];
783 else if (0 == strcmp(key, "idlocrange"))
785 // idlocrange %<first>i %<last>i
786 res = sscanf(&line[1 + strlen(key)], " %i %i",
790 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
794 d->idInfo.idAddrStart = val[0];
795 d->idInfo.idAddrEnd = val[1];
798 else if (0 == strcmp(key, "idword"))
800 // idword %<address>i %<value>i
801 res = sscanf(&line[1 + strlen(key)], " %i %i",
805 SYNTAX("<address> <value> (e.g., 0x3fffff 0x00) expected.");
809 val[0] -= d->idInfo.idAddrStart;
811 || (val[0] > (d->idInfo.idAddrEnd - d->idInfo.idAddrStart))
812 || (val[0] >= IDLOCATION_BYTES))
814 SYNTAX("address out of bounds.");
818 d->idInfo.irInfo[val[0]].value = val[1];
824 printf("%s: Invalid keyword in %s ignored: %s\n",
825 __func__, DEVICE_FILE_NAME, key);
848 d = find_in_list(name, devices);
857 /*-----------------------------------------------------------------*
859 *-----------------------------------------------------------------*/
860 void pic16_init_pic(const char *pic_type)
862 pic16 = pic16_find_device(pic_type);
864 if (&default_device == pic16) {
866 fprintf(stderr, "'%s' was not found.\n", pic_type);
868 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
872 fprintf(stderr,"Valid devices are (use --verbose for more details):\n");
873 pic16_list_devices(devices);
879 /*-----------------------------------------------------------------*
880 * char *pic16_processor_base_name(void) - Include file is derived from this.
881 *-----------------------------------------------------------------*/
882 char *pic16_processor_base_name(void)
888 return pic16->name[0];
891 #define DEBUG_CHECK 0
894 * return 1 if register wasn't found and added, 0 otherwise
896 int checkAddReg(set **set, regs *reg)
903 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
906 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
907 if(!strcmp(tmp->name, reg->name))break;
913 fprintf(stderr, "added\n");
919 fprintf(stderr, "already added\n");
924 int checkAddSym(set **set, symbol *sym)
930 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
933 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
934 if(!strcmp(tmp->name, sym->name))break;
940 fprintf(stderr, "added\n");
946 fprintf(stderr, "already added\n");
952 int checkSym(set *set, symbol *sym)
959 fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
962 for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
963 if(!strcmp(tmp->name, sym->name))break;
968 fprintf(stderr, "not found\n");
974 fprintf(stderr, "found\n");
980 /*-----------------------------------------------------------------*
981 * void pic16_groupRegistersInSection - add each register to its *
982 * corresponding section *
983 *-----------------------------------------------------------------*/
984 void pic16_groupRegistersInSection(set *regset)
990 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
993 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
994 __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
997 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
1000 /* avoid grouping registers that have an initial value,
1001 * they will be added later in idataSymSet */
1002 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
1006 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
1007 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
1008 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
1009 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
1010 (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
1014 for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
1015 if(!strcmp(ssym->name, reg->name)) {
1016 // fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
1017 // __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
1018 if(strcmp(ssym->section->name, "access")) {
1019 addSet(&ssym->section->regsSet, reg);
1024 reg->accessBank = 1;
1030 if(docontinue)continue;
1032 // fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
1034 if(reg->alias == 0x80) {
1035 checkAddReg(&pic16_equ_data, reg);
1038 checkAddReg(&pic16_fix_udata, reg);
1041 if(reg->pc_type == PO_GPR_TEMP)
1042 checkAddReg(&pic16_int_regs, reg);
1044 if(reg->accessBank) {
1045 if(reg->alias != 0x40)
1046 checkAddReg(&pic16_acs_udata, reg);
1048 checkAddReg(&pic16_rel_udata, reg);
1059 /*-----------------------------------------------------------------*
1060 * void pic16_assignConfigWordValue(int address, int value)
1062 * All high performance RISC CPU PICs have seven config word starting
1063 * at address 0x300000.
1064 * This routine will assign a value to that address.
1066 *-----------------------------------------------------------------*/
1068 pic16_assignConfigWordValue(int address, unsigned int value)
1072 for (i = 0; i < pic16->cwInfo.confAddrEnd - pic16->cwInfo.confAddrStart + 1; i++)
1074 if ((address == pic16->cwInfo.confAddrStart + i)
1075 && (pic16->cwInfo.crInfo[i].mask != -1)
1076 && (pic16->cwInfo.crInfo[i].mask != 0))
1080 fprintf(stderr, "setting location 0x%x to value 0x%x, mask: 0x%x, test: 0x%x\n",
1081 pic16->cwInfo.confAddrStart + i,
1083 pic16->cwInfo.crInfo[i].mask,
1084 (pic16->cwInfo.crInfo[i].mask) & (~value));
1088 if ((((pic16->cwInfo.crInfo[i].mask) & (~value)) & 0xff) != ((~value) & 0xff))
1090 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
1091 __FILE__, __LINE__, address);
1096 pic16->cwInfo.crInfo[i].value = (value & 0xff);
1097 if (pic16->cwInfo.crInfo[i].andmask
1098 && ((value & 0xff) != (value & 0xff & pic16->cwInfo.crInfo[i].andmask)))
1100 // apply andmask if effective
1101 printf ("INFO: changing configuration word at 0x%x from 0x%x to 0x%x due to %s\n",
1104 (value & 0xff & pic16->cwInfo.crInfo[i].andmask),
1106 pic16->cwInfo.crInfo[i].value &= pic16->cwInfo.crInfo[i].andmask;
1108 pic16->cwInfo.crInfo[i].emit = 1;
1115 pic16_assignIdByteValue(int address, char value)
1119 for (i = 0; i < pic16->idInfo.idAddrEnd - pic16->idInfo.idAddrStart + 1; i++)
1121 if (address == pic16->idInfo.idAddrStart + i)
1123 pic16->idInfo.irInfo[i].value = value;
1124 pic16->idInfo.irInfo[i].emit = 1;