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