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