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