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] file\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 " --cpu type\t\tSet cpu type\n"
86 " --debug-info\t\tAdd debug info to object file\n"
87 " --formfeeds\t\tAdd formfeeds to the output\n"
88 " --help\t\tHelp (this text)\n"
89 " --info name\t\tSpecify an info file\n"
90 " --pagelength n\tSet the page length for the listing\n"
91 " --start-addr addr\tSet the start/load address\n"
92 " --verbose\t\tIncrease verbosity\n"
93 " --version\t\tPrint the disassembler version\n",
99 static unsigned long CvtNumber (const char* Arg, const char* Number)
100 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
108 if (*Number == '$') {
110 Converted = sscanf (Number, "%lx", &Val);
112 Converted = sscanf (Number, "%li", (long*)&Val);
115 /* Check if we do really have a number */
116 if (Converted != 1) {
117 Error ("Invalid number given in argument: %s\n", Arg);
120 /* Return the result */
126 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
127 /* Handle the --cpu option */
129 /* Find the CPU from the given name */
136 static void OptDebugInfo (const char* Opt attribute ((unused)),
137 const char* Arg attribute ((unused)))
138 /* Add debug info to the object file */
145 static void OptFormFeeds (const char* Opt attribute ((unused)),
146 const char* Arg attribute ((unused)))
147 /* Add form feeds to the output */
154 static void OptHelp (const char* Opt attribute ((unused)),
155 const char* Arg attribute ((unused)))
156 /* Print usage information and exit */
164 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
165 /* Handle the --info option */
172 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
173 /* Handle the --pagelength option */
175 int Len = atoi (Arg);
176 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
177 AbEnd ("Invalid page length: %d", Len);
184 static void OptStartAddr (const char* Opt, const char* Arg)
185 /* Set the default start address */
187 StartAddr = CvtNumber (Opt, Arg);
192 static void OptVerbose (const char* Opt attribute ((unused)),
193 const char* Arg attribute ((unused)))
194 /* Increase verbosity */
201 static void OptVersion (const char* Opt attribute ((unused)),
202 const char* Arg attribute ((unused)))
203 /* Print the disassembler version */
206 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
207 VER_MAJOR, VER_MINOR, VER_PATCH);
212 static void OneOpcode (unsigned RemainingBytes)
213 /* Disassemble one opcode */
215 /* Get the opcode from the current address */
216 unsigned char OPC = GetCodeByte (PC);
218 /* Get the opcode description for the opcode byte */
219 const OpcDesc* D = &OpcTable[OPC];
221 /* If we have a label at this address, output the label */
222 if (MustDefLabel (PC)) {
223 DefLabel (GetLabel (PC));
227 * - ...if we have enough bytes remaining for the code at this address.
228 * - ...if the current instruction is valid for the given CPU.
229 * - ...if there is no label somewhere between the instruction bytes.
230 * If any of these conditions is true, switch to data mode.
232 if (GetStyleAttr (PC) == atDefault) {
233 if (D->Size > RemainingBytes) {
234 MarkAddr (PC, atIllegal);
235 } else if (D->Flags & flIllegal) {
236 MarkAddr (PC, atIllegal);
239 for (I = 1; I < D->Size; ++I) {
240 if (HaveLabel (PC+I)) {
241 MarkAddr (PC, atIllegal);
248 /* Disassemble the line */
249 switch (GetStyleAttr (PC)) {
291 static void OnePass (void)
292 /* Make one pass through the code */
296 /* Disassemble until nothing left */
297 while ((Count = GetRemainingBytes()) > 0) {
304 static void Disassemble (void)
305 /* Disassemble the code */
311 Output ("---------------------------");
318 DefOutOfRangeLabels ();
324 int main (int argc, char* argv [])
325 /* Assembler main program */
327 /* Program long options */
328 static const LongOpt OptTab[] = {
329 { "--cpu", 1, OptCPU },
330 { "--debug-info", 0, OptDebugInfo },
331 { "--formfeeds", 0, OptFormFeeds },
332 { "--help", 0, OptHelp },
333 { "--info", 1, OptInfo },
334 { "--pagelength", 1, OptPageLength },
335 { "--start-addr", 1, OptStartAddr },
336 { "--verbose", 0, OptVerbose },
337 { "--version", 0, OptVersion },
342 /* Initialize the cmdline module */
343 InitCmdLine (&argc, &argv, "da65");
345 /* Check the parameters */
347 while (I < ArgCount) {
349 /* Get the argument */
350 const char* Arg = ArgVec[I];
352 /* Check for an option */
353 if (Arg [0] == '-') {
357 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
361 OptDebugInfo (Arg, 0);
369 OptInfo (Arg, GetArg (&I, 2));
373 OutFile = GetArg (&I, 2);
381 OptStartAddr (Arg, GetArg (&I, 2));
394 /* Filename. Check if we already had one */
396 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
408 /* Must have an input file */
410 AbEnd ("No input file");
413 /* Try to read the configuration file */
416 /* Make the output file name from the input file name if none was given */
418 OutFile = MakeFilename (InFile, OutExt);
421 /* If no CPU given, use the default CPU */
422 if (CPU == CPU_UNKNOWN) {
426 /* Load the input file */
427 LoadCode (InFile, StartAddr);
429 /* Open the output file */
430 OpenOutput (OutFile);
432 /* Disassemble the code */
435 /* Close the output file */