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