* src/SDCCloop.c (loopInvariants): applied fix for bug 1717943, thanks
[fw/sdcc] / src / pic / device.c
1 /*-------------------------------------------------------------------------
2
3    device.c - Accomodates subtle variations in PIC devices
4    Written By -  Scott Dattalo scott@dattalo.com
5
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
9    later version.
10    
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.
15    
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 -------------------------------------------------------------------------*/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24
25 #include "common.h"   // Include everything in the SDCC src directory
26 #include "newalloc.h"
27
28
29 #include "main.h"
30 #include "pcode.h"
31 #include "ralloc.h"
32 #include "device.h"
33
34 extern int Gstack_base_addr;
35 extern int Gstack_size;
36
37 #define MAX_PICLIST 200
38 static PIC_device *Pics[MAX_PICLIST];
39 static int num_of_supported_PICS = 0;
40
41 static PIC_device *pic=NULL;
42
43 int maxRAMaddress = 0;
44
45 #define CONFIG_WORD_ADDRESS 0x2007
46 #define CONFIG2_WORD_ADDRESS 0x2008
47 #define DEFAULT_CONFIG_WORD 0x3fff
48 #define DEFAULT_CONFIG2_WORD 0x3ffc
49
50 #define DEVICE_FILE_NAME "pic14devices.txt"
51 #define PIC14_STRING_LEN 256
52 #define SPLIT_WORDS_MAX 16
53
54 static unsigned int config_word = DEFAULT_CONFIG_WORD;
55 static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
56 static memRange *rangeRAM = NULL;
57
58
59 /* parse a value from the configuration file */
60 static int parse_config_value(char *str)
61 {
62         if (str[strlen(str) - 1] == 'K')
63                 return atoi(str) * 1024;        /* like "1K" */
64                 
65         else if (STRNCASECMP(str, "0x", 2) == 0)
66                 return strtol(str+2, NULL, 16); /* like "0x400" */
67         
68         else
69                 return atoi(str);               /* like "1024" */
70 }
71
72
73 /* split a line into words */
74 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
75 {
76         char *pos = str;
77         int num_words = 0;
78         int ccount;
79         
80         while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
81                 /* remove leading spaces */
82                 while (isspace(*pos) || *pos == ',')
83                         pos++;
84         
85                 /* copy everything up until the first space or comma */
86                 for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
87                         result_word[num_words][ccount] = *pos;
88                 result_word[num_words][ccount] = '\0';
89                 
90                 num_words++;
91         }
92         
93         return num_words;
94 }
95
96
97 /* remove annoying prefixes from the processor name */
98 static char *sanitise_processor_name(char *name)
99 {
100         char *proc_pos = name;
101
102         if (name == NULL)
103                 return NULL;
104         
105         if (STRNCASECMP(proc_pos, "pic", 3) == 0)
106                 proc_pos += 3;
107
108         else if (tolower(*proc_pos) == 'p')
109                 proc_pos += 1;
110                                 
111         return proc_pos;
112 }
113
114
115 /* create a structure for a pic processor */
116 static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
117 {
118         PIC_device *new_pic;
119         char *simple_pic_name = sanitise_processor_name(pic_name);
120         
121         new_pic = Safe_calloc(1, sizeof(PIC_device));
122         new_pic->name = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
123         strcpy(new_pic->name, simple_pic_name);
124                         
125         new_pic->defMaxRAMaddrs = maxram;
126         new_pic->bankMask = bankmsk;
127         new_pic->hasSecondConfigReg = confsiz > 1;
128         
129         new_pic->programMemSize = program;
130         new_pic->dataMemSize = data;
131         new_pic->eepromMemSize = eeprom;
132         new_pic->ioPins = io;
133
134         new_pic->ram = rangeRAM;
135         
136         Pics[num_of_supported_PICS] = new_pic;
137         num_of_supported_PICS++;
138                         
139         return new_pic;
140 }
141
142
143 /* mark some registers as being duplicated across banks */
144 static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
145 {
146         memRange *r;
147         int pcount;
148         
149         if (num_words < 3) {
150                 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
151                 return;
152         }
153
154         for (pcount = 2; pcount < num_words; pcount++) {
155                 r = Safe_calloc(1, sizeof(memRange));
156                 
157                 r->start_address = parse_config_value(word[pcount]);
158                 r->end_address = parse_config_value(word[pcount]);
159                 r->alias = parse_config_value(word[1]);
160                 r->bank = (r->start_address >> 7) & 3;
161                 // add memRange to device entry for future lookup (sharebanks)
162                 r->next = rangeRAM;
163                 rangeRAM = r;
164         }
165 }
166
167
168 /* define ram areas - may be duplicated across banks */
169 static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
170 {
171         memRange *r;
172         
173         if (num_words < 4) {
174                 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
175                 return;
176         }
177
178         r = Safe_calloc(1, sizeof(memRange));
179         //fprintf (stderr, "%s: %s %s %s\n", __FUNCTION__, word[1], word[2], word[3]);
180         
181         r->start_address = parse_config_value(word[1]);
182         r->end_address = parse_config_value(word[2]);
183         r->alias = parse_config_value(word[3]);
184         r->bank = (r->start_address >> 7) & 3;
185                 
186         // add memRange to device entry for future lookup (sharebanks)
187         r->next = rangeRAM;
188         rangeRAM = r;
189 }
190
191 extern set *includeDirsSet;
192 extern set *userIncDirsSet;
193 extern set *libDirsSet;
194 extern set *libPathsSet;
195
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)
199 {
200         FILE *pic_file;
201         char pic_buf[PIC14_STRING_LEN];
202         char *pic_buf_pos;
203         int found_processor = FALSE;
204         int done = FALSE;
205         char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
206         int num_processor_names = 0;
207         int pic_maxram = 0;
208         int pic_bankmsk = 0;
209         int pic_confsiz = 0;
210         int pic_program = 0;
211         int pic_data = 0;
212         int pic_eeprom = 0;
213         int pic_io = 0;
214         char *simple_pic_name;
215         char *dir;
216         char filename[512];
217         int len = 512;
218         
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;
222         
223         /* open the piclist file */
224         /* first scan all include directories */
225         pic_file = NULL;
226         //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
227         for (dir = setFirstItem(userIncDirsSet);
228                 !pic_file && dir;
229                 dir = setNextItem(userIncDirsSet))
230         {
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" );
235           if (pic_file) break;
236         } // for
237
238         for (dir = setFirstItem(includeDirsSet);
239                 !pic_file && dir;
240                 dir = setNextItem(includeDirsSet))
241         {
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" );
246           if (pic_file) break;
247         } // for
248
249         for (dir = setFirstItem(libDirsSet);
250                 !pic_file && dir;
251                 dir = setNextItem(libDirsSet))
252         {
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" );
257           if (pic_file) break;
258         } // for
259
260         for (dir = setFirstItem(libPathsSet);
261                 !pic_file && dir;
262                 dir = setNextItem(libPathsSet))
263         {
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" );
268           if (pic_file) break;
269         } // for
270
271         if (!pic_file) {
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" );
277         } // if
278
279         if (pic_file == NULL) {
280             fprintf(stderr, "can't find %s\n", DEVICE_FILE_NAME);
281             return NULL;
282         } // if
283
284         if (options.verbose) {
285             printf ("Using devices from %s.\n", filename);
286         } // if
287         
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) {
291                 unsigned llen;
292                 llen = strlen (pic_buf);
293                 
294                 /* remove trailing spaces */
295                 while (llen && isspace(pic_buf[llen-1])) {
296                         pic_buf[llen-1] = '\0';
297                         llen--;
298                 }
299                 
300                 /* remove leading spaces */
301                 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
302                 {}
303                 
304                 /* ignore comment / empty lines */
305                 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
306                         
307                         /* split into fields */
308                         char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
309                         int num_pic_words;
310                         int wcount;
311                         
312                         num_pic_words = split_words(pic_word, pic_buf_pos);
313                         
314                         if (STRCASECMP(pic_word[0], "processor") == 0) {
315                         
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 */
320                                                 int dcount;
321                                                 
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);
325                                         }
326                                         
327                                         num_processor_names = split_words(processor_name, pic_buf_pos);
328                                 }
329                                 else {
330                                         /* if we've just completed reading a processor definition stop now */
331                                         if (found_processor)
332                                                 done = TRUE;
333                                         else {
334                                                 /* check if this processor name is a match */
335                                                 for (wcount = 1; wcount < num_pic_words; wcount++) {
336                                                         
337                                                         /* skip uninteresting prefixes */
338                                                         char *found_name = sanitise_processor_name(pic_word[wcount]);
339
340                                                         if (STRCASECMP(found_name, simple_pic_name) == 0)
341                                                                 found_processor = TRUE;
342                                                 }
343                                         }
344                                 }
345                         }
346                         
347                         else {
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);
353                                         }
354                                         else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
355                                                 pic_bankmsk = parse_config_value(pic_word[1]);
356                         
357                                         else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
358                                                 pic_confsiz = parse_config_value(pic_word[1]);
359                         
360                                         else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
361                                                 pic_program = parse_config_value(pic_word[1]);
362                         
363                                         else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
364                                                 pic_data = parse_config_value(pic_word[1]);
365                         
366                                         else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
367                                                 pic_eeprom = parse_config_value(pic_word[1]);
368                         
369                                         else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
370                                                 pic_io = parse_config_value(pic_word[1]);
371                         
372                                         else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
373                                                 if (found_processor)
374                                                         register_map(num_pic_words, pic_word);
375                                         }
376                                         else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
377                                                 if (found_processor)
378                                                         ram_map(num_pic_words, pic_word);
379                                         }
380                                         else {
381                                                 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
382                                         }
383                                 }
384                         }
385                 }
386         }
387         
388         fclose(pic_file);
389
390         /* if we're in read-the-lot mode then create the final processor definition */
391         if (pic_name == NULL) {
392             
393                 if (num_processor_names > 0) {
394                         /* store away all the previous processor definitions */
395                         int dcount;
396                 
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);
400                 }
401         }
402         else {
403                 /* in search mode */
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);
408                 }
409         }
410         
411         return NULL;
412 }
413
414 void setMaxRAM(int size)
415 {
416         maxRAMaddress = size;
417         
418         if (maxRAMaddress < 0) {
419                 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
420                         maxRAMaddress, DEVICE_FILE_NAME);
421                 return;
422         }
423 }
424
425 /*-----------------------------------------------------------------*
426 *-----------------------------------------------------------------*/
427
428 int isREGinBank(regs *reg, int bank)
429 {
430         
431         if(!reg || !pic)
432                 return 0;
433         
434         if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
435                 return 1;
436         
437         return 0;
438 }
439
440 /*-----------------------------------------------------------------*
441 *-----------------------------------------------------------------*/
442 int REGallBanks(regs *reg)
443 {
444         
445         if(!reg || !pic)
446                 return 0;
447         
448         return ((reg->address | reg->alias) & pic->bankMask);
449         
450 }
451
452 /*-----------------------------------------------------------------*
453  *  void list_valid_pics(int ncols, int list_alias)
454  *
455  * Print out a formatted list of valid PIC devices
456  *
457  * ncols - number of columns in the list.
458  *
459  * list_alias - if non-zero, print all of the supported aliases
460  *              for a device (e.g. F84, 16F84, etc...)
461  *-----------------------------------------------------------------*/
462 void list_valid_pics(int ncols)
463 {
464         int col=0,longest;
465         int i,k,l;
466         
467         if (num_of_supported_PICS == 0)
468                 find_device(NULL);          /* load all the definitions */
469         
470         /* decrement the column number if it's greater than zero */
471         ncols = (ncols > 1) ? ncols-1 : 4;
472         
473         /* Find the device with the longest name */
474         for(i=0,longest=0; i<num_of_supported_PICS; i++) {
475                 k = strlen(Pics[i]->name);
476                 if(k>longest)
477                         longest = k;
478         }
479         
480 #if 1
481         /* heading */
482         fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
483         fprintf(stderr, " processor");
484         for(k=0; k<longest-1; k++)
485                 fputc(' ',stderr);
486         fprintf(stderr, "program     RAM      EEPROM    I/O\n");
487         fprintf(stderr, "-----------------------------------------------------\n");
488         
489         for(i=0;  i < num_of_supported_PICS; i++) {
490                 fprintf(stderr,"  %s", Pics[i]->name);
491                 l = longest + 2 - strlen(Pics[i]->name);
492                 for(k=0; k<l; k++)
493                         fputc(' ',stderr);
494         
495                 fprintf(stderr, "     ");
496                 if (Pics[i]->programMemSize % 1024 == 0)
497                         fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
498                 else
499                         fprintf(stderr, "%5d", Pics[i]->programMemSize);
500         
501                 fprintf(stderr, "     %5d     %5d     %4d\n", 
502                         Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
503         }
504
505         col = 0;
506         
507         fprintf(stderr, "\nPIC14 processors supported:\n");
508         for(i=0;  i < num_of_supported_PICS; i++) {
509                         
510                 fprintf(stderr,"%s", Pics[i]->name);
511                 if(col<ncols) {
512                         l = longest + 2 - strlen(Pics[i]->name);
513                         for(k=0; k<l; k++)
514                                 fputc(' ',stderr);
515                                 
516                         col++;
517                                 
518                 } else {
519                         fputc('\n',stderr);
520                         col = 0;
521                 }
522                 
523         }
524 #endif
525         if(col != 0)
526                 fputc('\n',stderr);
527 }
528
529 /*-----------------------------------------------------------------*
530 *  
531 *-----------------------------------------------------------------*/
532 PIC_device *init_pic(char *pic_type)
533 {
534         char long_name[PIC14_STRING_LEN];
535         
536         pic = find_device(pic_type);
537         
538         if (pic == NULL) {
539                 /* check for shortened "16xxx" form */
540                 sprintf(long_name, "16%s", pic_type);
541                 pic = find_device(long_name);
542                 if (pic == NULL) {
543                         if(pic_type != NULL && pic_type[0] != '\0')
544                                 fprintf(stderr, "'%s' was not found.\n", pic_type);
545                         else
546                                 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
547                 
548                         list_valid_pics(7);
549                         exit(1);
550                 }
551         }
552         return pic;
553 }
554
555 /*-----------------------------------------------------------------*
556 *  
557 *-----------------------------------------------------------------*/
558 int picIsInitialized(void)
559 {
560         if(pic && maxRAMaddress > 0)
561                 return 1;
562         
563         return 0;
564         
565 }
566
567 /*-----------------------------------------------------------------*
568 *  char *processor_base_name(void) - Include file is derived from this.
569 *-----------------------------------------------------------------*/
570 char *processor_base_name(void)
571 {
572         
573         if(!pic)
574                 return NULL;
575         
576         return pic->name;
577 }
578
579 /*-----------------------------------------------------------------*
580 *-----------------------------------------------------------------*/
581 int validAddress(int address, int reg_size)
582 {
583         if (maxRAMaddress < 0) {
584                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
585                 return 0;
586         }
587         //  fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
588         assert (reg_size > 0);
589         if(address + (reg_size - 1) > maxRAMaddress)
590                 return 0;
591         
592         return 1;
593 }
594
595 #if 0
596 /* The following code should be (and is) implemented in the linker. */
597
598 /*-----------------------------------------------------------------*
599 *-----------------------------------------------------------------*/
600 void mapRegister(regs *reg)
601 {
602         
603         unsigned i;
604         int alias;
605         
606         if(!reg || !reg->size) {
607                 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
608                 return;
609         }
610         
611         if (maxRAMaddress < 0) {
612                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
613                 return;
614         }
615         
616         for(i=0; i<reg->size; i++) {
617                 
618                 assert(reg->address >= 0 && reg->address < maxRAMaddress);
619                 
620                 alias = finalMapping[reg->address].alias;
621                 reg->alias = alias;
622                 
623                 do {
624                         
625                         //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
626                         
627                         finalMapping[reg->address + alias + i].reg = reg;
628                         finalMapping[reg->address + alias + i].instance = i;
629                         
630                         /* Decrement alias */
631                         if(alias)
632                                 alias -= ((alias & (alias - 1)) ^ alias);
633                         else
634                                 alias--;
635                         
636                 } while (alias>=0);
637         }
638         
639         //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
640         
641         reg->isMapped = 1;
642         
643 }
644
645 /*-----------------------------------------------------------------*
646 *-----------------------------------------------------------------*/
647 int assignRegister(regs *reg, int start_address)
648 {
649         int i;
650         
651         //fprintf(stderr,"%s -  %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
652         if(reg->isFixed) {
653                 
654                 if (validAddress(reg->address,reg->size)) {
655                         //fprintf(stderr,"%s -  %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
656                         mapRegister(reg);
657                         return reg->address;
658                 }
659                 
660                 if (getenv("SDCCPICDEBUG")) {
661                         fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
662                             reg->address, reg->name);
663                 }
664                 
665         } else {
666                 
667         /* This register does not have a fixed address requirement
668         * so we'll search through all availble ram address and
669                 * assign the first one */
670                 
671                 for (i=start_address; i<=maxRAMaddress; i++) {
672                         
673                         if (validAddress(i,reg->size)) {
674                                 reg->address = i;
675                                 mapRegister(reg);
676                                 return i;
677                         }
678                 }
679                 
680                 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
681                 
682         }
683         
684         return -1;
685 }
686
687 /*-----------------------------------------------------------------*
688 *-----------------------------------------------------------------*/
689 void assignFixedRegisters(set *regset)
690 {
691         regs *reg;
692         
693         for (reg = setFirstItem(regset) ; reg ; 
694         reg = setNextItem(regset)) {
695                 
696                 if(reg->isFixed) 
697                         assignRegister(reg,0);
698         }
699         
700 }
701
702 /*-----------------------------------------------------------------*
703 *-----------------------------------------------------------------*/
704 void assignRelocatableRegisters(set *regset, int used)
705 {
706         
707         regs *reg;
708         int address = 0;
709         
710         for (reg = setFirstItem(regset) ; reg ; 
711         reg = setNextItem(regset)) {
712                 
713                 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
714                 
715                 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
716                         /* If register have been reused then shall not print it a second time. */
717                         set *s;
718                         int done = 0;
719                         for (s = regset; s; s = s->next) {
720                                 regs *r;
721                                 r = s->item;
722                                 if (r == reg)
723                                         break;
724                                 if((!r->isFixed) && ( used || r->wasUsed)) {
725                                         if (r->rIdx == reg->rIdx) {
726                                                 reg->address = r->address;
727                                                 done = 1;
728                                                 break;
729                                         }
730                                 }
731                         }
732                         if (!done)
733                                 address = assignRegister(reg,address);
734                 }
735         }
736         
737 }
738 #endif
739
740 /* Keep track of whether we found an assignment to the __config words. */
741 static int pic14_hasSetConfigWord = 0;
742
743 /*-----------------------------------------------------------------*
744  *  void assignConfigWordValue(int address, int value)
745  *
746  * Most midrange PICs have one config word at address 0x2007.
747  * Newer PIC14s have a second config word at address 0x2008.
748  * This routine will assign values to those addresses.
749  *
750  *-----------------------------------------------------------------*/
751
752 void pic14_assignConfigWordValue(int address, int value)
753 {
754         if (CONFIG_WORD_ADDRESS == address)
755                 config_word = value;
756         
757         else if (CONFIG2_WORD_ADDRESS == address)
758                 config2_word = value;
759         
760         //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
761         pic14_hasSetConfigWord = 1;
762 }
763
764 /*-----------------------------------------------------------------*
765  * int pic14_emitConfigWord (FILE * vFile)
766  * 
767  * Emit the __config directives iff we found a previous assignment
768  * to the config word.
769  *-----------------------------------------------------------------*/
770 extern char *iComments2;
771 int pic14_emitConfigWord (FILE * vFile)
772 {
773   if (pic14_hasSetConfigWord)
774   {
775     fprintf (vFile, "%s", iComments2);
776     fprintf (vFile, "; config word \n");
777     fprintf (vFile, "%s", iComments2);
778     if (pic14_getHasSecondConfigReg())
779     {
780       fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
781       fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
782     }
783     else
784       fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
785
786     return 1;
787   }
788   return 0;
789 }
790
791 /*-----------------------------------------------------------------*
792  * int pic14_getConfigWord(int address)
793  *
794  * Get the current value of a config word.
795  *
796  *-----------------------------------------------------------------*/
797
798 int pic14_getConfigWord(int address)
799 {
800         switch (address)
801         {
802         case CONFIG_WORD_ADDRESS:
803                 return config_word;
804         
805         case CONFIG2_WORD_ADDRESS:
806                 return config2_word;
807         
808         default:
809                 return 0;
810         }
811 }
812
813 /*-----------------------------------------------------------------*
814 *  
815 *-----------------------------------------------------------------*/
816 unsigned pic14_getMaxRam(void)
817 {
818         return pic->defMaxRAMaddrs;
819 }
820
821
822 /*-----------------------------------------------------------------*
823 *  int getHasSecondConfigReg(void) - check if the device has a 
824 *  second config register, rather than just one.
825 *-----------------------------------------------------------------*/
826 int pic14_getHasSecondConfigReg(void)
827 {
828         if(!pic)
829                 return 0;
830         else
831                 return pic->hasSecondConfigReg;
832 }
833
834 /*-----------------------------------------------------------------*
835  * True iff the device has memory aliased in every bank.
836  * If true, low and high will be set to the low and high address
837  * occupied by the (last) sharebank found.
838  *-----------------------------------------------------------------*/
839 int pic14_hasSharebank(int *low, int *high, int *size)
840 {
841         memRange *r;
842
843         assert(pic);
844         r = pic->ram;
845
846         while (r) {
847             //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);
848             // find sufficiently large shared region
849             if ((r->alias == pic->bankMask)
850                     && (r->end_address != r->start_address) // ignore SFRs
851                     && (!size || (*size <= (r->end_address - r->start_address + 1))))
852             {
853                 if (low) *low = r->start_address;
854                 if (high) *high = r->end_address;
855                 if (size) *size = r->end_address - r->start_address + 1;
856                 return 1;
857             } // if
858             r = r->next;
859         } // while
860
861         if (low) *low = 0x0;
862         if (high) *high = 0x0;
863         if (size) *size = 0x0;
864         //fprintf (stderr, "%s: no shared bank found\n", __FUNCTION__);
865         return 0;
866 }
867
868 /*
869  * True iff the memory region [low, high] is aliased in all banks.
870  */
871 int pic14_isShared(int low, int high)
872 {
873         memRange *r;
874
875         assert(pic);
876         r = pic->ram;
877
878         while (r) {
879             //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);
880             if ((r->alias == pic->bankMask) && (r->start_address <= low) && (r->end_address >= high)) {
881                 return 1;
882             } // if
883             r = r->next;
884         } // while
885
886         return 0;
887 }
888
889 /*
890  * True iff all RAM is aliased in all banks (no BANKSELs required except for
891  * SFRs).
892  */
893 int pic14_allRAMShared(void)
894 {
895         memRange *r;
896
897         assert(pic);
898         r = pic->ram;
899
900         while (r) {
901             if (r->alias != pic->bankMask) return 0;
902             r = r->next;
903         } // while
904         
905         return 1;
906 }
907
908 /*
909  * True iff the pseudo stack is a sharebank --> let linker place it.
910  * [low, high] denotes a size byte long block of (shared or banked)
911  * memory to be used.
912  */
913 int pic14_getSharedStack(int *low, int *high, int *size)
914 {
915     int haveShared;
916     int l, h, s;
917
918     s = options.stack_size ? options.stack_size : 0x10;
919     haveShared = pic14_hasSharebank(&l, &h, &s);
920     if ((options.stack_loc != 0) || !haveShared)
921     {
922         // sharebank not available or not to be used
923         s = options.stack_size ? options.stack_size : 0x10;
924         l = options.stack_loc ? options.stack_loc : 0x20;
925         h = l + s - 1;
926         if (low) *low = l;
927         if (high) *high = h;
928         if (size) *size = s;
929         // return 1 iff [low, high] is present in all banks
930         //fprintf(stderr, "%s: low %x, high %x, size %x, shared %d\n", __FUNCTION__, l, h, s, pic14_isShared(l, h));
931         return (pic14_isShared(l, h));
932     } else {
933         // sharebanks available for use by the stack
934         if (options.stack_size) s = options.stack_size;
935         else if (!s || s > 16) s = 16; // limit stack to 16 bytes in SHAREBANK
936
937         // provide addresses for sharebank
938         if (low) *low = l;
939         if (high) *high = l + s - 1;
940         if (size) *size = s;
941         //fprintf(stderr, "%s: low %x, high %x, size %x, shared 1\n", __FUNCTION__, l, h, s);
942         return 1;
943     }
944 }
945
946 PIC_device * pic14_getPIC(void)
947 {
948     return pic;
949 }