]> git.sur5r.net Git - cc65/blob - src/da65/main.c
94e535cb31634a49a1bd2ede7db4afcc51196993
[cc65] / src / da65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                  Main program for the da65 disassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 "cpu.h"
46 #include "fname.h"
47 #include "print.h"
48 #include "version.h"
49
50 /* da65 */
51 #include "attrtab.h"
52 #include "code.h"
53 #include "data.h"
54 #include "error.h"
55 #include "global.h"
56 #include "infofile.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] [inputfile]\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              "  -i name\t\tSpecify an info file\n"
78              "  -o name\t\tName the output file\n"
79              "  -v\t\t\tIncrease verbosity\n"
80              "  -F\t\t\tAdd formfeeds to the output\n"
81              "  -S addr\t\tSet the start/load address\n"
82              "  -V\t\t\tPrint the disassembler version\n"
83              "\n"
84              "Long options:\n"
85              "  --comments n\t\tSet the comment level for the output\n"
86              "  --cpu type\t\tSet cpu type\n"
87              "  --debug-info\t\tAdd debug info to object file\n"
88              "  --formfeeds\t\tAdd formfeeds to the output\n"
89              "  --help\t\tHelp (this text)\n"
90              "  --info name\t\tSpecify an info file\n"
91              "  --pagelength n\tSet the page length for the listing\n"
92              "  --start-addr addr\tSet the start/load address\n"
93              "  --verbose\t\tIncrease verbosity\n"
94              "  --version\t\tPrint the disassembler version\n",
95              ProgName);
96 }
97
98
99
100 static unsigned long CvtNumber (const char* Arg, const char* Number)
101 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
102  * numbers.
103  */
104 {
105     unsigned long Val;
106     int           Converted;
107     char          BoundsCheck;
108
109     /* Convert */
110     if (*Number == '$') {
111         ++Number;
112         Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
113     } else {
114         Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
115     }
116
117     /* Check if we do really have a number */
118     if (Converted != 1) {
119         Error ("Invalid number given in argument: %s\n", Arg);
120     }
121
122     /* Return the result */
123     return Val;
124 }
125
126
127
128 static void OptComments (const char* Opt, const char* Arg)
129 /* Handle the --comments option */
130 {
131     /* Convert the argument to a number */
132     unsigned long Val = CvtNumber (Opt, Arg);
133
134     /* Check for a valid range */
135     if (Val > MAX_COMMENTS) {
136         Error ("Argument for %s outside valid range (%d-%d)",
137                Opt, MIN_COMMENTS, MAX_COMMENTS);
138     }
139
140     /* Use the value */
141     Comments = (unsigned char) Val;
142 }
143
144
145
146 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
147 /* Handle the --cpu option */
148 {
149     /* Find the CPU from the given name */
150     CPU = FindCPU (Arg);
151     SetOpcTable (CPU);
152 }
153
154
155
156 static void OptDebugInfo (const char* Opt attribute ((unused)),
157                           const char* Arg attribute ((unused)))
158 /* Add debug info to the object file */
159 {
160     DebugInfo = 1;
161 }
162
163
164
165 static void OptFormFeeds (const char* Opt attribute ((unused)),
166                           const char* Arg attribute ((unused)))
167 /* Add form feeds to the output */
168 {
169     FormFeeds = 1;
170 }
171
172
173
174 static void OptHelp (const char* Opt attribute ((unused)),
175                      const char* Arg attribute ((unused)))
176 /* Print usage information and exit */
177 {
178     Usage ();
179     exit (EXIT_SUCCESS);
180 }
181
182
183
184 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
185 /* Handle the --info option */
186 {
187     InfoSetName (Arg);
188 }
189
190
191
192 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
193 /* Handle the --pagelength option */
194 {
195     int Len = atoi (Arg);
196     if (Len != 0 && (Len < MIN_PAGE_LEN || Len > MAX_PAGE_LEN)) {
197         AbEnd ("Invalid page length: %d", Len);
198     }
199     PageLength = Len;
200 }
201
202
203
204 static void OptStartAddr (const char* Opt, const char* Arg)
205 /* Set the default start address */
206 {
207     StartAddr = CvtNumber (Opt, Arg);
208 }
209
210
211
212 static void OptVerbose (const char* Opt attribute ((unused)),
213                         const char* Arg attribute ((unused)))
214 /* Increase verbosity */
215 {
216     ++Verbosity;
217 }
218
219
220
221 static void OptVersion (const char* Opt attribute ((unused)),
222                         const char* Arg attribute ((unused)))
223 /* Print the disassembler version */
224 {
225     fprintf (stderr,
226              "da65 V%u.%u.%u - (C) Copyright 2000 Ullrich von Bassewitz\n",
227              VER_MAJOR, VER_MINOR, VER_PATCH);
228 }
229
230
231
232 static void OneOpcode (unsigned RemainingBytes)
233 /* Disassemble one opcode */
234 {
235     /* Get the opcode from the current address */
236     unsigned char OPC = GetCodeByte (PC);
237
238     /* Get the opcode description for the opcode byte */
239     const OpcDesc* D = &OpcTable[OPC];
240
241     /* Get the output style for the current PC */
242     attr_t Style = GetStyleAttr (PC);
243
244     /* If we have a label at this address, output the label, provided that
245      * we aren't in a skip area.
246      */
247     if (Style != atSkip && MustDefLabel (PC)) {
248         DefLabel (GetLabel (PC));
249     }
250
251     /* Check...
252      *   - ...if we have enough bytes remaining for the code at this address.
253      *   - ...if the current instruction is valid for the given CPU.
254      *   - ...if there is no label somewhere between the instruction bytes.
255      * If any of these conditions is false, switch to data mode.
256      */
257     if (Style == atDefault) {
258         if (D->Size > RemainingBytes) {
259             MarkAddr (PC, atIllegal);
260         } else if (D->Flags & flIllegal) {
261             MarkAddr (PC, atIllegal);
262         } else {
263             unsigned I;
264             for (I = 1; I < D->Size; ++I) {
265                 if (HaveLabel (PC+I)) {
266                     MarkAddr (PC, atIllegal);
267                     break;
268                 }
269             }
270         }
271     }
272
273     /* Disassemble the line */
274     switch (Style) {
275
276         case atDefault:
277         case atCode:
278             D->Handler (D);
279             PC += D->Size;
280             break;
281
282         case atByteTab:
283             ByteTable ();
284             break;
285
286         case atDByteTab:
287             DByteTable ();
288             break;
289
290         case atWordTab:
291             WordTable ();
292             break;
293
294         case atDWordTab:
295             DWordTable ();
296             break;
297
298         case atAddrTab:
299             AddrTable ();
300             break;
301                     
302         case atRtsTab:
303             RtsTable ();
304             break;
305
306         case atTextTab:
307             TextTable ();
308             break;
309
310         case atSkip:
311             ++PC;
312             break;
313
314         default:
315             DataByteLine (1);
316             ++PC;
317             break;
318
319     }
320 }
321
322
323
324 static void OnePass (void)
325 /* Make one pass through the code */
326 {
327     unsigned Count;
328
329     /* Disassemble until nothing left */
330     while ((Count = GetRemainingBytes()) > 0) {
331         OneOpcode (Count);
332     }
333 }
334
335
336
337 static void Disassemble (void)
338 /* Disassemble the code */
339 {
340     /* Pass 1 */
341     Pass = 1;
342     OnePass ();
343
344     Output ("---------------------------");
345     LineFeed ();
346
347     /* Pass 2 */
348     Pass = 2;
349     ResetCode ();
350     OutputSettings ();
351     DefOutOfRangeLabels ();
352     OnePass ();
353 }
354
355
356
357 int main (int argc, char* argv [])
358 /* Assembler main program */
359 {
360     /* Program long options */
361     static const LongOpt OptTab[] = {
362         { "--comments",         1,      OptComments             },
363         { "--cpu",              1,      OptCPU                  },
364         { "--debug-info",       0,      OptDebugInfo            },
365         { "--formfeeds",        0,      OptFormFeeds            },
366         { "--help",             0,      OptHelp                 },
367         { "--info",             1,      OptInfo                 },
368         { "--pagelength",       1,      OptPageLength           },
369         { "--start-addr",       1,      OptStartAddr            },
370         { "--verbose",          0,      OptVerbose              },
371         { "--version",          0,      OptVersion              },
372     };
373
374     unsigned I;
375
376     /* Initialize the cmdline module */
377     InitCmdLine (&argc, &argv, "da65");
378
379     /* Check the parameters */
380     I = 1;
381     while (I < ArgCount) {
382
383         /* Get the argument */
384         const char* Arg = ArgVec[I];
385
386         /* Check for an option */
387         if (Arg [0] == '-') {
388             switch (Arg [1]) {
389
390                 case '-':
391                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
392                     break;
393
394                 case 'g':
395                     OptDebugInfo (Arg, 0);
396                     break;
397
398                 case 'h':
399                     OptHelp (Arg, 0);
400                     break;
401
402                 case 'i':
403                     OptInfo (Arg, GetArg (&I, 2));
404                     break;
405
406                 case 'o':
407                     OutFile = GetArg (&I, 2);
408                     break;
409
410                 case 'v':
411                     OptVerbose (Arg, 0);
412                     break;
413
414                 case 'S':
415                     OptStartAddr (Arg, GetArg (&I, 2));
416                     break;
417
418                 case 'V':
419                     OptVersion (Arg, 0);
420                     break;
421
422                 default:
423                     UnknownOption (Arg);
424                     break;
425
426             }
427         } else {
428             /* Filename. Check if we already had one */
429             if (InFile) {
430                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
431                          ProgName, Arg);
432                 exit (EXIT_FAILURE);
433             } else {
434                 InFile = Arg;
435             }
436         }
437
438         /* Next argument */
439         ++I;
440     }
441
442     /* Try to read the info file */
443     ReadInfoFile ();
444
445     /* Must have an input file */
446     if (InFile == 0) {
447         AbEnd ("No input file");
448     }
449
450     /* Make the output file name from the input file name if none was given */
451     if (OutFile == 0) {
452         OutFile = MakeFilename (InFile, OutExt);
453     }
454
455     /* If no CPU given, use the default CPU */
456     if (CPU == CPU_UNKNOWN) {
457         CPU = CPU_6502;
458     }
459
460     /* Load the input file */
461     LoadCode ();
462
463     /* Open the output file */
464     OpenOutput (OutFile);
465
466     /* Disassemble the code */
467     Disassemble ();
468
469     /* Close the output file */
470     CloseOutput ();
471
472     /* Done */
473     return EXIT_SUCCESS;
474 }
475
476
477