* src/pic/glue.h,src/pic16/glue.h: added prototypes
[fw/sdcc] / src / pic16 / device.c
1 /*-------------------------------------------------------------------------
2
3   device.c - Accomodates subtle variations in PIC16 devices
4
5    Written By -  Scott Dattalo scott@dattalo.com
6    Ported to PIC16 By -  Martin Dubuc m.dubuc@rogers.com
7
8    This program is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 -------------------------------------------------------------------------*/
22
23
24 /*
25         VR - Began writing code to make PIC16 C source files independent from
26         the header file (created by the inc2h.pl)
27
28         - adding maximum RAM memory into PIC_Device structure
29
30 */
31
32 #include <stdio.h>
33
34 #include "common.h"   // Include everything in the SDCC src directory
35 #include "newalloc.h"
36
37
38 #include "main.h"
39 #include "pcode.h"
40 #include "ralloc.h"
41 #include "device.h"
42
43
44 static PIC16_device Pics16[] = {
45 /* Device descriptions are now generated by scripts.
46  * For simplicities sake we just include them here.
47  * Manual overrides may be inserted on top of devices.inc
48  * or right here BEFORE inclusion of devices.inc. */
49 #include "devices.inc"
50 };
51
52 static int num_of_supported_PICS = sizeof(Pics16)/sizeof(PIC16_device);
53
54 stats_t statistics = { 0, 0, 0, 0 };
55
56 #define DEFAULT_PIC "452"
57
58 PIC16_device *pic16=NULL;
59 unsigned int stackPos = 0;
60 unsigned int stackLen = 0;
61
62 void pic16_setMaxRAM(int size)
63 {
64         pic16->maxRAMaddress = size;
65         stackPos = pic16->RAMsize-1;
66
67         if (pic16->maxRAMaddress < 0) {
68                 fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
69                         pic16->maxRAMaddress);
70           return;
71         }
72 }
73
74 extern char *iComments2;
75
76 void pic16_dump_equates(FILE *of, set *equs)
77 {
78   regs *r;
79
80         r = setFirstItem(equs);
81         if(!r)return;
82         
83         fprintf(of, "%s", iComments2);
84         fprintf(of, ";\tEquates to used internal registers\n");
85         fprintf(of, "%s", iComments2);
86         
87         for(; r; r = setNextItem(equs)) {
88                 fprintf(of, "%s\tequ\t0x%02x\n", r->name, r->address);
89         }
90 }
91
92
93 void pic16_dump_access(FILE *of, set *section)
94 {
95   regs *r;
96
97         r = setFirstItem(section);
98         if(!r)return;
99         
100         fprintf(of, "%s", iComments2);
101         fprintf(of, ";\tAccess bank symbols\n");
102         fprintf(of, "%s", iComments2);
103         
104         fprintf(of, "\tudata_acs\n");
105         for(; r; r = setNextItem(section)) {
106                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
107                 statistics.adsize += r->size;
108         }
109 }
110
111
112 int regCompare(const void *a, const void *b)
113 {
114   const regs *const *i = a;
115   const regs *const *j = b;
116
117         /* sort primarily by the address */
118         if( (*i)->address > (*j)->address)return 1;
119         if( (*i)->address < (*j)->address)return -1;
120         
121         /* and secondarily by size */
122         /* register size sorting may have strange results use with care */
123         if( (*i)->size > (*j)->size)return 1;
124         if( (*i)->size < (*j)->size)return -1;
125         
126         /* finally if in same address and same size sort by name */
127         return (strcmp( (*i)->name, (*j)->name));
128
129   return 0;
130 }
131
132 int symCompare(const void *a, const void *b)
133 {
134   const symbol *const *i = a;
135   const symbol *const *j = b;
136
137         /* sort primarily by the address */
138         if( SPEC_ADDR((*i)->etype) > SPEC_ADDR((*j)->etype))return 1;
139         if( SPEC_ADDR((*i)->etype) < SPEC_ADDR((*j)->etype))return -1;
140         
141         /* and secondarily by size */
142         /* register size sorting may have strange results use with care */
143         if( getSize((*i)->etype) > getSize((*j)->etype))return 1;
144         if( getSize((*i)->etype) < getSize((*j)->etype))return -1;
145
146         /* finally if in same address and same size sort by name */
147         return (strcmp( (*i)->rname, (*j)->rname));
148
149   return 0;
150 }
151
152 void pic16_dump_usection(FILE *of, set *section, int fix)
153 {
154   static int abs_usection_no=0;
155   static unsigned int usection_no=0;
156   regs *r, *rprev;
157   unsigned int init_addr, i;
158   regs **rlist;
159   regs *r1;
160
161         /* put all symbols in an array */
162         if (!elementsInSet(section)) return;
163         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
164         r = rlist[0]; i = 0;
165         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
166                 rlist[i] = rprev; i++;
167         }
168         
169         if(!i) {
170                 if(rlist)Safe_free(rlist);
171           return;
172         }
173
174         /* sort symbols according to their address */
175         qsort(rlist, i  /*elementsInSet(section)*/, sizeof(regs *), regCompare);
176         
177         if(!fix) {
178
179 #define EMIT_SINGLE_UDATA_SECTION       0
180 #if EMIT_SINGLE_UDATA_SECTION
181                 fprintf(of, "\n\n\tudata\n");
182                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
183                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
184                         statistics.udsize += r->size;
185                 }
186 #else
187                 for(r = setFirstItem(section); r; r = setNextItem(section)) {
188                         //fprintf(of, "\nudata_%s_%s\tudata\n", moduleName, r->name);
189                         fprintf(of, "\nudata_%s_%u\tudata\n", moduleName, usection_no++);
190                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
191                         statistics.udsize += r->size;
192                 }
193 #endif
194         } else {
195           unsigned int j=0;
196           int deb_addr=0;
197
198                 rprev = NULL;
199                 init_addr = rlist[j]->address;
200                 deb_addr = init_addr;
201                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
202         
203                 for(j=0;j<i;j++) {
204                         r = rlist[j];
205                         if(j < i-1)r1 = rlist[j+1]; else r1 = NULL;
206                         
207                         init_addr = r->address;
208                         deb_addr = init_addr;
209                         
210                         if((rprev && (init_addr > (rprev->address + rprev->size)))) {
211                                 fprintf(of, "\n\nustat_%s_%02d\tudata\t0X%04X\n", moduleName, abs_usection_no++, init_addr);
212                         }
213
214                         if(r1 && (init_addr == r1->address)) {
215                                 fprintf(of, "\n%s\tres\t0\n", r->name);
216                         } else {
217                                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
218                                 deb_addr += r->size;
219                                 statistics.udsize += r->size;
220                         }
221                         
222                         rprev = r;
223                 }
224         }
225         Safe_free(rlist);
226 }
227
228 void pic16_dump_gsection(FILE *of, set *sections)
229 {
230   regs *r;
231   sectName *sname;
232
233         for(sname = setFirstItem(sections); sname; sname = setNextItem(sections)) {
234                 if(!strcmp(sname->name, "access"))continue;
235                 fprintf(of, "\n\n%s\tudata\n", sname->name);
236
237                 for(r=setFirstItem(sname->regsSet); r; r=setNextItem(sname->regsSet)) {
238 #if 0
239                         fprintf(stderr, "%s:%d emitting variable %s for section %s (%p)\n", __FILE__, __LINE__,
240                                 r->name, sname->name, sname);
241 #endif
242                         fprintf(of, "%s\tres\t%d\n", r->name, r->size);
243                         statistics.udsize += r->size;
244                 }
245         }
246 }
247
248
249 /* forward declaration */
250 void pic16_printIval(symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
251 extern void pic16_pCodeConstString(char *name, char *value);
252
253 void pic16_dump_isection(FILE *of, set *section, int fix)
254 {
255   static int abs_isection_no=0;
256   symbol *s, *sprev;
257   unsigned int init_addr, i;
258   symbol **slist;
259
260         /* put all symbols in an array */
261         if (!elementsInSet(section)) return;
262         slist = Safe_calloc(elementsInSet(section), sizeof(symbol *));
263         s = slist[0]; i = 0;
264         for(sprev = setFirstItem(section); sprev; sprev = setNextItem(section)) {
265                 slist[i] = sprev; i++;
266         }
267         
268         if(!i) {
269                 if(slist)Safe_free(slist);
270           return;
271         }
272
273         /* sort symbols according to their address */
274         qsort(slist, i, sizeof(symbol *), symCompare);
275         
276         pic16_initDB();
277
278         if(!fix) {
279                 fprintf(of, "\n\n\tidata\n");
280                 for(s = setFirstItem(section); s; s = setNextItem(section)) {
281
282                         if(s->ival) {
283                                 fprintf(of, "%s", s->rname);
284                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
285                                 pic16_flushDB('f', (void *)of);
286                         } else {
287                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
288                                         && SPEC_CVAL (s->etype).v_char) {
289
290 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
291                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
292                                 } else {
293                                         assert(0);
294                                 }
295                         }
296                                 
297                 }
298         } else {
299           unsigned int j=0;
300           symbol *s1;
301           
302                 sprev = NULL;
303                 init_addr = SPEC_ADDR(slist[j]->etype);
304                 fprintf(of, "\n\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
305         
306                 for(j=0;j<i;j++) {
307                         s = slist[j];
308                         if(j < i-1)s1 = slist[j+1]; else s1 = NULL;
309                         
310                         init_addr = SPEC_ADDR(s->etype);
311
312                         if(sprev && (init_addr > (SPEC_ADDR(sprev->etype) + getSize(sprev->etype)))) {
313                                 fprintf(of, "\nistat_%s_%02d\tidata\t0X%04X\n", moduleName, abs_isection_no++, init_addr);
314                         }
315
316                         if(s->ival) {
317                                 fprintf(of, "%s", s->rname);
318                                 pic16_printIval(s, s->type, s->ival, 'f', (void *)of);
319                                 pic16_flushDB('f', (void *)of);
320                         } else {
321                                 if (IS_ARRAY (s->type) && IS_CHAR (s->type->next)
322                                         && SPEC_CVAL (s->etype).v_char) {
323
324 //                                      fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, s->rname);
325                                         pic16_pCodeConstString(s->rname , SPEC_CVAL (s->etype).v_char);
326                                 } else {
327                                         assert(0);
328                                 }
329                         }
330
331
332                         sprev = s;
333                 }
334         }
335         Safe_free(slist);
336 }
337
338
339 void pic16_dump_int_registers(FILE *of, set *section)
340 {
341   regs *r, *rprev;
342   int i;
343   regs **rlist;
344
345         /* put all symbols in an array */
346         if (!elementsInSet(section)) return;
347         rlist = Safe_calloc(elementsInSet(section), sizeof(regs *));
348         r = rlist[0]; i = 0;
349         for(rprev = setFirstItem(section); rprev; rprev = setNextItem(section)) {
350                 rlist[i] = rprev; i++;
351         }
352
353         /* sort symbols according to their address */
354         qsort(rlist, elementsInSet(section), sizeof(regs *), regCompare);
355         
356         if(!i) {
357                 if(rlist)Safe_free(rlist);
358           return;
359         }
360         
361         fprintf(of, "\n\n; Internal registers\n");
362         
363         fprintf(of, "%s\tudata_ovr\t0x0000\n", ".registers");
364         for(r = setFirstItem(section); r; r = setNextItem(section)) {
365                 fprintf(of, "%s\tres\t%d\n", r->name, r->size);
366                 statistics.intsize += r->size;
367         }
368
369         Safe_free(rlist);
370 }
371
372
373 #ifdef WORDS_BIGENDIAN
374   #define _ENDIAN(x)  (3-x)
375 #else
376   #define _ENDIAN(x)  (x)
377 #endif
378
379
380 /*-----------------------------------------------------------------*
381  *  void pic16_list_valid_pics(int ncols, int list_alias)
382  *
383  * Print out a formatted list of valid PIC devices
384  *
385  * ncols - number of columns in the list.
386  *
387  * list_alias - if non-zero, print all of the supported aliases
388  *              for a device (e.g. F84, 16F84, etc...)
389  *-----------------------------------------------------------------*/
390 void pic16_list_valid_pics(int ncols, int list_alias)
391 {
392   int col,longest;
393   int i,j,k,l;
394
395   if(list_alias)
396     list_alias = sizeof(Pics16[0].name) / sizeof(Pics16[0].name[0]);
397
398   /* decrement the column number if it's greater than zero */
399   ncols = (ncols > 1) ? ncols-1 : 4;
400
401   /* Find the device with the longest name */
402   for(i=0,longest=0; i<num_of_supported_PICS; i++) {
403     for(j=0; j<=list_alias; j++) {
404       k = strlen(Pics16[i].name[j]);
405       if(k>longest)
406         longest = k;
407     }
408   }
409
410   col = 0;
411
412   for(i=0;  i < num_of_supported_PICS; i++) {
413     j = 0;
414     do {
415
416       fprintf(stderr,"%s", Pics16[i].name[j]);
417       if(col<ncols) {
418         l = longest + 2 - strlen(Pics16[i].name[j]);
419         for(k=0; k<l; k++)
420           fputc(' ',stderr);
421
422         col++;
423
424       } else {
425         fputc('\n',stderr);
426         col = 0;
427       }
428
429     } while(++j<list_alias);
430
431   }
432   if(col != ncols)
433     fputc('\n',stderr);
434
435 }
436
437 /*-----------------------------------------------------------------*
438  *  
439  *-----------------------------------------------------------------*/
440 PIC16_device *pic16_find_device(char *name)
441 {
442
443   int i,j;
444
445   if(!name)
446     return NULL;
447
448   for(i = 0; i<num_of_supported_PICS; i++) {
449
450     for(j=0; j<PROCESSOR_NAMES; j++)
451       if(!STRCASECMP(Pics16[i].name[j], name) )
452         return &Pics16[i];
453   }
454
455   /* not found */
456   return NULL; 
457 }
458
459 /*-----------------------------------------------------------------*
460  *  
461  *-----------------------------------------------------------------*/
462 void pic16_init_pic(char *pic_type)
463 {
464         pic16 = pic16_find_device(pic_type);
465
466         if(!pic16) {
467                 if(pic_type)
468                         fprintf(stderr, "'%s' was not found.\n", pic_type);
469                 else
470                         fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
471
472                 fprintf(stderr,"Valid devices are:\n");
473
474                 pic16_list_valid_pics(4,0);
475                 exit(1);
476         }
477
478 //      printf("PIC processor found and initialized: %s\n", pic_type);
479         pic16_setMaxRAM( 0xfff  );
480 }
481
482 /*-----------------------------------------------------------------*
483  *  
484  *-----------------------------------------------------------------*/
485 int pic16_picIsInitialized(void)
486 {
487   if(pic16 && pic16->maxRAMaddress > 0)
488     return 1;
489
490   return 0;
491
492 }
493
494 /*-----------------------------------------------------------------*
495  *  char *pic16_processor_base_name(void) - Include file is derived from this.
496  *-----------------------------------------------------------------*/
497 char *pic16_processor_base_name(void)
498 {
499
500   if(!pic16)
501     return NULL;
502
503   return pic16->name[0];
504 }
505
506 #define DEBUG_CHECK     0
507
508 /*
509  * return 1 if register wasn't found and added, 0 otherwise
510  */
511 int checkAddReg(set **set, regs *reg)
512 {
513   regs *tmp;
514
515
516         if(!reg)return 0;
517 #if DEBUG_CHECK
518         fprintf(stderr, "%s: about to insert REGister: %s ... ", __FUNCTION__, reg->name);
519 #endif
520
521         for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
522                 if(!strcmp(tmp->name, reg->name))break;
523         }
524         
525         if(!tmp) {
526                 addSet(set, reg);
527 #if DEBUG_CHECK
528                 fprintf(stderr, "added\n");
529 #endif
530                 return 1;
531         }
532
533 #if DEBUG_CHECK
534         fprintf(stderr, "already added\n");
535 #endif
536   return 0;
537 }
538
539 int checkAddSym(set **set, symbol *sym)
540 {
541   symbol *tmp;
542
543         if(!sym)return 0;
544 #if DEBUG_CHECK
545         fprintf(stderr, "%s: about to add SYMbol: %s ... ", __FUNCTION__, sym->name);
546 #endif
547
548         for(tmp = setFirstItem( *set ); tmp; tmp = setNextItem(*set)) {
549                 if(!strcmp(tmp->name, sym->name))break;
550         }
551         
552         if(!tmp) {
553                 addSet(set, sym);
554 #if DEBUG_CHECK
555                 fprintf(stderr, "added\n");
556 #endif
557                 return 1;
558         }
559
560 #if DEBUG_CHECK
561         fprintf(stderr, "already added\n");
562 #endif
563
564   return 0;
565 }
566
567 int checkSym(set *set, symbol *sym)
568 {
569   symbol *tmp;
570
571         if(!sym)return 0;
572         
573 #if DEUG_CHECK
574         fprintf(stderr, "%s: about to search for SYMbol: %s ... ", __FUNCTION__, sym->name);
575 #endif
576
577         for(tmp = setFirstItem( set ); tmp; tmp = setNextItem( set )) {
578                 if(!strcmp(tmp->name, sym->name))break;
579         }
580         
581         if(!tmp) {
582 #if DEBUG_CHECK
583                 fprintf(stderr, "not found\n");
584 #endif
585                 return 0;
586         }
587
588 #if DEBUG_CHECK
589         fprintf(stderr, "found\n");
590 #endif
591
592   return 1;
593 }
594
595 /*-----------------------------------------------------------------*
596  * void pic16_groupRegistersInSection - add each register to its   *
597  *      corresponding section                                      *
598  *-----------------------------------------------------------------*/
599 void pic16_groupRegistersInSection(set *regset)
600 {
601   regs *reg;
602   sectSym *ssym;
603   int docontinue=0;
604
605         for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
606
607 #if 0
608                 fprintf(stderr, "%s:%d group registers in section, reg: %s (used: %d, %p)\n",
609                         __FILE__, __LINE__, reg->name, reg->wasUsed, reg);
610 #endif
611                 if((reg->wasUsed
612                         && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop))))
613                   ) {
614                         
615                         /* avoid grouping registers that have an initial value,
616                          * they will be added later in idataSymSet */
617                         if(reg->regop && (OP_SYMBOL(reg->regop)->ival && !OP_SYMBOL(reg->regop)->level))
618                                 continue;
619
620 #if 0
621                         fprintf(stderr, "%s:%d register %s alias:%d fix:%d ival=%i level=%i code=%i\n",
622                                 __FILE__, __LINE__, reg->name, reg->alias, reg->isFixed,
623                                         (reg->regop?(OP_SYMBOL(reg->regop)->ival?1:0):-1),
624                                         (reg->regop?(OP_SYMBOL(reg->regop)->level):-1),
625                                         (reg->regop?(IS_CODE(OP_SYM_ETYPE(reg->regop))):-1) );
626 #endif
627                         
628                         docontinue=0;
629                         for(ssym=setFirstItem(sectSyms);ssym;ssym=setNextItem(sectSyms)) {
630                                 if(!strcmp(ssym->name, reg->name)) {
631 //                                      fprintf(stderr, "%s:%d section found %s (%p) with var %s\n",
632 //                                                      __FILE__, __LINE__, ssym->section->name, ssym->section, ssym->name);
633                                         if(strcmp(ssym->section->name, "access")) {
634                                                 addSet(&ssym->section->regsSet, reg);
635                                                 docontinue=1;
636                                                 break;
637                                         } else {
638                                                 docontinue=0;
639                                                 reg->accessBank = 1;
640                                                 break;
641                                         }
642                                 }
643                         }
644
645                         if(docontinue)continue;
646
647 //                      fprintf(stderr, "%s:%d reg: %s\n", __FILE__, __LINE__, reg->name);
648
649                         if(reg->alias == 0x80) {
650                                 checkAddReg(&pic16_equ_data, reg);
651                         } else
652                         if(reg->isFixed) {
653                                 checkAddReg(&pic16_fix_udata, reg);
654                         } else
655                         if(!reg->isFixed) {
656                                 if(reg->pc_type == PO_GPR_TEMP)
657                                         checkAddReg(&pic16_int_regs, reg);
658                                 else {
659                                         if(reg->accessBank) {
660                                                 if(reg->alias != 0x40)
661                                                         checkAddReg(&pic16_acs_udata, reg);
662                                         } else
663                                                 checkAddReg(&pic16_rel_udata, reg);
664                                 }
665                         }
666                 }
667         }
668 }
669
670
671
672
673
674 /*-----------------------------------------------------------------*
675  *  void pic16_assignConfigWordValue(int address, int value)
676  *
677  * All high performance RISC CPU PICs have seven config word starting
678  * at address 0x300000.
679  * This routine will assign a value to that address.
680  *
681  *-----------------------------------------------------------------*/
682 void pic16_assignConfigWordValue(int address, unsigned int value)
683 {
684   int i;
685
686         for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart+1;i++) {
687                 if((address == pic16->cwInfo.confAddrStart+i)
688                   && (pic16->cwInfo.crInfo[i].mask != -1)) {
689
690 #if 0
691                         fprintf(stderr, "setting location 0x%X to value 0x%x\tmask: 0x%x\ttest: 0x%x\n",
692                                 /*address*/ pic16->cwInfo.confAddrStart+i, (~value)&0xff,
693                                         pic16->cwInfo.crInfo[i].mask,
694                                         (pic16->cwInfo.crInfo[i].mask) & (~value));
695 #endif
696
697 #if 0
698                         if((((pic16->cwInfo.crInfo[i].mask) & (~value))&0xff) != ((~value)&0xff)) {
699                                 fprintf(stderr, "%s:%d a wrong value has been given for configuration register 0x%x\n",
700                                         __FILE__, __LINE__, address);
701                                         return;
702                         }
703 #endif
704
705                         pic16->cwInfo.crInfo[i].value = value;
706                         pic16->cwInfo.crInfo[i].emit = 1;
707                         return;
708                 }
709         }
710 }
711
712 void pic16_assignIdByteValue(int address, char value)
713 {
714   int i;
715
716         for(i=0;i<pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart+1;i++) {
717                 if(address == pic16->idInfo.idAddrStart+i) {
718                         pic16->idInfo.irInfo[i].value = value;
719                         pic16->idInfo.irInfo[i].emit = 1;
720                 }
721         }
722 }