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 -------------------------------------------------------------------------*/
26 extern set *includeDirsSet;
27 extern set *userIncDirsSet;
28 extern set *libDirsSet;
29 extern set *libPathsSet;
31 #define MAX_PICLIST 200
32 static PIC_device *Pics[MAX_PICLIST];
33 static PIC_device *pic = NULL;
34 static int num_of_supported_PICS = 0;
35 static int maxRAMaddress = 0;
37 #define CONFIG_WORD_ADDRESS 0x2007
38 #define CONFIG2_WORD_ADDRESS 0x2008
39 #define DEFAULT_CONFIG_WORD 0x3fff
40 #define DEFAULT_CONFIG2_WORD 0x3ffc
42 #define DEVICE_FILE_NAME "pic14devices.txt"
43 #define PIC14_STRING_LEN 256
44 #define SPLIT_WORDS_MAX 16
46 /* Keep track of whether we found an assignment to the __config words. */
47 static int pic14_hasSetConfigWord = 0;
48 static unsigned int config_word = DEFAULT_CONFIG_WORD;
49 static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
50 static memRange *rangeRAM = NULL;
53 /* parse a value from the configuration file */
54 static int parse_config_value(char *str)
56 if (str[strlen(str) - 1] == 'K')
57 return atoi(str) * 1024; /* like "1K" */
59 else if (STRNCASECMP(str, "0x", 2) == 0)
60 return strtol(str+2, NULL, 16); /* like "0x400" */
63 return atoi(str); /* like "1024" */
67 /* split a line into words */
68 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
74 while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
75 /* remove leading spaces */
76 while (isspace(*pos) || *pos == ',')
79 /* copy everything up until the first space or comma */
80 for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
81 result_word[num_words][ccount] = *pos;
82 result_word[num_words][ccount] = '\0';
91 /* remove annoying prefixes from the processor name */
92 static char *sanitise_processor_name(char *name)
94 char *proc_pos = name;
99 if (STRNCASECMP(proc_pos, "pic", 3) == 0)
102 else if (tolower(*proc_pos) == 'p')
109 /* create a structure for a pic processor */
110 static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
113 char *simple_pic_name = sanitise_processor_name(pic_name);
115 new_pic = Safe_calloc(1, sizeof(PIC_device));
116 new_pic->name = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
117 strcpy(new_pic->name, simple_pic_name);
119 new_pic->defMaxRAMaddrs = maxram;
120 new_pic->bankMask = bankmsk;
121 new_pic->hasSecondConfigReg = confsiz > 1;
123 new_pic->programMemSize = program;
124 new_pic->dataMemSize = data;
125 new_pic->eepromMemSize = eeprom;
126 new_pic->ioPins = io;
128 new_pic->ram = rangeRAM;
130 Pics[num_of_supported_PICS] = new_pic;
131 num_of_supported_PICS++;
137 /* mark some registers as being duplicated across banks */
138 static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
144 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
148 for (pcount = 2; pcount < num_words; pcount++) {
149 r = Safe_calloc(1, sizeof(memRange));
151 r->start_address = parse_config_value(word[pcount]);
152 r->end_address = parse_config_value(word[pcount]);
153 r->alias = parse_config_value(word[1]);
154 r->bank = (r->start_address >> 7) & 3;
155 // add memRange to device entry for future lookup (sharebanks)
162 /* define ram areas - may be duplicated across banks */
163 static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
168 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
172 r = Safe_calloc(1, sizeof(memRange));
173 //fprintf (stderr, "%s: %s %s %s\n", __FUNCTION__, word[1], word[2], word[3]);
175 r->start_address = parse_config_value(word[1]);
176 r->end_address = parse_config_value(word[2]);
177 r->alias = parse_config_value(word[3]);
178 r->bank = (r->start_address >> 7) & 3;
180 // add memRange to device entry for future lookup (sharebanks)
185 static void setMaxRAM(int size)
187 maxRAMaddress = size;
189 if (maxRAMaddress < 0) {
190 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
191 maxRAMaddress, DEVICE_FILE_NAME);
196 /* read the file with all the pic14 definitions and pick out the definition
197 * for a processor if specified. if pic_name is NULL reads everything */
198 static PIC_device *find_device(char *pic_name)
201 char pic_buf[PIC14_STRING_LEN];
203 int found_processor = FALSE;
205 char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
206 int num_processor_names = 0;
214 char *simple_pic_name;
219 /* allow abbreviations of the form "f877" - convert to "16f877" */
220 simple_pic_name = sanitise_processor_name(pic_name);
221 num_of_supported_PICS = 0;
223 /* open the piclist file */
224 /* first scan all include directories */
226 //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
227 for (dir = setFirstItem(userIncDirsSet);
229 dir = setNextItem(userIncDirsSet))
231 //fprintf( stderr, "searching1 %s\n", dir );
232 SNPRINTF(&filename[0], len, "%s%s", dir,
233 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
234 pic_file = fopen( filename, "rt" );
238 for (dir = setFirstItem(includeDirsSet);
240 dir = setNextItem(includeDirsSet))
242 //fprintf( stderr, "searching2 %s\n", dir );
243 SNPRINTF(&filename[0], len, "%s%s", dir,
244 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
245 pic_file = fopen( filename, "rt" );
249 for (dir = setFirstItem(libDirsSet);
251 dir = setNextItem(libDirsSet))
253 //fprintf( stderr, "searching3 %s\n", dir );
254 SNPRINTF(&filename[0], len, "%s%s", dir,
255 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
256 pic_file = fopen( filename, "rt" );
260 for (dir = setFirstItem(libPathsSet);
262 dir = setNextItem(libPathsSet))
264 //fprintf( stderr, "searching4 %s\n", dir );
265 SNPRINTF(&filename[0], len, "%s%s", dir,
266 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
267 pic_file = fopen( filename, "rt" );
272 SNPRINTF(&filename[0], len, "%s",
273 DATADIR LIB_DIR_SUFFIX
274 DIR_SEPARATOR_STRING "pic"
275 DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
276 pic_file = fopen( filename, "rt" );
279 if (pic_file == NULL) {
280 fprintf(stderr, "can't find %s\n", DEVICE_FILE_NAME);
284 if (options.verbose) {
285 printf ("Using devices from %s.\n", filename);
288 /* read line by line */
289 pic_buf[sizeof(pic_buf)-1] = '\0';
290 while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
292 llen = strlen (pic_buf);
294 /* remove trailing spaces */
295 while (llen && isspace(pic_buf[llen-1])) {
296 pic_buf[llen-1] = '\0';
300 /* remove leading spaces */
301 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
304 /* ignore comment / empty lines */
305 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
307 /* split into fields */
308 char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
312 num_pic_words = split_words(pic_word, pic_buf_pos);
314 if (STRCASECMP(pic_word[0], "processor") == 0) {
316 if (pic_name == NULL) {
317 /* this is the mode where we read all the processors in - store the names for now */
318 if (num_processor_names > 0) {
319 /* store away all the previous processor definitions */
322 for (dcount = 1; dcount < num_processor_names; dcount++)
323 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
324 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
327 num_processor_names = split_words(processor_name, pic_buf_pos);
330 /* if we've just completed reading a processor definition stop now */
334 /* check if this processor name is a match */
335 for (wcount = 1; wcount < num_pic_words; wcount++) {
337 /* skip uninteresting prefixes */
338 char *found_name = sanitise_processor_name(pic_word[wcount]);
340 if (STRCASECMP(found_name, simple_pic_name) == 0)
341 found_processor = TRUE;
348 if (found_processor || pic_name == NULL) {
349 /* only parse a processor section if we've found the one we want */
350 if (STRCASECMP(pic_word[0], "maxram") == 0 && num_pic_words > 1) {
351 pic_maxram = parse_config_value(pic_word[1]);
352 setMaxRAM(pic_maxram);
354 else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
355 pic_bankmsk = parse_config_value(pic_word[1]);
357 else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
358 pic_confsiz = parse_config_value(pic_word[1]);
360 else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
361 pic_program = parse_config_value(pic_word[1]);
363 else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
364 pic_data = parse_config_value(pic_word[1]);
366 else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
367 pic_eeprom = parse_config_value(pic_word[1]);
369 else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
370 pic_io = parse_config_value(pic_word[1]);
372 else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
374 register_map(num_pic_words, pic_word);
376 else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
378 ram_map(num_pic_words, pic_word);
381 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
390 /* if we're in read-the-lot mode then create the final processor definition */
391 if (pic_name == NULL) {
393 if (num_processor_names > 0) {
394 /* store away all the previous processor definitions */
397 for (dcount = 1; dcount < num_processor_names; dcount++)
398 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
399 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
404 if (found_processor) {
405 /* create a new pic entry */
406 return create_pic(pic_name, pic_maxram, pic_bankmsk,
407 pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
414 /*-----------------------------------------------------------------*
415 * void list_valid_pics(int ncols, int list_alias)
417 * Print out a formatted list of valid PIC devices
419 * ncols - number of columns in the list.
421 * list_alias - if non-zero, print all of the supported aliases
422 * for a device (e.g. F84, 16F84, etc...)
423 *-----------------------------------------------------------------*/
424 static void list_valid_pics(int ncols)
429 if (num_of_supported_PICS == 0)
430 find_device(NULL); /* load all the definitions */
432 /* decrement the column number if it's greater than zero */
433 ncols = (ncols > 1) ? ncols-1 : 4;
435 /* Find the device with the longest name */
436 for(i=0,longest=0; i<num_of_supported_PICS; i++) {
437 k = strlen(Pics[i]->name);
444 fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
445 fprintf(stderr, " processor");
446 for(k=0; k<longest-1; k++)
448 fprintf(stderr, "program RAM EEPROM I/O\n");
449 fprintf(stderr, "-----------------------------------------------------\n");
451 for(i=0; i < num_of_supported_PICS; i++) {
452 fprintf(stderr," %s", Pics[i]->name);
453 l = longest + 2 - strlen(Pics[i]->name);
457 fprintf(stderr, " ");
458 if (Pics[i]->programMemSize % 1024 == 0)
459 fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
461 fprintf(stderr, "%5d", Pics[i]->programMemSize);
463 fprintf(stderr, " %5d %5d %4d\n",
464 Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
469 fprintf(stderr, "\nPIC14 processors supported:\n");
470 for(i=0; i < num_of_supported_PICS; i++) {
472 fprintf(stderr,"%s", Pics[i]->name);
474 l = longest + 2 - strlen(Pics[i]->name);
491 /*-----------------------------------------------------------------*
493 *-----------------------------------------------------------------*/
494 PIC_device *init_pic(char *pic_type)
496 char long_name[PIC14_STRING_LEN];
498 pic = find_device(pic_type);
501 /* check for shortened "16xxx" form */
502 sprintf(long_name, "16%s", pic_type);
503 pic = find_device(long_name);
505 if(pic_type != NULL && pic_type[0] != '\0')
506 fprintf(stderr, "'%s' was not found.\n", pic_type);
508 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
517 /*-----------------------------------------------------------------*
519 *-----------------------------------------------------------------*/
520 int picIsInitialized(void)
522 if(pic && maxRAMaddress > 0)
529 /*-----------------------------------------------------------------*
530 * char *processor_base_name(void) - Include file is derived from this.
531 *-----------------------------------------------------------------*/
532 char *processor_base_name(void)
541 int IS_CONFIG_ADDRESS(int address)
544 return ((address == CONFIG_WORD_ADDRESS)
545 || (address == CONFIG2_WORD_ADDRESS));
548 /*-----------------------------------------------------------------*
549 * void pic14_assignConfigWordValue(int address, int value)
551 * Most midrange PICs have one config word at address 0x2007.
552 * Newer PIC14s have a second config word at address 0x2008.
553 * This routine will assign values to those addresses.
555 *-----------------------------------------------------------------*/
557 void pic14_assignConfigWordValue(int address, int value)
559 if (CONFIG_WORD_ADDRESS == address)
562 else if (CONFIG2_WORD_ADDRESS == address)
563 config2_word = value;
565 //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
566 pic14_hasSetConfigWord = 1;
569 /*-----------------------------------------------------------------*
570 * int pic14_getConfigWord(int address)
572 * Get the current value of a config word.
574 *-----------------------------------------------------------------*/
576 static int pic14_getConfigWord(int address)
580 case CONFIG_WORD_ADDRESS:
583 case CONFIG2_WORD_ADDRESS:
591 /*-----------------------------------------------------------------*
592 * int getHasSecondConfigReg(void) - check if the device has a
593 * second config register, rather than just one.
594 *-----------------------------------------------------------------*/
595 static int pic14_getHasSecondConfigReg(void)
600 return pic->hasSecondConfigReg;
603 /*-----------------------------------------------------------------*
604 * int pic14_emitConfigWord (FILE * vFile)
606 * Emit the __config directives iff we found a previous assignment
607 * to the config word.
608 *-----------------------------------------------------------------*/
609 int pic14_emitConfigWord (FILE * vFile)
611 if (pic14_hasSetConfigWord)
613 fprintf (vFile, "%s", iComments2);
614 fprintf (vFile, "; config word \n");
615 fprintf (vFile, "%s", iComments2);
616 if (pic14_getHasSecondConfigReg())
618 fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
619 fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
622 fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
629 /*-----------------------------------------------------------------*
630 * True iff the device has memory aliased in every bank.
631 * If true, low and high will be set to the low and high address
632 * occupied by the (last) sharebank found.
633 *-----------------------------------------------------------------*/
634 static int pic14_hasSharebank(int *low, int *high, int *size)
642 //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x, min_size %d\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask, size ? *size : 0);
643 // find sufficiently large shared region
644 if ((r->alias == pic->bankMask)
645 && (r->end_address != r->start_address) // ignore SFRs
646 && (!size || (*size <= (r->end_address - r->start_address + 1))))
648 if (low) *low = r->start_address;
649 if (high) *high = r->end_address;
650 if (size) *size = r->end_address - r->start_address + 1;
657 if (high) *high = 0x0;
658 if (size) *size = 0x0;
659 //fprintf (stderr, "%s: no shared bank found\n", __FUNCTION__);
664 * True iff the memory region [low, high] is aliased in all banks.
666 static int pic14_isShared(int low, int high)
674 //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);
675 if ((r->alias == pic->bankMask) && (r->start_address <= low) && (r->end_address >= high)) {
685 * True iff all RAM is aliased in all banks (no BANKSELs required except for
688 int pic14_allRAMShared(void)
696 if (r->alias != pic->bankMask) return 0;
704 * True iff the pseudo stack is a sharebank --> let linker place it.
705 * [low, high] denotes a size byte long block of (shared or banked)
708 int pic14_getSharedStack(int *low, int *high, int *size)
713 s = options.stack_size ? options.stack_size : 0x10;
714 haveShared = pic14_hasSharebank(&l, &h, &s);
715 if ((options.stack_loc != 0) || !haveShared)
717 // sharebank not available or not to be used
718 s = options.stack_size ? options.stack_size : 0x10;
719 l = options.stack_loc ? options.stack_loc : 0x20;
724 // return 1 iff [low, high] is present in all banks
725 //fprintf(stderr, "%s: low %x, high %x, size %x, shared %d\n", __FUNCTION__, l, h, s, pic14_isShared(l, h));
726 return (pic14_isShared(l, h));
728 // sharebanks available for use by the stack
729 if (options.stack_size) s = options.stack_size;
730 else if (!s || s > 16) s = 16; // limit stack to 16 bytes in SHAREBANK
732 // provide addresses for sharebank
734 if (high) *high = l + s - 1;
736 //fprintf(stderr, "%s: low %x, high %x, size %x, shared 1\n", __FUNCTION__, l, h, s);
741 PIC_device * pic14_getPIC(void)