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 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void Usage (void)
69 /* Print usage information and exit */
72 "Usage: %s [options] file\n"
74 " -g\t\t\tAdd debug info to object file\n"
75 " -h\t\t\tHelp (this text)\n"
76 " -o name\t\tName the output file\n"
77 " -v\t\t\tIncrease verbosity\n"
78 " -F\t\t\tAdd formfeeds to the output\n"
79 " -S addr\t\tSet the start/load address\n"
80 " -V\t\t\tPrint the disassembler version\n"
83 " --cpu type\t\tSet cpu type\n"
84 " --formfeeds\t\tAdd formfeeds to the output\n"
85 " --help\t\tHelp (this text)\n"
86 " --pagelength n\tSet the page length for the listing\n"
87 " --start-addr addr\tSet the start/load address\n"
88 " --verbose\t\tIncrease verbosity\n"
89 " --version\t\tPrint the disassembler version\n",
95 static unsigned long CvtNumber (const char* Arg, const char* Number)
96 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
104 if (*Number == '$') {
106 Converted = sscanf (Number, "%lx", &Val);
108 Converted = sscanf (Number, "%li", (long*)&Val);
111 /* Check if we do really have a number */
112 if (Converted != 1) {
113 Error ("Invalid number given in argument: %s\n", Arg);
116 /* Return the result */
122 static void OptCPU (const char* Opt, const char* Arg)
123 /* Handle the --cpu option */
128 if (strcmp (Arg, "6502") == 0) {
130 } else if (strcmp (Arg, "65C02") == 0) {
132 } else if (strcmp (Arg, "65816") == 0) {
135 } else if (strcmp (Arg, "sunplus") == 0) {
136 SetCPU (CPU_SUNPLUS);
139 AbEnd ("Invalid CPU: `%s'", Arg);
145 static void OptFormFeeds (const char* Opt, const char* Arg)
146 /* Add form feeds to the output */
153 static void OptHelp (const char* Opt, const char* Arg)
154 /* Print usage information and exit */
162 static void OptPageLength (const char* Opt, const char* Arg)
163 /* Handle the --pagelength option */
170 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
171 AbEnd ("Invalid page length: %d", Len);
178 static void OptStartAddr (const char* Opt, const char* Arg)
179 /* Set the default start address */
181 StartAddr = CvtNumber (Opt, Arg);
186 static void OptVerbose (const char* Opt, const char* Arg)
187 /* Increase verbosity */
194 static void OptVersion (const char* Opt, const char* Arg)
195 /* Print the disassembler version */
198 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
199 VER_MAJOR, VER_MINOR, VER_PATCH);
204 static void OneOpcode (unsigned RemainingBytes)
205 /* Disassemble one opcode */
207 /* Get the opcode from the current address */
208 unsigned char OPC = GetCodeByte (PC);
210 /* Get the opcode description for the opcode byte */
211 const OpcDesc* D = &OpcTable[OPC];
213 /* If we have a label at this address, output the label */
214 if (MustDefLabel (PC)) {
215 DefLabel (GetLabel (PC));
219 * - ...if we have enough bytes remaining for the code at this address.
220 * - ...if the current instruction is valid for the given CPU.
221 * - ...if there is no label somewhere between the instruction bytes.
222 * If any of these conditions is true, switch to data mode.
224 if (GetStyleAttr (PC) == atDefault) {
225 if (D->Size > RemainingBytes) {
226 MarkAddr (PC, atIllegal);
227 } else if ((D->CPU & CPU) != CPU) {
228 MarkAddr (PC, atIllegal);
231 for (I = 1; I < D->Size; ++I) {
232 if (HaveLabel (PC+I)) {
233 MarkAddr (PC, atIllegal);
240 /* Disassemble the line */
241 switch (GetStyleAttr (PC)) {
279 static void OnePass (void)
280 /* Make one pass through the code */
284 /* Disassemble until nothing left */
285 while ((Count = GetRemainingBytes()) > 0) {
292 static void Disassemble (void)
293 /* Disassemble the code */
299 Output ("---------------------------");
305 DefOutOfRangeLabels ();
311 int main (int argc, char* argv [])
312 /* Assembler main program */
314 /* Program long options */
315 static const LongOpt OptTab[] = {
316 { "--cpu", 1, OptCPU },
317 { "--formfeeds", 0, OptFormFeeds },
318 { "--help", 0, OptHelp },
319 { "--pagelength", 1, OptPageLength },
320 { "--start-addr", 1, OptStartAddr },
321 { "--verbose", 0, OptVerbose },
322 { "--version", 0, OptVersion },
327 /* Initialize the cmdline module */
328 InitCmdLine (&argc, &argv, "da65");
330 /* Check the parameters */
332 while (I < ArgCount) {
334 /* Get the argument */
335 const char* Arg = ArgVec[I];
337 /* Check for an option */
338 if (Arg [0] == '-') {
342 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
350 OutFile = GetArg (&I, 2);
358 OptStartAddr (Arg, GetArg (&I, 2));
371 /* Filename. Check if we already had one */
373 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
385 /* Must have an input file */
387 AbEnd ("No input file");
390 /* Make the config file name from the input file if none was given */
392 CfgSetName (MakeFilename (InFile, CfgExt));
395 /* Try to read the configuration file */
398 /* Make the output file name from the input file name if none was given */
400 OutFile = MakeFilename (InFile, OutExt);
403 /* Load the input file */
404 LoadCode (InFile, StartAddr);
406 /* Open the output file */
407 OpenOutput (OutFile);
409 /* Disassemble the code */
412 /* Close the output file */