]> git.sur5r.net Git - cc65/blob - src/da65/main.c
Added INPUTOFFS and INPUTSIZE
[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     /* If we have a label at this address, output the label */
242     if (MustDefLabel (PC)) {
243         DefLabel (GetLabel (PC));
244     }
245
246     /* Check...
247      *   - ...if we have enough bytes remaining for the code at this address.
248      *   - ...if the current instruction is valid for the given CPU.
249      *   - ...if there is no label somewhere between the instruction bytes.
250      * If any of these conditions is false, switch to data mode.
251      */
252     if (GetStyleAttr (PC) == atDefault) {
253         if (D->Size > RemainingBytes) {
254             MarkAddr (PC, atIllegal);
255         } else if (D->Flags & flIllegal) {
256             MarkAddr (PC, atIllegal);
257         } else {
258             unsigned I;
259             for (I = 1; I < D->Size; ++I) {
260                 if (HaveLabel (PC+I)) {
261                     MarkAddr (PC, atIllegal);
262                     break;
263                 }
264             }
265         }
266     }
267
268     /* Disassemble the line */
269     switch (GetStyleAttr (PC)) {
270
271         case atDefault:
272         case atCode:
273             D->Handler (D);
274             PC += D->Size;
275             break;
276
277         case atByteTab:
278             ByteTable ();
279             break;
280
281         case atDByteTab:
282             DByteTable ();
283             break;
284
285         case atWordTab:
286             WordTable ();
287             break;
288
289         case atDWordTab:
290             DWordTable ();
291             break;
292
293         case atAddrTab:
294             AddrTable ();
295             break;
296
297         case atRtsTab:
298             RtsTable ();
299             break;
300
301         case atTextTab:
302             TextTable ();
303             break;
304
305         default:
306             DataByteLine (1);
307             ++PC;
308             break;
309
310     }
311 }
312
313
314
315 static void OnePass (void)
316 /* Make one pass through the code */
317 {
318     unsigned Count;
319
320     /* Disassemble until nothing left */
321     while ((Count = GetRemainingBytes()) > 0) {
322         OneOpcode (Count);
323     }
324 }
325
326
327
328 static void Disassemble (void)
329 /* Disassemble the code */
330 {
331     /* Pass 1 */
332     Pass = 1;
333     OnePass ();
334
335     Output ("---------------------------");
336     LineFeed ();
337
338     /* Pass 2 */
339     Pass = 2;
340     ResetCode ();
341     OutputSettings ();
342     DefOutOfRangeLabels ();
343     OnePass ();
344 }
345
346
347
348 int main (int argc, char* argv [])
349 /* Assembler main program */
350 {
351     /* Program long options */
352     static const LongOpt OptTab[] = {
353         { "--comments",         1,      OptComments             },
354         { "--cpu",              1,      OptCPU                  },
355         { "--debug-info",       0,      OptDebugInfo            },
356         { "--formfeeds",        0,      OptFormFeeds            },
357         { "--help",             0,      OptHelp                 },
358         { "--info",             1,      OptInfo                 },
359         { "--pagelength",       1,      OptPageLength           },
360         { "--start-addr",       1,      OptStartAddr            },
361         { "--verbose",          0,      OptVerbose              },
362         { "--version",          0,      OptVersion              },
363     };
364
365     unsigned I;
366
367     /* Initialize the cmdline module */
368     InitCmdLine (&argc, &argv, "da65");
369
370     /* Check the parameters */
371     I = 1;
372     while (I < ArgCount) {
373
374         /* Get the argument */
375         const char* Arg = ArgVec[I];
376
377         /* Check for an option */
378         if (Arg [0] == '-') {
379             switch (Arg [1]) {
380
381                 case '-':
382                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
383                     break;
384
385                 case 'g':
386                     OptDebugInfo (Arg, 0);
387                     break;
388
389                 case 'h':
390                     OptHelp (Arg, 0);
391                     break;
392
393                 case 'i':
394                     OptInfo (Arg, GetArg (&I, 2));
395                     break;
396
397                 case 'o':
398                     OutFile = GetArg (&I, 2);
399                     break;
400
401                 case 'v':
402                     OptVerbose (Arg, 0);
403                     break;
404
405                 case 'S':
406                     OptStartAddr (Arg, GetArg (&I, 2));
407                     break;
408
409                 case 'V':
410                     OptVersion (Arg, 0);
411                     break;
412
413                 default:
414                     UnknownOption (Arg);
415                     break;
416
417             }
418         } else {
419             /* Filename. Check if we already had one */
420             if (InFile) {
421                 fprintf (stderr, "%s: Don't know what to do with `%s'\n",
422                          ProgName, Arg);
423                 exit (EXIT_FAILURE);
424             } else {
425                 InFile = Arg;
426             }
427         }
428
429         /* Next argument */
430         ++I;
431     }
432
433     /* Try to read the info file */
434     ReadInfoFile ();
435
436     /* Must have an input file */
437     if (InFile == 0) {
438         AbEnd ("No input file");
439     }
440
441     /* Make the output file name from the input file name if none was given */
442     if (OutFile == 0) {
443         OutFile = MakeFilename (InFile, OutExt);
444     }
445
446     /* If no CPU given, use the default CPU */
447     if (CPU == CPU_UNKNOWN) {
448         CPU = CPU_6502;
449     }
450
451     /* Load the input file */
452     LoadCode ();
453
454     /* Open the output file */
455     OpenOutput (OutFile);
456
457     /* Disassemble the code */
458     Disassemble ();
459
460     /* Close the output file */
461     CloseOutput ();
462
463     /* Done */
464     return EXIT_SUCCESS;
465 }
466
467
468