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