]> git.sur5r.net Git - cc65/blob - src/da65/main.c
Save two bytes by a small code change.
[cc65] / src / da65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*                  Main program for the da65 disassembler                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2009, Ullrich von Bassewitz                                       */
10 /*                Roemerstrasse 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 <limits.h>
41 #include <time.h>
42
43 /* common */
44 #include "abend.h"
45 #include "cmdline.h"
46 #include "cpu.h"
47 #include "fname.h"
48 #include "print.h"
49 #include "version.h"
50
51 /* da65 */
52 #include "attrtab.h"
53 #include "code.h"
54 #include "comments.h"
55 #include "data.h"
56 #include "error.h"
57 #include "global.h"
58 #include "infofile.h"
59 #include "labels.h"
60 #include "opctable.h"
61 #include "output.h"
62 #include "scanner.h"
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static void Usage (void)
73 /* Print usage information and exit */
74 {
75     printf ("Usage: %s [options] [inputfile]\n"
76             "Short options:\n"
77             "  -g\t\t\tAdd debug info to object file\n"
78             "  -h\t\t\tHelp (this text)\n"
79             "  -i name\t\tSpecify an info file\n"
80             "  -o name\t\tName the output file\n"
81             "  -v\t\t\tIncrease verbosity\n"
82             "  -F\t\t\tAdd formfeeds to the output\n"
83             "  -S addr\t\tSet the start/load address\n"
84             "  -V\t\t\tPrint the disassembler version\n"
85             "\n"
86             "Long options:\n"
87             "  --argument-column n\tSpecify argument start column\n"
88             "  --comment-column n\tSpecify comment start column\n"
89             "  --comments n\t\tSet the comment level for the output\n"
90             "  --cpu type\t\tSet cpu type\n"
91             "  --debug-info\t\tAdd debug info to object file\n"
92             "  --formfeeds\t\tAdd formfeeds to the output\n"
93             "  --help\t\tHelp (this text)\n"
94             "  --hexoffs\t\tUse hexadecimal label offsets\n"
95             "  --info name\t\tSpecify an info file\n"
96             "  --label-break n\tAdd newline if label exceeds length n\n"
97             "  --mnemonic-column n\tSpecify mnemonic start column\n"
98             "  --pagelength n\tSet the page length for the listing\n"
99             "  --start-addr addr\tSet the start/load address\n"
100             "  --text-column n\tSpecify text start column\n"
101             "  --verbose\t\tIncrease verbosity\n"
102             "  --version\t\tPrint the disassembler version\n",
103             ProgName);
104 }
105
106
107
108 static void RangeCheck (const char* Opt, unsigned long Val,
109                         unsigned long Min, unsigned long Max)
110 /* Do a range check for the given option and abort if there's a range
111  * error.
112  */
113 {
114     if (Val < Min || Val > Max) {
115         Error ("Argument for %s outside valid range (%ld-%ld)", Opt, Min, Max);
116     }
117 }
118
119
120
121 static unsigned long CvtNumber (const char* Arg, const char* Number)
122 /* Convert a number from a string. Allow '$' and '0x' prefixes for hex
123  * numbers.
124  */
125 {
126     unsigned long Val;
127     int           Converted;
128     char          BoundsCheck;
129
130     /* Convert */
131     if (*Number == '$') {
132         ++Number;
133         Converted = sscanf (Number, "%lx%c", &Val, &BoundsCheck);
134     } else {
135         Converted = sscanf (Number, "%li%c", (long*)&Val, &BoundsCheck);
136     }
137
138     /* Check if we do really have a number */
139     if (Converted != 1) {
140         Error ("Invalid number given in argument: %s\n", Arg);
141     }
142
143     /* Return the result */
144     return Val;
145 }
146
147
148
149 static void OptArgumentColumn (const char* Opt, const char* Arg)
150 /* Handle the --argument-column option */
151 {
152     /* Convert the argument to a number */
153     unsigned long Val = CvtNumber (Opt, Arg);
154
155     /* Check for a valid range */
156     RangeCheck (Opt, Val, MIN_ACOL, MAX_ACOL);
157
158     /* Use the value */
159     ACol = (unsigned char) Val;
160 }
161
162
163
164 static void OptBytesPerLine (const char* Opt, const char* Arg)
165 /* Handle the --bytes-per-line option */
166 {
167     /* Convert the argument to a number */
168     unsigned long Val = CvtNumber (Opt, Arg);
169
170     /* Check for a valid range */
171     RangeCheck (Opt, Val, MIN_BYTESPERLINE, MAX_BYTESPERLINE);
172
173     /* Use the value */
174     BytesPerLine = (unsigned char) Val;
175 }
176
177
178
179 static void OptCommentColumn (const char* Opt, const char* Arg)
180 /* Handle the --comment-column option */
181 {
182     /* Convert the argument to a number */
183     unsigned long Val = CvtNumber (Opt, Arg);
184
185     /* Check for a valid range */
186     RangeCheck (Opt, Val, MIN_CCOL, MAX_CCOL);
187
188     /* Use the value */
189     CCol = (unsigned char) Val;
190 }
191
192
193
194 static void OptComments (const char* Opt, const char* Arg)
195 /* Handle the --comments option */
196 {
197     /* Convert the argument to a number */
198     unsigned long Val = CvtNumber (Opt, Arg);
199
200     /* Check for a valid range */
201     RangeCheck (Opt, Val, MIN_COMMENTS, MAX_COMMENTS);
202
203     /* Use the value */
204     Comments = (unsigned char) Val;
205 }
206
207
208
209 static void OptCPU (const char* Opt attribute ((unused)), const char* Arg)
210 /* Handle the --cpu option */
211 {
212     /* Find the CPU from the given name */
213     CPU = FindCPU (Arg);
214     SetOpcTable (CPU);
215 }
216
217
218
219 static void OptDebugInfo (const char* Opt attribute ((unused)),
220                           const char* Arg attribute ((unused)))
221 /* Add debug info to the object file */
222 {
223     DebugInfo = 1;
224 }
225
226
227
228 static void OptFormFeeds (const char* Opt attribute ((unused)),
229                           const char* Arg attribute ((unused)))
230 /* Add form feeds to the output */
231 {
232     FormFeeds = 1;
233 }
234
235
236
237 static void OptHelp (const char* Opt attribute ((unused)),
238                      const char* Arg attribute ((unused)))
239 /* Print usage information and exit */
240 {
241     Usage ();
242     exit (EXIT_SUCCESS);
243 }
244
245
246
247 static void OptHexOffs (const char* Opt attribute ((unused)),
248                         const char* Arg attribute ((unused)))
249 /* Handle the --hexoffs option */
250 {
251     UseHexOffs = 1;
252 }
253
254
255
256 static void OptInfo (const char* Opt attribute ((unused)), const char* Arg)
257 /* Handle the --info option */
258 {
259     InfoSetName (Arg);
260 }
261
262
263
264 static void OptLabelBreak (const char* Opt, const char* Arg)
265 /* Handle the --label-break option */
266 {
267     /* Convert the argument to a number */
268     unsigned long Val = CvtNumber (Opt, Arg);
269
270     /* Check for a valid range */
271     RangeCheck (Opt, Val, MIN_LABELBREAK, MAX_LABELBREAK);
272
273     /* Use the value */
274     LBreak = (unsigned char) Val;
275 }
276
277
278
279 static void OptMnemonicColumn (const char* Opt, const char* Arg)
280 /* Handle the --mnemonic-column option */
281 {
282     /* Convert the argument to a number */
283     unsigned long Val = CvtNumber (Opt, Arg);
284
285     /* Check for a valid range */
286     RangeCheck (Opt, Val, MIN_MCOL, MAX_MCOL);
287
288     /* Use the value */
289     MCol = (unsigned char) Val;
290 }
291
292
293
294 static void OptPageLength (const char* Opt attribute ((unused)), const char* Arg)
295 /* Handle the --pagelength option */
296 {
297     int Len = atoi (Arg);
298     if (Len != 0) {
299         RangeCheck (Opt, Len, MIN_PAGE_LEN, MAX_PAGE_LEN);
300     }
301     PageLength = Len;
302 }
303
304
305
306 static void OptStartAddr (const char* Opt, const char* Arg)
307 /* Set the default start address */
308 {
309     StartAddr = CvtNumber (Opt, Arg);
310 }
311
312
313
314 static void OptTextColumn (const char* Opt, const char* Arg)
315 /* Handle the --text-column option */
316 {
317     /* Convert the argument to a number */
318     unsigned long Val = CvtNumber (Opt, Arg);
319
320     /* Check for a valid range */
321     RangeCheck (Opt, Val, MIN_TCOL, MAX_TCOL);
322
323     /* Use the value */
324     TCol = (unsigned char) Val;
325 }
326
327
328
329 static void OptVerbose (const char* Opt attribute ((unused)),
330                         const char* Arg attribute ((unused)))
331 /* Increase verbosity */
332 {
333     ++Verbosity;
334 }
335
336
337
338 static void OptVersion (const char* Opt attribute ((unused)),
339                         const char* Arg attribute ((unused)))
340 /* Print the disassembler version */
341 {
342     fprintf (stderr,
343              "da65 V%s - (C) Copyright 2000-2009, Ullrich von Bassewitz\n",
344              GetVersionAsString ());  
345 }
346
347
348
349 static void OneOpcode (unsigned RemainingBytes)
350 /* Disassemble one opcode */
351 {
352     /* Get the opcode from the current address */
353     unsigned char OPC = GetCodeByte (PC);
354
355     /* Get the opcode description for the opcode byte */
356     const OpcDesc* D = &OpcTable[OPC];
357
358     /* Get the output style for the current PC */
359     attr_t Style = GetStyleAttr (PC);
360
361     /* If we have a label at this address, output the label and an attached
362      * comment, provided that we aren't in a skip area.
363      */
364     if (Style != atSkip && MustDefLabel (PC)) {
365         const char* Comment = GetComment (PC);
366         if (Comment) {
367             UserComment (Comment);
368         }
369         DefLabel (GetLabelName (PC));
370     }
371
372     /* Check...
373      *   - ...if we have enough bytes remaining for the code at this address.
374      *   - ...if the current instruction is valid for the given CPU.
375      *   - ...if there is no label somewhere between the instruction bytes.
376      * If any of these conditions is false, switch to data mode.
377      */
378     if (Style == atDefault) {
379         if (D->Size > RemainingBytes) {
380             Style = atIllegal;
381             MarkAddr (PC, Style);
382         } else if (D->Flags & flIllegal) {
383             Style = atIllegal;
384             MarkAddr (PC, Style);
385         } else {
386             unsigned I;
387             for (I = 1; I < D->Size; ++I) {
388                 if (HaveLabel (PC+I) || HaveSegmentChange (PC+I)) {
389                     Style = atIllegal;
390                     MarkAddr (PC, Style);
391                     break;
392                 }
393             }
394         }
395     }
396
397     /* Disassemble the line */
398     switch (Style) {
399
400         case atDefault:
401             D->Handler (D);
402             PC += D->Size;
403             break;
404
405         case atCode:
406             /* Beware: If we don't have enough bytes left to disassemble the
407              * following insn, fall through to byte mode.
408              */
409             if (D->Size <= RemainingBytes) {
410                 /* Output labels within the next insn */
411                 unsigned I;
412                 for (I = 1; I < D->Size; ++I) {
413                     ForwardLabel (I);
414                 }
415                 /* Output the insn */
416                 D->Handler (D);
417                 PC += D->Size;
418                 break;
419             }
420             /* FALLTHROUGH */
421
422         case atByteTab:
423             ByteTable ();
424             break;
425
426         case atDByteTab:
427             DByteTable ();
428             break;
429
430         case atWordTab:
431             WordTable ();
432             break;
433
434         case atDWordTab:
435             DWordTable ();
436             break;
437
438         case atAddrTab:
439             AddrTable ();
440             break;
441
442         case atRtsTab:
443             RtsTable ();
444             break;
445
446         case atTextTab:
447             TextTable ();
448             break;
449
450         case atSkip:
451             ++PC;
452             break;
453
454         default:
455             DataByteLine (1);
456             ++PC;
457             break;
458
459     }
460 }
461
462
463
464 static void OnePass (void)
465 /* Make one pass through the code */
466 {
467     unsigned Count;
468
469     /* Disassemble until nothing left */
470     while ((Count = GetRemainingBytes()) > 0) {
471         OneOpcode (Count);
472     }
473 }
474
475
476
477 static void Disassemble (void)
478 /* Disassemble the code */
479 {
480     /* Pass 1 */
481     Pass = 1;
482     OnePass ();
483
484     Output ("---------------------------");
485     LineFeed ();
486
487     /* Pass 2 */
488     Pass = 2;
489     ResetCode ();
490     OutputSettings ();
491     DefOutOfRangeLabels ();
492     OnePass ();
493 }
494
495
496
497 int main (int argc, char* argv [])
498 /* Assembler main program */
499 {
500     /* Program long options */
501     static const LongOpt OptTab[] = {
502         { "--argument-column",  1,      OptArgumentColumn       },
503         { "--bytes-per-line",   1,      OptBytesPerLine         },
504         { "--comment-column",   1,      OptCommentColumn        },
505         { "--comments",         1,      OptComments             },
506         { "--cpu",              1,      OptCPU                  },
507         { "--debug-info",       0,      OptDebugInfo            },
508         { "--formfeeds",        0,      OptFormFeeds            },
509         { "--help",             0,      OptHelp                 },
510         { "--hexoffs",          0,      OptHexOffs              },
511         { "--info",             1,      OptInfo                 },
512         { "--label-break",      1,      OptLabelBreak           },
513         { "--mnemonic-column",  1,      OptMnemonicColumn       },
514         { "--pagelength",       1,      OptPageLength           },
515         { "--start-addr",       1,      OptStartAddr            },
516         { "--text-column",      1,      OptTextColumn           },
517         { "--verbose",          0,      OptVerbose              },
518         { "--version",          0,      OptVersion              },
519     };
520
521     unsigned I;
522     time_t T;
523
524     /* Initialize the cmdline module */
525     InitCmdLine (&argc, &argv, "da65");
526
527     /* Check the parameters */
528     I = 1;
529     while (I < ArgCount) {
530
531         /* Get the argument */
532         const char* Arg = ArgVec[I];
533
534         /* Check for an option */
535         if (Arg [0] == '-') {
536             switch (Arg [1]) {
537
538                 case '-':
539                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
540                     break;
541
542                 case 'g':
543                     OptDebugInfo (Arg, 0);
544                     break;
545
546                 case 'h':
547                     OptHelp (Arg, 0);
548                     break;
549
550                 case 'i':
551                     OptInfo (Arg, GetArg (&I, 2));
552                     break;
553
554                 case 'o':
555                     OutFile = GetArg (&I, 2);
556                     break;
557
558                 case 'v':
559                     OptVerbose (Arg, 0);
560                     break;
561
562                 case 'S':
563                     OptStartAddr (Arg, GetArg (&I, 2));
564                     break;
565
566                 case 'V':
567                     OptVersion (Arg, 0);
568                     break;
569
570                 default:
571                     UnknownOption (Arg);
572                     break;
573
574             }
575         } else {
576             /* Filename. Check if we already had one */
577             if (InFile) {
578                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
579                          ProgName, Arg);
580                 exit (EXIT_FAILURE);
581             } else {
582                 InFile = Arg;
583             }
584         }
585
586         /* Next argument */
587         ++I;
588     }
589
590     /* Try to read the info file */
591     ReadInfoFile ();
592
593     /* Must have an input file */
594     if (InFile == 0) {
595         AbEnd ("No input file");
596     }
597
598     /* Check the formatting options for reasonable values. Note: We will not
599      * really check that they make sense, just that they aren't complete
600      * garbage.
601      */
602     if (MCol >= ACol) {
603         AbEnd ("mnemonic-column value must be smaller than argument-column value");
604     }
605     if (ACol >= CCol) {
606         AbEnd ("argument-column value must be smaller than comment-column value");
607     }
608     if (CCol >= TCol) {
609         AbEnd ("comment-column value must be smaller than text-column value");
610     }
611
612     /* If no CPU given, use the default CPU */
613     if (CPU == CPU_UNKNOWN) {
614         CPU = CPU_6502;
615     }
616
617     /* Get the current time and convert it to string so it can be used in
618      * the output page headers.
619      */
620     T = time (0);
621     strftime (Now, sizeof (Now), "%Y-%m-%d %H:%M:%S", localtime (&T));
622
623     /* Load the input file */
624     LoadCode ();
625
626     /* Open the output file */
627     OpenOutput (OutFile);
628
629     /* Disassemble the code */
630     Disassemble ();
631
632     /* Close the output file */
633     CloseOutput ();
634
635     /* Done */
636     return EXIT_SUCCESS;
637 }
638
639
640