* .version: bumped version to 2.5.6 (pic14 ABI changed)
[fw/sdcc] / src / pic / device.c
index fea8565c9f86da32bab0c096473278b0e7308914..df172481173abc96961597ad9483041eefc7f9b5 100644 (file)
@@ -19,6 +19,8 @@
 -------------------------------------------------------------------------*/
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
 
 #include "common.h"   // Include everything in the SDCC src directory
 #include "newalloc.h"
 
 #if defined(__BORLANDC__) || defined(_MSC_VER)
 #define STRCASECMP stricmp
+#define STRNCASECMP strnicmp
 #else
 #define STRCASECMP strcasecmp
+#define STRNCASECMP strncasecmp
 #endif
 
 extern int Gstack_base_addr;
 extern int Gstack_size;
 
-static PIC_device Pics[] = {
-       {
-               {"p16f627", "16f627", "pic16f627", "f627"}, /* processor name */
-               (memRange *)NULL,
-               (memRange *)NULL,
-               0,                /* max ram address (calculated) */
-               0x1ff,            /* default max ram address */
-               0x80,             /* Bank Mask */
-       },
-       
-       {
-       {"p16f628", "16f628", "pic16f628", "f628"},
-               (memRange *)NULL,
-               (memRange *)NULL,
-               0,
-               0x1ff,
-               0x80,
-       },
-       
-       {
-               {"p16f84", "16f84", "pic16f84", "f84"},
-                       (memRange *)NULL,
-                       (memRange *)NULL,
-                       0,
-                       0x4f, /* 68 register available 0x0C to 0x4F (0x8C to 0xCF mapped to bank 0) */
-                       0x80,
-       },
-       
-       {
-               {"p16f873", "16f873", "pic16f873", "f873"},
-                       (memRange *)NULL,
-                       (memRange *)NULL,
-                       0,
-                       0x1ff,
-                       0x180,
-       },
-
-       {
-               {"p16f877", "16f877", "pic16f877", "f877"},
-                       (memRange *)NULL,
-                       (memRange *)NULL,
-                       0,
-                       0x1ff,
-                       0x180,
-       },
-       
-       {
-               {"p16f819", "16f819", "pic16f819", "f819"},
-                       (memRange *)NULL,
-                       (memRange *)NULL,
-                       0,
-                       0x1ff,
-                       0x80,
-       },
-
-};
-
-static int num_of_supported_PICS = sizeof(Pics)/sizeof(PIC_device);
+#define MAX_PICLIST 200
+static PIC_device *Pics[MAX_PICLIST];
+static int num_of_supported_PICS = 0;
 
 static PIC_device *pic=NULL;
 
+int maxRAMaddress = 0;
 AssignedMemory *finalMapping=NULL;
 
 #define CONFIG_WORD_ADDRESS 0x2007
+#define CONFIG2_WORD_ADDRESS 0x2008
 #define DEFAULT_CONFIG_WORD 0x3fff
+#define DEFAULT_CONFIG2_WORD 0x3ffc
+
+#define DEVICE_FILE_NAME "pic14devices.txt"
+#define PIC14_STRING_LEN 256
+#define SPLIT_WORDS_MAX 16
 
 static unsigned int config_word = DEFAULT_CONFIG_WORD;
+static unsigned int config2_word = DEFAULT_CONFIG2_WORD;
 
 extern int pic14_is_shared (regs *reg);
 extern void emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize);
 
+
+/* parse a value from the configuration file */
+static int parse_config_value(char *str)
+{
+       if (str[strlen(str) - 1] == 'K')
+               return atoi(str) * 1024;        /* like "1K" */
+               
+       else if (STRNCASECMP(str, "0x", 2) == 0)
+               return strtol(str+2, NULL, 16); /* like "0x400" */
+       
+       else
+               return atoi(str);               /* like "1024" */
+}
+
+
+/* split a line into words */
+static int split_words(char result_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN], char *str)
+{
+       char *pos = str;
+       int num_words = 0;
+       int ccount;
+       
+       while (*pos != '\0' && num_words < SPLIT_WORDS_MAX) {
+               /* remove leading spaces */
+               while (isspace(*pos) || *pos == ',')
+                       pos++;
+       
+               /* copy everything up until the first space or comma */
+               for (ccount = 0; *pos != '\0' && !isspace(*pos) && *pos != ',' && ccount < PIC14_STRING_LEN-1; ccount++, pos++)
+                       result_word[num_words][ccount] = *pos;
+               result_word[num_words][ccount] = '\0';
+               
+               num_words++;
+       }
+       
+       return num_words;
+}
+
+
+/* remove annoying prefixes from the processor name */
+static char *sanitise_processor_name(char *name)
+{
+       char *proc_pos = name;
+
+       if (name == NULL)
+               return NULL;
+       
+       if (STRNCASECMP(proc_pos, "pic16", 5) == 0)
+               proc_pos += 5;
+
+       else if (STRNCASECMP(proc_pos, "p16", 3) == 0)
+               proc_pos += 3;
+                               
+       else if (STRNCASECMP(proc_pos, "16", 2) == 0)
+               proc_pos += 2;
+                                       
+       return proc_pos;
+}
+
+
+/* create a structure for a pic processor */
+static PIC_device *create_pic(char *pic_name, int maxram, int bankmsk, int confsiz, int program, int data, int eeprom, int io)
+{
+       PIC_device *new_pic;
+       char *simple_pic_name = sanitise_processor_name(pic_name);
+       
+       new_pic = Safe_calloc(1, sizeof(PIC_device));
+       new_pic->name[0] = Safe_calloc(strlen(simple_pic_name)+3, sizeof(char));
+       sprintf(new_pic->name[0], "16%s", simple_pic_name);
+       new_pic->name[1] = Safe_calloc(strlen(simple_pic_name)+4, sizeof(char));
+       sprintf(new_pic->name[1], "p16%s", simple_pic_name);
+       new_pic->name[2] = Safe_calloc(strlen(simple_pic_name)+6, sizeof(char));
+       sprintf(new_pic->name[2], "pic16%s", simple_pic_name);
+       new_pic->name[3] = Safe_calloc(strlen(simple_pic_name)+1, sizeof(char));
+       strcpy(new_pic->name[3], simple_pic_name);
+                       
+       new_pic->defMaxRAMaddrs = maxram;
+       new_pic->bankMask = bankmsk;
+       new_pic->hasSecondConfigReg = confsiz > 1;
+       
+       new_pic->programMemSize = program;
+       new_pic->dataMemSize = data;
+       new_pic->eepromMemSize = eeprom;
+       new_pic->ioPins = io;
+       
+       Pics[num_of_supported_PICS] = new_pic;
+       num_of_supported_PICS++;
+                       
+       return new_pic;
+}
+
+
+/* mark some registers as being duplicated across banks */
+static void register_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
+{
+       memRange r;
+       int pcount;
+       
+       if (num_words < 3) {
+               fprintf(stderr, "WARNING: not enough values in %s regmap directive\n", DEVICE_FILE_NAME);
+               return;
+       }
+       
+       r.alias = parse_config_value(word[1]);
+
+       for (pcount = 2; pcount < num_words; pcount++) {
+           
+               r.start_address = parse_config_value(word[pcount]);
+               r.end_address = parse_config_value(word[pcount]);
+               r.bank = (r.start_address >> 7) & 3;
+               
+               addMemRange(&r, 1);
+       }
+}
+
+
+/* define ram areas - may be duplicated across banks */
+static void ram_map(int num_words, char word[SPLIT_WORDS_MAX][PIC14_STRING_LEN])
+{
+       memRange r;
+       
+       if (num_words < 4) {
+               fprintf(stderr, "WARNING: not enough values in %s memmap directive\n", DEVICE_FILE_NAME);
+               return;
+       }
+       
+       r.start_address = parse_config_value(word[1]);
+       r.end_address = parse_config_value(word[2]);
+       r.alias = parse_config_value(word[3]);
+       r.bank = (r.start_address >> 7) & 3;
+               
+       addMemRange(&r, 0);
+}
+
+extern set *includeDirsSet;
+extern set *userIncDirsSet;
+extern set *libDirsSet;
+extern set *libPathsSet;
+
+/* 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;
+       char pic_buf[PIC14_STRING_LEN];
+       char *pic_buf_pos;
+       int found_processor = FALSE;
+       int done = FALSE;
+       char processor_name[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
+       int num_processor_names = 0;
+       int pic_maxram = 0;
+       int pic_bankmsk = 0;
+       int pic_confsiz = 0;
+       int pic_program = 0;
+       int pic_data = 0;
+       int pic_eeprom = 0;
+       int pic_io = 0;
+       char *simple_pic_name;
+       char *dir;
+       char filename[512];
+       int len = 512;
+       
+       simple_pic_name = sanitise_processor_name(pic_name);
+       
+       num_of_supported_PICS = 0;
+       
+       /* open the piclist file */
+       /* first scan all include directories */
+       pic_file = NULL;
+       //fprintf( stderr, "%s: searching %s\n", __FUNCTION__, DEVICE_FILE_NAME );
+       for (dir = setFirstItem(includeDirsSet);
+               !pic_file && dir;
+               dir = setNextItem(includeDirsSet))
+       {
+         fprintf( stderr, "searching1 %s\n", dir );
+         SNPRINTF(&filename[0], len, "%s%s%s", dir, DIR_SEPARATOR_STRING, DEVICE_FILE_NAME);
+         pic_file = fopen( filename, "rt" );
+         if (pic_file) break;
+       } // for
+       for (dir = setFirstItem(userIncDirsSet);
+               !pic_file && dir;
+               dir = setNextItem(userIncDirsSet))
+       {
+         fprintf( stderr, "searching2 %s\n", dir );
+         SNPRINTF(&filename[0], len, "%s%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);
+         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);
+         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;
+               }
+       }
+       
+       /* read line by line */
+       pic_buf[sizeof(pic_buf)-1] = '\0';
+       while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
+               
+               /* remove trailing spaces */
+               while (isspace(pic_buf[strlen(pic_buf)-1]))
+                       pic_buf[strlen(pic_buf)-1] = '\0';
+               
+               /* remove leading spaces */
+               for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
+               {}
+               
+               /* ignore comment / empty lines */
+               if (*pic_buf_pos != '\0' && *pic_buf_pos != '#') {
+                       
+                       /* split into fields */
+                       char pic_word[SPLIT_WORDS_MAX][PIC14_STRING_LEN];
+                       int num_pic_words;
+                       int wcount;
+                       
+                       num_pic_words = split_words(pic_word, pic_buf_pos);
+                       
+                       if (STRCASECMP(pic_word[0], "processor") == 0) {
+                       
+                               if (pic_name == NULL) {
+                                       /* this is the mode where we read all the processors in - store the names for now */
+                                       if (num_processor_names > 0) {
+                                               /* store away all the previous processor definitions */
+                                               int dcount;
+                                               
+                                               for (dcount = 1; dcount < num_processor_names; dcount++)
+                                                       create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
+                                                                  pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
+                                       }
+                                       
+                                       num_processor_names = split_words(processor_name, pic_buf_pos);
+                               }
+                               else {
+                                       /* if we've just completed reading a processor definition stop now */
+                                       if (found_processor)
+                                               done = TRUE;
+                                       else {
+                                               /* check if this processor name is a match */
+                                               for (wcount = 1; wcount < num_pic_words; wcount++) {
+                                                       
+                                                       /* skip uninteresting prefixes */
+                                                       char *found_name = sanitise_processor_name(pic_word[wcount]);
+
+                                                       if (STRCASECMP(found_name, simple_pic_name) == 0)
+                                                               found_processor = TRUE;
+                                               }
+                                       }
+                               }
+                       }
+                       
+                       else {
+                               if (found_processor || pic_name == NULL) {
+                                       /* only parse a processor section if we've found the one we want */
+                                       if (STRCASECMP(pic_word[0], "maxram") == 0 && num_pic_words > 1) {
+                                               pic_maxram = parse_config_value(pic_word[1]);
+                                               setMaxRAM(pic_maxram);
+                                       }
+                                       else if (STRCASECMP(pic_word[0], "bankmsk") == 0 && num_pic_words > 1)
+                                               pic_bankmsk = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "confsiz") == 0 && num_pic_words > 1)
+                                               pic_confsiz = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "program") == 0 && num_pic_words > 1)
+                                               pic_program = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "data") == 0 && num_pic_words > 1)
+                                               pic_data = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "eeprom") == 0 && num_pic_words > 1)
+                                               pic_eeprom = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "io") == 0 && num_pic_words > 1)
+                                               pic_io = parse_config_value(pic_word[1]);
+                       
+                                       else if (STRCASECMP(pic_word[0], "regmap") == 0 && num_pic_words > 2) {
+                                               if (found_processor)
+                                                       register_map(num_pic_words, pic_word);
+                                       }
+                                       else if (STRCASECMP(pic_word[0], "memmap") == 0 && num_pic_words > 2) {
+                                               if (found_processor)
+                                                       ram_map(num_pic_words, pic_word);
+                                       }
+                                       else {
+                                               fprintf(stderr, "WARNING: %s: bad syntax `%s'\n", DEVICE_FILE_NAME, pic_word[0]);
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       fclose(pic_file);
+
+       /* if we're in read-the-lot mode then create the final processor definition */
+       if (pic_name == NULL) {
+           
+               if (num_processor_names > 0) {
+                       /* store away all the previous processor definitions */
+                       int dcount;
+               
+                       for (dcount = 1; dcount < num_processor_names; dcount++)
+                               create_pic(processor_name[dcount], pic_maxram, pic_bankmsk,
+                                          pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
+               }
+       }
+       else {
+               /* in search mode */
+               if (found_processor) {
+                       /* create a new pic entry */
+                       return create_pic(pic_name, pic_maxram, pic_bankmsk,
+                                         pic_confsiz, pic_program, pic_data, pic_eeprom, pic_io);
+               }
+       }
+       
+       return NULL;
+}
+
 void addMemRange(memRange *r, int type)
 {
        int i;
        int alias = r->alias;
        
-       if (pic->maxRAMaddress < 0) {
-               fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+       if (maxRAMaddress < 0) {
+               fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
                return;
        }
        
        do {
                for (i=r->start_address; i<= r->end_address; i++) {
-                       if ((i|alias) <= pic->maxRAMaddress) {
+                       if ((i|alias) <= maxRAMaddress) {
+                               /* if we haven't seen this address before, enter it */
+                               if (!finalMapping[i | alias].isValid) {
                                finalMapping[i | alias].isValid = 1;
                                finalMapping[i | alias].alias = r->alias;
                                finalMapping[i | alias].bank  = r->bank;
@@ -131,9 +433,10 @@ void addMemRange(memRange *r, int type)
                                } 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);
+                                       __FILE__,__FUNCTION__,(i|alias), maxRAMaddress);
                        }
                }
                
@@ -150,20 +453,20 @@ void addMemRange(memRange *r, int type)
 void setMaxRAM(int size)
 {
        int i;
-       pic->maxRAMaddress = size;
+       maxRAMaddress = size;
        
-       if (pic->maxRAMaddress < 0) {
-               fprintf(stderr, "invalid \"#pragma maxram 0x%x\" setting\n",
-                       pic->maxRAMaddress);
+       if (maxRAMaddress < 0) {
+               fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
+                       maxRAMaddress, DEVICE_FILE_NAME);
                return;
        }
        
-       finalMapping = Safe_calloc(1+pic->maxRAMaddress,
+       finalMapping = Safe_calloc(1+maxRAMaddress,
                sizeof(AssignedMemory));
        
        /* Now initialize the finalMapping array */
        
-       for(i=0; i<=pic->maxRAMaddress; i++) {
+       for(i=0; i<=maxRAMaddress; i++) {
                finalMapping[i].reg = NULL;
                finalMapping[i].isValid = 0;
                finalMapping[i].bank = (i>>7);
@@ -203,7 +506,7 @@ int REGallBanks(regs *reg)
 int isSFR(int address)
 {
        
-       if( (address > pic->maxRAMaddress) || !finalMapping[address].isSFR)
+       if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
                return 0;
        
        return 1;
@@ -218,7 +521,7 @@ void dump_map(void)
 {
        int i;
        
-       for(i=0; i<=pic->maxRAMaddress; i++) {
+       for(i=0; i<=maxRAMaddress; i++) {
                //fprintf(stdout , "addr 0x%02x is %s\n", i, ((finalMapping[i].isValid) ? "valid":"invalid"));
                
                if(finalMapping[i].isValid) {
@@ -244,12 +547,12 @@ void dump_sfr(FILE *of)
        
        //dump_map();   /* display the register map */
        //fprintf(stdout,";dump_sfr  \n");
-       if (pic->maxRAMaddress < 0) {
-               fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+       if (maxRAMaddress < 0) {
+               fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
                return;
        }
        
-       for (addr = 0; addr <= pic->maxRAMaddress; addr++)
+       for (addr = 0; addr <= maxRAMaddress; addr++)
        {
                regs *reg = finalMapping[addr].reg;
                
@@ -258,7 +561,10 @@ void dump_sfr(FILE *of)
                  if (pic14_options.isLibrarySource && pic14_is_shared (reg))
                  {
                    /* rely on external declarations for the non-fixed stack */
-                   fprintf (of, "\textern\t%s\n", reg->name);
+                   /* Update: We always emit the STACK symbols into a
+                    * udata_shr section, so no extern declaration is
+                    * required. */
+                   //fprintf (of, "\textern\t%s\n", reg->name);
                  } else {
                    emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, pic14_is_shared (reg));
                  }
@@ -325,7 +631,7 @@ void dump_sfr(FILE *of)
                
                addr++;
                
-       } while(addr <= pic->maxRAMaddress);
+       } while(addr <= maxRAMaddress);
 
 
 #endif
@@ -343,33 +649,63 @@ void dump_sfr(FILE *of)
 *-----------------------------------------------------------------*/
 void list_valid_pics(int ncols, int list_alias)
 {
-       int col,longest;
-       int i,j,k,l;
+       int col=0,longest;
+       int i,j,k;
+       int max_alias = 1;
+       
+       if (num_of_supported_PICS == 0)
+               find_device(NULL);          /* load all the definitions */
        
        if(list_alias)
-               list_alias = sizeof(Pics[0].name) / sizeof(Pics[0].name[0]);
+               max_alias = PROCESSOR_NAMES;
        
        /* decrement the column number if it's greater than zero */
        ncols = (ncols > 1) ? ncols-1 : 4;
        
        /* Find the device with the longest name */
        for(i=0,longest=0; i<num_of_supported_PICS; i++) {
-               for(j=0; j<=list_alias; j++) {
-                       k = strlen(Pics[i].name[j]);
+               for(j=0; j<max_alias; j++) {
+                       k = strlen(Pics[i]->name[j]);
                        if(k>longest)
                                longest = k;
                }
        }
        
+#if 0
+       /* heading */
+       fprintf(stderr, "\nPIC14 processors and their characteristics:\n\n");
+       fprintf(stderr, " processor");
+       for(k=0; k<longest-1; k++)
+               fputc(' ',stderr);
+       fprintf(stderr, "program     RAM      EEPROM    I/O\n");
+       fprintf(stderr, "-----------------------------------------------------\n");
+       
+       for(i=0;  i < num_of_supported_PICS; i++) {
+               fprintf(stderr,"  %s", Pics[i]->name[0]);
+               l = longest + 2 - strlen(Pics[i]->name[0]);
+               for(k=0; k<l; k++)
+                       fputc(' ',stderr);
+       
+               fprintf(stderr, "     ");
+               if (Pics[i]->programMemSize % 1024 == 0)
+                       fprintf(stderr, "%4dK", Pics[i]->programMemSize / 1024);
+               else
+                       fprintf(stderr, "%5d", Pics[i]->programMemSize);
+       
+               fprintf(stderr, "     %5d     %5d     %4d\n", 
+                       Pics[i]->dataMemSize, Pics[i]->eepromMemSize, Pics[i]->ioPins);
+       }
+
        col = 0;
        
+       fprintf(stderr, "\nPIC14 processors supported:\n");
        for(i=0;  i < num_of_supported_PICS; i++) {
-               j = 0;
-               do {
                        
-                       fprintf(stderr,"%s", Pics[i].name[j]);
+               for (j = 0; j<max_alias; j++) {
+                       
+                       fprintf(stderr,"%s", Pics[i]->name[j]);
                        if(col<ncols) {
-                               l = longest + 2 - strlen(Pics[i].name[j]);
+                               l = longest + 2 - strlen(Pics[i]->name[j]);
                                for(k=0; k<l; k++)
                                        fputc(' ',stderr);
                                
@@ -380,34 +716,12 @@ void list_valid_pics(int ncols, int list_alias)
                                col = 0;
                        }
                        
-               } while(++j<list_alias);
+               }
                
        }
-       if(col != ncols)
+#endif
+       if(col != 0)
                fputc('\n',stderr);
-       
-}
-
-/*-----------------------------------------------------------------*
-*  
-*-----------------------------------------------------------------*/
-PIC_device *find_device(char *name)
-{
-       
-       int i,j;
-       
-       if(!name)
-               return NULL;
-       
-       for(i = 0; i<num_of_supported_PICS; i++) {
-               
-               for(j=0; j<PROCESSOR_NAMES; j++)
-                       if(!STRCASECMP(Pics[i].name[j], name) )
-                               return &Pics[i];
-       }
-       
-       /* not found */
-       return NULL; 
 }
 
 /*-----------------------------------------------------------------*
@@ -423,13 +737,9 @@ void init_pic(char *pic_type)
                else
                        fprintf(stderr, "No processor has been specified (use -pPROCESSOR_NAME)\n");
                
-               fprintf(stderr,"Valid devices are:\n");
-               
-               list_valid_pics(4,0);
+               list_valid_pics(7,0);
                exit(1);
        }
-       
-       pic->maxRAMaddress = -1;
 }
 
 /*-----------------------------------------------------------------*
@@ -437,7 +747,7 @@ void init_pic(char *pic_type)
 *-----------------------------------------------------------------*/
 int picIsInitialized(void)
 {
-       if(pic && pic->maxRAMaddress > 0)
+       if(pic && maxRAMaddress > 0)
                return 1;
        
        return 0;
@@ -453,7 +763,7 @@ char *processor_base_name(void)
        if(!pic)
                return NULL;
        
-       return pic->name[0];
+       return pic->name[1];
 }
 
 /*-----------------------------------------------------------------*
@@ -462,13 +772,13 @@ int validAddress(int address, int reg_size)
 {
        int i;
        
-       if (pic->maxRAMaddress < 0) {
-               fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+       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) > pic->maxRAMaddress)
+       if(address + (reg_size - 1) > maxRAMaddress)
                return 0;
        
        for (i=0; i<reg_size; i++)
@@ -493,8 +803,8 @@ void mapRegister(regs *reg)
                return;
        }
        
-       if (pic->maxRAMaddress < 0) {
-               fprintf(stderr, "missing \"#pragma maxram\" setting\n");
+       if (maxRAMaddress < 0) {
+               fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
                return;
        }
        
@@ -554,7 +864,7 @@ int assignRegister(regs *reg, int start_address)
        * so we'll search through all availble ram address and
                * assign the first one */
                
-               for (i=start_address; i<=pic->maxRAMaddress; i++) {
+               for (i=start_address; i<=maxRAMaddress; i++) {
                        
                        if (validAddress(i,reg->size)) {
                                reg->address = i;
@@ -626,54 +936,90 @@ void assignRelocatableRegisters(set *regset, int used)
 /*-----------------------------------------------------------------*
 *  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.
+* Most midrange PICs have one config word at address 0x2007.
+* Newer PIC14s have a second config word at address 0x2008.
+* This routine will assign values to those addresses.
 *
 *-----------------------------------------------------------------*/
 
 void assignConfigWordValue(int address, int value)
 {
-       if(CONFIG_WORD_ADDRESS == address)
+       if (CONFIG_WORD_ADDRESS == address)
                config_word = value;
        
-       //fprintf(stderr,"setting config word to 0x%x\n",value);
+       else if (CONFIG2_WORD_ADDRESS == address)
+               config2_word = value;
+       
+       //fprintf(stderr,"setting config word 0x%x to 0x%x\n", address, value);
        
 }
 /*-----------------------------------------------------------------*
 * int getConfigWord(int address)
 *
-* Get the current value of the config word.
+* Get the current value of a config word.
 *
 *-----------------------------------------------------------------*/
 
 int getConfigWord(int address)
 {
-       if(CONFIG_WORD_ADDRESS == address)
+       switch (address)
+       {
+       case CONFIG_WORD_ADDRESS:
                return config_word;
        
-       else
-               return 0;
+       case CONFIG2_WORD_ADDRESS:
+               return config2_word;
        
+       default:
+               return 0;
+       }
 }
 
 /*-----------------------------------------------------------------*
 *  
 *-----------------------------------------------------------------*/
-void setDefMaxRam(void)
+unsigned getMaxRam(void)
 {
-       unsigned i;
-       setMaxRAM(pic->defMaxRAMaddrs); /* Max RAM has not been included, so use default setting */
-       /* Validate full memory range for use by general purpose RAM */
-       for (i=0; i <= pic->defMaxRAMaddrs; i++) {
-               finalMapping[i].bank = (i>>7);
-               finalMapping[i].isValid = 1;
-       }
+       return pic->defMaxRAMaddrs;
 }
 
+
 /*-----------------------------------------------------------------*
-*  
+*  int getHasSecondConfigReg(void) - check if the device has a 
+*  second config register, rather than just one.
 *-----------------------------------------------------------------*/
-unsigned getMaxRam(void)
+int getHasSecondConfigReg(void)
 {
-       return pic->defMaxRAMaddrs;
+       if(!pic)
+               return 0;
+       else
+               return pic->hasSecondConfigReg;
+}
+
+/*-----------------------------------------------------------------*
+ * Query the size of the sharebank of the selected device.
+ * FIXME: Currently always returns 16.
+ *-----------------------------------------------------------------*/
+int pic14_getSharebankSize(void)
+{
+       return 16;
 }
+
+/*-----------------------------------------------------------------*
+ * Query the highest byte address occupied by the sharebank of the
+ * selected device.
+ * FIXME: Currently always returns 0x7f.
+ * THINK: Might not be needed, if we assign all shareable objects to
+ *        a `udata_shr' section and let the linker do the rest...
+ * Tried it, but yields `no target memory available' for pic16f877...
+ *-----------------------------------------------------------------*/
+int pic14_getSharebankAddress(void)
+{
+       int sharebankAddress = 0x7f;
+       /* If total RAM is less than 0x7f as with 16f84 then reduce
+        * sharebankAddress to fit */
+       if ((unsigned)sharebankAddress > getMaxRam())
+               sharebankAddress = (int)getMaxRam();
+       return sharebankAddress;
+}
+