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 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 void addMemRange(memRange *r, int type)
108 int alias = r->alias;
110 if (pic->maxRAMaddress < 0) {
111 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
116 for (i=r->start_address; i<= r->end_address; i++) {
117 if ((i|alias) <= pic->maxRAMaddress) {
118 finalMapping[i | alias].isValid = 1;
119 finalMapping[i | alias].alias = r->alias;
120 finalMapping[i | alias].bank = r->bank;
123 finalMapping[i | alias].isSFR = 1;
125 finalMapping[i | alias].isSFR = 0;
128 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
129 __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
133 /* Decrement alias */
135 alias -= ((alias & (alias - 1)) ^ alias);
140 } while (alias >= 0);
143 void setMaxRAM(int size)
146 pic->maxRAMaddress = size;
148 if (pic->maxRAMaddress < 0) {
149 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
154 finalMapping = Safe_calloc(1+pic->maxRAMaddress,
155 sizeof(AssignedMemory));
157 /* Now initialize the finalMapping array */
159 for(i=0; i<=pic->maxRAMaddress; i++) {
160 finalMapping[i].reg = NULL;
161 finalMapping[i].isValid = 0;
162 finalMapping[i].bank = (i>>7);
166 /*-----------------------------------------------------------------*
167 *-----------------------------------------------------------------*/
169 int isREGinBank(regs *reg, int bank)
175 if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
181 /*-----------------------------------------------------------------*
182 *-----------------------------------------------------------------*/
183 int REGallBanks(regs *reg)
189 return ((reg->address | reg->alias) & pic->bankMask);
193 /*-----------------------------------------------------------------*
194 *-----------------------------------------------------------------*/
197 * dump_map -- debug stuff
204 for(i=0; i<=pic->maxRAMaddress; i++) {
205 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
207 if(finalMapping[i].isValid) {
208 fprintf(stderr,"addr: 0x%02x",i);
209 if(finalMapping[i].isSFR)
210 fprintf(stderr," isSFR");
211 if(finalMapping[i].reg)
212 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
213 fprintf(stderr, "\n");
219 void dump_sfr(FILE *of)
225 static int udata_flag=0;
227 //dump_map(); /* display the register map */
228 //fprintf(stdout,";dump_sfr \n");
229 if (pic->maxRAMaddress < 0) {
230 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
236 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
243 /* clear the lower 7-bits of the start address of the first
244 * variable declared in this bank. The upper bits for the mid
245 * range pics are the bank select bits.
248 bank_base = start & 0xfff8;
250 /* The bank number printed in the cblock comment tacitly
251 * assumes that the first register in the contiguous group
252 * of registers represents the bank for the whole group */
254 if ( (start != addr) && (!udata_flag) ) {
256 //fprintf(of,"\tudata\n");
259 for( ; start < addr; start++) {
260 if((finalMapping[start].reg) &&
261 (!finalMapping[start].reg->isEmitted) &&
262 (!finalMapping[start].instance) &&
263 (!finalMapping[start].isSFR)) {
265 if (finalMapping[start].reg->isFixed) {
267 for (i=0; i<finalMapping[start].reg->size; i++) {
268 fprintf(of,"%s\tEQU\t0x%04x\n",
269 finalMapping[start].reg->name,
270 finalMapping[start].reg->address+i);
273 fprintf(of,"%s\tres\t%i\n",
274 finalMapping[start].reg->name,
275 finalMapping[start].reg->size);
277 finalMapping[start].reg->isEmitted = 1;
288 } while(addr <= pic->maxRAMaddress);
293 /*-----------------------------------------------------------------*
294 * void list_valid_pics(int ncols, int list_alias)
296 * Print out a formatted list of valid PIC devices
298 * ncols - number of columns in the list.
300 * list_alias - if non-zero, print all of the supported aliases
301 * for a device (e.g. F84, 16F84, etc...)
302 *-----------------------------------------------------------------*/
303 void list_valid_pics(int ncols, int list_alias)
309 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
311 /* decrement the column number if it's greater than zero */
312 ncols = (ncols > 1) ? ncols-1 : 4;
314 /* Find the device with the longest name */
315 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
316 for(j=0; j<=list_alias; j++) {
317 k = strlen(Pics[i].name[j]);
325 for(i=0; i < num_of_supported_PICS; i++) {
329 fprintf(stderr,"%s", Pics[i].name[j]);
331 l = longest + 2 - strlen(Pics[i].name[j]);
342 } while(++j<list_alias);
350 /*-----------------------------------------------------------------*
352 *-----------------------------------------------------------------*/
353 PIC_device *find_device(char *name)
361 for(i = 0; i<num_of_supported_PICS; i++) {
363 for(j=0; j<PROCESSOR_NAMES; j++)
364 if(!STRCASECMP(Pics[i].name[j], name) )
372 /*-----------------------------------------------------------------*
374 *-----------------------------------------------------------------*/
375 void init_pic(char *pic_type)
377 pic = find_device(pic_type);
381 fprintf(stderr, "'%s' was not found.\n", pic_type);
383 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
385 fprintf(stderr,"Valid devices are:\n");
387 list_valid_pics(4,0);
391 pic->maxRAMaddress = -1;
394 /*-----------------------------------------------------------------*
396 *-----------------------------------------------------------------*/
397 int picIsInitialized(void)
399 if(pic && pic->maxRAMaddress > 0)
406 /*-----------------------------------------------------------------*
407 * char *processor_base_name(void) - Include file is derived from this.
408 *-----------------------------------------------------------------*/
409 char *processor_base_name(void)
418 int isSFR(int address)
421 if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
428 /*-----------------------------------------------------------------*
429 *-----------------------------------------------------------------*/
430 int validAddress(int address, int reg_size)
434 if (pic->maxRAMaddress < 0) {
435 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
438 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
439 if(address > pic->maxRAMaddress)
442 for (i=0; i<reg_size; i++)
443 if(!finalMapping[address + i].isValid ||
444 finalMapping[address+i].reg ||
445 finalMapping[address+i].isSFR )
451 /*-----------------------------------------------------------------*
452 *-----------------------------------------------------------------*/
453 void mapRegister(regs *reg)
459 if(!reg || !reg->size) {
460 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
464 if (pic->maxRAMaddress < 0) {
465 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
469 for(i=0; i<reg->size; i++) {
471 alias = finalMapping[reg->address].alias;
476 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
478 finalMapping[reg->address + alias + i].reg = reg;
479 finalMapping[reg->address + alias + i].instance = i;
481 /* Decrement alias */
483 alias -= ((alias & (alias - 1)) ^ alias);
490 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
496 /*-----------------------------------------------------------------*
497 *-----------------------------------------------------------------*/
498 int assignRegister(regs *reg, int start_address)
502 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
505 if (validAddress(reg->address,reg->size)) {
506 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
511 if( isSFR(reg->address)) {
516 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
517 // reg->address, reg->name);
521 /* This register does not have a fixed address requirement
522 * so we'll search through all availble ram address and
523 * assign the first one */
525 for (i=start_address; i<=pic->maxRAMaddress; i++) {
527 if (validAddress(i,reg->size)) {
534 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
541 /*-----------------------------------------------------------------*
542 *-----------------------------------------------------------------*/
543 void assignFixedRegisters(set *regset)
547 for (reg = setFirstItem(regset) ; reg ;
548 reg = setNextItem(regset)) {
551 assignRegister(reg,0);
556 /*-----------------------------------------------------------------*
557 *-----------------------------------------------------------------*/
558 void assignRelocatableRegisters(set *regset, int used)
564 for (reg = setFirstItem(regset) ; reg ;
565 reg = setNextItem(regset)) {
567 //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
569 if((!reg->isFixed) && ( used || reg->wasUsed)) {
570 /* If register have been reused then shall not print it a second time. */
573 for (s = regset; s; s = s->next) {
578 if((!r->isFixed) && ( used || r->wasUsed)) {
579 if (r->rIdx == reg->rIdx) {
580 reg->address = r->address;
587 address = assignRegister(reg,address);
594 /*-----------------------------------------------------------------*
595 * void assignConfigWordValue(int address, int value)
597 * All midrange PICs have one config word at address 0x2007.
598 * This routine will assign a value to that address.
600 *-----------------------------------------------------------------*/
602 void assignConfigWordValue(int address, int value)
604 if(CONFIG_WORD_ADDRESS == address)
607 //fprintf(stderr,"setting config word to 0x%x\n",value);
610 /*-----------------------------------------------------------------*
611 * int getConfigWord(int address)
613 * Get the current value of the config word.
615 *-----------------------------------------------------------------*/
617 int getConfigWord(int address)
619 if(CONFIG_WORD_ADDRESS == address)
627 /*-----------------------------------------------------------------*
629 *-----------------------------------------------------------------*/
630 void setDefMaxRam(void)
633 setMaxRAM(pic->defMaxRAMaddrs); /* Max RAM has not been included, so use default setting */
634 /* Validate full memory range for use by general purpose RAM */
635 for (i=pic->defMaxRAMaddrs; i--; ) {
636 finalMapping[i].bank = (i>>7);
637 finalMapping[i].isValid = 1;
641 /*-----------------------------------------------------------------*
643 *-----------------------------------------------------------------*/
644 unsigned getMaxRam(void)
646 return pic->defMaxRAMaddrs;