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) */
47 {"p16f628", "16f628", "pic16f628", "f628"},
55 {"p16f84", "16f84", "pic16f84", "f84"},
63 {"p16f873", "16f873", "pic16f873", "f873"},
71 {"p16f877", "16f877", "pic16f877", "f877"},
79 {"p16f819", "16f819", "pic16f819", "f819"},
88 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
90 #define DEFAULT_PIC "f877"
92 static PIC_device *pic=NULL;
94 AssignedMemory *finalMapping=NULL;
96 #define CONFIG_WORD_ADDRESS 0x2007
97 #define DEFAULT_CONFIG_WORD 0x3fff
99 static unsigned int config_word = DEFAULT_CONFIG_WORD;
101 void addMemRange(memRange *r, int type)
104 int alias = r->alias;
106 if (pic->maxRAMaddress < 0) {
107 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
112 for (i=r->start_address; i<= r->end_address; i++) {
113 if ((i|alias) <= pic->maxRAMaddress) {
114 finalMapping[i | alias].isValid = 1;
115 finalMapping[i | alias].alias = r->alias;
116 finalMapping[i | alias].bank = r->bank;
119 finalMapping[i | alias].isSFR = 1;
121 finalMapping[i | alias].isSFR = 0;
124 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
125 __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
129 /* Decrement alias */
131 alias -= ((alias & (alias - 1)) ^ alias);
136 } while (alias >= 0);
139 void setMaxRAM(int size)
142 pic->maxRAMaddress = size;
144 if (pic->maxRAMaddress < 0) {
145 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
150 finalMapping = Safe_calloc(1+pic->maxRAMaddress,
151 sizeof(AssignedMemory));
153 /* Now initialize the finalMapping array */
155 for(i=0; i<=pic->maxRAMaddress; i++) {
156 finalMapping[i].reg = NULL;
157 finalMapping[i].isValid = 0;
161 /*-----------------------------------------------------------------*
162 *-----------------------------------------------------------------*/
164 int isREGinBank(regs *reg, int bank)
170 if(((reg->address | reg->alias) & pic->bankMask & bank) == bank)
176 /*-----------------------------------------------------------------*
177 *-----------------------------------------------------------------*/
178 int REGallBanks(regs *reg)
184 return ((reg->address | reg->alias) & pic->bankMask);
188 /*-----------------------------------------------------------------*
189 *-----------------------------------------------------------------*/
192 * dump_map -- debug stuff
199 for(i=0; i<=pic->maxRAMaddress; i++) {
200 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
202 if(finalMapping[i].isValid) {
203 fprintf(stderr,"addr: 0x%02x",i);
204 if(finalMapping[i].isSFR)
205 fprintf(stderr," isSFR");
206 if(finalMapping[i].reg)
207 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
208 fprintf(stderr, "\n");
214 void dump_sfr(FILE *of)
220 static int udata_flag=0;
222 //dump_map(); /* display the register map */
223 //fprintf(stdout,";dump_sfr \n");
224 if (pic->maxRAMaddress < 0) {
225 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
231 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
238 /* clear the lower 7-bits of the start address of the first
239 * variable declared in this bank. The upper bits for the mid
240 * range pics are the bank select bits.
243 bank_base = start & 0xfff8;
245 /* The bank number printed in the cblock comment tacitly
246 * assumes that the first register in the contiguous group
247 * of registers represents the bank for the whole group */
249 if ( (start != addr) && (!udata_flag) ) {
251 //fprintf(of,"\tudata\n");
254 for( ; start < addr; start++) {
255 if((finalMapping[start].reg) &&
256 (!finalMapping[start].reg->isEmitted) &&
257 (!finalMapping[start].instance) &&
258 (!finalMapping[start].isSFR)) {
260 fprintf(of,"%s\tres\t%i\n",
261 finalMapping[start].reg->name,
262 finalMapping[start].reg->size);
264 finalMapping[start].reg->isEmitted = 1;
275 } while(addr <= pic->maxRAMaddress);
280 /*-----------------------------------------------------------------*
281 * void list_valid_pics(int ncols, int list_alias)
283 * Print out a formatted list of valid PIC devices
285 * ncols - number of columns in the list.
287 * list_alias - if non-zero, print all of the supported aliases
288 * for a device (e.g. F84, 16F84, etc...)
289 *-----------------------------------------------------------------*/
290 void list_valid_pics(int ncols, int list_alias)
296 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
298 /* decrement the column number if it's greater than zero */
299 ncols = (ncols > 1) ? ncols-1 : 4;
301 /* Find the device with the longest name */
302 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
303 for(j=0; j<=list_alias; j++) {
304 k = strlen(Pics[i].name[j]);
312 for(i=0; i < num_of_supported_PICS; i++) {
316 fprintf(stderr,"%s", Pics[i].name[j]);
318 l = longest + 2 - strlen(Pics[i].name[j]);
329 } while(++j<list_alias);
337 /*-----------------------------------------------------------------*
339 *-----------------------------------------------------------------*/
340 PIC_device *find_device(char *name)
348 for(i = 0; i<num_of_supported_PICS; i++) {
350 for(j=0; j<PROCESSOR_NAMES; j++)
351 if(!STRCASECMP(Pics[i].name[j], name) )
359 /*-----------------------------------------------------------------*
361 *-----------------------------------------------------------------*/
362 void init_pic(char *pic_type)
364 pic = find_device(pic_type);
368 fprintf(stderr, "'%s' was not found.\n", pic_type);
370 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
372 fprintf(stderr,"Valid devices are:\n");
374 list_valid_pics(4,0);
378 pic->maxRAMaddress = -1;
381 /*-----------------------------------------------------------------*
383 *-----------------------------------------------------------------*/
384 int picIsInitialized(void)
386 if(pic && pic->maxRAMaddress > 0)
393 /*-----------------------------------------------------------------*
394 * char *processor_base_name(void) - Include file is derived from this.
395 *-----------------------------------------------------------------*/
396 char *processor_base_name(void)
405 int isSFR(int address)
408 if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
415 /*-----------------------------------------------------------------*
416 *-----------------------------------------------------------------*/
417 int validAddress(int address, int reg_size)
421 if (pic->maxRAMaddress < 0) {
422 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
425 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
426 if(address > pic->maxRAMaddress)
429 for (i=0; i<reg_size; i++)
430 if(!finalMapping[address + i].isValid ||
431 finalMapping[address+i].reg ||
432 finalMapping[address+i].isSFR )
438 /*-----------------------------------------------------------------*
439 *-----------------------------------------------------------------*/
440 void mapRegister(regs *reg)
446 if(!reg || !reg->size) {
447 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
451 if (pic->maxRAMaddress < 0) {
452 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
456 for(i=0; i<reg->size; i++) {
458 alias = finalMapping[reg->address].alias;
463 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
465 finalMapping[reg->address + alias + i].reg = reg;
466 finalMapping[reg->address + alias + i].instance = i;
468 /* Decrement alias */
470 alias -= ((alias & (alias - 1)) ^ alias);
477 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
483 /*-----------------------------------------------------------------*
484 *-----------------------------------------------------------------*/
485 int assignRegister(regs *reg, int start_address)
489 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
492 if (validAddress(reg->address,reg->size)) {
493 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
498 if( isSFR(reg->address)) {
503 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
504 // reg->address, reg->name);
508 /* This register does not have a fixed address requirement
509 * so we'll search through all availble ram address and
510 * assign the first one */
512 for (i=start_address; i<=pic->maxRAMaddress; i++) {
514 if (validAddress(i,reg->size)) {
521 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
528 /*-----------------------------------------------------------------*
529 *-----------------------------------------------------------------*/
530 void assignFixedRegisters(set *regset)
534 for (reg = setFirstItem(regset) ; reg ;
535 reg = setNextItem(regset)) {
538 assignRegister(reg,0);
543 /*-----------------------------------------------------------------*
544 *-----------------------------------------------------------------*/
545 void assignRelocatableRegisters(set *regset, int used)
551 for (reg = setFirstItem(regset) ; reg ;
552 reg = setNextItem(regset)) {
554 //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
556 if((!reg->isFixed) && ( used || reg->wasUsed))
557 address = assignRegister(reg,address);
564 /*-----------------------------------------------------------------*
565 * void assignConfigWordValue(int address, int value)
567 * All midrange PICs have one config word at address 0x2007.
568 * This routine will assign a value to that address.
570 *-----------------------------------------------------------------*/
572 void assignConfigWordValue(int address, int value)
574 if(CONFIG_WORD_ADDRESS == address)
577 //fprintf(stderr,"setting config word to 0x%x\n",value);
580 /*-----------------------------------------------------------------*
581 * int getConfigWord(int address)
583 * Get the current value of the config word.
585 *-----------------------------------------------------------------*/
587 int getConfigWord(int address)
589 if(CONFIG_WORD_ADDRESS == address)