1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2014, 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 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void Usage (void)
74 /* Print usage information and exit */
76 printf ("Usage: %s [options] [inputfile]\n"
78 " -g\t\t\tAdd debug info to object file\n"
79 " -h\t\t\tHelp (this text)\n"
80 " -i name\t\tSpecify an info file\n"
81 " -o name\t\tName the output file\n"
82 " -v\t\t\tIncrease verbosity\n"
83 " -F\t\t\tAdd formfeeds to the output\n"
84 " -S addr\t\tSet the start/load address\n"
85 " -s\t\t\tAccept line markers in the info file\n"
86 " -V\t\t\tPrint the disassembler version\n"
89 " --argument-column n\tSpecify argument start column\n"
90 " --comment-column n\tSpecify comment start column\n"
91 " --comments n\t\tSet the comment level for the output\n"
92 " --cpu type\t\tSet cpu type\n"
93 " --debug-info\t\tAdd debug info to object file\n"
94 " --formfeeds\t\tAdd formfeeds to the output\n"
95 " --help\t\tHelp (this text)\n"
96 " --hexoffs\t\tUse hexadecimal label offsets\n"
97 " --info name\t\tSpecify an info file\n"
98 " --label-break n\tAdd newline if label exceeds length n\n"
99 " --mnemonic-column n\tSpecify mnemonic start column\n"
100 " --pagelength n\tSet the page length for the listing\n"
101 " --start-addr addr\tSet the start/load address\n"
102 " --sync-lines\t\tAccept line markers in the info file\n"
103 " --text-column n\tSpecify text start column\n"
104 " --verbose\t\tIncrease verbosity\n"
105 " --version\t\tPrint the disassembler version\n",
111 static void RangeCheck (const char* Opt, unsigned long Val,
112 unsigned long Min, unsigned long Max)
113 /* Do a range check for the given option and abort if there's a range
117 if (Val < Min || Val > Max) {
118 Error ("Argument for %s outside valid range (%ld-%ld)", Opt, Min, Max);
124 static unsigned long CvtNumber (const char* Arg, const char* Number)
125 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
134 if (*Number == '$') {
136 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
138 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
141 /* Check if we do really have a number */
142 if (Converted != 1) {
143 Error ("Invalid number given in argument: %s\n", Arg);
146 /* Return the result */
152 static void OptArgumentColumn (const char* Opt, const char* Arg)
153 /* Handle the --argument-column option */
155 /* Convert the argument to a number */
156 unsigned long Val = CvtNumber (Opt, Arg);
158 /* Check for a valid range */
159 RangeCheck (Opt, Val, MIN_ACOL, MAX_ACOL);
162 ACol = (unsigned char) Val;
167 static void OptBytesPerLine (const char* Opt, const char* Arg)
168 /* Handle the --bytes-per-line option */
170 /* Convert the argument to a number */
171 unsigned long Val = CvtNumber (Opt, Arg);
173 /* Check for a valid range */
174 RangeCheck (Opt, Val, MIN_BYTESPERLINE, MAX_BYTESPERLINE);
177 BytesPerLine = (unsigned char) Val;
182 static void OptCommentColumn (const char* Opt, const char* Arg)
183 /* Handle the --comment-column option */
185 /* Convert the argument to a number */
186 unsigned long Val = CvtNumber (Opt, Arg);
188 /* Check for a valid range */
189 RangeCheck (Opt, Val, MIN_CCOL, MAX_CCOL);
192 CCol = (unsigned char) Val;
197 static void OptComments (const char* Opt, const char* Arg)
198 /* Handle the --comments option */
200 /* Convert the argument to a number */
201 unsigned long Val = CvtNumber (Opt, Arg);
203 /* Check for a valid range */
204 RangeCheck (Opt, Val, MIN_COMMENTS, MAX_COMMENTS);
207 Comments = (unsigned char) Val;
212 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
213 /* Handle the --cpu option */
215 /* Find the CPU from the given name */
222 static void OptDebugInfo (const char* Opt attribute ((unused)),
223 const char* Arg attribute ((unused)))
224 /* Add debug info to the object file */
231 static void OptFormFeeds (const char* Opt attribute ((unused)),
232 const char* Arg attribute ((unused)))
233 /* Add form feeds to the output */
240 static void OptHelp (const char* Opt attribute ((unused)),
241 const char* Arg attribute ((unused)))
242 /* Print usage information and exit */
250 static void OptHexOffs (const char* Opt attribute ((unused)),
251 const char* Arg attribute ((unused)))
252 /* Handle the --hexoffs option */
259 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
260 /* Handle the --info option */
267 static void OptLabelBreak (const char* Opt, const char* Arg)
268 /* Handle the --label-break option */
270 /* Convert the argument to a number */
271 unsigned long Val = CvtNumber (Opt, Arg);
273 /* Check for a valid range */
274 RangeCheck (Opt, Val, MIN_LABELBREAK, MAX_LABELBREAK);
277 LBreak = (unsigned char) Val;
282 static void OptMnemonicColumn (const char* Opt, const char* Arg)
283 /* Handle the --mnemonic-column option */
285 /* Convert the argument to a number */
286 unsigned long Val = CvtNumber (Opt, Arg);
288 /* Check for a valid range */
289 RangeCheck (Opt, Val, MIN_MCOL, MAX_MCOL);
292 MCol = (unsigned char) Val;
297 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
298 /* Handle the --pagelength option */
300 int Len = atoi (Arg);
302 RangeCheck (Opt, Len, MIN_PAGE_LEN, MAX_PAGE_LEN);
309 static void OptStartAddr (const char* Opt, const char* Arg)
310 /* Set the default start address */
312 StartAddr = CvtNumber (Opt, Arg);
317 static void OptSyncLines (const char* Opt attribute ((unused)),
318 const char* Arg attribute ((unused)))
319 /* Handle the --sync-lines option */
326 static void OptTextColumn (const char* Opt, const char* Arg)
327 /* Handle the --text-column option */
329 /* Convert the argument to a number */
330 unsigned long Val = CvtNumber (Opt, Arg);
332 /* Check for a valid range */
333 RangeCheck (Opt, Val, MIN_TCOL, MAX_TCOL);
336 TCol = (unsigned char) Val;
341 static void OptVerbose (const char* Opt attribute ((unused)),
342 const char* Arg attribute ((unused)))
343 /* Increase verbosity */
350 static void OptVersion (const char* Opt attribute ((unused)),
351 const char* Arg attribute ((unused)))
352 /* Print the disassembler version */
354 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
360 static void OneOpcode (unsigned RemainingBytes)
361 /* Disassemble one opcode */
366 /* Get the opcode from the current address */
367 unsigned char OPC = GetCodeByte (PC);
369 /* Get the opcode description for the opcode byte */
370 const OpcDesc* D = &OpcTable[OPC];
372 /* Get the output style for the current PC */
373 attr_t Style = GetStyleAttr (PC);
375 /* If a segment begins here, then name that segment.
376 ** Note that the segment is named even if its code is being skipped,
377 ** because some of its later code might not be skipped.
379 if (IsSegmentStart (PC)) {
380 StartSegment (GetSegmentStartName (PC), GetSegmentAddrSize (PC));
383 /* If we have a label at this address, output the label and an attached
384 ** comment, provided that we aren't in a skip area.
386 if (Style != atSkip && MustDefLabel (PC)) {
387 const char* Comment = GetComment (PC);
389 UserComment (Comment);
391 DefLabel (GetLabelName (PC));
395 ** - ...if we have enough bytes remaining for the code at this address.
396 ** - ...if the current instruction is valid for the given CPU.
397 ** - ...if there is no label somewhere between the instruction bytes.
398 ** - ...if there is no segment change between the instruction bytes.
399 ** If any one of those conditions is false, switch to data mode.
401 if (Style == atDefault) {
402 if (D->Size > RemainingBytes) {
404 MarkAddr (PC, Style);
405 } else if (D->Flags & flIllegal) {
407 MarkAddr (PC, Style);
409 for (I = PC + D->Size; --I > PC; ) {
410 if (HaveLabel (I) || IsSegmentStart (I)) {
412 MarkAddr (PC, Style);
416 for (I = 0; I < D->Size - 1u; ++I) {
417 if (IsSegmentEnd (PC + I)) {
419 MarkAddr (PC, Style);
426 /* Disassemble the line */
435 /* Beware: If we don't have enough bytes left to disassemble the
436 ** following insn, fall through to byte mode.
438 if (D->Size <= RemainingBytes) {
439 /* Output labels within the next insn */
440 for (I = 1; I < D->Size; ++I) {
443 /* Output the insn */
488 /* Change back to the default CODE segment if
489 ** a named segment stops at the current address.
491 for (I = PC - OldPC; I > 0; --I) {
492 if (IsSegmentEnd (PC - I)) {
501 static void OnePass (void)
502 /* Make one pass through the code */
506 /* Disassemble until nothing left */
507 while ((Count = GetRemainingBytes()) > 0) {
514 static void Disassemble (void)
515 /* Disassemble the code */
521 Output ("---------------------------");
528 DefOutOfRangeLabels ();
534 int main (int argc, char* argv [])
535 /* Assembler main program */
537 /* Program long options */
538 static const LongOpt OptTab[] = {
539 { "--argument-column", 1, OptArgumentColumn },
540 { "--bytes-per-line", 1, OptBytesPerLine },
541 { "--comment-column", 1, OptCommentColumn },
542 { "--comments", 1, OptComments },
543 { "--cpu", 1, OptCPU },
544 { "--debug-info", 0, OptDebugInfo },
545 { "--formfeeds", 0, OptFormFeeds },
546 { "--help", 0, OptHelp },
547 { "--hexoffs", 0, OptHexOffs },
548 { "--info", 1, OptInfo },
549 { "--label-break", 1, OptLabelBreak },
550 { "--mnemonic-column", 1, OptMnemonicColumn },
551 { "--pagelength", 1, OptPageLength },
552 { "--start-addr", 1, OptStartAddr },
553 { "--sync-lines", 0, OptSyncLines },
554 { "--text-column", 1, OptTextColumn },
555 { "--verbose", 0, OptVerbose },
556 { "--version", 0, OptVersion },
562 /* Initialize the cmdline module */
563 InitCmdLine (&argc, &argv, "da65");
565 /* Check the parameters */
567 while (I < ArgCount) {
569 /* Get the argument */
570 const char* Arg = ArgVec[I];
572 /* Check for an option */
573 if (Arg [0] == '-') {
577 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
581 OptDebugInfo (Arg, 0);
589 OptInfo (Arg, GetArg (&I, 2));
593 OutFile = GetArg (&I, 2);
601 OptStartAddr (Arg, GetArg (&I, 2));
605 OptSyncLines (Arg, 0);
618 /* Filename. Check if we already had one */
620 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
632 /* Try to read the info file */
635 /* Must have an input file */
637 AbEnd ("No input file");
640 /* Check the formatting options for reasonable values. Note: We will not
641 ** really check that they make sense, just that they aren't complete
645 AbEnd ("mnemonic-column value must be smaller than argument-column value");
648 AbEnd ("argument-column value must be smaller than comment-column value");
651 AbEnd ("comment-column value must be smaller than text-column value");
654 /* If no CPU given, use the default CPU */
655 if (CPU == CPU_UNKNOWN) {
659 /* Get the current time and convert it to string so it can be used in
660 ** the output page headers.
663 strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
665 /* Load the input file */
668 /* Open the output file */
669 OpenOutput (OutFile);
671 /* Disassemble the code */
674 /* Close the output file */