1 /*-------------------------------------------------------------------------
3 device.c - Accomodates subtle variations in PIC16 devices
4 Written By - Scott Dattalo scott@dattalo.com
5 Ported to PIC16 By - Martin Dubuc m.dubuc@rogers.com
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 -------------------------------------------------------------------------*/
24 #include "common.h" // Include everything in the SDCC src directory
32 #if defined(__BORLANDC__) || defined(_MSC_VER)
33 #define STRCASECMP stricmp
35 #define STRCASECMP strcasecmp
38 static PIC_device Pics[] = {
40 {"p18f242", "18f242", "pic18f242", "f242"},
48 {"p18f252", "18f252", "pic18f252", "f252"},
56 {"p18f442", "18f442", "pic18f442", "f442"},
64 {"p18f452", "18f452", "pic18f452", "f452"},
73 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
75 #define DEFAULT_PIC "f452"
77 static PIC_device *pic=NULL;
79 AssignedMemory *pic16_finalMapping=NULL;
81 #define DEFAULT_CONFIG_BYTE 0xff
83 #define CONFIG1H_WORD_ADDRESS 0x300001
84 #define DEFAULT_CONFIG1H_WORD DEFAULT_CONFIG_BYTE
86 #define CONFIG2L_WORD_ADDRESS 0x300002
87 #define DEFAULT_CONFIG2L_WORD DEFAULT_CONFIG_BYTE
89 #define CONFIG2H_WORD_ADDRESS 0x300003
90 #define DEFAULT_CONFIG2H_WORD DEFAULT_CONFIG_BYTE
92 #define CONFIG3H_WORD_ADDRESS 0x300005
93 #define DEFAULT_CONFIG3H_WORD DEFAULT_CONFIG_BYTE
95 #define CONFIG4L_WORD_ADDRESS 0x300006
96 #define DEFAULT_CONFIG4L_WORD DEFAULT_CONFIG_BYTE
98 #define CONFIG5L_WORD_ADDRESS 0x300008
99 #define DEFAULT_CONFIG5L_WORD DEFAULT_CONFIG_BYTE
101 #define CONFIG5H_WORD_ADDRESS 0x300009
102 #define DEFAULT_CONFIG5H_WORD DEFAULT_CONFIG_BYTE
104 #define CONFIG6L_WORD_ADDRESS 0x30000a
105 #define DEFAULT_CONFIG6L_WORD DEFAULT_CONFIG_BYTE
107 #define CONFIG6H_WORD_ADDRESS 0x30000b
108 #define DEFAULT_CONFIG6H_WORD DEFAULT_CONFIG_BYTE
110 #define CONFIG7L_WORD_ADDRESS 0x30000c
111 #define DEFAULT_CONFIG7L_WORD DEFAULT_CONFIG_BYTE
113 #define CONFIG7H_WORD_ADDRESS 0x30000d
114 #define DEFAULT_CONFIG7H_WORD DEFAULT_CONFIG_BYTE
116 static unsigned int config1h_word = DEFAULT_CONFIG1H_WORD;
117 static unsigned int config2l_word = DEFAULT_CONFIG2L_WORD;
118 static unsigned int config2h_word = DEFAULT_CONFIG2H_WORD;
119 static unsigned int config3h_word = DEFAULT_CONFIG3H_WORD;
120 static unsigned int config4l_word = DEFAULT_CONFIG4L_WORD;
121 static unsigned int config5l_word = DEFAULT_CONFIG5L_WORD;
122 static unsigned int config5h_word = DEFAULT_CONFIG5H_WORD;
123 static unsigned int config6l_word = DEFAULT_CONFIG6L_WORD;
124 static unsigned int config6h_word = DEFAULT_CONFIG6H_WORD;
125 static unsigned int config7l_word = DEFAULT_CONFIG7L_WORD;
126 static unsigned int config7h_word = DEFAULT_CONFIG7H_WORD;
128 void pic16_addMemRange(memRange *r, int type)
131 int alias = r->alias;
133 if (pic->maxRAMaddress < 0) {
134 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
139 for (i=r->start_address; i<= r->end_address; i++) {
140 if ((i|alias) <= pic->maxRAMaddress) {
141 pic16_finalMapping[i | alias].isValid = 1;
142 pic16_finalMapping[i | alias].alias = r->alias;
143 pic16_finalMapping[i | alias].bank = r->bank;
146 pic16_finalMapping[i | alias].isSFR = 1;
148 pic16_finalMapping[i | alias].isSFR = 0;
151 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
152 __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
156 /* Decrement alias */
158 alias -= ((alias & (alias - 1)) ^ alias);
163 } while (alias >= 0);
166 void pic16_setMaxRAM(int size)
169 pic->maxRAMaddress = size;
171 if (pic->maxRAMaddress < 0) {
172 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
177 pic16_finalMapping = Safe_calloc(1+pic->maxRAMaddress,
178 sizeof(AssignedMemory));
180 /* Now initialize the pic16_finalMapping array */
182 for(i=0; i<=pic->maxRAMaddress; i++) {
183 pic16_finalMapping[i].reg = NULL;
184 pic16_finalMapping[i].isValid = 0;
188 /*-----------------------------------------------------------------*
189 *-----------------------------------------------------------------*/
191 int pic16_isREGinBank(regs *reg, int bank)
197 if(pic16_finalMapping[reg->address].bank == bank)
203 /*-----------------------------------------------------------------*
204 *-----------------------------------------------------------------*/
205 int pic16_REGallBanks(regs *reg)
211 if (reg->address > pic->maxRAMaddress)
218 /*-----------------------------------------------------------------*
219 *-----------------------------------------------------------------*/
222 * pic16_dump_map -- debug stuff
225 void pic16_dump_map(void)
229 for(i=0; i<=pic->maxRAMaddress; i++) {
230 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((pic16_finalMapping[i].isValid) ? "valid":"invalid"));
232 if(pic16_finalMapping[i].isValid) {
233 fprintf(stderr,"addr: 0x%02x",i);
234 if(pic16_finalMapping[i].isSFR)
235 fprintf(stderr," isSFR");
236 if(pic16_finalMapping[i].reg)
237 fprintf( stderr, " reg %s", pic16_finalMapping[i].reg->name);
238 fprintf(stderr, "\n");
244 void pic16_dump_cblock(FILE *of)
250 //pic16_dump_map(); /* display the register map */
252 if (pic->maxRAMaddress < 0) {
253 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
259 if(pic16_finalMapping[addr].reg && !pic16_finalMapping[addr].reg->isEmitted) {
266 /* clear the lower 7-bits of the start address of the first
267 * variable declared in this bank. The upper bits for the mid
268 * range pics are the bank select bits.
271 bank_base = start & 0xfff8;
273 /* The bank number printed in the cblock comment tacitly
274 * assumes that the first register in the contiguous group
275 * of registers represents the bank for the whole group */
277 if ((pic16_finalMapping[start].bank == 0 && start <= 0x7f) ||
278 pic16_finalMapping[start].isSFR)
279 fprintf(of," cblock 0X%04X\t; Access Bank\n",start);
281 fprintf(of," cblock 0X%04X\t; Bank %d\n",start,pic16_finalMapping[start].bank);
283 for( ; start < addr; start++) {
284 if((pic16_finalMapping[start].reg) && !pic16_finalMapping[start].reg->isEmitted ) {
285 fprintf(of,"\t%s",pic16_finalMapping[start].reg->name);
287 /* If this register is aliased in multiple banks, then
288 * mangle the variable name with the alias address: */
289 if(pic16_finalMapping[start].alias & start)
290 fprintf(of,"_%x",bank_base);
292 if(pic16_finalMapping[start].instance)
293 fprintf(of,"_%d",pic16_finalMapping[start].instance);
298 //pic16_finalMapping[start].reg->isEmitted = 1;
302 fprintf(of," endc\n");
311 } while(addr <= pic->maxRAMaddress);
316 /*-----------------------------------------------------------------*
317 * void pic16_list_valid_pics(int ncols, int list_alias)
319 * Print out a formatted list of valid PIC devices
321 * ncols - number of columns in the list.
323 * list_alias - if non-zero, print all of the supported aliases
324 * for a device (e.g. F84, 16F84, etc...)
325 *-----------------------------------------------------------------*/
326 void pic16_list_valid_pics(int ncols, int list_alias)
332 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
334 /* decrement the column number if it's greater than zero */
335 ncols = (ncols > 1) ? ncols-1 : 4;
337 /* Find the device with the longest name */
338 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
339 for(j=0; j<=list_alias; j++) {
340 k = strlen(Pics[i].name[j]);
348 for(i=0; i < num_of_supported_PICS; i++) {
352 fprintf(stderr,"%s", Pics[i].name[j]);
354 l = longest + 2 - strlen(Pics[i].name[j]);
365 } while(++j<list_alias);
373 /*-----------------------------------------------------------------*
375 *-----------------------------------------------------------------*/
376 PIC_device *pic16_find_device(char *name)
384 for(i = 0; i<num_of_supported_PICS; i++) {
386 for(j=0; j<PROCESSOR_NAMES; j++)
387 if(!STRCASECMP(Pics[i].name[j], name) )
395 /*-----------------------------------------------------------------*
397 *-----------------------------------------------------------------*/
398 void pic16_init_pic(char *pic_type)
400 pic = pic16_find_device(pic_type);
404 fprintf(stderr, "'%s' was not found.\n", pic_type);
406 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
408 fprintf(stderr,"Valid devices are:\n");
410 pic16_list_valid_pics(4,0);
414 pic->maxRAMaddress = -1;
417 /*-----------------------------------------------------------------*
419 *-----------------------------------------------------------------*/
420 int pic16_picIsInitialized(void)
422 if(pic && pic->maxRAMaddress > 0)
429 /*-----------------------------------------------------------------*
430 * char *pic16_processor_base_name(void) - Include file is derived from this.
431 *-----------------------------------------------------------------*/
432 char *pic16_processor_base_name(void)
441 static int isSFR(int address)
444 if( (address > pic->maxRAMaddress) || !pic16_finalMapping[address].isSFR)
451 /*-----------------------------------------------------------------*
452 *-----------------------------------------------------------------*/
453 static int validAddress(int address, int reg_size)
457 if (pic->maxRAMaddress < 0) {
458 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
461 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
462 if(address > pic->maxRAMaddress)
465 for (i=0; i<reg_size; i++)
466 if(!pic16_finalMapping[address + i].isValid ||
467 pic16_finalMapping[address+i].reg ||
468 pic16_finalMapping[address+i].isSFR )
474 /*-----------------------------------------------------------------*
475 *-----------------------------------------------------------------*/
476 static void mapRegister(regs *reg)
482 if(!reg || !reg->size) {
483 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
487 if (pic->maxRAMaddress < 0) {
488 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
492 for(i=0; i<reg->size; i++) {
494 alias = pic16_finalMapping[reg->address].alias;
499 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
501 pic16_finalMapping[reg->address + alias + i].reg = reg;
502 pic16_finalMapping[reg->address + alias + i].instance = i;
504 /* Decrement alias */
506 alias -= ((alias & (alias - 1)) ^ alias);
513 // fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
519 /*-----------------------------------------------------------------*
520 *-----------------------------------------------------------------*/
521 static int assignRegister(regs *reg, int start_address)
525 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
528 if (validAddress(reg->address,reg->size)) {
529 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
534 if( isSFR(reg->address)) {
539 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
540 // reg->address, reg->name);
544 /* This register does not have a fixed address requirement
545 * so we'll search through all availble ram address and
546 * assign the first one */
548 for (i=start_address; i<=pic->maxRAMaddress; i++) {
550 if (validAddress(i,reg->size)) {
557 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
564 /*-----------------------------------------------------------------*
565 *-----------------------------------------------------------------*/
566 void pic16_assignFixedRegisters(set *regset)
570 for (reg = setFirstItem(regset) ; reg ;
571 reg = setNextItem(regset)) {
574 assignRegister(reg,0);
579 /*-----------------------------------------------------------------*
580 *-----------------------------------------------------------------*/
581 void pic16_assignRelocatableRegisters(set *regset, int used)
587 for (reg = setFirstItem(regset) ; reg ;
588 reg = setNextItem(regset)) {
590 //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
592 if((!reg->isFixed) && ( used || reg->wasUsed))
593 address = assignRegister(reg,address);
600 /*-----------------------------------------------------------------*
601 * void pic16_assignConfigWordValue(int address, int value)
603 * All high performance RISC CPU PICs have seven config word starting
604 * at address 0x300000.
605 * This routine will assign a value to that address.
607 *-----------------------------------------------------------------*/
609 void pic16_assignConfigWordValue(int address, int value)
612 case CONFIG1H_WORD_ADDRESS:
613 config1h_word = value;
615 case CONFIG2L_WORD_ADDRESS:
616 config2l_word = value;
618 case CONFIG2H_WORD_ADDRESS:
619 config2h_word = value;
621 case CONFIG3H_WORD_ADDRESS:
622 config3h_word = value;
624 case CONFIG4L_WORD_ADDRESS:
625 config4l_word = value;
627 case CONFIG5L_WORD_ADDRESS:
628 config5l_word = value;
630 case CONFIG5H_WORD_ADDRESS:
631 config5h_word = value;
633 case CONFIG6L_WORD_ADDRESS:
634 config6l_word = value;
636 case CONFIG6H_WORD_ADDRESS:
637 config6h_word = value;
639 case CONFIG7L_WORD_ADDRESS:
640 config7l_word = value;
642 case CONFIG7H_WORD_ADDRESS:
643 config7h_word = value;
647 //fprintf(stderr,"setting config word to 0x%x\n",value);
650 /*-----------------------------------------------------------------*
651 * int pic16_getConfigWord(int address)
653 * Get the current value of the config word.
655 *-----------------------------------------------------------------*/
657 int pic16_getConfigWord(int address)
660 case CONFIG1H_WORD_ADDRESS:
661 return config1h_word;
662 case CONFIG2L_WORD_ADDRESS:
663 return config2l_word;
664 case CONFIG2H_WORD_ADDRESS:
665 return config2h_word;
666 case CONFIG3H_WORD_ADDRESS:
667 return config3h_word;
668 case CONFIG4L_WORD_ADDRESS:
669 return config4l_word;
670 case CONFIG5L_WORD_ADDRESS:
671 return config5l_word;
672 case CONFIG5H_WORD_ADDRESS:
673 return config5h_word;
674 case CONFIG6L_WORD_ADDRESS:
675 return config6l_word;
676 case CONFIG6H_WORD_ADDRESS:
677 return config6h_word;
678 case CONFIG7L_WORD_ADDRESS:
679 return config7l_word;
680 case CONFIG7H_WORD_ADDRESS:
681 return config7h_word;