* src/pic/glue.h: added pic14aopLiteral prototype
[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
66 extern int pic14_is_shared (regs *reg);
67 extern void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize);
68
69
70 /* parse a value from the configuration file */
71 static int parse_config_value(char *str)
72 {
73         if (str[strlen(str) - 1] == 'K')
74                 return atoi(str) * 1024;        /* like "1K" */
75                 
76         else if (STRNCASECMP(str, "0x", 2) == 0)
77                 return strtol(str+2, NULL, 16); /* like "0x400" */
78         
79         else
80                 return atoi(str);               /* like "1024" */
81 }
82
83
84 /* split a line into words */
85 static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
86 {
87         char *pos = str;
88         int num_words = 0;
89         int ccount;
90         
91         while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
92                 /* remove leading spaces */
93                 while (isspace(*pos) || *pos == ',')
94                         pos++;
95         
96                 /* copy everything up until the first space or comma */
97                 for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
98                         result_word[num_words][ccount] = *pos;
99                 result_word[num_words][ccount] = '\0';
100                 
101                 num_words++;
102         }
103         
104         return num_words;
105 }
106
107
108 /* remove annoying prefixes from the processor name */
109 static char *sanitise_processor_name(char *name)
110 {
111         char *proc_pos = name;
112
113         if (name == NULL)
114                 return NULL;
115         
116         if (STRNCASECMP(proc_pos, "pic", 3) == 0)
117                 proc_pos += 3;
118
119         else if (tolower(*proc_pos) == 'p')
120                 proc_pos += 1;
121                                 
122         return proc_pos;
123 }
124
125
126 /* create a structure for a pic processor */
127 static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
128 {
129         PIC_device *new_pic;
130         char *simple_pic_name = sanitise_processor_name(pic_name);
131         
132         new_pic = Safe_calloc(1, sizeof(PIC_device));
133         new_pic->name = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
134         strcpy(new_pic->name, simple_pic_name);
135                         
136         new_pic->defMaxRAMaddrs = maxram;
137         new_pic->bankMask = bankmsk;
138         new_pic->hasSecondConfigReg = confsiz > 1;
139         
140         new_pic->programMemSize = program;
141         new_pic->dataMemSize = data;
142         new_pic->eepromMemSize = eeprom;
143         new_pic->ioPins = io;
144         
145         Pics[num_of_supported_PICS] = new_pic;
146         num_of_supported_PICS++;
147                         
148         return new_pic;
149 }
150
151
152 /* mark some registers as being duplicated across banks */
153 static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
154 {
155         memRange r;
156         int pcount;
157         
158         if (num_words < 3) {
159                 fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
160                 return;
161         }
162         
163         r.alias = parse_config_value(word[1]);
164
165         for (pcount = 2; pcount < num_words; pcount++) {
166             
167                 r.start_address = parse_config_value(word[pcount]);
168                 r.end_address = parse_config_value(word[pcount]);
169                 r.bank = (r.start_address >> 7) & 3;
170                 
171                 addMemRange(&r, 1);
172         }
173 }
174
175
176 /* define ram areas - may be duplicated across banks */
177 static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
178 {
179         memRange r;
180         
181         if (num_words < 4) {
182                 fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
183                 return;
184         }
185         
186         r.start_address = parse_config_value(word[1]);
187         r.end_address = parse_config_value(word[2]);
188         r.alias = parse_config_value(word[3]);
189         r.bank = (r.start_address >> 7) & 3;
190                 
191         addMemRange(&r, 0);
192 }
193
194 extern set *includeDirsSet;
195 extern set *userIncDirsSet;
196 extern set *libDirsSet;
197 extern set *libPathsSet;
198
199 /* read the file with all the pic14 definitions and pick out the definition for a processor
200  * if specified. if pic_name is NULL reads everything */
201 static PIC_device *find_device(char *pic_name)
202 {
203         FILE *pic_file;
204         char pic_buf[PIC14_STRING_LEN];
205         char *pic_buf_pos;
206         int found_processor = FALSE;
207         int done = FALSE;
208         char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
209         int num_processor_names = 0;
210         int pic_maxram = 0;
211         int pic_bankmsk = 0;
212         int pic_confsiz = 0;
213         int pic_program = 0;
214         int pic_data = 0;
215         int pic_eeprom = 0;
216         int pic_io = 0;
217         char *simple_pic_name;
218         char *dir;
219         char filename[512];
220         int len = 512;
221         
222         /* allow abbreviations of the form "f877" - convert to "16f877" */
223         simple_pic_name = sanitise_processor_name(pic_name);
224         num_of_supported_PICS = 0;
225         
226         /* open the piclist file */
227         /* first scan all include directories */
228         pic_file = NULL;
229         //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
230         for (dir = setFirstItem(includeDirsSet);
231                 !pic_file && dir;
232                 dir = setNextItem(includeDirsSet))
233         {
234           //fprintf( stderr, "searching1 %s\n", dir );
235           SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
236           pic_file = fopen( filename, "rt" );
237           if (pic_file) break;
238         } // for
239         for (dir = setFirstItem(userIncDirsSet);
240                 !pic_file && dir;
241                 dir = setNextItem(userIncDirsSet))
242         {
243           //fprintf( stderr, "searching2 %s\n", dir );
244           SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
245           pic_file = fopen( filename, "rt" );
246           if (pic_file) break;
247         } // for
248         for (dir = setFirstItem(libDirsSet);
249                 !pic_file && dir;
250                 dir = setNextItem(libDirsSet))
251         {
252           //fprintf( stderr, "searching3 %s\n", dir );
253           SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
254           pic_file = fopen( filename, "rt" );
255           if (pic_file) break;
256         } // for
257         for (dir = setFirstItem(libPathsSet);
258                 !pic_file && dir;
259                 dir = setNextItem(libPathsSet))
260         {
261           //fprintf( stderr, "searching4 %s\n", dir );
262           SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
263           pic_file = fopen( filename, "rt" );
264           if (pic_file) break;
265         } // for
266         if (!pic_file) {
267           pic_file = fopen(DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME, "rt");
268         }
269         if (pic_file == NULL) {
270                 /* this second attempt is used when initially building the libraries */
271                 pic_file = fopen(".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." 
272                                 DIR_SEPARATOR_STRING "src" DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING 
273                                 DEVICE_FILE_NAME, "rt");
274                 if (pic_file == NULL) {
275                         fprintf(stderr, "can't find %s\n", DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" 
276                                         DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
277                         return NULL;
278                 }
279         }
280         
281         /* read line by line */
282         pic_buf[sizeof(pic_buf)-1] = '\0';
283         while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
284                 
285                 /* remove trailing spaces */
286                 while (isspace(pic_buf[strlen(pic_buf)-1]))
287                         pic_buf[strlen(pic_buf)-1] = '\0';
288                 
289                 /* remove leading spaces */
290                 for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
291                 {}
292                 
293                 /* ignore comment / empty lines */
294                 if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
295                         
296                         /* split into fields */
297                         char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
298                         int num_pic_words;
299                         int wcount;
300                         
301                         num_pic_words = split_words(pic_word, pic_buf_pos);
302                         
303                         if (STRCASECMP(pic_word[0], "processor") == 0) {
304                         
305                                 if (pic_name == NULL) {
306                                         /* this is the mode where we read all the processors in - store the names for now */
307                                         if (num_processor_names > 0) {
308                                                 /* store away all the previous processor definitions */
309                                                 int dcount;
310                                                 
311                                                 for (dcount = 1; dcount < num_processor_names; dcount++)
312                                                         create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
313                                                                    pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
314                                         }
315                                         
316                                         num_processor_names = split_words(processor_name, pic_buf_pos);
317                                 }
318                                 else {
319                                         /* if we've just completed reading a processor definition stop now */
320                                         if (found_processor)
321                                                 done = TRUE;
322                                         else {
323                                                 /* check if this processor name is a match */
324                                                 for (wcount = 1; wcount < num_pic_words; wcount++) {
325                                                         
326                                                         /* skip uninteresting prefixes */
327                                                         char *found_name = sanitise_processor_name(pic_word[wcount]);
328
329                                                         if (STRCASECMP(found_name, simple_pic_name) == 0)
330                                                                 found_processor = TRUE;
331                                                 }
332                                         }
333                                 }
334                         }
335                         
336                         else {
337                                 if (found_processor || pic_name == NULL) {
338                                         /* only parse a processor section if we've found the one we want */
339                                         if (STRCASECMP(pic_word[0], "maxram") == 0 && num_pic_words > 1) {
340                                                 pic_maxram = parse_config_value(pic_word[1]);
341                                                 setMaxRAM(pic_maxram);
342                                         }
343                                         else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
344                                                 pic_bankmsk = parse_config_value(pic_word[1]);
345                         
346                                         else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
347                                                 pic_confsiz = parse_config_value(pic_word[1]);
348                         
349                                         else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
350                                                 pic_program = parse_config_value(pic_word[1]);
351                         
352                                         else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
353                                                 pic_data = parse_config_value(pic_word[1]);
354                         
355                                         else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
356                                                 pic_eeprom = parse_config_value(pic_word[1]);
357                         
358                                         else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
359                                                 pic_io = parse_config_value(pic_word[1]);
360                         
361                                         else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
362                                                 if (found_processor)
363                                                         register_map(num_pic_words, pic_word);
364                                         }
365                                         else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
366                                                 if (found_processor)
367                                                         ram_map(num_pic_words, pic_word);
368                                         }
369                                         else {
370                                                 fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
371                                         }
372                                 }
373                         }
374                 }
375         }
376         
377         fclose(pic_file);
378
379         /* if we're in read-the-lot mode then create the final processor definition */
380         if (pic_name == NULL) {
381             
382                 if (num_processor_names > 0) {
383                         /* store away all the previous processor definitions */
384                         int dcount;
385                 
386                         for (dcount = 1; dcount < num_processor_names; dcount++)
387                                 create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
388                                            pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
389                 }
390         }
391         else {
392                 /* in search mode */
393                 if (found_processor) {
394                         /* create a new pic entry */
395                         return create_pic(pic_name, pic_maxram, pic_bankmsk,
396                                           pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
397                 }
398         }
399         
400         return NULL;
401 }
402
403 void addMemRange(memRange *r, int type)
404 {
405         int i;
406         int alias = r->alias;
407         
408         if (maxRAMaddress < 0) {
409                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
410                 return;
411         }
412         
413         do {
414                 for (i=r->start_address; i<= r->end_address; i++) {
415                         if ((i|alias) <= maxRAMaddress) {
416                                 /* if we haven't seen this address before, enter it */
417                                 if (!finalMapping[i | alias].isValid) {
418                                 finalMapping[i | alias].isValid = 1;
419                                 finalMapping[i | alias].alias = r->alias;
420                                 finalMapping[i | alias].bank  = r->bank;
421                                 if(type) {
422                                         /* hack for now */
423                                         finalMapping[i | alias].isSFR  = 1;
424                                 } else {
425                                         finalMapping[i | alias].isSFR  = 0;
426                                 }
427                                 }
428                         } else {
429                                 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
430                                         __FILE__,__FUNCTION__,(i|alias), maxRAMaddress);
431                         }
432                 }
433                 
434                 /* Decrement alias */
435                 if (alias) {
436                         alias -= ((alias & (alias - 1)) ^ alias);
437                 } else {
438                         alias--;
439                 }
440                 
441         } while (alias >= 0);
442 }
443
444 void setMaxRAM(int size)
445 {
446         int i;
447         maxRAMaddress = size;
448         
449         if (maxRAMaddress < 0) {
450                 fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
451                         maxRAMaddress, DEVICE_FILE_NAME);
452                 return;
453         }
454         
455         finalMapping = Safe_calloc(1+maxRAMaddress,
456                 sizeof(AssignedMemory));
457         
458         /* Now initialize the finalMapping array */
459         
460         for(i=0; i<=maxRAMaddress; i++) {
461                 finalMapping[i].reg = NULL;
462                 finalMapping[i].isValid = 0;
463                 finalMapping[i].bank = (i>>7);
464         }
465 }
466
467 /*-----------------------------------------------------------------*
468 *-----------------------------------------------------------------*/
469
470 int isREGinBank(regs *reg, int bank)
471 {
472         
473         if(!reg || !pic)
474                 return 0;
475         
476         if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
477                 return 1;
478         
479         return 0;
480 }
481
482 /*-----------------------------------------------------------------*
483 *-----------------------------------------------------------------*/
484 int REGallBanks(regs *reg)
485 {
486         
487         if(!reg || !pic)
488                 return 0;
489         
490         return ((reg->address | reg->alias) & pic->bankMask);
491         
492 }
493
494 /*-----------------------------------------------------------------*
495 *-----------------------------------------------------------------*/
496
497 int isSFR(int address)
498 {
499         
500         if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
501                 return 0;
502         
503         return 1;
504         
505 }
506
507 /*
508 *  dump_map -- debug stuff
509 */
510
511 void dump_map(void)
512 {
513         int i;
514         
515         for(i=0; i<=maxRAMaddress; i++) {
516                 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
517                 
518                 if(finalMapping[i].isValid) {
519                         fprintf(stderr,"addr: 0x%02x",i);
520                         if(finalMapping[i].isSFR)
521                                 fprintf(stderr," isSFR");
522                         if(finalMapping[i].reg) 
523                                 fprintf( stderr, "  reg %s", finalMapping[i].reg->name);
524                         fprintf(stderr, "\n");
525                 }
526         }
527         
528 }
529
530 void dump_sfr(FILE *of)
531 {
532 #if 0
533         int start=-1;
534         int bank_base;
535         static int udata_flag=0;
536 #endif
537         int addr=0;
538         
539         //dump_map();   /* display the register map */
540         //fprintf(stdout,";dump_sfr  \n");
541         if (maxRAMaddress < 0) {
542                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
543                 return;
544         }
545         
546         for (addr = 0; addr <= maxRAMaddress; addr++)
547         {
548                 regs *reg = finalMapping[addr].reg;
549                 
550                 if (reg && !reg->isEmitted)
551                 {
552                   if (pic14_options.isLibrarySource && pic14_is_shared (reg))
553                   {
554                     /* rely on external declarations for the non-fixed stack */
555                     /* Update: We always emit the STACK symbols into a
556                      * udata_shr section, so no extern declaration is
557                      * required. */
558                     //fprintf (of, "\textern\t%s\n", reg->name);
559                   } else {
560                     emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, pic14_is_shared (reg));
561                   }
562                   
563                   reg->isEmitted = 1;
564                 }
565         } // for
566
567 #if 0
568         do {
569
570                 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
571                         
572                         if(start<0)
573                                 start = addr;
574                 } else {
575                         if(start>=0) {
576                                 
577                                 /* clear the lower 7-bits of the start address of the first
578                                 * variable declared in this bank. The upper bits for the mid
579                                 * range pics are the bank select bits.
580                                 */
581                                 
582                                 bank_base = start & 0xfff8;
583                                 
584                                 /* The bank number printed in the cblock comment tacitly
585                                 * assumes that the first register in the contiguous group
586                                 * of registers represents the bank for the whole group */
587                                 
588                                 if ( (start != addr) && (!udata_flag) ) {
589                                         udata_flag = 1;
590                                         //fprintf(of,"\tudata\n");
591                                 }
592                                 
593                                 for( ; start < addr; start++) {
594                                         if((finalMapping[start].reg) && 
595                                                 (!finalMapping[start].reg->isEmitted) &&
596                                                 (!finalMapping[start].instance) && 
597                                                 (!finalMapping[start].isSFR)) {
598                                                 
599                                                 if (finalMapping[start].reg->isFixed) {
600                                                         unsigned i;
601                                                         for (i=0; i<finalMapping[start].reg->size; i++) {
602                                                                 fprintf(of,"%s\tEQU\t0x%04x\n",
603                                                                         finalMapping[start].reg->name, 
604                                                                         finalMapping[start].reg->address+i);
605                                                         }
606                                                 } else {
607                                                         emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
608 #if 0
609                                                         fprintf(of,"%s\tres\t%i\n",
610                                                                 finalMapping[start].reg->name, 
611                                                                 finalMapping[start].reg->size);
612 #endif
613                                                 }
614                                                 finalMapping[start].reg->isEmitted = 1;
615                                         }
616                                 }
617                                 
618                                 start = -1;
619                         }
620                         
621                 }
622                 
623                 addr++;
624                 
625         } while(addr <= maxRAMaddress);
626
627
628 #endif
629 }
630
631 /*-----------------------------------------------------------------*
632 *  void list_valid_pics(int ncols, int list_alias)
633 *
634 * Print out a formatted list of valid PIC devices
635 *
636 * ncols - number of columns in the list.
637 *
638 * list_alias - if non-zero, print all of the supported aliases
639 *              for a device (e.g. F84, 16F84, etc...)
640 *-----------------------------------------------------------------*/
641 void list_valid_pics(int ncols)
642 {
643         int col=0,longest;
644         int i,k,l;
645         
646         if (num_of_supported_PICS == 0)
647                 find_device(NULL);          /* load all the definitions */
648         
649         /* decrement the column number if it's greater than zero */
650         ncols = (ncols > 1) ? ncols-1 : 4;
651         
652         /* Find the device with the longest name */
653         for(i=0,longest=0; i<num_of_supported_PICS; i++) {
654                 k = strlen(Pics[i]->name);
655                 if(k>longest)
656                         longest = k;
657         }
658         
659 #if 1
660         /* heading */
661         fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
662         fprintf(stderr, " processor");
663         for(k=0; k<longest-1; k++)
664                 fputc(' ',stderr);
665         fprintf(stderr, "program     RAM      EEPROM    I/O\n");
666         fprintf(stderr, "-----------------------------------------------------\n");
667         
668         for(i=0;  i < num_of_supported_PICS; i++) {
669                 fprintf(stderr,"  %s", Pics[i]->name);
670                 l = longest + 2 - strlen(Pics[i]->name);
671                 for(k=0; k<l; k++)
672                         fputc(' ',stderr);
673         
674                 fprintf(stderr, "     ");
675                 if (Pics[i]->programMemSize % 1024 == 0)
676                         fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
677                 else
678                         fprintf(stderr, "%5d", Pics[i]->programMemSize);
679         
680                 fprintf(stderr, "     %5d     %5d     %4d\n", 
681                         Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
682         }
683
684         col = 0;
685         
686         fprintf(stderr, "\nPIC14 processors supported:\n");
687         for(i=0;  i < num_of_supported_PICS; i++) {
688                         
689                 fprintf(stderr,"%s", Pics[i]->name);
690                 if(col<ncols) {
691                         l = longest + 2 - strlen(Pics[i]->name);
692                         for(k=0; k<l; k++)
693                                 fputc(' ',stderr);
694                                 
695                         col++;
696                                 
697                 } else {
698                         fputc('\n',stderr);
699                         col = 0;
700                 }
701                 
702         }
703 #endif
704         if(col != 0)
705                 fputc('\n',stderr);
706 }
707
708 /*-----------------------------------------------------------------*
709 *  
710 *-----------------------------------------------------------------*/
711 void init_pic(char *pic_type)
712 {
713         char long_name[PIC14_STRING_LEN];
714         
715         pic = find_device(pic_type);
716         
717         if (pic == NULL) {
718                 /* check for shortened "16xxx" form */
719                 sprintf(long_name, "16%s", pic_type);
720                 pic = find_device(long_name);
721                 if (pic == NULL) {
722                         if(pic_type != NULL && pic_type[0] != '\0')
723                                 fprintf(stderr, "'%s' was not found.\n", pic_type);
724                         else
725                                 fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
726                 
727                         list_valid_pics(7);
728                         exit(1);
729                 }
730         }
731 }
732
733 /*-----------------------------------------------------------------*
734 *  
735 *-----------------------------------------------------------------*/
736 int picIsInitialized(void)
737 {
738         if(pic && maxRAMaddress > 0)
739                 return 1;
740         
741         return 0;
742         
743 }
744
745 /*-----------------------------------------------------------------*
746 *  char *processor_base_name(void) - Include file is derived from this.
747 *-----------------------------------------------------------------*/
748 char *processor_base_name(void)
749 {
750         
751         if(!pic)
752                 return NULL;
753         
754         return pic->name;
755 }
756
757 /*-----------------------------------------------------------------*
758 *-----------------------------------------------------------------*/
759 int validAddress(int address, int reg_size)
760 {
761         int i;
762         
763         if (maxRAMaddress < 0) {
764                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
765                 return 0;
766         }
767         //  fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
768         assert (reg_size > 0);
769         if(address + (reg_size - 1) > maxRAMaddress)
770                 return 0;
771         
772         for (i=0; i<reg_size; i++)
773                 if(!finalMapping[address + i].isValid || 
774                         finalMapping[address+i].reg ||
775                         finalMapping[address+i].isSFR )
776                         return 0;
777                 
778                 return 1;
779 }
780
781 /*-----------------------------------------------------------------*
782 *-----------------------------------------------------------------*/
783 void mapRegister(regs *reg)
784 {
785         
786         unsigned i;
787         int alias;
788         
789         if(!reg || !reg->size) {
790                 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
791                 return;
792         }
793         
794         if (maxRAMaddress < 0) {
795                 fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
796                 return;
797         }
798         
799         for(i=0; i<reg->size; i++) {
800                 
801                 alias = finalMapping[reg->address].alias;
802                 reg->alias = alias;
803                 
804                 do {
805                         
806                         //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
807                         
808                         finalMapping[reg->address + alias + i].reg = reg;
809                         finalMapping[reg->address + alias + i].instance = i;
810                         
811                         /* Decrement alias */
812                         if(alias)
813                                 alias -= ((alias & (alias - 1)) ^ alias);
814                         else
815                                 alias--;
816                         
817                 } while (alias>=0);
818         }
819         
820         //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
821         
822         reg->isMapped = 1;
823         
824 }
825
826 /*-----------------------------------------------------------------*
827 *-----------------------------------------------------------------*/
828 int assignRegister(regs *reg, int start_address)
829 {
830         int i;
831         
832         //fprintf(stderr,"%s -  %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
833         if(reg->isFixed) {
834                 
835                 if (validAddress(reg->address,reg->size)) {
836                         //fprintf(stderr,"%s -  %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
837                         mapRegister(reg);
838                         return reg->address;
839                 }
840                 
841                 if( isSFR(reg->address)) {
842                         mapRegister(reg);
843                         return reg->address;
844                 }
845                 
846                 fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
847                     reg->address, reg->name);
848                 
849         } else {
850                 
851         /* This register does not have a fixed address requirement
852         * so we'll search through all availble ram address and
853                 * assign the first one */
854                 
855                 for (i=start_address; i<=maxRAMaddress; i++) {
856                         
857                         if (validAddress(i,reg->size)) {
858                                 reg->address = i;
859                                 mapRegister(reg);
860                                 return i;
861                         }
862                 }
863                 
864                 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
865                 
866         }
867         
868         return -1;
869 }
870
871 /*-----------------------------------------------------------------*
872 *-----------------------------------------------------------------*/
873 void assignFixedRegisters(set *regset)
874 {
875         regs *reg;
876         
877         for (reg = setFirstItem(regset) ; reg ; 
878         reg = setNextItem(regset)) {
879                 
880                 if(reg->isFixed) 
881                         assignRegister(reg,0);
882         }
883         
884 }
885
886 /*-----------------------------------------------------------------*
887 *-----------------------------------------------------------------*/
888 void assignRelocatableRegisters(set *regset, int used)
889 {
890         
891         regs *reg;
892         int address = 0;
893         
894         for (reg = setFirstItem(regset) ; reg ; 
895         reg = setNextItem(regset)) {
896                 
897                 //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
898                 
899                 if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
900                         /* If register have been reused then shall not print it a second time. */
901                         set *s;
902                         int done = 0;
903                         for (s = regset; s; s = s->next) {
904                                 regs *r;
905                                 r = s->item;
906                                 if (r == reg)
907                                         break;
908                                 if((!r->isFixed) && ( used || r->wasUsed)) {
909                                         if (r->rIdx == reg->rIdx) {
910                                                 reg->address = r->address;
911                                                 done = 1;
912                                                 break;
913                                         }
914                                 }
915                         }
916                         if (!done)
917                                 address = assignRegister(reg,address);
918                 }
919         }
920         
921 }
922
923 /* Keep track of whether we found an assignment to the __config words. */
924 static int pic14_hasSetConfigWord = 0;
925
926 /*-----------------------------------------------------------------*
927  *  void assignConfigWordValue(int address, int value)
928  *
929  * Most midrange PICs have one config word at address 0x2007.
930  * Newer PIC14s have a second config word at address 0x2008.
931  * This routine will assign values to those addresses.
932  *
933  *-----------------------------------------------------------------*/
934
935 void pic14_assignConfigWordValue(int address, int value)
936 {
937         if (CONFIG_WORD_ADDRESS == address)
938                 config_word = value;
939         
940         else if (CONFIG2_WORD_ADDRESS == address)
941                 config2_word = value;
942         
943         //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
944         pic14_hasSetConfigWord = 1;
945 }
946
947 /*-----------------------------------------------------------------*
948  * int pic14_emitConfigWord (FILE * vFile)
949  * 
950  * Emit the __config directives iff we found a previous assignment
951  * to the config word.
952  *-----------------------------------------------------------------*/
953 extern char *iComments2;
954 int pic14_emitConfigWord (FILE * vFile)
955 {
956   if (pic14_hasSetConfigWord)
957   {
958     fprintf (vFile, "%s", iComments2);
959     fprintf (vFile, "; config word \n");
960     fprintf (vFile, "%s", iComments2);
961     if (pic14_getHasSecondConfigReg())
962     {
963       fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
964       fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
965     }
966     else
967       fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
968
969     return 1;
970   }
971   return 0;
972 }
973
974 /*-----------------------------------------------------------------*
975  * int pic14_getConfigWord(int address)
976  *
977  * Get the current value of a config word.
978  *
979  *-----------------------------------------------------------------*/
980
981 int pic14_getConfigWord(int address)
982 {
983         switch (address)
984         {
985         case CONFIG_WORD_ADDRESS:
986                 return config_word;
987         
988         case CONFIG2_WORD_ADDRESS:
989                 return config2_word;
990         
991         default:
992                 return 0;
993         }
994 }
995
996 /*-----------------------------------------------------------------*
997 *  
998 *-----------------------------------------------------------------*/
999 unsigned pic14_getMaxRam(void)
1000 {
1001         return pic->defMaxRAMaddrs;
1002 }
1003
1004
1005 /*-----------------------------------------------------------------*
1006 *  int getHasSecondConfigReg(void) - check if the device has a 
1007 *  second config register, rather than just one.
1008 *-----------------------------------------------------------------*/
1009 int pic14_getHasSecondConfigReg(void)
1010 {
1011         if(!pic)
1012                 return 0;
1013         else
1014                 return pic->hasSecondConfigReg;
1015 }
1016
1017 /*-----------------------------------------------------------------*
1018  * Query the size of the sharebank of the selected device.
1019  * FIXME: Currently always returns 16.
1020  *-----------------------------------------------------------------*/
1021 int pic14_getSharebankSize(void)
1022 {
1023         return 16;
1024 }
1025
1026 /*-----------------------------------------------------------------*
1027  * Query the highest byte address occupied by the sharebank of the
1028  * selected device.
1029  * FIXME: Currently always returns 0x7f.
1030  * THINK: Might not be needed, if we assign all shareable objects to
1031  *        a `udata_shr' section and let the linker do the rest...
1032  * Tried it, but yields `no target memory available' for pic16f877...
1033  *-----------------------------------------------------------------*/
1034 int pic14_getSharebankAddress(void)
1035 {
1036         int sharebankAddress = 0x7f;
1037         /* If total RAM is less than 0x7f as with 16f84 then reduce
1038          * sharebankAddress to fit */
1039         if ((unsigned)sharebankAddress > pic14_getMaxRam())
1040                 sharebankAddress = (int)pic14_getMaxRam();
1041         return sharebankAddress;
1042 }
1043