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 -------------------------------------------------------------------------*/
25 #include "common.h" // Include everything in the SDCC src directory
34 #if defined(__BORLANDC__) || defined(_MSC_VER)
35 #define STRCASECMP stricmp
36 #define STRNCASECMP strnicmp
38 #define STRCASECMP strcasecmp
39 #define STRNCASECMP strncasecmp
42 extern int Gstack_base_addr;
43 extern int Gstack_size;
45 #define MAX_PICLIST 200
46 static PIC_device *Pics[MAX_PICLIST];
47 static int num_of_supported_PICS = 0;
49 static PIC_device *pic=NULL;
51 int maxRAMaddress = 0;
52 AssignedMemory *finalMapping=NULL;
54 #define CONFIG_WORD_ADDRESS 0x2007
55 #define CONFIG2_WORD_ADDRESS 0x2008
56 #define DEFAULT_CONFIG_WORD 0x3fff
57 #define DEFAULT_CONFIG2_WORD 0x3ffc
59 #define DEVICE_FILE_NAME "pic14devices.txt"
60 #define PIC14_STRING_LEN 256
61 #define SPLIT_WORDS_MAX 16
63 static unsigned int config_word = DEFAULT_CONFIG_WORD;
64 static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
65 static memRange *rangeRAM = NULL;
67 extern int pic14_is_shared (regs *reg);
68 extern void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize);
71 /* parse a value from the configuration file */
72 static int parse_config_value(char *str)
74 if (str[strlen(str) - 1] == 'K')
75 return atoi(str) * 1024; /* like "1K" */
77 else if (STRNCASECMP(str, "0x", 2) == 0)
78 return strtol(str+2, NULL, 16); /* like "0x400" */
81 return atoi(str); /* like "1024" */
85 /* split a line into words */
86 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
92 while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
93 /* remove leading spaces */
94 while (isspace(*pos) || *pos == ',')
97 /* copy everything up until the first space or comma */
98 for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
99 result_word[num_words][ccount] = *pos;
100 result_word[num_words][ccount] = '\0';
109 /* remove annoying prefixes from the processor name */
110 static char *sanitise_processor_name(char *name)
112 char *proc_pos = name;
117 if (STRNCASECMP(proc_pos, "pic", 3) == 0)
120 else if (tolower(*proc_pos) == 'p')
127 /* create a structure for a pic processor */
128 static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
131 char *simple_pic_name = sanitise_processor_name(pic_name);
133 new_pic = Safe_calloc(1, sizeof(PIC_device));
134 new_pic->name = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
135 strcpy(new_pic->name, simple_pic_name);
137 new_pic->defMaxRAMaddrs = maxram;
138 new_pic->bankMask = bankmsk;
139 new_pic->hasSecondConfigReg = confsiz > 1;
141 new_pic->programMemSize = program;
142 new_pic->dataMemSize = data;
143 new_pic->eepromMemSize = eeprom;
144 new_pic->ioPins = io;
146 new_pic->ram = rangeRAM;
148 Pics[num_of_supported_PICS] = new_pic;
149 num_of_supported_PICS++;
155 /* mark some registers as being duplicated across banks */
156 static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
162 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
166 r.alias = parse_config_value(word[1]);
168 for (pcount = 2; pcount < num_words; pcount++) {
170 r.start_address = parse_config_value(word[pcount]);
171 r.end_address = parse_config_value(word[pcount]);
172 r.bank = (r.start_address >> 7) & 3;
179 /* define ram areas - may be duplicated across banks */
180 static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
185 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
189 r = Safe_calloc(1, sizeof(memRange));
190 //fprintf (stderr, "%s: %s %s %s\n", __FUNCTION__, word[1], word[2], word[3]);
192 r->start_address = parse_config_value(word[1]);
193 r->end_address = parse_config_value(word[2]);
194 r->alias = parse_config_value(word[3]);
195 r->bank = (r->start_address >> 7) & 3;
199 // add memRange to device entry for future lookup (sharebanks)
204 extern set *includeDirsSet;
205 extern set *userIncDirsSet;
206 extern set *libDirsSet;
207 extern set *libPathsSet;
209 /* read the file with all the pic14 definitions and pick out the definition for a processor
210 * if specified. if pic_name is NULL reads everything */
211 static PIC_device *find_device(char *pic_name)
214 char pic_buf[PIC14_STRING_LEN];
216 int found_processor = FALSE;
218 char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
219 int num_processor_names = 0;
227 char *simple_pic_name;
232 /* allow abbreviations of the form "f877" - convert to "16f877" */
233 simple_pic_name = sanitise_processor_name(pic_name);
234 num_of_supported_PICS = 0;
236 /* open the piclist file */
237 /* first scan all include directories */
239 //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
240 for (dir = setFirstItem(includeDirsSet);
242 dir = setNextItem(includeDirsSet))
244 //fprintf( stderr, "searching1 %s\n", dir );
245 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
246 pic_file = fopen( filename, "rt" );
249 for (dir = setFirstItem(userIncDirsSet);
251 dir = setNextItem(userIncDirsSet))
253 //fprintf( stderr, "searching2 %s\n", dir );
254 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
255 pic_file = fopen( filename, "rt" );
258 for (dir = setFirstItem(libDirsSet);
260 dir = setNextItem(libDirsSet))
262 //fprintf( stderr, "searching3 %s\n", dir );
263 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
264 pic_file = fopen( filename, "rt" );
267 for (dir = setFirstItem(libPathsSet);
269 dir = setNextItem(libPathsSet))
271 //fprintf( stderr, "searching4 %s\n", dir );
272 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
273 pic_file = fopen( filename, "rt" );
277 pic_file = fopen(DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME, "rt");
279 if (pic_file == NULL) {
280 /* this second attempt is used when initially building the libraries */
281 pic_file = fopen(".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".."
282 DIR_SEPARATOR_STRING "src" DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING
283 DEVICE_FILE_NAME, "rt");
284 if (pic_file == NULL) {
285 fprintf(stderr, "can't find %s\n", DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic"
286 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
291 /* read line by line */
292 pic_buf[sizeof(pic_buf)-1] = '\0';
293 while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
295 /* remove trailing spaces */
296 while (isspace(pic_buf[strlen(pic_buf)-1]))
297 pic_buf[strlen(pic_buf)-1] = '\0';
299 /* remove leading spaces */
300 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
303 /* ignore comment / empty lines */
304 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
306 /* split into fields */
307 char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
311 num_pic_words = split_words(pic_word, pic_buf_pos);
313 if (STRCASECMP(pic_word[0], "processor") == 0) {
315 if (pic_name == NULL) {
316 /* this is the mode where we read all the processors in - store the names for now */
317 if (num_processor_names > 0) {
318 /* store away all the previous processor definitions */
321 for (dcount = 1; dcount < num_processor_names; dcount++)
322 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
323 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
326 num_processor_names = split_words(processor_name, pic_buf_pos);
329 /* if we've just completed reading a processor definition stop now */
333 /* check if this processor name is a match */
334 for (wcount = 1; wcount < num_pic_words; wcount++) {
336 /* skip uninteresting prefixes */
337 char *found_name = sanitise_processor_name(pic_word[wcount]);
339 if (STRCASECMP(found_name, simple_pic_name) == 0)
340 found_processor = TRUE;
347 if (found_processor || pic_name == NULL) {
348 /* only parse a processor section if we've found the one we want */
349 if (STRCASECMP(pic_word[0], "maxram") == 0 && num_pic_words > 1) {
350 pic_maxram = parse_config_value(pic_word[1]);
351 setMaxRAM(pic_maxram);
353 else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
354 pic_bankmsk = parse_config_value(pic_word[1]);
356 else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
357 pic_confsiz = parse_config_value(pic_word[1]);
359 else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
360 pic_program = parse_config_value(pic_word[1]);
362 else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
363 pic_data = parse_config_value(pic_word[1]);
365 else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
366 pic_eeprom = parse_config_value(pic_word[1]);
368 else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
369 pic_io = parse_config_value(pic_word[1]);
371 else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
373 register_map(num_pic_words, pic_word);
375 else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
377 ram_map(num_pic_words, pic_word);
380 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
389 /* if we're in read-the-lot mode then create the final processor definition */
390 if (pic_name == NULL) {
392 if (num_processor_names > 0) {
393 /* store away all the previous processor definitions */
396 for (dcount = 1; dcount < num_processor_names; dcount++)
397 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
398 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
403 if (found_processor) {
404 /* create a new pic entry */
405 return create_pic(pic_name, pic_maxram, pic_bankmsk,
406 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
413 void addMemRange(memRange *r, int type)
416 int alias = r->alias;
418 //fprintf (stderr, "%s: range %x..%x, alias %x, bank %x\n", __FUNCTION__, r->start_address, r->end_address, r->alias, r->bank);
420 if (maxRAMaddress < 0) {
421 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
426 for (i=r->start_address; i<= r->end_address; i++) {
427 if ((i|alias) <= maxRAMaddress) {
428 /* if we haven't seen this address before, enter it */
429 if (!finalMapping[i | alias].isValid) {
430 finalMapping[i | alias].isValid = 1;
431 finalMapping[i | alias].alias = r->alias;
432 finalMapping[i | alias].bank = r->bank;
435 finalMapping[i | alias].isSFR = 1;
437 finalMapping[i | alias].isSFR = 0;
441 if (getenv("SDCCPICDEBUG")) {
442 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
443 __FILE__,__FUNCTION__,(i|alias), maxRAMaddress);
448 /* Decrement alias */
450 alias -= ((alias & (alias - 1)) ^ alias);
455 } while (alias >= 0);
458 void setMaxRAM(int size)
461 maxRAMaddress = size;
463 if (maxRAMaddress < 0) {
464 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
465 maxRAMaddress, DEVICE_FILE_NAME);
469 finalMapping = Safe_calloc(1+maxRAMaddress,
470 sizeof(AssignedMemory));
472 /* Now initialize the finalMapping array */
474 for(i=0; i<=maxRAMaddress; i++) {
475 finalMapping[i].reg = NULL;
476 finalMapping[i].isValid = 0;
477 finalMapping[i].bank = (i>>7);
481 /*-----------------------------------------------------------------*
482 *-----------------------------------------------------------------*/
484 int isREGinBank(regs *reg, int bank)
490 if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
496 /*-----------------------------------------------------------------*
497 *-----------------------------------------------------------------*/
498 int REGallBanks(regs *reg)
504 return ((reg->address | reg->alias) & pic->bankMask);
508 /*-----------------------------------------------------------------*
509 *-----------------------------------------------------------------*/
511 int isSFR(int address)
514 if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
522 * dump_map -- debug stuff
529 for(i=0; i<=maxRAMaddress; i++) {
530 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
532 if(finalMapping[i].isValid) {
533 fprintf(stderr,"addr: 0x%02x",i);
534 if(finalMapping[i].isSFR)
535 fprintf(stderr," isSFR");
536 if(finalMapping[i].reg)
537 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
538 fprintf(stderr, "\n");
544 void dump_sfr(FILE *of)
549 static int udata_flag=0;
553 //dump_map(); /* display the register map */
554 //fprintf(stdout,";dump_sfr \n");
555 if (maxRAMaddress < 0) {
556 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
560 for (addr = 0; addr <= maxRAMaddress; addr++)
562 regs *reg = finalMapping[addr].reg;
564 if (reg && !reg->isEmitted)
566 if (pic14_options.isLibrarySource && pic14_is_shared (reg))
568 /* rely on external declarations for the non-fixed stack */
569 /* Update: We always emit the STACK symbols into a
570 * udata_shr section, so no extern declaration is
572 //fprintf (of, "\textern\t%s\n", reg->name);
574 emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, pic14_is_shared (reg));
584 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
591 /* clear the lower 7-bits of the start address of the first
592 * variable declared in this bank. The upper bits for the mid
593 * range pics are the bank select bits.
596 bank_base = start & 0xfff8;
598 /* The bank number printed in the cblock comment tacitly
599 * assumes that the first register in the contiguous group
600 * of registers represents the bank for the whole group */
602 if ( (start != addr) && (!udata_flag) ) {
604 //fprintf(of,"\tudata\n");
607 for( ; start < addr; start++) {
608 if((finalMapping[start].reg) &&
609 (!finalMapping[start].reg->isEmitted) &&
610 (!finalMapping[start].instance) &&
611 (!finalMapping[start].isSFR)) {
613 if (finalMapping[start].reg->isFixed) {
615 for (i=0; i<finalMapping[start].reg->size; i++) {
616 fprintf(of,"%s\tEQU\t0x%04x\n",
617 finalMapping[start].reg->name,
618 finalMapping[start].reg->address+i);
621 emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
623 fprintf(of,"%s\tres\t%i\n",
624 finalMapping[start].reg->name,
625 finalMapping[start].reg->size);
628 finalMapping[start].reg->isEmitted = 1;
639 } while(addr <= maxRAMaddress);
645 /*-----------------------------------------------------------------*
646 * void list_valid_pics(int ncols, int list_alias)
648 * Print out a formatted list of valid PIC devices
650 * ncols - number of columns in the list.
652 * list_alias - if non-zero, print all of the supported aliases
653 * for a device (e.g. F84, 16F84, etc...)
654 *-----------------------------------------------------------------*/
655 void list_valid_pics(int ncols)
660 if (num_of_supported_PICS == 0)
661 find_device(NULL); /* load all the definitions */
663 /* decrement the column number if it's greater than zero */
664 ncols = (ncols > 1) ? ncols-1 : 4;
666 /* Find the device with the longest name */
667 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
668 k = strlen(Pics[i]->name);
675 fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
676 fprintf(stderr, " processor");
677 for(k=0; k<longest-1; k++)
679 fprintf(stderr, "program RAM EEPROM I/O\n");
680 fprintf(stderr, "-----------------------------------------------------\n");
682 for(i=0; i < num_of_supported_PICS; i++) {
683 fprintf(stderr," %s", Pics[i]->name);
684 l = longest + 2 - strlen(Pics[i]->name);
688 fprintf(stderr, " ");
689 if (Pics[i]->programMemSize % 1024 == 0)
690 fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
692 fprintf(stderr, "%5d", Pics[i]->programMemSize);
694 fprintf(stderr, " %5d %5d %4d\n",
695 Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
700 fprintf(stderr, "\nPIC14 processors supported:\n");
701 for(i=0; i < num_of_supported_PICS; i++) {
703 fprintf(stderr,"%s", Pics[i]->name);
705 l = longest + 2 - strlen(Pics[i]->name);
722 /*-----------------------------------------------------------------*
724 *-----------------------------------------------------------------*/
725 PIC_device *init_pic(char *pic_type)
727 char long_name[PIC14_STRING_LEN];
729 pic = find_device(pic_type);
732 /* check for shortened "16xxx" form */
733 sprintf(long_name, "16%s", pic_type);
734 pic = find_device(long_name);
736 if(pic_type != NULL && pic_type[0] != '\0')
737 fprintf(stderr, "'%s' was not found.\n", pic_type);
739 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
748 /*-----------------------------------------------------------------*
750 *-----------------------------------------------------------------*/
751 int picIsInitialized(void)
753 if(pic && maxRAMaddress > 0)
760 /*-----------------------------------------------------------------*
761 * char *processor_base_name(void) - Include file is derived from this.
762 *-----------------------------------------------------------------*/
763 char *processor_base_name(void)
772 /*-----------------------------------------------------------------*
773 *-----------------------------------------------------------------*/
774 int validAddress(int address, int reg_size)
778 if (maxRAMaddress < 0) {
779 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
782 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
783 assert (reg_size > 0);
784 if(address + (reg_size - 1) > maxRAMaddress)
787 for (i=0; i<reg_size; i++)
788 if(!finalMapping[address + i].isValid ||
789 finalMapping[address+i].reg ||
790 finalMapping[address+i].isSFR )
796 /*-----------------------------------------------------------------*
797 *-----------------------------------------------------------------*/
798 void mapRegister(regs *reg)
804 if(!reg || !reg->size) {
805 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
809 if (maxRAMaddress < 0) {
810 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
814 for(i=0; i<reg->size; i++) {
816 assert(reg->address >= 0 && reg->address < maxRAMaddress);
818 alias = finalMapping[reg->address].alias;
823 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
825 finalMapping[reg->address + alias + i].reg = reg;
826 finalMapping[reg->address + alias + i].instance = i;
828 /* Decrement alias */
830 alias -= ((alias & (alias - 1)) ^ alias);
837 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
843 /*-----------------------------------------------------------------*
844 *-----------------------------------------------------------------*/
845 int assignRegister(regs *reg, int start_address)
849 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
852 if (validAddress(reg->address,reg->size)) {
853 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
858 if( isSFR(reg->address)) {
863 if (getenv("SDCCPICDEBUG")) {
864 fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
865 reg->address, reg->name);
870 /* This register does not have a fixed address requirement
871 * so we'll search through all availble ram address and
872 * assign the first one */
874 for (i=start_address; i<=maxRAMaddress; i++) {
876 if (validAddress(i,reg->size)) {
883 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
890 /*-----------------------------------------------------------------*
891 *-----------------------------------------------------------------*/
892 void assignFixedRegisters(set *regset)
896 for (reg = setFirstItem(regset) ; reg ;
897 reg = setNextItem(regset)) {
900 assignRegister(reg,0);
905 /*-----------------------------------------------------------------*
906 *-----------------------------------------------------------------*/
907 void assignRelocatableRegisters(set *regset, int used)
913 for (reg = setFirstItem(regset) ; reg ;
914 reg = setNextItem(regset)) {
916 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
918 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
919 /* If register have been reused then shall not print it a second time. */
922 for (s = regset; s; s = s->next) {
927 if((!r->isFixed) && ( used || r->wasUsed)) {
928 if (r->rIdx == reg->rIdx) {
929 reg->address = r->address;
936 address = assignRegister(reg,address);
942 /* Keep track of whether we found an assignment to the __config words. */
943 static int pic14_hasSetConfigWord = 0;
945 /*-----------------------------------------------------------------*
946 * void assignConfigWordValue(int address, int value)
948 * Most midrange PICs have one config word at address 0x2007.
949 * Newer PIC14s have a second config word at address 0x2008.
950 * This routine will assign values to those addresses.
952 *-----------------------------------------------------------------*/
954 void pic14_assignConfigWordValue(int address, int value)
956 if (CONFIG_WORD_ADDRESS == address)
959 else if (CONFIG2_WORD_ADDRESS == address)
960 config2_word = value;
962 //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
963 pic14_hasSetConfigWord = 1;
966 /*-----------------------------------------------------------------*
967 * int pic14_emitConfigWord (FILE * vFile)
969 * Emit the __config directives iff we found a previous assignment
970 * to the config word.
971 *-----------------------------------------------------------------*/
972 extern char *iComments2;
973 int pic14_emitConfigWord (FILE * vFile)
975 if (pic14_hasSetConfigWord)
977 fprintf (vFile, "%s", iComments2);
978 fprintf (vFile, "; config word \n");
979 fprintf (vFile, "%s", iComments2);
980 if (pic14_getHasSecondConfigReg())
982 fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
983 fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
986 fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
993 /*-----------------------------------------------------------------*
994 * int pic14_getConfigWord(int address)
996 * Get the current value of a config word.
998 *-----------------------------------------------------------------*/
1000 int pic14_getConfigWord(int address)
1004 case CONFIG_WORD_ADDRESS:
1007 case CONFIG2_WORD_ADDRESS:
1008 return config2_word;
1015 /*-----------------------------------------------------------------*
1017 *-----------------------------------------------------------------*/
1018 unsigned pic14_getMaxRam(void)
1020 return pic->defMaxRAMaddrs;
1024 /*-----------------------------------------------------------------*
1025 * int getHasSecondConfigReg(void) - check if the device has a
1026 * second config register, rather than just one.
1027 *-----------------------------------------------------------------*/
1028 int pic14_getHasSecondConfigReg(void)
1033 return pic->hasSecondConfigReg;
1036 /*-----------------------------------------------------------------*
1037 * True iff the device has memory aliased in every bank.
1038 * If true, low and high will be set to the low and high address
1039 * occupied by the (last) sharebank found.
1040 *-----------------------------------------------------------------*/
1041 int pic14_hasSharebank(int *low, int *high, int *size)
1049 //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask);
1050 if (r->alias == pic->bankMask) {
1051 if (low) *low = r->start_address;
1052 if (high) *high = r->end_address;
1053 if (size) *size = r->end_address - r->start_address + 1;
1059 if (low) *low = 0x0;
1060 if (high) *high = 0x0;
1061 if (size) *size = 0x0;
1062 //fprintf (stderr, "%s: no shared bank found\n", __FUNCTION__);
1067 * True iff the memory region [low, high] is aliased in all banks.
1069 int pic14_isShared(int low, int high)
1077 //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask);
1078 if ((r->alias == pic->bankMask) && (r->start_address <= low) && (r->end_address >= high)) {
1088 * True iff all RAM is aliased in all banks (no BANKSELs required except for
1091 int pic14_allRAMShared(void)
1099 if (r->alias != pic->bankMask) return 0;
1107 * True iff the pseudo stack is a sharebank --> let linker place it.
1108 * [low, high] denotes a size byte long block of (shared or banked)
1109 * memory to be used.
1111 int pic14_getSharedStack(int *low, int *high, int *size)
1116 haveShared = pic14_hasSharebank(&l, &h, &s);
1117 if ((options.stack_loc != 0) || !haveShared)
1119 // sharebank not available or not to be used
1120 s = options.stack_size ? options.stack_size : 0x10;
1121 l = options.stack_loc ? options.stack_loc : 0x20;
1122 h = (options.stack_loc ? options.stack_loc : 0x20) + s - 1;
1124 if (high) *high = h;
1125 if (size) *size = s;
1126 // return 1 iff [low, high] is present in all banks
1127 //fprintf(stderr, "%s: low %x, high %x, size %x, shared %d\n", __FUNCTION__, l, h, s, pic14_isShared(l, h));
1128 return (pic14_isShared(l, h));
1130 // sharebanks available for use by the stack
1131 if (options.stack_size) s = options.stack_size;
1132 else if (!s || s > 16) s = 16; // limit stack to 16 bytes in SHAREBANK
1134 // provide addresses for sharebank
1136 if (high) *high = l + s - 1;
1137 if (size) *size = s;
1138 //fprintf(stderr, "%s: low %x, high %x, size %x, shared 1\n", __FUNCTION__, l, h, s);
1143 PIC_device * pic14_getPIC(void)