Fixed two bugs with inline assembly:
[fw/sdcc] / src / pic / device.c
index db4e2b3967662fd3febad90683557db8f05e42c1..fe0ef87236c80c93544439c81146c94fe8b5e98c 100644 (file)
 #define STRCASECMP strcasecmp
 #endif
 
-/* 16F627 */
-memRange p16f627_mem[] = {
-  {0x20,  0x6f,  0x00,  0},
-  {0xa0,  0xef,  0x00,  1},
-  {0x120, 0x14f, 0x00,  2},
-  {0x70,  0x7f,  0x180, 0},
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-memRange p16f627_sfr[] = {
-  {0x00,  0x00,  0x180, 0},
-  {0x01,  0x01,  0x100, 0},
-  {0x02,  0x04,  0x180, 0},
-  {0x05,  0x05,  0x000, 0},
-  {0x06,  0x06,  0x100, 0},
-  {0x81,  0x81,  0x100, 1},
-  {0x85,  0x85,  0x000, 1},
-  {0x86,  0x86,  0x100, 1},
-  {0x0a,  0x0b,  0x180, 0},
-  {0x0c,  0x0c,  0x000, 0},
-  {0x0e,  0x12,  0x000, 0},
-  {0x15,  0x1a,  0x000, 0},
-  {0x1f,  0x1f,  0x000, 0},
-  {0x8e,  0x8e,  0x000, 1},
-  {0x92,  0x92,  0x000, 1},
-  {0x98,  0x9d,  0x000, 1},
-  {0x9f,  0x9f,  0x000, 1},
-
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-
-/* 16F84 */
-memRange p16f84_mem[] = {
-  {0x0c,  0x4f,  0x80,  0},
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-memRange p16f84_sfr[] = {
-  {0x01,  0x01,  0x00, 0},
-  {0x02,  0x04,  0x80, 0},
-  {0x05,  0x06,  0x00, 0},
-  {0x81,  0x81,  0x00, 1},
-  {0x85,  0x86,  0x00, 1},
-  {0x08,  0x09,  0x00, 0},
-  {0x88,  0x89,  0x00, 1},
-  {0x0a,  0x0b,  0x80, 0},
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-
-/* 16F877 */
-memRange p16f877_mem[] = {
-  {0x20,  0x6f,  0x00,  0},
-  {0xa0,  0xef,  0x00,  1},
-  {0x110, 0x16f, 0x00,  2},
-  {0x190, 0x1ef, 0x00,  3},
-  {0x70,  0x7f,  0x180, 0},
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-memRange p16f877_sfr[] = {
-  {0x00,  0x00,  0x180, 0},
-  {0x01,  0x01,  0x100, 0},
-  {0x02,  0x04,  0x180, 0},
-  {0x05,  0x05,  0x000, 0},
-  {0x85,  0x85,  0x000, 1},
-  {0x81,  0x81,  0x100, 1},
-  {0x06,  0x06,  0x100, 0},
-  {0x86,  0x86,  0x100, 1},
-  {0x07,  0x09,  0x000, 0},
-  {0x87,  0x89,  0x000, 1},
-  {0x0a,  0x0b,  0x180, 0},
-  {0x0c,  0x1f,  0x000, 0},
-  {0x8c,  0x8e,  0x000, 1},
-  {0x91,  0x94,  0x000, 1},
-  {0x98,  0x99,  0x000, 1},
-  {0x9e,  0x9f,  0x000, 1},
-  {0x10c, 0x10f, 0x000, 2},
-  {0x18c, 0x18f, 0x000, 3},
-
-  {-1,    -1,    -1,   -1}     /* end indicator */
-};
-
-
 static PIC_device Pics[] = {
   {
     {"p16f627", "16f627", "pic16f627", "f627"}, /* processor name */
-    p16f627_mem,                     /* ram mem map */
-    p16f627_sfr,                     /* sfr mem map */
+    (memRange *)NULL,
+    (memRange *)NULL,
     0,                               /* max ram address (calculated) */
+    0x80,                            /* Bank Mask */
   },
 
   {
     {"p16f628", "16f628", "pic16f628", "f628"},
-    p16f627_mem,
-    p16f627_sfr,
+    (memRange *)NULL,
+    (memRange *)NULL,
     0,
+    0x80,
   },
 
   {
     {"p16f84", "16f84", "pic16f84", "f84"},
-    p16f84_mem,
-    p16f84_sfr,
+    (memRange *)NULL,
+    (memRange *)NULL,
     0,
+    0x80,
+  },
+
+  {
+    {"p16f873", "16f873", "pic16f873", "f873"},
+    (memRange *)NULL,
+    (memRange *)NULL,
+    0,
+    0x180,
   },
 
   {
     {"p16f877", "16f877", "pic16f877", "f877"},
-    p16f877_mem,
-    p16f877_sfr,
+    (memRange *)NULL,
+    (memRange *)NULL,
     0,
+    0x180,
   },
 
 };
@@ -152,91 +84,102 @@ static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
 static PIC_device *pic=NULL;
 
 AssignedMemory *finalMapping=NULL;
-/*-----------------------------------------------------------------*
- *
- * void addMem(memRange *ranges,int type)
- *
- *
- *-----------------------------------------------------------------*/
 
-static void addMem(memRange *ranges,int type)
-{
-  memRange *r = ranges;
-  int i;
+#define CONFIG_WORD_ADDRESS 0x2007
+#define DEFAULT_CONFIG_WORD 0x3fff
 
-  do {
+static unsigned int config_word = DEFAULT_CONFIG_WORD;
 
-    int alias = r->alias;
+void addMemRange(memRange *r, int type)
+{
+  int i;
+  int alias = r->alias;
 
-    do {
+  if (pic->maxRAMaddress < 0) {
+    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+    return;
+  }
 
-      for(i=r->start_address; i<= r->end_address; i++) {
-       if(i <= pic->maxRAMaddress) {
-         finalMapping[i | alias].isValid = 1;
-         finalMapping[i | alias].alias = r->alias;
-         finalMapping[i | alias].bank  = r->bank;
-         if(type) {
-           /* hack for now */
-           finalMapping[i | alias].isSFR  = 1;
-         } else
-           finalMapping[i | alias].isSFR  = 0;
+  do {
+    for (i=r->start_address; i<= r->end_address; i++) {
+      if ((i|alias) <= pic->maxRAMaddress) {
+       finalMapping[i | alias].isValid = 1;
+       finalMapping[i | alias].alias = r->alias;
+       finalMapping[i | alias].bank  = r->bank;
+       if(type) {
+         /* hack for now */
+         finalMapping[i | alias].isSFR  = 1;
+       } else {
+         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);
-
-    r++;
-
-  } while (r->start_address >= 0);
-
+    /* Decrement alias */
+    if (alias) {
+      alias -= ((alias & (alias - 1)) ^ alias);
+    } else {
+      alias--;
+    }
 
+  } while (alias >= 0);
 }
 
-static void addMaps(PIC_device *pPic)
+void setMaxRAM(int size)
 {
   int i;
-  memRange *r;
+  pic->maxRAMaddress = size;
 
-  if(!pPic)
+  if (pic->maxRAMaddress < 0) {
+    fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
+           pic->maxRAMaddress);
     return;
+  }
 
+  finalMapping = Safe_calloc(1+pic->maxRAMaddress,
+                            sizeof(AssignedMemory));
 
-  /* First, find the maximum address */
-
-  r = pPic->ram;
-  pPic->maxRAMaddress = 0;
-
-  do {
+  /* Now initialize the finalMapping array */
 
-    if((r->end_address | r->alias) > pPic->maxRAMaddress)
-      pPic->maxRAMaddress = r->end_address | r->alias;
+  for(i=0; i<=pic->maxRAMaddress; i++) {
+    finalMapping[i].reg = NULL;
+    finalMapping[i].isValid = 0;
+  }
+}
 
-    r++;
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 
-  } while (r->start_address >= 0);
+int isREGinBank(regs *reg, int bank)
+{
 
+  if(!reg || !pic)
+    return 0;
 
+  if(((reg->address | reg->alias) & pic->bankMask & bank) == bank)
+    return 1;
 
-  finalMapping = Safe_calloc(1+pPic->maxRAMaddress, sizeof(AssignedMemory));
+  return 0;
+}
 
-  /* Now initialize the finalMapping array */
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
+int REGallBanks(regs *reg)
+{
 
-  for(i=0; i<=pPic->maxRAMaddress; i++) {
-    finalMapping[i].reg = NULL;
-    finalMapping[i].isValid = 0;
-  }
+  if(!reg || !pic)
+    return 0;
 
-  addMem(pPic->ram,0); /* add general purpose regs to the map */
-  addMem(pPic->sfr,1); /* Add SFR's to the memmap */
+  return ((reg->address | reg->alias) & pic->bankMask);
 
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
+
 /*
  *  dump_map -- debug stuff
  */
@@ -264,9 +207,15 @@ void dump_cblock(FILE *of)
 {
   int start=-1;
   int addr=0;
+  int bank_base;
 
   //dump_map();   /* display the register map */
 
+  if (pic->maxRAMaddress < 0) {
+    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+    return;
+  }
+
   do {
 
     if(finalMapping[addr].reg && !finalMapping[addr].reg->isEmitted) {
@@ -276,9 +225,17 @@ void dump_cblock(FILE *of)
     } 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 */
+
        fprintf(of,"  cblock  0X%04X\t; Bank %d\n",start,finalMapping[start].bank);
 
        for( ; start < addr; start++) {
@@ -288,7 +245,7 @@ void dump_cblock(FILE *of)
            /* If this register is aliased in multiple banks, then
             * mangle the variable name with the alias address: */
            if(finalMapping[start].alias & start)
-             fprintf(of,"_%x",finalMapping[start].alias);
+             fprintf(of,"_%x",bank_base);
 
            if(finalMapping[start].instance)
              fprintf(of,"_%d",finalMapping[start].instance);
@@ -412,9 +369,18 @@ void init_pic(char *pic_type)
     exit(1);
   }
 
-  
-  addMaps(pic);
+  pic->maxRAMaddress = -1;
+}
 
+/*-----------------------------------------------------------------*
+ *  
+ *-----------------------------------------------------------------*/
+int picIsInitialized(void)
+{
+  if(pic && pic->maxRAMaddress > 0)
+    return 1;
+
+  return 0;
 
 }
 
@@ -440,10 +406,17 @@ int isSFR(int address)
 
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 int validAddress(int address, int reg_size)
 {
   int i;
 
+  if (pic->maxRAMaddress < 0) {
+    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+    return 0;
+  }
+
   if(address > pic->maxRAMaddress)
     return 0;
 
@@ -456,6 +429,8 @@ int validAddress(int address, int reg_size)
   return 1;
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 void mapRegister(regs *reg)
 {
 
@@ -467,6 +442,11 @@ void mapRegister(regs *reg)
     return;
   }
 
+  if (pic->maxRAMaddress < 0) {
+    fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+    return;
+  }
+
   for(i=0; i<reg->size; i++) {
 
     alias = finalMapping[reg->address].alias;
@@ -474,7 +454,7 @@ void mapRegister(regs *reg)
 
     do {
 
-      // fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
+      //fprintf(stdout,"mapping %s to address 0x%02x, reg size = %d\n",reg->name, (reg->address+alias+i),reg->size);
 
       finalMapping[reg->address + alias + i].reg = reg;
       finalMapping[reg->address + alias + i].instance = i;
@@ -494,6 +474,8 @@ void mapRegister(regs *reg)
 
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 int assignRegister(regs *reg, int start_address)
 {
   int i;
@@ -502,7 +484,7 @@ int assignRegister(regs *reg, int 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;
     }
@@ -536,6 +518,8 @@ int assignRegister(regs *reg, int start_address)
   return -1;
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 void assignFixedRegisters(set *regset)
 {
   regs *reg;
@@ -549,6 +533,8 @@ void assignFixedRegisters(set *regset)
 
 }
 
+/*-----------------------------------------------------------------*
+ *-----------------------------------------------------------------*/
 void assignRelocatableRegisters(set *regset, int used)
 {
 
@@ -558,12 +544,45 @@ void assignRelocatableRegisters(set *regset, int used)
   for (reg = setFirstItem(regset) ; reg ; 
        reg = setNextItem(regset)) {
 
-    // fprintf(stdout,"assigning %s\n",reg->name);
+    //fprintf(stdout,"assigning %s isFixed=%d, wasUsed=%d\n",reg->name,reg->isFixed,reg->wasUsed);
 
-    if((!reg->isFixed) && ( (used==0) || reg->wasUsed))
+    if((!reg->isFixed) && ( used || reg->wasUsed))
       address = assignRegister(reg,address);
 
   }
 
 }
 
+
+/*-----------------------------------------------------------------*
+ *  void assignConfigWordValue(int address, int value)
+ *
+ * All midrange PICs have one config word at address 0x2007.
+ * This routine will assign a value to that address.
+ *
+ *-----------------------------------------------------------------*/
+
+void assignConfigWordValue(int address, int value)
+{
+  if(CONFIG_WORD_ADDRESS == address)
+    config_word = value;
+
+  //fprintf(stderr,"setting config word to 0x%x\n",value);
+
+}
+/*-----------------------------------------------------------------*
+ * int getConfigWord(int address)
+ *
+ * Get the current value of the config word.
+ *
+ *-----------------------------------------------------------------*/
+
+int getConfigWord(int address)
+{
+  if(CONFIG_WORD_ADDRESS == address)
+    return config_word;
+
+  else
+    return 0;
+
+}