]> git.sur5r.net Git - cc65/blob - src/da65/main.c
Added SBC optimizations suggested by Piotr Fusik
[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 attribute ((unused)),
147                           const char* Arg attribute ((unused)))
148 /* Add form feeds to the output */
149 {
150     FormFeeds = 1;
151 }
152
153
154
155 static void OptHelp (const char* Opt attribute ((unused)),
156                      const char* Arg attribute ((unused)))
157 /* Print usage information and exit */
158 {
159     Usage ();
160     exit (EXIT_SUCCESS);
161 }
162
163
164
165 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
166 /* Handle the --pagelength option */
167 {
168     int Len = atoi (Arg);
169     if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
170         AbEnd ("Invalid page length: %d", Len);
171     }
172     PageLength = Len;
173 }
174
175
176
177 static void OptStartAddr (const char* Opt, const char* Arg)
178 /* Set the default start address */
179 {
180     StartAddr = CvtNumber (Opt, Arg);
181 }
182
183
184
185 static void OptVerbose (const char* Opt attribute ((unused)),
186                         const char* Arg attribute ((unused)))
187 /* Increase verbosity */
188 {
189     ++Verbosity;
190 }
191
192
193
194 static void OptVersion (const char* Opt attribute ((unused)),
195                         const char* Arg attribute ((unused)))
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         case atTextTab:
271             TextTable ();
272             break;
273
274         default:
275             DataByteLine (1);
276             ++PC;
277             break;
278
279     }
280 }
281
282
283
284 static void OnePass (void)
285 /* Make one pass through the code */
286 {
287     unsigned Count;
288
289     /* Disassemble until nothing left */
290     while ((Count = GetRemainingBytes()) > 0) {
291         OneOpcode (Count);
292     }
293 }
294
295
296
297 static void Disassemble (void)
298 /* Disassemble the code */
299 {
300     /* Pass 1 */
301     Pass = 1;
302     OnePass ();
303
304     Output ("---------------------------");
305     LineFeed ();
306
307     /* Pass 2 */
308     Pass = 2;
309     ResetCode ();
310     DefOutOfRangeLabels ();
311     OnePass ();
312 }
313
314
315
316 int main (int argc, char* argv [])
317 /* Assembler main program */
318 {
319     /* Program long options */
320     static const LongOpt OptTab[] = {
321         { "--cpu",              1,      OptCPU                  },
322         { "--formfeeds",        0,      OptFormFeeds            },
323         { "--help",             0,      OptHelp                 },
324         { "--pagelength",       1,      OptPageLength           },
325         { "--start-addr",       1,      OptStartAddr            },
326         { "--verbose",          0,      OptVerbose              },
327         { "--version",          0,      OptVersion              },
328     };
329
330     unsigned I;
331
332     /* Initialize the cmdline module */
333     InitCmdLine (&argc, &argv, "da65");
334
335     /* Check the parameters */
336     I = 1;
337     while (I < ArgCount) {
338
339         /* Get the argument */
340         const char* Arg = ArgVec[I];
341
342         /* Check for an option */
343         if (Arg [0] == '-') {
344             switch (Arg [1]) {
345
346                 case '-':
347                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
348                     break;
349
350                 case 'h':
351                     OptHelp (Arg, 0);
352                     break;
353
354                 case 'o':
355                     OutFile = GetArg (&I, 2);
356                     break;
357
358                 case 'v':
359                     OptVerbose (Arg, 0);
360                     break;
361
362                 case 'S':
363                     OptStartAddr (Arg, GetArg (&I, 2));
364                     break;
365
366                 case 'V':
367                     OptVersion (Arg, 0);
368                     break;
369
370                 default:
371                     UnknownOption (Arg);
372                     break;
373
374             }
375         } else {
376             /* Filename. Check if we already had one */
377             if (InFile) {
378                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
379                          ProgName, Arg);
380                 exit (EXIT_FAILURE);
381             } else {
382                 InFile = Arg;
383             }
384         }
385
386         /* Next argument */
387         ++I;
388     }
389
390     /* Must have an input file */
391     if (InFile == 0) {
392         AbEnd ("No input file");
393     }
394
395     /* Make the config file name from the input file if none was given */
396     if (!CfgAvail ()) {
397         CfgSetName (MakeFilename (InFile, CfgExt));
398     }
399
400     /* Try to read the configuration file */
401     CfgRead ();
402
403     /* Make the output file name from the input file name if none was given */
404     if (OutFile == 0) {
405         OutFile = MakeFilename (InFile, OutExt);
406     }
407
408     /* Load the input file */
409     LoadCode (InFile, StartAddr);
410
411     /* Open the output file */
412     OpenOutput (OutFile);
413
414     /* Disassemble the code */
415     Disassemble ();
416
417     /* Close the output file */
418     CloseOutput ();
419
420     /* Done */
421     return EXIT_SUCCESS;
422 }
423
424
425