1 /*****************************************************************************/
5 /* Main program for the da65 disassembler */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 " --formfeeds\t\tAdd formfeeds to the output\n"
86 " --help\t\tHelp (this text)\n"
87 " --pagelength n\tSet the page length for the listing\n"
88 " --start-addr addr\tSet the start/load address\n"
89 " --verbose\t\tIncrease verbosity\n"
90 " --version\t\tPrint the disassembler version\n",
96 static unsigned long CvtNumber (const char* Arg, const char* Number)
97 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
105 if (*Number == '$') {
107 Converted = sscanf (Number, "%lx", &Val);
109 Converted = sscanf (Number, "%li", (long*)&Val);
112 /* Check if we do really have a number */
113 if (Converted != 1) {
114 Error ("Invalid number given in argument: %s\n", Arg);
117 /* Return the result */
123 static void OptCPU (const char* Opt, const char* Arg)
124 /* Handle the --cpu option */
129 if (strcmp (Arg, "6502") == 0) {
131 } else if (strcmp (Arg, "65C02") == 0) {
133 } else if (strcmp (Arg, "65816") == 0) {
136 } else if (strcmp (Arg, "sunplus") == 0) {
137 SetCPU (CPU_SUNPLUS);
140 AbEnd ("Invalid CPU: `%s'", Arg);
146 static void OptFormFeeds (const char* Opt, const char* Arg)
147 /* Add form feeds to the output */
154 static void OptHelp (const char* Opt, const char* Arg)
155 /* Print usage information and exit */
163 static void OptPageLength (const char* Opt, const char* Arg)
164 /* Handle the --pagelength option */
171 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
172 AbEnd ("Invalid page length: %d", Len);
179 static void OptStartAddr (const char* Opt, const char* Arg)
180 /* Set the default start address */
182 StartAddr = CvtNumber (Opt, Arg);
187 static void OptVerbose (const char* Opt, const char* Arg)
188 /* Increase verbosity */
195 static void OptVersion (const char* Opt, const char* Arg)
196 /* Print the disassembler version */
199 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
200 VER_MAJOR, VER_MINOR, VER_PATCH);
205 static void OneOpcode (unsigned RemainingBytes)
206 /* Disassemble one opcode */
208 /* Get the opcode from the current address */
209 unsigned char OPC = GetCodeByte (PC);
211 /* Get the opcode description for the opcode byte */
212 const OpcDesc* D = &OpcTable[OPC];
214 /* If we have a label at this address, output the label */
215 if (MustDefLabel (PC)) {
216 DefLabel (GetLabel (PC));
220 * - ...if we have enough bytes remaining for the code at this address.
221 * - ...if the current instruction is valid for the given CPU.
222 * - ...if there is no label somewhere between the instruction bytes.
223 * If any of these conditions is true, switch to data mode.
225 if (GetStyleAttr (PC) == atDefault) {
226 if (D->Size > RemainingBytes) {
227 MarkAddr (PC, atIllegal);
228 } else if ((D->CPU & CPU) != CPU) {
229 MarkAddr (PC, atIllegal);
232 for (I = 1; I < D->Size; ++I) {
233 if (HaveLabel (PC+I)) {
234 MarkAddr (PC, atIllegal);
241 /* Disassemble the line */
242 switch (GetStyleAttr (PC)) {
280 static void OnePass (void)
281 /* Make one pass through the code */
285 /* Disassemble until nothing left */
286 while ((Count = GetRemainingBytes()) > 0) {
293 static void Disassemble (void)
294 /* Disassemble the code */
300 Output ("---------------------------");
306 DefOutOfRangeLabels ();
312 int main (int argc, char* argv [])
313 /* Assembler main program */
315 /* Program long options */
316 static const LongOpt OptTab[] = {
317 { "--cpu", 1, OptCPU },
318 { "--formfeeds", 0, OptFormFeeds },
319 { "--help", 0, OptHelp },
320 { "--pagelength", 1, OptPageLength },
321 { "--start-addr", 1, OptStartAddr },
322 { "--verbose", 0, OptVerbose },
323 { "--version", 0, OptVersion },
328 /* Initialize the cmdline module */
329 InitCmdLine (&argc, &argv, "da65");
331 /* Check the parameters */
333 while (I < ArgCount) {
335 /* Get the argument */
336 const char* Arg = ArgVec[I];
338 /* Check for an option */
339 if (Arg [0] == '-') {
343 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
351 OutFile = GetArg (&I, 2);
359 OptStartAddr (Arg, GetArg (&I, 2));
372 /* Filename. Check if we already had one */
374 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
386 /* Must have an input file */
388 AbEnd ("No input file");
391 /* Make the config file name from the input file if none was given */
393 CfgSetName (MakeFilename (InFile, CfgExt));
396 /* Try to read the configuration file */
399 /* Make the output file name from the input file name if none was given */
401 OutFile = MakeFilename (InFile, OutExt);
404 /* Load the input file */
405 LoadCode (InFile, StartAddr);
407 /* Open the output file */
408 OpenOutput (OutFile);
410 /* Disassemble the code */
413 /* Close the output file */