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 VR - Began writing code to make PIC16 C source files independent from
26 the header file (created by the inc2h.pl)
28 - adding maximum RAM memory into PIC_Device structure
34 #include "common.h" // Include everything in the SDCC src directory
36 #include "dbuf_string.h"
44 stats_t statistics = { 0, 0, 0, 0 };
46 #define DEVICE_FILE_NAME "pic16devices.txt"
48 static PIC16_device default_device = {
49 { "p18f452", "18f452", "pic18f452", "f452" },
52 { /* configuration words */
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 */ }
64 { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
65 { 0, 0 }, { 0, 0 }, { 0, 0 } }
70 PIC16_device *pic16 = &default_device;
71 static PIC16_device *devices = NULL;
73 extern set *includeDirsSet;
74 extern set *userIncDirsSet;
76 extern char *iComments2;
78 void pic16_dump_equates(FILE *of, set *equs)
82 r = setFirstItem(equs);
85 fprintf(of, "%s", iComments2);
86 fprintf(of, ";\tEquates to used internal registers\n");
87 fprintf(of, "%s", iComments2);
89 for(; r; r = setNextItem(equs)) {
90 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
95 void pic16_dump_access(FILE *of, set *section)
99 r = setFirstItem(section);
102 fprintf(of, "%s", iComments2);
103 fprintf(of, ";\tAccess bank symbols\n");
104 fprintf(of, "%s", iComments2);
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;
114 int regCompare(const void *a, const void *b)
116 const regs *const *i = a;
117 const regs *const *j = b;
119 /* sort primarily by the address */
120 if( (*i)->address > (*j)->address)return 1;
121 if( (*i)->address < (*j)->address)return -1;
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;
128 /* finally if in same address and same size sort by name */
129 return (strcmp( (*i)->name, (*j)->name));
134 int symCompare(const void *a, const void *b)
136 const symbol *const *i = a;
137 const symbol *const *j = b;
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;
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;
148 /* finally if in same address and same size sort by name */
149 return (strcmp( (*i)->rname, (*j)->rname));
154 void pic16_dump_usection(FILE *of, set *section, int fix)
156 static int abs_usection_no=0;
157 static unsigned int usection_no=0;
159 unsigned int init_addr, i;
163 /* put all symbols in an array */
164 if (!elementsInSet(section)) return;
165 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
167 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
168 rlist[i] = rprev; i++;
172 if(rlist)Safe_free(rlist);
176 /* sort symbols according to their address */
177 qsort(rlist, i /*elementsInSet(section)*/, sizeof(regs *), regCompare);
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;
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;
201 init_addr = rlist[j]->address;
202 deb_addr = init_addr;
203 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
207 if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
209 init_addr = r->address;
210 deb_addr = init_addr;
212 if((rprev && (init_addr > (rprev->address + rprev->size)))) {
213 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
216 if(r1 && (init_addr == r1->address)) {
217 fprintf(of, "\n%s\tres\t0\n", r->name);
219 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
221 statistics.udsize += r->size;
230 void pic16_dump_gsection(FILE *of, set *sections)
235 for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
236 if(!strcmp(sname->name, "access"))continue;
237 fprintf(of, "\n\n%s\tudata\n", sname->name);
239 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
241 fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
242 r->name, sname->name, sname);
244 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
245 statistics.udsize += r->size;
251 /* forward declaration */
252 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
253 extern void pic16_pCodeConstString(char *name, char *value, unsigned length);
255 void pic16_dump_isection(FILE *of, set *section, int fix)
257 static int abs_isection_no=0;
259 unsigned int init_addr, i;
262 /* put all symbols in an array */
263 if (!elementsInSet(section)) return;
264 slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
266 for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
267 slist[i] = sprev; i++;
271 if(slist)Safe_free(slist);
275 /* sort symbols according to their address */
276 qsort(slist, i, sizeof(symbol *), symCompare);
281 fprintf(of, "\n\n\tidata\n");
282 for(s = setFirstItem(section); s; s = setNextItem(section)) {
285 fprintf(of, "%s", s->rname);
286 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
287 pic16_flushDB('f', (void *)of);
289 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
290 && SPEC_CVAL (s->etype).v_char) {
292 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
293 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char, getSize(s->type));
305 init_addr = SPEC_ADDR(slist[j]->etype);
306 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
310 if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
312 init_addr = SPEC_ADDR(s->etype);
314 if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
315 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
319 fprintf(of, "%s", s->rname);
320 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
321 pic16_flushDB('f', (void *)of);
323 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
324 && SPEC_CVAL (s->etype).v_char) {
326 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
327 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char, getSize(s->type));
341 void pic16_dump_int_registers(FILE *of, set *section)
347 /* put all symbols in an array */
348 if (!elementsInSet(section)) return;
349 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
351 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
352 rlist[i] = rprev; i++;
355 /* sort symbols according to their address */
356 qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
359 if(rlist)Safe_free(rlist);
363 fprintf(of, "\n\n; Internal registers\n");
365 fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
366 for(r = setFirstItem(section); r; r = setNextItem(section)) {
367 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
368 statistics.intsize += r->size;
375 * Find the device structure for the named device.
376 * Consider usind pic16_find_device() instead!
379 * a name for the desired device
381 * a pointer to the head of the list of devices
383 * a pointer to the structure for the desired
386 static PIC16_device *
387 find_in_list(const char *name, PIC16_device *head)
392 for (i = 0; i < 4; i++) {
393 if (0 == strcmp(head->name[i], name)) {
405 * Print a list of supported devices.
406 * If --verbose was given, also emit key characteristics (memory size,
407 * access bank split point, address range of SFRs and config words).
410 * a pointer to the head of the list of devices
413 pic16_list_devices(PIC16_device *head)
417 if (options.verbose) {
418 printf("device RAM split config words\n");
421 printf("%-10s ", head->name[0]);
422 if (options.verbose) {
423 printf("%5d 0x%02x 0x%06x..0x%06x\n",
426 head->cwInfo.confAddrStart,
427 head->cwInfo.confAddrEnd);
440 * Read a single line from the given file.
443 * a pointer to the open file to read
445 * a pointer to a malloc'ed copy of the (next) line, or NULL
448 get_line (FILE *file)
450 static struct dbuf_s dbuf;
451 static int initialized = 0;
455 dbuf_init (&dbuf, 129);
459 dbuf_set_length (&dbuf, 0);
462 if (dbuf_getline (&dbuf, file) != 0)
465 /* (char *) type cast is an ugly hack since pic16_find_device() modifies the buffer */
466 return (char *)dbuf_get_buf (&dbuf);
477 * Truncate the given string in place (!) at the first '#' character (if any).
480 * a pointer to the string to truncate
482 * a pointer to the truncated string (i.e., line)
485 strip_comment (char *line)
494 while (0 != (c = *l)) {
506 * Report errors in the device specification.
509 * a pointer to the detailed message
511 #define SYNTAX(msg) do { \
512 fprintf(stderr, "%s:%d: Syntax error: %s\n", \
513 DEVICE_FILE_NAME, lineno, msg); \
517 * Locate and read in the device specification (if required) and
518 * return the device structure for the named device.
521 * a pointer to the name of the desired device
523 * a pointer to the device structure, or NULL
525 static PIC16_device *
526 pic16_find_device(const char *name)
529 char buffer[PATH_MAX];
531 const char *sep = " \t\n\r";
533 PIC16_device *d = NULL, *template;
534 PIC16_device *head = NULL, *tail = NULL;
535 set *_sets[] = { userIncDirsSet, includeDirsSet };
536 set **sets = &_sets[0];
542 //printf("%s: searching %s\n", __func__, DEVICE_FILE_NAME);
544 // locate the specification file in the include search paths
545 for (i = 0; (NULL == f) && (i < 2); i++) {
546 for (path = setFirstItem(sets[i]);
548 path = setNextItem(sets[i]))
550 SNPRINTF(&buffer[0], PATH_MAX, "%s%s%s",
551 path, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
552 //printf("%s: checking %s\n", __func__, &buffer[0]);
553 f = fopen(&buffer[0], "r");
559 // list already set up, nothing to do
560 } else if (NULL == f) {
561 fprintf(stderr, "ERROR: device list %s not found, specify its path via -I<path>\n", DEVICE_FILE_NAME);
564 // parse the specification file and construct a linked list of
567 while (NULL != (line = get_line(f))) {
569 //printf("%s: read %s\n", __func__, line);
571 key = strtok(line, sep);
573 // empty line---ignore
574 } else if (0 == strcmp(key, "name")) {
586 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[3]);
587 if ((1 < res) || (3 > strlen(&buffer[3]))) {
588 SYNTAX("<name> (e.g., 18f452) expected.");
590 d = Safe_calloc(1, sizeof(PIC16_device));
592 // { "p18f452", "18f452", "pic18f452", "f452" }
596 d->name[3] = Safe_strdup(&buffer[5]);
597 d->name[2] = Safe_strdup(&buffer[0]);
598 d->name[1] = Safe_strdup(&buffer[3]);
600 d->name[0] = Safe_strdup(&buffer[2]);
602 } else if (0 == strcmp(key, "using")) {
604 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[0]);
605 if ((1 < res) || (3 > strlen(&buffer[3]))) {
606 SYNTAX("<name> (e.g., 18f452) expected.");
608 template = find_in_list(&buffer[0], head);
610 SYNTAX("<name> (e.g., 18f452) expected.");
612 memcpy(&d->RAMsize, &template->RAMsize,
613 ((char *)&d->next) - ((char *)&d->RAMsize));
616 } else if (0 == strcmp(key, "ramsize")) {
618 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
620 SYNTAX("<bytes> (e.g., 256) expected.");
624 } else if (0 == strcmp(key, "split")) {
626 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
628 SYNTAX("<offset> (e.g., 0x80) expected.");
630 d->acsSplitOfs = val[0];
632 } else if (0 == strcmp(key, "configrange")) {
633 // configrange %<first>i %<last>i
634 res = sscanf(&line[1+strlen(key)], " %i %i",
637 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
639 d->cwInfo.confAddrStart = val[0];
640 d->cwInfo.confAddrEnd = val[1];
642 } else if (0 == strcmp(key, "configword")) {
643 // configword %<address>i %<mask>i %<value>i
644 res = sscanf(&line[1+strlen(key)], " %i %i %i",
645 &val[0], &val[1], &val[2]);
647 SYNTAX("<address> <mask> <value> (e.g., 0x200001 0x0f 0x07) expected.");
649 val[0] -= d->cwInfo.confAddrStart;
651 || (val[0] > (d->cwInfo.confAddrEnd - d->cwInfo.confAddrStart))
652 || (val[0] >= CONFIGURATION_WORDS))
654 SYNTAX("address out of bounds.");
656 d->cwInfo.crInfo[val[0]].mask = val[1];
657 d->cwInfo.crInfo[val[0]].value = val[2];
660 } else if (0 == strcmp(key, "idlocrange")) {
661 // idlocrange %<first>i %<last>i
662 res = sscanf(&line[1+strlen(key)], " %i %i",
665 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
667 d->idInfo.idAddrStart = val[0];
668 d->idInfo.idAddrEnd = val[1];
670 } else if (0 == strcmp(key, "idword")) {
671 // idword %<address>i %<value>i
672 res = sscanf(&line[1+strlen(key)], " %i %i",
675 SYNTAX("<address> <value> (e.g., 0x3fffff 0x00) expected.");
677 val[0] -= d->idInfo.idAddrStart;
679 || (val[0] > (d->idInfo.idAddrEnd - d->idInfo.idAddrStart))
680 || (val[0] >= IDLOCATION_BYTES))
682 SYNTAX("address out of bounds.");
684 d->idInfo.irInfo[val[0]].value = val[1];
688 printf("%s: Invalid keyword in %s ignored: %s\n",
689 __func__, DEVICE_FILE_NAME, key);
708 d = find_in_list(name, devices);
716 /*-----------------------------------------------------------------*
718 *-----------------------------------------------------------------*/
719 void pic16_init_pic(const char *pic_type)
721 pic16 = pic16_find_device(pic_type);
723 if (&default_device == pic16) {
725 fprintf(stderr, "'%s' was not found.\n", pic_type);
727 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
731 fprintf(stderr,"Valid devices are (use --verbose for more details):\n");
732 pic16_list_devices(devices);
738 /*-----------------------------------------------------------------*
739 * char *pic16_processor_base_name(void) - Include file is derived from this.
740 *-----------------------------------------------------------------*/
741 char *pic16_processor_base_name(void)
747 return pic16->name[0];
750 #define DEBUG_CHECK 0
753 * return 1 if register wasn't found and added, 0 otherwise
755 int checkAddReg(set **set, regs *reg)
762 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
765 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
766 if(!strcmp(tmp->name, reg->name))break;
772 fprintf(stderr, "added\n");
778 fprintf(stderr, "already added\n");
783 int checkAddSym(set **set, symbol *sym)
789 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
792 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
793 if(!strcmp(tmp->name, sym->name))break;
799 fprintf(stderr, "added\n");
805 fprintf(stderr, "already added\n");
811 int checkSym(set *set, symbol *sym)
818 fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
821 for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
822 if(!strcmp(tmp->name, sym->name))break;
827 fprintf(stderr, "not found\n");
833 fprintf(stderr, "found\n");
839 /*-----------------------------------------------------------------*
840 * void pic16_groupRegistersInSection - add each register to its *
841 * corresponding section *
842 *-----------------------------------------------------------------*/
843 void pic16_groupRegistersInSection(set *regset)
849 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
852 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
853 __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
856 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
859 /* avoid grouping registers that have an initial value,
860 * they will be added later in idataSymSet */
861 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
865 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
866 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
867 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
868 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
869 (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
873 for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
874 if(!strcmp(ssym->name, reg->name)) {
875 // fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
876 // __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
877 if(strcmp(ssym->section->name, "access")) {
878 addSet(&ssym->section->regsSet, reg);
889 if(docontinue)continue;
891 // fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
893 if(reg->alias == 0x80) {
894 checkAddReg(&pic16_equ_data, reg);
897 checkAddReg(&pic16_fix_udata, reg);
900 if(reg->pc_type == PO_GPR_TEMP)
901 checkAddReg(&pic16_int_regs, reg);
903 if(reg->accessBank) {
904 if(reg->alias != 0x40)
905 checkAddReg(&pic16_acs_udata, reg);
907 checkAddReg(&pic16_rel_udata, reg);
918 /*-----------------------------------------------------------------*
919 * void pic16_assignConfigWordValue(int address, int value)
921 * All high performance RISC CPU PICs have seven config word starting
922 * at address 0x300000.
923 * This routine will assign a value to that address.
925 *-----------------------------------------------------------------*/
926 void pic16_assignConfigWordValue(int address, unsigned int value)
930 for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
931 if((address == pic16->cwInfo.confAddrStart+i)
932 && (pic16->cwInfo.crInfo[i].mask != -1)
933 && (pic16->cwInfo.crInfo[i].mask != 0)) {
936 fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
937 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
938 pic16->cwInfo.crInfo[i].mask,
939 (pic16->cwInfo.crInfo[i].mask) & (~value));
943 if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
944 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
945 __FILE__, __LINE__, address);
950 pic16->cwInfo.crInfo[i].value = value;
951 pic16->cwInfo.crInfo[i].emit = 1;
957 void pic16_assignIdByteValue(int address, char value)
961 for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
962 if(address == pic16->idInfo.idAddrStart+i) {
963 pic16->idInfo.irInfo[i].value = value;
964 pic16->idInfo.irInfo[i].emit = 1;