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 const char* Label = GetLabel (PC);
183 * - ...if we have enough bytes remaining for the code at this address.
184 * - ...if the current instruction is valid for the given CPU.
185 * - ...if there is no label somewhere between the instruction bytes.
186 * If any of these conditions is true, switch to data mode.
188 if (GetStyle (PC) == atDefault) {
189 if (D->Size > RemainingBytes) {
190 MarkAddr (PC, atIllegal);
191 } else if ((D->CPU & CPU) != CPU) {
192 MarkAddr (PC, atIllegal);
195 for (I = 1; I < D->Size; ++I) {
196 if (HaveLabel (PC+I)) {
197 MarkAddr (PC, atIllegal);
204 /* Disassemble the line */
205 switch (GetStyle (PC)) {
214 ByteTable (RemainingBytes);
218 WordTable (RemainingBytes);
222 AddrTable (RemainingBytes);
235 static void OnePass (void)
236 /* Make one pass through the code */
240 /* Disassemble until nothing left */
241 while ((Count = GetRemainingBytes()) > 0) {
248 static void Disassemble (void)
249 /* Disassemble the code */
255 Output ("---------------------------");
261 DefOutOfRangeLabels ();
267 int main (int argc, char* argv [])
268 /* Assembler main program */
270 /* Program long options */
271 static const LongOpt OptTab[] = {
272 { "--cpu", 1, OptCPU },
273 { "--formfeeds", 0, OptFormFeeds },
274 { "--help", 0, OptHelp },
275 { "--pagelength", 1, OptPageLength },
276 { "--verbose", 0, OptVerbose },
277 { "--version", 0, OptVersion },
282 /* Initialize the cmdline module */
283 InitCmdLine (argc, argv, "da65");
285 /* Check the parameters */
289 /* Get the argument */
290 const char* Arg = argv [I];
292 /* Check for an option */
293 if (Arg [0] == '-') {
297 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
305 OutFile = GetArg (&I, 2);
322 /* Filename. Check if we already had one */
324 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
336 /* Must have an input file */
338 AbEnd ("No input file");
341 /* Make the config file name from the input file if none was given */
343 CfgSetName (MakeFilename (InFile, CfgExt));
346 /* Try to read the configuration file */
349 /* Make the output file name from the input file name if none was given */
351 OutFile = MakeFilename (InFile, OutExt);
354 /* Load the input file */
355 LoadCode (InFile, 0xE000); /* ### */
357 /* Open the output file */
358 OpenOutput (OutFile);
360 /* Disassemble the code */
363 /* Close the output file */