From 61a1fa52c4bcad3506ad1445c18e4d8691f53874 Mon Sep 17 00:00:00 2001 From: cuz Date: Sat, 9 Dec 2000 10:10:07 +0000 Subject: [PATCH] Fixed several obvious omissions. Allow specifying a start address. git-svn-id: svn://svn.cc65.org/cc65/trunk@570 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/da65/code.c | 31 ++++++++++++++++++++++------ src/da65/config.c | 10 ++++++++++ src/da65/data.c | 5 ++++- src/da65/global.c | 1 + src/da65/global.h | 2 ++ src/da65/main.c | 50 ++++++++++++++++++++++++++++++++++++++++++---- src/da65/scanner.h | 1 + 7 files changed, 89 insertions(+), 11 deletions(-) diff --git a/src/da65/code.c b/src/da65/code.c index 2a879459c..3e58dea54 100644 --- a/src/da65/code.c +++ b/src/da65/code.c @@ -69,6 +69,7 @@ void LoadCode (const char* Name, unsigned long StartAddress) /* Load the code from the given file */ { unsigned Count, MaxCount; + long Size; FILE* F; @@ -80,18 +81,36 @@ void LoadCode (const char* Name, unsigned long StartAddress) /* Open the file */ F = fopen (Name, "rb"); if (F == 0) { - Error ("Cannot open `%s': %s", Name, strerror (errno)); + Error ("Cannot open `%s': %s", Name, strerror (errno)); + } + + /* Seek to the end to get the size of the file */ + if (fseek (F, 0, SEEK_END) != 0) { + Error ("Cannot seek on file `%s': %s", Name, strerror (errno)); + } + Size = ftell (F); + rewind (F); + + /* Check if the size is larger than what we can read */ + if (Size == 0) { + Error ("File `%s' contains no data", Name); + } + if (Size > MaxCount) { + Warning ("File `%s' is too large, ignoring %ld bytes", + Name, Size - MaxCount); + } else if (MaxCount > Size) { + MaxCount = (unsigned) Size; } /* Read from the file and remember the number of bytes read */ Count = fread (CodeBuf + StartAddress, 1, MaxCount, F); - if (ferror (F)) { - Error ("Error reading from `%s': %s", Name, strerror (errno)); - } - if (Count == 0) { - Error ("File `%s' contains no data", Name); + if (ferror (F) || Count != MaxCount) { + Error ("Error reading from `%s': %s", Name, strerror (errno)); } + /* Close the file */ + fclose (F); + /* Set the buffer variables */ CodeStart = PC = StartAddress; CodeEnd = CodeStart + Count - 1; /* CodeEnd is inclusive */ diff --git a/src/da65/config.c b/src/da65/config.c index 275ba7d50..7bbe46340 100644 --- a/src/da65/config.c +++ b/src/da65/config.c @@ -68,6 +68,7 @@ static void GlobalSection (void) { "INPUTNAME", CFGTOK_INPUTNAME }, { "OUTPUTNAME", CFGTOK_OUTPUTNAME }, { "PAGELENGTH", CFGTOK_PAGELENGTH }, + { "STARTADDR", CFGTOK_STARTADDR }, }; /* Skip the token */ @@ -114,6 +115,15 @@ static void GlobalSection (void) PageLength = CfgIVal; CfgNextTok (); break; + + case CFGTOK_STARTADDR: + CfgNextTok (); + CfgAssureInt (); + CfgRangeCheck (0x0000, 0xFFFF); + StartAddr = CfgIVal; + CfgNextTok (); + break; + } /* Directive is followed by a semicolon */ diff --git a/src/da65/data.c b/src/da65/data.c index ec8a64204..8e21fd621 100644 --- a/src/da65/data.c +++ b/src/da65/data.c @@ -154,7 +154,10 @@ unsigned AddrTable (void) /* Count how many bytes may be output. */ unsigned Count = GetSpan (atAddrTab); - /* Need to handle Count == 1 here!!! ### */ + /* Handle Count == 1 ### */ + if (Count == 1) { + ByteTable (); + } /* Make the given number even */ Count &= ~1U; diff --git a/src/da65/global.c b/src/da65/global.c index 01f1018ab..c7833a4fc 100644 --- a/src/da65/global.c +++ b/src/da65/global.c @@ -55,6 +55,7 @@ const char CfgExt[] = ".cfg"; /* Config file extension */ unsigned char Verbosity = 4; /* Verbosity of the output file */ unsigned char FormFeeds = 0; /* Add form feeds to the output? */ unsigned char PassCount = 2; /* How many passed do we do? */ +unsigned long StartAddr = 0xC000; /* Start/load address of the program */ /* Stuff needed by many routines */ unsigned char Pass = 0; /* Disassembler pass */ diff --git a/src/da65/global.h b/src/da65/global.h index 1c53b9dcd..4dcef7077 100644 --- a/src/da65/global.h +++ b/src/da65/global.h @@ -56,6 +56,8 @@ extern const char CfgExt[]; /* Config file extension */ extern unsigned char Verbosity; /* Verbosity of the output file */ extern unsigned char FormFeeds; /* Add form feeds to the output? */ extern unsigned char PassCount; /* How many passed do we do? */ +extern unsigned long StartAddr; /* Start/load address of the program */ + /* Stuff needed by many routines */ extern unsigned char Pass; /* Disassembler pass */ diff --git a/src/da65/main.c b/src/da65/main.c index 3164c189e..070c762dd 100644 --- a/src/da65/main.c +++ b/src/da65/main.c @@ -60,7 +60,7 @@ /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ @@ -76,20 +76,49 @@ static void Usage (void) " -o name\t\tName the output file\n" " -v\t\t\tIncrease verbosity\n" " -F\t\t\tAdd formfeeds to the output\n" - " -V\t\t\tPrint the assembler version\n" + " -S addr\t\tSet the start/load address\n" + " -V\t\t\tPrint the disassembler version\n" "\n" "Long options:\n" " --cpu type\t\tSet cpu type\n" " --formfeeds\t\tAdd formfeeds to the output\n" " --help\t\tHelp (this text)\n" " --pagelength n\tSet the page length for the listing\n" + " --start-addr addr\tSet the start/load address\n" " --verbose\t\tIncrease verbosity\n" - " --version\t\tPrint the assembler version\n", + " --version\t\tPrint the disassembler version\n", ProgName); } +static unsigned long CvtNumber (const char* Arg, const char* Number) +/* Convert a number from a string. Allow '$' and '0x' prefixes for hex + * numbers. + */ +{ + unsigned long Val; + int Converted; + + /* Convert */ + if (*Number == '$') { + ++Number; + Converted = sscanf (Number, "%lx", &Val); + } else { + Converted = sscanf (Number, "%li", (long*)&Val); + } + + /* Check if we do really have a number */ + if (Converted != 1) { + Error ("Invalid number given in argument: %s\n", Arg); + } + + /* Return the result */ + return Val; +} + + + static void OptCPU (const char* Opt, const char* Arg) /* Handle the --cpu option */ { @@ -146,6 +175,14 @@ static void OptPageLength (const char* Opt, const char* Arg) +static void OptStartAddr (const char* Opt, const char* Arg) +/* Set the default start address */ +{ + StartAddr = CvtNumber (Opt, Arg); +} + + + static void OptVerbose (const char* Opt, const char* Arg) /* Increase verbosity */ { @@ -280,6 +317,7 @@ int main (int argc, char* argv []) { "--formfeeds", 0, OptFormFeeds }, { "--help", 0, OptHelp }, { "--pagelength", 1, OptPageLength }, + { "--start-addr", 1, OptStartAddr }, { "--verbose", 0, OptVerbose }, { "--version", 0, OptVersion }, }; @@ -316,6 +354,10 @@ int main (int argc, char* argv []) OptVerbose (Arg, 0); break; + case 'S': + OptStartAddr (Arg, GetArg (&I, 2)); + break; + case 'V': OptVersion (Arg, 0); break; @@ -359,7 +401,7 @@ int main (int argc, char* argv []) } /* Load the input file */ - LoadCode (InFile, 0xE000); /* ### */ + LoadCode (InFile, StartAddr); /* Open the output file */ OpenOutput (OutFile); diff --git a/src/da65/scanner.h b/src/da65/scanner.h index 441ab6c0a..51b033386 100644 --- a/src/da65/scanner.h +++ b/src/da65/scanner.h @@ -68,6 +68,7 @@ typedef enum token_t { CFGTOK_INPUTNAME, CFGTOK_OUTPUTNAME, CFGTOK_PAGELENGTH, + CFGTOK_STARTADDR, /* Range section */ CFGTOK_START, -- 2.39.5