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