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;
66 extern int pic14_is_shared (regs *reg);
67 extern void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize);
70 /* parse a value from the configuration file */
71 static int parse_config_value(char *str)
73 if (str[strlen(str) - 1] == 'K')
74 return atoi(str) * 1024; /* like "1K" */
76 else if (STRNCASECMP(str, "0x", 2) == 0)
77 return strtol(str+2, NULL, 16); /* like "0x400" */
80 return atoi(str); /* like "1024" */
84 /* split a line into words */
85 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
91 while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
92 /* remove leading spaces */
93 while (isspace(*pos) || *pos == ',')
96 /* copy everything up until the first space or comma */
97 for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
98 result_word[num_words][ccount] = *pos;
99 result_word[num_words][ccount] = '\0';
108 /* remove annoying prefixes from the processor name */
109 static char *sanitise_processor_name(char *name)
111 char *proc_pos = name;
116 if (STRNCASECMP(proc_pos, "pic", 3) == 0)
119 else if (tolower(*proc_pos) == 'p')
126 /* create a structure for a pic processor */
127 static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
130 char *simple_pic_name = sanitise_processor_name(pic_name);
132 new_pic = Safe_calloc(1, sizeof(PIC_device));
133 new_pic->name = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
134 strcpy(new_pic->name, simple_pic_name);
136 new_pic->defMaxRAMaddrs = maxram;
137 new_pic->bankMask = bankmsk;
138 new_pic->hasSecondConfigReg = confsiz > 1;
140 new_pic->programMemSize = program;
141 new_pic->dataMemSize = data;
142 new_pic->eepromMemSize = eeprom;
143 new_pic->ioPins = io;
145 Pics[num_of_supported_PICS] = new_pic;
146 num_of_supported_PICS++;
152 /* mark some registers as being duplicated across banks */
153 static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
159 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
163 r.alias = parse_config_value(word[1]);
165 for (pcount = 2; pcount < num_words; pcount++) {
167 r.start_address = parse_config_value(word[pcount]);
168 r.end_address = parse_config_value(word[pcount]);
169 r.bank = (r.start_address >> 7) & 3;
176 /* define ram areas - may be duplicated across banks */
177 static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
182 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
186 r.start_address = parse_config_value(word[1]);
187 r.end_address = parse_config_value(word[2]);
188 r.alias = parse_config_value(word[3]);
189 r.bank = (r.start_address >> 7) & 3;
194 extern set *includeDirsSet;
195 extern set *userIncDirsSet;
196 extern set *libDirsSet;
197 extern set *libPathsSet;
199 /* read the file with all the pic14 definitions and pick out the definition for a processor
200 * if specified. if pic_name is NULL reads everything */
201 static PIC_device *find_device(char *pic_name)
204 char pic_buf[PIC14_STRING_LEN];
206 int found_processor = FALSE;
208 char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
209 int num_processor_names = 0;
217 char *simple_pic_name;
222 /* allow abbreviations of the form "f877" - convert to "16f877" */
223 simple_pic_name = sanitise_processor_name(pic_name);
224 num_of_supported_PICS = 0;
226 /* open the piclist file */
227 /* first scan all include directories */
229 //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
230 for (dir = setFirstItem(includeDirsSet);
232 dir = setNextItem(includeDirsSet))
234 //fprintf( stderr, "searching1 %s\n", dir );
235 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
236 pic_file = fopen( filename, "rt" );
239 for (dir = setFirstItem(userIncDirsSet);
241 dir = setNextItem(userIncDirsSet))
243 //fprintf( stderr, "searching2 %s\n", dir );
244 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
245 pic_file = fopen( filename, "rt" );
248 for (dir = setFirstItem(libDirsSet);
250 dir = setNextItem(libDirsSet))
252 //fprintf( stderr, "searching3 %s\n", dir );
253 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
254 pic_file = fopen( filename, "rt" );
257 for (dir = setFirstItem(libPathsSet);
259 dir = setNextItem(libPathsSet))
261 //fprintf( stderr, "searching4 %s\n", dir );
262 SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
263 pic_file = fopen( filename, "rt" );
267 pic_file = fopen(DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME, "rt");
269 if (pic_file == NULL) {
270 /* this second attempt is used when initially building the libraries */
271 pic_file = fopen(".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".."
272 DIR_SEPARATOR_STRING "src" DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING
273 DEVICE_FILE_NAME, "rt");
274 if (pic_file == NULL) {
275 fprintf(stderr, "can't find %s\n", DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic"
276 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
281 /* read line by line */
282 pic_buf[sizeof(pic_buf)-1] = '\0';
283 while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
285 /* remove trailing spaces */
286 while (isspace(pic_buf[strlen(pic_buf)-1]))
287 pic_buf[strlen(pic_buf)-1] = '\0';
289 /* remove leading spaces */
290 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
293 /* ignore comment / empty lines */
294 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
296 /* split into fields */
297 char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
301 num_pic_words = split_words(pic_word, pic_buf_pos);
303 if (STRCASECMP(pic_word[0], "processor") == 0) {
305 if (pic_name == NULL) {
306 /* this is the mode where we read all the processors in - store the names for now */
307 if (num_processor_names > 0) {
308 /* store away all the previous processor definitions */
311 for (dcount = 1; dcount < num_processor_names; dcount++)
312 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
313 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
316 num_processor_names = split_words(processor_name, pic_buf_pos);
319 /* if we've just completed reading a processor definition stop now */
323 /* check if this processor name is a match */
324 for (wcount = 1; wcount < num_pic_words; wcount++) {
326 /* skip uninteresting prefixes */
327 char *found_name = sanitise_processor_name(pic_word[wcount]);
329 if (STRCASECMP(found_name, simple_pic_name) == 0)
330 found_processor = TRUE;
337 if (found_processor || pic_name == NULL) {
338 /* only parse a processor section if we've found the one we want */
339 if (STRCASECMP(pic_word[0], "maxram") == 0 && num_pic_words > 1) {
340 pic_maxram = parse_config_value(pic_word[1]);
341 setMaxRAM(pic_maxram);
343 else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
344 pic_bankmsk = parse_config_value(pic_word[1]);
346 else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
347 pic_confsiz = parse_config_value(pic_word[1]);
349 else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
350 pic_program = parse_config_value(pic_word[1]);
352 else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
353 pic_data = parse_config_value(pic_word[1]);
355 else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
356 pic_eeprom = parse_config_value(pic_word[1]);
358 else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
359 pic_io = parse_config_value(pic_word[1]);
361 else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
363 register_map(num_pic_words, pic_word);
365 else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
367 ram_map(num_pic_words, pic_word);
370 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
379 /* if we're in read-the-lot mode then create the final processor definition */
380 if (pic_name == NULL) {
382 if (num_processor_names > 0) {
383 /* store away all the previous processor definitions */
386 for (dcount = 1; dcount < num_processor_names; dcount++)
387 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
388 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
393 if (found_processor) {
394 /* create a new pic entry */
395 return create_pic(pic_name, pic_maxram, pic_bankmsk,
396 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
403 void addMemRange(memRange *r, int type)
406 int alias = r->alias;
408 if (maxRAMaddress < 0) {
409 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
414 for (i=r->start_address; i<= r->end_address; i++) {
415 if ((i|alias) <= maxRAMaddress) {
416 /* if we haven't seen this address before, enter it */
417 if (!finalMapping[i | alias].isValid) {
418 finalMapping[i | alias].isValid = 1;
419 finalMapping[i | alias].alias = r->alias;
420 finalMapping[i | alias].bank = r->bank;
423 finalMapping[i | alias].isSFR = 1;
425 finalMapping[i | alias].isSFR = 0;
429 if (getenv("SDCCPICDEBUG")) {
430 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
431 __FILE__,__FUNCTION__,(i|alias), maxRAMaddress);
436 /* Decrement alias */
438 alias -= ((alias & (alias - 1)) ^ alias);
443 } while (alias >= 0);
446 void setMaxRAM(int size)
449 maxRAMaddress = size;
451 if (maxRAMaddress < 0) {
452 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
453 maxRAMaddress, DEVICE_FILE_NAME);
457 finalMapping = Safe_calloc(1+maxRAMaddress,
458 sizeof(AssignedMemory));
460 /* Now initialize the finalMapping array */
462 for(i=0; i<=maxRAMaddress; i++) {
463 finalMapping[i].reg = NULL;
464 finalMapping[i].isValid = 0;
465 finalMapping[i].bank = (i>>7);
469 /*-----------------------------------------------------------------*
470 *-----------------------------------------------------------------*/
472 int isREGinBank(regs *reg, int bank)
478 if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
484 /*-----------------------------------------------------------------*
485 *-----------------------------------------------------------------*/
486 int REGallBanks(regs *reg)
492 return ((reg->address | reg->alias) & pic->bankMask);
496 /*-----------------------------------------------------------------*
497 *-----------------------------------------------------------------*/
499 int isSFR(int address)
502 if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
510 * dump_map -- debug stuff
517 for(i=0; i<=maxRAMaddress; i++) {
518 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
520 if(finalMapping[i].isValid) {
521 fprintf(stderr,"addr: 0x%02x",i);
522 if(finalMapping[i].isSFR)
523 fprintf(stderr," isSFR");
524 if(finalMapping[i].reg)
525 fprintf( stderr, " reg %s", finalMapping[i].reg->name);
526 fprintf(stderr, "\n");
532 void dump_sfr(FILE *of)
537 static int udata_flag=0;
541 //dump_map(); /* display the register map */
542 //fprintf(stdout,";dump_sfr \n");
543 if (maxRAMaddress < 0) {
544 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
548 for (addr = 0; addr <= maxRAMaddress; addr++)
550 regs *reg = finalMapping[addr].reg;
552 if (reg && !reg->isEmitted)
554 if (pic14_options.isLibrarySource && pic14_is_shared (reg))
556 /* rely on external declarations for the non-fixed stack */
557 /* Update: We always emit the STACK symbols into a
558 * udata_shr section, so no extern declaration is
560 //fprintf (of, "\textern\t%s\n", reg->name);
562 emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, pic14_is_shared (reg));
572 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
579 /* clear the lower 7-bits of the start address of the first
580 * variable declared in this bank. The upper bits for the mid
581 * range pics are the bank select bits.
584 bank_base = start & 0xfff8;
586 /* The bank number printed in the cblock comment tacitly
587 * assumes that the first register in the contiguous group
588 * of registers represents the bank for the whole group */
590 if ( (start != addr) && (!udata_flag) ) {
592 //fprintf(of,"\tudata\n");
595 for( ; start < addr; start++) {
596 if((finalMapping[start].reg) &&
597 (!finalMapping[start].reg->isEmitted) &&
598 (!finalMapping[start].instance) &&
599 (!finalMapping[start].isSFR)) {
601 if (finalMapping[start].reg->isFixed) {
603 for (i=0; i<finalMapping[start].reg->size; i++) {
604 fprintf(of,"%s\tEQU\t0x%04x\n",
605 finalMapping[start].reg->name,
606 finalMapping[start].reg->address+i);
609 emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
611 fprintf(of,"%s\tres\t%i\n",
612 finalMapping[start].reg->name,
613 finalMapping[start].reg->size);
616 finalMapping[start].reg->isEmitted = 1;
627 } while(addr <= maxRAMaddress);
633 /*-----------------------------------------------------------------*
634 * void list_valid_pics(int ncols, int list_alias)
636 * Print out a formatted list of valid PIC devices
638 * ncols - number of columns in the list.
640 * list_alias - if non-zero, print all of the supported aliases
641 * for a device (e.g. F84, 16F84, etc...)
642 *-----------------------------------------------------------------*/
643 void list_valid_pics(int ncols)
648 if (num_of_supported_PICS == 0)
649 find_device(NULL); /* load all the definitions */
651 /* decrement the column number if it's greater than zero */
652 ncols = (ncols > 1) ? ncols-1 : 4;
654 /* Find the device with the longest name */
655 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
656 k = strlen(Pics[i]->name);
663 fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
664 fprintf(stderr, " processor");
665 for(k=0; k<longest-1; k++)
667 fprintf(stderr, "program RAM EEPROM I/O\n");
668 fprintf(stderr, "-----------------------------------------------------\n");
670 for(i=0; i < num_of_supported_PICS; i++) {
671 fprintf(stderr," %s", Pics[i]->name);
672 l = longest + 2 - strlen(Pics[i]->name);
676 fprintf(stderr, " ");
677 if (Pics[i]->programMemSize % 1024 == 0)
678 fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
680 fprintf(stderr, "%5d", Pics[i]->programMemSize);
682 fprintf(stderr, " %5d %5d %4d\n",
683 Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
688 fprintf(stderr, "\nPIC14 processors supported:\n");
689 for(i=0; i < num_of_supported_PICS; i++) {
691 fprintf(stderr,"%s", Pics[i]->name);
693 l = longest + 2 - strlen(Pics[i]->name);
710 /*-----------------------------------------------------------------*
712 *-----------------------------------------------------------------*/
713 void init_pic(char *pic_type)
715 char long_name[PIC14_STRING_LEN];
717 pic = find_device(pic_type);
720 /* check for shortened "16xxx" form */
721 sprintf(long_name, "16%s", pic_type);
722 pic = find_device(long_name);
724 if(pic_type != NULL && pic_type[0] != '\0')
725 fprintf(stderr, "'%s' was not found.\n", pic_type);
727 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
735 /*-----------------------------------------------------------------*
737 *-----------------------------------------------------------------*/
738 int picIsInitialized(void)
740 if(pic && maxRAMaddress > 0)
747 /*-----------------------------------------------------------------*
748 * char *processor_base_name(void) - Include file is derived from this.
749 *-----------------------------------------------------------------*/
750 char *processor_base_name(void)
759 /*-----------------------------------------------------------------*
760 *-----------------------------------------------------------------*/
761 int validAddress(int address, int reg_size)
765 if (maxRAMaddress < 0) {
766 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
769 // fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
770 assert (reg_size > 0);
771 if(address + (reg_size - 1) > maxRAMaddress)
774 for (i=0; i<reg_size; i++)
775 if(!finalMapping[address + i].isValid ||
776 finalMapping[address+i].reg ||
777 finalMapping[address+i].isSFR )
783 /*-----------------------------------------------------------------*
784 *-----------------------------------------------------------------*/
785 void mapRegister(regs *reg)
791 if(!reg || !reg->size) {
792 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
796 if (maxRAMaddress < 0) {
797 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
801 for(i=0; i<reg->size; i++) {
803 assert(reg->address >= 0 && reg->address < maxRAMaddress);
805 alias = finalMapping[reg->address].alias;
810 //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
812 finalMapping[reg->address + alias + i].reg = reg;
813 finalMapping[reg->address + alias + i].instance = i;
815 /* Decrement alias */
817 alias -= ((alias & (alias - 1)) ^ alias);
824 //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
830 /*-----------------------------------------------------------------*
831 *-----------------------------------------------------------------*/
832 int assignRegister(regs *reg, int start_address)
836 //fprintf(stderr,"%s - %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
839 if (validAddress(reg->address,reg->size)) {
840 //fprintf(stderr,"%s - %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
845 if( isSFR(reg->address)) {
850 if (getenv("SDCCPICDEBUG")) {
851 fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
852 reg->address, reg->name);
857 /* This register does not have a fixed address requirement
858 * so we'll search through all availble ram address and
859 * assign the first one */
861 for (i=start_address; i<=maxRAMaddress; i++) {
863 if (validAddress(i,reg->size)) {
870 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
877 /*-----------------------------------------------------------------*
878 *-----------------------------------------------------------------*/
879 void assignFixedRegisters(set *regset)
883 for (reg = setFirstItem(regset) ; reg ;
884 reg = setNextItem(regset)) {
887 assignRegister(reg,0);
892 /*-----------------------------------------------------------------*
893 *-----------------------------------------------------------------*/
894 void assignRelocatableRegisters(set *regset, int used)
900 for (reg = setFirstItem(regset) ; reg ;
901 reg = setNextItem(regset)) {
903 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
905 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
906 /* If register have been reused then shall not print it a second time. */
909 for (s = regset; s; s = s->next) {
914 if((!r->isFixed) && ( used || r->wasUsed)) {
915 if (r->rIdx == reg->rIdx) {
916 reg->address = r->address;
923 address = assignRegister(reg,address);
929 /* Keep track of whether we found an assignment to the __config words. */
930 static int pic14_hasSetConfigWord = 0;
932 /*-----------------------------------------------------------------*
933 * void assignConfigWordValue(int address, int value)
935 * Most midrange PICs have one config word at address 0x2007.
936 * Newer PIC14s have a second config word at address 0x2008.
937 * This routine will assign values to those addresses.
939 *-----------------------------------------------------------------*/
941 void pic14_assignConfigWordValue(int address, int value)
943 if (CONFIG_WORD_ADDRESS == address)
946 else if (CONFIG2_WORD_ADDRESS == address)
947 config2_word = value;
949 //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
950 pic14_hasSetConfigWord = 1;
953 /*-----------------------------------------------------------------*
954 * int pic14_emitConfigWord (FILE * vFile)
956 * Emit the __config directives iff we found a previous assignment
957 * to the config word.
958 *-----------------------------------------------------------------*/
959 extern char *iComments2;
960 int pic14_emitConfigWord (FILE * vFile)
962 if (pic14_hasSetConfigWord)
964 fprintf (vFile, "%s", iComments2);
965 fprintf (vFile, "; config word \n");
966 fprintf (vFile, "%s", iComments2);
967 if (pic14_getHasSecondConfigReg())
969 fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
970 fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
973 fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
980 /*-----------------------------------------------------------------*
981 * int pic14_getConfigWord(int address)
983 * Get the current value of a config word.
985 *-----------------------------------------------------------------*/
987 int pic14_getConfigWord(int address)
991 case CONFIG_WORD_ADDRESS:
994 case CONFIG2_WORD_ADDRESS:
1002 /*-----------------------------------------------------------------*
1004 *-----------------------------------------------------------------*/
1005 unsigned pic14_getMaxRam(void)
1007 return pic->defMaxRAMaddrs;
1011 /*-----------------------------------------------------------------*
1012 * int getHasSecondConfigReg(void) - check if the device has a
1013 * second config register, rather than just one.
1014 *-----------------------------------------------------------------*/
1015 int pic14_getHasSecondConfigReg(void)
1020 return pic->hasSecondConfigReg;
1023 /*-----------------------------------------------------------------*
1024 * Query the size of the sharebank of the selected device.
1025 * FIXME: Currently always returns 16.
1026 *-----------------------------------------------------------------*/
1027 int pic14_getSharebankSize(void)
1029 if (options.stack_size <= 0) {
1030 // default size: 16 bytes
1033 return options.stack_size;
1037 /*-----------------------------------------------------------------*
1038 * Query the highest byte address occupied by the sharebank of the
1040 * THINK: Might not be needed, if we assign all shareable objects to
1041 * a `udata_shr' section and let the linker do the rest...
1042 * Tried it, but yields `no target memory available' for pic16f877...
1043 *-----------------------------------------------------------------*/
1044 int pic14_getSharebankAddress(void)
1046 int sharebankAddress = 0x7f;
1047 if (options.stack_loc != 0) {
1048 // permanent (?) workaround for pic16f84a-like devices with hardly
1051 // 0x0C-0x4F memory,
1052 // 0x50-0x7F unimplemented (reads as 0),
1053 // 0x80-0x8B SFRs (partly mapped to 0x0?)
1054 // 0x8c-0xCF mapped to 0x0C-0x4F
1055 sharebankAddress = options.stack_loc + pic14_getSharebankSize() - 1;
1057 /* If total RAM is less than 0x7f as with 16f84 then reduce
1058 * sharebankAddress to fit */
1059 if ((unsigned)sharebankAddress > pic14_getMaxRam())
1060 sharebankAddress = (int)pic14_getMaxRam();
1062 return sharebankAddress;