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"},
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 #define DEFAULT_PIC "f877"
98 static PIC_device *pic=NULL;
100 AssignedMemory *finalMapping=NULL;
102 #define CONFIG_WORD_ADDRESS 0x2007
103 #define DEFAULT_CONFIG_WORD 0x3fff
105 static unsigned int config_word = DEFAULT_CONFIG_WORD;
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;
167 /*-----------------------------------------------------------------*
168 *-----------------------------------------------------------------*/
170 int isREGinBank(regs *reg, int bank)
176 if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
182 /*-----------------------------------------------------------------*
183 *-----------------------------------------------------------------*/
184 int REGallBanks(regs *reg)
190 return ((reg->address | reg->alias) & pic->bankMask);
194 /*-----------------------------------------------------------------*
195 *-----------------------------------------------------------------*/
198 * dump_map -- debug stuff
205 for(i=0; i<=pic->maxRAMaddress; i++) {
206 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
208 if(finalMapping[i].isValid) {
209 fprintf(stderr,"addr: 0x%02x",i);
210 if(finalMapping[i].isSFR)
211 fprintf(stderr," isSFR");
212 if(finalMapping[i].reg)
213 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
214 fprintf(stderr, "\n");
220 void dump_sfr(FILE *of)
226 static int udata_flag=0;
228 //dump_map(); /* display the register map */
229 //fprintf(stdout,";dump_sfr \n");
230 if (pic->maxRAMaddress < 0) {
231 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
237 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
244 /* clear the lower 7-bits of the start address of the first
245 * variable declared in this bank. The upper bits for the mid
246 * range pics are the bank select bits.
249 bank_base = start & 0xfff8;
251 /* The bank number printed in the cblock comment tacitly
252 * assumes that the first register in the contiguous group
253 * of registers represents the bank for the whole group */
255 if ( (start != addr) && (!udata_flag) ) {
257 //fprintf(of,"\tudata\n");
260 for( ; start < addr; start++) {
261 if((finalMapping[start].reg) &&
262 (!finalMapping[start].reg->isEmitted) &&
263 (!finalMapping[start].instance) &&
264 (!finalMapping[start].isSFR)) {
266 if (finalMapping[start].reg->isFixed) {
268 for (i=0; i<finalMapping[start].reg->size; i++) {
269 fprintf(of,"%s\tEQU\t0x%04x\n",
270 finalMapping[start].reg->name,
271 finalMapping[start].reg->address+i);
274 fprintf(of,"%s\tres\t%i\n",
275 finalMapping[start].reg->name,
276 finalMapping[start].reg->size);
278 finalMapping[start].reg->isEmitted = 1;
289 } while(addr <= pic->maxRAMaddress);
294 /*-----------------------------------------------------------------*
295 * void list_valid_pics(int ncols, int list_alias)
297 * Print out a formatted list of valid PIC devices
299 * ncols - number of columns in the list.
301 * list_alias - if non-zero, print all of the supported aliases
302 * for a device (e.g. F84, 16F84, etc...)
303 *-----------------------------------------------------------------*/
304 void list_valid_pics(int ncols, int list_alias)
310 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
312 /* decrement the column number if it's greater than zero */
313 ncols = (ncols > 1) ? ncols-1 : 4;
315 /* Find the device with the longest name */
316 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
317 for(j=0; j<=list_alias; j++) {
318 k = strlen(Pics[i].name[j]);
326 for(i=0; i < num_of_supported_PICS; i++) {
330 fprintf(stderr,"%s", Pics[i].name[j]);
332 l = longest + 2 - strlen(Pics[i].name[j]);
343 } while(++j<list_alias);
351 /*-----------------------------------------------------------------*
353 *-----------------------------------------------------------------*/
354 PIC_device *find_device(char *name)
362 for(i = 0; i<num_of_supported_PICS; i++) {
364 for(j=0; j<PROCESSOR_NAMES; j++)
365 if(!STRCASECMP(Pics[i].name[j], name) )
373 /*-----------------------------------------------------------------*
375 *-----------------------------------------------------------------*/
376 void init_pic(char *pic_type)
378 pic = find_device(pic_type);
382 fprintf(stderr, "'%s' was not found.\n", pic_type);
384 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
386 fprintf(stderr,"Valid devices are:\n");
388 list_valid_pics(4,0);
392 pic->maxRAMaddress = -1;
395 /*-----------------------------------------------------------------*
397 *-----------------------------------------------------------------*/
398 int picIsInitialized(void)
400 if(pic && pic->maxRAMaddress > 0)
407 /*-----------------------------------------------------------------*
408 * char *processor_base_name(void) - Include file is derived from this.
409 *-----------------------------------------------------------------*/
410 char *processor_base_name(void)
419 int isSFR(int address)
422 if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
429 /*-----------------------------------------------------------------*
430 *-----------------------------------------------------------------*/
431 int validAddress(int address, int reg_size)
435 if (pic->maxRAMaddress < 0) {
436 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
439 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
440 if(address > pic->maxRAMaddress)
443 for (i=0; i<reg_size; i++)
444 if(!finalMapping[address + i].isValid ||
445 finalMapping[address+i].reg ||
446 finalMapping[address+i].isSFR )
452 /*-----------------------------------------------------------------*
453 *-----------------------------------------------------------------*/
454 void mapRegister(regs *reg)
460 if(!reg || !reg->size) {
461 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
465 if (pic->maxRAMaddress < 0) {
466 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
470 for(i=0; i<reg->size; i++) {
472 alias = finalMapping[reg->address].alias;
477 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
479 finalMapping[reg->address + alias + i].reg = reg;
480 finalMapping[reg->address + alias + i].instance = i;
482 /* Decrement alias */
484 alias -= ((alias & (alias - 1)) ^ alias);
491 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
497 /*-----------------------------------------------------------------*
498 *-----------------------------------------------------------------*/
499 int assignRegister(regs *reg, int start_address)
503 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
506 if (validAddress(reg->address,reg->size)) {
507 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
512 if( isSFR(reg->address)) {
517 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
518 // reg->address, reg->name);
522 /* This register does not have a fixed address requirement
523 * so we'll search through all availble ram address and
524 * assign the first one */
526 for (i=start_address; i<=pic->maxRAMaddress; i++) {
528 if (validAddress(i,reg->size)) {
535 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
542 /*-----------------------------------------------------------------*
543 *-----------------------------------------------------------------*/
544 void assignFixedRegisters(set *regset)
548 for (reg = setFirstItem(regset) ; reg ;
549 reg = setNextItem(regset)) {
552 assignRegister(reg,0);
557 /*-----------------------------------------------------------------*
558 *-----------------------------------------------------------------*/
559 void assignRelocatableRegisters(set *regset, int used)
565 for (reg = setFirstItem(regset) ; reg ;
566 reg = setNextItem(regset)) {
568 //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
570 if((!reg->isFixed) && ( used || reg->wasUsed)) {
571 /* If register have been reused then shall not print it a second time. */
574 for (s = regset; s; s = s->next) {
579 if((!r->isFixed) && ( used || r->wasUsed)) {
580 if (r->rIdx == reg->rIdx) {
581 reg->address = r->address;
588 address = assignRegister(reg,address);
595 /*-----------------------------------------------------------------*
596 * void assignConfigWordValue(int address, int value)
598 * All midrange PICs have one config word at address 0x2007.
599 * This routine will assign a value to that address.
601 *-----------------------------------------------------------------*/
603 void assignConfigWordValue(int address, int value)
605 if(CONFIG_WORD_ADDRESS == address)
608 //fprintf(stderr,"setting config word to 0x%x\n",value);
611 /*-----------------------------------------------------------------*
612 * int getConfigWord(int address)
614 * Get the current value of the config word.
616 *-----------------------------------------------------------------*/
618 int getConfigWord(int address)
620 if(CONFIG_WORD_ADDRESS == address)
628 /*-----------------------------------------------------------------*
630 *-----------------------------------------------------------------*/
631 void setDefMaxRam(void)
633 setMaxRAM(pic->defMaxRAMaddrs); // Max RAM has not been included, so use default setting