* src/pic16/main.c (_pic16_finaliseOptions): do not quote the
[fw/sdcc] / src / pic / device.c
index 528cde2767cc51e0f19956b8942a919cecf64504..17ab6d5d054fcb7710552d7b3ab02963ab5fcf0f 100644 (file)
    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 -------------------------------------------------------------------------*/
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-#include "common.h"   // Include everything in the SDCC src directory
-#include "newalloc.h"
-
-
-#include "main.h"
-#include "pcode.h"
-#include "ralloc.h"
 #include "device.h"
 
-extern int Gstack_base_addr;
-extern int Gstack_size;
+/*
+ * Imports
+ */
+extern set *includeDirsSet;
+extern set *userIncDirsSet;
+extern set *libDirsSet;
+extern set *libPathsSet;
 
 #define MAX_PICLIST 200
 static PIC_device *Pics[MAX_PICLIST];
+static PIC_device *pic = NULL;
 static int num_of_supported_PICS = 0;
-
-static PIC_device *pic=NULL;
-
-int maxRAMaddress = 0;
+static int maxRAMaddress = 0;
 
 #define CONFIG_WORD_ADDRESS 0x2007
 #define CONFIG2_WORD_ADDRESS 0x2008
@@ -51,6 +43,8 @@ int maxRAMaddress = 0;
 #define PIC14_STRING_LEN 256
 #define SPLIT_WORDS_MAX 16
 
+/* Keep track of whether we found an assignment to the __config words. */
+static int pic14_hasSetConfigWord = 0;
 static unsigned int config_word = DEFAULT_CONFIG_WORD;
 static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
 static memRange *rangeRAM = NULL;
@@ -188,13 +182,19 @@ static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
        rangeRAM = r;
 }
 
-extern set *includeDirsSet;
-extern set *userIncDirsSet;
-extern set *libDirsSet;
-extern set *libPathsSet;
+static void setMaxRAM(int size)
+{
+       maxRAMaddress = size;
 
-/* read the file with all the pic14 definitions and pick out the definition for a processor
- * if specified. if pic_name is NULL reads everything */
+       if (maxRAMaddress < 0) {
+               fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
+                       maxRAMaddress, DEVICE_FILE_NAME);
+               return;
+       }
+}
+
+/* read the file with all the pic14 definitions and pick out the definition
+ * for a processor if specified. if pic_name is NULL reads everything */
 static PIC_device *find_device(char *pic_name)
 {
        FILE *pic_file;
@@ -224,56 +224,66 @@ static PIC_device *find_device(char *pic_name)
        /* first scan all include directories */
        pic_file = NULL;
        //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
-       for (dir = setFirstItem(includeDirsSet);
+       for (dir = setFirstItem(userIncDirsSet);
                !pic_file && dir;
-               dir = setNextItem(includeDirsSet))
+               dir = setNextItem(userIncDirsSet))
        {
          //fprintf( stderr, "searching1 %s\n", dir );
-         SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
+         SNPRINTF(&filename[0], len, "%s%s", dir,
+           DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
          pic_file = fopen( filename, "rt" );
          if (pic_file) break;
        } // for
-       for (dir = setFirstItem(userIncDirsSet);
+
+       for (dir = setFirstItem(includeDirsSet);
                !pic_file && dir;
-               dir = setNextItem(userIncDirsSet))
+               dir = setNextItem(includeDirsSet))
        {
          //fprintf( stderr, "searching2 %s\n", dir );
-         SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
+         SNPRINTF(&filename[0], len, "%s%s", dir,
+           DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
          pic_file = fopen( filename, "rt" );
          if (pic_file) break;
        } // for
+
        for (dir = setFirstItem(libDirsSet);
                !pic_file && dir;
                dir = setNextItem(libDirsSet))
        {
          //fprintf( stderr, "searching3 %s\n", dir );
-         SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
+         SNPRINTF(&filename[0], len, "%s%s", dir,
+           DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
          pic_file = fopen( filename, "rt" );
          if (pic_file) break;
        } // for
+
        for (dir = setFirstItem(libPathsSet);
                !pic_file && dir;
                dir = setNextItem(libPathsSet))
        {
          //fprintf( stderr, "searching4 %s\n", dir );
-         SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
+         SNPRINTF(&filename[0], len, "%s%s", dir,
+           DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
          pic_file = fopen( filename, "rt" );
          if (pic_file) break;
        } // for
+
        if (!pic_file) {
-         pic_file = fopen(DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING DEVICE_FILE_NAME, "rt");
-       }
-       if (pic_file == NULL) {
-               /* this second attempt is used when initially building the libraries */
-               pic_file = fopen(".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." DIR_SEPARATOR_STRING ".." 
-                               DIR_SEPARATOR_STRING "src" DIR_SEPARATOR_STRING "pic" DIR_SEPARATOR_STRING 
-                               DEVICE_FILE_NAME, "rt");
-               if (pic_file == NULL) {
-                       fprintf(stderr, "can't find %s\n", DATADIR LIB_DIR_SUFFIX DIR_SEPARATOR_STRING "pic" 
-                                       DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
-                       return NULL;
-               }
-       }
+         SNPRINTF(&filename[0], len, "%s",
+           DATADIR LIB_DIR_SUFFIX
+           DIR_SEPARATOR_STRING "pic"
+           DIR_SEPARATOR_STRING DEVICE_FILE_NAME);
+         pic_file = fopen( filename, "rt" );
+       } // if
+
+        if (pic_file == NULL) {
+            fprintf(stderr, "can't find %s\n", DEVICE_FILE_NAME);
+            return NULL;
+        } // if
+
+       if (options.verbose) {
+           printf ("Using devices from %s.\n", filename);
+       } // if
        
        /* read line by line */
        pic_buf[sizeof(pic_buf)-1] = '\0';
@@ -401,44 +411,6 @@ static PIC_device *find_device(char *pic_name)
        return NULL;
 }
 
-void setMaxRAM(int size)
-{
-       maxRAMaddress = size;
-       
-       if (maxRAMaddress < 0) {
-               fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
-                       maxRAMaddress, DEVICE_FILE_NAME);
-               return;
-       }
-}
-
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-
-int isREGinBank(regs *reg, int bank)
-{
-       
-       if(!reg || !pic)
-               return 0;
-       
-       if((int)((reg->address | reg->alias) & pic->bankMask & bank) == bank)
-               return 1;
-       
-       return 0;
-}
-
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-int REGallBanks(regs *reg)
-{
-       
-       if(!reg || !pic)
-               return 0;
-       
-       return ((reg->address | reg->alias) & pic->bankMask);
-       
-}
-
 /*-----------------------------------------------------------------*
  *  void list_valid_pics(int ncols, int list_alias)
  *
@@ -449,7 +421,7 @@ int REGallBanks(regs *reg)
  * list_alias - if non-zero, print all of the supported aliases
  *              for a device (e.g. F84, 16F84, etc...)
  *-----------------------------------------------------------------*/
-void list_valid_pics(int ncols)
+static void list_valid_pics(int ncols)
 {
        int col=0,longest;
        int i,k,l;
@@ -566,172 +538,15 @@ char *processor_base_name(void)
        return pic->name;
 }
 
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-int validAddress(int address, int reg_size)
+int IS_CONFIG_ADDRESS(int address)
 {
-       if (maxRAMaddress < 0) {
-               fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
-               return 0;
-       }
-       //  fprintf(stderr, "validAddress: Checking 0x%04x\n",address);
-       assert (reg_size > 0);
-       if(address + (reg_size - 1) > maxRAMaddress)
-               return 0;
-       
-       return 1;
-}
-
-#if 0
-/* The following code should be (and is) implemented in the linker. */
 
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-void mapRegister(regs *reg)
-{
-       
-       unsigned i;
-       int alias;
-       
-       if(!reg || !reg->size) {
-               fprintf(stderr,"WARNING: %s:%s:%d Bad register\n",__FILE__,__FUNCTION__,__LINE__);
-               return;
-       }
-       
-       if (maxRAMaddress < 0) {
-               fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
-               return;
-       }
-       
-       for(i=0; i<reg->size; i++) {
-               
-               assert(reg->address >= 0 && reg->address < maxRAMaddress);
-               
-               alias = 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);
-                       
-                       finalMapping[reg->address + alias + i].reg = reg;
-                       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;
-       
-}
-
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-int assignRegister(regs *reg, int start_address)
-{
-       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 (getenv("SDCCPICDEBUG")) {
-                       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<=maxRAMaddress; i++) {
-                       
-                       if (validAddress(i,reg->size)) {
-                               reg->address = i;
-                               mapRegister(reg);
-                               return i;
-                       }
-               }
-               
-               fprintf(stderr, "WARNING: No more RAM available for %s\n",reg->name);
-               
-       }
-       
-       return -1;
-}
-
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-void assignFixedRegisters(set *regset)
-{
-       regs *reg;
-       
-       for (reg = setFirstItem(regset) ; reg ; 
-       reg = setNextItem(regset)) {
-               
-               if(reg->isFixed) 
-                       assignRegister(reg,0);
-       }
-       
+        return ((address == CONFIG_WORD_ADDRESS)
+                || (address == CONFIG2_WORD_ADDRESS));
 }
 
 /*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-void assignRelocatableRegisters(set *regset, int used)
-{
-       
-       regs *reg;
-       int address = 0;
-       
-       for (reg = setFirstItem(regset) ; reg ; 
-       reg = setNextItem(regset)) {
-               
-               //fprintf(stdout,"assigning %s (%d) isFixed=%d, wasUsed=%d\n",reg->name,reg->size,reg->isFixed,reg->wasUsed);
-               
-               if((!reg->isExtern) && (!reg->isFixed) && ( used || reg->wasUsed)) {
-                       /* If register have been reused then shall not print it a second time. */
-                       set *s;
-                       int done = 0;
-                       for (s = regset; s; s = s->next) {
-                               regs *r;
-                               r = s->item;
-                               if (r == reg)
-                                       break;
-                               if((!r->isFixed) && ( used || r->wasUsed)) {
-                                       if (r->rIdx == reg->rIdx) {
-                                               reg->address = r->address;
-                                               done = 1;
-                                               break;
-                                       }
-                               }
-                       }
-                       if (!done)
-                               address = assignRegister(reg,address);
-               }
-       }
-       
-}
-#endif
-
-/* Keep track of whether we found an assignment to the __config words. */
-static int pic14_hasSetConfigWord = 0;
-
-/*-----------------------------------------------------------------*
- *  void assignConfigWordValue(int address, int value)
+ *  void pic14_assignConfigWordValue(int address, int value)
  *
  * Most midrange PICs have one config word at address 0x2007.
  * Newer PIC14s have a second config word at address 0x2008.
@@ -751,33 +566,6 @@ void pic14_assignConfigWordValue(int address, int value)
        pic14_hasSetConfigWord = 1;
 }
 
-/*-----------------------------------------------------------------*
- * int pic14_emitConfigWord (FILE * vFile)
- * 
- * Emit the __config directives iff we found a previous assignment
- * to the config word.
- *-----------------------------------------------------------------*/
-extern char *iComments2;
-int pic14_emitConfigWord (FILE * vFile)
-{
-  if (pic14_hasSetConfigWord)
-  {
-    fprintf (vFile, "%s", iComments2);
-    fprintf (vFile, "; config word \n");
-    fprintf (vFile, "%s", iComments2);
-    if (pic14_getHasSecondConfigReg())
-    {
-      fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
-      fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
-    }
-    else
-      fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
-
-    return 1;
-  }
-  return 0;
-}
-
 /*-----------------------------------------------------------------*
  * int pic14_getConfigWord(int address)
  *
@@ -785,7 +573,7 @@ int pic14_emitConfigWord (FILE * vFile)
  *
  *-----------------------------------------------------------------*/
 
-int pic14_getConfigWord(int address)
+static int pic14_getConfigWord(int address)
 {
        switch (address)
        {
@@ -800,20 +588,11 @@ int pic14_getConfigWord(int address)
        }
 }
 
-/*-----------------------------------------------------------------*
-*  
-*-----------------------------------------------------------------*/
-unsigned pic14_getMaxRam(void)
-{
-       return pic->defMaxRAMaddrs;
-}
-
-
 /*-----------------------------------------------------------------*
 *  int getHasSecondConfigReg(void) - check if the device has a 
 *  second config register, rather than just one.
 *-----------------------------------------------------------------*/
-int pic14_getHasSecondConfigReg(void)
+static int pic14_getHasSecondConfigReg(void)
 {
        if(!pic)
                return 0;
@@ -821,12 +600,38 @@ int pic14_getHasSecondConfigReg(void)
                return pic->hasSecondConfigReg;
 }
 
+/*-----------------------------------------------------------------*
+ * int pic14_emitConfigWord (FILE * vFile)
+ *
+ * Emit the __config directives iff we found a previous assignment
+ * to the config word.
+ *-----------------------------------------------------------------*/
+int pic14_emitConfigWord (FILE * vFile)
+{
+  if (pic14_hasSetConfigWord)
+  {
+    fprintf (vFile, "%s", iComments2);
+    fprintf (vFile, "; config word \n");
+    fprintf (vFile, "%s", iComments2);
+    if (pic14_getHasSecondConfigReg())
+    {
+      fprintf (vFile, "\t__config _CONFIG1, 0x%x\n", pic14_getConfigWord(0x2007));
+      fprintf (vFile, "\t__config _CONFIG2, 0x%x\n", pic14_getConfigWord(0x2008));
+    }
+    else
+      fprintf (vFile, "\t__config 0x%x\n", pic14_getConfigWord(0x2007));
+
+    return 1;
+  }
+  return 0;
+}
+
 /*-----------------------------------------------------------------*
  * True iff the device has memory aliased in every bank.
  * If true, low and high will be set to the low and high address
  * occupied by the (last) sharebank found.
  *-----------------------------------------------------------------*/
-int pic14_hasSharebank(int *low, int *high, int *size)
+static int pic14_hasSharebank(int *low, int *high, int *size)
 {
        memRange *r;
 
@@ -858,7 +663,7 @@ int pic14_hasSharebank(int *low, int *high, int *size)
 /*
  * True iff the memory region [low, high] is aliased in all banks.
  */
-int pic14_isShared(int low, int high)
+static int pic14_isShared(int low, int high)
 {
        memRange *r;