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
53 {"p18f252", "18f252", "pic18f252", "f252"}, // aliases
61 {"p18f442", "18f442", "pic18f442", "f442"}, // aliases
69 {"p18f452", "18f452", "pic18f452", "f452"}, // aliases
77 {"p18f248", "18f248", "pic18f248", "f248"}, // aliases
85 {"p18f258", "18f258", "pic18f258", "f258"}, // aliases
93 {"p18f448", "18f448", "pic18f448", "f448"}, // aliases
101 {"p18f458", "18f458", "pic18f458", "f458"}, // aliases
109 {"p18f6520", "18f6520", "pic18f6520", "f6520"}, // aliases
117 {"p18f6620", "18f6620", "pic18f6620", "f6620"}, // aliases
124 {"p18f6680", "18f6680", "pic18f6680", "f6680"}, // aliases
131 {"p18f6720", "18f6720", "pic18f6720", "f6720"}, // aliases
138 {"p18f8520", "18f8520", "pic18f8520", "f8520"}, // aliases
145 {"p18f8620", "18f8620", "pic18f8620", "f8620"}, // aliases
152 {"p18f8680", "18f8680", "pic18f8680", "f8680"}, // aliases
159 {"p18f8720", "18f8720", "pic18f8720", "f8720"}, // aliases
168 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
170 #define DEFAULT_PIC "452"
172 PIC16_device *pic16=NULL;
174 #define DEFAULT_CONFIG_BYTE 0xff
176 #define CONFIG1H_WORD_ADDRESS 0x300001
177 #define DEFAULT_CONFIG1H_WORD DEFAULT_CONFIG_BYTE
179 #define CONFIG2L_WORD_ADDRESS 0x300002
180 #define DEFAULT_CONFIG2L_WORD DEFAULT_CONFIG_BYTE
182 #define CONFIG2H_WORD_ADDRESS 0x300003
183 #define DEFAULT_CONFIG2H_WORD DEFAULT_CONFIG_BYTE
185 #define CONFIG3H_WORD_ADDRESS 0x300005
186 #define DEFAULT_CONFIG3H_WORD DEFAULT_CONFIG_BYTE
188 #define CONFIG4L_WORD_ADDRESS 0x300006
189 #define DEFAULT_CONFIG4L_WORD DEFAULT_CONFIG_BYTE
191 #define CONFIG5L_WORD_ADDRESS 0x300008
192 #define DEFAULT_CONFIG5L_WORD DEFAULT_CONFIG_BYTE
194 #define CONFIG5H_WORD_ADDRESS 0x300009
195 #define DEFAULT_CONFIG5H_WORD DEFAULT_CONFIG_BYTE
197 #define CONFIG6L_WORD_ADDRESS 0x30000a
198 #define DEFAULT_CONFIG6L_WORD DEFAULT_CONFIG_BYTE
200 #define CONFIG6H_WORD_ADDRESS 0x30000b
201 #define DEFAULT_CONFIG6H_WORD DEFAULT_CONFIG_BYTE
203 #define CONFIG7L_WORD_ADDRESS 0x30000c
204 #define DEFAULT_CONFIG7L_WORD DEFAULT_CONFIG_BYTE
206 #define CONFIG7H_WORD_ADDRESS 0x30000d
207 #define DEFAULT_CONFIG7H_WORD DEFAULT_CONFIG_BYTE
209 static unsigned int config1h_word = DEFAULT_CONFIG1H_WORD;
210 static unsigned int config2l_word = DEFAULT_CONFIG2L_WORD;
211 static unsigned int config2h_word = DEFAULT_CONFIG2H_WORD;
212 static unsigned int config3h_word = DEFAULT_CONFIG3H_WORD;
213 static unsigned int config4l_word = DEFAULT_CONFIG4L_WORD;
214 static unsigned int config5l_word = DEFAULT_CONFIG5L_WORD;
215 static unsigned int config5h_word = DEFAULT_CONFIG5H_WORD;
216 static unsigned int config6l_word = DEFAULT_CONFIG6L_WORD;
217 static unsigned int config6h_word = DEFAULT_CONFIG6H_WORD;
218 static unsigned int config7l_word = DEFAULT_CONFIG7L_WORD;
219 static unsigned int config7h_word = DEFAULT_CONFIG7H_WORD;
221 unsigned int stackPos = 0;
223 extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
225 void pic16_setMaxRAM(int size)
227 pic16->maxRAMaddress = size;
228 stackPos = pic16->RAMsize-1;
230 if (pic16->maxRAMaddress < 0) {
231 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
232 pic16->maxRAMaddress);
237 extern char *iComments2;
239 void pic16_dump_equates(FILE *of, set *equs)
243 r = setFirstItem(equs);
246 fprintf(of, "%s", iComments2);
247 fprintf(of, ";\tEquates to used internal registers\n");
248 fprintf(of, "%s", iComments2);
250 for(; r; r = setNextItem(equs)) {
251 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
256 int regCompare(const void *a, const void *b)
258 const regs *const *i = a;
259 const regs *const *j = b;
261 /* sort primarily by the address */
262 if( (*i)->address > (*j)->address)return 1;
263 if( (*i)->address < (*j)->address)return -1;
265 /* and secondarily by size */
266 if( (*i)->size > (*j)->size)return 1;
267 if( (*i)->size < (*j)->size)return -1;
269 /* register size sorting may have strange results use with care */
271 /* finally if in same address and same size sort by name */
272 return (strcmp( (*i)->name, (*j)->name));
277 void pic16_dump_section(FILE *of, set *section, int fix)
279 static int abs_section_no=0;
284 /* put all symbols in an array */
285 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
287 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
288 rlist[i] = rprev; i++;
292 if(rlist)free(rlist);
296 /* sort symbols according to their address */
297 qsort(rlist, i /*elementsInSet(section)*/, sizeof(regs *), regCompare);
300 fprintf(of, "\n\n\tudata\n");
301 for(r = setFirstItem(section); r; r = setNextItem(section)) {
302 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
309 init_addr = rlist[j]->address;
310 fprintf(of, "\n\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
314 if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
316 init_addr = r->address;
318 if(rprev && (init_addr > (rprev->address + rprev->size))) {
319 fprintf(of, "\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
323 if(r1 && (init_addr == r1->address)) {
324 fprintf(of, "%s\tres\t0\n\n", r->name);
326 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
335 void pic16_dump_int_registers(FILE *of, set *section)
341 /* put all symbols in an array */
342 rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
344 for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
345 rlist[i] = rprev; i++;
348 /* sort symbols according to their address */
349 qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
352 if(rlist)free(rlist);
356 fprintf(of, "\n\n; Internal registers\n");
358 fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
359 for(r = setFirstItem(section); r; r = setNextItem(section))
360 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
366 #ifdef WORDS_BIGENDIAN
367 #define _ENDIAN(x) (3-x)
369 #define _ENDIAN(x) (x)
372 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
374 /*-----------------------------------------------------------------*/
375 /* printIvalType - generates ival for int/char */
376 /*-----------------------------------------------------------------*/
377 void print_idataType (FILE *of, symbol *sym, sym_link * type, initList * ilist)
382 //fprintf(stderr, "%s\n",__FUNCTION__);
384 /* if initList is deep */
385 if (ilist->type == INIT_DEEP)
386 ilist = ilist->init.deep;
388 if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
389 werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
392 if (!(val = list2val (ilist))) {
393 // assuming a warning has been thrown
397 if (val->type != type) {
398 val = valCastLiteral(type, floatFromVal(val));
402 ulval = (unsigned long) floatFromVal (val);
406 switch (getSize (type)) {
408 fprintf(of, "%s\tdata\t0x%02x\n", sym->name, (unsigned char)BYTE_IN_LONG(ulval, 0));
409 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
413 // fprintf(of, "%s\tdw\t0x%04x\n", sym->name, (unsigned int)(BYTE_IN_LONG(ulval, 0)
414 // + (BYTE_IN_LONG(ulval, 1) << 8)));
415 fprintf(of, "%s\tdata\t0x%02x,0x%02x\n", sym->name, (unsigned char)BYTE_IN_LONG(ulval, 0),
416 (unsigned char)(BYTE_IN_LONG(ulval, 1) << 8));
417 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
418 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
422 fprintf(of, "%s\tdw\t0x%04x,0x%04x\n", sym->name, (unsigned int)(BYTE_IN_LONG(ulval, 0)
423 + (BYTE_IN_LONG(ulval, 1) << 8)),
424 (unsigned)(BYTE_IN_LONG(ulval, 2)
425 + (BYTE_IN_LONG(ulval, 3) << 8)));
426 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
427 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
428 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,2))));
429 // pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETLW,pic16_newpCodeOpLit(BYTE_IN_LONG(ulval,3))));
436 /*-----------------------------------------------------------------*/
437 /* printIval - generates code for initial value */
438 /*-----------------------------------------------------------------*/
439 void print_idata(FILE *of, symbol * sym, sym_link * type, initList * ilist)
444 /* if structure then */
445 if (IS_STRUCT (type))
447 //fprintf(stderr,"%s struct\n",__FUNCTION__);
448 //printIvalStruct (sym, type, ilist, oFile);
452 /* if this is a pointer */
455 //fprintf(stderr,"%s pointer\n",__FUNCTION__);
456 //printIvalPtr (sym, type, ilist, oFile);
460 /* if this is an array */
463 //fprintf(stderr,"%s array\n",__FUNCTION__);
464 // printIvalArray (sym, type, ilist, pb);
468 /* if type is SPECIFIER */
471 // fprintf(stderr,"%s spec\n",__FUNCTION__);
472 print_idataType(of, sym, type, ilist);
478 void pic16_dump_idata(FILE *of, set *idataSymSet)
484 i = elementsInSet(idataSymSet);
487 fprintf(of, "\n\n; Initialised data (idata)\n");
488 fprintf(of, "\tidata\n");
489 for(ids = setFirstItem(idataSymSet); ids; ids = setNextItem(idataSymSet))
490 print_idata(of, ids, ids->type, ids->ival);
495 /*-----------------------------------------------------------------*
496 * void pic16_list_valid_pics(int ncols, int list_alias)
498 * Print out a formatted list of valid PIC devices
500 * ncols - number of columns in the list.
502 * list_alias - if non-zero, print all of the supported aliases
503 * for a device (e.g. F84, 16F84, etc...)
504 *-----------------------------------------------------------------*/
505 void pic16_list_valid_pics(int ncols, int list_alias)
511 list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
513 /* decrement the column number if it's greater than zero */
514 ncols = (ncols > 1) ? ncols-1 : 4;
516 /* Find the device with the longest name */
517 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
518 for(j=0; j<=list_alias; j++) {
519 k = strlen(Pics16[i].name[j]);
527 for(i=0; i < num_of_supported_PICS; i++) {
531 fprintf(stderr,"%s", Pics16[i].name[j]);
533 l = longest + 2 - strlen(Pics16[i].name[j]);
544 } while(++j<list_alias);
552 /*-----------------------------------------------------------------*
554 *-----------------------------------------------------------------*/
555 PIC16_device *pic16_find_device(char *name)
563 for(i = 0; i<num_of_supported_PICS; i++) {
565 for(j=0; j<PROCESSOR_NAMES; j++)
566 if(!STRCASECMP(Pics16[i].name[j], name) )
574 /*-----------------------------------------------------------------*
576 *-----------------------------------------------------------------*/
577 void pic16_init_pic(char *pic_type)
579 pic16 = pic16_find_device(pic_type);
583 fprintf(stderr, "'%s' was not found.\n", pic_type);
585 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
587 fprintf(stderr,"Valid devices are:\n");
589 pic16_list_valid_pics(4,0);
593 // printf("PIC processor found and initialized: %s\n", pic_type);
594 pic16_setMaxRAM( 0xfff );
597 /*-----------------------------------------------------------------*
599 *-----------------------------------------------------------------*/
600 int pic16_picIsInitialized(void)
602 if(pic16 && pic16->maxRAMaddress > 0)
609 /*-----------------------------------------------------------------*
610 * char *pic16_processor_base_name(void) - Include file is derived from this.
611 *-----------------------------------------------------------------*/
612 char *pic16_processor_base_name(void)
618 return pic16->name[0];
621 #define DEBUG_CHECK 0
624 * return 1 if register wasn't found and added, 0 otherwise
626 int checkAddReg(set **set, regs *reg)
631 fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
634 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
635 if(!strcmp(tmp->name, reg->name))break;
641 fprintf(stderr, "added\n");
647 fprintf(stderr, "already added\n");
652 int checkAddSym(set **set, symbol *sym)
657 fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
660 for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
661 if(!strcmp(tmp->name, sym->name))break;
667 fprintf(stderr, "added\n");
673 fprintf(stderr, "already added\n");
680 /*-----------------------------------------------------------------*
681 * void pic16_groupRegistersInSection - add each register to its *
682 * corresponding section *
683 *-----------------------------------------------------------------*/
684 void pic16_groupRegistersInSection(set *regset)
688 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
690 // fprintf(stderr, "%s:%d group registers in section, reg: %s\n", __FILE__, __LINE__, reg->name);
693 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
695 // fprintf(stderr, "%s:%d register %s alias:%d fix:%d\n",
696 // __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed);
699 checkAddReg(&pic16_equ_data, reg);
702 checkAddReg(&pic16_fix_udata, reg);
705 if(reg->pc_type == PO_GPR_TEMP)
706 checkAddReg(&pic16_int_regs, reg);
708 checkAddReg(&pic16_rel_udata, reg);
718 /*-----------------------------------------------------------------*
719 * void pic16_assignConfigWordValue(int address, int value)
721 * All high performance RISC CPU PICs have seven config word starting
722 * at address 0x300000.
723 * This routine will assign a value to that address.
725 *-----------------------------------------------------------------*/
727 void pic16_assignConfigWordValue(int address, int value)
730 case CONFIG1H_WORD_ADDRESS:
731 config1h_word = value;
733 case CONFIG2L_WORD_ADDRESS:
734 config2l_word = value;
736 case CONFIG2H_WORD_ADDRESS:
737 config2h_word = value;
739 case CONFIG3H_WORD_ADDRESS:
740 config3h_word = value;
742 case CONFIG4L_WORD_ADDRESS:
743 config4l_word = value;
745 case CONFIG5L_WORD_ADDRESS:
746 config5l_word = value;
748 case CONFIG5H_WORD_ADDRESS:
749 config5h_word = value;
751 case CONFIG6L_WORD_ADDRESS:
752 config6l_word = value;
754 case CONFIG6H_WORD_ADDRESS:
755 config6h_word = value;
757 case CONFIG7L_WORD_ADDRESS:
758 config7l_word = value;
760 case CONFIG7H_WORD_ADDRESS:
761 config7h_word = value;
765 // fprintf(stderr,"setting config word to 0x%x\n",value);
768 /*-----------------------------------------------------------------*
769 * int pic16_getConfigWord(int address)
771 * Get the current value of the config word.
773 *-----------------------------------------------------------------*/
775 int pic16_getConfigWord(int address)
778 case CONFIG1H_WORD_ADDRESS:
779 return config1h_word;
780 case CONFIG2L_WORD_ADDRESS:
781 return config2l_word;
782 case CONFIG2H_WORD_ADDRESS:
783 return config2h_word;
784 case CONFIG3H_WORD_ADDRESS:
785 return config3h_word;
786 case CONFIG4L_WORD_ADDRESS:
787 return config4l_word;
788 case CONFIG5L_WORD_ADDRESS:
789 return config5l_word;
790 case CONFIG5H_WORD_ADDRESS:
791 return config5h_word;
792 case CONFIG6L_WORD_ADDRESS:
793 return config6l_word;
794 case CONFIG6H_WORD_ADDRESS:
795 return config6h_word;
796 case CONFIG7L_WORD_ADDRESS:
797 return config7l_word;
798 case CONFIG7H_WORD_ADDRESS:
799 return config7h_word;