]> git.sur5r.net Git - cc65/blob - src/da65/main.c
916afbdfb02936fd74250fa5d9b2c2c225462646
[cc65] / src / da65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                  Main program for the da65 disassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>
41
42 /* common */
43 #include "abend.h"
44 #include "cmdline.h"
45 #include "fname.h"
46 #include "print.h"
47 #include "version.h"
48
49 /* da65 */
50 #include "attrtab.h"
51 #include "code.h"
52 #include "config.h"
53 #include "cpu.h"
54 #include "data.h"
55 #include "error.h"
56 #include "global.h"
57 #include "opctable.h"
58 #include "output.h"
59 #include "scanner.h"
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 static void Usage (void)
70 /* Print usage information and exit */
71 {
72     fprintf (stderr,
73              "Usage: %s [options] file\n"
74              "Short options:\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"
82              "\n"
83              "Long options:\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",
91              ProgName);
92 }
93
94
95
96 static unsigned long CvtNumber (const char* Arg, const char* Number)
97 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
98  * numbers.
99  */
100 {
101     unsigned long Val;
102     int           Converted;
103
104     /* Convert */
105     if (*Number == '$') {
106         ++Number;
107         Converted = sscanf (Number, "%lx", &Val);
108     } else {
109         Converted = sscanf (Number, "%li", (long*)&Val);
110     }
111
112     /* Check if we do really have a number */
113     if (Converted != 1) {
114         Error ("Invalid number given in argument: %s\n", Arg);
115     }
116
117     /* Return the result */
118     return Val;
119 }
120
121
122
123 static void OptCPU (const char* Opt, const char* Arg)
124 /* Handle the --cpu option */
125 {
126     if (Arg == 0) {
127         NeedArg (Opt);
128     }
129     if (strcmp (Arg, "6502") == 0) {
130         SetCPU (CPU_6502);
131     } else if (strcmp (Arg, "65C02") == 0) {
132         SetCPU (CPU_65C02);
133     } else if (strcmp (Arg, "65816") == 0) {
134         SetCPU (CPU_65816);
135 #ifdef SUNPLUS
136     } else if (strcmp (Arg, "sunplus") == 0) {
137         SetCPU (CPU_SUNPLUS);
138 #endif
139     } else {
140         AbEnd ("Invalid CPU: `%s'", Arg);
141     }
142 }
143
144
145
146 static void OptFormFeeds (const char* Opt, const char* Arg)
147 /* Add form feeds to the output */
148 {
149     FormFeeds = 1;
150 }
151
152
153
154 static void OptHelp (const char* Opt, const char* Arg)
155 /* Print usage information and exit */
156 {
157     Usage ();
158     exit (EXIT_SUCCESS);
159 }
160
161
162
163 static void OptPageLength (const char* Opt, const char* Arg)
164 /* Handle the --pagelength option */
165 {
166     int Len;
167     if (Arg == 0) {
168         NeedArg (Opt);
169     }
170     Len = atoi (Arg);
171     if (Len != -1 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
172         AbEnd ("Invalid page length: %d", Len);
173     }
174     PageLength = Len;
175 }
176
177
178
179 static void OptStartAddr (const char* Opt, const char* Arg)
180 /* Set the default start address */
181 {
182     StartAddr = CvtNumber (Opt, Arg);
183 }
184
185
186
187 static void OptVerbose (const char* Opt, const char* Arg)
188 /* Increase verbosity */
189 {
190     ++Verbosity;
191 }
192
193
194
195 static void OptVersion (const char* Opt, const char* Arg)
196 /* Print the disassembler version */
197 {
198     fprintf (stderr,
199              "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
200              VER_MAJOR, VER_MINOR, VER_PATCH);
201 }
202
203
204
205 static void OneOpcode (unsigned RemainingBytes)
206 /* Disassemble one opcode */
207 {
208     /* Get the opcode from the current address */
209     unsigned char OPC = GetCodeByte (PC);
210
211     /* Get the opcode description for the opcode byte */
212     const OpcDesc* D = &OpcTable[OPC];
213
214     /* If we have a label at this address, output the label */
215     if (MustDefLabel (PC)) {
216         DefLabel (GetLabel (PC));
217     }
218
219     /* Check...
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.
224      */
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);
230         } else {
231             unsigned I;
232             for (I = 1; I < D->Size; ++I) {
233                 if (HaveLabel (PC+I)) {
234                     MarkAddr (PC, atIllegal);
235                     break;
236                 }
237             }
238         }
239     }
240
241     /* Disassemble the line */
242     switch (GetStyleAttr (PC)) {
243
244         case atDefault:
245         case atCode:
246             D->Handler (D);
247             PC += D->Size;
248             break;
249
250         case atByteTab:
251             ByteTable ();
252             break;
253
254         case atWordTab:
255             WordTable ();
256             break;
257
258         case atDWordTab:
259             DWordTable ();
260             break;
261
262         case atAddrTab:
263             AddrTable ();
264             break;
265
266         case atRtsTab:
267             RtsTable ();
268             break;
269
270         default:
271             DataByteLine (1);
272             ++PC;
273             break;
274
275     }
276 }
277
278
279
280 static void OnePass (void)
281 /* Make one pass through the code */
282 {
283     unsigned Count;
284
285     /* Disassemble until nothing left */
286     while ((Count = GetRemainingBytes()) > 0) {
287         OneOpcode (Count);
288     }
289 }
290
291
292
293 static void Disassemble (void)
294 /* Disassemble the code */
295 {
296     /* Pass 1 */
297     Pass = 1;
298     OnePass ();
299
300     Output ("---------------------------");
301     LineFeed ();
302
303     /* Pass 2 */
304     Pass = 2;
305     ResetCode ();
306     DefOutOfRangeLabels ();
307     OnePass ();
308 }
309
310
311
312 int main (int argc, char* argv [])
313 /* Assembler main program */
314 {
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              },
324     };
325
326     int I;
327
328     /* Initialize the cmdline module */
329     InitCmdLine (&argc, &argv, "da65");
330
331     /* Check the parameters */
332     I = 1;
333     while (I < ArgCount) {
334
335         /* Get the argument */
336         const char* Arg = ArgVec[I];
337
338         /* Check for an option */
339         if (Arg [0] == '-') {
340             switch (Arg [1]) {
341
342                 case '-':
343                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
344                     break;
345
346                 case 'h':
347                     OptHelp (Arg, 0);
348                     break;
349
350                 case 'o':
351                     OutFile = GetArg (&I, 2);
352                     break;
353
354                 case 'v':
355                     OptVerbose (Arg, 0);
356                     break;
357
358                 case 'S':
359                     OptStartAddr (Arg, GetArg (&I, 2));
360                     break;
361
362                 case 'V':
363                     OptVersion (Arg, 0);
364                     break;
365
366                 default:
367                     UnknownOption (Arg);
368                     break;
369
370             }
371         } else {
372             /* Filename. Check if we already had one */
373             if (InFile) {
374                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
375                          ProgName, Arg);
376                 exit (EXIT_FAILURE);
377             } else {
378                 InFile = Arg;
379             }
380         }
381
382         /* Next argument */
383         ++I;
384     }
385
386     /* Must have an input file */
387     if (InFile == 0) {
388         AbEnd ("No input file");
389     }
390
391     /* Make the config file name from the input file if none was given */
392     if (!CfgAvail ()) {
393         CfgSetName (MakeFilename (InFile, CfgExt));
394     }
395
396     /* Try to read the configuration file */
397     CfgRead ();
398
399     /* Make the output file name from the input file name if none was given */
400     if (OutFile == 0) {
401         OutFile = MakeFilename (InFile, OutExt);
402     }
403
404     /* Load the input file */
405     LoadCode (InFile, StartAddr);
406
407     /* Open the output file */
408     OpenOutput (OutFile);
409
410     /* Disassemble the code */
411     Disassemble ();
412
413     /* Close the output file */
414     CloseOutput ();
415
416     /* Done */
417     return EXIT_SUCCESS;
418 }
419
420
421