1 /*-------------------------------------------------------------------------
3 device.c - Accomodates subtle variations in PIC devices
4 Written By - Scott Dattalo scott@dattalo.com
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 -------------------------------------------------------------------------*/
23 #include "common.h" // Include everything in the SDCC src directory
31 #if defined(__BORLANDC__) || defined(_MSC_VER)
32 #define STRCASECMP stricmp
34 #define STRCASECMP strcasecmp
37 static PIC_device Pics[] = {
39 {"p16f627", "16f627", "pic16f627", "f627"}, /* processor name */
42 0, /* max ram address (calculated) */
43 0x1ff, /* default max ram address */
48 {"p16f628", "16f628", "pic16f628", "f628"},
57 {"p16f84", "16f84", "pic16f84", "f84"},
61 0x4f, /* 68 register available 0x0C to 0x4F (0x8C to 0xCF mapped to bank 0) */
66 {"p16f873", "16f873", "pic16f873", "f873"},
75 {"p16f877", "16f877", "pic16f877", "f877"},
84 {"p16f819", "16f819", "pic16f819", "f819"},
94 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
96 static PIC_device *pic=NULL;
98 AssignedMemory *finalMapping=NULL;
100 #define CONFIG_WORD_ADDRESS 0x2007
101 #define DEFAULT_CONFIG_WORD 0x3fff
103 static unsigned int config_word = DEFAULT_CONFIG_WORD;
105 extern void emitSymbolToFile (FILE *of, const char *name, int size);
107 void addMemRange(memRange *r, int type)
110 int alias = r->alias;
112 if (pic->maxRAMaddress < 0) {
113 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
118 for (i=r->start_address; i<= r->end_address; i++) {
119 if ((i|alias) <= pic->maxRAMaddress) {
120 finalMapping[i | alias].isValid = 1;
121 finalMapping[i | alias].alias = r->alias;
122 finalMapping[i | alias].bank = r->bank;
125 finalMapping[i | alias].isSFR = 1;
127 finalMapping[i | alias].isSFR = 0;
130 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
131 __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
135 /* Decrement alias */
137 alias -= ((alias & (alias - 1)) ^ alias);
142 } while (alias >= 0);
145 void setMaxRAM(int size)
148 pic->maxRAMaddress = size;
150 if (pic->maxRAMaddress < 0) {
151 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
156 finalMapping = Safe_calloc(1+pic->maxRAMaddress,
157 sizeof(AssignedMemory));
159 /* Now initialize the finalMapping array */
161 for(i=0; i<=pic->maxRAMaddress; i++) {
162 finalMapping[i].reg = NULL;
163 finalMapping[i].isValid = 0;
164 finalMapping[i].bank = (i>>7);
168 /*-----------------------------------------------------------------*
169 *-----------------------------------------------------------------*/
171 int isREGinBank(regs *reg, int bank)
177 if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
183 /*-----------------------------------------------------------------*
184 *-----------------------------------------------------------------*/
185 int REGallBanks(regs *reg)
191 return ((reg->address | reg->alias) & pic->bankMask);
195 /*-----------------------------------------------------------------*
196 *-----------------------------------------------------------------*/
199 * dump_map -- debug stuff
206 for(i=0; i<=pic->maxRAMaddress; i++) {
207 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
209 if(finalMapping[i].isValid) {
210 fprintf(stderr,"addr: 0x%02x",i);
211 if(finalMapping[i].isSFR)
212 fprintf(stderr," isSFR");
213 if(finalMapping[i].reg)
214 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
215 fprintf(stderr, "\n");
221 void dump_sfr(FILE *of)
227 static int udata_flag=0;
229 //dump_map(); /* display the register map */
230 //fprintf(stdout,";dump_sfr \n");
231 if (pic->maxRAMaddress < 0) {
232 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
238 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
245 /* clear the lower 7-bits of the start address of the first
246 * variable declared in this bank. The upper bits for the mid
247 * range pics are the bank select bits.
250 bank_base = start & 0xfff8;
252 /* The bank number printed in the cblock comment tacitly
253 * assumes that the first register in the contiguous group
254 * of registers represents the bank for the whole group */
256 if ( (start != addr) && (!udata_flag) ) {
258 //fprintf(of,"\tudata\n");
261 for( ; start < addr; start++) {
262 if((finalMapping[start].reg) &&
263 (!finalMapping[start].reg->isEmitted) &&
264 (!finalMapping[start].instance) &&
265 (!finalMapping[start].isSFR)) {
267 if (finalMapping[start].reg->isFixed) {
269 for (i=0; i<finalMapping[start].reg->size; i++) {
270 fprintf(of,"%s\tEQU\t0x%04x\n",
271 finalMapping[start].reg->name,
272 finalMapping[start].reg->address+i);
275 emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
277 fprintf(of,"%s\tres\t%i\n",
278 finalMapping[start].reg->name,
279 finalMapping[start].reg->size);
282 finalMapping[start].reg->isEmitted = 1;
293 } while(addr <= pic->maxRAMaddress);
298 /*-----------------------------------------------------------------*
299 * void list_valid_pics(int ncols, int list_alias)
301 * Print out a formatted list of valid PIC devices
303 * ncols - number of columns in the list.
305 * list_alias - if non-zero, print all of the supported aliases
306 * for a device (e.g. F84, 16F84, etc...)
307 *-----------------------------------------------------------------*/
308 void list_valid_pics(int ncols, int list_alias)
314 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
316 /* decrement the column number if it's greater than zero */
317 ncols = (ncols > 1) ? ncols-1 : 4;
319 /* Find the device with the longest name */
320 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
321 for(j=0; j<=list_alias; j++) {
322 k = strlen(Pics[i].name[j]);
330 for(i=0; i < num_of_supported_PICS; i++) {
334 fprintf(stderr,"%s", Pics[i].name[j]);
336 l = longest + 2 - strlen(Pics[i].name[j]);
347 } while(++j<list_alias);
355 /*-----------------------------------------------------------------*
357 *-----------------------------------------------------------------*/
358 PIC_device *find_device(char *name)
366 for(i = 0; i<num_of_supported_PICS; i++) {
368 for(j=0; j<PROCESSOR_NAMES; j++)
369 if(!STRCASECMP(Pics[i].name[j], name) )
377 /*-----------------------------------------------------------------*
379 *-----------------------------------------------------------------*/
380 void init_pic(char *pic_type)
382 pic = find_device(pic_type);
386 fprintf(stderr, "'%s' was not found.\n", pic_type);
388 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
390 fprintf(stderr,"Valid devices are:\n");
392 list_valid_pics(4,0);
396 pic->maxRAMaddress = -1;
399 /*-----------------------------------------------------------------*
401 *-----------------------------------------------------------------*/
402 int picIsInitialized(void)
404 if(pic && pic->maxRAMaddress > 0)
411 /*-----------------------------------------------------------------*
412 * char *processor_base_name(void) - Include file is derived from this.
413 *-----------------------------------------------------------------*/
414 char *processor_base_name(void)
423 int isSFR(int address)
426 if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
433 /*-----------------------------------------------------------------*
434 *-----------------------------------------------------------------*/
435 int validAddress(int address, int reg_size)
439 if (pic->maxRAMaddress < 0) {
440 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
443 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
444 if(address > pic->maxRAMaddress)
447 for (i=0; i<reg_size; i++)
448 if(!finalMapping[address + i].isValid ||
449 finalMapping[address+i].reg ||
450 finalMapping[address+i].isSFR )
456 /*-----------------------------------------------------------------*
457 *-----------------------------------------------------------------*/
458 void mapRegister(regs *reg)
464 if(!reg || !reg->size) {
465 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
469 if (pic->maxRAMaddress < 0) {
470 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
474 for(i=0; i<reg->size; i++) {
476 alias = finalMapping[reg->address].alias;
481 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
483 finalMapping[reg->address + alias + i].reg = reg;
484 finalMapping[reg->address + alias + i].instance = i;
486 /* Decrement alias */
488 alias -= ((alias & (alias - 1)) ^ alias);
495 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
501 /*-----------------------------------------------------------------*
502 *-----------------------------------------------------------------*/
503 int assignRegister(regs *reg, int start_address)
507 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
510 if (validAddress(reg->address,reg->size)) {
511 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
516 if( isSFR(reg->address)) {
521 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
522 // reg->address, reg->name);
526 /* This register does not have a fixed address requirement
527 * so we'll search through all availble ram address and
528 * assign the first one */
530 for (i=start_address; i<=pic->maxRAMaddress; i++) {
532 if (validAddress(i,reg->size)) {
539 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
546 /*-----------------------------------------------------------------*
547 *-----------------------------------------------------------------*/
548 void assignFixedRegisters(set *regset)
552 for (reg = setFirstItem(regset) ; reg ;
553 reg = setNextItem(regset)) {
556 assignRegister(reg,0);
561 /*-----------------------------------------------------------------*
562 *-----------------------------------------------------------------*/
563 void assignRelocatableRegisters(set *regset, int used)
569 for (reg = setFirstItem(regset) ; reg ;
570 reg = setNextItem(regset)) {
572 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
574 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
575 /* If register have been reused then shall not print it a second time. */
578 for (s = regset; s; s = s->next) {
583 if((!r->isFixed) && ( used || r->wasUsed)) {
584 if (r->rIdx == reg->rIdx) {
585 reg->address = r->address;
592 address = assignRegister(reg,address);
599 /*-----------------------------------------------------------------*
600 * void assignConfigWordValue(int address, int value)
602 * All midrange PICs have one config word at address 0x2007.
603 * This routine will assign a value to that address.
605 *-----------------------------------------------------------------*/
607 void assignConfigWordValue(int address, int value)
609 if(CONFIG_WORD_ADDRESS == address)
612 //fprintf(stderr,"setting config word to 0x%x\n",value);
615 /*-----------------------------------------------------------------*
616 * int getConfigWord(int address)
618 * Get the current value of the config word.
620 *-----------------------------------------------------------------*/
622 int getConfigWord(int address)
624 if(CONFIG_WORD_ADDRESS == address)
632 /*-----------------------------------------------------------------*
634 *-----------------------------------------------------------------*/
635 void setDefMaxRam(void)
638 setMaxRAM(pic->defMaxRAMaddrs); /* Max RAM has not been included, so use default setting */
639 /* Validate full memory range for use by general purpose RAM */
640 for (i=pic->defMaxRAMaddrs; i--; ) {
641 finalMapping[i].bank = (i>>7);
642 finalMapping[i].isValid = 1;
646 /*-----------------------------------------------------------------*
648 *-----------------------------------------------------------------*/
649 unsigned getMaxRam(void)
651 return pic->defMaxRAMaddrs;