1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Usage (void)
70 /* Print usage information and exit */
73 "Usage: %s [options] [inputfile]\n"
75 " -g\t\t\tAdd debug info to object file\n"
76 " -h\t\t\tHelp (this text)\n"
77 " -i name\t\tSpecify an info file\n"
78 " -o name\t\tName the output file\n"
79 " -v\t\t\tIncrease verbosity\n"
80 " -F\t\t\tAdd formfeeds to the output\n"
81 " -S addr\t\tSet the start/load address\n"
82 " -V\t\t\tPrint the disassembler version\n"
85 " --comments n\t\tSet the comment level for the output\n"
86 " --cpu type\t\tSet cpu type\n"
87 " --debug-info\t\tAdd debug info to object file\n"
88 " --formfeeds\t\tAdd formfeeds to the output\n"
89 " --help\t\tHelp (this text)\n"
90 " --info name\t\tSpecify an info file\n"
91 " --pagelength n\tSet the page length for the listing\n"
92 " --start-addr addr\tSet the start/load address\n"
93 " --verbose\t\tIncrease verbosity\n"
94 " --version\t\tPrint the disassembler version\n",
100 static unsigned long CvtNumber (const char* Arg, const char* Number)
101 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
110 if (*Number == '$') {
112 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
114 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
117 /* Check if we do really have a number */
118 if (Converted != 1) {
119 Error ("Invalid number given in argument: %s\n", Arg);
122 /* Return the result */
128 static void OptComments (const char* Opt, const char* Arg)
129 /* Handle the --comments option */
131 /* Convert the argument to a number */
132 unsigned long Val = CvtNumber (Opt, Arg);
134 /* Check for a valid range */
135 if (Val > MAX_COMMENTS) {
136 Error ("Argument for %s outside valid range (%d-%d)",
137 Opt, MIN_COMMENTS, MAX_COMMENTS);
141 Comments = (unsigned char) Val;
146 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
147 /* Handle the --cpu option */
149 /* Find the CPU from the given name */
156 static void OptDebugInfo (const char* Opt attribute ((unused)),
157 const char* Arg attribute ((unused)))
158 /* Add debug info to the object file */
165 static void OptFormFeeds (const char* Opt attribute ((unused)),
166 const char* Arg attribute ((unused)))
167 /* Add form feeds to the output */
174 static void OptHelp (const char* Opt attribute ((unused)),
175 const char* Arg attribute ((unused)))
176 /* Print usage information and exit */
184 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
185 /* Handle the --info option */
192 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
193 /* Handle the --pagelength option */
195 int Len = atoi (Arg);
196 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
197 AbEnd ("Invalid page length: %d", Len);
204 static void OptStartAddr (const char* Opt, const char* Arg)
205 /* Set the default start address */
207 StartAddr = CvtNumber (Opt, Arg);
212 static void OptVerbose (const char* Opt attribute ((unused)),
213 const char* Arg attribute ((unused)))
214 /* Increase verbosity */
221 static void OptVersion (const char* Opt attribute ((unused)),
222 const char* Arg attribute ((unused)))
223 /* Print the disassembler version */
226 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
227 VER_MAJOR, VER_MINOR, VER_PATCH);
232 static void OneOpcode (unsigned RemainingBytes)
233 /* Disassemble one opcode */
235 /* Get the opcode from the current address */
236 unsigned char OPC = GetCodeByte (PC);
238 /* Get the opcode description for the opcode byte */
239 const OpcDesc* D = &OpcTable[OPC];
241 /* If we have a label at this address, output the label */
242 if (MustDefLabel (PC)) {
243 DefLabel (GetLabel (PC));
247 * - ...if we have enough bytes remaining for the code at this address.
248 * - ...if the current instruction is valid for the given CPU.
249 * - ...if there is no label somewhere between the instruction bytes.
250 * If any of these conditions is false, switch to data mode.
252 if (GetStyleAttr (PC) == atDefault) {
253 if (D->Size > RemainingBytes) {
254 MarkAddr (PC, atIllegal);
255 } else if (D->Flags & flIllegal) {
256 MarkAddr (PC, atIllegal);
259 for (I = 1; I < D->Size; ++I) {
260 if (HaveLabel (PC+I)) {
261 MarkAddr (PC, atIllegal);
268 /* Disassemble the line */
269 switch (GetStyleAttr (PC)) {
315 static void OnePass (void)
316 /* Make one pass through the code */
320 /* Disassemble until nothing left */
321 while ((Count = GetRemainingBytes()) > 0) {
328 static void Disassemble (void)
329 /* Disassemble the code */
335 Output ("---------------------------");
342 DefOutOfRangeLabels ();
348 int main (int argc, char* argv [])
349 /* Assembler main program */
351 /* Program long options */
352 static const LongOpt OptTab[] = {
353 { "--comments", 1, OptComments },
354 { "--cpu", 1, OptCPU },
355 { "--debug-info", 0, OptDebugInfo },
356 { "--formfeeds", 0, OptFormFeeds },
357 { "--help", 0, OptHelp },
358 { "--info", 1, OptInfo },
359 { "--pagelength", 1, OptPageLength },
360 { "--start-addr", 1, OptStartAddr },
361 { "--verbose", 0, OptVerbose },
362 { "--version", 0, OptVersion },
367 /* Initialize the cmdline module */
368 InitCmdLine (&argc, &argv, "da65");
370 /* Check the parameters */
372 while (I < ArgCount) {
374 /* Get the argument */
375 const char* Arg = ArgVec[I];
377 /* Check for an option */
378 if (Arg [0] == '-') {
382 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
386 OptDebugInfo (Arg, 0);
394 OptInfo (Arg, GetArg (&I, 2));
398 OutFile = GetArg (&I, 2);
406 OptStartAddr (Arg, GetArg (&I, 2));
419 /* Filename. Check if we already had one */
421 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
433 /* Try to read the info file */
436 /* Must have an input file */
438 AbEnd ("No input file");
441 /* Make the output file name from the input file name if none was given */
443 OutFile = MakeFilename (InFile, OutExt);
446 /* If no CPU given, use the default CPU */
447 if (CPU == CPU_UNKNOWN) {
451 /* Load the input file */
454 /* Open the output file */
455 OpenOutput (OutFile);
457 /* Disassemble the code */
460 /* Close the output file */