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;
200 init_addr = (rlist[j]->address & 0x0FFF); // warning(s) emitted below
201 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, (init_addr & 0x0FFF));
205 if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
207 init_addr = (r->address & 0x0FFF);
208 if (init_addr != r->address) {
209 fprintf (stderr, "%s: WARNING: Changed address of pinned variable %s from 0x%x to 0x%x\n",
210 moduleName, r->name, r->address, init_addr);
213 if((rprev && (init_addr != ((rprev->address & 0x0FFF) + rprev->size)))) {
214 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
217 /* XXX: Does not handle partial overlap correctly. */
218 if(r1 && (init_addr == (r1->address & 0x0FFF))) {
219 fprintf(of, "\n%s\tres\t0\n", r->name);
221 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
222 statistics.udsize += r->size;
231 void pic16_dump_gsection(FILE *of, set *sections)
236 for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
237 if(!strcmp(sname->name, "access"))continue;
238 fprintf(of, "\n\n%s\tudata\n", sname->name);
240 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
242 fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
243 r->name, sname->name, sname);
245 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
246 statistics.udsize += r->size;
252 /* forward declaration */
253 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
254 extern void pic16_pCodeConstString(char *name, char *value, unsigned length);
256 void pic16_dump_isection(FILE *of, set *section, int fix)
258 static int abs_isection_no=0;
260 unsigned int init_addr, i;
263 /* put all symbols in an array */
264 if (!elementsInSet(section)) return;
265 slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
267 for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
268 slist[i] = sprev; i++;
272 if(slist)Safe_free(slist);
276 /* sort symbols according to their address */
277 qsort(slist, i, sizeof(symbol *), symCompare);
282 fprintf(of, "\n\n\tidata\n");
283 for(s = setFirstItem(section); s; s = setNextItem(section)) {
286 fprintf(of, "%s", s->rname);
287 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
288 pic16_flushDB('f', (void *)of);
290 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
291 && SPEC_CVAL (s->etype).v_char) {
293 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
294 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char, getSize(s->type));
306 init_addr = SPEC_ADDR(slist[j]->etype);
307 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
311 if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
313 init_addr = SPEC_ADDR(s->etype);
315 if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
316 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
320 fprintf(of, "%s", s->rname);
321 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
322 pic16_flushDB('f', (void *)of);
324 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
325 && SPEC_CVAL (s->etype).v_char) {
327 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
328 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char, getSize(s->type));
342 void pic16_dump_int_registers(FILE *of, set *section)
348 /* put all symbols in an array */
349 if (!elementsInSet(section)) return;
350 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
352 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
353 rlist[i] = rprev; i++;
356 /* sort symbols according to their address */
357 qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
360 if(rlist)Safe_free(rlist);
364 fprintf(of, "\n\n; Internal registers\n");
366 fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
367 for(r = setFirstItem(section); r; r = setNextItem(section)) {
368 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
369 statistics.intsize += r->size;
376 * Find the device structure for the named device.
377 * Consider usind pic16_find_device() instead!
380 * a name for the desired device
382 * a pointer to the head of the list of devices
384 * a pointer to the structure for the desired
387 static PIC16_device *
388 find_in_list(const char *name, PIC16_device *head)
393 for (i = 0; i < 4; i++) {
394 if (0 == strcmp(head->name[i], name)) {
406 * Print a list of supported devices.
407 * If --verbose was given, also emit key characteristics (memory size,
408 * access bank split point, address range of SFRs and config words).
411 * a pointer to the head of the list of devices
414 pic16_list_devices(PIC16_device *head)
418 if (options.verbose) {
419 printf("device RAM split config words\n");
422 printf("%-10s ", head->name[0]);
423 if (options.verbose) {
424 printf("%5d 0x%02x 0x%06x..0x%06x\n",
427 head->cwInfo.confAddrStart,
428 head->cwInfo.confAddrEnd);
441 * Read a single line from the given file.
444 * a pointer to the open file to read
446 * a pointer to a malloc'ed copy of the (next) line, or NULL
449 get_line (FILE *file)
451 static struct dbuf_s dbuf;
452 static int initialized = 0;
456 dbuf_init (&dbuf, 129);
460 dbuf_set_length (&dbuf, 0);
463 if (dbuf_getline (&dbuf, file) != 0)
466 /* (char *) type cast is an ugly hack since pic16_find_device() modifies the buffer */
467 return (char *)dbuf_get_buf (&dbuf);
478 * Truncate the given string in place (!) at the first '#' character (if any).
481 * a pointer to the string to truncate
483 * a pointer to the truncated string (i.e., line)
486 strip_comment (char *line)
495 while (0 != (c = *l)) {
507 * Report errors in the device specification.
510 * a pointer to the detailed message
512 #define SYNTAX(msg) do { \
513 fprintf(stderr, "%s:%d: Syntax error: %s\n", \
514 DEVICE_FILE_NAME, lineno, msg); \
518 * Locate and read in the device specification (if required) and
519 * return the device structure for the named device.
522 * a pointer to the name of the desired device
524 * a pointer to the device structure, or NULL
526 static PIC16_device *
527 pic16_find_device(const char *name)
530 char buffer[PATH_MAX];
532 const char *sep = " \t\n\r";
534 PIC16_device *d = NULL, *template;
535 PIC16_device *head = NULL, *tail = NULL;
536 set *_sets[] = { userIncDirsSet, includeDirsSet };
537 set **sets = &_sets[0];
543 //printf("%s: searching %s\n", __func__, DEVICE_FILE_NAME);
545 // locate the specification file in the include search paths
546 for (i = 0; (NULL == f) && (i < 2); i++) {
547 for (path = setFirstItem(sets[i]);
549 path = setNextItem(sets[i]))
551 SNPRINTF(&buffer[0], PATH_MAX, "%s%s%s",
552 path, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
553 //printf("%s: checking %s\n", __func__, &buffer[0]);
554 f = fopen(&buffer[0], "r");
560 // list already set up, nothing to do
561 } else if (NULL == f) {
562 fprintf(stderr, "ERROR: device list %s not found, specify its path via -I<path>\n", DEVICE_FILE_NAME);
565 // parse the specification file and construct a linked list of
568 while (NULL != (line = get_line(f))) {
570 //printf("%s: read %s\n", __func__, line);
572 key = strtok(line, sep);
574 // empty line---ignore
575 } else if (0 == strcmp(key, "name")) {
587 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[3]);
588 if ((1 < res) || (3 > strlen(&buffer[3]))) {
589 SYNTAX("<name> (e.g., 18f452) expected.");
591 d = Safe_calloc(1, sizeof(PIC16_device));
593 // { "p18f452", "18f452", "pic18f452", "f452" }
597 d->name[3] = Safe_strdup(&buffer[5]);
598 d->name[2] = Safe_strdup(&buffer[0]);
599 d->name[1] = Safe_strdup(&buffer[3]);
601 d->name[0] = Safe_strdup(&buffer[2]);
603 } else if (0 == strcmp(key, "using")) {
605 res = sscanf(&line[1+strlen(key)], " %16s", &buffer[0]);
606 if ((1 < res) || (3 > strlen(&buffer[3]))) {
607 SYNTAX("<name> (e.g., 18f452) expected.");
609 template = find_in_list(&buffer[0], head);
611 SYNTAX("<name> (e.g., 18f452) expected.");
613 memcpy(&d->RAMsize, &template->RAMsize,
614 ((char *)&d->next) - ((char *)&d->RAMsize));
617 } else if (0 == strcmp(key, "ramsize")) {
619 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
621 SYNTAX("<bytes> (e.g., 256) expected.");
625 } else if (0 == strcmp(key, "split")) {
627 res = sscanf(&line[1+strlen(key)], " %i", &val[0]);
629 SYNTAX("<offset> (e.g., 0x80) expected.");
631 d->acsSplitOfs = val[0];
633 } else if (0 == strcmp(key, "configrange")) {
634 // configrange %<first>i %<last>i
635 res = sscanf(&line[1+strlen(key)], " %i %i",
638 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
640 d->cwInfo.confAddrStart = val[0];
641 d->cwInfo.confAddrEnd = val[1];
643 } else if (0 == strcmp(key, "configword")) {
644 // configword %<address>i %<mask>i %<value>i
645 res = sscanf(&line[1+strlen(key)], " %i %i %i",
646 &val[0], &val[1], &val[2]);
648 SYNTAX("<address> <mask> <value> (e.g., 0x200001 0x0f 0x07) expected.");
650 val[0] -= d->cwInfo.confAddrStart;
652 || (val[0] > (d->cwInfo.confAddrEnd - d->cwInfo.confAddrStart))
653 || (val[0] >= CONFIGURATION_WORDS))
655 SYNTAX("address out of bounds.");
657 d->cwInfo.crInfo[val[0]].mask = val[1];
658 d->cwInfo.crInfo[val[0]].value = val[2];
661 } else if (0 == strcmp(key, "idlocrange")) {
662 // idlocrange %<first>i %<last>i
663 res = sscanf(&line[1+strlen(key)], " %i %i",
666 SYNTAX("<first> <last> (e.g., 0xf60 0xfff) expected.");
668 d->idInfo.idAddrStart = val[0];
669 d->idInfo.idAddrEnd = val[1];
671 } else if (0 == strcmp(key, "idword")) {
672 // idword %<address>i %<value>i
673 res = sscanf(&line[1+strlen(key)], " %i %i",
676 SYNTAX("<address> <value> (e.g., 0x3fffff 0x00) expected.");
678 val[0] -= d->idInfo.idAddrStart;
680 || (val[0] > (d->idInfo.idAddrEnd - d->idInfo.idAddrStart))
681 || (val[0] >= IDLOCATION_BYTES))
683 SYNTAX("address out of bounds.");
685 d->idInfo.irInfo[val[0]].value = val[1];
689 printf("%s: Invalid keyword in %s ignored: %s\n",
690 __func__, DEVICE_FILE_NAME, key);
709 d = find_in_list(name, devices);
717 /*-----------------------------------------------------------------*
719 *-----------------------------------------------------------------*/
720 void pic16_init_pic(const char *pic_type)
722 pic16 = pic16_find_device(pic_type);
724 if (&default_device == pic16) {
726 fprintf(stderr, "'%s' was not found.\n", pic_type);
728 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
732 fprintf(stderr,"Valid devices are (use --verbose for more details):\n");
733 pic16_list_devices(devices);
739 /*-----------------------------------------------------------------*
740 * char *pic16_processor_base_name(void) - Include file is derived from this.
741 *-----------------------------------------------------------------*/
742 char *pic16_processor_base_name(void)
748 return pic16->name[0];
751 #define DEBUG_CHECK 0
754 * return 1 if register wasn't found and added, 0 otherwise
756 int checkAddReg(set **set, regs *reg)
763 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
766 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
767 if(!strcmp(tmp->name, reg->name))break;
773 fprintf(stderr, "added\n");
779 fprintf(stderr, "already added\n");
784 int checkAddSym(set **set, symbol *sym)
790 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
793 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
794 if(!strcmp(tmp->name, sym->name))break;
800 fprintf(stderr, "added\n");
806 fprintf(stderr, "already added\n");
812 int checkSym(set *set, symbol *sym)
819 fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
822 for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
823 if(!strcmp(tmp->name, sym->name))break;
828 fprintf(stderr, "not found\n");
834 fprintf(stderr, "found\n");
840 /*-----------------------------------------------------------------*
841 * void pic16_groupRegistersInSection - add each register to its *
842 * corresponding section *
843 *-----------------------------------------------------------------*/
844 void pic16_groupRegistersInSection(set *regset)
850 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
853 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
854 __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
857 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
860 /* avoid grouping registers that have an initial value,
861 * they will be added later in idataSymSet */
862 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
866 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
867 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
868 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
869 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
870 (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
874 for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
875 if(!strcmp(ssym->name, reg->name)) {
876 // fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
877 // __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
878 if(strcmp(ssym->section->name, "access")) {
879 addSet(&ssym->section->regsSet, reg);
890 if(docontinue)continue;
892 // fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
894 if(reg->alias == 0x80) {
895 checkAddReg(&pic16_equ_data, reg);
898 checkAddReg(&pic16_fix_udata, reg);
901 if(reg->pc_type == PO_GPR_TEMP)
902 checkAddReg(&pic16_int_regs, reg);
904 if(reg->accessBank) {
905 if(reg->alias != 0x40)
906 checkAddReg(&pic16_acs_udata, reg);
908 checkAddReg(&pic16_rel_udata, reg);
919 /*-----------------------------------------------------------------*
920 * void pic16_assignConfigWordValue(int address, int value)
922 * All high performance RISC CPU PICs have seven config word starting
923 * at address 0x300000.
924 * This routine will assign a value to that address.
926 *-----------------------------------------------------------------*/
927 void pic16_assignConfigWordValue(int address, unsigned int value)
931 for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
932 if((address == pic16->cwInfo.confAddrStart+i)
933 && (pic16->cwInfo.crInfo[i].mask != -1)
934 && (pic16->cwInfo.crInfo[i].mask != 0)) {
937 fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
938 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
939 pic16->cwInfo.crInfo[i].mask,
940 (pic16->cwInfo.crInfo[i].mask) & (~value));
944 if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
945 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
946 __FILE__, __LINE__, address);
951 pic16->cwInfo.crInfo[i].value = value;
952 pic16->cwInfo.crInfo[i].emit = 1;
958 void pic16_assignIdByteValue(int address, char value)
962 for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
963 if(address == pic16->idInfo.idAddrStart+i) {
964 pic16->idInfo.irInfo[i].value = value;
965 pic16->idInfo.irInfo[i].emit = 1;