1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2003 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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Usage (void)
70 /* Print usage information and exit */
73 "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 " --pagelength n\tSet the page length for the listing\n"
93 " --start-addr addr\tSet the start/load address\n"
94 " --verbose\t\tIncrease verbosity\n"
95 " --version\t\tPrint the disassembler version\n",
101 static unsigned long CvtNumber (const char* Arg, const char* Number)
102 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
111 if (*Number == '$') {
113 Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
115 Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
118 /* Check if we do really have a number */
119 if (Converted != 1) {
120 Error ("Invalid number given in argument: %s\n", Arg);
123 /* Return the result */
129 static void OptComments (const char* Opt, const char* Arg)
130 /* Handle the --comments option */
132 /* Convert the argument to a number */
133 unsigned long Val = CvtNumber (Opt, Arg);
135 /* Check for a valid range */
136 if (Val > MAX_COMMENTS) {
137 Error ("Argument for %s outside valid range (%d-%d)",
138 Opt, MIN_COMMENTS, MAX_COMMENTS);
142 Comments = (unsigned char) Val;
147 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
148 /* Handle the --cpu option */
150 /* Find the CPU from the given name */
157 static void OptDebugInfo (const char* Opt attribute ((unused)),
158 const char* Arg attribute ((unused)))
159 /* Add debug info to the object file */
166 static void OptFormFeeds (const char* Opt attribute ((unused)),
167 const char* Arg attribute ((unused)))
168 /* Add form feeds to the output */
175 static void OptHelp (const char* Opt attribute ((unused)),
176 const char* Arg attribute ((unused)))
177 /* Print usage information and exit */
185 static void OptHexOffs (const char* Opt attribute ((unused)),
186 const char* Arg attribute ((unused)))
187 /* Handle the --hexoffs option */
194 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
195 /* Handle the --info option */
202 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
203 /* Handle the --pagelength option */
205 int Len = atoi (Arg);
206 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
207 AbEnd ("Invalid page length: %d", Len);
214 static void OptStartAddr (const char* Opt, const char* Arg)
215 /* Set the default start address */
217 StartAddr = CvtNumber (Opt, Arg);
222 static void OptVerbose (const char* Opt attribute ((unused)),
223 const char* Arg attribute ((unused)))
224 /* Increase verbosity */
231 static void OptVersion (const char* Opt attribute ((unused)),
232 const char* Arg attribute ((unused)))
233 /* Print the disassembler version */
236 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
237 VER_MAJOR, VER_MINOR, VER_PATCH);
242 static void OneOpcode (unsigned RemainingBytes)
243 /* Disassemble one opcode */
245 /* Get the opcode from the current address */
246 unsigned char OPC = GetCodeByte (PC);
248 /* Get the opcode description for the opcode byte */
249 const OpcDesc* D = &OpcTable[OPC];
251 /* Get the output style for the current PC */
252 attr_t Style = GetStyleAttr (PC);
254 /* If we have a label at this address, output the label, provided that
255 * we aren't in a skip area.
257 if (Style != atSkip && MustDefLabel (PC)) {
258 DefLabel (GetLabel (PC));
262 * - ...if we have enough bytes remaining for the code at this address.
263 * - ...if the current instruction is valid for the given CPU.
264 * - ...if there is no label somewhere between the instruction bytes.
265 * If any of these conditions is false, switch to data mode.
267 if (Style == atDefault) {
268 if (D->Size > RemainingBytes) {
269 MarkAddr (PC, atIllegal);
270 } else if (D->Flags & flIllegal) {
271 MarkAddr (PC, atIllegal);
274 for (I = 1; I < D->Size; ++I) {
275 if (HaveLabel (PC+I)) {
276 MarkAddr (PC, atIllegal);
283 /* Disassemble the line */
334 static void OnePass (void)
335 /* Make one pass through the code */
339 /* Disassemble until nothing left */
340 while ((Count = GetRemainingBytes()) > 0) {
347 static void Disassemble (void)
348 /* Disassemble the code */
354 Output ("---------------------------");
361 DefOutOfRangeLabels ();
367 int main (int argc, char* argv [])
368 /* Assembler main program */
370 /* Program long options */
371 static const LongOpt OptTab[] = {
372 { "--comments", 1, OptComments },
373 { "--cpu", 1, OptCPU },
374 { "--debug-info", 0, OptDebugInfo },
375 { "--formfeeds", 0, OptFormFeeds },
376 { "--help", 0, OptHelp },
377 { "--hexoffs", 0, OptHexOffs },
378 { "--info", 1, OptInfo },
379 { "--pagelength", 1, OptPageLength },
380 { "--start-addr", 1, OptStartAddr },
381 { "--verbose", 0, OptVerbose },
382 { "--version", 0, OptVersion },
387 /* Initialize the cmdline module */
388 InitCmdLine (&argc, &argv, "da65");
390 /* Check the parameters */
392 while (I < ArgCount) {
394 /* Get the argument */
395 const char* Arg = ArgVec[I];
397 /* Check for an option */
398 if (Arg [0] == '-') {
402 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
406 OptDebugInfo (Arg, 0);
414 OptInfo (Arg, GetArg (&I, 2));
418 OutFile = GetArg (&I, 2);
426 OptStartAddr (Arg, GetArg (&I, 2));
439 /* Filename. Check if we already had one */
441 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
453 /* Try to read the info file */
456 /* Must have an input file */
458 AbEnd ("No input file");
461 /* If no CPU given, use the default CPU */
462 if (CPU == CPU_UNKNOWN) {
466 /* Load the input file */
469 /* Open the output file */
470 OpenOutput (OutFile);
472 /* Disassemble the code */
475 /* Close the output file */