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 /*****************************************************************************/
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 " --comments n\t\tSet the comment level for the output\n"
88 " --cpu type\t\tSet cpu type\n"
89 " --debug-info\t\tAdd debug info to object file\n"
90 " --formfeeds\t\tAdd formfeeds to the output\n"
91 " --help\t\tHelp (this text)\n"
92 " --hexoffs\t\tUse hexadecimal label offsets\n"
93 " --info name\t\tSpecify an info file\n"
94 " --label-break n\tAdd newline if label exceeds length n\n"
95 " --pagelength n\tSet the page length for the listing\n"
96 " --start-addr addr\tSet the start/load address\n"
97 " --verbose\t\tIncrease verbosity\n"
98 " --version\t\tPrint the disassembler version\n",
104 static unsigned long CvtNumber (const char* Arg, const char* Number)
105 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
114 if (*Number == '$') {
116 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
118 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
121 /* Check if we do really have a number */
122 if (Converted != 1) {
123 Error ("Invalid number given in argument: %s\n", Arg);
126 /* Return the result */
132 static void OptComments (const char* Opt, const char* Arg)
133 /* Handle the --comments option */
135 /* Convert the argument to a number */
136 unsigned long Val = CvtNumber (Opt, Arg);
138 /* Check for a valid range */
139 if (Val > MAX_COMMENTS) {
140 Error ("Argument for %s outside valid range (%d-%d)",
141 Opt, MIN_COMMENTS, MAX_COMMENTS);
145 Comments = (unsigned char) Val;
150 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
151 /* Handle the --cpu option */
153 /* Find the CPU from the given name */
160 static void OptDebugInfo (const char* Opt attribute ((unused)),
161 const char* Arg attribute ((unused)))
162 /* Add debug info to the object file */
169 static void OptFormFeeds (const char* Opt attribute ((unused)),
170 const char* Arg attribute ((unused)))
171 /* Add form feeds to the output */
178 static void OptHelp (const char* Opt attribute ((unused)),
179 const char* Arg attribute ((unused)))
180 /* Print usage information and exit */
188 static void OptHexOffs (const char* Opt attribute ((unused)),
189 const char* Arg attribute ((unused)))
190 /* Handle the --hexoffs option */
197 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
198 /* Handle the --info option */
205 static void OptLabelBreak (const char* Opt, const char* Arg)
206 /* Handle the --label-break option */
208 /* Convert the argument to a number */
209 unsigned long Val = CvtNumber (Opt, Arg);
211 /* Check for a valid range */
212 if (Val >= UCHAR_MAX) {
213 Error ("Argument for %s out of valid range (%d)",
218 LBreak = (unsigned char) Val;
223 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
224 /* Handle the --pagelength option */
226 int Len = atoi (Arg);
227 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
228 AbEnd ("Invalid page length: %d", Len);
235 static void OptStartAddr (const char* Opt, const char* Arg)
236 /* Set the default start address */
238 StartAddr = CvtNumber (Opt, Arg);
243 static void OptVerbose (const char* Opt attribute ((unused)),
244 const char* Arg attribute ((unused)))
245 /* Increase verbosity */
252 static void OptVersion (const char* Opt attribute ((unused)),
253 const char* Arg attribute ((unused)))
254 /* Print the disassembler version */
257 "da65 V%u.%u.%u - (C) Copyright 2000-2006, Ullrich von Bassewitz\n",
258 VER_MAJOR, VER_MINOR, VER_PATCH);
263 static void OneOpcode (unsigned RemainingBytes)
264 /* Disassemble one opcode */
266 /* Get the opcode from the current address */
267 unsigned char OPC = GetCodeByte (PC);
269 /* Get the opcode description for the opcode byte */
270 const OpcDesc* D = &OpcTable[OPC];
272 /* Get the output style for the current PC */
273 attr_t Style = GetStyleAttr (PC);
275 /* If we have a label at this address, output the label and an attached
276 * comment, provided that we aren't in a skip area.
278 if (Style != atSkip && MustDefLabel (PC)) {
279 const char* Comment = GetComment (PC);
281 UserComment (Comment);
283 DefLabel (GetLabelName (PC));
287 * - ...if we have enough bytes remaining for the code at this address.
288 * - ...if the current instruction is valid for the given CPU.
289 * - ...if there is no label somewhere between the instruction bytes.
290 * If any of these conditions is false, switch to data mode.
292 if (Style == atDefault) {
293 if (D->Size > RemainingBytes) {
295 MarkAddr (PC, Style);
296 } else if (D->Flags & flIllegal) {
298 MarkAddr (PC, Style);
301 for (I = 1; I < D->Size; ++I) {
302 if (HaveLabel (PC+I)) {
304 MarkAddr (PC, Style);
311 /* Disassemble the line */
320 /* Beware: If we don't have enough bytes left to disassemble the
321 * following insn, fall through to byte mode.
323 if (D->Size <= RemainingBytes) {
324 /* Output labels within the next insn */
326 for (I = 1; I < D->Size; ++I) {
377 static void OnePass (void)
378 /* Make one pass through the code */
382 /* Disassemble until nothing left */
383 while ((Count = GetRemainingBytes()) > 0) {
390 static void Disassemble (void)
391 /* Disassemble the code */
397 Output ("---------------------------");
404 DefOutOfRangeLabels ();
410 int main (int argc, char* argv [])
411 /* Assembler main program */
413 /* Program long options */
414 static const LongOpt OptTab[] = {
415 { "--comments", 1, OptComments },
416 { "--cpu", 1, OptCPU },
417 { "--debug-info", 0, OptDebugInfo },
418 { "--formfeeds", 0, OptFormFeeds },
419 { "--help", 0, OptHelp },
420 { "--hexoffs", 0, OptHexOffs },
421 { "--info", 1, OptInfo },
422 { "--label-break", 1, OptLabelBreak },
423 { "--pagelength", 1, OptPageLength },
424 { "--start-addr", 1, OptStartAddr },
425 { "--verbose", 0, OptVerbose },
426 { "--version", 0, OptVersion },
432 /* Initialize the cmdline module */
433 InitCmdLine (&argc, &argv, "da65");
435 /* Check the parameters */
437 while (I < ArgCount) {
439 /* Get the argument */
440 const char* Arg = ArgVec[I];
442 /* Check for an option */
443 if (Arg [0] == '-') {
447 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
451 OptDebugInfo (Arg, 0);
459 OptInfo (Arg, GetArg (&I, 2));
463 OutFile = GetArg (&I, 2);
471 OptStartAddr (Arg, GetArg (&I, 2));
484 /* Filename. Check if we already had one */
486 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
498 /* Try to read the info file */
501 /* Must have an input file */
503 AbEnd ("No input file");
506 /* If no CPU given, use the default CPU */
507 if (CPU == CPU_UNKNOWN) {
511 /* Get the current time and convert it to string so it can be used in
512 * the output page headers.
515 strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
517 /* Load the input file */
520 /* Open the output file */
521 OpenOutput (OutFile);
523 /* Disassemble the code */
526 /* Close the output file */