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 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static void Usage (void)
68 /* Print usage information and exit */
71 "Usage: %s [options] file\n"
73 " -g\t\t\tAdd debug info to object file\n"
74 " -h\t\t\tHelp (this text)\n"
75 " -i\t\t\tIgnore case of symbols\n"
76 " -l\t\t\tCreate a listing if assembly was ok\n"
77 " -o name\t\tName the output file\n"
78 " -s\t\t\tEnable smart mode\n"
79 " -t sys\t\tSet the target system\n"
80 " -v\t\t\tIncrease verbosity\n"
81 " -D name[=value]\tDefine a symbol\n"
82 " -I dir\t\tSet an include directory search path\n"
83 " -U\t\t\tMark unresolved symbols as import\n"
84 " -V\t\t\tPrint the assembler version\n"
85 " -W n\t\t\tSet warning level n\n"
88 " --auto-import\t\tMark unresolved symbols as import\n"
89 " --cpu type\t\tSet cpu type\n"
90 " --debug-info\t\tAdd debug info to object file\n"
91 " --feature name\tSet an emulation feature\n"
92 " --help\t\tHelp (this text)\n"
93 " --ignore-case\t\tIgnore case of symbols\n"
94 " --include-dir dir\tSet an include directory search path\n"
95 " --listing\t\tCreate a listing if assembly was ok\n"
96 " --pagelength n\tSet the page length for the listing\n"
97 " --smart\t\tEnable smart mode\n"
98 " --target sys\t\tSet the target system\n"
99 " --verbose\t\tIncrease verbosity\n"
100 " --version\t\tPrint the assembler version\n",
106 static void OptCPU (const char* Opt, const char* Arg)
107 /* Handle the --cpu option */
112 if (strcmp (Arg, "6502") == 0) {
114 } else if (strcmp (Arg, "65C02") == 0) {
116 } else if (strcmp (Arg, "65816") == 0) {
119 } else if (strcmp (Arg, "sunplus") == 0) {
120 SetCPU (CPU_SUNPLUS);
123 AbEnd ("Invalid CPU: `%s'", Arg);
129 static void OptHelp (const char* Opt, const char* Arg)
130 /* Print usage information and exit */
138 static void OptPageLength (const char* Opt, const char* Arg)
139 /* Handle the --pagelength option */
146 if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
147 AbEnd ("Invalid page length: %d", Len);
154 static void OptVerbose (const char* Opt, const char* Arg)
155 /* Increase verbosity */
162 static void OptVersion (const char* Opt, const char* Arg)
163 /* Print the disassembler version */
166 "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
167 VER_MAJOR, VER_MINOR, VER_PATCH);
172 static void ByteTable (unsigned RemainingBytes)
173 /* Output a table of bytes */
175 /* Count how many bytes may be output. This number is limited by the
176 * number of remaining bytes, a label, or the end of the ByteTable
180 while (Count < RemainingBytes) {
181 if (HaveLabel(PC+Count) || GetStyle (PC+Count) != atByteTab) {
186 RemainingBytes -= Count;
188 /* Output as many data bytes lines as needed */
191 /* Calculate the number of bytes for the next line */
192 unsigned Chunk = (Count > BytesPerLine)? BytesPerLine : Count;
194 /* Output a line with these bytes */
195 DataByteLine (Chunk);
202 /* If the next line is not a byte table line, add a separator */
203 if (RemainingBytes > 0 && GetStyle (PC) != atByteTab) {
210 static void WordTable (unsigned RemainingBytes)
211 /* Output a table of words */
213 /* Count how many bytes may be output. This number is limited by the
214 * number of remaining bytes, a label, or the end of the WordTable
218 while (Count < RemainingBytes) {
219 if (HaveLabel(PC+Count) || GetStyle (PC+Count) != atWordTab) {
224 RemainingBytes -= Count;
226 /* Make the given number even */
229 /* Output as many data word lines as needed */
232 /* Calculate the number of bytes for the next line */
233 unsigned Chunk = (Count > BytesPerLine)? BytesPerLine : Count;
235 /* Output a line with these bytes */
236 DataWordLine (Chunk);
243 /* If the next line is not a byte table line, add a separator */
244 if (RemainingBytes > 0 && GetStyle (PC) != atWordTab) {
251 static void AddrTable (unsigned RemainingBytes)
252 /* Output a table of addresses */
254 /* Count how many bytes may be output. This number is limited by the
255 * number of remaining bytes, a label, or the end of the WordTable
259 while (Count < RemainingBytes) {
260 if (HaveLabel(PC+Count) || GetStyle (PC+Count) != atAddrTab) {
265 RemainingBytes -= Count;
267 /* Make the given number even */
270 /* Output as many data bytes lines as needed. For addresses, each line
271 * will hold just one address.
275 /* Get the address */
276 unsigned Addr = GetCodeWord (PC);
278 /* In pass 1, define a label, in pass 2 output the line */
280 if (!HaveLabel (Addr)) {
281 AddLabel (Addr, MakeLabelName (Addr));
284 const char* Label = GetLabel (Addr);
286 /* OOPS! Should not happen */
287 Internal ("OOPS - Label for address %04X disappeard!", Addr);
292 Output ("%s", Label);
302 /* If the next line is not a byte table line, add a separator */
303 if (RemainingBytes > 0 && GetStyle (PC) != atAddrTab) {
310 static void OneOpcode (unsigned RemainingBytes)
311 /* Disassemble one opcode */
313 /* Get the opcode from the current address */
314 unsigned char OPC = GetCodeByte (PC);
316 /* Get the opcode description for the opcode byte */
317 const OpcDesc* D = &OpcTable[OPC];
319 /* If we have a label at this address, output the label */
320 const char* Label = GetLabel (PC);
326 * - ...if we have enough bytes remaining for the code at this address.
327 * - ...if the current instruction is valid for the given CPU.
328 * - ...if there is no label somewhere between the instruction bytes.
329 * If any of these conditions is true, switch to data mode.
331 if (GetStyle (PC) == atDefault) {
332 if (D->Size > RemainingBytes) {
333 MarkAddr (PC, atIllegal);
334 } else if ((D->CPU & CPU) != CPU) {
335 MarkAddr (PC, atIllegal);
338 for (I = 1; I < D->Size; ++I) {
339 if (HaveLabel (PC+I)) {
340 MarkAddr (PC, atIllegal);
347 /* Disassemble the line */
348 switch (GetStyle (PC)) {
357 ByteTable (RemainingBytes);
361 WordTable (RemainingBytes);
365 AddrTable (RemainingBytes);
378 static void OnePass (void)
379 /* Make one pass through the code */
383 /* Disassemble until nothing left */
384 while ((Count = GetRemainingBytes()) > 0) {
391 static void Disassemble (void)
392 /* Disassemble the code */
398 Output ("---------------------------");
404 DefOutOfRangeLabels ();
410 int main (int argc, char* argv [])
411 /* Assembler main program */
413 /* Program long options */
414 static const LongOpt OptTab[] = {
415 { "--cpu", 1, OptCPU },
416 { "--help", 0, OptHelp },
417 { "--pagelength", 1, OptPageLength },
418 { "--verbose", 0, OptVerbose },
419 { "--version", 0, OptVersion },
424 /* Initialize the cmdline module */
425 InitCmdLine (argc, argv, "da65");
427 /* Check the parameters */
431 /* Get the argument */
432 const char* Arg = argv [I];
434 /* Check for an option */
435 if (Arg [0] == '-') {
439 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
447 OutFile = GetArg (&I, 2);
464 /* Filename. Check if we already had one */
466 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
478 /* Must have an input file */
480 AbEnd ("No input file");
483 /* Make the config file name from the input file if none was given */
485 CfgSetName (MakeFilename (InFile, CfgExt));
488 /* Try to read the configuration file */
491 /* Make the output file name from the input file name if none was given */
493 OutFile = MakeFilename (InFile, OutExt);
496 /* Load the input file */
497 LoadCode (InFile, 0xE000); /* ### */
499 /* Open the output file */
500 OpenOutput (OutFile);
502 /* Disassemble the code */
505 /* Close the output file */