1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2006 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 /*****************************************************************************/
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void Usage (void)
71 /* Print usage information and exit */
73 printf ("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 " --hexoffs\t\tUse hexadecimal label offsets\n"
91 " --info name\t\tSpecify an info file\n"
92 " --label-break n\tAdd newline if label exceeds length n\n"
93 " --pagelength n\tSet the page length for the listing\n"
94 " --start-addr addr\tSet the start/load address\n"
95 " --verbose\t\tIncrease verbosity\n"
96 " --version\t\tPrint the disassembler version\n",
102 static unsigned long CvtNumber (const char* Arg, const char* Number)
103 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
112 if (*Number == '$') {
114 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
116 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
119 /* Check if we do really have a number */
120 if (Converted != 1) {
121 Error ("Invalid number given in argument: %s\n", Arg);
124 /* Return the result */
130 static void OptComments (const char* Opt, const char* Arg)
131 /* Handle the --comments option */
133 /* Convert the argument to a number */
134 unsigned long Val = CvtNumber (Opt, Arg);
136 /* Check for a valid range */
137 if (Val > MAX_COMMENTS) {
138 Error ("Argument for %s outside valid range (%d-%d)",
139 Opt, MIN_COMMENTS, MAX_COMMENTS);
143 Comments = (unsigned char) Val;
148 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
149 /* Handle the --cpu option */
151 /* Find the CPU from the given name */
158 static void OptDebugInfo (const char* Opt attribute ((unused)),
159 const char* Arg attribute ((unused)))
160 /* Add debug info to the object file */
167 static void OptFormFeeds (const char* Opt attribute ((unused)),
168 const char* Arg attribute ((unused)))
169 /* Add form feeds to the output */
176 static void OptHelp (const char* Opt attribute ((unused)),
177 const char* Arg attribute ((unused)))
178 /* Print usage information and exit */
186 static void OptHexOffs (const char* Opt attribute ((unused)),
187 const char* Arg attribute ((unused)))
188 /* Handle the --hexoffs option */
195 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
196 /* Handle the --info option */
203 static void OptLabelBreak (const char* Opt, const char* Arg)
204 /* Handle the --label-break option */
206 /* Convert the argument to a number */
207 unsigned long Val = CvtNumber (Opt, Arg);
209 /* Check for a valid range */
210 if (Val >= UCHAR_MAX) {
211 Error ("Argument for %s out of valid range (%d)",
216 LBreak = (unsigned char) Val;
221 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
222 /* Handle the --pagelength option */
224 int Len = atoi (Arg);
225 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
226 AbEnd ("Invalid page length: %d", Len);
233 static void OptStartAddr (const char* Opt, const char* Arg)
234 /* Set the default start address */
236 StartAddr = CvtNumber (Opt, Arg);
241 static void OptVerbose (const char* Opt attribute ((unused)),
242 const char* Arg attribute ((unused)))
243 /* Increase verbosity */
250 static void OptVersion (const char* Opt attribute ((unused)),
251 const char* Arg attribute ((unused)))
252 /* Print the disassembler version */
255 "da65 V%u.%u.%u - (C) Copyright 2005 Ullrich von Bassewitz\n",
256 VER_MAJOR, VER_MINOR, VER_PATCH);
261 static void OneOpcode (unsigned RemainingBytes)
262 /* Disassemble one opcode */
264 /* Get the opcode from the current address */
265 unsigned char OPC = GetCodeByte (PC);
267 /* Get the opcode description for the opcode byte */
268 const OpcDesc* D = &OpcTable[OPC];
270 /* Get the output style for the current PC */
271 attr_t Style = GetStyleAttr (PC);
273 /* If we have a label at this address, output the label and an attached
274 * comment, provided that we aren't in a skip area.
276 if (Style != atSkip && MustDefLabel (PC)) {
277 const char* Comment = GetComment (PC);
279 UserComment (Comment);
281 DefLabel (GetLabel (PC));
285 * - ...if we have enough bytes remaining for the code at this address.
286 * - ...if the current instruction is valid for the given CPU.
287 * - ...if there is no label somewhere between the instruction bytes.
288 * If any of these conditions is false, switch to data mode.
290 if (Style == atDefault) {
291 if (D->Size > RemainingBytes) {
293 MarkAddr (PC, Style);
294 } else if (D->Flags & flIllegal) {
296 MarkAddr (PC, Style);
299 for (I = 1; I < D->Size; ++I) {
300 if (HaveLabel (PC+I)) {
302 MarkAddr (PC, Style);
309 /* Disassemble the line */
318 /* Beware: If we don't have enough bytes left to disassemble the
319 * following insn, fall through to byte mode.
321 if (D->Size <= RemainingBytes) {
370 static void OnePass (void)
371 /* Make one pass through the code */
375 /* Disassemble until nothing left */
376 while ((Count = GetRemainingBytes()) > 0) {
383 static void Disassemble (void)
384 /* Disassemble the code */
390 Output ("---------------------------");
397 DefOutOfRangeLabels ();
403 int main (int argc, char* argv [])
404 /* Assembler main program */
406 /* Program long options */
407 static const LongOpt OptTab[] = {
408 { "--comments", 1, OptComments },
409 { "--cpu", 1, OptCPU },
410 { "--debug-info", 0, OptDebugInfo },
411 { "--formfeeds", 0, OptFormFeeds },
412 { "--help", 0, OptHelp },
413 { "--hexoffs", 0, OptHexOffs },
414 { "--info", 1, OptInfo },
415 { "--label-break", 1, OptLabelBreak },
416 { "--pagelength", 1, OptPageLength },
417 { "--start-addr", 1, OptStartAddr },
418 { "--verbose", 0, OptVerbose },
419 { "--version", 0, OptVersion },
425 /* Initialize the cmdline module */
426 InitCmdLine (&argc, &argv, "da65");
428 /* Check the parameters */
430 while (I < ArgCount) {
432 /* Get the argument */
433 const char* Arg = ArgVec[I];
435 /* Check for an option */
436 if (Arg [0] == '-') {
440 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
444 OptDebugInfo (Arg, 0);
452 OptInfo (Arg, GetArg (&I, 2));
456 OutFile = GetArg (&I, 2);
464 OptStartAddr (Arg, GetArg (&I, 2));
477 /* Filename. Check if we already had one */
479 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
491 /* Try to read the info file */
494 /* Must have an input file */
496 AbEnd ("No input file");
499 /* If no CPU given, use the default CPU */
500 if (CPU == CPU_UNKNOWN) {
504 /* Get the current time and convert it to string so it can be used in
505 * the output page headers.
508 strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
510 /* Load the input file */
513 /* Open the output file */
514 OpenOutput (OutFile);
516 /* Disassemble the code */
519 /* Close the output file */