/* */
/* */
/* */
-/* (C) 2000 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 2000-2003 Ullrich von Bassewitz */
+/* Römerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
const char* MakeLabelName (unsigned Addr)
/* Make the default label name from the given address and return it in a
* static buffer.
- */
+ */
{
static char LabelBuf [32];
xsprintf (LabelBuf, sizeof (LabelBuf), "L%04X", Addr);
+void AddExtLabelRange (unsigned Addr, const char* Name, unsigned Count)
+/* Add an external label for a range. The first entry gets the label "Name"
+ * while the others get "Name+offs".
+ */
+{
+ /* Define the label */
+ AddLabel (Addr, atExtLabel, Name);
+
+ /* Define dependent labels if necessary */
+ if (Count > 1) {
+ unsigned Offs;
+
+ /* Allocate memory for the dependent label names */
+ unsigned NameLen = strlen (Name);
+ char* DepName = xmalloc (NameLen + 7);
+ char* DepOffs = DepName + NameLen + 1;
+
+ /* Copy the original name into the buffer */
+ memcpy (DepName, Name, NameLen);
+ DepName[NameLen] = '+';
+
+ /* Define the labels */
+ for (Offs = 1; Offs < Count; ++Offs) {
+ sprintf (DepOffs, "%u", Offs);
+ AddLabel (Addr + Offs, atDepLabel, DepName);
+ }
+
+ /* Free the name buffer */
+ xfree (DepName);
+ }
+}
+
+
+
int HaveLabel (unsigned Addr)
/* Check if there is a label for the given address */
{
/* */
/* */
/* */
-/* (C) 2000 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 2000-2003 Ullrich von Bassewitz */
+/* Römerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
atCode = 0x01,
atIllegal = 0x02,
atByteTab = 0x03, /* Same as illegal */
- atWordTab = 0x04,
- atDWordTab = 0x05,
- atAddrTab = 0x06,
- atRtsTab = 0x07,
- atTextTab = 0x08,
+ atDByteTab = 0x04,
+ atWordTab = 0x05,
+ atDWordTab = 0x06,
+ atAddrTab = 0x07,
+ atRtsTab = 0x08,
+ atTextTab = 0x09,
/* Label flags */
atNoLabel = 0x00, /* No label for this address */
void AddLabel (unsigned Addr, attr_t Attr, const char* Name);
/* Add a label */
+void AddExtLabelRange (unsigned Addr, const char* Name, unsigned Count);
+/* Add an external label for a range. The first entry gets the label "Name"
+ * while the others get "Name+offs".
+ */
+
int HaveLabel (unsigned Addr);
/* Check if there is a label for the given address */
+unsigned GetCodeDByte (unsigned Addr)
+/* Get a dbyte from the given address */
+{
+ unsigned Lo = GetCodeByte (Addr);
+ unsigned Hi = GetCodeByte (Addr+1);
+ return (Lo <<8) | Hi;
+}
+
+
+
unsigned GetCodeWord (unsigned Addr)
/* Get a word from the given address */
{
unsigned char GetCodeByte (unsigned Addr);
/* Get a byte from the given address */
+unsigned GetCodeDByte (unsigned Addr);
+/* Get a dbyte from the given address */
+
unsigned GetCodeWord (unsigned Addr);
/* Get a word from the given address */
+unsigned DByteTable (void)
+/* Output a table of dbytes */
+{
+ /* Call the low level function */
+ return DoTable (atDByteTab, 2, DataDByteLine);
+}
+
+
+
unsigned WordTable (void)
/* Output a table of words */
{
/* Count how many bytes may be output. */
unsigned Count = GetSpan (atAddrTab);
- /* Handle Count == 1 ### */
+ /* Handle Count == 1 */
if (Count == 1) {
ByteTable ();
}
/* Count how many bytes may be output. */
unsigned Count = GetSpan (atRtsTab);
- /* Need to handle Count == 1 here!!! ### */
+ /* Handle Count == 1 */
+ if (Count == 1) {
+ ByteTable ();
+ }
/* Make the given number even */
Count &= ~1U;
unsigned ByteTable (void);
/* Output a table of bytes */
+unsigned DByteTable (void);
+/* Output a table of dbytes */
+
unsigned WordTable (void);
/* Output a table of words */
/* Stuff needed by many routines */
unsigned char Pass = 0; /* Disassembler pass */
+/* Comments */
+unsigned char Comments = 0; /* Add which comments to the output? */
+
/* Page formatting */
unsigned PageLength = 0; /* Length of a listing page */
unsigned MIndent = 9; /* Mnemonic indent */
/* Stuff needed by many routines */
extern unsigned char Pass; /* Disassembler pass */
+/* Comments */
+#define MIN_COMMENTS 0
+#define MAX_COMMENTS 4
+extern unsigned char Comments; /* Add which comments to the output? */
+
/* Page formatting */
#define MIN_PAGE_LEN 32
#define MAX_PAGE_LEN 127
/* Parse a global section */
{
static const IdentTok GlobalDefs[] = {
+ { "COMMENTS", INFOTOK_COMMENTS },
{ "CPU", INFOTOK_CPU },
{ "INPUTNAME", INFOTOK_INPUTNAME },
{ "OUTPUTNAME", INFOTOK_OUTPUTNAME },
/* Look at the token */
switch (InfoTok) {
+ case INFOTOK_COMMENTS:
+ InfoNextTok ();
+ InfoAssureInt ();
+ InfoRangeCheck (MIN_COMMENTS, MAX_COMMENTS);
+ Comments = InfoIVal;
+ InfoNextTok ();
+ break;
+
case INFOTOK_CPU:
InfoNextTok ();
InfoAssureStr ();
/* Parse a range section */
{
static const IdentTok RangeDefs[] = {
- { "START", INFOTOK_START },
{ "END", INFOTOK_END },
- { "TYPE", INFOTOK_TYPE },
+ { "NAME", INFOTOK_NAME },
+ { "START", INFOTOK_START },
+ { "TYPE", INFOTOK_TYPE },
};
static const IdentTok TypeDefs[] = {
- { "CODE", INFOTOK_CODE },
- { "BYTETABLE", INFOTOK_BYTETAB },
- { "WORDTABLE", INFOTOK_WORDTAB },
- { "DWORDTABLE", INFOTOK_DWORDTAB },
- { "ADDRTABLE", INFOTOK_ADDRTAB },
- { "RTSTABLE", INFOTOK_RTSTAB },
- { "TEXTTABLE", INFOTOK_TEXTTAB },
+ { "CODE", INFOTOK_CODE },
+ { "BYTETABLE", INFOTOK_BYTETAB },
+ { "DBYTETABLE", INFOTOK_DBYTETAB },
+ { "WORDTABLE", INFOTOK_WORDTAB },
+ { "DWORDTABLE", INFOTOK_DWORDTAB },
+ { "ADDRTABLE", INFOTOK_ADDRTAB },
+ { "RTSTABLE", INFOTOK_RTSTAB },
+ { "TEXTTABLE", INFOTOK_TEXTTAB },
};
/* Which values did we get? */
enum {
- tNone = 0x00,
- tStart = 0x01,
- tEnd = 0x02,
- tType = 0x04,
- tAll = 0x07
- } Needed = tNone;
+ tNone = 0x00,
+ tStart = 0x01,
+ tEnd = 0x02,
+ tType = 0x04,
+ tName = 0x08,
+ tNeeded = (tStart | tEnd | tType)
+ } Attributes = tNone;
/* Locals - initialize to avoid gcc warnings */
unsigned Start = 0;
unsigned End = 0;
unsigned char Type = 0;
+ char* Name = 0;
/* Skip the token */
InfoNextTok ();
/* Look at the token */
switch (InfoTok) {
- case INFOTOK_START:
- InfoNextTok ();
+ case INFOTOK_END:
+ InfoNextTok ();
InfoAssureInt ();
InfoRangeCheck (0x0000, 0xFFFF);
- Start = InfoIVal;
- Needed |= tStart;
+ End = InfoIVal;
+ Attributes |= tEnd;
InfoNextTok ();
break;
- case INFOTOK_END:
- InfoNextTok ();
+ case INFOTOK_NAME:
+ InfoNextTok ();
+ if (Name) {
+ InfoError ("Name already given");
+ }
+ InfoAssureStr ();
+ if (InfoSVal[0] == '\0') {
+ InfoError ("Name may not be empty");
+ }
+ Name = xstrdup (InfoSVal);
+ Attributes |= tName;
+ InfoNextTok ();
+ break;
+
+ case INFOTOK_START:
+ InfoNextTok ();
InfoAssureInt ();
InfoRangeCheck (0x0000, 0xFFFF);
- End = InfoIVal;
- Needed |= tEnd;
+ Start = InfoIVal;
+ Attributes |= tStart;
InfoNextTok ();
break;
switch (InfoTok) {
case INFOTOK_CODE: Type = atCode; break;
case INFOTOK_BYTETAB: Type = atByteTab; break;
+ case INFOTOK_DBYTETAB: Type = atDByteTab; break;
case INFOTOK_WORDTAB: Type = atWordTab; break;
case INFOTOK_DWORDTAB: Type = atDWordTab; break;
case INFOTOK_ADDRTAB: Type = atAddrTab; break;
case INFOTOK_RTSTAB: Type = atRtsTab; break;
case INFOTOK_TEXTTAB: Type = atTextTab; break;
}
- Needed |= tType;
+ Attributes |= tType;
InfoNextTok ();
break;
}
}
/* Did we get all required values? */
- if (Needed != tAll) {
+ if ((Attributes & tNeeded) != tNeeded) {
InfoError ("Required values missing from this section");
}
/* Set the range */
MarkRange (Start, End, Type);
+ /* Do we have a label? */
+ if (Attributes & tName) {
+ /* Define a label for the table */
+ AddLabel (Start, atExtLabel, Name);
+ /* Delete the name */
+ xfree (Name);
+ }
+
/* Consume the closing brace */
InfoConsumeRCurly ();
}
InfoError ("Label for address $%04lX already defined", Value);
}
- /* Define the label */
- AddLabel ((unsigned) Value, atExtLabel, Name);
-
- /* Define dependent labels if necessary */
- if (Size > 1) {
- unsigned Offs;
-
- /* Allocate memory for the dependent label names */
- unsigned NameLen = strlen (Name);
- char* DepName = xmalloc (NameLen + 7);
- char* DepOffs = DepName + NameLen + 1;
-
- /* Copy the original name into the buffer */
- memcpy (DepName, Name, NameLen);
- DepName[NameLen] = '+';
-
- /* Define the labels */
- for (Offs = 1; Offs < (unsigned) Size; ++Offs) {
- sprintf (DepOffs, "%u", Offs);
- AddLabel ((unsigned) Value+Offs, atDepLabel, DepName);
- }
-
- /* Free the name buffer */
- xfree (DepName);
- }
+ /* Define the label(s) */
+ AddExtLabelRange ((unsigned) Value, Name, Size);
/* Delete the dynamically allocated memory for Name */
xfree (Name);
" -V\t\t\tPrint the disassembler version\n"
"\n"
"Long options:\n"
+ " --comments n\t\tSet the comment level for the output\n"
" --cpu type\t\tSet cpu type\n"
" --debug-info\t\tAdd debug info to object file\n"
" --formfeeds\t\tAdd formfeeds to the output\n"
{
unsigned long Val;
int Converted;
+ char BoundsCheck;
/* Convert */
if (*Number == '$') {
++Number;
- Converted = sscanf (Number, "%lx", &Val);
+ Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
} else {
- Converted = sscanf (Number, "%li", (long*)&Val);
+ Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
}
/* Check if we do really have a number */
+static void OptComments (const char* Opt, const char* Arg)
+/* Handle the --comments option */
+{
+ /* Convert the argument to a number */
+ unsigned long Val = CvtNumber (Opt, Arg);
+
+ /* Check for a valid range */
+ if (Val > MAX_COMMENTS) {
+ Error ("Argument for %s outside valid range (%d-%d)",
+ Opt, MIN_COMMENTS, MAX_COMMENTS);
+ }
+
+ /* Use the value */
+ Comments = (unsigned char) Val;
+}
+
+
+
static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
/* Handle the --cpu option */
{
static void OptDebugInfo (const char* Opt attribute ((unused)),
- const char* Arg attribute ((unused)))
+ const char* Arg attribute ((unused)))
/* Add debug info to the object file */
{
DebugInfo = 1;
ByteTable ();
break;
+ case atDByteTab:
+ DByteTable ();
+ break;
+
case atWordTab:
WordTable ();
break;
{
/* Program long options */
static const LongOpt OptTab[] = {
+ { "--comments", 1, OptComments },
{ "--cpu", 1, OptCPU },
{ "--debug-info", 0, OptDebugInfo },
{ "--formfeeds", 0, OptFormFeeds },
/* common */
#include "cpu.h"
-#include "print.h"
#include "version.h"
/* da65 */
+void DataDByteLine (unsigned ByteCount)
+/* Output a line with dbytes */
+{
+ unsigned I;
+
+ Indent (MIndent);
+ Output (".dbyte");
+ Indent (AIndent);
+ for (I = 0; I < ByteCount; I += 2) {
+ if (I > 0) {
+ Output (",$%04X", GetCodeDByte (PC+I));
+ } else {
+ Output ("$%04X", GetCodeDByte (PC+I));
+ }
+ }
+ LineComment (PC, ByteCount);
+ LineFeed ();
+}
+
+
+
void DataWordLine (unsigned ByteCount)
/* Output a line with words */
{
void SeparatorLine (void)
/* Print a separator line */
{
- if (Pass == PassCount && Verbosity >= 1) {
+ if (Pass == PassCount && Comments >= 1) {
Output ("; ----------------------------------------------------------------------------");
LineFeed ();
}
{
unsigned I;
- if (Pass == PassCount && Verbosity >= 2) {
+ if (Pass == PassCount && Comments >= 2) {
Indent (CIndent);
Output ("; %04X", PC);
- if (Verbosity >= 3) {
+ if (Comments >= 3) {
for (I = 0; I < Count; ++I) {
Output (" %02X", CodeBuf [PC+I]);
}
- if (Verbosity >= 4) {
+ if (Comments >= 4) {
Indent (TIndent);
for (I = 0; I < Count; ++I) {
unsigned char C = CodeBuf [PC+I];
void DataByteLine (unsigned ByteCount);
/* Output a line with bytes */
+void DataDByteLine (unsigned ByteCount);
+/* Output a line with dbytes */
+
void DataWordLine (unsigned ByteCount);
/* Output a line with words */
INFOTOK_LABEL,
/* Global section */
+ INFOTOK_COMMENTS,
INFOTOK_CPU,
INFOTOK_INPUTNAME,
INFOTOK_OUTPUTNAME,
INFOTOK_TYPE,
INFOTOK_CODE,
- INFOTOK_BYTETAB,
+ INFOTOK_BYTETAB,
+ INFOTOK_DBYTETAB,
INFOTOK_WORDTAB,
INFOTOK_DWORDTAB,
INFOTOK_ADDRTAB,