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 " -o name\t\tName the output file\n"
78 " -v\t\t\tIncrease verbosity\n"
79 " -F\t\t\tAdd formfeeds to the output\n"
80 " -S addr\t\tSet the start/load address\n"
81 " -V\t\t\tPrint the disassembler version\n"
84 " --cpu type\t\tSet cpu type\n"
85 " --debug-info\t\tAdd debug info to object file\n"
86 " --formfeeds\t\tAdd formfeeds to the output\n"
87 " --help\t\tHelp (this text)\n"
88 " --pagelength n\tSet the page length for the listing\n"
89 " --start-addr addr\tSet the start/load address\n"
90 " --verbose\t\tIncrease verbosity\n"
91 " --version\t\tPrint the disassembler version\n",
97 static unsigned long CvtNumber (const char* Arg, const char* Number)
98 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
106 if (*Number == '$') {
108 Converted = sscanf (Number, "%lx", &Val);
110 Converted = sscanf (Number, "%li", (long*)&Val);
113 /* Check if we do really have a number */
114 if (Converted != 1) {
115 Error ("Invalid number given in argument: %s\n", Arg);
118 /* Return the result */
124 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
125 /* Handle the --cpu option */
127 /* Find the CPU from the given name */
134 static void OptDebugInfo (const char* Opt attribute ((unused)),
135 const char* Arg attribute ((unused)))
136 /* Add debug info to the object file */
143 static void OptFormFeeds (const char* Opt attribute ((unused)),
144 const char* Arg attribute ((unused)))
145 /* Add form feeds to the output */
152 static void OptHelp (const char* Opt attribute ((unused)),
153 const char* Arg attribute ((unused)))
154 /* Print usage information and exit */
162 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
163 /* Handle the --pagelength option */
165 int Len = atoi (Arg);
166 if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
167 AbEnd ("Invalid page length: %d", Len);
174 static void OptStartAddr (const char* Opt, const char* Arg)
175 /* Set the default start address */
177 StartAddr = CvtNumber (Opt, Arg);
182 static void OptVerbose (const char* Opt attribute ((unused)),
183 const char* Arg attribute ((unused)))
184 /* Increase verbosity */
191 static void OptVersion (const char* Opt attribute ((unused)),
192 const char* Arg attribute ((unused)))
193 /* Print the disassembler version */
196 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
197 VER_MAJOR, VER_MINOR, VER_PATCH);
202 static void OneOpcode (unsigned RemainingBytes)
203 /* Disassemble one opcode */
205 /* Get the opcode from the current address */
206 unsigned char OPC = GetCodeByte (PC);
208 /* Get the opcode description for the opcode byte */
209 const OpcDesc* D = &OpcTable[OPC];
211 /* If we have a label at this address, output the label */
212 if (MustDefLabel (PC)) {
213 DefLabel (GetLabel (PC));
217 * - ...if we have enough bytes remaining for the code at this address.
218 * - ...if the current instruction is valid for the given CPU.
219 * - ...if there is no label somewhere between the instruction bytes.
220 * If any of these conditions is true, switch to data mode.
222 if (GetStyleAttr (PC) == atDefault) {
223 if (D->Size > RemainingBytes) {
224 MarkAddr (PC, atIllegal);
225 } else if (D->Flags & flIllegal) {
226 MarkAddr (PC, atIllegal);
229 for (I = 1; I < D->Size; ++I) {
230 if (HaveLabel (PC+I)) {
231 MarkAddr (PC, atIllegal);
238 /* Disassemble the line */
239 switch (GetStyleAttr (PC)) {
281 static void OnePass (void)
282 /* Make one pass through the code */
286 /* Disassemble until nothing left */
287 while ((Count = GetRemainingBytes()) > 0) {
294 static void Disassemble (void)
295 /* Disassemble the code */
301 Output ("---------------------------");
308 DefOutOfRangeLabels ();
314 int main (int argc, char* argv [])
315 /* Assembler main program */
317 /* Program long options */
318 static const LongOpt OptTab[] = {
319 { "--cpu", 1, OptCPU },
320 { "--debug-info", 0, OptDebugInfo },
321 { "--formfeeds", 0, OptFormFeeds },
322 { "--help", 0, OptHelp },
323 { "--pagelength", 1, OptPageLength },
324 { "--start-addr", 1, OptStartAddr },
325 { "--verbose", 0, OptVerbose },
326 { "--version", 0, OptVersion },
331 /* Initialize the cmdline module */
332 InitCmdLine (&argc, &argv, "da65");
334 /* Check the parameters */
336 while (I < ArgCount) {
338 /* Get the argument */
339 const char* Arg = ArgVec[I];
341 /* Check for an option */
342 if (Arg [0] == '-') {
346 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
350 OptDebugInfo (Arg, 0);
358 OutFile = GetArg (&I, 2);
366 OptStartAddr (Arg, GetArg (&I, 2));
379 /* Filename. Check if we already had one */
381 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
393 /* Must have an input file */
395 AbEnd ("No input file");
398 /* Make the config file name from the input file if none was given */
400 CfgSetName (MakeFilename (InFile, CfgExt));
403 /* Try to read the configuration file */
406 /* Make the output file name from the input file name if none was given */
408 OutFile = MakeFilename (InFile, OutExt);
411 /* If no CPU given, use the default CPU */
412 if (CPU == CPU_UNKNOWN) {
416 /* Load the input file */
417 LoadCode (InFile, StartAddr);
419 /* Open the output file */
420 OpenOutput (OutFile);
422 /* Disassemble the code */
425 /* Close the output file */