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"
-#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;
+/*
+ * 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;
-AssignedMemory *finalMapping=NULL;
+static int maxRAMaddress = 0;
#define CONFIG_WORD_ADDRESS 0x2007
#define CONFIG2_WORD_ADDRESS 0x2008
#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;
-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)
/* 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;
+ 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;
+ r = Safe_calloc(1, sizeof(memRange));
- addMemRange(&r, 1);
+ r->start_address = parse_config_value(word[pcount]);
+ r->end_address = parse_config_value(word[pcount]);
+ r->alias = parse_config_value(word[1]);
+ r->bank = (r->start_address >> 7) & 3;
+ // add memRange to device entry for future lookup (sharebanks)
+ r->next = rangeRAM;
+ rangeRAM = r;
}
}
r->alias = parse_config_value(word[3]);
r->bank = (r->start_address >> 7) & 3;
- addMemRange(r, 0);
-
// add memRange to device entry for future lookup (sharebanks)
r->next = rangeRAM;
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;
/* 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';
while (fgets(pic_buf, sizeof(pic_buf)-1, pic_file) != NULL && !done) {
+ unsigned llen;
+ llen = strlen (pic_buf);
/* remove trailing spaces */
- while (isspace(pic_buf[strlen(pic_buf)-1]))
- pic_buf[strlen(pic_buf)-1] = '\0';
+ while (llen && isspace(pic_buf[llen-1])) {
+ pic_buf[llen-1] = '\0';
+ llen--;
+ }
/* remove leading spaces */
for (pic_buf_pos = pic_buf; isspace(*pic_buf_pos); pic_buf_pos++)
return NULL;
}
-void addMemRange(memRange *r, int type)
-{
- int i;
- int alias = r->alias;
-
- //fprintf (stderr, "%s: range %x..%x, alias %x, bank %x\n", __FUNCTION__, r->start_address, r->end_address, r->alias, r->bank);
-
- 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) <= 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;
- if(type) {
- /* hack for now */
- finalMapping[i | alias].isSFR = 1;
- } else {
- finalMapping[i | alias].isSFR = 0;
- }
- }
- } else {
- if (getenv("SDCCPICDEBUG")) {
- fprintf(stderr, "WARNING: %s:%s memory at 0x%x is beyond max ram = 0x%x\n",
- __FILE__,__FUNCTION__,(i|alias), maxRAMaddress);
- }
- }
- }
-
- /* Decrement alias */
- if (alias) {
- alias -= ((alias & (alias - 1)) ^ alias);
- } else {
- alias--;
- }
-
- } while (alias >= 0);
-}
-
-void setMaxRAM(int size)
-{
- int i;
- maxRAMaddress = size;
-
- if (maxRAMaddress < 0) {
- fprintf(stderr, "invalid maxram 0x%x setting in %s\n",
- maxRAMaddress, DEVICE_FILE_NAME);
- return;
- }
-
- finalMapping = Safe_calloc(1+maxRAMaddress,
- sizeof(AssignedMemory));
-
- /* Now initialize the finalMapping array */
-
- for(i=0; i<=maxRAMaddress; i++) {
- finalMapping[i].reg = NULL;
- finalMapping[i].isValid = 0;
- finalMapping[i].bank = (i>>7);
- }
-}
-
-/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-
-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);
-
-}
-
/*-----------------------------------------------------------------*
-*-----------------------------------------------------------------*/
-
-int isSFR(int address)
-{
-
- if( (address > maxRAMaddress) || !finalMapping[address].isSFR)
- return 0;
-
- return 1;
-
-}
-
-/*
-* dump_map -- debug stuff
-*/
-
-void dump_map(void)
-{
- int 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) {
- fprintf(stderr,"addr: 0x%02x",i);
- if(finalMapping[i].isSFR)
- fprintf(stderr," isSFR");
- if(finalMapping[i].reg)
- fprintf( stderr, " reg %s", finalMapping[i].reg->name);
- fprintf(stderr, "\n");
- }
- }
-
-}
-
-void dump_sfr(FILE *of)
-{
-#if 0
- int start=-1;
- int bank_base;
- static int udata_flag=0;
-#endif
- int addr=0;
-
- //dump_map(); /* display the register map */
- //fprintf(stdout,";dump_sfr \n");
- if (maxRAMaddress < 0) {
- fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
- return;
- }
-
- for (addr = 0; addr <= maxRAMaddress; addr++)
- {
- regs *reg = finalMapping[addr].reg;
-
- if (reg && !reg->isEmitted)
- {
- emitSymbolToFile (of, reg->name, "udata", reg->size, reg->isFixed ? reg->address : -1, 0, 0);
-
- reg->isEmitted = 1;
- }
- } // for
-
-#if 0
- do {
-
- if(finalMapping[addr].reg && !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 ( (start != addr) && (!udata_flag) ) {
- udata_flag = 1;
- //fprintf(of,"\tudata\n");
- }
-
- for( ; start < addr; start++) {
- if((finalMapping[start].reg) &&
- (!finalMapping[start].reg->isEmitted) &&
- (!finalMapping[start].instance) &&
- (!finalMapping[start].isSFR)) {
-
- if (finalMapping[start].reg->isFixed) {
- unsigned i;
- for (i=0; i<finalMapping[start].reg->size; i++) {
- fprintf(of,"%s\tEQU\t0x%04x\n",
- finalMapping[start].reg->name,
- finalMapping[start].reg->address+i);
- }
- } else {
- emitSymbolToFile (of, finalMapping[start].reg->name, finalMapping[start].reg->size);
-#if 0
- fprintf(of,"%s\tres\t%i\n",
- finalMapping[start].reg->name,
- finalMapping[start].reg->size);
-#endif
- }
- finalMapping[start].reg->isEmitted = 1;
- }
- }
-
- start = -1;
- }
-
- }
-
- addr++;
-
- } while(addr <= maxRAMaddress);
-
-
-#endif
-}
-
-/*-----------------------------------------------------------------*
-* void list_valid_pics(int ncols, int list_alias)
-*
-* Print out a formatted list of valid PIC devices
-*
-* ncols - number of columns in the list.
-*
-* 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)
+ * void list_valid_pics(int ncols, int list_alias)
+ *
+ * Print out a formatted list of valid PIC devices
+ *
+ * ncols - number of columns in the list.
+ *
+ * list_alias - if non-zero, print all of the supported aliases
+ * for a device (e.g. F84, 16F84, etc...)
+ *-----------------------------------------------------------------*/
+static void list_valid_pics(int ncols)
{
int col=0,longest;
int i,k,l;
return pic->name;
}
+#if 0
/*-----------------------------------------------------------------*
*-----------------------------------------------------------------*/
-int validAddress(int address, int reg_size)
+static int validAddress(int address, int reg_size)
{
- int i;
-
if (maxRAMaddress < 0) {
fprintf(stderr, "missing maxram setting in %s\n", DEVICE_FILE_NAME);
return 0;
if(address + (reg_size - 1) > maxRAMaddress)
return 0;
- for (i=0; i<reg_size; i++)
- if(!finalMapping[address + i].isValid ||
- finalMapping[address+i].reg ||
- finalMapping[address+i].isSFR )
- return 0;
-
- return 1;
+ return 1;
}
+#endif
+
+#if 0
+/* The following code should be (and is) implemented in the linker. */
/*-----------------------------------------------------------------*
*-----------------------------------------------------------------*/
return reg->address;
}
- if( isSFR(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);
}
}
+#endif
-/* Keep track of whether we found an assignment to the __config words. */
-static int pic14_hasSetConfigWord = 0;
+int IS_CONFIG_ADDRESS(int address)
+{
+
+ return ((address == CONFIG_WORD_ADDRESS)
+ || (address == CONFIG2_WORD_ADDRESS));
+}
/*-----------------------------------------------------------------*
- * 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.
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)
*
*
*-----------------------------------------------------------------*/
-int pic14_getConfigWord(int address)
+static int pic14_getConfigWord(int address)
{
switch (address)
{
}
}
+#if 0
/*-----------------------------------------------------------------*
*
*-----------------------------------------------------------------*/
-unsigned pic14_getMaxRam(void)
+static unsigned pic14_getMaxRam(void)
{
- return pic->defMaxRAMaddrs;
+ return pic->defMaxRAMaddrs;
}
-
+#endif
/*-----------------------------------------------------------------*
* 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;
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;
r = pic->ram;
while (r) {
- //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask);
- if (r->alias == pic->bankMask) {
+ //fprintf (stderr, "%s: region %x..%x, bank %x, alias %x, pic->bankmask %x, min_size %d\n", __FUNCTION__, r->start_address, r->end_address, r->bank, r->alias, pic->bankMask, size ? *size : 0);
+ // find sufficiently large shared region
+ if ((r->alias == pic->bankMask)
+ && (r->end_address != r->start_address) // ignore SFRs
+ && (!size || (*size <= (r->end_address - r->start_address + 1))))
+ {
if (low) *low = r->start_address;
if (high) *high = r->end_address;
if (size) *size = r->end_address - r->start_address + 1;
/*
* 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;
int haveShared;
int l, h, s;
+ s = options.stack_size ? options.stack_size : 0x10;
haveShared = pic14_hasSharebank(&l, &h, &s);
if ((options.stack_loc != 0) || !haveShared)
{
// sharebank not available or not to be used
s = options.stack_size ? options.stack_size : 0x10;
l = options.stack_loc ? options.stack_loc : 0x20;
- h = (options.stack_loc ? options.stack_loc : 0x20) + s - 1;
+ h = l + s - 1;
if (low) *low = l;
if (high) *high = h;
if (size) *size = s;