1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2009, 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 */
343 "da65 V%s - (C) Copyright 2000-2009, Ullrich von Bassewitz\n",
344 GetVersionAsString ());
349 static void OneOpcode (unsigned RemainingBytes)
350 /* Disassemble one opcode */
352 /* Get the opcode from the current address */
353 unsigned char OPC = GetCodeByte (PC);
355 /* Get the opcode description for the opcode byte */
356 const OpcDesc* D = &OpcTable[OPC];
358 /* Get the output style for the current PC */
359 attr_t Style = GetStyleAttr (PC);
361 /* If we have a label at this address, output the label and an attached
362 * comment, provided that we aren't in a skip area.
364 if (Style != atSkip && MustDefLabel (PC)) {
365 const char* Comment = GetComment (PC);
367 UserComment (Comment);
369 DefLabel (GetLabelName (PC));
373 * - ...if we have enough bytes remaining for the code at this address.
374 * - ...if the current instruction is valid for the given CPU.
375 * - ...if there is no label somewhere between the instruction bytes.
376 * If any of these conditions is false, switch to data mode.
378 if (Style == atDefault) {
379 if (D->Size > RemainingBytes) {
381 MarkAddr (PC, Style);
382 } else if (D->Flags & flIllegal) {
384 MarkAddr (PC, Style);
387 for (I = 1; I < D->Size; ++I) {
388 if (HaveLabel (PC+I) || HaveSegmentChange (PC+I)) {
390 MarkAddr (PC, Style);
397 /* Disassemble the line */
406 /* Beware: If we don't have enough bytes left to disassemble the
407 * following insn, fall through to byte mode.
409 if (D->Size <= RemainingBytes) {
410 /* Output labels within the next insn */
412 for (I = 1; I < D->Size; ++I) {
415 /* Output the insn */
464 static void OnePass (void)
465 /* Make one pass through the code */
469 /* Disassemble until nothing left */
470 while ((Count = GetRemainingBytes()) > 0) {
477 static void Disassemble (void)
478 /* Disassemble the code */
484 Output ("---------------------------");
491 DefOutOfRangeLabels ();
497 int main (int argc, char* argv [])
498 /* Assembler main program */
500 /* Program long options */
501 static const LongOpt OptTab[] = {
502 { "--argument-column", 1, OptArgumentColumn },
503 { "--bytes-per-line", 1, OptBytesPerLine },
504 { "--comment-column", 1, OptCommentColumn },
505 { "--comments", 1, OptComments },
506 { "--cpu", 1, OptCPU },
507 { "--debug-info", 0, OptDebugInfo },
508 { "--formfeeds", 0, OptFormFeeds },
509 { "--help", 0, OptHelp },
510 { "--hexoffs", 0, OptHexOffs },
511 { "--info", 1, OptInfo },
512 { "--label-break", 1, OptLabelBreak },
513 { "--mnemonic-column", 1, OptMnemonicColumn },
514 { "--pagelength", 1, OptPageLength },
515 { "--start-addr", 1, OptStartAddr },
516 { "--text-column", 1, OptTextColumn },
517 { "--verbose", 0, OptVerbose },
518 { "--version", 0, OptVersion },
524 /* Initialize the cmdline module */
525 InitCmdLine (&argc, &argv, "da65");
527 /* Check the parameters */
529 while (I < ArgCount) {
531 /* Get the argument */
532 const char* Arg = ArgVec[I];
534 /* Check for an option */
535 if (Arg [0] == '-') {
539 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
543 OptDebugInfo (Arg, 0);
551 OptInfo (Arg, GetArg (&I, 2));
555 OutFile = GetArg (&I, 2);
563 OptStartAddr (Arg, GetArg (&I, 2));
576 /* Filename. Check if we already had one */
578 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
590 /* Try to read the info file */
593 /* Must have an input file */
595 AbEnd ("No input file");
598 /* Check the formatting options for reasonable values. Note: We will not
599 * really check that they make sense, just that they aren't complete
603 AbEnd ("mnemonic-column value must be smaller than argument-column value");
606 AbEnd ("argument-column value must be smaller than comment-column value");
609 AbEnd ("comment-column value must be smaller than text-column value");
612 /* If no CPU given, use the default CPU */
613 if (CPU == CPU_UNKNOWN) {
617 /* Get the current time and convert it to string so it can be used in
618 * the output page headers.
621 strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
623 /* Load the input file */
626 /* Open the output file */
627 OpenOutput (OutFile);
629 /* Disassemble the code */
632 /* Close the output file */