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 " -V\t\t\tPrint the assembler version\n"
82 " --cpu type\t\tSet cpu type\n"
83 " --formfeeds\t\tAdd formfeeds to the output\n"
84 " --help\t\tHelp (this text)\n"
85 " --pagelength n\tSet the page length for the listing\n"
86 " --verbose\t\tIncrease verbosity\n"
87 " --version\t\tPrint the assembler version\n",
93 static void OptCPU (const char* Opt, const char* Arg)
94 /* Handle the --cpu option */
99 if (strcmp (Arg, "6502") == 0) {
101 } else if (strcmp (Arg, "65C02") == 0) {
103 } else if (strcmp (Arg, "65816") == 0) {
106 } else if (strcmp (Arg, "sunplus") == 0) {
107 SetCPU (CPU_SUNPLUS);
110 AbEnd ("Invalid CPU: `%s'", Arg);
116 static void OptFormFeeds (const char* Opt, const char* Arg)
117 /* Add form feeds to the output */
124 static void OptHelp (const char* Opt, const char* Arg)
125 /* Print usage information and exit */
133 static void OptPageLength (const char* Opt, const char* Arg)
134 /* Handle the --pagelength option */
141 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
142 AbEnd ("Invalid page length: %d", Len);
149 static void OptVerbose (const char* Opt, const char* Arg)
150 /* Increase verbosity */
157 static void OptVersion (const char* Opt, const char* Arg)
158 /* Print the disassembler version */
161 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
162 VER_MAJOR, VER_MINOR, VER_PATCH);
167 static void OneOpcode (unsigned RemainingBytes)
168 /* Disassemble one opcode */
170 /* Get the opcode from the current address */
171 unsigned char OPC = GetCodeByte (PC);
173 /* Get the opcode description for the opcode byte */
174 const OpcDesc* D = &OpcTable[OPC];
176 /* If we have a label at this address, output the label */
177 if (MustDefLabel (PC)) {
178 DefLabel (GetLabel (PC));
182 * - ...if we have enough bytes remaining for the code at this address.
183 * - ...if the current instruction is valid for the given CPU.
184 * - ...if there is no label somewhere between the instruction bytes.
185 * If any of these conditions is true, switch to data mode.
187 if (GetStyleAttr (PC) == atDefault) {
188 if (D->Size > RemainingBytes) {
189 MarkAddr (PC, atIllegal);
190 } else if ((D->CPU & CPU) != CPU) {
191 MarkAddr (PC, atIllegal);
194 for (I = 1; I < D->Size; ++I) {
195 if (HaveLabel (PC+I)) {
196 MarkAddr (PC, atIllegal);
203 /* Disassemble the line */
204 switch (GetStyleAttr (PC)) {
242 static void OnePass (void)
243 /* Make one pass through the code */
247 /* Disassemble until nothing left */
248 while ((Count = GetRemainingBytes()) > 0) {
255 static void Disassemble (void)
256 /* Disassemble the code */
262 Output ("---------------------------");
268 DefOutOfRangeLabels ();
274 int main (int argc, char* argv [])
275 /* Assembler main program */
277 /* Program long options */
278 static const LongOpt OptTab[] = {
279 { "--cpu", 1, OptCPU },
280 { "--formfeeds", 0, OptFormFeeds },
281 { "--help", 0, OptHelp },
282 { "--pagelength", 1, OptPageLength },
283 { "--verbose", 0, OptVerbose },
284 { "--version", 0, OptVersion },
289 /* Initialize the cmdline module */
290 InitCmdLine (argc, argv, "da65");
292 /* Check the parameters */
296 /* Get the argument */
297 const char* Arg = argv [I];
299 /* Check for an option */
300 if (Arg [0] == '-') {
304 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
312 OutFile = GetArg (&I, 2);
329 /* Filename. Check if we already had one */
331 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
343 /* Must have an input file */
345 AbEnd ("No input file");
348 /* Make the config file name from the input file if none was given */
350 CfgSetName (MakeFilename (InFile, CfgExt));
353 /* Try to read the configuration file */
356 /* Make the output file name from the input file name if none was given */
358 OutFile = MakeFilename (InFile, OutExt);
361 /* Load the input file */
362 LoadCode (InFile, 0xE000); /* ### */
364 /* Open the output file */
365 OpenOutput (OutFile);
367 /* Disassemble the code */
370 /* Close the output file */