When no max ram set validate full memory range.
[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
23 #include "common.h"   // Include everything in the SDCC src directory
24 #include "newalloc.h"
25
26
27 #include "pcode.h"
28 #include "ralloc.h"
29 #include "device.h"
30
31 #if defined(__BORLANDC__) || defined(_MSC_VER)
32 #define STRCASECMP stricmp
33 #else
34 #define STRCASECMP strcasecmp
35 #endif
36
37 static PIC_device Pics[] = {
38         {
39                 {"p16f627", "16f627", "pic16f627", "f627"}, /* processor name */
40                 (memRange *)NULL,
41                 (memRange *)NULL,
42                 0,                /* max ram address (calculated) */
43                 0x1ff,            /* default max ram address */
44                 0x80,             /* Bank Mask */
45         },
46         
47         {
48         {"p16f628", "16f628", "pic16f628", "f628"},
49                 (memRange *)NULL,
50                 (memRange *)NULL,
51                 0,
52                 0x1ff,
53                 0x80,
54         },
55         
56         {
57                 {"p16f84", "16f84", "pic16f84", "f84"},
58                         (memRange *)NULL,
59                         (memRange *)NULL,
60                         0,
61                         0xcf,
62                         0x80,
63         },
64         
65         {
66                 {"p16f873", "16f873", "pic16f873", "f873"},
67                         (memRange *)NULL,
68                         (memRange *)NULL,
69                         0,
70                         0x1ff,
71                         0x180,
72         },
73
74         {
75                 {"p16f877", "16f877", "pic16f877", "f877"},
76                         (memRange *)NULL,
77                         (memRange *)NULL,
78                         0,
79                         0x1ff,
80                         0x180,
81         },
82         
83         {
84                 {"p16f819", "16f819", "pic16f819", "f819"},
85                         (memRange *)NULL,
86                         (memRange *)NULL,
87                         0,
88                         0x1ff,
89                         0x80,
90         },
91
92 };
93
94 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
95
96 static PIC_device *pic=NULL;
97
98 AssignedMemory *finalMapping=NULL;
99
100 #define CONFIG_WORD_ADDRESS 0x2007
101 #define DEFAULT_CONFIG_WORD 0x3fff
102
103 static unsigned int config_word = DEFAULT_CONFIG_WORD;
104
105 void addMemRange(memRange *r, int type)
106 {
107         int i;
108         int alias = r->alias;
109         
110         if (pic->maxRAMaddress < 0) {
111                 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
112                 return;
113         }
114         
115         do {
116                 for (i=r->start_address; i<= r->end_address; i++) {
117                         if ((i|alias) <= pic->maxRAMaddress) {
118                                 finalMapping[i | alias].isValid = 1;
119                                 finalMapping[i | alias].alias = r->alias;
120                                 finalMapping[i | alias].bank  = r->bank;
121                                 if(type) {
122                                         /* hack for now */
123                                         finalMapping[i | alias].isSFR  = 1;
124                                 } else {
125                                         finalMapping[i | alias].isSFR  = 0;
126                                 }
127                         } else {
128                                 fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
129                                         __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
130                         }
131                 }
132                 
133                 /* Decrement alias */
134                 if (alias) {
135                         alias -= ((alias & (alias - 1)) ^ alias);
136                 } else {
137                         alias--;
138                 }
139                 
140         } while (alias >= 0);
141 }
142
143 void setMaxRAM(int size)
144 {
145         int i;
146         pic->maxRAMaddress = size;
147         
148         if (pic->maxRAMaddress < 0) {
149                 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
150                         pic->maxRAMaddress);
151                 return;
152         }
153         
154         finalMapping = Safe_calloc(1+pic->maxRAMaddress,
155                 sizeof(AssignedMemory));
156         
157         /* Now initialize the finalMapping array */
158         
159         for(i=0; i<=pic->maxRAMaddress; i++) {
160                 finalMapping[i].reg = NULL;
161                 finalMapping[i].isValid = 0;
162         }
163 }
164
165 /*-----------------------------------------------------------------*
166 *-----------------------------------------------------------------*/
167
168 int isREGinBank(regs *reg, int bank)
169 {
170         
171         if(!reg || !pic)
172                 return 0;
173         
174         if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
175                 return 1;
176         
177         return 0;
178 }
179
180 /*-----------------------------------------------------------------*
181 *-----------------------------------------------------------------*/
182 int REGallBanks(regs *reg)
183 {
184         
185         if(!reg || !pic)
186                 return 0;
187         
188         return ((reg->address | reg->alias) & pic->bankMask);
189         
190 }
191
192 /*-----------------------------------------------------------------*
193 *-----------------------------------------------------------------*/
194
195 /*
196 *  dump_map -- debug stuff
197 */
198
199 void dump_map(void)
200 {
201         int i;
202         
203         for(i=0; i<=pic->maxRAMaddress; i++) {
204                 //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
205                 
206                 if(finalMapping[i].isValid) {
207                         fprintf(stderr,"addr: 0x%02x",i);
208                         if(finalMapping[i].isSFR)
209                                 fprintf(stderr," isSFR");
210                         if(finalMapping[i].reg) 
211                                 fprintf( stderr, "  reg %s", finalMapping[i].reg->name);
212                         fprintf(stderr, "\n");
213                 }
214         }
215         
216 }
217
218 void dump_sfr(FILE *of)
219 {
220         
221         int start=-1;
222         int addr=0;
223         int bank_base;
224         static int udata_flag=0;
225         
226         //dump_map();   /* display the register map */
227         //fprintf(stdout,";dump_sfr  \n");
228         if (pic->maxRAMaddress < 0) {
229                 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
230                 return;
231         }
232         
233         do {
234                 
235                 if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
236                         
237                         if(start<0)
238                                 start = addr;
239                 } else {
240                         if(start>=0) {
241                                 
242                         /* clear the lower 7-bits of the start address of the first
243                         * variable declared in this bank. The upper bits for the mid
244                         * range pics are the bank select bits.
245                                 */
246                                 
247                                 bank_base = start & 0xfff8;
248                                 
249                                 /* The bank number printed in the cblock comment tacitly
250                                 * assumes that the first register in the contiguous group
251                                 * of registers represents the bank for the whole group */
252                                 
253                                 if ( (start != addr) && (!udata_flag) ) {
254                                         udata_flag = 1;
255                                         //fprintf(of,"\tudata\n");
256                                 }
257                                 
258                                 for( ; start < addr; start++) {
259                                         if((finalMapping[start].reg) && 
260                                                 (!finalMapping[start].reg->isEmitted) &&
261                                                 (!finalMapping[start].instance) && 
262                                                 (!finalMapping[start].isSFR)) {
263                                                 
264                                                 if (finalMapping[start].reg->isFixed) {
265                                                         unsigned i;
266                                                         for (i=0; i<finalMapping[start].reg->size; i++) {
267                                                                 fprintf(of,"%s\tEQU\t0x%04x\n",
268                                                                         finalMapping[start].reg->name, 
269                                                                         finalMapping[start].reg->address+i);
270                                                         }
271                                                 } else {
272                                                         fprintf(of,"%s\tres\t%i\n",
273                                                                 finalMapping[start].reg->name, 
274                                                                 finalMapping[start].reg->size);
275                                                 }
276                                                 finalMapping[start].reg->isEmitted = 1;
277                                         }
278                                 }
279                                 
280                                 start = -1;
281                         }
282                         
283                 }
284                 
285                 addr++;
286                 
287         } while(addr <= pic->maxRAMaddress);
288         
289         
290 }
291
292 /*-----------------------------------------------------------------*
293 *  void list_valid_pics(int ncols, int list_alias)
294 *
295 * Print out a formatted list of valid PIC devices
296 *
297 * ncols - number of columns in the list.
298 *
299 * list_alias - if non-zero, print all of the supported aliases
300 *              for a device (e.g. F84, 16F84, etc...)
301 *-----------------------------------------------------------------*/
302 void list_valid_pics(int ncols, int list_alias)
303 {
304         int col,longest;
305         int i,j,k,l;
306         
307         if(list_alias)
308                 list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
309         
310         /* decrement the column number if it's greater than zero */
311         ncols = (ncols > 1) ? ncols-1 : 4;
312         
313         /* Find the device with the longest name */
314         for(i=0,longest=0; i<num_of_supported_PICS; i++) {
315                 for(j=0; j<=list_alias; j++) {
316                         k = strlen(Pics[i].name[j]);
317                         if(k>longest)
318                                 longest = k;
319                 }
320         }
321         
322         col = 0;
323         
324         for(i=0;  i < num_of_supported_PICS; i++) {
325                 j = 0;
326                 do {
327                         
328                         fprintf(stderr,"%s", Pics[i].name[j]);
329                         if(col<ncols) {
330                                 l = longest + 2 - strlen(Pics[i].name[j]);
331                                 for(k=0; k<l; k++)
332                                         fputc(' ',stderr);
333                                 
334                                 col++;
335                                 
336                         } else {
337                                 fputc('\n',stderr);
338                                 col = 0;
339                         }
340                         
341                 } while(++j<list_alias);
342                 
343         }
344         if(col != ncols)
345                 fputc('\n',stderr);
346         
347 }
348
349 /*-----------------------------------------------------------------*
350 *  
351 *-----------------------------------------------------------------*/
352 PIC_device *find_device(char *name)
353 {
354         
355         int i,j;
356         
357         if(!name)
358                 return NULL;
359         
360         for(i = 0; i<num_of_supported_PICS; i++) {
361                 
362                 for(j=0; j<PROCESSOR_NAMES; j++)
363                         if(!STRCASECMP(Pics[i].name[j], name) )
364                                 return &Pics[i];
365         }
366         
367         /* not found */
368         return NULL; 
369 }
370
371 /*-----------------------------------------------------------------*
372 *  
373 *-----------------------------------------------------------------*/
374 void init_pic(char *pic_type)
375 {
376         pic = find_device(pic_type);
377         
378         if(!pic) {
379                 if(pic_type)
380                         fprintf(stderr, "'%s' was not found.\n", pic_type);
381                 else
382                         fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
383                 
384                 fprintf(stderr,"Valid devices are:\n");
385                 
386                 list_valid_pics(4,0);
387                 exit(1);
388         }
389         
390         pic->maxRAMaddress = -1;
391 }
392
393 /*-----------------------------------------------------------------*
394 *  
395 *-----------------------------------------------------------------*/
396 int picIsInitialized(void)
397 {
398         if(pic && pic->maxRAMaddress > 0)
399                 return 1;
400         
401         return 0;
402         
403 }
404
405 /*-----------------------------------------------------------------*
406 *  char *processor_base_name(void) - Include file is derived from this.
407 *-----------------------------------------------------------------*/
408 char *processor_base_name(void)
409 {
410         
411         if(!pic)
412                 return NULL;
413         
414         return pic->name[0];
415 }
416
417 int isSFR(int address)
418 {
419         
420         if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
421                 return 0;
422         
423         return 1;
424         
425 }
426
427 /*-----------------------------------------------------------------*
428 *-----------------------------------------------------------------*/
429 int validAddress(int address, int reg_size)
430 {
431         int i;
432         
433         if (pic->maxRAMaddress < 0) {
434                 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
435                 return 0;
436         }
437         //  fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
438         if(address > pic->maxRAMaddress)
439                 return 0;
440         
441         for (i=0; i<reg_size; i++)
442                 if(!finalMapping[address + i].isValid || 
443                         finalMapping[address+i].reg ||
444                         finalMapping[address+i].isSFR )
445                         return 0;
446                 
447                 return 1;
448 }
449
450 /*-----------------------------------------------------------------*
451 *-----------------------------------------------------------------*/
452 void mapRegister(regs *reg)
453 {
454         
455         unsigned i;
456         int alias;
457         
458         if(!reg || !reg->size) {
459                 fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
460                 return;
461         }
462         
463         if (pic->maxRAMaddress < 0) {
464                 fprintf(stderr, "missing \"#pragma maxram\" setting\n");
465                 return;
466         }
467         
468         for(i=0; i<reg->size; i++) {
469                 
470                 alias = finalMapping[reg->address].alias;
471                 reg->alias = alias;
472                 
473                 do {
474                         
475                         //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
476                         
477                         finalMapping[reg->address + alias + i].reg = reg;
478                         finalMapping[reg->address + alias + i].instance = i;
479                         
480                         /* Decrement alias */
481                         if(alias)
482                                 alias -= ((alias & (alias - 1)) ^ alias);
483                         else
484                                 alias--;
485                         
486                 } while (alias>=0);
487         }
488         
489         //fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
490         
491         reg->isMapped = 1;
492         
493 }
494
495 /*-----------------------------------------------------------------*
496 *-----------------------------------------------------------------*/
497 int assignRegister(regs *reg, int start_address)
498 {
499         int i;
500         
501         //fprintf(stderr,"%s -  %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
502         if(reg->isFixed) {
503                 
504                 if (validAddress(reg->address,reg->size)) {
505                         //fprintf(stderr,"%s -  %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
506                         mapRegister(reg);
507                         return reg->address;
508                 }
509                 
510                 if( isSFR(reg->address)) {
511                         mapRegister(reg);
512                         return reg->address;
513                 }
514                 
515                 //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
516                 //    reg->address, reg->name);
517                 
518         } else {
519                 
520         /* This register does not have a fixed address requirement
521         * so we'll search through all availble ram address and
522                 * assign the first one */
523                 
524                 for (i=start_address; i<=pic->maxRAMaddress; i++) {
525                         
526                         if (validAddress(i,reg->size)) {
527                                 reg->address = i;
528                                 mapRegister(reg);
529                                 return i;
530                         }
531                 }
532                 
533                 fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
534                 
535         }
536         
537         return -1;
538 }
539
540 /*-----------------------------------------------------------------*
541 *-----------------------------------------------------------------*/
542 void assignFixedRegisters(set *regset)
543 {
544         regs *reg;
545         
546         for (reg = setFirstItem(regset) ; reg ; 
547         reg = setNextItem(regset)) {
548                 
549                 if(reg->isFixed) 
550                         assignRegister(reg,0);
551         }
552         
553 }
554
555 /*-----------------------------------------------------------------*
556 *-----------------------------------------------------------------*/
557 void assignRelocatableRegisters(set *regset, int used)
558 {
559         
560         regs *reg;
561         int address = 0;
562         
563         for (reg = setFirstItem(regset) ; reg ; 
564         reg = setNextItem(regset)) {
565                 
566                 //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
567                 
568                 if((!reg->isFixed) && ( used || reg->wasUsed)) {
569                         /* If register have been reused then shall not print it a second time. */
570                         set *s;
571                         int done = 0;
572                         for (s = regset; s; s = s->next) {
573                                 regs *r;
574                                 r = s->item;
575                                 if (r == reg)
576                                         break;
577                                 if((!r->isFixed) && ( used || r->wasUsed)) {
578                                         if (r->rIdx == reg->rIdx) {
579                                                 reg->address = r->address;
580                                                 done = 1;
581                                                 break;
582                                         }
583                                 }
584                         }
585                         if (!done)
586                                 address = assignRegister(reg,address);
587                 }
588         }
589         
590 }
591
592
593 /*-----------------------------------------------------------------*
594 *  void assignConfigWordValue(int address, int value)
595 *
596 * All midrange PICs have one config word at address 0x2007.
597 * This routine will assign a value to that address.
598 *
599 *-----------------------------------------------------------------*/
600
601 void assignConfigWordValue(int address, int value)
602 {
603         if(CONFIG_WORD_ADDRESS == address)
604                 config_word = value;
605         
606         //fprintf(stderr,"setting config word to 0x%x\n",value);
607         
608 }
609 /*-----------------------------------------------------------------*
610 * int getConfigWord(int address)
611 *
612 * Get the current value of the config word.
613 *
614 *-----------------------------------------------------------------*/
615
616 int getConfigWord(int address)
617 {
618         if(CONFIG_WORD_ADDRESS == address)
619                 return config_word;
620         
621         else
622                 return 0;
623         
624 }
625
626 /*-----------------------------------------------------------------*
627 *  
628 *-----------------------------------------------------------------*/
629 void setDefMaxRam(void)
630 {
631         unsigned i;
632         setMaxRAM(pic->defMaxRAMaddrs); /* Max RAM has not been included, so use default setting */
633         /* Validate full memory range for use by general purpose RAM */
634         for (i=pic->defMaxRAMaddrs; i--; ) {
635                 finalMapping[i].isValid = 1;
636         }
637 }
638
639 /*-----------------------------------------------------------------*
640 *  
641 *-----------------------------------------------------------------*/
642 unsigned getMaxRam(void)
643 {
644         return pic->defMaxRAMaddrs;
645 }