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
42 #if defined(__BORLANDC__) || defined(_MSC_VER)
43 #define STRCASECMP stricmp
45 #define STRCASECMP strcasecmp
48 static PIC_device Pics[] = {
50 {"p18f242", "18f242", "pic18f242", "f242"}, // aliases
51 (memRange *)NULL, // ram memory map
52 (memRange *)NULL, // sfr memory map
60 {"p18f252", "18f252", "pic18f252", "f252"}, // aliases
61 (memRange *)NULL, // ram memory map
62 (memRange *)NULL, // sfr memory map
70 {"p18f442", "18f442", "pic18f442", "f442"}, // aliases
71 (memRange *)NULL, // ram memory map
72 (memRange *)NULL, // sfr memory map
80 {"p18f452", "18f452", "pic18f452", "f452"}, // aliases
81 (memRange *)NULL, // ram memory map
82 (memRange *)NULL, // sfr memory map
90 {"p18f248", "18f248", "pic18f248", "f248"}, // aliases
91 (memRange *)NULL, // ram memory map
92 (memRange *)NULL, // sfr memory map
100 {"p18f258", "18f258", "pic18f258", "f258"}, // aliases
101 (memRange *)NULL, // ram memory map
102 (memRange *)NULL, // sfr memory map
110 {"p18f448", "18f448", "pic18f448", "f448"}, // aliases
111 (memRange *)NULL, // ram memory map
112 (memRange *)NULL, // sfr memory map
120 {"p18f458", "18f458", "pic18f458", "f458"}, // aliases
121 (memRange *)NULL, // ram memory map
122 (memRange *)NULL, // sfr memory map
130 {"p18f6520", "18f6520", "pic18f6520", "f6520"}, // aliases
131 (memRange *)NULL, // ram memory map
132 (memRange *)NULL, // sfr memory map
140 {"p18f6620", "18f6620", "pic18f6620", "f6620"}, // aliases
141 (memRange *)NULL, // ram memory map
142 (memRange *)NULL, // sfr memory map
149 {"p18f6680", "18f6680", "pic18f6680", "f6680"}, // aliases
150 (memRange *)NULL, // ram memory map
151 (memRange *)NULL, // sfr memory map
158 {"p18f6720", "18f6720", "pic18f6720", "f6720"}, // aliases
159 (memRange *)NULL, // ram memory map
160 (memRange *)NULL, // sfr memory map
167 {"p18f8520", "18f8520", "pic18f8520", "f8520"}, // aliases
168 (memRange *)NULL, // ram memory map
169 (memRange *)NULL, // sfr memory map
176 {"p18f8620", "18f8620", "pic18f8620", "f8620"}, // aliases
177 (memRange *)NULL, // ram memory map
178 (memRange *)NULL, // sfr memory map
185 {"p18f8680", "18f8680", "pic18f8680", "f8680"}, // aliases
186 (memRange *)NULL, // ram memory map
187 (memRange *)NULL, // sfr memory map
194 {"p18f8720", "18f8720", "pic18f8720", "f8720"}, // aliases
195 (memRange *)NULL, // ram memory map
196 (memRange *)NULL, // sfr memory map
205 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
207 #define DEFAULT_PIC "452"
209 static PIC_device *pic=NULL;
211 //AssignedMemory *pic16_finalMapping=NULL;
212 //int pic16_finalMappingSize=0;
214 #define DEFAULT_CONFIG_BYTE 0xff
216 #define CONFIG1H_WORD_ADDRESS 0x300001
217 #define DEFAULT_CONFIG1H_WORD DEFAULT_CONFIG_BYTE
219 #define CONFIG2L_WORD_ADDRESS 0x300002
220 #define DEFAULT_CONFIG2L_WORD DEFAULT_CONFIG_BYTE
222 #define CONFIG2H_WORD_ADDRESS 0x300003
223 #define DEFAULT_CONFIG2H_WORD DEFAULT_CONFIG_BYTE
225 #define CONFIG3H_WORD_ADDRESS 0x300005
226 #define DEFAULT_CONFIG3H_WORD DEFAULT_CONFIG_BYTE
228 #define CONFIG4L_WORD_ADDRESS 0x300006
229 #define DEFAULT_CONFIG4L_WORD DEFAULT_CONFIG_BYTE
231 #define CONFIG5L_WORD_ADDRESS 0x300008
232 #define DEFAULT_CONFIG5L_WORD DEFAULT_CONFIG_BYTE
234 #define CONFIG5H_WORD_ADDRESS 0x300009
235 #define DEFAULT_CONFIG5H_WORD DEFAULT_CONFIG_BYTE
237 #define CONFIG6L_WORD_ADDRESS 0x30000a
238 #define DEFAULT_CONFIG6L_WORD DEFAULT_CONFIG_BYTE
240 #define CONFIG6H_WORD_ADDRESS 0x30000b
241 #define DEFAULT_CONFIG6H_WORD DEFAULT_CONFIG_BYTE
243 #define CONFIG7L_WORD_ADDRESS 0x30000c
244 #define DEFAULT_CONFIG7L_WORD DEFAULT_CONFIG_BYTE
246 #define CONFIG7H_WORD_ADDRESS 0x30000d
247 #define DEFAULT_CONFIG7H_WORD DEFAULT_CONFIG_BYTE
249 static unsigned int config1h_word = DEFAULT_CONFIG1H_WORD;
250 static unsigned int config2l_word = DEFAULT_CONFIG2L_WORD;
251 static unsigned int config2h_word = DEFAULT_CONFIG2H_WORD;
252 static unsigned int config3h_word = DEFAULT_CONFIG3H_WORD;
253 static unsigned int config4l_word = DEFAULT_CONFIG4L_WORD;
254 static unsigned int config5l_word = DEFAULT_CONFIG5L_WORD;
255 static unsigned int config5h_word = DEFAULT_CONFIG5H_WORD;
256 static unsigned int config6l_word = DEFAULT_CONFIG6L_WORD;
257 static unsigned int config6h_word = DEFAULT_CONFIG6H_WORD;
258 static unsigned int config7l_word = DEFAULT_CONFIG7L_WORD;
259 static unsigned int config7h_word = DEFAULT_CONFIG7H_WORD;
261 unsigned int stackPos = 0;
263 void pic16_setMaxRAM(int size)
265 pic->maxRAMaddress = size;
266 stackPos = pic->RAMsize-1;
268 if (pic->maxRAMaddress < 0) {
269 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
274 // pic16_finalMapping = Safe_calloc(1+pic->maxRAMaddress,
275 // sizeof(AssignedMemory));
277 /* Now initialize the pic16_finalMapping array */
279 // for(i=0; i<=pic->maxRAMaddress; i++) {
280 // pic16_finalMapping[i].reg = NULL;
281 // pic16_finalMapping[i].isValid = 0;
285 /*-----------------------------------------------------------------*
286 *-----------------------------------------------------------------*/
288 int pic16_isREGinBank(regs *reg, int bank)
294 if(pic16_finalMapping[reg->address].bank == bank)
300 /*-----------------------------------------------------------------*
301 *-----------------------------------------------------------------*/
302 int pic16_REGallBanks(regs *reg)
308 if ((int)reg->address > pic->maxRAMaddress)
315 /*-----------------------------------------------------------------*
316 *-----------------------------------------------------------------*/
319 * pic16_dump_map -- debug stuff
322 void pic16_dump_map(void)
326 for(i=0; i<=pic->maxRAMaddress; i++) {
327 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((pic16_finalMapping[i].isValid) ? "valid":"invalid"));
329 if(pic16_finalMapping[i].isValid) {
330 fprintf(stderr,"addr: 0x%02x",i);
331 if(pic16_finalMapping[i].isSFR)
332 fprintf(stderr," isSFR");
333 if(pic16_finalMapping[i].reg)
334 fprintf( stderr, " reg %s", pic16_finalMapping[i].reg->name);
335 fprintf(stderr, "\n");
342 extern char *iComments2;
344 void pic16_dump_equates(FILE *of, set *equs)
348 r = setFirstItem(equs);
351 fprintf(of, "%s", iComments2);
352 fprintf(of, ";\tEquates to used internal registers\n");
353 fprintf(of, "%s", iComments2);
355 for(; r; r = setNextItem(equs)) {
356 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
361 void pic16_dump_section(FILE *of, set *section, int fix)
363 static int abs_section_no=0;
368 fprintf(of, "\n\n\tudata\n");
369 for(r = setFirstItem(section); r; r = setNextItem(section)) {
370 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
373 r = setFirstItem(section);
375 init_addr = r->address;
376 fprintf(of, "\n\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
379 for(; r; r = setNextItem(section)) {
380 init_addr = r->address;
382 if(rprev && (init_addr != (rprev->address + rprev->size))) {
383 fprintf(of, "\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
386 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
395 /*-----------------------------------------------------------------*
396 * void pic16_list_valid_pics(int ncols, int list_alias)
398 * Print out a formatted list of valid PIC devices
400 * ncols - number of columns in the list.
402 * list_alias - if non-zero, print all of the supported aliases
403 * for a device (e.g. F84, 16F84, etc...)
404 *-----------------------------------------------------------------*/
405 void pic16_list_valid_pics(int ncols, int list_alias)
411 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
413 /* decrement the column number if it's greater than zero */
414 ncols = (ncols > 1) ? ncols-1 : 4;
416 /* Find the device with the longest name */
417 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
418 for(j=0; j<=list_alias; j++) {
419 k = strlen(Pics[i].name[j]);
427 for(i=0; i < num_of_supported_PICS; i++) {
431 fprintf(stderr,"%s", Pics[i].name[j]);
433 l = longest + 2 - strlen(Pics[i].name[j]);
444 } while(++j<list_alias);
452 /*-----------------------------------------------------------------*
454 *-----------------------------------------------------------------*/
455 PIC_device *pic16_find_device(char *name)
463 for(i = 0; i<num_of_supported_PICS; i++) {
465 for(j=0; j<PROCESSOR_NAMES; j++)
466 if(!STRCASECMP(Pics[i].name[j], name) )
474 /*-----------------------------------------------------------------*
476 *-----------------------------------------------------------------*/
477 void pic16_init_pic(char *pic_type)
479 pic = pic16_find_device(pic_type);
483 fprintf(stderr, "'%s' was not found.\n", pic_type);
485 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
487 fprintf(stderr,"Valid devices are:\n");
489 pic16_list_valid_pics(4,0);
493 // printf("PIC processor found and initialized: %s\n", pic_type);
494 pic16_setMaxRAM( 0xfff );
497 /*-----------------------------------------------------------------*
499 *-----------------------------------------------------------------*/
500 int pic16_picIsInitialized(void)
502 if(pic && pic->maxRAMaddress > 0)
509 /*-----------------------------------------------------------------*
510 * char *pic16_processor_base_name(void) - Include file is derived from this.
511 *-----------------------------------------------------------------*/
512 char *pic16_processor_base_name(void)
523 /*-----------------------------------------------------------------*
524 *-----------------------------------------------------------------*/
525 static int validAddress(int address, int reg_size)
530 if (pic->maxRAMaddress < 0) {
531 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
534 // fprintf(stderr, "validAddress: Checking 0x%04x (max=0x%04x) (reg_size = %d)\n",address, pic->maxRAMaddress, reg_size);
535 if(address > pic->maxRAMaddress)
538 for (i=0; i<reg_size; i++)
539 if(!pic16_finalMapping[address + i].isValid ||
540 pic16_finalMapping[address+i].reg ||
541 pic16_finalMapping[address+i].isSFR )
549 void checkAddReg(set **set, regs *reg)
554 for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
555 if(!strcmp(tmp->name, reg->name))break;
562 /*-----------------------------------------------------------------*
563 * void pic16_groupRegistersInSection - add each register to its *
564 * corresponding section *
565 *-----------------------------------------------------------------*/
566 void pic16_groupRegistersInSection(set *regset)
570 for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
572 && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
575 checkAddReg(&pic16_equ_data, reg);
578 checkAddReg(&pic16_fix_udata, reg);
581 // fprintf(stderr, "%s:%d adding symbol %s in relocatable udata section\n",
582 // __FILE__, __LINE__, reg->name);
583 checkAddReg(&pic16_rel_udata, reg);
593 /*-----------------------------------------------------------------*
594 * void pic16_assignConfigWordValue(int address, int value)
596 * All high performance RISC CPU PICs have seven config word starting
597 * at address 0x300000.
598 * This routine will assign a value to that address.
600 *-----------------------------------------------------------------*/
602 void pic16_assignConfigWordValue(int address, int value)
605 case CONFIG1H_WORD_ADDRESS:
606 config1h_word = value;
608 case CONFIG2L_WORD_ADDRESS:
609 config2l_word = value;
611 case CONFIG2H_WORD_ADDRESS:
612 config2h_word = value;
614 case CONFIG3H_WORD_ADDRESS:
615 config3h_word = value;
617 case CONFIG4L_WORD_ADDRESS:
618 config4l_word = value;
620 case CONFIG5L_WORD_ADDRESS:
621 config5l_word = value;
623 case CONFIG5H_WORD_ADDRESS:
624 config5h_word = value;
626 case CONFIG6L_WORD_ADDRESS:
627 config6l_word = value;
629 case CONFIG6H_WORD_ADDRESS:
630 config6h_word = value;
632 case CONFIG7L_WORD_ADDRESS:
633 config7l_word = value;
635 case CONFIG7H_WORD_ADDRESS:
636 config7h_word = value;
640 fprintf(stderr,"setting config word to 0x%x\n",value);
643 /*-----------------------------------------------------------------*
644 * int pic16_getConfigWord(int address)
646 * Get the current value of the config word.
648 *-----------------------------------------------------------------*/
650 int pic16_getConfigWord(int address)
653 case CONFIG1H_WORD_ADDRESS:
654 return config1h_word;
655 case CONFIG2L_WORD_ADDRESS:
656 return config2l_word;
657 case CONFIG2H_WORD_ADDRESS:
658 return config2h_word;
659 case CONFIG3H_WORD_ADDRESS:
660 return config3h_word;
661 case CONFIG4L_WORD_ADDRESS:
662 return config4l_word;
663 case CONFIG5L_WORD_ADDRESS:
664 return config5l_word;
665 case CONFIG5H_WORD_ADDRESS:
666 return config5h_word;
667 case CONFIG6L_WORD_ADDRESS:
668 return config6l_word;
669 case CONFIG6H_WORD_ADDRESS:
670 return config6h_word;
671 case CONFIG7L_WORD_ADDRESS:
672 return config7l_word;
673 case CONFIG7H_WORD_ADDRESS:
674 return config7h_word;