* src/pic/device.c (create_pic,ram_map): add memRange entries to PIC
[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 #if defined(__BORLANDC__) || defined(_MSC_VER)
35 #define STRCASECMP stricmp
36 #define STRNCASECMP strnicmp
37 #else
38 #define STRCASECMP strcasecmp
39 #define STRNCASECMP strncasecmp
40 #endif
41
42 extern int Gstack_base_addr;
43 extern int Gstack_size;
44
45 #define MAX_PICLIST 200
46 static PIC_device *Pics[MAX_PICLIST];
47 static int num_of_supported_PICS = 0;
48
49 static PIC_device *pic=NULL;
50
51 int maxRAMaddress = 0;
52 AssignedMemory *finalMapping=NULL;
53
54 #define CONFIG_WORD_ADDRESS 0x2007
55 #define CONFIG2_WORD_ADDRESS 0x2008
56 #define DEFAULT_CONFIG_WORD 0x3fff
57 #define DEFAULT_CONFIG2_WORD 0x3ffc
58
59 #define DEVICE_FILE_NAME "pic14devices.txt"
60 #define PIC14_STRING_LEN 256
61 #define SPLIT_WORDS_MAX 16
62
63 static unsigned int config_word = DEFAULT_CONFIG_WORD;
64 static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
65 static memRange *rangeRAM = NULL;
66
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);
69
70
71 /* parse a value from the configuration file */
72 static int parse_config_value(char *str)
73 {
74         if (str[strlen(str) - 1] == 'K')
75                 return atoi(str) * 1024;        /* like "1K" */
76                 
77         else if (STRNCASECMP(str, "0x", 2) == 0)
78                 return strtol(str+2, NULL, 16); /* like "0x400" */
79         
80         else
81                 return atoi(str);               /* like "1024" */
82 }
83
84
85 /* split a line into words */
86 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
87 {
88         char *pos = str;
89         int num_words = 0;
90         int ccount;
91         
92         while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
93                 /* remove leading spaces */
94                 while (isspace(*pos) || *pos == ',')
95                         pos++;
96         
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';
101                 
102                 num_words++;
103         }
104         
105         return num_words;
106 }
107
108
109 /* remove annoying prefixes from the processor name */
110 static char *sanitise_processor_name(char *name)
111 {
112         char *proc_pos = name;
113
114         if (name == NULL)
115                 return NULL;
116         
117         if (STRNCASECMP(proc_pos, "pic", 3) == 0)
118                 proc_pos += 3;
119
120         else if (tolower(*proc_pos) == 'p')
121                 proc_pos += 1;
122                                 
123         return proc_pos;
124 }
125
126
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)
129 {
130         PIC_device *new_pic;
131         char *simple_pic_name = sanitise_processor_name(pic_name);
132         
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);
136                         
137         new_pic->defMaxRAMaddrs = maxram;
138         new_pic->bankMask = bankmsk;
139         new_pic->hasSecondConfigReg = confsiz > 1;
140         
141         new_pic->programMemSize = program;
142         new_pic->dataMemSize = data;
143         new_pic->eepromMemSize = eeprom;
144         new_pic->ioPins = io;
145
146         new_pic->ram = rangeRAM;
147         
148         Pics[num_of_supported_PICS] = new_pic;
149         num_of_supported_PICS++;
150                         
151         return new_pic;
152 }
153
154
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])
157 {
158         memRange r;
159         int pcount;
160         
161         if (num_words < 3) {
162                 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
163                 return;
164         }
165         
166         r.alias = parse_config_value(word[1]);
167
168         for (pcount = 2; pcount < num_words; pcount++) {
169             
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;
173                 
174                 addMemRange(&r, 1);
175         }
176 }
177
178
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])
181 {
182         memRange *r;
183         
184         if (num_words < 4) {
185                 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
186                 return;
187         }
188
189         r = Safe_calloc(1, sizeof(memRange));
190         //fprintf (stderr, "%s: %s %s %s\n", __FUNCTION__, word[1], word[2], word[3]);
191         
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;
196                 
197         addMemRange(r, 0);
198
199         // add memRange to device entry for future lookup (sharebanks)
200         r->next = rangeRAM;
201         rangeRAM = r;
202 }
203
204 extern set *includeDirsSet;
205 extern set *userIncDirsSet;
206 extern set *libDirsSet;
207 extern set *libPathsSet;
208
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)
212 {
213         FILE *pic_file;
214         char pic_buf[PIC14_STRING_LEN];
215         char *pic_buf_pos;
216         int found_processor = FALSE;
217         int done = FALSE;
218         char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
219         int num_processor_names = 0;
220         int pic_maxram = 0;
221         int pic_bankmsk = 0;
222         int pic_confsiz = 0;
223         int pic_program = 0;
224         int pic_data = 0;
225         int pic_eeprom = 0;
226         int pic_io = 0;
227         char *simple_pic_name;
228         char *dir;
229         char filename[512];
230         int len = 512;
231         
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;
235         
236         /* open the piclist file */
237         /* first scan all include directories */
238         pic_file = NULL;
239         //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
240         for (dir = setFirstItem(includeDirsSet);
241                 !pic_file && dir;
242                 dir = setNextItem(includeDirsSet))
243         {
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" );
247           if (pic_file) break;
248         } // for
249         for (dir = setFirstItem(userIncDirsSet);
250                 !pic_file && dir;
251                 dir = setNextItem(userIncDirsSet))
252         {
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" );
256           if (pic_file) break;
257         } // for
258         for (dir = setFirstItem(libDirsSet);
259                 !pic_file && dir;
260                 dir = setNextItem(libDirsSet))
261         {
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" );
265           if (pic_file) break;
266         } // for
267         for (dir = setFirstItem(libPathsSet);
268                 !pic_file && dir;
269                 dir = setNextItem(libPathsSet))
270         {
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" );
274           if (pic_file) break;
275         } // for
276         if (!pic_file) {
277           pic_file = fopen(DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME, "rt");
278         }
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);
287                         return NULL;
288                 }
289         }
290         
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) {
294                 
295                 /* remove trailing spaces */
296                 while (isspace(pic_buf[strlen(pic_buf)-1]))
297                         pic_buf[strlen(pic_buf)-1] = '\0';
298                 
299                 /* remove leading spaces */
300                 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
301                 {}
302                 
303                 /* ignore comment / empty lines */
304                 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
305                         
306                         /* split into fields */
307                         char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
308                         int num_pic_words;
309                         int wcount;
310                         
311                         num_pic_words = split_words(pic_word, pic_buf_pos);
312                         
313                         if (STRCASECMP(pic_word[0], "processor") == 0) {
314                         
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 */
319                                                 int dcount;
320                                                 
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);
324                                         }
325                                         
326                                         num_processor_names = split_words(processor_name, pic_buf_pos);
327                                 }
328                                 else {
329                                         /* if we've just completed reading a processor definition stop now */
330                                         if (found_processor)
331                                                 done = TRUE;
332                                         else {
333                                                 /* check if this processor name is a match */
334                                                 for (wcount = 1; wcount < num_pic_words; wcount++) {
335                                                         
336                                                         /* skip uninteresting prefixes */
337                                                         char *found_name = sanitise_processor_name(pic_word[wcount]);
338
339                                                         if (STRCASECMP(found_name, simple_pic_name) == 0)
340                                                                 found_processor = TRUE;
341                                                 }
342                                         }
343                                 }
344                         }
345                         
346                         else {
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);
352                                         }
353                                         else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
354                                                 pic_bankmsk = parse_config_value(pic_word[1]);
355                         
356                                         else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
357                                                 pic_confsiz = parse_config_value(pic_word[1]);
358                         
359                                         else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
360                                                 pic_program = parse_config_value(pic_word[1]);
361                         
362                                         else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
363                                                 pic_data = parse_config_value(pic_word[1]);
364                         
365                                         else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
366                                                 pic_eeprom = parse_config_value(pic_word[1]);
367                         
368                                         else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
369                                                 pic_io = parse_config_value(pic_word[1]);
370                         
371                                         else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
372                                                 if (found_processor)
373                                                         register_map(num_pic_words, pic_word);
374                                         }
375                                         else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
376                                                 if (found_processor)
377                                                         ram_map(num_pic_words, pic_word);
378                                         }
379                                         else {
380                                                 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
381                                         }
382                                 }
383                         }
384                 }
385         }
386         
387         fclose(pic_file);
388
389         /* if we're in read-the-lot mode then create the final processor definition */
390         if (pic_name == NULL) {
391             
392                 if (num_processor_names > 0) {
393                         /* store away all the previous processor definitions */
394                         int dcount;
395                 
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);
399                 }
400         }
401         else {
402                 /* in search mode */
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);
407                 }
408         }
409         
410         return NULL;
411 }
412
413 void addMemRange(memRange *r, int type)
414 {
415         int i;
416         int alias = r->alias;
417
418         //fprintf (stderr, "%s: range %x..%x, alias %x, bank %x\n", __FUNCTION__, r->start_address, r->end_address, r->alias, r->bank);
419         
420         if (maxRAMaddress < 0) {
421                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
422                 return;
423         }
424         
425         do {
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;
433                                 if(type) {
434                                         /* hack for now */
435                                         finalMapping[i | alias].isSFR  = 1;
436                                 } else {
437                                         finalMapping[i | alias].isSFR  = 0;
438                                 }
439                                 }
440                         } else {
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);
444                                 }
445                         }
446                 }
447                 
448                 /* Decrement alias */
449                 if (alias) {
450                         alias -= ((alias & (alias - 1)) ^ alias);
451                 } else {
452                         alias--;
453                 }
454                 
455         } while (alias >= 0);
456 }
457
458 void setMaxRAM(int size)
459 {
460         int i;
461         maxRAMaddress = size;
462         
463         if (maxRAMaddress < 0) {
464                 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
465                         maxRAMaddress, DEVICE_FILE_NAME);
466                 return;
467         }
468         
469         finalMapping = Safe_calloc(1+maxRAMaddress,
470                 sizeof(AssignedMemory));
471         
472         /* Now initialize the finalMapping array */
473         
474         for(i=0; i<=maxRAMaddress; i++) {
475                 finalMapping[i].reg = NULL;
476                 finalMapping[i].isValid = 0;
477                 finalMapping[i].bank = (i>>7);
478         }
479 }
480
481 /*-----------------------------------------------------------------*
482 *-----------------------------------------------------------------*/
483
484 int isREGinBank(regs *reg, int bank)
485 {
486         
487         if(!reg || !pic)
488                 return 0;
489         
490         if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
491                 return 1;
492         
493         return 0;
494 }
495
496 /*-----------------------------------------------------------------*
497 *-----------------------------------------------------------------*/
498 int REGallBanks(regs *reg)
499 {
500         
501         if(!reg || !pic)
502                 return 0;
503         
504         return ((reg->address | reg->alias) & pic->bankMask);
505         
506 }
507
508 /*-----------------------------------------------------------------*
509 *-----------------------------------------------------------------*/
510
511 int isSFR(int address)
512 {
513         
514         if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
515                 return 0;
516         
517         return 1;
518         
519 }
520
521 /*
522 *  dump_map -- debug stuff
523 */
524
525 void dump_map(void)
526 {
527         int i;
528         
529         for(i=0; i<=maxRAMaddress; i++) {
530                 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
531                 
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");
539                 }
540         }
541         
542 }
543
544 void dump_sfr(FILE *of)
545 {
546 #if 0
547         int start=-1;
548         int bank_base;
549         static int udata_flag=0;
550 #endif
551         int addr=0;
552         
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);
557                 return;
558         }
559         
560         for (addr = 0; addr <= maxRAMaddress; addr++)
561         {
562                 regs *reg = finalMapping[addr].reg;
563                 
564                 if (reg && !reg->isEmitted)
565                 {
566                   if (pic14_options.isLibrarySource && pic14_is_shared (reg))
567                   {
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
571                      * required. */
572                     //fprintf (of, "\textern\t%s\n", reg->name);
573                   } else {
574                     emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, pic14_is_shared (reg));
575                   }
576                   
577                   reg->isEmitted = 1;
578                 }
579         } // for
580
581 #if 0
582         do {
583
584                 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
585                         
586                         if(start<0)
587                                 start = addr;
588                 } else {
589                         if(start>=0) {
590                                 
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.
594                                 */
595                                 
596                                 bank_base = start & 0xfff8;
597                                 
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 */
601                                 
602                                 if ( (start != addr) && (!udata_flag) ) {
603                                         udata_flag = 1;
604                                         //fprintf(of,"\tudata\n");
605                                 }
606                                 
607                                 for( ; start < addr; start++) {
608                                         if((finalMapping[start].reg) && 
609                                                 (!finalMapping[start].reg->isEmitted) &&
610                                                 (!finalMapping[start].instance) && 
611                                                 (!finalMapping[start].isSFR)) {
612                                                 
613                                                 if (finalMapping[start].reg->isFixed) {
614                                                         unsigned i;
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);
619                                                         }
620                                                 } else {
621                                                         emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
622 #if 0
623                                                         fprintf(of,"%s\tres\t%i\n",
624                                                                 finalMapping[start].reg->name, 
625                                                                 finalMapping[start].reg->size);
626 #endif
627                                                 }
628                                                 finalMapping[start].reg->isEmitted = 1;
629                                         }
630                                 }
631                                 
632                                 start = -1;
633                         }
634                         
635                 }
636                 
637                 addr++;
638                 
639         } while(addr <= maxRAMaddress);
640
641
642 #endif
643 }
644
645 /*-----------------------------------------------------------------*
646 *  void list_valid_pics(int ncols, int list_alias)
647 *
648 * Print out a formatted list of valid PIC devices
649 *
650 * ncols - number of columns in the list.
651 *
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)
656 {
657         int col=0,longest;
658         int i,k,l;
659         
660         if (num_of_supported_PICS == 0)
661                 find_device(NULL);          /* load all the definitions */
662         
663         /* decrement the column number if it's greater than zero */
664         ncols = (ncols > 1) ? ncols-1 : 4;
665         
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);
669                 if(k>longest)
670                         longest = k;
671         }
672         
673 #if 1
674         /* heading */
675         fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
676         fprintf(stderr, " processor");
677         for(k=0; k<longest-1; k++)
678                 fputc(' ',stderr);
679         fprintf(stderr, "program     RAM      EEPROM    I/O\n");
680         fprintf(stderr, "-----------------------------------------------------\n");
681         
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);
685                 for(k=0; k<l; k++)
686                         fputc(' ',stderr);
687         
688                 fprintf(stderr, "     ");
689                 if (Pics[i]->programMemSize % 1024 == 0)
690                         fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
691                 else
692                         fprintf(stderr, "%5d", Pics[i]->programMemSize);
693         
694                 fprintf(stderr, "     %5d     %5d     %4d\n", 
695                         Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
696         }
697
698         col = 0;
699         
700         fprintf(stderr, "\nPIC14 processors supported:\n");
701         for(i=0;  i < num_of_supported_PICS; i++) {
702                         
703                 fprintf(stderr,"%s", Pics[i]->name);
704                 if(col<ncols) {
705                         l = longest + 2 - strlen(Pics[i]->name);
706                         for(k=0; k<l; k++)
707                                 fputc(' ',stderr);
708                                 
709                         col++;
710                                 
711                 } else {
712                         fputc('\n',stderr);
713                         col = 0;
714                 }
715                 
716         }
717 #endif
718         if(col != 0)
719                 fputc('\n',stderr);
720 }
721
722 /*-----------------------------------------------------------------*
723 *  
724 *-----------------------------------------------------------------*/
725 PIC_device *init_pic(char *pic_type)
726 {
727         char long_name[PIC14_STRING_LEN];
728         
729         pic = find_device(pic_type);
730         
731         if (pic == NULL) {
732                 /* check for shortened "16xxx" form */
733                 sprintf(long_name, "16%s", pic_type);
734                 pic = find_device(long_name);
735                 if (pic == NULL) {
736                         if(pic_type != NULL && pic_type[0] != '\0')
737                                 fprintf(stderr, "'%s' was not found.\n", pic_type);
738                         else
739                                 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
740                 
741                         list_valid_pics(7);
742                         exit(1);
743                 }
744         }
745         return pic;
746 }
747
748 /*-----------------------------------------------------------------*
749 *  
750 *-----------------------------------------------------------------*/
751 int picIsInitialized(void)
752 {
753         if(pic && maxRAMaddress > 0)
754                 return 1;
755         
756         return 0;
757         
758 }
759
760 /*-----------------------------------------------------------------*
761 *  char *processor_base_name(void) - Include file is derived from this.
762 *-----------------------------------------------------------------*/
763 char *processor_base_name(void)
764 {
765         
766         if(!pic)
767                 return NULL;
768         
769         return pic->name;
770 }
771
772 /*-----------------------------------------------------------------*
773 *-----------------------------------------------------------------*/
774 int validAddress(int address, int reg_size)
775 {
776         int i;
777         
778         if (maxRAMaddress < 0) {
779                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
780                 return 0;
781         }
782         //  fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
783         assert (reg_size > 0);
784         if(address + (reg_size - 1) > maxRAMaddress)
785                 return 0;
786         
787         for (i=0; i<reg_size; i++)
788                 if(!finalMapping[address + i].isValid || 
789                         finalMapping[address+i].reg ||
790                         finalMapping[address+i].isSFR )
791                         return 0;
792                 
793                 return 1;
794 }
795
796 /*-----------------------------------------------------------------*
797 *-----------------------------------------------------------------*/
798 void mapRegister(regs *reg)
799 {
800         
801         unsigned i;
802         int alias;
803         
804         if(!reg || !reg->size) {
805                 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
806                 return;
807         }
808         
809         if (maxRAMaddress < 0) {
810                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
811                 return;
812         }
813         
814         for(i=0; i<reg->size; i++) {
815                 
816                 assert(reg->address >= 0 && reg->address < maxRAMaddress);
817                 
818                 alias = finalMapping[reg->address].alias;
819                 reg->alias = alias;
820                 
821                 do {
822                         
823                         //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
824                         
825                         finalMapping[reg->address + alias + i].reg = reg;
826                         finalMapping[reg->address + alias + i].instance = i;
827                         
828                         /* Decrement alias */
829                         if(alias)
830                                 alias -= ((alias & (alias - 1)) ^ alias);
831                         else
832                                 alias--;
833                         
834                 } while (alias>=0);
835         }
836         
837         //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
838         
839         reg->isMapped = 1;
840         
841 }
842
843 /*-----------------------------------------------------------------*
844 *-----------------------------------------------------------------*/
845 int assignRegister(regs *reg, int start_address)
846 {
847         int i;
848         
849         //fprintf(stderr,"%s -  %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
850         if(reg->isFixed) {
851                 
852                 if (validAddress(reg->address,reg->size)) {
853                         //fprintf(stderr,"%s -  %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
854                         mapRegister(reg);
855                         return reg->address;
856                 }
857                 
858                 if( isSFR(reg->address)) {
859                         mapRegister(reg);
860                         return reg->address;
861                 }
862                 
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);
866                 }
867                 
868         } else {
869                 
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 */
873                 
874                 for (i=start_address; i<=maxRAMaddress; i++) {
875                         
876                         if (validAddress(i,reg->size)) {
877                                 reg->address = i;
878                                 mapRegister(reg);
879                                 return i;
880                         }
881                 }
882                 
883                 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
884                 
885         }
886         
887         return -1;
888 }
889
890 /*-----------------------------------------------------------------*
891 *-----------------------------------------------------------------*/
892 void assignFixedRegisters(set *regset)
893 {
894         regs *reg;
895         
896         for (reg = setFirstItem(regset) ; reg ; 
897         reg = setNextItem(regset)) {
898                 
899                 if(reg->isFixed) 
900                         assignRegister(reg,0);
901         }
902         
903 }
904
905 /*-----------------------------------------------------------------*
906 *-----------------------------------------------------------------*/
907 void assignRelocatableRegisters(set *regset, int used)
908 {
909         
910         regs *reg;
911         int address = 0;
912         
913         for (reg = setFirstItem(regset) ; reg ; 
914         reg = setNextItem(regset)) {
915                 
916                 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
917                 
918                 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
919                         /* If register have been reused then shall not print it a second time. */
920                         set *s;
921                         int done = 0;
922                         for (s = regset; s; s = s->next) {
923                                 regs *r;
924                                 r = s->item;
925                                 if (r == reg)
926                                         break;
927                                 if((!r->isFixed) && ( used || r->wasUsed)) {
928                                         if (r->rIdx == reg->rIdx) {
929                                                 reg->address = r->address;
930                                                 done = 1;
931                                                 break;
932                                         }
933                                 }
934                         }
935                         if (!done)
936                                 address = assignRegister(reg,address);
937                 }
938         }
939         
940 }
941
942 /* Keep track of whether we found an assignment to the __config words. */
943 static int pic14_hasSetConfigWord = 0;
944
945 /*-----------------------------------------------------------------*
946  *  void assignConfigWordValue(int address, int value)
947  *
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.
951  *
952  *-----------------------------------------------------------------*/
953
954 void pic14_assignConfigWordValue(int address, int value)
955 {
956         if (CONFIG_WORD_ADDRESS == address)
957                 config_word = value;
958         
959         else if (CONFIG2_WORD_ADDRESS == address)
960                 config2_word = value;
961         
962         //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
963         pic14_hasSetConfigWord = 1;
964 }
965
966 /*-----------------------------------------------------------------*
967  * int pic14_emitConfigWord (FILE * vFile)
968  * 
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)
974 {
975   if (pic14_hasSetConfigWord)
976   {
977     fprintf (vFile, "%s", iComments2);
978     fprintf (vFile, "; config word \n");
979     fprintf (vFile, "%s", iComments2);
980     if (pic14_getHasSecondConfigReg())
981     {
982       fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
983       fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
984     }
985     else
986       fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
987
988     return 1;
989   }
990   return 0;
991 }
992
993 /*-----------------------------------------------------------------*
994  * int pic14_getConfigWord(int address)
995  *
996  * Get the current value of a config word.
997  *
998  *-----------------------------------------------------------------*/
999
1000 int pic14_getConfigWord(int address)
1001 {
1002         switch (address)
1003         {
1004         case CONFIG_WORD_ADDRESS:
1005                 return config_word;
1006         
1007         case CONFIG2_WORD_ADDRESS:
1008                 return config2_word;
1009         
1010         default:
1011                 return 0;
1012         }
1013 }
1014
1015 /*-----------------------------------------------------------------*
1016 *  
1017 *-----------------------------------------------------------------*/
1018 unsigned pic14_getMaxRam(void)
1019 {
1020         return pic->defMaxRAMaddrs;
1021 }
1022
1023
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)
1029 {
1030         if(!pic)
1031                 return 0;
1032         else
1033                 return pic->hasSecondConfigReg;
1034 }
1035
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)
1042 {
1043         memRange *r;
1044
1045         assert(pic);
1046         r = pic->ram;
1047
1048         while (r) {
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;
1054                 return 1;
1055             } // if
1056             r = r->next;
1057         } // while
1058
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__);
1063         return 0;
1064 }
1065
1066 /*
1067  * True iff the memory region [low, high] is aliased in all banks.
1068  */
1069 int pic14_isShared(int low, int high)
1070 {
1071         memRange *r;
1072
1073         assert(pic);
1074         r = pic->ram;
1075
1076         while (r) {
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)) {
1079                 return 1;
1080             } // if
1081             r = r->next;
1082         } // while
1083
1084         return 0;
1085 }
1086
1087 /*
1088  * True iff all RAM is aliased in all banks (no BANKSELs required except for
1089  * SFRs).
1090  */
1091 int pic14_allRAMShared(void)
1092 {
1093         memRange *r;
1094
1095         assert(pic);
1096         r = pic->ram;
1097
1098         while (r) {
1099             if (r->alias != pic->bankMask) return 0;
1100             r = r->next;
1101         } // while
1102         
1103         return 1;
1104 }
1105
1106 /*
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.
1110  */
1111 int pic14_getSharedStack(int *low, int *high, int *size)
1112 {
1113     int haveShared;
1114     int l, h, s;
1115
1116     haveShared = pic14_hasSharebank(&l, &h, &s);
1117     if ((options.stack_loc != 0) || !haveShared)
1118     {
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;
1123         if (low) *low = l;
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));
1129     } else {
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
1133
1134         // provide addresses for sharebank
1135         if (low) *low = l;
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);
1139         return 1;
1140     }
1141 }
1142
1143 PIC_device * pic14_getPIC(void)
1144 {
1145     return pic;
1146 }