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
43 static PIC16_device Pics16[] = {
45 {"p18f242", "18f242", "pic18f242", "f242"}, // aliases
51 /* PIC18F242 configuration words */
54 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
55 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
56 { -1, 0, 0xff } /* 7 */ , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
57 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ ,
58 { 0x40, 0, 0xff } /* d */ }
63 {"p18f248", "18f248", "pic18f248", "f248"}, // aliases
69 /* PIC18F248 configuration words */
72 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
73 { -1, 0, 0xff } /* 4 */ , { -1, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
74 { -1, 0, 0xff } /* 7 */ , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
75 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ ,
76 { 0x40, 0, 0xff } /* d */ }
81 {"p18f252", "18f252", "pic18f252", "f252"}, // aliases
87 /* PIC18F252 configuration words */
90 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
91 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
92 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
93 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
94 { 0x40, 0, 0xff } /* d */ }
99 {"p18f258", "18f258", "pic18f258", "f258"}, // aliases
105 /* PIC18F258 configuration words */
108 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
109 { -1, 0, 0xff } /* 4 */ , { -1, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
110 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
111 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
112 { 0x40, 0, 0xff } /* d */ }
117 {"p18f442", "18f442", "pic18f442", "f442"}, // aliases
123 /* PIC18F442 configuration words */
126 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
127 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
128 { -1, 0, 0xff } /* 7 */ , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
129 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ ,
130 { 0x40, 0, 0xff } /* d */ }
135 {"p18f448", "18f448", "pic18f448", "f448"}, // aliases
141 /* PIC18F448 configuration words */
144 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
145 { -1, 0, 0xff } /* 4 */ , { -1, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
146 { -1, 0, 0xff } /* 7 */ , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
147 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ ,
148 { 0x40, 0, 0xff } /* d */ }
153 {"p18f452", "18f452", "pic18f452", "f452"}, // aliases
159 /* PIC18F452 configuration words */
162 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
163 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
164 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
165 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
166 { 0x40, 0, 0xff } /* d */ }
171 {"p18f458", "18f458", "pic18f458", "f458"}, // aliases
177 /* PIC18F458 configuration words */
180 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
181 { -1, 0, 0xff } /* 4 */ , { -1, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
182 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
183 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
184 { 0x40, 0, 0xff } /* d */ }
189 {"p18f1220", "18f1220", "pic18f1220", "f1220"}, // aliases
195 /* PIC18F1220 configuration words */
198 { { 0xcf, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ ,
199 { -1, 0, 0xff } /* 4 */ , { 0x80, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
200 { -1, 0, 0xff } /* 7 */ , { 0x03, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
201 { 0x03, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x03, 0, 0xff } /* c */ ,
202 { 0x40, 0, 0xff } /* d */ }
208 {"p18f6520", "18f6520", "pic18f6520", "f6520"}, // aliases
214 /* PIC18F6520 configuration words */
217 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
218 { 0x80, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */ ,
219 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
220 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
221 { 0x40, 0, 0xff } /* d */ }
226 {"p18f6620", "18f6620", "pic18f6620", "f6620"}, // aliases
232 /* PIC18F6620 configuration words */
235 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
236 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
237 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
238 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
239 { 0x40, 0, 0xff } /* d */ }
244 {"p18f6680", "18f6680", "pic18f6680", "f6680"}, // aliases
250 /* PIC18F6680 configuration words */
253 { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ ,
254 { -1, 0, 0xff } /* 4 */ , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
255 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
256 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
257 { 0x40, 0, 0xff } /* d */ }
262 {"p18f6720", "18f6720", "pic18f6720", "f6720"}, // aliases
268 /* PIC18F6720 configuration words */
271 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
272 { -1, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
273 { -1, 0, 0xff } /* 7 */ , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
274 { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ ,
275 { 0x40, 0, 0xff } /* d */ }
280 {"p18f8520", "18f8520", "pic18f8520", "f8520"}, // aliases
286 /* PIC18F8520 configuration words */
289 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
290 { 0x83, 0, 0xff } /* 4 */ , { 0x88, 0, 0xff } /* 5 */ , { -1, 0, 0xff } /* 6 */ ,
291 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
292 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
293 { 0x40, 0, 0xff } /* d */ }
298 {"p18f8620", "18f8620", "pic18f8620", "f8620"}, // aliases
304 /* PIC18F8620 configuration words */
307 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
308 { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
309 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
310 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
311 { 0x40, 0, 0xff } /* d */ }
315 {"p18f8680", "18f8680", "pic18f8680", "f8680"}, // aliases
321 /* PIC18F8680 configuration words */
324 { { 0x2f, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x1f, 0, 0xff } /* 3 */ ,
325 { 0x83, 0, 0xff } /* 4 */ , { 0x83, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
326 { -1, 0, 0xff } /* 7 */ , { 0x0f, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
327 { 0x0f, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0x0f, 0, 0xff } /* c */ ,
328 { 0x40, 0, 0xff } /* d */ }
333 {"p18f8720", "18f8720", "pic18f8720", "f8720"}, // aliases
339 /* PIC18F8720 configuration words */
342 { { 0x27, 0, 0xff } /* 1 */ , { 0x0f, 0, 0xff } /* 2 */ , { 0x0f, 0, 0xff } /* 3 */ ,
343 { 0x83, 0, 0xff } /* 4 */ , { 0x01, 0, 0xff } /* 5 */ , { 0x85, 0, 0xff } /* 6 */ ,
344 { -1, 0, 0xff } /* 7 */ , { 0xff, 0, 0xff } /* 8 */ , { 0xc0, 0, 0xff } /* 9 */ ,
345 { 0xff, 0, 0xff } /* a */ , { 0xe0, 0, 0xff } /* b */ , { 0xff, 0, 0xff } /* c */ ,
346 { 0x40, 0, 0xff } /* d */ }
352 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
354 #define DEFAULT_PIC "452"
356 PIC16_device *pic16=NULL;
357 unsigned int stackPos = 0;
359 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
361 void pic16_setMaxRAM(int size)
363 pic16->maxRAMaddress = size;
364 stackPos = pic16->RAMsize-1;
366 if (pic16->maxRAMaddress < 0) {
367 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
368 pic16->maxRAMaddress);
373 extern char *iComments2;
375 void pic16_dump_equates(FILE *of, set *equs)
379 r = setFirstItem(equs);
382 fprintf(of, "%s", iComments2);
383 fprintf(of, ";\tEquates to used internal registers\n");
384 fprintf(of, "%s", iComments2);
386 for(; r; r = setNextItem(equs)) {
387 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
392 int regCompare(const void *a, const void *b)
394 const regs *const *i = a;
395 const regs *const *j = b;
397 /* sort primarily by the address */
398 if( (*i)->address > (*j)->address)return 1;
399 if( (*i)->address < (*j)->address)return -1;
401 /* and secondarily by size */
402 /* register size sorting may have strange results use with care */
403 if( (*i)->size > (*j)->size)return 1;
404 if( (*i)->size < (*j)->size)return -1;
406 /* finally if in same address and same size sort by name */
407 return (strcmp( (*i)->name, (*j)->name));
412 int symCompare(const void *a, const void *b)
414 const symbol *const *i = a;
415 const symbol *const *j = b;
417 /* sort primarily by the address */
418 if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
419 if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
421 /* and secondarily by size */
422 /* register size sorting may have strange results use with care */
423 if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
424 if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
426 /* finally if in same address and same size sort by name */
427 return (strcmp( (*i)->rname, (*j)->rname));
432 void pic16_dump_usection(FILE *of, set *section, int fix)
434 static int abs_usection_no=0;
440 /* put all symbols in an array */
441 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
443 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
444 rlist[i] = rprev; i++;
448 if(rlist)free(rlist);
452 /* sort symbols according to their address */
453 qsort(rlist, i /*elementsInSet(section)*/, sizeof(regs *), regCompare);
456 fprintf(of, "\n\n\tudata\n");
457 for(r = setFirstItem(section); r; r = setNextItem(section)) {
458 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
465 init_addr = rlist[j]->address;
466 deb_addr = init_addr;
467 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
471 if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
473 init_addr = r->address;
474 deb_addr = init_addr;
476 if((rprev && (init_addr > (rprev->address + rprev->size)))) {
477 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
480 if(r1 && (init_addr == r1->address)) {
481 fprintf(of, "\n%s\tres\t0\n", r->name);
483 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
494 /* forward declaration */
495 void print_idata(FILE *of, symbol * sym, sym_link * type, initList * ilist);
497 void pic16_dump_isection(FILE *of, set *section, int fix)
499 static int abs_isection_no=0;
504 /* put all symbols in an array */
505 slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
507 for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
508 slist[i] = sprev; i++;
512 if(slist)free(slist);
516 /* sort symbols according to their address */
517 qsort(slist, i, sizeof(symbol *), symCompare);
520 fprintf(of, "\n\n\tidata\n");
521 for(s = setFirstItem(section); s; s = setNextItem(section)) {
522 print_idata(of, s, s->type, s->ival);
529 init_addr = SPEC_ADDR(slist[j]->etype);
530 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
534 if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
536 init_addr = SPEC_ADDR(s->etype);
538 if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
539 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
542 print_idata(of, s, s->type, s->ival);
545 if(r1 && (init_addr == r1->address)) {
546 fprintf(of, "%s\tres\t0\n\n", r->name);
548 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
559 void pic16_dump_int_registers(FILE *of, set *section)
565 /* put all symbols in an array */
566 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
568 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
569 rlist[i] = rprev; i++;
572 /* sort symbols according to their address */
573 qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
576 if(rlist)free(rlist);
580 fprintf(of, "\n\n; Internal registers\n");
582 fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
583 for(r = setFirstItem(section); r; r = setNextItem(section))
584 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
590 #ifdef WORDS_BIGENDIAN
591 #define _ENDIAN(x) (3-x)
593 #define _ENDIAN(x) (x)
596 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
600 /*-----------------------------------------------------------------*/
601 /* printIvalType - generates ival for int/char */
602 /*-----------------------------------------------------------------*/
603 void print_idataType (FILE *of, symbol *sym, sym_link * type, initList * ilist)
608 //fprintf(stderr, "%s\n",__FUNCTION__);
610 /* if initList is deep */
611 if (ilist->type == INIT_DEEP)
612 ilist = ilist->init.deep;
614 if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
615 werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
618 if (!(val = list2val (ilist))) {
619 // assuming a warning has been thrown
623 if (val->type != type) {
624 val = valCastLiteral(type, floatFromVal(val));
628 ulval = (unsigned long) floatFromVal (val);
632 switch (getSize (type)) {
634 if(isprint(BYTE_IN_LONG(ulval, 0)))
635 fprintf(of, "%s\tdata\t\"%c\"\n", sym->rname, (unsigned char)BYTE_IN_LONG(ulval, 0));
637 fprintf(of, "%s\tdata\t0x%02x\n", sym->rname, (unsigned char)BYTE_IN_LONG(ulval, 0));
638 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
642 fprintf(of, "%s\tdw\t0x%02x, 0x%02x\n", sym->rname, (unsigned int)(BYTE_IN_LONG(ulval, 0)),
643 (unsigned int)(BYTE_IN_LONG(ulval, 1)));
644 // fprintf(of, "%s\tdata\t0x%04x\n", sym->rname, (unsigned int)BYTE_IN_LONG(ulval, 0) +
645 // (unsigned int)(BYTE_IN_LONG(ulval, 1) << 8));
649 fprintf(of, "%s\tdw\t0x%04x,0x%04x\n", sym->rname, (unsigned int)(BYTE_IN_LONG(ulval, 0)
650 + (BYTE_IN_LONG(ulval, 1) << 8)),
651 (unsigned)(BYTE_IN_LONG(ulval, 2)
652 + (BYTE_IN_LONG(ulval, 3) << 8)));
653 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
654 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
655 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,2))));
656 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,3))));
662 /*-----------------------------------------------------------------*/
663 /* print_idataChar - generates initital value for character array */
664 /*-----------------------------------------------------------------*/
666 print_idataChar (FILE *of, symbol *sym, sym_link * type, initList * ilist, char *s)
670 char old_ch=0, *vchar;
672 // fprintf(stderr, "%s\n",__FUNCTION__);
676 val = list2val (ilist);
677 /* if the value is a character string */
678 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
680 if (!DCL_ELEM (type))
681 DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
684 fprintf(of, "%s\tdata\t", sym->rname);
685 vchar = SPEC_CVAL(val->etype).v_char;
686 for(remain=0; remain<DCL_ELEM(type); remain++) {
687 if(!isprint(old_ch)) {
688 if(isprint(vchar[remain]))fprintf(of, "%s\"%c", (remain?", ":""), vchar[remain]);
689 else fprintf(of, "0x%02x", vchar[remain]);
691 if(!isprint(vchar[remain]))fprintf(of, "\", 0x%02x", vchar[remain]);
692 else fprintf(of, "%c", vchar[remain]);
693 old_ch = vchar[ remain ];
695 // fprintf(of, "0x%02x%s", SPEC_CVAL(val->etype).v_char[ remain ], (remain==DCL_ELEM(type)-1?"":","));
698 if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0)
702 fprintf(of, "0x%02x%s", 0x00 /*SPEC_CVAL(val->etype).v_char[ remain ]*/ , (remain==DCL_ELEM(type)-1?"":","));
712 fprintf(of, "%s\tdata\t", sym->rname);
713 vchar = s; old_ch = 0;
714 for(remain=0; remain<strlen(s); remain++)
716 if(!isprint(old_ch)) {
717 if(isprint(vchar[remain]))fprintf(of, "%s\"%c", (remain?", ":""), vchar[remain]);
718 else fprintf(of, "0x%02x", vchar[remain]);
720 if(!isprint(vchar[remain]))fprintf(of, "\", 0x%02x", vchar[remain]);
721 else fprintf(of, "%c", vchar[remain]);
722 old_ch = vchar[ remain ];
723 // fprintf(of, "0x%02x%s", s[ remain ], (remain==strlen(s)-1?"":","));
729 /*-----------------------------------------------------------------*/
730 /* print_idataArray - generates code for array initialization */
731 /*-----------------------------------------------------------------*/
733 print_idataArray (FILE *of, symbol * sym, sym_link * type, initList * ilist)
736 int lcnt = 0, size = 0;
738 /* take care of the special case */
739 /* array of characters can be init */
741 if (IS_CHAR (type->next)) {
742 // fprintf(stderr,"%s:%d - is_char\n",__FUNCTION__,__LINE__);
743 if (!IS_LITERAL(list2val(ilist)->etype)) {
744 werror (W_INIT_WRONG);
747 if (print_idataChar (of, sym, type,
748 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
749 SPEC_CVAL (sym->etype).v_char))
752 /* not the special case */
753 if (ilist->type != INIT_DEEP)
755 werror (E_INIT_STRUCT, sym->name);
759 iloop = ilist->init.deep;
760 lcnt = DCL_ELEM (type);
764 //fprintf(stderr,"%s:%d - is_char\n",__FUNCTION__,__LINE__);
766 print_idata (of, sym, type->next, iloop);
767 iloop = (iloop ? iloop->next : NULL);
770 /* if not array limits given & we */
771 /* are out of initialisers then */
772 if (!DCL_ELEM (type) && !iloop)
775 /* no of elements given and we */
776 /* have generated for all of them */
778 /* if initializers left */
780 werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
786 /* if we have not been given a size */
787 if (!DCL_ELEM (type))
788 DCL_ELEM (type) = size;
793 /*-----------------------------------------------------------------*/
794 /* print_idata - generates code for initial value */
795 /*-----------------------------------------------------------------*/
796 void print_idata(FILE *of, symbol * sym, sym_link * type, initList * ilist)
801 /* if structure then */
802 if (IS_STRUCT (type))
804 //fprintf(stderr,"%s struct\n",__FUNCTION__);
805 //printIvalStruct (sym, type, ilist, oFile);
806 fprintf(stderr, "%s:%d: PIC16 port error: structure initialisation is not implemented yet.\n",
812 /* if this is a pointer */
815 //fprintf(stderr,"%s pointer\n",__FUNCTION__);
816 //printIvalPtr (sym, type, ilist, oFile);
817 fprintf(stderr, "%s:%d: PIC16 port error: pointer initialisation is not immplemented yet.\n",
823 /* if this is an array */
826 //fprintf(stderr,"%s array\n",__FUNCTION__);
827 print_idataArray (of, sym, type, ilist);
831 /* if type is SPECIFIER */
834 // fprintf(stderr,"%s spec\n",__FUNCTION__);
835 print_idataType(of, sym, type, ilist);
843 /*-----------------------------------------------------------------*
844 * void pic16_list_valid_pics(int ncols, int list_alias)
846 * Print out a formatted list of valid PIC devices
848 * ncols - number of columns in the list.
850 * list_alias - if non-zero, print all of the supported aliases
851 * for a device (e.g. F84, 16F84, etc...)
852 *-----------------------------------------------------------------*/
853 void pic16_list_valid_pics(int ncols, int list_alias)
859 list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
861 /* decrement the column number if it's greater than zero */
862 ncols = (ncols > 1) ? ncols-1 : 4;
864 /* Find the device with the longest name */
865 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
866 for(j=0; j<=list_alias; j++) {
867 k = strlen(Pics16[i].name[j]);
875 for(i=0; i < num_of_supported_PICS; i++) {
879 fprintf(stderr,"%s", Pics16[i].name[j]);
881 l = longest + 2 - strlen(Pics16[i].name[j]);
892 } while(++j<list_alias);
900 /*-----------------------------------------------------------------*
902 *-----------------------------------------------------------------*/
903 PIC16_device *pic16_find_device(char *name)
911 for(i = 0; i<num_of_supported_PICS; i++) {
913 for(j=0; j<PROCESSOR_NAMES; j++)
914 if(!STRCASECMP(Pics16[i].name[j], name) )
922 /*-----------------------------------------------------------------*
924 *-----------------------------------------------------------------*/
925 void pic16_init_pic(char *pic_type)
927 pic16 = pic16_find_device(pic_type);
931 fprintf(stderr, "'%s' was not found.\n", pic_type);
933 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
935 fprintf(stderr,"Valid devices are:\n");
937 pic16_list_valid_pics(4,0);
941 // printf("PIC processor found and initialized: %s\n", pic_type);
942 pic16_setMaxRAM( 0xfff );
945 /*-----------------------------------------------------------------*
947 *-----------------------------------------------------------------*/
948 int pic16_picIsInitialized(void)
950 if(pic16 && pic16->maxRAMaddress > 0)
957 /*-----------------------------------------------------------------*
958 * char *pic16_processor_base_name(void) - Include file is derived from this.
959 *-----------------------------------------------------------------*/
960 char *pic16_processor_base_name(void)
966 return pic16->name[0];
969 #define DEBUG_CHECK 0
972 * return 1 if register wasn't found and added, 0 otherwise
974 int checkAddReg(set **set, regs *reg)
979 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
982 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
983 if(!strcmp(tmp->name, reg->name))break;
989 fprintf(stderr, "added\n");
995 fprintf(stderr, "already added\n");
1000 int checkAddSym(set **set, symbol *sym)
1005 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
1008 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
1009 if(!strcmp(tmp->name, sym->name))break;
1015 fprintf(stderr, "added\n");
1021 fprintf(stderr, "already added\n");
1028 /*-----------------------------------------------------------------*
1029 * void pic16_groupRegistersInSection - add each register to its *
1030 * corresponding section *
1031 *-----------------------------------------------------------------*/
1032 void pic16_groupRegistersInSection(set *regset)
1036 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
1038 // fprintf(stderr, "%s:%d group registers in section, reg: %s\n", __FILE__, __LINE__, reg->name);
1041 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
1043 /* avoid grouping registers that have an initial value,
1044 * they will be added later in idataSymSet */
1045 if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
1049 fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i\n",
1050 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
1051 (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
1052 (reg->regop?(OP_SYMBOL(reg->regop)->level):-1) );
1056 checkAddReg(&pic16_equ_data, reg);
1059 checkAddReg(&pic16_fix_udata, reg);
1062 if(reg->pc_type == PO_GPR_TEMP)
1063 checkAddReg(&pic16_int_regs, reg);
1065 checkAddReg(&pic16_rel_udata, reg);
1075 /*-----------------------------------------------------------------*
1076 * void pic16_assignConfigWordValue(int address, int value)
1078 * All high performance RISC CPU PICs have seven config word starting
1079 * at address 0x300000.
1080 * This routine will assign a value to that address.
1082 *-----------------------------------------------------------------*/
1083 void pic16_assignConfigWordValue(int address, int value)
1087 for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
1088 if(pic16->cwInfo.crInfo[i].mask != -1) {
1089 // fprintf(stderr, "setting location 0x%X\n", pic16->cwInfo.confAddrStart+i);
1090 pic16->cwInfo.crInfo[i].value = value;
1091 pic16->cwInfo.crInfo[i].emit = 1;