1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Usage (void)
73 /* Print usage information and exit */
75 printf ("Usage: %s [options] [inputfile]\n"
77 " -g\t\t\tAdd debug info to object file\n"
78 " -h\t\t\tHelp (this text)\n"
79 " -i name\t\tSpecify an info file\n"
80 " -o name\t\tName the output file\n"
81 " -v\t\t\tIncrease verbosity\n"
82 " -F\t\t\tAdd formfeeds to the output\n"
83 " -S addr\t\tSet the start/load address\n"
84 " -V\t\t\tPrint the disassembler version\n"
87 " --argument-column n\tSpecify argument start column\n"
88 " --comment-column n\tSpecify comment start column\n"
89 " --comments n\t\tSet the comment level for the output\n"
90 " --cpu type\t\tSet cpu type\n"
91 " --debug-info\t\tAdd debug info to object file\n"
92 " --formfeeds\t\tAdd formfeeds to the output\n"
93 " --help\t\tHelp (this text)\n"
94 " --hexoffs\t\tUse hexadecimal label offsets\n"
95 " --info name\t\tSpecify an info file\n"
96 " --label-break n\tAdd newline if label exceeds length n\n"
97 " --mnemonic-column n\tSpecify mnemonic start column\n"
98 " --pagelength n\tSet the page length for the listing\n"
99 " --start-addr addr\tSet the start/load address\n"
100 " --text-column n\tSpecify text start column\n"
101 " --verbose\t\tIncrease verbosity\n"
102 " --version\t\tPrint the disassembler version\n",
108 static void RangeCheck (const char* Opt, unsigned long Val,
109 unsigned long Min, unsigned long Max)
110 /* Do a range check for the given option and abort if there's a range
114 if (Val < Min || Val > Max) {
115 Error ("Argument for %s outside valid range (%ld-%ld)", Opt, Min, Max);
121 static unsigned long CvtNumber (const char* Arg, const char* Number)
122 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
131 if (*Number == '$') {
133 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
135 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
138 /* Check if we do really have a number */
139 if (Converted != 1) {
140 Error ("Invalid number given in argument: %s\n", Arg);
143 /* Return the result */
149 static void OptArgumentColumn (const char* Opt, const char* Arg)
150 /* Handle the --argument-column option */
152 /* Convert the argument to a number */
153 unsigned long Val = CvtNumber (Opt, Arg);
155 /* Check for a valid range */
156 RangeCheck (Opt, Val, MIN_ACOL, MAX_ACOL);
159 ACol = (unsigned char) Val;
164 static void OptBytesPerLine (const char* Opt, const char* Arg)
165 /* Handle the --bytes-per-line option */
167 /* Convert the argument to a number */
168 unsigned long Val = CvtNumber (Opt, Arg);
170 /* Check for a valid range */
171 RangeCheck (Opt, Val, MIN_BYTESPERLINE, MAX_BYTESPERLINE);
174 BytesPerLine = (unsigned char) Val;
179 static void OptCommentColumn (const char* Opt, const char* Arg)
180 /* Handle the --comment-column option */
182 /* Convert the argument to a number */
183 unsigned long Val = CvtNumber (Opt, Arg);
185 /* Check for a valid range */
186 RangeCheck (Opt, Val, MIN_CCOL, MAX_CCOL);
189 CCol = (unsigned char) Val;
194 static void OptComments (const char* Opt, const char* Arg)
195 /* Handle the --comments option */
197 /* Convert the argument to a number */
198 unsigned long Val = CvtNumber (Opt, Arg);
200 /* Check for a valid range */
201 RangeCheck (Opt, Val, MIN_COMMENTS, MAX_COMMENTS);
204 Comments = (unsigned char) Val;
209 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
210 /* Handle the --cpu option */
212 /* Find the CPU from the given name */
219 static void OptDebugInfo (const char* Opt attribute ((unused)),
220 const char* Arg attribute ((unused)))
221 /* Add debug info to the object file */
228 static void OptFormFeeds (const char* Opt attribute ((unused)),
229 const char* Arg attribute ((unused)))
230 /* Add form feeds to the output */
237 static void OptHelp (const char* Opt attribute ((unused)),
238 const char* Arg attribute ((unused)))
239 /* Print usage information and exit */
247 static void OptHexOffs (const char* Opt attribute ((unused)),
248 const char* Arg attribute ((unused)))
249 /* Handle the --hexoffs option */
256 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
257 /* Handle the --info option */
264 static void OptLabelBreak (const char* Opt, const char* Arg)
265 /* Handle the --label-break option */
267 /* Convert the argument to a number */
268 unsigned long Val = CvtNumber (Opt, Arg);
270 /* Check for a valid range */
271 RangeCheck (Opt, Val, MIN_LABELBREAK, MAX_LABELBREAK);
274 LBreak = (unsigned char) Val;
279 static void OptMnemonicColumn (const char* Opt, const char* Arg)
280 /* Handle the --mnemonic-column option */
282 /* Convert the argument to a number */
283 unsigned long Val = CvtNumber (Opt, Arg);
285 /* Check for a valid range */
286 RangeCheck (Opt, Val, MIN_MCOL, MAX_MCOL);
289 MCol = (unsigned char) Val;
294 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
295 /* Handle the --pagelength option */
297 int Len = atoi (Arg);
299 RangeCheck (Opt, Len, MIN_PAGE_LEN, MAX_PAGE_LEN);
306 static void OptStartAddr (const char* Opt, const char* Arg)
307 /* Set the default start address */
309 StartAddr = CvtNumber (Opt, Arg);
314 static void OptTextColumn (const char* Opt, const char* Arg)
315 /* Handle the --text-column option */
317 /* Convert the argument to a number */
318 unsigned long Val = CvtNumber (Opt, Arg);
320 /* Check for a valid range */
321 RangeCheck (Opt, Val, MIN_TCOL, MAX_TCOL);
324 TCol = (unsigned char) Val;
329 static void OptVerbose (const char* Opt attribute ((unused)),
330 const char* Arg attribute ((unused)))
331 /* Increase verbosity */
338 static void OptVersion (const char* Opt attribute ((unused)),
339 const char* Arg attribute ((unused)))
340 /* Print the disassembler version */
342 fprintf (stderr, "da65 V%s\n", GetVersionAsString ());
347 static void OneOpcode (unsigned RemainingBytes)
348 /* Disassemble one opcode */
350 /* Get the opcode from the current address */
351 unsigned char OPC = GetCodeByte (PC);
353 /* Get the opcode description for the opcode byte */
354 const OpcDesc* D = &OpcTable[OPC];
356 /* Get the output style for the current PC */
357 attr_t Style = GetStyleAttr (PC);
359 /* If we have a label at this address, output the label and an attached
360 * comment, provided that we aren't in a skip area.
362 if (Style != atSkip && MustDefLabel (PC)) {
363 const char* Comment = GetComment (PC);
365 UserComment (Comment);
367 DefLabel (GetLabelName (PC));
371 * - ...if we have enough bytes remaining for the code at this address.
372 * - ...if the current instruction is valid for the given CPU.
373 * - ...if there is no label somewhere between the instruction bytes.
374 * If any of these conditions is false, switch to data mode.
376 if (Style == atDefault) {
377 if (D->Size > RemainingBytes) {
379 MarkAddr (PC, Style);
380 } else if (D->Flags & flIllegal) {
382 MarkAddr (PC, Style);
385 for (I = 1; I < D->Size; ++I) {
386 if (HaveLabel (PC+I) || HaveSegmentChange (PC+I)) {
388 MarkAddr (PC, Style);
395 /* Disassemble the line */
404 /* Beware: If we don't have enough bytes left to disassemble the
405 * following insn, fall through to byte mode.
407 if (D->Size <= RemainingBytes) {
408 /* Output labels within the next insn */
410 for (I = 1; I < D->Size; ++I) {
413 /* Output the insn */
462 static void OnePass (void)
463 /* Make one pass through the code */
467 /* Disassemble until nothing left */
468 while ((Count = GetRemainingBytes()) > 0) {
475 static void Disassemble (void)
476 /* Disassemble the code */
482 Output ("---------------------------");
489 DefOutOfRangeLabels ();
495 int main (int argc, char* argv [])
496 /* Assembler main program */
498 /* Program long options */
499 static const LongOpt OptTab[] = {
500 { "--argument-column", 1, OptArgumentColumn },
501 { "--bytes-per-line", 1, OptBytesPerLine },
502 { "--comment-column", 1, OptCommentColumn },
503 { "--comments", 1, OptComments },
504 { "--cpu", 1, OptCPU },
505 { "--debug-info", 0, OptDebugInfo },
506 { "--formfeeds", 0, OptFormFeeds },
507 { "--help", 0, OptHelp },
508 { "--hexoffs", 0, OptHexOffs },
509 { "--info", 1, OptInfo },
510 { "--label-break", 1, OptLabelBreak },
511 { "--mnemonic-column", 1, OptMnemonicColumn },
512 { "--pagelength", 1, OptPageLength },
513 { "--start-addr", 1, OptStartAddr },
514 { "--text-column", 1, OptTextColumn },
515 { "--verbose", 0, OptVerbose },
516 { "--version", 0, OptVersion },
522 /* Initialize the cmdline module */
523 InitCmdLine (&argc, &argv, "da65");
525 /* Check the parameters */
527 while (I < ArgCount) {
529 /* Get the argument */
530 const char* Arg = ArgVec[I];
532 /* Check for an option */
533 if (Arg [0] == '-') {
537 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
541 OptDebugInfo (Arg, 0);
549 OptInfo (Arg, GetArg (&I, 2));
553 OutFile = GetArg (&I, 2);
561 OptStartAddr (Arg, GetArg (&I, 2));
574 /* Filename. Check if we already had one */
576 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
588 /* Try to read the info file */
591 /* Must have an input file */
593 AbEnd ("No input file");
596 /* Check the formatting options for reasonable values. Note: We will not
597 * really check that they make sense, just that they aren't complete
601 AbEnd ("mnemonic-column value must be smaller than argument-column value");
604 AbEnd ("argument-column value must be smaller than comment-column value");
607 AbEnd ("comment-column value must be smaller than text-column value");
610 /* If no CPU given, use the default CPU */
611 if (CPU == CPU_UNKNOWN) {
615 /* Get the current time and convert it to string so it can be used in
616 * the output page headers.
619 strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
621 /* Load the input file */
624 /* Open the output file */
625 OpenOutput (OutFile);
627 /* Disassemble the code */
630 /* Close the output file */