2004-01-06 Vangelis Rokas <vrokas@otenet.gr>
[fw/sdcc] / src / pic16 / device.c
index b04f455efebc695c1d919ee035bc761f252d7cde..b7eb78657786f8251a8c1edf38396ff1051e0bb7 100644 (file)
@@ -1,6 +1,7 @@
 /*-------------------------------------------------------------------------
 
-   device.c - Accomodates subtle variations in PIC16 devices
+  device.c - Accomodates subtle variations in PIC16 devices
+
    Written By -  Scott Dattalo scott@dattalo.com
    Ported to PIC16 By -  Martin Dubuc m.dubuc@rogers.com
 
    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 -------------------------------------------------------------------------*/
 
+
+/*
+       VR - Began writing code to make PIC16 C source files independent from
+       the header file (created by the inc2h.pl)
+
+       - adding maximum RAM memory into PIC_Device structure
+
+*/
+
 #include <stdio.h>
 
 #include "common.h"   // Include everything in the SDCC src directory
 
 static PIC_device Pics[] = {
   {
-    {"p18f242", "18f242", "pic18f242", "f242"},
-    (memRange *)NULL,
-    (memRange *)NULL,
+    {"p18f242", "18f242", "pic18f242", "f242"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x300,                                             // bank mask
+    0x300,                                             // RAMsize
+    0
+  },
+
+  {
+    {"p18f252", "18f252", "pic18f252", "f252"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x600,                                             // bank mask
+    0x600,                                             // RAMsize
+    0
+  },
+
+  {
+    {"p18f442", "18f442", "pic18f442", "f442"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x300,                                             // bank mask
+    0x300,                                             // RAMsize
+    0
+  },
+
+  {
+    {"p18f452", "18f452", "pic18f452", "f452"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x600,                                             // bank mask
+    0x600,                                             // RAMsize
+    0
+  },
+
+  {
+    {"p18f248", "18f248", "pic18f248", "f248"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
     0,
-    0x300,
+    0x300,                                             // bank mask
+    0x300,                                             // RAMsize
+    0
   },
 
   {
-    {"p18f252", "18f252", "pic18f252", "f252"},
-    (memRange *)NULL,
-    (memRange *)NULL,
+    {"p18f258", "18f258", "pic18f258", "f258"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
     0,
-    0x600,
+    0x600,                                             // bank mask
+    0x600,                                             // RAMsize
+    0
   },
 
   {
-    {"p18f442", "18f442", "pic18f442", "f442"},
-    (memRange *)NULL,
-    (memRange *)NULL,
+    {"p18f448", "18f448", "pic18f448", "f448"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
     0,
-    0x300,
+    0x300,                                             // bank mask
+    0x300,                                             // RAMsize
+    0
   },
 
   {
-    {"p18f452", "18f452", "pic18f452", "f452"},
-    (memRange *)NULL,
-    (memRange *)NULL,
+    {"p18f458", "18f458", "pic18f458", "f458"},                // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x600,                                             // bank mask
+    0x600,                                             // RAMsize
+    0
+  },
+
+  {
+    {"p18f6520", "18f6520", "pic18f6520", "f6520"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x800,                                             // bank mask
+    0x800,                                             // RAMsize
+    1
+  },
+
+  {
+    {"p18f6620", "18f6620", "pic18f6620", "f6620"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0xf00,                                             // bank mask
+    0xf00,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f6680", "18f6680", "pic18f6680", "f6680"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0xc00,                                             // bank mask
+    0xc00,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f6720", "18f6720", "pic18f6720", "f6720"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0xf00,                                             // bank mask
+    0xf00,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f8520", "18f8520", "pic18f8520", "f8520"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0x800,                                             // bank mask
+    0x800,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f8620", "18f8620", "pic18f8620", "f8620"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0xf00,                                             // bank mask
+    0xf00,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f8680", "18f8680", "pic18f8680", "f8680"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
+    0,
+    0xc00,                                             // bank mask
+    0x800,                                             // RAMsize
+    1
+  },
+  {
+    {"p18f8720", "18f8720", "pic18f8720", "f8720"},    // aliases
+    (memRange *)NULL,                                  // ram memory map
+    (memRange *)NULL,                                  // sfr memory map
     0,
-    0x600,
+    0xf00,                                             // bank mask
+    0xf00,                                             // RAMsize
+    1
   },
 
 };
 
 static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
 
-#define DEFAULT_PIC "f452"
+#define DEFAULT_PIC "452"
 
 static PIC_device *pic=NULL;
 
-AssignedMemory *pic16_finalMapping=NULL;
+//AssignedMemory *pic16_finalMapping=NULL;
+//int pic16_finalMappingSize=0;
 
 #define DEFAULT_CONFIG_BYTE 0xff
 
@@ -125,48 +258,12 @@ static unsigned int config6h_word = DEFAULT_CONFIG6H_WORD;
 static unsigned int config7l_word = DEFAULT_CONFIG7L_WORD;
 static unsigned int config7h_word = DEFAULT_CONFIG7H_WORD;
 
-void pic16_addMemRange(memRange *r, int type)
-{
-  int i;
-  int alias = r->alias;
-
-  if (pic->maxRAMaddress < 0) {
-    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
-    return;
-  }
-
-  do {
-    for (i=r->start_address; i<= r->end_address; i++) {
-      if ((i|alias) <= pic->maxRAMaddress) {
-       pic16_finalMapping[i | alias].isValid = 1;
-       pic16_finalMapping[i | alias].alias = r->alias;
-       pic16_finalMapping[i | alias].bank  = r->bank;
-       if(type) {
-         /* hack for now */
-         pic16_finalMapping[i | alias].isSFR  = 1;
-       } else {
-         pic16_finalMapping[i | alias].isSFR  = 0;
-       }
-      } else {
-       fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
-               __FILE__,__FUNCTION__,(i|alias), pic->maxRAMaddress);
-      }
-    }
-
-    /* Decrement alias */
-    if (alias) {
-      alias -= ((alias & (alias - 1)) ^ alias);
-    } else {
-      alias--;
-    }
-
-  } while (alias >= 0);
-}
+unsigned int stackPos = 0;
 
 void pic16_setMaxRAM(int size)
 {
-  int i;
   pic->maxRAMaddress = size;
+  stackPos = pic->RAMsize-1;
 
   if (pic->maxRAMaddress < 0) {
     fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
@@ -174,20 +271,20 @@ void pic16_setMaxRAM(int size)
     return;
   }
 
-  pic16_finalMapping = Safe_calloc(1+pic->maxRAMaddress,
-                            sizeof(AssignedMemory));
+//  pic16_finalMapping = Safe_calloc(1+pic->maxRAMaddress,
+//                          sizeof(AssignedMemory));
 
   /* Now initialize the pic16_finalMapping array */
 
-  for(i=0; i<=pic->maxRAMaddress; i++) {
-    pic16_finalMapping[i].reg = NULL;
-    pic16_finalMapping[i].isValid = 0;
-  }
+//  for(i=0; i<=pic->maxRAMaddress; i++) {
+//    pic16_finalMapping[i].reg = NULL;
+//    pic16_finalMapping[i].isValid = 0;
+//  }
 }
 
 /*-----------------------------------------------------------------*
  *-----------------------------------------------------------------*/
-
+#if 0
 int pic16_isREGinBank(regs *reg, int bank)
 {
 
@@ -199,7 +296,7 @@ int pic16_isREGinBank(regs *reg, int bank)
 
   return 0;
 }
-
+#endif
 /*-----------------------------------------------------------------*
  *-----------------------------------------------------------------*/
 int pic16_REGallBanks(regs *reg)
@@ -208,7 +305,7 @@ int pic16_REGallBanks(regs *reg)
   if(!reg || !pic)
     return 0;
 
-  if (reg->address > pic->maxRAMaddress)
+  if ((int)reg->address > pic->maxRAMaddress)
     return 0;
 
   return 1;
@@ -221,7 +318,7 @@ int pic16_REGallBanks(regs *reg)
 /*
  *  pic16_dump_map -- debug stuff
  */
-
+#if 0
 void pic16_dump_map(void)
 {
   int i;
@@ -240,78 +337,42 @@ void pic16_dump_map(void)
   }
 
 }
+#endif
 
-void pic16_dump_cblock(FILE *of)
-{
-  int start=-1;
-  int addr=0;
-  int bank_base;
-
-  //pic16_dump_map();   /* display the register map */
-
-  if (pic->maxRAMaddress < 0) {
-    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
-    return;
-  }
-
-  do {
-
-    if(pic16_finalMapping[addr].reg && !pic16_finalMapping[addr].reg->isEmitted) {
-
-      if(start<0)
-       start = addr;
-    } else {
-      if(start>=0) {
-
-       /* clear the lower 7-bits of the start address of the first
-        * variable declared in this bank. The upper bits for the mid
-        * range pics are the bank select bits.
-        */
-
-       bank_base = start & 0xfff8;
-
-       /* The bank number printed in the cblock comment tacitly
-        * assumes that the first register in the contiguous group
-        * of registers represents the bank for the whole group */
-
-        if ((pic16_finalMapping[start].bank == 0 && start <= 0x7f) ||
-            pic16_finalMapping[start].isSFR)
-         fprintf(of,"  cblock  0X%04X\t; Access Bank\n",start);
-        else
-         fprintf(of,"  cblock  0X%04X\t; Bank %d\n",start,pic16_finalMapping[start].bank);
-
-       for( ; start < addr; start++) {
-         if((pic16_finalMapping[start].reg) && !pic16_finalMapping[start].reg->isEmitted ) {
-           fprintf(of,"\t%s",pic16_finalMapping[start].reg->name);
-
-           /* If this register is aliased in multiple banks, then
-            * mangle the variable name with the alias address: */
-           if(pic16_finalMapping[start].alias & start)
-             fprintf(of,"_%x",bank_base);
-
-           if(pic16_finalMapping[start].instance)
-             fprintf(of,"_%d",pic16_finalMapping[start].instance);
-
-           
-           fputc('\n',of);
 
-           //pic16_finalMapping[start].reg->isEmitted = 1;
-         }
+void pic16_dump_section(FILE *of, char *sname, set *section, int fix)
+{
+  static int abs_section_no=0;
+  regs *r, *rprev;
+  int init_addr;
+
+       if(!fix) {
+               fprintf(of, "\n\n\tudata\n");
+               for(r = setFirstItem(section); r; r = setNextItem(section)) {
+                       fprintf(of, "%s\tres\t%d\n", r->name, r->size);
+               }
+       } else {
+               r = setFirstItem(section);
+               if(!r)return;
+               init_addr = r->address;
+               fprintf(of, "\n\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
+               
+               rprev = NULL;
+               for(; r; r = setNextItem(section)) {
+                       init_addr = r->address;
+
+                       if(rprev && (init_addr != (rprev->address + rprev->size))) {
+                               fprintf(of, "\nstatic_%s_%02d\tudata\t0X%04X\n", moduleName, abs_section_no++, init_addr);
+                       }
+
+                       fprintf(of, "%s\tres\t%d\n", r->name, r->size);
+                       rprev = r;
+               }
        }
+}
 
-       fprintf(of,"  endc\n");
-
-       start = -1;
-      }
-
-    }
-
-    addr++;
 
-  } while(addr <= pic->maxRAMaddress);
-  
 
-}
 
 /*-----------------------------------------------------------------*
  *  void pic16_list_valid_pics(int ncols, int list_alias)
@@ -397,21 +458,22 @@ PIC_device *pic16_find_device(char *name)
  *-----------------------------------------------------------------*/
 void pic16_init_pic(char *pic_type)
 {
-  pic = pic16_find_device(pic_type);
+       pic = pic16_find_device(pic_type);
 
-  if(!pic) {
-    if(pic_type)
-      fprintf(stderr, "'%s' was not found.\n", pic_type);
-    else
-      fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
+       if(!pic) {
+               if(pic_type)
+                       fprintf(stderr, "'%s' was not found.\n", pic_type);
+               else
+                       fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
 
-    fprintf(stderr,"Valid devices are:\n");
+               fprintf(stderr,"Valid devices are:\n");
 
-    pic16_list_valid_pics(4,0);
-    exit(1);
-  }
+               pic16_list_valid_pics(4,0);
+               exit(1);
+       }
 
-  pic->maxRAMaddress = -1;
+//     printf("PIC processor found and initialized: %s\n", pic_type);
+       pic16_setMaxRAM( 0xfff  );
 }
 
 /*-----------------------------------------------------------------*
@@ -438,22 +500,15 @@ char *pic16_processor_base_name(void)
   return pic->name[0];
 }
 
-static int isSFR(int address)
-{
-
-  if( (address > pic->maxRAMaddress) || !pic16_finalMapping[address].isSFR)
-    return 0;
-
-  return 1;
-
-}
 
+#if 0
 /*-----------------------------------------------------------------*
  *-----------------------------------------------------------------*/
 static int validAddress(int address, int reg_size)
 {
   int i;
 
+#if 0
   if (pic->maxRAMaddress < 0) {
     fprintf(stderr, "missing \"#pragma maxram\" setting\n");
     return 0;
@@ -467,135 +522,47 @@ static int validAddress(int address, int reg_size)
        pic16_finalMapping[address+i].reg ||
        pic16_finalMapping[address+i].isSFR )
       return 0;
+#endif
 
   return 1;
 }
+#endif
 
-/*-----------------------------------------------------------------*
- *-----------------------------------------------------------------*/
-static void mapRegister(regs *reg)
-{
-
-  int i;
-  int alias;
-
-  if(!reg || !reg->size) {
-    fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
-    return;
-  }
-
-  if (pic->maxRAMaddress < 0) {
-    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
-    return;
-  }
-
-  for(i=0; i<reg->size; i++) {
-
-    alias = pic16_finalMapping[reg->address].alias;
-    reg->alias = alias;
-
-    do {
-
-//     fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
-
-      pic16_finalMapping[reg->address + alias + i].reg = reg;
-      pic16_finalMapping[reg->address + alias + i].instance = i;
-
-      /* Decrement alias */
-      if(alias)
-       alias -= ((alias & (alias - 1)) ^ alias);
-      else
-       alias--;
-
-    } while (alias>=0);
-  }
-
-  //  fprintf(stderr,"%s - %s addr = 0x%03x, size %d\n",__FUNCTION__,reg->name, reg->address,reg->size);
-
-  reg->isMapped = 1;
-
-}
-
-/*-----------------------------------------------------------------*
- *-----------------------------------------------------------------*/
-static int assignRegister(regs *reg, int start_address)
+void checkAddReg(set **set, regs *reg)
 {
-  int i;
-
-       //fprintf(stderr,"%s -  %s start_address = 0x%03x\n",__FUNCTION__,reg->name, start_address);
-  if(reg->isFixed) {
-
-    if (validAddress(reg->address,reg->size)) {
-      //fprintf(stderr,"%s -  %s address = 0x%03x\n",__FUNCTION__,reg->name, reg->address);
-      mapRegister(reg);
-      return reg->address;
-    }
-
-    if( isSFR(reg->address)) {
-      mapRegister(reg);
-      return reg->address;
-    }
-
-    //fprintf(stderr, "WARNING: Ignoring Out of Range register assignment at fixed address %d, %s\n",
-    //    reg->address, reg->name);
-
-  } else {
-
-    /* This register does not have a fixed address requirement
-     * so we'll search through all availble ram address and
-     * assign the first one */
-
-    for (i=start_address; i<=pic->maxRAMaddress; i++) {
+  regs *tmp;
 
-      if (validAddress(i,reg->size)) {
-//     fprintf(stderr, "found valid address = 0x%04x\n", i);
-       reg->address = i;
-       mapRegister(reg);
-       return i;
-      }
-    }
-
-    fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
 
-  }
-
-  return -1;
+       for(tmp = setFirstItem(*set); tmp; tmp = setNextItem(*set)) {
+               if(!strcmp(tmp->name, reg->name))break;
+       }
+       
+       if(!tmp)
+               addSet(set, reg);
 }
 
 /*-----------------------------------------------------------------*
+ * void pic16_groupRegistersInSection - add each register to its   *
+ *     corresponding section                                      *
  *-----------------------------------------------------------------*/
-void pic16_assignFixedRegisters(set *regset)
+void pic16_groupRegistersInSection(set *regset)
 {
   regs *reg;
 
-  for (reg = setFirstItem(regset) ; reg ; 
-       reg = setNextItem(regset)) {
-
-    if(reg->isFixed) 
-      assignRegister(reg,0);
-  }
-
+       for(reg=setFirstItem(regset); reg; reg = setNextItem(regset)) {
+               if(reg->wasUsed
+                       && !(reg->regop && SPEC_EXTR(OP_SYM_ETYPE(reg->regop)))) {
+                       if(reg->isFixed)
+                               checkAddReg(&pic16_fix_udata, reg);
+               
+                       if(!reg->isFixed)
+                               checkAddReg(&pic16_rel_udata, reg);
+               }
+       }
 }
 
-/*-----------------------------------------------------------------*
- *-----------------------------------------------------------------*/
-void pic16_assignRelocatableRegisters(set *regset, int used)
-{
-
-  regs *reg;
-  int address = 0;
-
-  for (reg = setFirstItem(regset) ; reg ; 
-       reg = setNextItem(regset)) {
 
-    //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
 
-    if((!reg->isFixed) && (used || reg->wasUsed))
-      address = assignRegister(reg,address);
-
-  }
-
-}
 
 
 /*-----------------------------------------------------------------*
@@ -645,7 +612,7 @@ void pic16_assignConfigWordValue(int address, int value)
     break;
   }
 
-  //fprintf(stderr,"setting config word to 0x%x\n",value);
+       fprintf(stderr,"setting config word to 0x%x\n",value);
 
 }
 /*-----------------------------------------------------------------*
@@ -684,4 +651,3 @@ int pic16_getConfigWord(int address)
     return 0;
   }
 }
-