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
44 static PIC16_device Pics16[] = {
45 /* Device descriptions are now generated by scripts.
46 * For simplicities sake we just include them here.
47 * Manual overrides may be inserted on top of devices.inc
48 * or right here BEFORE inclusion of devices.inc. */
49 #include "devices.inc"
52 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
54 stats_t statistics = { 0, 0, 0, 0 };
56 #define DEFAULT_PIC "452"
58 PIC16_device *pic16=NULL;
59 unsigned int stackPos = 0;
60 unsigned int stackLen = 0;
62 void pic16_setMaxRAM(int size)
64 pic16->maxRAMaddress = size;
65 stackPos = pic16->RAMsize-1;
67 if (pic16->maxRAMaddress < 0) {
68 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
69 pic16->maxRAMaddress);
74 extern char *iComments2;
76 void pic16_dump_equates(FILE *of, set *equs)
80 r = setFirstItem(equs);
83 fprintf(of, "%s", iComments2);
84 fprintf(of, ";\tEquates to used internal registers\n");
85 fprintf(of, "%s", iComments2);
87 for(; r; r = setNextItem(equs)) {
88 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
93 void pic16_dump_access(FILE *of, set *section)
97 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)) {
106 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
107 statistics.adsize += r->size;
112 int regCompare(const void *a, const void *b)
114 const regs *const *i = a;
115 const regs *const *j = b;
117 /* sort primarily by the address */
118 if( (*i)->address > (*j)->address)return 1;
119 if( (*i)->address < (*j)->address)return -1;
121 /* and secondarily by size */
122 /* register size sorting may have strange results use with care */
123 if( (*i)->size > (*j)->size)return 1;
124 if( (*i)->size < (*j)->size)return -1;
126 /* finally if in same address and same size sort by name */
127 return (strcmp( (*i)->name, (*j)->name));
132 int symCompare(const void *a, const void *b)
134 const symbol *const *i = a;
135 const symbol *const *j = b;
137 /* sort primarily by the address */
138 if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
139 if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
141 /* and secondarily by size */
142 /* register size sorting may have strange results use with care */
143 if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
144 if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
146 /* finally if in same address and same size sort by name */
147 return (strcmp( (*i)->rname, (*j)->rname));
152 void pic16_dump_usection(FILE *of, set *section, int fix)
154 static int abs_usection_no=0;
155 static unsigned int usection_no=0;
157 unsigned int init_addr, i;
161 /* put all symbols in an array */
162 if (!elementsInSet(section)) return;
163 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
165 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
166 rlist[i] = rprev; i++;
170 if(rlist)Safe_free(rlist);
174 /* sort symbols according to their address */
175 qsort(rlist, i /*elementsInSet(section)*/, sizeof(regs *), regCompare);
179 #define EMIT_SINGLE_UDATA_SECTION 0
180 #if EMIT_SINGLE_UDATA_SECTION
181 fprintf(of, "\n\n\tudata\n");
182 for(r = setFirstItem(section); r; r = setNextItem(section)) {
183 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
184 statistics.udsize += r->size;
187 for(r = setFirstItem(section); r; r = setNextItem(section)) {
188 //fprintf(of, "\nudata_%s_%s\tudata\n", moduleName, r->name);
189 fprintf(of, "\nudata_%s_%u\tudata\n", moduleName, usection_no++);
190 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
191 statistics.udsize += r->size;
199 init_addr = rlist[j]->address;
200 deb_addr = init_addr;
201 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
205 if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
207 init_addr = r->address;
208 deb_addr = init_addr;
210 if((rprev && (init_addr > (rprev->address + rprev->size)))) {
211 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
214 if(r1 && (init_addr == r1->address)) {
215 fprintf(of, "\n%s\tres\t0\n", r->name);
217 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
219 statistics.udsize += r->size;
228 void pic16_dump_gsection(FILE *of, set *sections)
233 for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
234 if(!strcmp(sname->name, "access"))continue;
235 fprintf(of, "\n\n%s\tudata\n", sname->name);
237 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
239 fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
240 r->name, sname->name, sname);
242 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
243 statistics.udsize += r->size;
249 /* forward declaration */
250 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
251 extern void pic16_pCodeConstString(char *name, char *value);
253 void pic16_dump_isection(FILE *of, set *section, int fix)
255 static int abs_isection_no=0;
257 unsigned int init_addr, i;
260 /* put all symbols in an array */
261 if (!elementsInSet(section)) return;
262 slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
264 for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
265 slist[i] = sprev; i++;
269 if(slist)Safe_free(slist);
273 /* sort symbols according to their address */
274 qsort(slist, i, sizeof(symbol *), symCompare);
279 fprintf(of, "\n\n\tidata\n");
280 for(s = setFirstItem(section); s; s = setNextItem(section)) {
283 fprintf(of, "%s", s->rname);
284 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
285 pic16_flushDB('f', (void *)of);
287 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
288 && SPEC_CVAL (s->etype).v_char) {
290 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
291 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
303 init_addr = SPEC_ADDR(slist[j]->etype);
304 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
308 if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
310 init_addr = SPEC_ADDR(s->etype);
312 if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
313 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
317 fprintf(of, "%s", s->rname);
318 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
319 pic16_flushDB('f', (void *)of);
321 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
322 && SPEC_CVAL (s->etype).v_char) {
324 // fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
325 pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
339 void pic16_dump_int_registers(FILE *of, set *section)
345 /* put all symbols in an array */
346 if (!elementsInSet(section)) return;
347 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
349 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
350 rlist[i] = rprev; i++;
353 /* sort symbols according to their address */
354 qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
357 if(rlist)Safe_free(rlist);
361 fprintf(of, "\n\n; Internal registers\n");
363 fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
364 for(r = setFirstItem(section); r; r = setNextItem(section)) {
365 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
366 statistics.intsize += r->size;
373 #ifdef WORDS_BIGENDIAN
374 #define _ENDIAN(x) (3-x)
376 #define _ENDIAN(x) (x)
380 /*-----------------------------------------------------------------*
381 * void pic16_list_valid_pics(int ncols, int list_alias)
383 * Print out a formatted list of valid PIC devices
385 * ncols - number of columns in the list.
387 * list_alias - if non-zero, print all of the supported aliases
388 * for a device (e.g. F84, 16F84, etc...)
389 *-----------------------------------------------------------------*/
390 void pic16_list_valid_pics(int ncols, int list_alias)
396 list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
398 /* decrement the column number if it's greater than zero */
399 ncols = (ncols > 1) ? ncols-1 : 4;
401 /* Find the device with the longest name */
402 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
403 for(j=0; j<=list_alias; j++) {
404 k = strlen(Pics16[i].name[j]);
412 for(i=0; i < num_of_supported_PICS; i++) {
416 fprintf(stderr,"%s", Pics16[i].name[j]);
418 l = longest + 2 - strlen(Pics16[i].name[j]);
429 } while(++j<list_alias);
437 /*-----------------------------------------------------------------*
439 *-----------------------------------------------------------------*/
440 PIC16_device *pic16_find_device(char *name)
448 for(i = 0; i<num_of_supported_PICS; i++) {
450 for(j=0; j<PROCESSOR_NAMES; j++)
451 if(!STRCASECMP(Pics16[i].name[j], name) )
459 /*-----------------------------------------------------------------*
461 *-----------------------------------------------------------------*/
462 void pic16_init_pic(char *pic_type)
464 pic16 = pic16_find_device(pic_type);
468 fprintf(stderr, "'%s' was not found.\n", pic_type);
470 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
472 fprintf(stderr,"Valid devices are:\n");
474 pic16_list_valid_pics(4,0);
478 // printf("PIC processor found and initialized: %s\n", pic_type);
479 pic16_setMaxRAM( 0xfff );
482 /*-----------------------------------------------------------------*
484 *-----------------------------------------------------------------*/
485 int pic16_picIsInitialized(void)
487 if(pic16 && pic16->maxRAMaddress > 0)
494 /*-----------------------------------------------------------------*
495 * char *pic16_processor_base_name(void) - Include file is derived from this.
496 *-----------------------------------------------------------------*/
497 char *pic16_processor_base_name(void)
503 return pic16->name[0];
506 #define DEBUG_CHECK 0
509 * return 1 if register wasn't found and added, 0 otherwise
511 int checkAddReg(set **set, regs *reg)
518 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
521 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
522 if(!strcmp(tmp->name, reg->name))break;
528 fprintf(stderr, "added\n");
534 fprintf(stderr, "already added\n");
539 int checkAddSym(set **set, symbol *sym)
545 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
548 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
549 if(!strcmp(tmp->name, sym->name))break;
555 fprintf(stderr, "added\n");
561 fprintf(stderr, "already added\n");
567 int checkSym(set *set, symbol *sym)
574 fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
577 for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
578 if(!strcmp(tmp->name, sym->name))break;
583 fprintf(stderr, "not found\n");
589 fprintf(stderr, "found\n");
595 /*-----------------------------------------------------------------*
596 * void pic16_groupRegistersInSection - add each register to its *
597 * corresponding section *
598 *-----------------------------------------------------------------*/
599 void pic16_groupRegistersInSection(set *regset)
605 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
608 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
609 __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
612 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
615 /* avoid grouping registers that have an initial value,
616 * they will be added later in idataSymSet */
617 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
621 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
622 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
623 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
624 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
625 (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
629 for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
630 if(!strcmp(ssym->name, reg->name)) {
631 // fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
632 // __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
633 if(strcmp(ssym->section->name, "access")) {
634 addSet(&ssym->section->regsSet, reg);
645 if(docontinue)continue;
647 // fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
649 if(reg->alias == 0x80) {
650 checkAddReg(&pic16_equ_data, reg);
653 checkAddReg(&pic16_fix_udata, reg);
656 if(reg->pc_type == PO_GPR_TEMP)
657 checkAddReg(&pic16_int_regs, reg);
659 if(reg->accessBank) {
660 if(reg->alias != 0x40)
661 checkAddReg(&pic16_acs_udata, reg);
663 checkAddReg(&pic16_rel_udata, reg);
674 /*-----------------------------------------------------------------*
675 * void pic16_assignConfigWordValue(int address, int value)
677 * All high performance RISC CPU PICs have seven config word starting
678 * at address 0x300000.
679 * This routine will assign a value to that address.
681 *-----------------------------------------------------------------*/
682 void pic16_assignConfigWordValue(int address, unsigned int value)
686 for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
687 if((address == pic16->cwInfo.confAddrStart+i)
688 && (pic16->cwInfo.crInfo[i].mask != -1)) {
691 fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
692 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
693 pic16->cwInfo.crInfo[i].mask,
694 (pic16->cwInfo.crInfo[i].mask) & (~value));
698 if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
699 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
700 __FILE__, __LINE__, address);
705 pic16->cwInfo.crInfo[i].value = value;
706 pic16->cwInfo.crInfo[i].emit = 1;
712 void pic16_assignIdByteValue(int address, char value)
716 for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
717 if(address == pic16->idInfo.idAddrStart+i) {
718 pic16->idInfo.irInfo[i].value = value;
719 pic16->idInfo.irInfo[i].emit = 1;